Skip to content

Commit c0d86e6

Browse files
authored
fix: Handle exportRuntime in generated bindings (#2694)
1 parent 688746a commit c0d86e6

File tree

8 files changed

+72
-18
lines changed

8 files changed

+72
-18
lines changed

Diff for: src/bindings/js.ts

+23-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import {
1313
CommonFlags
1414
} from "../common";
1515

16+
import {
17+
runtimeFunctions,
18+
runtimeGlobals
19+
} from "../compiler";
20+
1621
import {
1722
ElementKind,
1823
Element,
@@ -945,19 +950,31 @@ export class JSBuilder extends ExportsWalker {
945950
assert(this.indentLevel == 0);
946951

947952
if (this.esm) {
948-
sb.push("export const {\n ");
953+
sb.push("export const {\n");
949954
if (this.program.options.exportMemory) {
950-
sb.push("memory,\n ");
955+
sb.push(" memory,\n");
951956
}
952957
if (this.program.options.exportTable) {
953-
sb.push("table,\n ");
958+
sb.push(" table,\n");
959+
}
960+
if (this.program.options.exportRuntime) {
961+
for (let i = 0, k = runtimeFunctions.length; i < k; ++i) {
962+
sb.push(" ");
963+
sb.push(runtimeFunctions[i]);
964+
sb.push(",\n");
965+
}
966+
for (let i = 0, k = runtimeGlobals.length; i < k; ++i) {
967+
sb.push(" ");
968+
sb.push(runtimeGlobals[i]);
969+
sb.push(",\n");
970+
}
954971
}
955972
for (let i = 0, k = exports.length; i < k; ++i) {
956-
if (i > 0) sb.push(",\n ");
973+
sb.push(" ");
957974
sb.push(exports[i]);
975+
sb.push(",\n");
958976
}
959-
sb.push(`
960-
} = await (async url => instantiate(
977+
sb.push(`} = await (async url => instantiate(
961978
await (async () => {
962979
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
963980
catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }

Diff for: src/bindings/tsd.ts

+14
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,20 @@ export class TSDBuilder extends ExportsWalker {
198198
indent(sb, this.indentLevel);
199199
sb.push(`export ${this.esm ? "declare " : ""}const table: WebAssembly.Table;\n`);
200200
}
201+
if (this.program.options.exportRuntime) {
202+
indent(sb, this.indentLevel);
203+
sb.push("// Exported runtime interface\n");
204+
indent(sb, this.indentLevel);
205+
sb.push(`export ${this.esm ? "declare " : ""}function __new(size: number, id: number): number;\n`);
206+
indent(sb, this.indentLevel);
207+
sb.push(`export ${this.esm ? "declare " : ""}function __pin(ptr: number): number;\n`);
208+
indent(sb, this.indentLevel);
209+
sb.push(`export ${this.esm ? "declare " : ""}function __unpin(ptr: number): void;\n`);
210+
indent(sb, this.indentLevel);
211+
sb.push(`export ${this.esm ? "declare " : ""}function __collect(): void;\n`);
212+
indent(sb, this.indentLevel);
213+
sb.push(`export ${this.esm ? "declare " : ""}const __rtti_base: number;\n`);
214+
}
201215
this.walk();
202216
if (!this.esm) {
203217
--this.indentLevel;

Diff for: src/compiler.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,9 @@ export namespace ExportNames {
418418
}
419419

420420
/** Functions to export if `--exportRuntime` is set. */
421-
const runtimeFunctions = [ "__new", "__pin", "__unpin", "__collect" ];
421+
export const runtimeFunctions = [ "__new", "__pin", "__unpin", "__collect" ];
422422
/** Globals to export if `--exportRuntime` is set. */
423-
const runtimeGlobals = [ "__rtti_base" ];
423+
export const runtimeGlobals = [ "__rtti_base" ];
424424

425425
/** Compiler interface. */
426426
export class Compiler extends DiagnosticEmitter {

Diff for: tests/compiler/bindings/esm.debug.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/** Exported memory */
22
export declare const memory: WebAssembly.Memory;
3+
// Exported runtime interface
4+
export declare function __new(size: number, id: number): number;
5+
export declare function __pin(ptr: number): number;
6+
export declare function __unpin(ptr: number): void;
7+
export declare function __collect(): void;
8+
export declare const __rtti_base: number;
39
/** bindings/esm/plainGlobal */
410
export declare const plainGlobal: {
511
/** @type `i32` */

Diff for: tests/compiler/bindings/esm.debug.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ async function instantiate(module, imports = {}) {
1818
// ~lib/bindings/dom/Math.E: f64
1919
Math.E
2020
),
21-
"Math.log": (
21+
"Math.log"(x) {
2222
// ~lib/bindings/dom/Math.log(f64) => f64
23-
Math.log
24-
),
23+
return Math.log(x);
24+
},
2525
"globalThis.globalThis": (
2626
// bindings/esm/immutableGlobalNested: ref_extern | null}
2727
globalThis.globalThis
@@ -522,6 +522,11 @@ async function instantiate(module, imports = {}) {
522522
}
523523
export const {
524524
memory,
525+
__new,
526+
__pin,
527+
__unpin,
528+
__collect,
529+
__rtti_base,
525530
plainGlobal,
526531
plainMutableGlobal,
527532
stringGlobal,
@@ -545,7 +550,7 @@ export const {
545550
newInternref,
546551
internrefFunction,
547552
functionFunction,
548-
fn
553+
fn,
549554
} = await (async url => instantiate(
550555
await (async () => {
551556
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }

Diff for: tests/compiler/bindings/esm.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"asc_flags": [
33
"--exportStart _start",
4-
"--bindings esm"
4+
"--bindings esm",
5+
"--exportRuntime"
56
],
67
"features": [
78
"reference-types"

Diff for: tests/compiler/bindings/esm.release.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/** Exported memory */
22
export declare const memory: WebAssembly.Memory;
3+
// Exported runtime interface
4+
export declare function __new(size: number, id: number): number;
5+
export declare function __pin(ptr: number): number;
6+
export declare function __unpin(ptr: number): void;
7+
export declare function __collect(): void;
8+
export declare const __rtti_base: number;
39
/** bindings/esm/plainGlobal */
410
export declare const plainGlobal: {
511
/** @type `i32` */

Diff for: tests/compiler/bindings/esm.release.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ async function instantiate(module, imports = {}) {
1818
// ~lib/bindings/dom/Math.E: f64
1919
Math.E
2020
),
21-
"Math.log": (
21+
"Math.log"(x) {
2222
// ~lib/bindings/dom/Math.log(f64) => f64
23-
Math.log
24-
),
23+
return Math.log(x);
24+
},
2525
"globalThis.globalThis": (
26-
// bindings/esm/immutableGlobalNested: externref
26+
// bindings/esm/immutableGlobalNested: ref_extern | null}
2727
globalThis.globalThis
2828
),
2929
"Date.getTimezoneOffset"() {
@@ -522,6 +522,11 @@ async function instantiate(module, imports = {}) {
522522
}
523523
export const {
524524
memory,
525+
__new,
526+
__pin,
527+
__unpin,
528+
__collect,
529+
__rtti_base,
525530
plainGlobal,
526531
plainMutableGlobal,
527532
stringGlobal,
@@ -545,7 +550,7 @@ export const {
545550
newInternref,
546551
internrefFunction,
547552
functionFunction,
548-
fn
553+
fn,
549554
} = await (async url => instantiate(
550555
await (async () => {
551556
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }

0 commit comments

Comments
 (0)