Skip to content

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

Open
@dcharkes

Description

@dcharkes

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

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