@@ -34,6 +34,18 @@ static void Finalize(void* isolate_callback_data,
34
34
delete reinterpret_cast <T*>(peer);
35
35
}
36
36
37
+ static void FinalizeWasmModule (void * isolate_callback_data,
38
+ Dart_WeakPersistentHandle handle,
39
+ void * module) {
40
+ wasmer_module_destroy (reinterpret_cast <wasmer_module_t *>(module));
41
+ }
42
+
43
+ static void FinalizeWasmMemory (void * isolate_callback_data,
44
+ Dart_WeakPersistentHandle handle,
45
+ void * memory) {
46
+ wasmer_memory_destroy (reinterpret_cast <wasmer_memory_t *>(memory));
47
+ }
48
+
37
49
static std::unique_ptr<char []> ToUTF8 (const String& str) {
38
50
const intptr_t str_size = Utf8::Length (str);
39
51
auto str_raw = std::unique_ptr<char []>(new char [str_size + 1 ]);
@@ -87,53 +99,11 @@ static RawObject* ToDartObject(wasmer_value_t ret) {
87
99
}
88
100
}
89
101
90
- class WasmModule {
91
- public:
92
- WasmModule (uint8_t * data, intptr_t len) {
93
- ThrowIfFailed (wasmer_compile (&_module, data, len));
94
- }
95
-
96
- ~WasmModule () { wasmer_module_destroy (_module); }
97
- wasmer_module_t * module () { return _module; }
98
-
99
- private:
100
- wasmer_module_t * _module;
101
-
102
- DISALLOW_COPY_AND_ASSIGN (WasmModule);
103
- };
104
-
105
- class WasmMemory {
106
- public:
107
- WasmMemory (uint32_t init, int64_t max) {
108
- wasmer_limits_t descriptor;
109
- descriptor.min = init;
110
- if (max < 0 ) {
111
- descriptor.max .has_some = false ;
112
- } else {
113
- descriptor.max .has_some = true ;
114
- descriptor.max .some = max;
115
- }
116
- ThrowIfFailed (wasmer_memory_new (&_memory, descriptor));
117
- }
118
-
119
- ~WasmMemory () { wasmer_memory_destroy (_memory); }
120
- wasmer_memory_t * memory () { return _memory; }
121
-
122
- void Grow (intptr_t delta) {
123
- ThrowIfFailed (wasmer_memory_grow (_memory, delta));
124
- }
125
-
126
- RawExternalTypedData* ToExternalTypedData () {
127
- uint8_t * data = wasmer_memory_data (_memory);
128
- uint32_t size = wasmer_memory_data_length (_memory);
129
- return ExternalTypedData::New (kExternalTypedDataUint8ArrayCid , data, size);
130
- }
131
-
132
- private:
133
- wasmer_memory_t * _memory;
134
-
135
- DISALLOW_COPY_AND_ASSIGN (WasmMemory);
136
- };
102
+ RawExternalTypedData* WasmMemoryToExternalTypedData (wasmer_memory_t * memory) {
103
+ uint8_t * data = wasmer_memory_data (memory);
104
+ uint32_t size = wasmer_memory_data_length (memory);
105
+ return ExternalTypedData::New (kExternalTypedDataUint8ArrayCid , data, size);
106
+ }
137
107
138
108
class WasmImports {
139
109
public:
@@ -152,9 +122,9 @@ class WasmImports {
152
122
size_t NumImports () const { return _imports.length (); }
153
123
wasmer_import_t * RawImports () { return _imports.data (); }
154
124
155
- void AddMemory (std::unique_ptr<char []> name, WasmMemory * memory) {
125
+ void AddMemory (std::unique_ptr<char []> name, wasmer_memory_t * memory) {
156
126
AddImport (std::move (name), wasmer_import_export_kind::WASM_MEMORY)->memory =
157
- memory-> memory () ;
127
+ memory;
158
128
}
159
129
160
130
void AddGlobal (std::unique_ptr<char []> name,
@@ -256,11 +226,10 @@ class WasmFunction {
256
226
257
227
class WasmInstance {
258
228
public:
259
- explicit WasmInstance (WasmModule * module, WasmImports* imports) {
229
+ explicit WasmInstance (wasmer_module_t * module, WasmImports* imports) {
260
230
// Instantiate module.
261
- ThrowIfFailed (wasmer_module_instantiate (module->module (), &_instance,
262
- imports->RawImports (),
263
- imports->NumImports ()));
231
+ ThrowIfFailed (wasmer_module_instantiate (
232
+ module, &_instance, imports->RawImports (), imports->NumImports ()));
264
233
265
234
// Load all functions.
266
235
wasmer_instance_exports (_instance, &_exports);
@@ -381,15 +350,15 @@ DEFINE_NATIVE_ENTRY(Wasm_initModule, 0, 2) {
381
350
memcpy (data_copy.get (), data.DataAddr (0 ), len); // NOLINT
382
351
}
383
352
384
- WasmModule * module;
353
+ wasmer_module_t * module;
385
354
{
386
355
TransitionVMToNative transition (thread);
387
- module = new WasmModule ( data_copy.get (), len);
356
+ ThrowIfFailed ( wasmer_compile (& module, data_copy.get (), len) );
388
357
}
389
358
390
359
mod_wrap.SetNativeField (0 , reinterpret_cast <intptr_t >(module));
391
360
FinalizablePersistentHandle::New (thread->isolate (), mod_wrap, module,
392
- Finalize<WasmModule>, sizeof (WasmModule) );
361
+ FinalizeWasmModule, len );
393
362
394
363
return Object::null ();
395
364
}
@@ -419,8 +388,8 @@ DEFINE_NATIVE_ENTRY(Wasm_addMemoryImport, 0, 3) {
419
388
420
389
WasmImports* imports =
421
390
reinterpret_cast <WasmImports*>(imp_wrap.GetNativeField (0 ));
422
- WasmMemory * memory =
423
- reinterpret_cast <WasmMemory *>(mem_wrap.GetNativeField (0 ));
391
+ wasmer_memory_t * memory =
392
+ reinterpret_cast <wasmer_memory_t *>(mem_wrap.GetNativeField (0 ));
424
393
425
394
imports->AddMemory (ToUTF8 (name), memory);
426
395
@@ -455,13 +424,23 @@ DEFINE_NATIVE_ENTRY(Wasm_initMemory, 0, 3) {
455
424
GET_NATIVE_ARGUMENT (Integer, max, arguments->NativeArgAt (2 ));
456
425
457
426
ASSERT (mem_wrap.NumNativeFields () == 1 );
458
-
459
- WasmMemory* memory = new WasmMemory (init.AsInt64Value (),
460
- max.IsNull () ? -1 : max.AsInt64Value ());
427
+ const int64_t init_size = init.AsInt64Value ();
428
+ const int64_t max_size = max.AsInt64Value ();
429
+
430
+ wasmer_memory_t * memory;
431
+ wasmer_limits_t descriptor;
432
+ descriptor.min = init_size;
433
+ if (max_size < 0 ) {
434
+ descriptor.max .has_some = false ;
435
+ } else {
436
+ descriptor.max .has_some = true ;
437
+ descriptor.max .some = max_size;
438
+ }
439
+ ThrowIfFailed (wasmer_memory_new (&memory, descriptor));
461
440
mem_wrap.SetNativeField (0 , reinterpret_cast <intptr_t >(memory));
462
441
FinalizablePersistentHandle::New (thread->isolate (), mem_wrap, memory,
463
- Finalize<WasmMemory>, sizeof (WasmMemory) );
464
- return memory-> ToExternalTypedData ( );
442
+ FinalizeWasmMemory, init_size );
443
+ return WasmMemoryToExternalTypedData (memory );
465
444
}
466
445
467
446
DEFINE_NATIVE_ENTRY (Wasm_growMemory, 0 , 2 ) {
@@ -470,10 +449,10 @@ DEFINE_NATIVE_ENTRY(Wasm_growMemory, 0, 2) {
470
449
471
450
ASSERT (mem_wrap.NumNativeFields () == 1 );
472
451
473
- WasmMemory * memory =
474
- reinterpret_cast <WasmMemory *>(mem_wrap.GetNativeField (0 ));
475
- memory-> Grow ( delta.AsInt64Value ());
476
- return memory-> ToExternalTypedData ( );
452
+ wasmer_memory_t * memory =
453
+ reinterpret_cast <wasmer_memory_t *>(mem_wrap.GetNativeField (0 ));
454
+ ThrowIfFailed ( wasmer_memory_grow (memory, delta.AsInt64Value () ));
455
+ return WasmMemoryToExternalTypedData (memory );
477
456
}
478
457
479
458
DEFINE_NATIVE_ENTRY (Wasm_initInstance, 0 , 3 ) {
@@ -485,8 +464,8 @@ DEFINE_NATIVE_ENTRY(Wasm_initInstance, 0, 3) {
485
464
ASSERT (mod_wrap.NumNativeFields () == 1 );
486
465
ASSERT (imp_wrap.NumNativeFields () == 1 );
487
466
488
- WasmModule * module =
489
- reinterpret_cast <WasmModule *>(mod_wrap.GetNativeField (0 ));
467
+ wasmer_module_t * module =
468
+ reinterpret_cast <wasmer_module_t *>(mod_wrap.GetNativeField (0 ));
490
469
WasmImports* imports =
491
470
reinterpret_cast <WasmImports*>(imp_wrap.GetNativeField (0 ));
492
471
0 commit comments