@@ -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. */
96108export 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