duktape icon indicating copy to clipboard operation
duktape copied to clipboard

WebAssembly integration options

Open svaarala opened this issue 9 years ago • 16 comments

WebAssembly is now stable enough to be implemented: https://lists.w3.org/Archives/Public/public-webassembly/2017Feb/0002.html.

There are several options for Duktape to interact with WebAssembly; two main approaches are:

  • Least coupled approach is to make WebAssembly an external library. Compilation and execution of WebAssembly code is via Duktape/C calls, without core Duktape awareness. The Duktape changes required would be ensuring that this integration is natural.
  • More coupled approach is to support the WebAssembly Javascript API (and provide a C counterpart). WebAssembly functions could get their own internal representation and integrate more closely with core Duktape, e.g. via the value stack. The whole WebAssembly support must be optional for footprint reasons.

Regardless of the coupling approach, providing optional JIT or direct code generation would be useful for WebAssembly. It's a smaller problem than a Javascript JIT, and could help with implementing e.g. numerical algorithms in a portable manner.

svaarala avatar Mar 01 '17 17:03 svaarala

I was under the impression that WebAssembly was just a subset of full JavaScript. Is that not actually the case?

fatcerberus avatar Mar 01 '17 17:03 fatcerberus

You might be thinking of asm.js - webassembly provides a binary bytecode format, a Javascript API for it, etc.

svaarala avatar Mar 01 '17 18:03 svaarala

See http://webassembly.org/:

The wasm stack machine is designed to be encoded in a size- and load-time-efficient binary format.

There's a textual format, but it's for debugging etc (http://webassembly.org/docs/text-format/), example:

get_local 0
i64.const 0
i64.eq
if i64
    i64.const 1
else
    get_local 0
    get_local 0
    i64.const 1
    i64.sub
    call 0
    i64.mul
end

The intent is to be able to compile e.g. a C program directly to WebAssembly binary format, and then execute from there.

svaarala avatar Mar 01 '17 18:03 svaarala

Yeah, asm.js was what I was thinking of. WebAssembly seems interesting: what I got out of that page was that it's essentially an attempt at a standardized bytecode format for JS, mainly this part:

WebAssembly describes a memory-safe, sandboxed execution environment that may even be implemented inside existing JavaScript virtual machines.

That's a really nice idea. Even something like miniSphere could benefit from it. :)

fatcerberus avatar Mar 01 '17 19:03 fatcerberus

It's not for JS, but a pretty different language - it's best explained by looking at the bytecode format and what operations it supports.

svaarala avatar Mar 01 '17 19:03 svaarala

Would be interesting to see how a javascript to wasm compiled program competes against duktape (both without JIT) ;)

harold-b avatar Mar 01 '17 20:03 harold-b

FWIW, I've recently added WebAssembly parse + execute support to a project of mine called VMIR.

Don't want to hijack this thread but more info can be found on its GitHub page: https://github.com/andoma/vmir

andoma avatar Mar 01 '17 22:03 andoma

@andoma Thanks, looks interesting! It should be possible to use VMIR for loosely coupling a WebAssembly executor to Duktape, as long as the WebAssembly <-> Javascript transition works well enough.

svaarala avatar Mar 01 '17 22:03 svaarala

Would it be possible for you to share the c progam(s) / emscripten makefile that you used to produce dukweb.js that is used on your browser repl?

I am interested in creating a web assembly version of this repl. Or maybe you have already done this?

Thanks

mikeptweet avatar Nov 18 '18 14:11 mikeptweet

All the stuff is in the main repo:

  • Some support code etc: https://github.com/svaarala/duktape/tree/master/dukweb
  • Build target: https://github.com/svaarala/duktape/blob/master/Makefile#L583-L587

Let me know how it goes, would be nice to know how that works out :)

svaarala avatar Nov 18 '18 14:11 svaarala

Thanks @svaarala ... I created a test of the WebAssembly version here ... https://s3.amazonaws.com/mparsons/dukweb/index.html

It is considerably faster than the Javascript version and is pretty close to native speed :-)

Just add -s EXTRA_EXPORTED_RUNTIME_METHODS="['cwrap','ccall']" -s WASM=1 to your make file.

Thanks so much for this incredible library :-)

mikeptweet avatar Nov 19 '18 11:11 mikeptweet

@mikeptweet Awesome, I'll add that to the Makefile too :)

svaarala avatar Nov 19 '18 14:11 svaarala

Anybody worked on something yet? WAVM and wasmjit only work on 64bit CPU, while the big JS engines (SpiderMonkey, v8, ChakraCore, JavaScriptCore) probably work on 32/64bit CPU's each.

I guess the best would be to just rip out one WASM implementation of these four engines and write Duktape bindings for it?

I don't see a good way yet to bring WebAssembly into small software projects, without bloating them up with one of those four giant JS engines. Duktape+WASM would be kinda perfect.

kungfooman avatar Mar 21 '19 18:03 kungfooman

Duktape+WASM would be kinda perfect.

whaqzhzd avatar Apr 28 '19 06:04 whaqzhzd

I think Wasm3 is now stable enough to be integrated with Duktape: https://github.com/wasm3/wasm3

vshymanskyy avatar Jan 05 '20 21:01 vshymanskyy

Any updates on this?

dbohdan avatar Mar 13 '20 20:03 dbohdan