@@ -49,13 +49,11 @@ class WasmModule {
4949}
5050
5151Pointer <WasmerTrap > _wasmFnImportTrampoline (Pointer <_WasmFnImport > imp,
52- Pointer <WasmerVal > args, Pointer <WasmerVal > results) {
52+ Pointer <WasmerValVec > args, Pointer <WasmerValVec > results) {
5353 try {
5454 _WasmFnImport ._call (imp, args, results);
55- } catch (e) {
56- // TODO: Use WasmerTrap to handle this case. For now just print the
57- // exception (if we ignore it, FFI will silently return a default result).
58- print (e);
55+ } catch (exception) {
56+ return WasmRuntime ().newTrap (imp.ref.store, exception);
5957 }
6058 return nullptr;
6159}
@@ -66,43 +64,45 @@ void _wasmFnImportFinalizer(Pointer<_WasmFnImport> imp) {
6664}
6765
6866final _wasmFnImportTrampolineNative = Pointer .fromFunction<
69- Pointer <WasmerTrap > Function (Pointer <_WasmFnImport >, Pointer <WasmerVal >,
70- Pointer <WasmerVal >)> (_wasmFnImportTrampoline);
67+ Pointer <WasmerTrap > Function (Pointer <_WasmFnImport >, Pointer <WasmerValVec >,
68+ Pointer <WasmerValVec >)> (_wasmFnImportTrampoline);
7169final _wasmFnImportToFn = < int , Function > {};
7270final _wasmFnImportFinalizerNative =
7371 Pointer .fromFunction< Void Function (Pointer <_WasmFnImport >)> (
7472 _wasmFnImportFinalizer);
7573
7674class _WasmFnImport extends Struct {
77- @Int 32()
78- external int numArgs;
79-
8075 @Int 32()
8176 external int returnType;
8277
83- static void _call (Pointer <_WasmFnImport > imp, Pointer <WasmerVal > rawArgs,
84- Pointer <WasmerVal > rawResult) {
78+ external Pointer <WasmerStore > store;
79+
80+ static void _call (Pointer <_WasmFnImport > imp, Pointer <WasmerValVec > rawArgs,
81+ Pointer <WasmerValVec > rawResult) {
8582 Function fn = _wasmFnImportToFn[imp.address] as Function ;
8683 var args = [];
87- for (var i = 0 ; i < imp .ref.numArgs ; ++ i) {
88- args.add (rawArgs[i].toDynamic);
84+ for (var i = 0 ; i < rawArgs .ref.length ; ++ i) {
85+ args.add (rawArgs.ref.data [i].toDynamic);
8986 }
87+ assert (
88+ rawResult.ref.length == 1 || imp.ref.returnType == WasmerValKindVoid );
9089 var result = Function .apply (fn, args);
91- switch (imp.ref.returnType) {
92- case WasmerValKindI32 :
93- rawResult.ref.i32 = result;
94- break ;
95- case WasmerValKindI64 :
96- rawResult.ref.i64 = result;
97- break ;
98- case WasmerValKindF32 :
99- rawResult.ref.f32 = result;
100- break ;
101- case WasmerValKindF64 :
102- rawResult.ref.f64 = result;
103- break ;
104- case WasmerValKindVoid :
105- // Do nothing.
90+ if (imp.ref.returnType != WasmerValKindVoid ) {
91+ rawResult.ref.data[0 ].kind = imp.ref.returnType;
92+ switch (imp.ref.returnType) {
93+ case WasmerValKindI32 :
94+ rawResult.ref.data[0 ].i32 = result;
95+ break ;
96+ case WasmerValKindI64 :
97+ rawResult.ref.data[0 ].i64 = result;
98+ break ;
99+ case WasmerValKindF32 :
100+ rawResult.ref.data[0 ].f32 = result;
101+ break ;
102+ case WasmerValKindF64 :
103+ rawResult.ref.data[0 ].f64 = result;
104+ break ;
105+ }
106106 }
107107 }
108108}
@@ -113,24 +113,26 @@ class WasmInstanceBuilder {
113113 WasmModule _module;
114114 late List <WasmImportDescriptor > _importDescs;
115115 Map <String , int > _importIndex;
116- late Pointer <Pointer < WasmerExtern >> _imports;
116+ Pointer <WasmerExternVec > _imports = allocate < WasmerExternVec >() ;
117117 Pointer <WasmerWasiEnv > _wasiEnv = nullptr;
118118
119119 WasmInstanceBuilder (this ._module) : _importIndex = {} {
120120 _importDescs = WasmRuntime ().importDescriptors (_module._module);
121- _imports = allocate <Pointer <WasmerExtern >>(count: _importDescs.length);
121+ _imports.ref.length = _importDescs.length;
122+ _imports.ref.data =
123+ allocate <Pointer <WasmerExtern >>(count: _importDescs.length);
122124 for (var i = 0 ; i < _importDescs.length; ++ i) {
123125 var imp = _importDescs[i];
124126 _importIndex["${imp .moduleName }::${imp .name }" ] = i;
125- _imports[i] = nullptr;
127+ _imports.ref.data [i] = nullptr;
126128 }
127129 }
128130
129131 int _getIndex (String moduleName, String name) {
130132 var index = _importIndex["${moduleName }::${name }" ];
131133 if (index == null ) {
132134 throw Exception ("Import not found: ${moduleName }::${name }" );
133- } else if (_imports[index] != nullptr) {
135+ } else if (_imports.ref.data [index] != nullptr) {
134136 throw Exception ("Import already filled: ${moduleName }::${name }" );
135137 } else {
136138 return index;
@@ -145,7 +147,7 @@ class WasmInstanceBuilder {
145147 if (imp.kind != WasmerExternKindMemory ) {
146148 throw Exception ("Import is not a memory: $imp " );
147149 }
148- _imports[index] = WasmRuntime ().memoryToExtern (memory._mem);
150+ _imports.ref.data [index] = WasmRuntime ().memoryToExtern (memory._mem);
149151 return this ;
150152 }
151153
@@ -162,16 +164,16 @@ class WasmInstanceBuilder {
162164 var argTypes = runtime.getArgTypes (imp.funcType);
163165 var returnType = runtime.getReturnType (imp.funcType);
164166 var wasmFnImport = allocate <_WasmFnImport >();
165- wasmFnImport.ref.numArgs = argTypes.length;
166167 wasmFnImport.ref.returnType = returnType;
168+ wasmFnImport.ref.store = _module._store;
167169 _wasmFnImportToFn[wasmFnImport.address] = fn;
168170 var fnImp = runtime.newFunc (
169171 _module._store,
170172 imp.funcType,
171173 _wasmFnImportTrampolineNative,
172174 wasmFnImport,
173175 _wasmFnImportFinalizerNative);
174- _imports[index] = runtime.functionToExtern (fnImp);
176+ _imports.ref.data [index] = runtime.functionToExtern (fnImp);
175177 return this ;
176178 }
177179
@@ -193,7 +195,7 @@ class WasmInstanceBuilder {
193195 /// Build the module instance.
194196 WasmInstance build () {
195197 for (var i = 0 ; i < _importDescs.length; ++ i) {
196- if (_imports[i] == nullptr) {
198+ if (_imports.ref.data [i] == nullptr) {
197199 throw Exception ("Missing import: ${_importDescs [i ]}" );
198200 }
199201 }
@@ -211,8 +213,7 @@ class WasmInstance {
211213 Stream <List <int >>? _stderr;
212214 Map <String , WasmFunction > _functions = {};
213215
214- WasmInstance (
215- this ._module, Pointer <Pointer <WasmerExtern >> imports, this ._wasiEnv)
216+ WasmInstance (this ._module, Pointer <WasmerExternVec > imports, this ._wasiEnv)
216217 : _instance = WasmRuntime ()
217218 .instantiate (_module._store, _module._module, imports) {
218219 var runtime = WasmRuntime ();
0 commit comments