@@ -92,6 +92,18 @@ import {
92
92
// callee expects a properly coerced integer value, leading to more `>>> 0`
93
93
// coercions than necessary when the import is actually another Wasm module.
94
94
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
+
95
107
/** A JavaScript bindings builder. */
96
108
export class JSBuilder extends ExportsWalker {
97
109
@@ -515,6 +527,12 @@ export class JSBuilder extends ExportsWalker {
515
527
sb . push ( escapeString ( moduleName , CharCode . DoubleQuote ) ) ;
516
528
sb . push ( "\"" ) ;
517
529
}
530
+ if ( ! shouldInstrument ( moduleName ) ) {
531
+ sb . push ( ": __module" ) ;
532
+ sb . push ( moduleId . toString ( ) ) ;
533
+ sb . push ( ",\n" ) ;
534
+ continue ;
535
+ }
518
536
let resetPos = sb . length ;
519
537
sb . push ( ": Object.assign(Object.create(" ) ;
520
538
if ( moduleName == "env" ) {
@@ -580,6 +598,14 @@ export class JSBuilder extends ExportsWalker {
580
598
map . push ( " const env = imports.env;\n" ) ;
581
599
} else {
582
600
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
+ }
583
609
map . push ( " const __module" ) ;
584
610
map . push ( moduleId . toString ( ) ) ;
585
611
map . push ( " = imports" ) ;
@@ -914,7 +940,7 @@ export class JSBuilder extends ExportsWalker {
914
940
importExpr . push ( "import * as __import" ) ;
915
941
importExpr . push ( moduleId . toString ( ) ) ;
916
942
importExpr . push ( " from \"" ) ;
917
- importExpr . push ( escapeString ( moduleName , CharCode . DoubleQuote ) ) ;
943
+ importExpr . push ( escapeString ( importToModule ( moduleName ) , CharCode . DoubleQuote ) ) ;
918
944
importExpr . push ( "\";\n" ) ;
919
945
needsMaybeDefault = true ;
920
946
}
0 commit comments