Generate function wrapper to take `Struct` instead of `Pointer<Struct>` and pass `address` property to native function inside it
Background
I use address property of the Struct class to pass a pointer to a struct to native function when the function is marked "Leaf", like:
C
struct version {
uint16_t major;
uint16_t minor;
}
void library_version(version* v);
Dart
final class version extends ffi.Struct {
@ffi.Uint16()
external int major;
@ffi.Uint16()
external int minor;
}
@ffi.Native<ffi.Void Function(ffi.Pointer<version>)>(isLeaf: true)
external void library_version(ffi.Pointer<version> ver);
final ver = Struct.create<version>();
library_version(ver.address);
but I'm unable to pass a pointer when the native function is wrapped in the generated code (by ffigen) because the wrapper is not marked as a "Leaf" (and it actually can't be marked) and there is no way to pass address property that requires the function to be marked "Leaf":
@ffi.Native<ffi.Void Function(ffi.Pointer<version>)>(symbol: 'library_version', isLeaf: true)
external void _library_version(ffi.Pointer<version> ver);
void library_version(ffi.Pointer<version> ver) => _library_version(ver);
...
library_version(ver.address); // ERROR: The '.address' expression can only be used as argument to a leaf native external call.dart(address_position)
Proposal
Generate the wrapper to take structures instead of pointer to structures and pass address property inside it:
@ffi.Native<ffi.Void Function(ffi.Pointer<version>)>(symbol: 'library_version', isLeaf: true)
external void _library_version(ffi.Pointer<version> ver);
void library_version(version ver) => _library_version(ver.address);
...
library_version(ver); //OK
Pointers in C can represent single objects or arrays. This proposal assumes that the pointer represents a single object. If we were going to do this, it would have to be disabled by default, and enabled on a case by case basis for each function, maybe using regex filtering similar to the include/exclude rules.
Interesting, because we have the extra potential unwrapping of ObjC types, we can't have the .address.
@liamappelbe Could we make the generator not have the forwarding function if no unwrapping is happening?
@ffi.Native<ffi.Void Function(ffi.Pointer<version>)>(symbol: 'library_version', isLeaf: true)
external void _library_version(ffi.Pointer<version> ver);
void library_version(ffi.Pointer<version> ver) => _library_version(ver);
->
@ffi.Native<ffi.Void Function(ffi.Pointer<version>)>(symbol: 'library_version', isLeaf: true)
external void library_version(ffi.Pointer<version> ver);
That would solve it for functions that have only C types.
My hunch is that if you have ObjC types in your signature, then probably the function is more heavy-weight, and that it doesn't matter that much if it's leaf or not.
(Maybe my hunch is not correct, then we can see what we want to do in that case. I wonder if it would be good to have a .unwrap() for every ObjC wrapper type and then expose a library_version_raw_leaf toggled by a flag. This would work for arbitrary signatures as long as we have an .unwrap() method for all wrapped types. This would avoid workarounds per FFI type as the pointer->struct workaround proposed here. And we probably don't have a possible workaround for every type. E.g. there's no workaround for an int-pointer.)
Could we make the generator not have the forwarding function if no unwrapping is happening?
Yep, that should solve the problem in most cases.
@liamappelbe @dcharkes, I just want to clarify that the wrapper function is generated when enum as a return value takes place:
@ffi.Native<ffi.Int Function(ffi.Pointer<version>)>(symbol: 'library_version', isLeaf: true)
external int _library_version(ffi.Pointer<version> ver);
code library_version(ffi.Pointer<version> ver) => code.fromValue(
_library_version(ver),
);
I didn't mention that because this is something I am OK and is not related to my root concern.
BTW, please note that, sometimes, comma is generated after the inner call in fromValue in cases when ); happens on the next line.
I see. In that case indeed there will still be a wrapper function.
Then I think we should explore my second option, exposing the underlying external
@ffi.Native<ffi.Int Function(ffi.Pointer<version>)>(symbol: 'library_version', isLeaf: true)
external int library_version_unwrapped(ffi.Pointer<version> ver);
And then you can write your own wrapper function:
code library_version(version ver) => code.fromValue(
library_version_unwrapped(ver.address),
);
@liamappelbe What is the list of things that we wrap/unwrap? enums, ObjC-objects, blocks, .. ? Can users write the wrapper for all of those themselves?
We should probably not also generate the wrapped version then. I'm assuming that the users will then write their own code to wrap the unwrapped call.
(This also suspiciously sounds like something that we could do in transformations cc @HosseinYousefi. But maybe that's overkill in this case because simply wrapping a call in another is easy enough to do.)
And then you can write your own wrapper function:
code library_version(version ver) => code.fromValue( library_version_unwrapped(ver.address), );
This would work for me well.
I also discovered a Dart compiler crash while writing a wrapper as written above. When the native function starts with "_" (private) it causes a crash. But when I remove prepending underscore from "_library_version" to "library_version" then it compiles successfully.
Error log:
Launching lib\main.dart on Windows in debug mode...
Unhandled exception:
Crash when compiling:
Null check operator used on a null value
#0 new Name.byReference (package:kernel/src/ast/names.dart:40)
#1 new Name (package:kernel/src/ast/names.dart:33)
#2 _FfiUseSiteTransformer._replaceNativeCall (package:vm/modular/transformations/ffi/use_sites.dart:1939)
#3 _FfiUseSiteTransformer._visitStaticInvocation (package:vm/modular/transformations/ffi/use_sites.dart:670)
#4 _FfiUseSiteTransformer.visitStaticInvocation (package:vm/modular/transformations/ffi/use_sites.dart:239)
#5 StaticInvocation.accept (package:kernel/src/ast/expressions.dart:2602)
#6 Transformer.transform (package:kernel/visitor.dart:1771)
#7 Transformer.transformList (package:kernel/visitor.dart:1788)
#8 Arguments.transformChildren (package:kernel/src/ast/expressions.dart:1429)
#9 Transformer.defaultTreeNode (package:kernel/visitor.dart:1805)
#10 TreeVisitorDefault.defaultExpression (package:kernel/visitor.dart:618)
#11 Arguments.accept (package:kernel/src/ast/expressions.dart:1414)
#12 Transformer.transform (package:kernel/visitor.dart:1771)
#13 ConstructorInvocation.transformChildren (package:kernel/src/ast/expressions.dart:2700)
#14 Transformer.defaultTreeNode (package:kernel/visitor.dart:1805)
#15 TreeVisitorDefault.defaultExpression (package:kernel/visitor.dart:618)
#16 ExpressionVisitorDefaultMixin.visitInstanceInvocation (package:kernel/visitor.dart:136)
#17 _FfiUseSiteTransformer.visitStaticInvocation (package:vm/modular/transformations/ffi/use_sites.dart:241)
#18 StaticInvocation.accept (package:kernel/src/ast/expressions.dart:2602)
#19 Transformer.transform (package:kernel/visitor.dart:1771)
#20 ReturnStatement.transformChildren (package:kernel/src/ast/statements.dart:1090)
#21 Transformer.defaultTreeNode (package:kernel/visitor.dart:1805)
#22 TreeVisitorDefault.defaultExpression (package:kernel/visitor.dart:618)
#23 StatementVisitorDefaultMixin.visitIfCaseStatement (package:kernel/visitor.dart:375)
#24 FinalizableTransformer.visitReturnStatement (package:vm/modular/transformations/ffi/finalizable.dart:341)
#25 ReturnStatement.accept (package:kernel/src/ast/statements.dart:1076)
#26 Transformer.transform (package:kernel/visitor.dart:1771)
#27 FunctionNode.transformChildren (package:kernel/src/ast/functions.dart:247)
#28 Transformer.defaultTreeNode (package:kernel/visitor.dart:1805)
#29 TreeVisitorDefault.defaultExpression (package:kernel/visitor.dart:618)
#30 FunctionNode.accept (package:kernel/src/ast/functions.dart:214)
#31 Transformer.transform (package:kernel/visitor.dart:1771)
#32 Procedure.transformChildren (package:kernel/src/ast/members.dart:1218)
#33 Transformer.defaultTreeNode (package:kernel/visitor.dart:1805)
#34 TreeVisitorDefault.defaultExpression (package:kernel/visitor.dart:618)
#35 MemberVisitorDefaultMixin.visitField (package:kernel/visitor.dart:413)
#36 _FfiUseSiteTransformer.visitProcedure (package:vm/modular/transformations/ffi/use_sites.dart:232)
#37 FinalizableTransformer.visitProcedure.<anonymous closure> (package:vm/modular/transformations/ffi/finalizable.dart:155)
#38 FinalizableTransformer.inScope (package:vm/modular/transformations/ffi/finalizable.dart:60)
#39 FinalizableTransformer.visitProcedure (package:vm/modular/transformations/ffi/finalizable.dart:153)
#40 Procedure.accept (package:kernel/src/ast/members.dart:1199)
#41 Transformer.transform (package:kernel/visitor.dart:1771)
#42 Transformer.transformList (package:kernel/visitor.dart:1788)
#43 Library.transformChildren (package:kernel/src/ast/libraries.dart:320)
#44 Transformer.defaultTreeNode (package:kernel/visitor.dart:1805)
#45 TreeVisitorDefault.defaultExpression (package:kernel/visitor.dart:618)
#46 FfiTransformer.visitLibrary (package:vm/modular/transformations/ffi/common.dart:1027)
#47 _FfiUseSiteTransformer.visitLibrary (package:vm/modular/transformations/ffi/use_sites.dart:133)
#48 List.forEach (dart:core-patch/growable_array.dart:425)
#49 transformLibraries (package:vm/modular/transformations/ffi/use_sites.dart:80)
#50 VmTarget.performModularTransformationsOnLibraries (package:vm/modular/target/vm.dart:226)
#51 KernelTarget.runBuildTransformations (package:front_end/src/kernel/kernel_target.dart:1542)
#52 KernelTarget.buildComponent.<anonymous closure> (package:front_end/src/kernel/kernel_target.dart:704)
<asynchronous suspension>
#53 withCrashReporting (package:front_end/src/base/crash.dart:139)
<asynchronous suspension>
#54 KernelTarget.buildComponent (package:front_end/src/kernel/kernel_target.dart:645)
<asynchronous suspension>
#55 IncrementalCompiler.computeDelta.<anonymous closure> (package:front_end/src/base/incremental_compiler.dart:377)
<asynchronous suspension>
#56 CompilerContext.clear (package:front_end/src/base/compiler_context.dart:77)
<asynchronous suspension>
#57 IncrementalCompiler.compile (package:vm/incremental_compiler.dart:86)
<asynchronous suspension>
#58 FrontendCompiler.compile (package:frontend_server/frontend_server.dart:645)
<asynchronous suspension>
#59 listenAndCompile.<anonymous closure> (package:frontend_server/frontend_server.dart:1403)
<asynchronous suspension>
2
the Dart compiler exited unexpectedly.
Exited (1).
@liamappelbe What is the list of things that we wrap/unwrap? enums, ObjC-objects, blocks, .. ? Can users write the wrapper for all of those themselves?
Yeah, it's already possible to wrap and unwrap those objects.
The idea of optionally generating a raw version of these functions is fine, but I'm hesitant to add support for it to the yaml config. It would mean a whole extra set of regex based include/exclude rules for functions. And presumably we'd also want this for ObjC interfaces, categories, and protocols. I think it'd be better to make this exclusive to the Dart API, using transformations. That would make the change pretty simple.
I also discovered a Dart compiler crash while writing a wrapper as written above.
@DenisDvlp Can you file a separate issue in https://github.com/dart-lang/sdk/issues for the compiler crash?
I think it'd be better to make this exclusive to the Dart API, using transformations. That would make the change pretty simple.
That SGTM, but then we need to to consider a YAML->Dart-config pretty printer for easy migration.
@liamappelbe
@DenisDvlp Can you file a separate issue in https://github.com/dart-lang/sdk/issues for the compiler crash?
Done https://github.com/dart-lang/sdk/issues/61321