Comments (7)
from sdk.
Is this a bug? Why ResultObject is assigned to dartInstance, not resultObject.instance?
This is the same issue described in #55412. WebAssembly.instantiate
confusingly returns different types of values depending on the types of the arguments. In the generated .mjs we're expecting the module to be compiled with the "secondary overload": https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/instantiate_static#secondary_overload_%E2%80%94_taking_a_module_object_instance
I.e. you need to pass a WebAssembly.Module
(instead of the binary for a Wasm module) to the instantiate
function exported by the .mjs file.
Did I do something wrong here?
Pragmas starting with wasm:
are for internal use only so shouldn't be used. We will hide them from users soon (#55733).
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 wasm
pragmas)
from sdk.
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.
from sdk.
I.e. you need to pass a WebAssembly.Module (instead of the binary for a Wasm module) to the instantiate function exported by the .mjs file.
Thanks, didn't know that, passing compiled module indeed works
You should use js_interop: https://dart.dev/interop/js-interop/usage
This also worked, so with my run.mjs
updated to
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 dart_wasm_node.dart
to:
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 🎉
I missed the part that this is about supporting node.js. I think that's up to the product team to decide.
Not sure if anything else is required, so I'll leave it up to you to decide whether this issue should be kept open.
from sdk.
Pragmas starting with wasm: are for internal use only so shouldn't be used. We will hide them from users soon (#55733).
What would be an alternative for wasm:export
in js_interop
land? Can't find anything in docs that would help me call something in dart from node.
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));
from sdk.
@lesnitsky The instance.exports
are exports of wasm functions - but we don't support that "officially" yet (we'll hide it soon and expose a proper mechanism with sufficient checks later on). Using JS interop you can convert dart functions to JS wrapper functions which can be called from JS. One way is to make the Dart code convert dart functions to JSFunction
and making them available to JS via setting properties (e.g. on globalThis
).
See e.g. the example here #55715 (comment)
from sdk.
@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++;
});
}
from sdk.
Related Issues (20)
- SocketException: Connection reset by peer (OS Error: Connection reset by peer) HOT 6
- Analyzer Feedback from IntelliJ
- Analyser falsely reports unused function
- Building app for Linux musl? HOT 2
- MSAN use-of-uninitialized-value in FFI tests HOT 1
- Track macro dependencies at the library level for improved invalidation HOT 8
- The SDK configured in dart.flutterSdkPath is not a valid SDK folder. HOT 1
- [breaking change] Change `SecurityContext` to an `abstract final class` HOT 4
- Unhandled Exception: type '_Type' is not a subtype of type 'IBaseModel' in type cast HOT 2
- `Thread 1 is not paused` error at the end of a web test HOT 7
- textDocument/completion payload sizes can be very large because of documentation of in-scope items HOT 7
- Dart cannot open a socket on Android device, not simulator HOT 6
- stdin stream closes unexpectedly when Dart program is spawned from a bat file (which is spawned by a NodeJS process) and an msys2 program has been run earlier in the bat file HOT 1
- vm/cc/DartAPI_BreakpointLockRace flaky -> Timeout on vm-linux-debug-x64c HOT 1
- Crash on new ffi/msan_test on ARM64 MSAN configurations
- Set-Cookie path should not be case-insensitive
- Provide a compatible method to conditionally import between WASM and regular Web with also backward compatibility HOT 5
- [resource_identifiers] closures out of static functions
- pkg/json breaks when a field name contains $ HOT 2
- [vm/ffi] `Pointer.asTypedList` shared across isolates causes use after free
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sdk.