-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Make it easier to run dart compiled to wasm in node.js #55744
Comments
This is the same issue described in #55412. I.e. you need to pass a
Pragmas starting with You should use js_interop: https://dart.dev/interop/js-interop/usage With js_interop you should be able to just pass a JS string and it should work. Closing as there isn't a bug here. (we have a tracking issue for hiding the |
I missed the part that this is about supporting node.js. I think that's up to the product team to decide. Reopened the issue. |
Thanks, didn't know that, passing compiled module indeed works
This also worked, so with my import fs from "node:fs/promises";
import { instantiate, invoke } from "./lib/dart_wasm_node.mjs";
global.sayHi = (msg) => {
console.log(msg);
};
const wasmBufferPromise = await fs.readFile("./lib/dart_wasm_node.wasm");
const wasmModule = await WebAssembly.compile(wasmBufferPromise);
const instance = await instantiate(wasmModule);
invoke(instance); and import 'dart:async';
import 'dart:js_interop';
@JS()
external void sayHi(String message);
void main() async {
var iterations = 0;
Timer.periodic(Duration(seconds: 1), (timer) async {
sayHi('$iterations');
if (iterations == 5) {
timer.cancel();
}
iterations++;
});
} everything works as expected 🎉
Not sure if anything else is required, so I'll leave it up to you to decide whether this issue should be kept open. |
What would be an alternative for Something along these lines: @pragma('wasm:export', 'receive')
void receive(String message) {
print('Received: $message');
} const wasmBufferPromise = await fs.readFile("./lib/dart_wasm_node.wasm");
const wasmModule = await WebAssembly.compile(wasmBufferPromise);
const instance = await instantiate(wasmModule);
instance.exports.receive(stringToDartString(msg)); |
@lesnitsky The See e.g. the example here #55715 (comment) |
@mkustermann Thanks! #55715 (comment) helped. import fs from "node:fs/promises";
import { instantiate, invoke } from "./lib/dart_wasm_node.mjs";
const wasmBuffer = await fs.readFile("./lib/dart_wasm_node.wasm");
const wasmModule = await WebAssembly.compile(wasmBuffer);
const instance = await instantiate(wasmModule);
invoke(instance);
global.onDartMessage = (msg) => {
console.log(msg);
onJSMessage("hello from js");
}; import 'dart:async';
import 'dart:js_interop';
@JS()
external void onDartMessage(String message);
@JS()
external set onJSMessage(JSFunction handler);
void handler(String message) {
print(message);
}
void main() async {
onJSMessage = handler.toJS;
var iterations = 0;
Timer.periodic(Duration(seconds: 1), (timer) async {
onDartMessage('hello from dart');
if (iterations == 5) timer.cancel();
iterations++;
});
} |
I'm trying to run dart compiled to wasm in node.js:
Dart version:
Dart SDK version: 3.4.0 (stable) (Mon May 6 07:59:58 2024 -0700) on "macos_arm64"
Node version:
v22.2.0
Dart code (dart_wasm_node.dart):
Compilation:
Node.js code (run.mjs)
dart_wasm_node.mjs
node run.mjs
Lines 216-220 of generated
dart_wasm_node.mjs
As per MDN docs for
WebAssembly.instantiate
:Return value
A Promise that resolves to a
ResultObject
which contains two fields:module
: A WebAssembly.Module object representing the compiled WebAssembly module. This Module can be instantiated again, shared via postMessage(), or cached.instance
: A WebAssembly.Instance object that contains all the Exported WebAssembly functions.Question
Is this a bug? Why
ResultObject
is assigned todartInstance
, notresultObject.instance
?Fix (?)
Running again:
node run.mjs
Yay! Dart wasm is executed, but returned value is not a string.
Question?
Did I do something wrong here?
Fix (?)
I'm able to get an actual string if I manually copy
stringFromDartString
fromdart_wasm_node.js
This is the final
run.mjs
:The text was updated successfully, but these errors were encountered: