apollovm_wasm 1.0.0 copy "apollovm_wasm: ^1.0.0" to clipboard
apollovm_wasm: ^1.0.0 copied to clipboard

Native (Dart VM) WebAssembly execution for ApolloVM: the wasm_run-backed WasmRuntime, kept out of the core package so apollovm stays FFI-free.

ApolloVM Wasm #

pub package Null Safety Codecov Dart CI GitHub Tag New Commits Last Commits Pull Requests Code size License

Native (Dart VM) WebAssembly execution for ApolloVM.

ApolloVM compiles its AST to Wasm on the fly, on every platform, without any third-party toolchain. Running a compiled module is what needs an engine: browsers ship one, the Dart VM does not. This package supplies the VM engine, backed by wasm_run.


Why a Separate Package? #

The native engine brings an FFI/Rust toolchain with it (wasm_runflutter_rust_bridge). Keeping it here means package:apollovm — widely used only to parse, translate and generate code — never drags that cost, nor its dependency constraints, into consumers that never execute Wasm.

apollovm apollovm + apollovm_wasm
Parse / translate / generate
Compile to Wasm
Execute Wasm in the browser
Execute Wasm on the Dart VM
Native / FFI toolchain pulled in no yes

Without this package, WasmRuntime() on the Dart VM reports isSupported == false: Wasm still compiles, it just cannot be executed.


Usage #

Add both packages:

dependencies:
  apollovm: ^2.0.0
  apollovm_wasm: ^1.0.0

Install the native library once (it is downloaded, not built):

dart run wasm_run:setup

Register the runtime, and WasmRuntime() returns an engine that executes:

import 'package:apollovm/apollovm.dart';
import 'package:apollovm_wasm/apollovm_wasm.dart';

void main() {
  registerApolloVMWasmRuntime();

  var runtime = WasmRuntime()..ensureBooted();
  print(runtime.isSupported); // true
}

Compiling Dart to Wasm, then executing it #

import 'package:apollovm/apollovm.dart';
import 'package:apollovm_wasm/apollovm_wasm.dart';

void main() async {
  registerApolloVMWasmRuntime();

  // Compile: this half is core `apollovm`.
  var vm = ApolloVM();

  await vm.loadCodeUnit(SourceCodeUnit('dart', r'''
  
    int sumTo(int limit) {
      int sum = 0;
      for (int i = 1; i <= limit; ++i) {
        sum += i;
      }
      return sum;
    }
    
  ''', id: 'test'));

  var storageWasm = vm.generateAllIn<BytesOutput>('wasm');
  var wasmModules = await storageWasm.allEntries();
  var wasmBytes = wasmModules.values.first.values.first.output();

  // Execute: this half is what `apollovm_wasm` makes possible on the VM.
  var wasmVM = ApolloVM();

  await wasmVM.loadCodeUnit(
      BinaryCodeUnit('wasm', wasmBytes, id: 'test.wasm', namespace: ''));

  var runner = wasmVM.createRunner('wasm')!;

  var result =
      await runner.executeFunction('', 'sumTo', positionalParameters: [10]);

  print(result.getValueNoContext()); // 55
}

WebAssembly GC #

The wasm_run backend (wasmtime 14 / wasmi 0.31) does not implement the WebAssembly GC proposal. Modules that rely on it run in the browser (Chrome 119+), not on this runtime.

See Also #

  • apollovm — the VM itself: parsing, translation, execution and Wasm compilation.
  • wasm_run — the native Wasm engine this package binds to.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Author #

Graciliano M. Passos: gmpassos@GitHub.

Don't be shy, show some love, and become our GitHub Sponsor. Your support means the world to us, and it keeps the code caffeinated! ☕✨

Thanks a million! 🚀😄

License #

Apache License - Version 2.0

0
likes
150
points
65
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Native (Dart VM) WebAssembly execution for ApolloVM: the wasm_run-backed WasmRuntime, kept out of the core package so apollovm stays FFI-free.

Repository (GitHub)
View/report issues

Topics

#vm #wasm #webassembly #compiler

License

Apache-2.0 (license)

Dependencies

apollovm, crypto, path, wasm_run

More

Packages that depend on apollovm_wasm