Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
WebAssembly in Dart for web example
<!DOCTYPE html>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="scaffolded-by" content="" />
<link rel="stylesheet" href="styles.css" />
<link rel="icon" href="favicon.ico" />
<script defer src="main.dart.js"></script>
// web/main.dart
import 'dart:html';
import 'package:js/js_util.dart';
import './wa_interop.dart';
void main() async {
var request = await HttpRequest.request(
responseType: 'arraybuffer',
mimeType: 'application/wasm',
var wa = await instantiate(
'imports': {
'imported_func': (args) => window.console.log(args),
wa.then((results) {
var exportsObj = getProperty(results.instance, 'exports');
Function exportedFn = getProperty(exportsObj, 'exported_func');
;; web/simple.wasm
(type $t0 (func (param i32)))
(type $t1 (func))
(import "imports" "imported_func" (func $imports.imported_func (type $t0)))
(func $exported_func (type $t1)
i32.const 42
call $imports.imported_func)
(export "exported_func" (func $exported_func)))
// web/wa_interop.dart
library wasm_interop;
import 'dart:html';
import 'package:js/js.dart';
import 'package:js/js_util.dart';
external instantiate(bytes, dynamic importObj);
jsObj(Map<String, dynamic> dartMap) {
var jsObject = newObject();
dartMap.forEach((name, value) {
if (value is Map<String, dynamic>) {
setProperty(jsObject, name, jsObj(value));
} else {
setProperty(jsObject, name, value);
return jsObject;

This comment has been minimized.

Copy link
Owner Author

@graphicbeacon graphicbeacon commented Apr 27, 2019

This uses the js package for interop with WebAssembly's instantiate() method.

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