@@ -11,14 +11,23 @@ import 'package:ffi/ffi.dart';
11
11
12
12
/// WasmModule is a compiled module that can be instantiated.
13
13
class WasmModule {
14
- Pointer <WasmerModule > _module;
14
+ Pointer <WasmerStore > _store;
15
+ late Pointer <WasmerModule > _module;
15
16
16
17
/// Compile a module.
17
- WasmModule (Uint8List data) : _module = WasmRuntime ().compile (data) {}
18
+ WasmModule (Uint8List data) : _store = WasmRuntime ().newStore () {
19
+ _module = WasmRuntime ().compile (_store, data);
20
+ }
18
21
19
22
/// Instantiate the module with the given imports.
20
23
WasmInstance instantiate (WasmImports imports) {
21
- return WasmInstance (_module, imports);
24
+ return WasmInstance (_store, _module, imports);
25
+ }
26
+
27
+ /// Create a new memory with the given number of initial pages, and optional
28
+ /// maximum number of pages.
29
+ WasmMemory createMemory (int pages, [int ? maxPages]) {
30
+ return WasmMemory ._create (_store, pages, maxPages);
22
31
}
23
32
24
33
/// Returns a description of all of the module's imports and exports, for
@@ -28,12 +37,12 @@ class WasmModule {
28
37
var runtime = WasmRuntime ();
29
38
var imports = runtime.importDescriptors (_module);
30
39
for (var imp in imports) {
31
- var kind = wasmerImpExpKindName (imp.kind);
40
+ var kind = wasmerExternKindName (imp.kind);
32
41
description.write ('import $kind : ${imp .moduleName }::${imp .name }\n ' );
33
42
}
34
43
var exports = runtime.exportDescriptors (_module);
35
44
for (var exp in exports) {
36
- var kind = wasmerImpExpKindName (exp.kind);
45
+ var kind = wasmerExternKindName (exp.kind);
37
46
description.write ('export $kind : ${exp .name }\n ' );
38
47
}
39
48
return description.toString ();
@@ -42,13 +51,13 @@ class WasmModule {
42
51
43
52
/// WasmImports holds all the imports for a WasmInstance.
44
53
class WasmImports {
45
- Pointer <WasmerImport > _imports;
54
+ Pointer <Pointer < WasmerExtern > > _imports;
46
55
int _capacity;
47
56
int _length;
48
57
49
58
/// Create an imports object.
50
59
WasmImports ([this ._capacity = 4 ])
51
- : _imports = allocate <WasmerImport >(count: _capacity),
60
+ : _imports = allocate <Pointer < WasmerExtern > >(count: _capacity),
52
61
_length = 0 {}
53
62
54
63
/// Returns the number of imports.
@@ -57,26 +66,31 @@ class WasmImports {
57
66
58
67
/// WasmInstance is an instantiated WasmModule.
59
68
class WasmInstance {
69
+ Pointer <WasmerStore > _store;
60
70
Pointer <WasmerModule > _module;
61
71
Pointer <WasmerInstance > _instance;
62
72
Pointer <WasmerMemory >? _exportedMemory;
63
73
Map <String , WasmFunction > _functions = {};
64
74
65
- WasmInstance (this ._module, WasmImports imports)
75
+ WasmInstance (this ._store, this . _module, WasmImports imports)
66
76
: _instance = WasmRuntime ()
67
- .instantiate (_module, imports._imports, imports.length) {
77
+ .instantiate (_store, _module, imports._imports, imports.length) {
68
78
var runtime = WasmRuntime ();
69
- var exps = runtime.exports (_instance);
70
- for (var e in exps) {
71
- var kind = runtime.exportKind (e);
72
- String name = runtime.exportName (e);
73
- if (kind == WasmerImpExpKindFunction ) {
74
- var f = runtime.exportToFunction (e);
79
+ var exports = runtime.exports (_instance);
80
+ var exportDescs = runtime.exportDescriptors (_module);
81
+ assert (exports.ref.length == exportDescs.length);
82
+ for (var i = 0 ; i < exports.ref.length; ++ i) {
83
+ var e = exports.ref.data[i];
84
+ var kind = runtime.externKind (exports.ref.data[i]);
85
+ String name = exportDescs[i].name;
86
+ if (kind == WasmerExternKindFunction ) {
87
+ var f = runtime.externToFunction (e);
88
+ var ft = exportDescs[i].funcType;
75
89
_functions[name] = WasmFunction (
76
- name, f, runtime.getArgTypes (f ), runtime.getReturnType (f ));
77
- } else if (kind == WasmerImpExpKindMemory ) {
90
+ name, f, runtime.getArgTypes (ft ), runtime.getReturnType (ft ));
91
+ } else if (kind == WasmerExternKindMemory ) {
78
92
// WASM currently allows only one memory per module.
79
- _exportedMemory = runtime.exportToMemory (e);
93
+ _exportedMemory = runtime.externToMemory (e);
80
94
}
81
95
}
82
96
}
@@ -107,8 +121,8 @@ class WasmMemory {
107
121
108
122
/// Create a new memory with the given number of initial pages, and optional
109
123
/// maximum number of pages.
110
- WasmMemory ( int pages, [ int ? maxPages] )
111
- : _mem = WasmRuntime ().newMemory (pages, maxPages) {
124
+ WasmMemory . _create ( Pointer < WasmerStore > store, int pages, int ? maxPages)
125
+ : _mem = WasmRuntime ().newMemory (store, pages, maxPages) {
112
126
_view = WasmRuntime ().memoryView (_mem);
113
127
}
114
128
0 commit comments