@@ -49,13 +49,11 @@ class WasmModule {
49
49
}
50
50
51
51
Pointer <WasmerTrap > _wasmFnImportTrampoline (Pointer <_WasmFnImport > imp,
52
- Pointer <WasmerVal > args, Pointer <WasmerVal > results) {
52
+ Pointer <WasmerValVec > args, Pointer <WasmerValVec > results) {
53
53
try {
54
54
_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);
59
57
}
60
58
return nullptr;
61
59
}
@@ -66,43 +64,45 @@ void _wasmFnImportFinalizer(Pointer<_WasmFnImport> imp) {
66
64
}
67
65
68
66
final _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);
71
69
final _wasmFnImportToFn = < int , Function > {};
72
70
final _wasmFnImportFinalizerNative =
73
71
Pointer .fromFunction< Void Function (Pointer <_WasmFnImport >)> (
74
72
_wasmFnImportFinalizer);
75
73
76
74
class _WasmFnImport extends Struct {
77
- @Int 32()
78
- external int numArgs;
79
-
80
75
@Int 32()
81
76
external int returnType;
82
77
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) {
85
82
Function fn = _wasmFnImportToFn[imp.address] as Function ;
86
83
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);
89
86
}
87
+ assert (
88
+ rawResult.ref.length == 1 || imp.ref.returnType == WasmerValKindVoid );
90
89
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
+ }
106
106
}
107
107
}
108
108
}
@@ -113,24 +113,26 @@ class WasmInstanceBuilder {
113
113
WasmModule _module;
114
114
late List <WasmImportDescriptor > _importDescs;
115
115
Map <String , int > _importIndex;
116
- late Pointer <Pointer < WasmerExtern >> _imports;
116
+ Pointer <WasmerExternVec > _imports = allocate < WasmerExternVec >() ;
117
117
Pointer <WasmerWasiEnv > _wasiEnv = nullptr;
118
118
119
119
WasmInstanceBuilder (this ._module) : _importIndex = {} {
120
120
_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);
122
124
for (var i = 0 ; i < _importDescs.length; ++ i) {
123
125
var imp = _importDescs[i];
124
126
_importIndex["${imp .moduleName }::${imp .name }" ] = i;
125
- _imports[i] = nullptr;
127
+ _imports.ref.data [i] = nullptr;
126
128
}
127
129
}
128
130
129
131
int _getIndex (String moduleName, String name) {
130
132
var index = _importIndex["${moduleName }::${name }" ];
131
133
if (index == null ) {
132
134
throw Exception ("Import not found: ${moduleName }::${name }" );
133
- } else if (_imports[index] != nullptr) {
135
+ } else if (_imports.ref.data [index] != nullptr) {
134
136
throw Exception ("Import already filled: ${moduleName }::${name }" );
135
137
} else {
136
138
return index;
@@ -145,7 +147,7 @@ class WasmInstanceBuilder {
145
147
if (imp.kind != WasmerExternKindMemory ) {
146
148
throw Exception ("Import is not a memory: $imp " );
147
149
}
148
- _imports[index] = WasmRuntime ().memoryToExtern (memory._mem);
150
+ _imports.ref.data [index] = WasmRuntime ().memoryToExtern (memory._mem);
149
151
return this ;
150
152
}
151
153
@@ -162,16 +164,16 @@ class WasmInstanceBuilder {
162
164
var argTypes = runtime.getArgTypes (imp.funcType);
163
165
var returnType = runtime.getReturnType (imp.funcType);
164
166
var wasmFnImport = allocate <_WasmFnImport >();
165
- wasmFnImport.ref.numArgs = argTypes.length;
166
167
wasmFnImport.ref.returnType = returnType;
168
+ wasmFnImport.ref.store = _module._store;
167
169
_wasmFnImportToFn[wasmFnImport.address] = fn;
168
170
var fnImp = runtime.newFunc (
169
171
_module._store,
170
172
imp.funcType,
171
173
_wasmFnImportTrampolineNative,
172
174
wasmFnImport,
173
175
_wasmFnImportFinalizerNative);
174
- _imports[index] = runtime.functionToExtern (fnImp);
176
+ _imports.ref.data [index] = runtime.functionToExtern (fnImp);
175
177
return this ;
176
178
}
177
179
@@ -193,7 +195,7 @@ class WasmInstanceBuilder {
193
195
/// Build the module instance.
194
196
WasmInstance build () {
195
197
for (var i = 0 ; i < _importDescs.length; ++ i) {
196
- if (_imports[i] == nullptr) {
198
+ if (_imports.ref.data [i] == nullptr) {
197
199
throw Exception ("Missing import: ${_importDescs [i ]}" );
198
200
}
199
201
}
@@ -211,8 +213,7 @@ class WasmInstance {
211
213
Stream <List <int >>? _stderr;
212
214
Map <String , WasmFunction > _functions = {};
213
215
214
- WasmInstance (
215
- this ._module, Pointer <Pointer <WasmerExtern >> imports, this ._wasiEnv)
216
+ WasmInstance (this ._module, Pointer <WasmerExternVec > imports, this ._wasiEnv)
216
217
: _instance = WasmRuntime ()
217
218
.instantiate (_module._store, _module._module, imports) {
218
219
var runtime = WasmRuntime ();
0 commit comments