-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Allow access to global variables in ffi static binding #50551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Possible syntax: // Global var in native asset, similar to `@Native external` function.
@NativeSymbol(/* optional symbol and asset */); // No type argument, as the Pointer already has it.
external Pointer<Uint8> get greeting; |
Is someone currently working on this? I need this feature to use native assets, so I'm happy to give this a shot myself otherwise.
Given that @NativeSymbol<Uint8>(/* optional symbol and asset */);
external int greeting; // Field loads and assignments lowered to calls on the pointer. A downside is that the #50552 might need special consideration (but detecting |
The way that this should be implemented is going to be https://dart-review.googlesource.com/c/sdk/+/284300 completely changes how the |
We should be able to merge both functions and non-function symbols: // Global var in native asset, similar to `@Native external` function.
@Native<Int64>(/* optional symbol and asset */);
external int get greeting;
external set greeting(int value);
final greetingAddress = addressOf<Int64>(greeting); // Unfortunately no inference, requires extra custom check. |
cc @lrhn for any bright API ideas. We could possibly make the global vars nicer by exposing the |
I have seen existing code using We could make a distinction by saying that getters/setters are functions in C, whereas annotated fields would be symbols. Or we just break the existing (probably rare?) usages of getters linking functions. |
We could probably make both nullary functions and non-functions getters (and unary functions and non functions setters). |
The closer the syntax is to how functions are accessed, the better. @Native<Int64>(/* optional symbol and asset */);
external int greeting; seems optimal, if If the variable declaration is We can, and probably should, deliberately allow variables to be treated as nullary getter functions/unary setter functions in Dart, and vice versa. @Native<Int64 Function()>("something.getFoo")
external int get foo;
@Native<Void Function(Int64)>("something.setFoo")
external set foo(int value);
@Native<Int64>("something.foo") // native variable reference, reading method signature.
external int fetchFoo();
@Native<Int64>("something.foo") // same native variable reference, writing method signature.
external void pushFoo(int value); Nothing more complicated than this, if at all possible. (Don't know how we deal with immutable variables, but I guess writing to immutable memory will crash you through a setter the same way it would writing through a pointer. If we can somehow detect that the variable is not writable, we can just give a better error message.) |
I think in FFIgen we will just not generate a setter if we can detect that something is |
|
The only way I found is to use the type spelling ( |
I'm still happy to work on this now that e16bb21 has landed, I just want to make sure I understand the changes correctly. @dcharkes mentioned it may be easier to implement this and const a = Native<Int64>(...);
// input:
@a
external int foo;
// transformed:
int get foo {
final ptr = ffi._addressOfAnnotation(a);
return ffi._loadInt32(ptr);
}
set foo(int value) {
final ptr = ffi._addressOfAnnotation(a);
ffi._storeInt32(ptr, value);
} Or should a field be lowered into an external getter/setter pair with the body constructed in the VM, similar to what's now being done with |
We could probably lower the fields as you propose, I believe then the optimizer should optimize the P.S. Unfortunately we had to revert e16bb21. |
I've started working on this in https://dart-review.googlesource.com/c/sdk/+/338020. |
@Native<Int64>(/* optional symbol and asset */);
external int greeting; One strange thing about using the re-existing Though |
We're moving towards a direction where all native symbols will be declaratively specified. We have support for declaratively specifying native C functions (see #43889).
Though we'd also want a way to declaratively specify global variables - which can then be accessed from Dart.
The text was updated successfully, but these errors were encountered: