Skip to content

[vm/ffi] Breaking change: Throw errors when returned as Dart_Handle #49936

@dcharkes

Description

@dcharkes

The following snippet throws an error instead of returning an error object:

Dart_Handle DartRuntimeHooks::GetCallbackFromHandle(int64_t handle) {
  Dart_Handle result = DartCallbackCache::GetCallback(handle);
  if (Dart_IsError(result)) {
    Dart_PropagateError(result);
  }
  return result;
}

(Example combined from flutter/flutter#111243, flutter/engine#36032, and flutter_engine/lib/ui/dart_runtime_hooks.cc.)

We should consider automatically throwing when Dart_IsError is true so that this pattern would not be needed.

Dart_IsError is defined in terms of a number of predefined class ids:

  • Error
  • ApiError
  • LanguageError
  • UnhandledError
  • UnwindError

Throwing these would make it impossible to return objects with these types through dart:ffi. (If we ever want to do that, we could wrap the object in another object. Or if we're really concerned about not allocating another object, we could add another boolean flag to @FfiNative and asFunction.)

This is a breaking change, as code which currently returns these 5 objects types by Dart_Handle in dart:ffi will start throwing instead of returning the error object, requiring a try-catch as migration. However, I believe it might be unlikely that many users are relying on this.

cc @mraleph @dnfield @mkustermann

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-ffi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions