Skip to content

[vm/ffi/NNBD] treatment of non-nullable and nullable C types #40606

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

Open
dcharkes opened this issue Feb 12, 2020 · 3 comments
Open

[vm/ffi/NNBD] treatment of non-nullable and nullable C types #40606

dcharkes opened this issue Feb 12, 2020 · 3 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi

Comments

@dcharkes
Copy link
Contributor

Currently, our representation of C types does not encode whether something can be null, and all types are represented as non-nullable.

Now that Dart will get NNBD, we might want to revisit that.

void MyCoolFunction(Foo& foo) {
  // ...
}
final f = dynamicLibrary.lookupFunction<Void Function(Pointer<Foo>),
                                        void Function(Pointer<Foo>)>("MyCoolFunction");
f(nullptr); // Passing null, without the Dart type system understanding it.

If we would like to represent the difference between references (non-nullable) and pointers (nullable), we could do so in the following way:

Pointer<Int8>  p1;  // non-nullable, a C++ Reference
Pointer<Int8>? p2;  // nullable,     a C++ Pointer

Then, we should also change the representation of the nullptr back to a Dart null instead of Pointer<Never>(address=0).

However, this also requires us to take real care that everywhere where we currently return Pointers with address 0, we return null instead:

  • return values of FFI calls
  • arguments of FFI callbacks
  • loads from Pointer<Pointer<T>>.
  • struct field loads.

Year old discussions (before NNBD): #35756.

cc @mkustermann @mraleph

@dcharkes dcharkes added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi NNBD Issues related to NNBD Release labels Feb 12, 2020
@a-siva
Copy link
Contributor

a-siva commented Jul 8, 2020

@dcharkes do we want this to be done for the beta release of the null-safety feature ?

@mkustermann
Copy link
Member

@dcharkes do we want this to be done for the beta release of the null-safety feature ?

@a-siva I think this is rather a FFI design question how to represent a C pointer that is nullptr in Dart. Is not related/blocking to the NNBD work itself.

@mkustermann mkustermann removed the NNBD Issues related to NNBD Release label Jul 28, 2020
@mit-mit
Copy link
Member

mit-mit commented Sep 1, 2020

Removing from null safety tracking epic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi
Projects
None yet
Development

No branches or pull requests

4 participants