Skip to content

fix: Add memory & table for esm binding's exports #2344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/bindings/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,12 @@ export class JSBuilder extends ExportsWalker {

if (this.esm) {
sb.push("export const {\n ");
if (this.program.options.exportMemory) {
sb.push("memory,\n ");
}
if (this.program.options.exportTable) {
sb.push("table,\n ");
}
for (let i = 0, k = exports.length; i < k; ++i) {
if (i > 0) sb.push(",\n ");
sb.push(exports[i]);
Expand Down
12 changes: 12 additions & 0 deletions src/bindings/tsd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ export class TSDBuilder extends ExportsWalker {
sb.push("declare namespace __AdaptedExports {\n");
++this.indentLevel;
}
if (this.program.options.exportMemory) {
indent(sb, this.indentLevel);
sb.push("/** Exported memory */\n");
indent(sb, this.indentLevel);
sb.push(`export ${this.esm ? "declare " : ""}const memory: WebAssembly.Memory;\n`);
}
if (this.program.options.exportTable) {
indent(sb, this.indentLevel);
sb.push("/** Exported table */\n");
indent(sb, this.indentLevel);
sb.push(`export ${this.esm ? "declare " : ""}const table: WebAssembly.Table;\n`);
}
this.walk();
if (!this.esm) {
--this.indentLevel;
Expand Down
2 changes: 2 additions & 0 deletions tests/compiler/bindings/esm.debug.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/** Exported memory */
export declare const memory: WebAssembly.Memory;
/** bindings/esm/plainGlobal */
export declare const plainGlobal: {
/** @type `i32` */
Expand Down
1 change: 1 addition & 0 deletions tests/compiler/bindings/esm.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ async function instantiate(module, imports = {}) {
return adaptedExports;
}
export const {
memory,
plainGlobal,
plainMutableGlobal,
stringGlobal,
Expand Down
2 changes: 2 additions & 0 deletions tests/compiler/bindings/esm.release.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/** Exported memory */
export declare const memory: WebAssembly.Memory;
/** bindings/esm/plainGlobal */
export declare const plainGlobal: {
/** @type `i32` */
Expand Down
1 change: 1 addition & 0 deletions tests/compiler/bindings/esm.release.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ async function instantiate(module, imports = {}) {
return adaptedExports;
}
export const {
memory,
plainGlobal,
plainMutableGlobal,
stringGlobal,
Expand Down
2 changes: 2 additions & 0 deletions tests/compiler/bindings/raw.debug.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
declare namespace __AdaptedExports {
/** Exported memory */
export const memory: WebAssembly.Memory;
/** bindings/esm/plainGlobal */
export const plainGlobal: {
/** @type `i32` */
Expand Down
2 changes: 2 additions & 0 deletions tests/compiler/bindings/raw.release.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
declare namespace __AdaptedExports {
/** Exported memory */
export const memory: WebAssembly.Memory;
/** bindings/esm/plainGlobal */
export const plainGlobal: {
/** @type `i32` */
Expand Down