mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-28 06:53:42 +08:00
A build now translates every WebAssembly module whose bytes it can see (W2's discovery) into LLVM IR with a TypeScript translator (packages/compiler/src/wasm/module.ts, translate.ts) and appends it to the program module, so the existing LLVM helper optimizes, partitions and caches it with the program. The program also emits scr_wasm_registry, a portable table of module descriptors (scr_wasm_abi.h). At run time, WebAssembly.compile, instantiate, validate, new Module and new Instance find the translated module by the SHA-256 of its bytes; bytes the build did not see reject with a CompileError. There is no interpreter and no JIT. The runtime (scr_wasm.c, link-gated by the new `wasm` feature, handle tags inside W0's headroom) implements the JavaScript API once, over descriptors, with Node's semantics and messages: exports (functions, memories, tables, globals), imports (functions, immutable globals, and memories, tables and globals other instances export), every trap as WebAssembly.RuntimeError, the CompileError, LinkError and RuntimeError classes, compile and instantiate settling on a later turn, and globalThis.WebAssembly as a value. On 64-bit Linux and macOS, memories sit in 8 GiB guard reservations and a SIGSEGV/SIGBUS handler turns faults into traps or stack exhaustion; async fibers get a guard page on first use. Sanitizer builds and Windows check bounds and the stack explicitly. A WebAssembly.Module crosses to workers through W0's transfer hook (W4). W1's refusals become lowerings; the Memory, Table and Global constructors, Module.imports/exports/customSections and the streaming functions still refuse with SC2020. The globals: WebAssembly compatibility row moves to partial. Evidence: differential corpus programs for the API, TypeScript use and the codemode worker shape, plus five programs executing about 9,300 commands of the WebAssembly 2.0 spec suite; all match Node in the plain and sanitized lanes. quickjs-wasi runs through its own glue, matching Node (apart from a pre-existing non-wasm fence) and on par with or faster than V8. The zero-cost gate leaves every non-wasm program byte-identical. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
53 lines
2.8 KiB
WebAssembly Text Format
53 lines
2.8 KiB
WebAssembly Text Format
(module
|
|
(import "env" "log" (func $log (param i32)))
|
|
(import "env" "fail" (func $fail (param i32) (result i32)))
|
|
(import "env" "base" (global $base i32))
|
|
(import "env" "big" (global $big i64))
|
|
(memory (export "memory") 1 4)
|
|
(table (export "table") 2 8 funcref)
|
|
(global $counter (export "counter") (mut i32) (i32.const 0))
|
|
(global (export "pi") f64 (f64.const 3.25))
|
|
(global (export "big2") i64 (global.get $big))
|
|
(data (i32.const 16) "scriptc\00")
|
|
(elem (i32.const 0) $double $square)
|
|
(type $unop (func (param i32) (result i32)))
|
|
(type $binop (func (param i32 i32) (result i32)))
|
|
(func $double (type $unop) (i32.mul (local.get 0) (i32.const 2)))
|
|
(func $square (type $unop) (i32.mul (local.get 0) (local.get 0)))
|
|
(start $init)
|
|
(func $init (call $log (global.get $base)))
|
|
(func (export "add") (type $binop) (i32.add (local.get 0) (local.get 1)))
|
|
(func (export "apply") (param i32 i32) (result i32)
|
|
(call_indirect (type $unop) (local.get 1) (local.get 0)))
|
|
(func (export "bump") (result i32)
|
|
(global.set $counter (i32.add (global.get $counter) (i32.const 1)))
|
|
(global.get $counter))
|
|
(func (export "mul64") (param i64 i64) (result i64) (i64.mul (local.get 0) (local.get 1)))
|
|
(func (export "divmod") (param i32 i32) (result i32 i32)
|
|
(i32.div_s (local.get 0) (local.get 1)) (i32.rem_s (local.get 0) (local.get 1)))
|
|
(func (export "narrow") (param f32) (result f32) (local.get 0))
|
|
(func (export "strlen") (param $p i32) (result i32) (local $n i32)
|
|
(block $done (loop $next
|
|
(br_if $done (i32.eqz (i32.load8_u (i32.add (local.get $p) (local.get $n)))))
|
|
(local.set $n (i32.add (local.get $n) (i32.const 1)))
|
|
(br $next)))
|
|
(local.get $n))
|
|
(func (export "grow") (param i32) (result i32) (memory.grow (local.get 0)))
|
|
(func (export "pages") (result i32) (memory.size))
|
|
(func (export "callFail") (param i32) (result i32) (i32.add (call $fail (local.get 0)) (i32.const 1)))
|
|
(func (export "trap") (param i32) (result i32)
|
|
(block $b6 (block $b5 (block $b4 (block $b3 (block $b2 (block $b1 (block $b0
|
|
(br_table $b0 $b1 $b2 $b3 $b4 $b5 $b6 (local.get 0)))
|
|
unreachable)
|
|
(return (i32.div_u (i32.const 1) (i32.const 0))))
|
|
(return (i32.load (i32.const 0x7fffffff))))
|
|
(return (call_indirect (type $binop) (i32.const 1) (i32.const 2) (i32.const 0))))
|
|
(return (call_indirect (type $unop) (i32.const 1) (i32.const 7))))
|
|
(return (i32.trunc_f64_s (f64.const nan))))
|
|
(i32.div_s (i32.const 0x80000000) (i32.const -1)))
|
|
(func $rec (export "recurse") (param i32) (result i32)
|
|
(i32.add (call $rec (i32.add (local.get 0) (i32.const 1))) (i32.const 1)))
|
|
(func (export "fill") (param i32 i32 i32) (memory.fill (local.get 0) (local.get 1) (local.get 2)))
|
|
(func (export "copy") (param i32 i32 i32) (memory.copy (local.get 0) (local.get 1) (local.get 2)))
|
|
)
|