Skip to content
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

[ffi] Convert allocators to use @Native #1401

Closed
dcharkes opened this issue Aug 7, 2024 · 1 comment · Fixed by #1404
Closed

[ffi] Convert allocators to use @Native #1401

dcharkes opened this issue Aug 7, 2024 · 1 comment · Fixed by #1404

Comments

@dcharkes
Copy link
Collaborator

dcharkes commented Aug 7, 2024

This would make them compatible with dart2wasm if that exports the right symbols:

@ffi.Native<ffi.Pointer<ffi.Void> Function(ffi.Int)>(symbol: 'malloc')
external ffi.Pointer<ffi.Void> _wasmAllocate(
  int size,
);

@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>)>(symbol: 'free')
external void _wasmDeallocate(
  ffi.Pointer<ffi.Void> ptr,
);

class _WasmAllocator implements ffi.Allocator {
  @override
  ffi.Pointer<T> allocate<T extends ffi.NativeType>(
    int byteCount, {
    int? alignment,
  }) {
    return _wasmAllocate(byteCount).cast<T>();
  }

  @override
  void free(ffi.Pointer<ffi.NativeType> pointer) {
    _wasmDeallocate(pointer.cast());
  }
}

final calloc = _WasmAllocator();

Thanks @TheComputerM!

@sam-p-at-pieces
Copy link

@dcharkes Not sure exactly how this works but I'm running into a compilation issue when build for web with flutter build web --wasm:

Target dart2wasm failed: ProcessException: Process exited abnormally with exit code 64:
../../../.pub-cache/hosted/pub.dev/ffi-2.1.3/lib/src/allocation.dart:24:22: Error: Argument to 'Native.addressOf' must be annotated with @Native.
    Native.addressOf(posixFree);

Flutter Doctor output:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, 3.23.0-0.1.pre, on macOS 14.5 23F79 darwin-arm64 (Rosetta), locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.3)
[✓] IntelliJ IDEA Ultimate Edition (version 2024.1.2)
[✓] VS Code (version 1.92.0)
[✓] Connected device (4 available)
[✓] Network resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants