@@ -471,25 +471,34 @@ class WasmFunction {
471
471
472
472
class WasmInstance {
473
473
public:
474
- WasmInstance () : _instance(nullptr ), _exports(nullptr ) {}
474
+ WasmInstance () : _instance(nullptr ), _exports(nullptr ), _memory( nullptr ) {}
475
475
476
476
bool Instantiate (wasmer_module_t * module, WasmImports* imports) {
477
+ ASSERT (_instance == nullptr );
478
+
477
479
// Instantiate module.
478
480
if (wasmer_module_instantiate (module, &_instance, imports->RawImports (),
479
481
imports->NumImports ()) !=
480
482
wasmer_result_t ::WASMER_OK) {
481
483
return false ;
482
484
}
483
485
484
- // Load all functions .
486
+ // Load all exports .
485
487
wasmer_instance_exports (_instance, &_exports);
486
488
intptr_t num_exports = wasmer_exports_len (_exports);
487
489
for (intptr_t i = 0 ; i < num_exports; ++i) {
488
490
wasmer_export_t * exp = wasmer_exports_get (_exports, i);
489
- if (wasmer_export_kind (exp ) == wasmer_import_export_kind::WASM_FUNCTION) {
491
+ wasmer_import_export_kind kind = wasmer_export_kind (exp );
492
+ if (kind == wasmer_import_export_kind::WASM_FUNCTION) {
490
493
if (!AddFunction (exp )) {
491
494
return false ;
492
495
}
496
+ } else if (kind == wasmer_import_export_kind::WASM_MEMORY) {
497
+ ASSERT (_memory == nullptr );
498
+ if (wasmer_export_to_memory (exp , &_memory) !=
499
+ wasmer_result_t ::WASMER_OK) {
500
+ return false ;
501
+ }
493
502
}
494
503
}
495
504
@@ -541,10 +550,13 @@ class WasmInstance {
541
550
o << ' }' << std::endl;
542
551
}
543
552
553
+ wasmer_memory_t * memory () { return _memory; }
554
+
544
555
private:
545
556
wasmer_instance_t * _instance;
546
557
wasmer_exports_t * _exports;
547
558
MallocDirectChainedHashMap<CStringKeyValueTrait<WasmFunction*>> _functions;
559
+ wasmer_memory_t * _memory;
548
560
549
561
static classid_t ToDartType (wasmer_value_tag wasm_type) {
550
562
switch (wasm_type) {
@@ -840,6 +852,36 @@ DEFINE_NATIVE_ENTRY(Wasm_initInstance, 0, 3) {
840
852
return Object::null ();
841
853
}
842
854
855
+ DEFINE_NATIVE_ENTRY (Wasm_initMemoryFromInstance, 0 , 2 ) {
856
+ GET_NON_NULL_NATIVE_ARGUMENT (Instance, mem_wrap, arguments->NativeArgAt (0 ));
857
+ GET_NON_NULL_NATIVE_ARGUMENT (Instance, inst_wrap, arguments->NativeArgAt (1 ));
858
+
859
+ ASSERT (mem_wrap.NumNativeFields () == 1 );
860
+ ASSERT (inst_wrap.NumNativeFields () == 1 );
861
+
862
+ WasmInstance* inst =
863
+ reinterpret_cast <WasmInstance*>(inst_wrap.GetNativeField (0 ));
864
+
865
+ wasmer_memory_t * memory = inst->memory ();
866
+
867
+ mem_wrap.SetNativeField (0 , reinterpret_cast <intptr_t >(memory));
868
+ FinalizablePersistentHandle::New (thread->isolate (), mem_wrap, memory,
869
+ FinalizeWasmMemory,
870
+ wasmer_memory_length (memory));
871
+ return WasmMemoryToExternalTypedData (memory);
872
+ }
873
+
874
+ DEFINE_NATIVE_ENTRY (Wasm_getMemoryPages, 0 , 1 ) {
875
+ GET_NON_NULL_NATIVE_ARGUMENT (Instance, mem_wrap, arguments->NativeArgAt (0 ));
876
+
877
+ ASSERT (mem_wrap.NumNativeFields () == 1 );
878
+
879
+ wasmer_memory_t * memory =
880
+ reinterpret_cast <wasmer_memory_t *>(mem_wrap.GetNativeField (0 ));
881
+
882
+ return Integer::New (wasmer_memory_length (memory));
883
+ }
884
+
843
885
DEFINE_NATIVE_ENTRY (Wasm_initFunction, 0 , 4 ) {
844
886
GET_NON_NULL_NATIVE_ARGUMENT (Instance, fn_wrap, arguments->NativeArgAt (0 ));
845
887
GET_NON_NULL_NATIVE_ARGUMENT (Instance, inst_wrap, arguments->NativeArgAt (1 ));
@@ -978,6 +1020,16 @@ DEFINE_NATIVE_ENTRY(Wasm_initInstance, 0, 3) {
978
1020
return nullptr ;
979
1021
}
980
1022
1023
+ DEFINE_NATIVE_ENTRY (Wasm_initMemoryFromInstance, 0 , 2 ) {
1024
+ Exceptions::ThrowUnsupportedError (" WASM is disabled" );
1025
+ return nullptr ;
1026
+ }
1027
+
1028
+ DEFINE_NATIVE_ENTRY (Wasm_getMemoryPages, 0 , 1 ) {
1029
+ Exceptions::ThrowUnsupportedError (" WASM is disabled" );
1030
+ return nullptr ;
1031
+ }
1032
+
981
1033
DEFINE_NATIVE_ENTRY (Wasm_initFunction, 0 , 4 ) {
982
1034
Exceptions::ThrowUnsupportedError (" WASM is disabled" );
983
1035
return nullptr ;
0 commit comments