Skip to content

Integrate Rtrace with ESM bindings #2595

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 2 commits into from
Dec 22, 2022
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
},
"./*": "./*"
},
"imports": {
"#rtrace": {
"import": "./lib/rtrace/index.js",
"types": "./lib/rtrace/index.d.ts"
}
},
"bin": {
"asc": "./bin/asc.js",
"asinit": "./bin/asinit.js"
Expand Down
28 changes: 27 additions & 1 deletion src/bindings/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ import {
// callee expects a properly coerced integer value, leading to more `>>> 0`
// coercions than necessary when the import is actually another Wasm module.

/** Maps special imports to their actual modules. */
function importToModule(moduleName: string): string {
// Map rtrace via `imports` in package.json
if (moduleName == "rtrace") return "#rtrace";
return moduleName;
}

/** Determines whether a module's imports should be instrumented. */
function shouldInstrument(moduleName: string): bool {
return moduleName != "rtrace";
}

/** A JavaScript bindings builder. */
export class JSBuilder extends ExportsWalker {

Expand Down Expand Up @@ -515,6 +527,12 @@ export class JSBuilder extends ExportsWalker {
sb.push(escapeString(moduleName, CharCode.DoubleQuote));
sb.push("\"");
}
if (!shouldInstrument(moduleName)) {
sb.push(": __module");
sb.push(moduleId.toString());
sb.push(",\n");
continue;
}
let resetPos = sb.length;
sb.push(": Object.assign(Object.create(");
if (moduleName == "env") {
Expand Down Expand Up @@ -580,6 +598,14 @@ export class JSBuilder extends ExportsWalker {
map.push(" const env = imports.env;\n");
} else {
let moduleId = <i32>mappings.get(moduleName);
if (moduleName == "rtrace") {
// Rtrace is special in that it needs to be installed on the imports
// object. Use sensible defaults and substitute the original import.
map.push(" ((rtrace) => {\n");
map.push(" delete imports.rtrace;\n");
map.push(" new rtrace.Rtrace({ getMemory() { return memory; }, onerror(err) { console.log(`RTRACE: ${err.stack}`); } }).install(imports);\n");
map.push(" })(imports.rtrace);\n");
}
map.push(" const __module");
map.push(moduleId.toString());
map.push(" = imports");
Expand Down Expand Up @@ -914,7 +940,7 @@ export class JSBuilder extends ExportsWalker {
importExpr.push("import * as __import");
importExpr.push(moduleId.toString());
importExpr.push(" from \"");
importExpr.push(escapeString(moduleName, CharCode.DoubleQuote));
importExpr.push(escapeString(importToModule(moduleName), CharCode.DoubleQuote));
importExpr.push("\";\n");
needsMaybeDefault = true;
}
Expand Down