3 ms·
If you need to call into user-generated Javascript synchronously or have greater control over the sandbox environment, you can use WebAssembly to run a Javascri
by jitl 4y ago
If you need to call into user-generated Javascript synchronously or have greater control over the sandbox environment, you can use WebAssembly to run a Javascript interpreter: https://github.com/justjake/quickjs-emscripten#quickjs-emscripten https://github.com/justjake/quickjs-emscripten#quickjs-emscr...
QuickJS in WebAssembly is much slower than your browser's native Javascript runtime, but possibly faster than async calls using postMessage. As an added bonus, it can make async functions in the host appear to be synchronous inside the sandbox using asyncify: https://github.com/justjake/quickjs-emscripten#async-on-host-sync-in-quickjs https://github.com/justjake/quickjs-emscripten#async-on-host...
- m1sta_ 4y agoNobody has benchmarked postMessage vs QuickJS?
- laurencerowe 4y agoI believe SharedArrayBuffer and Atomics.wait allow the same synchronous access within the worker to async functions in the host. This is the approach used by PartyTown: https://partytown.builder.io/how-does-partytown-work https://partytown.builder.io/how-does-partytown-work
- idealmedtech 4y agoI know the industry has moved on to WebAssembly, but I thought it might be worth mentioning that I just recently finished a personal project to compile an arbitrary NodeJS script in QuickJS and call the code from C - all* in a statically or dynamically linked library, no support files. In case anyone is interested in calling JS from old school C-supported languages, check it out: https://github.com/ijustlovemath/determine-basal-native https://github.com/ijustlovemath/determine-basal-native It's specific to my application but could easily be reused with a few tweaks. * Mostly all, the last step is to store the ESM JS script text in an object file and link it in, which I'm still working on.