MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

allow wasm memory.grow

+37 -3
+37 -3
src/modules/wasm.c
··· 2 2 #include <stdlib.h> 3 3 #include <string.h> 4 4 #include <inttypes.h> 5 + #include <wasm_c_api.h> 6 + #include <wasm_export.h> 7 + 8 + #pragma clang diagnostic push 9 + #pragma clang diagnostic ignored "-Wundef" 10 + #pragma clang diagnostic ignored "-Wimplicit-int-conversion" 11 + #include <wasm_c_api_internal.h> 12 + #include <wasm_runtime.h> 13 + #pragma clang diagnostic pop 5 14 6 15 #include "ant.h" 7 16 #include "errors.h" ··· 14 23 #include "modules/buffer.h" 15 24 #include "modules/wasm.h" 16 25 #include "modules/wasi.h" 17 - #include "wasm_c_api.h" 18 26 19 27 typedef struct { 20 28 wasm_store_t *store; ··· 813 821 if (imports && import_types.size > 0) 814 822 import_vec = (wasm_extern_vec_t){ import_types.size, imports, import_types.size, sizeof(*imports), NULL }; 815 823 816 - instance = wasm_instance_new(module_handle->store, module_handle->module, &import_vec, &trap); 824 + instance = wasm_instance_new_with_args(module_handle->store, module_handle->module, &import_vec, &trap, KILOBYTE(32), 0); 817 825 } 818 826 819 827 free(imports); ··· 1013 1021 } 1014 1022 1015 1023 static ant_value_t js_wasm_memory_grow(ant_t *js, ant_value_t *args, int nargs) { 1016 - return js_mkerr_typed(js, JS_ERR_TYPE, "The current WAMR backend does not support host-side memory.grow"); 1024 + wasm_extern_handle_t *handle = wasm_extern_handle(js->this_val, WASM_EXTERN_WRAP_MEMORY); 1025 + wasm_memory_pages_t old_size; 1026 + uint32_t delta; 1027 + 1028 + if (!handle || !handle->as.memory) 1029 + return js_mkerr_typed(js, JS_ERR_TYPE, "Expected a WebAssembly.Memory"); 1030 + 1031 + delta = (uint32_t)(nargs > 0 ? js_to_number(js, args[0]) : 0); 1032 + old_size = wasm_memory_size(handle->as.memory); 1033 + 1034 + if (delta == 0) return js_mknum((double)old_size); 1035 + 1036 + wasm_module_inst_t inst = (wasm_module_inst_t)handle->as.memory->inst_comm_rt; 1037 + if (!inst) 1038 + return js_mkerr_typed(js, JS_ERR_RANGE, "Memory instance not available"); 1039 + 1040 + if (inst->module_type == Wasm_Module_Bytecode) { 1041 + WASMModuleInstance *wasm_inst = (WASMModuleInstance *)inst; 1042 + WASMMemoryInstance *mem_inst = wasm_inst->memories[handle->as.memory->memory_idx_rt]; 1043 + uint32_t needed = mem_inst->cur_page_count + delta; 1044 + if (needed > mem_inst->max_page_count) mem_inst->max_page_count = needed; 1045 + } 1046 + 1047 + if (!wasm_runtime_enlarge_memory(inst, (uint64_t)delta)) 1048 + return js_mkerr_typed(js, JS_ERR_RANGE, "Failed to grow memory by %u pages", delta); 1049 + 1050 + return js_mknum((double)old_size); 1017 1051 } 1018 1052 1019 1053 static ant_value_t js_wasm_memory_ctor(ant_t *js, ant_value_t *args, int nargs) {