wasm_ffi 2.3.0 copy "wasm_ffi: ^2.3.0" to clipboard
wasm_ffi: ^2.3.0 copied to clipboard

Platformweb

A wrapper for wasm which can be used as a drop-in replacement for dart:ffi for Web platform.

example/README.md

wasm_ffi Example #

This directory contains a complete example of using wasm_ffi to port the opus_dart library to the web.

It demonstrates:

  • Compiling a C library (libopus) to WebAssembly using Emscripten.
  • Using ffigen with a proxy file to generate bindings.
  • Setting up the necessary boilerplate to initialize wasm_ffi in a vanilla Dart web app.

Project Structure #

  • bin/: Contains the main entry point for the vanilla Dart app.
  • lib/src/: Contains the specific binding code (generated.dart), the proxy (proxy_ffi.dart), and initialization logic.
  • web/: Contains the HTML and build artifacts for the web.

Building the C Library #

We use Docker to ensure a consistent build environment for the libopus library.

  1. Build the Docker image: The generic Dockerfile is available in the root of the example.

  2. Compile to WASM: Detailed instructions and the Makefile are in the Makefile (if present) or described in the original tutorial. > Note: The pre-compiled libopus.js and libopus.wasm are already included in the web/ directory for convenience.

    To rebuild them yourself using the Docker method from the original tutorial:

    docker build -t wasm_ffi_opus_builder .
    docker run --rm -v $(pwd):/src wasm_ffi_opus_builder
    

    (Adjust the commands based on the actual Dockerfile logic if you want to reproduce the build exactly).

Running the Example #

  1. Compile Dart to JavaScript: We use dart2js to compile the Dart application code.

    dart compile js ./bin/main.dart -o ./web/main.dart.js
    
  2. Serve the Web Application: You need a simple HTTP server to serve the web directory. We recommend dhttpd.

    Install dhttpd if you haven't:

    dart pub global activate dhttpd
    

    Run the server:

    cd web
    dart pub global run dhttpd -p 8080
    
  3. Open in Browser: Navigate to http://localhost:8080 in your browser. Open the Developer Console (F12) to see the output. You should see the Opus version string printed (e.g., libopus 1.3.1).

Learn More #

For a detailed explanation of the concepts used here, including how proxy_ffi.dart works and how to support both Native and Web platforms, please refer to the main wasm_ffi README.

14
likes
160
points
4.11k
downloads

Publisher

verified publishervm75.duckdns.org

Weekly Downloads

A wrapper for wasm which can be used as a drop-in replacement for dart:ffi for Web platform.

Repository (GitHub)
View/report issues

Topics

#ffi #wasm #native #web

Documentation

API reference

License

BSD-2-Clause (license)

Dependencies

ffi, http, meta, path, web

More

Packages that depend on wasm_ffi