@@ -6,18 +6,17 @@ import { isReservedExportKey } from "./reserved-export-keys";
6
6
7
7
const SECTION_NAME = "as-bind_bindings" ;
8
8
9
- // Need to traverse the importObject and bind all import functions
10
- function traverseObjectAndRunCallback ( obj , callback , keys = [ ] , baseObj = obj ) {
11
- if ( ! obj ) {
12
- return ;
13
- }
14
- for ( const key of Object . keys ( obj ) ) {
15
- if ( obj [ key ] && Object . getPrototypeOf ( obj [ key ] ) === Object . prototype ) {
16
- traverseObjectAndRunCallback ( obj [ key ] , callback , [ ...keys , key ] , baseObj ) ;
17
- continue ;
18
- }
19
- callback ( obj [ key ] , [ ...keys , key ] , baseObj , key , obj ) ;
9
+ // Basically a deep-copy, but can be limited in levels.
10
+ function copyObject ( obj , { depth = Number . POSITIVE_INFINITY } = { } ) {
11
+ if ( depth <= 0 || ! obj || typeof obj !== "object" ) {
12
+ return obj ;
20
13
}
14
+ return Object . fromEntries (
15
+ Object . entries ( obj ) . map ( ( [ key , val ] ) => [
16
+ key ,
17
+ copyObject ( val , { depth : depth - 1 } )
18
+ ] )
19
+ ) ;
21
20
}
22
21
23
22
async function compileStreaming ( source ) {
@@ -80,19 +79,17 @@ export default class AsbindInstance {
80
79
}
81
80
82
81
async _instantiate ( source , importObject ) {
83
- this . importObject = importObject ;
84
82
this . module = await compileStreaming ( source ) ;
85
83
86
84
this . _validate ( ) ;
87
85
this . typeDescriptor = extractTypeDescriptor ( this . module ) ;
88
- this . _instantiateBindImportFunctions ( ) ;
86
+ this . _instantiateBindImportFunctions ( importObject ) ;
89
87
// Instantiate the module through the loader
90
88
this . loadedModule = await asbindInstantiate ( this . module , this . importObject ) ;
91
89
this . _instantiateBindUnboundExports ( ) ;
92
90
}
93
91
94
92
_instantiateSync ( source , importObject ) {
95
- this . importObject = importObject ;
96
93
this . module = new WebAssembly . Module ( source ) ;
97
94
98
95
this . _validate ( ) ;
@@ -103,46 +100,41 @@ export default class AsbindInstance {
103
100
}
104
101
105
102
_instantiateBindImportFunctions ( importObject ) {
106
- // Need to traverse the importObject and bind all import functions
107
- traverseObjectAndRunCallback (
108
- this . importObject ,
109
- ( importedFunction , keys , importObject , currentKey , currentObj ) => {
110
- if ( typeof importedFunction === "function" ) {
111
- // Save original function
112
- // TODO: Maybe use symbols here instead
113
- currentObj [ `__asbind_unbound_${ currentKey } ` ] = importedFunction ;
114
- currentObj [ currentKey ] = bindImportFunction (
115
- this ,
116
- importedFunction ,
117
- keys
118
- ) ;
119
- }
103
+ this . importObject = copyObject ( importObject , { depth : 2 } ) ;
104
+
105
+ for ( const [ moduleName , moduleDescriptor ] of Object . entries (
106
+ this . typeDescriptor . importedFunctions
107
+ ) ) {
108
+ for ( const [ importedFunctionName , descriptor ] of Object . entries (
109
+ moduleDescriptor
110
+ ) ) {
111
+ this . importObject [ moduleName ] [
112
+ `__asbind_unbound_${ importedFunctionName } `
113
+ ] = importObject [ moduleName ] [ importedFunctionName ] ;
114
+ this . importObject [ moduleName ] [
115
+ importedFunctionName
116
+ ] = bindImportFunction (
117
+ this ,
118
+ importObject [ moduleName ] [ importedFunctionName ] ,
119
+ descriptor
120
+ ) ;
120
121
}
121
- ) ;
122
+ }
122
123
}
123
124
124
125
_instantiateBindUnboundExports ( ) {
125
126
// Wrap appropriate the appropriate export functions
126
127
const unboundExports = this . loadedModule . exports ;
127
- this . exports = { } ;
128
-
129
- traverseObjectAndRunCallback (
130
- unboundExports ,
131
- ( exportedValue , keys , importObject , currentKey , currentObj ) => {
132
- if (
133
- typeof exportedValue === "function" &&
134
- ! isReservedExportKey ( currentKey )
135
- ) {
136
- // Wrap the export
137
- this . exports [ currentKey ] = bindExportFunction (
138
- this ,
139
- exportedValue ,
140
- currentKey
141
- ) ;
142
- } else {
143
- this . exports [ currentKey ] = exportedValue ;
144
- }
145
- }
146
- ) ;
128
+ this . exports = copyObject ( unboundExports , { depth : 1 } ) ;
129
+
130
+ for ( const [ exportedFunctionName , descriptor ] of Object . entries (
131
+ this . typeDescriptor . exportedFunctions
132
+ ) ) {
133
+ this . exports [ exportedFunctionName ] = bindExportFunction (
134
+ this ,
135
+ unboundExports [ exportedFunctionName ] ,
136
+ descriptor
137
+ ) ;
138
+ }
147
139
}
148
140
}
0 commit comments