About native compilation
Is there any plan to support compilation to native binaries (for example by using LLVM or a C backend)?
Is there any plan to support compilation to native binaries (for example by using LLVM or a C backend)?
Yes, support for different backends is planned and actively being worked on.
@paulocoghi Right now you can use Static Hermes, QuickJS, txiki.js, Bun, Deno to compile to native executable. There's a few other libraries that can do WASM to native executable. So, this is technically possible now, outside of using js2wasm.
@guest271314 Bun and Deno create executables, but not by AoT compilation and, instead, by bundling the interpreter with the JS file. See https://github.com/oven-sh/bun/issues/2376.
There, in March 2023, I suggested real AoT by using Hop.js (https://github.com/manuel-serrano/hop, the only option at that time) but it was rejected.
The other options you mentioned that do real AoT, each of them solve the standard library in its own way. So it is extremely positive if js2 implements its own direct AoT compilation, ideally without intermediary WASM as a way to achieve it but, instead, by also using LLVM to do so.
@paulocoghi Have you tried Facebook (Meta) Static Hermes?
No, and the subject of my issue here was specifically about js2 and native binaries
Alright, I've been testing JZ https://github.com/dy/jz which uses watr https://github.com/dy/watr. JavaScript to WASM and native executable via wasm2c.
So it occurred to me that watr could do the js2wasm WASM GC to native executable thing. Version 5.13.0 can and does https://github.com/dy/watr/issues/12#issuecomment-6431260024.
git clone https://github.com/dy/watr
cd watr
bun install
wasm-dis nm_js2wasm_node_fs.wasm -o nm.wat # binaryen; WABT's wasm2wat can't read this binary (packed fields)
node ./bin/watr.js nm.wat --polyfill --exports _start,memory -o nm.mvp.wasm
WABT=$HOME/bin/wabt
$WABT/bin/wasm2c -n nm nm.mvp.wasm -o nm.c # WABT 1.0.39: add --enable-multi-memory --enable-exceptions
R=$WABT/wasm2c
cc -O2 -I. -I$R -I$WABT/include nm.c $R/wasm-rt-impl.c $R/wasm-rt-mem-impl.c $R/wasm-rt-exceptions-impl.c wasi.c -o nm -lm
wasi.c
/* fd_read and fd_write for a WASI command translated by `wasm2c -n nm`:
* blocking readv/writev on the process's descriptors, partial counts as the
* OS returns them, 0 at EOF. Runs _start; a trap prints its reason, exits 134. */
#include
#include
#include
#include
#include
#include
#include "wasm-rt.h"
#include "wasm-rt-impl.h"
#include "nm.h"
struct w2c_wasi__snapshot__preview1 { wasm_rt_memory_t* mem; };
typedef struct w2c_wasi__snapshot__preview1 wasi_t;
enum { E_SUCCESS = 0, E_BADF = 8, E_FAULT = 21, E_INVAL = 28, E_IO = 29, E_PIPE = 64 };
static u8* span(wasi_t* w, u32 p, u64 n) { return (u64)p + n <= w->mem->size ? w->mem->data + p : NULL; }
static u32 ld32(const u8* p) { u32 v; memcpy(&v, p, 4); return v; }
static u32 rw(wasi_t* w, int out, u32 fd, u32 iovs, u32 n, u32 count_ptr) {
u8* list = span(w, iovs, (u64)n * 8), *count = span(w, count_ptr, 4);
if (!list || !count || n > 1024) return E_FAULT;
struct iovec v[1024];
for (u32 i = 0; i < n; i++) {
u32 len = ld32(list + i * 8 + 4);
if (!(v[i].iov_base = span(w, ld32(list + i * 8), len))) return E_FAULT;
v[i].iov_len = len;
}
ssize_t r;
do r = out ? writev((int)fd, v, (int)n) : readv((int)fd, v, (int)n);
while (r < 0 && errno == EINTR);
if (r < 0) return errno == EBADF ? E_BADF : errno == EPIPE ? E_PIPE : errno == EINVAL ? E_INVAL : E_IO;
u32 c = (u32)r;
memcpy(count, &c, 4);
return E_SUCCESS;
}
u32 w2c_wasi__snapshot__preview1_fd_read(wasi_t* w, u32 fd, u32 iovs, u32 n, u32 nread) { return rw(w, 0, fd, iovs, n, nread); }
u32 w2c_wasi__snapshot__preview1_fd_write(wasi_t* w, u32 fd, u32 iovs, u32 n, u32 nwritten) { return rw(w, 1, fd, iovs, n, nwritten); }
int main(void) {
signal(SIGPIPE, SIG_IGN);
wasm_rt_init();
w2c_nm inst;
wasi_t wasi = { w2c_nm_memory(&inst) };
wasm_rt_trap_t trap = wasm_rt_impl_try();
if (trap) { fprintf(stderr, "wasm trap: %s\n", wasm_rt_strerror(trap)); return 134; }
wasm2c_nm_instantiate(&inst, &wasi);
w2c_nm_0x5Fstart(&inst);
wasm2c_nm_free(&inst);
wasm_rt_free();
return 0;
}
Notably in the linked watr issue, this was pointed out about the WAT js2wasm emitted
Use the .wasm, not your .wat. They're different builds (210 vs 50 functions), and the .wat text isn't valid in five ways, all from js2wasm's printer:
- type indices from its own numbering (it counts 105 types and prints 74, so (type 95) is the 68th printed)
- supertypes that don't exist ($type0, $type6, $type88)
- negative heap types (-19, -18)
- 20 locals all named $undefined
- ref.cast null (ref null 6), which mixes old and final syntax watr rejects these, as WABT and binaryen do.