Skip to content

Commit 0e0a3c1

Browse files
authored
Integrate Rtrace with ESM bindings (#2595)
1 parent 280a8ef commit 0e0a3c1

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

Diff for: package.json

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
},
6060
"./*": "./*"
6161
},
62+
"imports": {
63+
"#rtrace": {
64+
"import": "./lib/rtrace/index.js",
65+
"types": "./lib/rtrace/index.d.ts"
66+
}
67+
},
6268
"bin": {
6369
"asc": "./bin/asc.js",
6470
"asinit": "./bin/asinit.js"

Diff for: src/bindings/js.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ import {
9292
// callee expects a properly coerced integer value, leading to more `>>> 0`
9393
// coercions than necessary when the import is actually another Wasm module.
9494

95+
/** Maps special imports to their actual modules. */
96+
function importToModule(moduleName: string): string {
97+
// Map rtrace via `imports` in package.json
98+
if (moduleName == "rtrace") return "#rtrace";
99+
return moduleName;
100+
}
101+
102+
/** Determines whether a module's imports should be instrumented. */
103+
function shouldInstrument(moduleName: string): bool {
104+
return moduleName != "rtrace";
105+
}
106+
95107
/** A JavaScript bindings builder. */
96108
export class JSBuilder extends ExportsWalker {
97109

@@ -515,6 +527,12 @@ export class JSBuilder extends ExportsWalker {
515527
sb.push(escapeString(moduleName, CharCode.DoubleQuote));
516528
sb.push("\"");
517529
}
530+
if (!shouldInstrument(moduleName)) {
531+
sb.push(": __module");
532+
sb.push(moduleId.toString());
533+
sb.push(",\n");
534+
continue;
535+
}
518536
let resetPos = sb.length;
519537
sb.push(": Object.assign(Object.create(");
520538
if (moduleName == "env") {
@@ -580,6 +598,14 @@ export class JSBuilder extends ExportsWalker {
580598
map.push(" const env = imports.env;\n");
581599
} else {
582600
let moduleId = <i32>mappings.get(moduleName);
601+
if (moduleName == "rtrace") {
602+
// Rtrace is special in that it needs to be installed on the imports
603+
// object. Use sensible defaults and substitute the original import.
604+
map.push(" ((rtrace) => {\n");
605+
map.push(" delete imports.rtrace;\n");
606+
map.push(" new rtrace.Rtrace({ getMemory() { return memory; }, onerror(err) { console.log(`RTRACE: ${err.stack}`); } }).install(imports);\n");
607+
map.push(" })(imports.rtrace);\n");
608+
}
583609
map.push(" const __module");
584610
map.push(moduleId.toString());
585611
map.push(" = imports");
@@ -914,7 +940,7 @@ export class JSBuilder extends ExportsWalker {
914940
importExpr.push("import * as __import");
915941
importExpr.push(moduleId.toString());
916942
importExpr.push(" from \"");
917-
importExpr.push(escapeString(moduleName, CharCode.DoubleQuote));
943+
importExpr.push(escapeString(importToModule(moduleName), CharCode.DoubleQuote));
918944
importExpr.push("\";\n");
919945
needsMaybeDefault = true;
920946
}

0 commit comments

Comments
 (0)