Skip to content

Commit 97bf98c

Browse files
committed
Declare the WebAssembly W5 and stream-slot entry points in the runtime headers
The backend can emit scr_wasm_memory_construct, scr_wasm_table_construct, scr_wasm_global_construct, scr_wasm_construct_without_new, scr_wasm_module_imports, scr_wasm_module_exports, scr_wasm_module_custom_sections and scr_wasm_value_of (vercel-labs#1410), and scr_stream_dyn_native, scr_stream_pipe_dyn and scr_stream_unpipe_dyn (vercel-labs#1524). Their prototypes were in scr_wasm.c or nowhere, so the runtime ABI guard failed. They are now in scr_wasm.h and scr_stream.h, and scr_wasm.c no longer repeats the header's declarations. The speculative-hoisting test relied on `v instanceof Num` in a JavaScript function failing, which constructor-function instanceof now compiles (vercel-labs#1512). Its hoisted function reads the prototype through Reflect.getPrototypeOf instead, which still fences.
1 parent 73a5956 commit 97bf98c

4 files changed

Lines changed: 20 additions & 22 deletions

File tree

‎packages/compiler/test/speculative-hoisted-functions.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import type { IrModule } from "../src/ir/ir.js";
99
// list above the constructor. The initializer lowers speculatively first,
1010
// which hoists the constructor and the helper it calls. Those bodies are
1111
// kept, so their failing statements must keep runtime fences; dropping
12-
// them left `isNum` without its return (an SC9002 trap at load).
12+
// them left `isNum` without its return (an SC9002 trap at load). isNum's
13+
// return reads the prototype through Reflect.getPrototypeOf, which has no
14+
// lowering, so it fences.
1315
const source = `
1416
function clone(config) {
1517
var P = (Num.prototype = { constructor: Num, toString: null }),
@@ -20,7 +22,7 @@ function clone(config) {
2022
x.v = isNum(v) ? 0 : v * unit;
2123
}
2224
function isNum(v) {
23-
return v instanceof Num || (!!v && v._isNum === true);
25+
return Reflect.getPrototypeOf(v) === P || (!!v && v._isNum === true);
2426
}
2527
P._isNum = true;
2628
return Num;
@@ -59,6 +61,6 @@ test("speculatively hoisted JavaScript functions keep their statement fences", a
5961
const mod = await lower();
6062
const isNum = mod.functions.find((fn) => fn.name.endsWith("_isNum"));
6163
expect(isNum).toBeDefined();
62-
expect(fenceCodes(isNum!.body)).toEqual(["SC1090"]);
64+
expect(fenceCodes(isNum!.body)).toEqual(["SC2020"]);
6365
expect(mod.functions.flatMap((fn) => fenceCodes(fn.body))).not.toContain("SC9002");
6466
});

‎packages/runtime/src/scr_stream.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ ScrStream *scr_stream_pipe_general(ScrStream *src, ScrStream *dst, bool end);
136136
/* A boxed native stream out of a checked-dynamic pipeline stage (+1), or
137137
* NULL with `message` thrown as the not-supported Error. */
138138
ScrStream *scr_stream_unbox_dyn(const struct ScrDyn *value, const ScrStr *message);
139+
/* Checked-dynamic stream slots (scr_stream_dyn.c): the native stream a
140+
* value boxes with the requested side (1 readable, 2 writable; +1, or
141+
* NULL), and readable.pipe()/unpipe() for a destination that is not a
142+
* native stream. */
143+
ScrStream *scr_stream_dyn_native(const struct ScrDyn *value, double side);
144+
void scr_stream_pipe_dyn(ScrStream *src, struct ScrDyn *src_box, struct ScrDyn *dest, bool end);
145+
void scr_stream_unpipe_dyn(ScrStream *src, struct ScrDyn *dest);
139146
ScrStream *scr_stream_new_duplex_dyn(struct ScrDyn *opts);
140147
ScrStream *scr_stream_new_transform_dyn(struct ScrDyn *opts);
141148
ScrStream *scr_stream_new_passthrough_dyn(struct ScrDyn *opts);

‎packages/runtime/src/scr_wasm.c‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,6 @@
9090
#define SCR_WASM_ASAN 1
9191
#endif
9292

93-
ScrPromise *scr_wasm_compile(ScrDyn *bytes);
94-
bool scr_wasm_validate(ScrDyn *bytes);
95-
ScrPromise *scr_wasm_instantiate(ScrDyn *source, ScrDyn *imports);
96-
ScrDyn *scr_wasm_module_new(ScrDyn *bytes);
97-
ScrDyn *scr_wasm_instance_new(ScrDyn *module, ScrDyn *imports);
98-
bool scr_wasm_is(const ScrDyn *value, double kind);
99-
ScrDyn *scr_wasm_error_new(double kind, ScrDyn *message, ScrDyn *options);
100-
ScrDyn *scr_wasm_function(double kind);
101-
ScrDyn *scr_wasm_namespace(void);
102-
ScrDyn *scr_wasm_global_view(void);
103-
ScrDyn *scr_wasm_memory_construct(ScrDyn *descriptor);
104-
ScrDyn *scr_wasm_table_construct(ScrDyn *descriptor, ScrDyn *value);
105-
ScrDyn *scr_wasm_global_construct(ScrDyn *descriptor, ScrDyn *value);
106-
ScrDyn *scr_wasm_construct_without_new(double kind, ScrDyn *arg0, ScrDyn *arg1);
107-
ScrDyn *scr_wasm_module_imports(ScrDyn *module);
108-
ScrDyn *scr_wasm_module_exports(ScrDyn *module);
109-
ScrDyn *scr_wasm_module_custom_sections(ScrDyn *module, ScrDyn *name);
110-
ScrDyn *scr_wasm_value_of(ScrDyn *value);
111-
11293
/* Entry points of translated code. */
11394
/* Sanitized builds do not declare it noreturn: AddressSanitizer would
11495
* call __asan_handle_no_return before every call, which warns on the

‎packages/runtime/src/scr_wasm.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,13 @@ ScrDyn *scr_wasm_error_new(double kind, ScrDyn *message, ScrDyn *options);
1919
ScrDyn *scr_wasm_function(double kind);
2020
ScrDyn *scr_wasm_namespace(void);
2121
ScrDyn *scr_wasm_global_view(void);
22+
ScrDyn *scr_wasm_memory_construct(ScrDyn *descriptor);
23+
ScrDyn *scr_wasm_table_construct(ScrDyn *descriptor, ScrDyn *value);
24+
ScrDyn *scr_wasm_global_construct(ScrDyn *descriptor, ScrDyn *value);
25+
ScrDyn *scr_wasm_construct_without_new(double kind, ScrDyn *arg0, ScrDyn *arg1);
26+
ScrDyn *scr_wasm_module_imports(ScrDyn *module);
27+
ScrDyn *scr_wasm_module_exports(ScrDyn *module);
28+
ScrDyn *scr_wasm_module_custom_sections(ScrDyn *module, ScrDyn *name);
29+
ScrDyn *scr_wasm_value_of(ScrDyn *value);
2230

2331
#endif /* SCR_WASM_H */

0 commit comments

Comments
 (0)