@@ -7,23 +7,17 @@ import { isReservedExportKey } from "./reserved-export-keys";
7
7
const SECTION_NAME = "as-bind_bindings" ;
8
8
9
9
// Need to traverse the importObject and bind all import functions
10
- function traverseObjectAndRunCallbackForFunctions ( baseObject , keys , callback ) {
11
- if ( ! baseObject ) {
10
+ function traverseObjectAndRunCallback ( obj , callback , keys = [ ] , baseObj = obj ) {
11
+ if ( ! obj ) {
12
12
return ;
13
13
}
14
-
15
- Object . keys ( baseObject ) . forEach ( baseObjectKey => {
16
- if ( typeof baseObject [ baseObjectKey ] === "function" ) {
17
- // Call the callback
18
- callback ( baseObject , keys , baseObjectKey ) ;
19
- } else if ( typeof baseObject [ baseObjectKey ] === "object" ) {
20
- traverseObjectAndRunCallbackForFunctions (
21
- baseObject [ baseObjectKey ] ,
22
- [ ...keys , baseObjectKey ] ,
23
- callback
24
- ) ;
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 ;
25
18
}
26
- } ) ;
19
+ callback ( obj [ key ] , [ ...keys , key ] , baseObj , key , obj ) ;
20
+ }
27
21
}
28
22
29
23
async function compileStreaming ( source ) {
@@ -86,46 +80,43 @@ export default class AsbindInstance {
86
80
}
87
81
88
82
async _instantiate ( source , importObject ) {
83
+ this . importObject = importObject ;
89
84
this . module = await compileStreaming ( source ) ;
85
+
90
86
this . _validate ( ) ;
91
87
this . typeDescriptor = extractTypeDescriptor ( this . module ) ;
92
- // Bind our import function
93
- this . _instantiateBindImportFunctions ( importObject ) ;
94
-
88
+ this . _instantiateBindImportFunctions ( ) ;
95
89
// Instantiate the module through the loader
96
90
this . loadedModule = await asbindInstantiate ( this . module , this . importObject ) ;
97
-
98
- // Bind our unbound exports
99
91
this . _instantiateBindUnboundExports ( ) ;
100
92
}
101
93
102
94
_instantiateSync ( source , importObject ) {
95
+ this . importObject = importObject ;
103
96
this . module = new WebAssembly . Module ( source ) ;
97
+
104
98
this . _validate ( ) ;
105
99
this . typeDescriptor = extractTypeDescriptor ( this . module ) ;
106
100
this . _instantiateBindImportFunctions ( importObject ) ;
107
-
108
101
this . loadedModule = asbindInstantiateSync ( this . module , this . importObject ) ;
109
102
this . _instantiateBindUnboundExports ( ) ;
110
103
}
111
104
112
105
_instantiateBindImportFunctions ( importObject ) {
113
- // Set our import object, as we will need it to store type caching
114
- this . importObject = importObject ;
115
-
116
106
// Need to traverse the importObject and bind all import functions
117
- traverseObjectAndRunCallbackForFunctions (
107
+ traverseObjectAndRunCallback (
118
108
this . importObject ,
119
- [ ] ,
120
- ( baseObject , keys , baseObjectKey ) => {
121
- // Wrap the original key, but then expose a new key for the unbound import
122
- let importFunction = baseObject [ baseObjectKey ] ;
123
- baseObject [ `__asbind_unbound_${ baseObjectKey } ` ] = importFunction ;
124
- baseObject [ baseObjectKey ] = bindImportFunction (
125
- this ,
126
- this . importObject ,
127
- [ ...keys , baseObjectKey ]
128
- ) ;
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
+ }
129
120
}
130
121
) ;
131
122
}
@@ -135,19 +126,23 @@ export default class AsbindInstance {
135
126
const unboundExports = this . loadedModule . exports ;
136
127
this . exports = { } ;
137
128
138
- Object . keys ( unboundExports ) . forEach ( unboundExportKey => {
139
- if (
140
- typeof unboundExports [ unboundExportKey ] === "function" &&
141
- ! isReservedExportKey ( unboundExportKey )
142
- ) {
143
- // Wrap the export
144
- this . exports [ unboundExportKey ] = bindExportFunction (
145
- this ,
146
- unboundExportKey
147
- ) ;
148
- } else {
149
- this . exports [ unboundExportKey ] = unboundExports [ unboundExportKey ] ;
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
+ }
150
145
}
151
- } ) ;
146
+ ) ;
152
147
}
153
148
}
0 commit comments