diff --git a/build.bat b/build.bat index 9e38dcc..413aa6a 100644 --- a/build.bat +++ b/build.bat @@ -56,7 +56,7 @@ if %target% == orca ( ::compile orca set INCLUDES=/I src /I sdk /I ext\bytebox\include /I milepost\src /I milepost\ext - set LIBS=/LIBPATH:bin milepost.dll.lib ext\bytebox\lib\bytebox.lib + set LIBS=/LIBPATH:bin milepost.dll.lib ext\bytebox\lib\bytebox.lib ntdll.lib - cl /Zi /Zc:preprocessor /std:c11 /experimental:c11atomics %INCLUDES% src\main.c /link %LIBS% /out:bin\orca.exe + cl /Zi /Zc:preprocessor /std:c11 /experimental:c11atomics %INCLUDES% src\main.c /link %LIBS% /STACK:8388608,8388608 /out:bin\orca.exe ) diff --git a/ext/bytebox/include/bytebox.h b/ext/bytebox/include/bytebox.h index d6c533a..d4fc133 100644 --- a/ext/bytebox/include/bytebox.h +++ b/ext/bytebox/include/bytebox.h @@ -17,6 +17,8 @@ enum bb_error BB_ERROR_OUTOFMEMORY, BB_ERROR_INVALIDPARAM, BB_ERROR_UNKNOWNEXPORT, + BB_ERROR_UNKNOWNIMPORT, + BB_ERROR_INCOMPATIBLEIMPORT, }; typedef enum bb_error bb_error; @@ -47,44 +49,18 @@ struct bb_module_definition_init_opts }; typedef struct bb_module_definition_init_opts bb_module_definition_init_opts; -struct bb_module_definition -{ - void* module; -}; typedef struct bb_module_definition bb_module_definition; - -// struct bb_import_function -// { -// const char* name; -// bb_host_function* func; -// bb_valtype* params; -// size_t num_params; -// bb_valtype* returns; -// size_t num_returns; -// void* userdata; -// }; -// typedef struct bb_import_function bb_import_function; - -struct bb_import_package -{ - void* package; -}; +typedef struct bb_module_instance bb_module_instance; typedef struct bb_import_package bb_import_package; struct bb_module_instance_instantiate_opts { - bb_import_package* packages; + bb_import_package** packages; size_t num_packages; bool enable_debug; }; typedef struct bb_module_instance_instantiate_opts bb_module_instance_instantiate_opts; -struct bb_module_instance -{ - void* module; -}; -typedef struct bb_module_instance bb_module_instance; - struct bb_module_instance_invoke_opts { bool trap_on_start; @@ -116,6 +92,21 @@ typedef enum bb_debug_trap_mode bb_debug_trap_mode; typedef void bb_host_function(void* userdata, bb_module_instance* module, const bb_val* params, bb_val* returns); +enum bb_global_mut +{ + BB_GLOBAL_MUT_IMMUTABLE, + BB_GLOBAL_MUT_MUTABLE, +}; +typedef enum bb_global_mut bb_global_mut; + +struct bb_global +{ + bb_val* value; + bb_valtype type; + bb_global_mut mut; +}; +typedef struct bb_global bb_global; + // typedef void* bb_malloc_func(size_t size, void* userdata); // typedef void* bb_realloc_func(void* mem, size_t size, void* userdata); // typedef void bb_free_func(void* mem, void* userdata); @@ -124,20 +115,19 @@ typedef void bb_host_function(void* userdata, bb_module_instance* module, const const char* bb_error_str(bb_error err); -bb_module_definition bb_module_definition_init(bb_module_definition_init_opts opts); +bb_module_definition* bb_module_definition_init(bb_module_definition_init_opts opts); void bb_module_definition_deinit(bb_module_definition* definition); bb_error bb_module_definition_decode(bb_module_definition* definition, const char* data, size_t length); bb_slice bb_module_definition_get_custom_section(const bb_module_definition* definition, const char* name); -bb_import_package bb_import_package_init(const char* name); +bb_import_package* bb_import_package_init(const char* name); void bb_import_package_deinit(bb_import_package* package); // only deinit when all module_instances using the package have been deinited -void* bb_import_package_userdata(const bb_import_package* package); bb_error bb_import_package_add_function(bb_import_package* package, bb_host_function* func, const char* export_name, bb_valtype* params, size_t num_params, bb_valtype* returns, size_t num_returns, void* userdata); -bb_module_instance bb_module_instance_init(bb_module_definition* definition); +bb_module_instance* bb_module_instance_init(bb_module_definition* definition); void bb_module_instance_deinit(bb_module_instance* instance); bb_error bb_module_instance_instantiate(bb_module_instance* instance, bb_module_instance_instantiate_opts opts); -bb_func_handle bb_module_instance_find_func(bb_module_instance* instance, const char* func_name); +bb_error bb_module_instance_find_func(bb_module_instance* instance, const char* func_name, bb_func_handle* out_handle); bb_func_info bb_module_instance_func_info(bb_module_instance* instance, bb_func_handle handle); bb_error bb_module_instance_invoke(bb_module_instance* instance, bb_func_handle, const bb_val* params, size_t num_params, bb_val* returns, size_t num_returns, bb_module_instance_invoke_opts opts); bb_error bb_module_instance_resume(bb_module_instance* instance, bb_val* returns, size_t num_returns); @@ -145,3 +135,6 @@ bb_error bb_module_instance_step(bb_module_instance* instance, bb_val* returns, bb_error bb_module_instance_debug_set_trap(bb_module_instance* instance, uint32_t address, bb_debug_trap_mode trap_mode); void* bb_module_instance_mem(bb_module_instance* instance, size_t offset, size_t length); bb_slice bb_module_instance_mem_all(bb_module_instance* instance); +bb_global bb_module_instance_find_global(bb_module_instance* instance, const char* global_name); + +bool bb_func_handle_isvalid(bb_func_handle handle); diff --git a/scripts/bindgen_bb.py b/scripts/bindgen_bb.py index c50304b..9806d74 100644 --- a/scripts/bindgen_bb.py +++ b/scripts/bindgen_bb.py @@ -196,12 +196,9 @@ for decl in data: paramTypes = [] numParams = 0 if retTag == 'S': - retType = 'BB_VALTYPE_I32' # unused, but we need something to put in the the returns array since 0-size arrays are illegal in C paramTypes.append('BB_VALTYPE_I32') - elif retTag == 'v': #no returns - retType = 'BB_VALTYPE_I32' #unused but we need a dummy value - numReturns = 0 - else: + numParams += 1 + elif retTag != 'v': #no returns retType = translateTag(retTag) numReturns = 1 @@ -212,16 +209,20 @@ for decl in data: paramTypes.append(translateTag(tag)) numParams += 1 + # dummy values to avoid 0-length arrays in C + if numReturns == 0: + retType = 'BB_VALTYPE_I32' + if numParams == 0: - paramTypes.append('BB_VALTYPE_I32') # unused but need a dummy value to avoid 0-length array + paramTypes.append('BB_VALTYPE_I32') s += '\t{\n' s += '\t\tbb_valtype params[] = {' + ', '.join(paramTypes) + '};\n' s += '\t\tsize_t num_params = ' + str(len(paramTypes)) + ';\n' - s += '\t\tbb_valtype returns[] = {' + retType + '};\n' + s += '\t\tbb_valtype return_type = ' + retType + ';\n' s += '\t\tsize_t num_returns = ' + str(numReturns) + ';\n' - s += '\t\tbb_error err = bb_import_package_add_function(package, ' + cname + '_stub, "' + name + '", params, num_params, returns, num_returns, NULL);\n' + s += '\t\tbb_error err = bb_import_package_add_function(package, ' + cname + '_stub, "' + name + '", params, num_params, &return_type, num_returns, NULL);\n' s += '\t\tif(err != BB_ERROR_OK) { log_error("error: %s\\n", bb_error_str(err)); return(-1); }\n' s += '\t}\n' diff --git a/src/main.c b/src/main.c index 23b3839..76d1db5 100644 --- a/src/main.c +++ b/src/main.c @@ -288,6 +288,20 @@ void log_entry_ui(orca_debug_overlay* overlay, log_entry* entry) // } // } +bool tag_to_valtype(char tag, bb_valtype* out) +{ + switch (tag) + { + case 'i': *out = BB_VALTYPE_I32; return true; + case 'I': *out = BB_VALTYPE_I64; return true; + case 'f': *out = BB_VALTYPE_F32; return true; + case 'F': *out = BB_VALTYPE_F64; return true; + case 'd': *out = BB_VALTYPE_F64; return true; + } + + return false; +} + void orca_runtime_init(orca_runtime* runtime) { memset(runtime, 0, sizeof(orca_runtime)); @@ -305,19 +319,21 @@ void orca_runtime_init(orca_runtime* runtime) #include"gles_api_bind_gen.c" #include"manual_gles_api.c" -void* quit_on_wasm_init_failure(const char* stage, bb_error err) +i32 quit_on_wasm_init_failure(const char* stage, bb_error err) { const char* errStr = bb_error_str(err); - log_error("wasm error at init stage '%s': %s\n", errStr); + log_error("wasm error at init stage '%s': %s\n", stage, errStr); + + str8 msg = str8_pushf(mem_scratch(), "The application couldn't load: encountered fatal error at stage '%s': %s", stage, errStr); const char* options[] = {"OK"}; mp_alert_popup("Error", - "The application couldn't load: encountered fatal error at stage '%s': %s", - stage, - errStr); + msg.ptr, + 1, + options); mp_request_quit(); - return((void*)-1); + return(-1); } i32 orca_runloop(void* user) @@ -357,7 +373,7 @@ i32 orca_runloop(void* user) bb_error wasm_init_err; bb_module_definition_init_opts module_def_init_opts = { .debug_name = bundleNameCString }; app->runtime.bbModuleDef = bb_module_definition_init(module_def_init_opts); - wasm_init_err = bb_module_definition_decode(&app->runtime.bbModuleDef, app->runtime.wasmBytecode.ptr, app->runtime.wasmBytecode.len); + wasm_init_err = bb_module_definition_decode(app->runtime.bbModuleDef, app->runtime.wasmBytecode.ptr, app->runtime.wasmBytecode.len); if (wasm_init_err != BB_ERROR_OK) { return quit_on_wasm_init_failure("wasm decode", wasm_init_err); @@ -381,13 +397,13 @@ i32 orca_runloop(void* user) mem_arena_clear(mem_scratch()); //NOTE: bind orca APIs - bb_import_package module_imports = bb_import_package_init("*"); - bindgen_link_core_api(&module_imports); - bindgen_link_canvas_api(&module_imports); - bindgen_link_clock_api(&module_imports); - bindgen_link_io_api(&module_imports); - bindgen_link_gles_api(&module_imports); - manual_link_gles_api(&module_imports); + bb_import_package* module_imports = bb_import_package_init("*"); + bindgen_link_core_api(module_imports); + bindgen_link_canvas_api(module_imports); + bindgen_link_clock_api(module_imports); + bindgen_link_io_api(module_imports); + bindgen_link_gles_api(module_imports); + manual_link_gles_api(module_imports); //NOTE: compile // M3Result res = m3_CompileModule(app->runtime.m3Module); @@ -407,16 +423,16 @@ i32 orca_runloop(void* user) // return(-1); // } - app->runtime.bbModuleInst = bb_module_instance_init(&app->runtime.bbModuleDef); + app->runtime.bbModuleInst = bb_module_instance_init(app->runtime.bbModuleDef); bb_module_instance_instantiate_opts module_inst_instantiate_opts = { .packages = &module_imports, .num_packages = 1, .enable_debug = false, }; - wasm_init_err = bb_module_instance_instantiate(&app->runtime.bbModuleInst, bb_module_instance_instantiate_opts opts); + wasm_init_err = bb_module_instance_instantiate(app->runtime.bbModuleInst, module_inst_instantiate_opts); if (wasm_init_err != BB_ERROR_OK) { return quit_on_wasm_init_failure("wasm instantiate", wasm_init_err); } - bb_import_package_deinit(&module_imports); // safe to deinit this now that the module has been instantiated + bb_import_package_deinit(module_imports); // safe to deinit this now that the module has been instantiated // TODO up next: expose a cached handle to functions to invoke instead of a string search @@ -426,47 +442,67 @@ i32 orca_runloop(void* user) const g_export_desc* desc = &G_EXPORT_DESC[i]; bb_func_handle handler; - bb_module_instance_find_func(&app->runtime.bbModuleInst, desc->name.ptr); + const bb_error find_func_err = bb_module_instance_find_func(app->runtime.bbModuleInst, desc->name.ptr, &handler); // IM3Function handler = 0; // m3_FindFunction(&handler, app->runtime.m3Runtime, desc->name.ptr); - if(handler) + if(find_func_err == BB_ERROR_OK) { - bool checked = false; - //NOTE: check function signature - int retCount = m3_GetRetCount(handler); - int argCount = m3_GetArgCount(handler); - if(retCount == desc->retTags.len && argCount == desc->argTags.len) + bb_func_info func_info = bb_module_instance_func_info(app->runtime.bbModuleInst, handler); + + bool checked = func_info.num_returns == desc->retTags.len && func_info.num_params == desc->argTags.len; + + for(int retIndex = 0; checked && retIndex < func_info.num_returns; retIndex++) { - checked = true; - for(int retIndex = 0; retIndex < retCount; retIndex++) + bb_valtype valtype; + if(tag_to_valtype(desc->retTags.ptr[retIndex], &valtype) && valtype != func_info.returns[retIndex]) { - M3ValueType m3Type = m3_GetRetType(handler, retIndex); - char tag = m3_type_to_tag(m3Type); - - if(tag != desc->retTags.ptr[retIndex]) - { - checked = false; - break; - } - } - if(checked) - { - for(int argIndex = 0; argIndex < argCount; argIndex++) - { - M3ValueType m3Type = m3_GetArgType(handler, argIndex); - char tag = m3_type_to_tag(m3Type); - - if(tag != desc->argTags.ptr[argIndex]) - { - checked = false; - break; - } - } + checked = false; } } + for(int argIndex = 0; checked && argIndex < func_info.num_params; argIndex++) + { + bb_valtype valtype; + if(tag_to_valtype(desc->argTags.ptr[argIndex], &valtype) && valtype != func_info.params[argIndex]) + { + checked = false; + } + } + + // int retCount = m3_GetRetCount(handler); + // int argCount = m3_GetArgCount(handler); + // if(retCount == desc->retTags.len && argCount == desc->argTags.len) + // { + // checked = true; + // for(int retIndex = 0; retIndex < retCount; retIndex++) + // { + // M3ValueType m3Type = m3_GetRetType(handler, retIndex); + // char tag = m3_type_to_tag(m3Type); + + // if(tag != desc->retTags.ptr[retIndex]) + // { + // checked = false; + // break; + // } + // } + // if(checked) + // { + // for(int argIndex = 0; argIndex < argCount; argIndex++) + // { + // M3ValueType m3Type = m3_GetArgType(handler, argIndex); + // char tag = m3_type_to_tag(m3Type); + + // if(tag != desc->argTags.ptr[argIndex]) + // { + // checked = false; + // break; + // } + // } + // } + // } + if(checked) { app->runtime.exports[i] = handler; @@ -479,8 +515,10 @@ i32 orca_runloop(void* user) } //NOTE: get location of the raw event slot - IM3Global rawEventGlobal = m3_FindGlobal(app->runtime.m3Module, "_OrcaRawEvent"); - app->runtime.rawEventOffset = (u32)rawEventGlobal->intValue; + // IM3Global rawEventGlobal = m3_FindGlobal(app->runtime.m3Module, "_OrcaRawEvent"); + // app->runtime.rawEventOffset = (u32)rawEventGlobal->intValue; + bb_global rawEventGlobal = bb_module_instance_find_global(app->runtime.bbModuleInst, "_OrcaRawEvent"); + app->runtime.rawEventOffset = (u32)rawEventGlobal.value->i32_val; //NOTE: preopen the app local root dir { @@ -497,17 +535,22 @@ i32 orca_runloop(void* user) //NOTE: prepare GL surface mg_surface_prepare(app->surface); - IM3Function* exports = app->runtime.exports; + // IM3Function* exports = app->runtime.exports; + bb_func_handle* exports = app->runtime.exports; //NOTE: call init handler - if(exports[G_EXPORT_ON_INIT]) + if(bb_func_handle_isvalid(exports[G_EXPORT_ON_INIT])) { - M3Result err = m3_Call(exports[G_EXPORT_ON_INIT], 0, 0); - if(err != NULL) - { - log_error("runtime error: %s\n", err); + bb_error err = bb_module_instance_invoke(app->runtime.bbModuleInst, exports[G_EXPORT_ON_INIT], 0, 0, 0, 0, (bb_module_instance_invoke_opts){0}); - str8 msg = str8_pushf(mem_scratch(), "Runtime error: %s\n", err); + // M3Result err = m3_Call(exports[G_EXPORT_ON_INIT], 0, 0); + // if(err != NULL) + if(err != BB_ERROR_OK) + { + const char* errStr = bb_error_str(err); + log_error("runtime error: %s\n", errStr); + + str8 msg = str8_pushf(mem_scratch(), "Runtime error: %s\n", errStr); const char* options[] = {"OK"}; mp_alert_popup("Error", msg.ptr, @@ -519,13 +562,14 @@ i32 orca_runloop(void* user) } } - if(exports[G_EXPORT_FRAME_RESIZE]) + if(bb_func_handle_isvalid(exports[G_EXPORT_FRAME_RESIZE])) { mp_rect content = mp_window_get_content_rect(app->window); - u32 width = (u32)content.w; - u32 height = (u32)content.h; - const void* args[2] = {&width, &height}; - m3_Call(exports[G_EXPORT_FRAME_RESIZE], 2, args); + const bb_val args[2] = { + { .i32_val = (i32)content.w }, + { .i32_val = (i32)content.h }, + }; + bb_module_instance_invoke(app->runtime.bbModuleInst, exports[G_EXPORT_FRAME_RESIZE], args, 2, 0, 0, (bb_module_instance_invoke_opts){0}); } ui_set_context(&app->debugOverlay.ui); @@ -541,14 +585,14 @@ i32 orca_runloop(void* user) ui_process_event(event); } - if(exports[G_EXPORT_RAW_EVENT]) + if(bb_func_handle_isvalid(exports[G_EXPORT_RAW_EVENT])) { #ifndef M3_BIG_ENDIAN mp_event* eventPtr = (mp_event*)wasm_memory_offset_to_ptr(&app->runtime.wasmMemory, app->runtime.rawEventOffset); memcpy(eventPtr, event, sizeof(*event)); - - const void* args[1] = {&app->runtime.rawEventOffset}; - m3_Call(exports[G_EXPORT_RAW_EVENT], 1, args); + bb_val args[1]; + args[0].i32_val = app->runtime.rawEventOffset; + bb_module_instance_invoke(app->runtime.bbModuleInst, exports[G_EXPORT_RAW_EVENT], args, 1, 0, 0, (bb_module_instance_invoke_opts){0}); #else log_error("OnRawEvent() is not supported on big endian platforms"); #endif @@ -565,12 +609,18 @@ i32 orca_runloop(void* user) { mp_rect frame = {0, 0, event->move.frame.w, event->move.frame.h}; - if(exports[G_EXPORT_FRAME_RESIZE]) + if(bb_func_handle_isvalid(exports[G_EXPORT_FRAME_RESIZE])) { - u32 width = (u32)event->move.content.w; - u32 height = (u32)event->move.content.h; - const void* args[2] = {&width, &height}; - m3_Call(exports[G_EXPORT_FRAME_RESIZE], 2, args); + const bb_val args[2] = { + { .i32_val = (i32)(u32)event->move.content.w }, + { .i32_val = (i32)(u32)event->move.content.h }, + }; + bb_module_instance_invoke(app->runtime.bbModuleInst, exports[G_EXPORT_FRAME_RESIZE], args, 2, 0, 0, (bb_module_instance_invoke_opts){0}); + + // u32 width = (u32)event->move.content.w; + // u32 height = (u32)event->move.content.h; + // const void* args[2] = {&width, &height}; + // m3_Call(exports[G_EXPORT_FRAME_RESIZE], 2, args); } } break; @@ -578,30 +628,41 @@ i32 orca_runloop(void* user) { if(event->key.action == MP_KEY_PRESS) { - if(exports[G_EXPORT_MOUSE_DOWN]) + if(bb_func_handle_isvalid(exports[G_EXPORT_MOUSE_DOWN])) { - int key = event->key.code; - const void* args[1] = {&key}; - m3_Call(exports[G_EXPORT_MOUSE_DOWN], 1, args); + const bb_val key = {.i32_val = event->key.code }; + // int key = event->key.code; + // const void* args[1] = {&key}; + // m3_Call(exports[G_EXPORT_MOUSE_DOWN], 1, args); + bb_module_instance_invoke(app->runtime.bbModuleInst, exports[G_EXPORT_MOUSE_DOWN], &key, 1, 0, 0, (bb_module_instance_invoke_opts){0}); } } else { - if(exports[G_EXPORT_MOUSE_UP]) + if(bb_func_handle_isvalid(exports[G_EXPORT_MOUSE_UP])) { - int key = event->key.code; - const void* args[1] = {&key}; - m3_Call(exports[G_EXPORT_MOUSE_UP], 1, args); + const bb_val key = {.i32_val = event->key.code }; + // int key = event->key.code; + // const void* args[1] = {&key}; + // m3_Call(exports[G_EXPORT_MOUSE_UP], 1, args); + bb_module_instance_invoke(app->runtime.bbModuleInst, exports[G_EXPORT_MOUSE_UP], &key, 1, 0, 0, (bb_module_instance_invoke_opts){0}); } } } break; case MP_EVENT_MOUSE_MOVE: { - if(exports[G_EXPORT_MOUSE_MOVE]) + if(bb_func_handle_isvalid(exports[G_EXPORT_MOUSE_MOVE])) { - const void* args[4] = {&event->mouse.x, &event->mouse.y, &event->mouse.deltaX, &event->mouse.deltaY}; - m3_Call(exports[G_EXPORT_MOUSE_MOVE], 4, args); + const bb_val args[4] = { + { .f32_val = event->mouse.x }, + { .f32_val = event->mouse.y }, + { .f32_val = event->mouse.deltaX }, + { .f32_val = event->mouse.deltaY }, + }; + // const void* args[4] = {&event->mouse.x, &event->mouse.y, &event->mouse.deltaX, &event->mouse.deltaY}; + // m3_Call(exports[G_EXPORT_MOUSE_MOVE], 4, args); + bb_module_instance_invoke(app->runtime.bbModuleInst, exports[G_EXPORT_MOUSE_MOVE], args, 4, 0, 0, (bb_module_instance_invoke_opts){0}); } } break; @@ -616,18 +677,22 @@ i32 orca_runloop(void* user) #endif } - if(exports[G_EXPORT_KEY_DOWN]) + if(bb_func_handle_isvalid(exports[G_EXPORT_KEY_DOWN])) { - const void* args[1] = {&event->key.code}; - m3_Call(exports[G_EXPORT_KEY_DOWN], 1, args); + const bb_val key = {.i32_val = event->key.code }; + // const void* args[1] = {&event->key.code}; + // m3_Call(exports[G_EXPORT_KEY_DOWN], 1, args); + bb_module_instance_invoke(app->runtime.bbModuleInst, exports[G_EXPORT_KEY_DOWN], &key, 1, 0, 0, (bb_module_instance_invoke_opts){0}); } } else if(event->key.action == MP_KEY_RELEASE) { - if(exports[G_EXPORT_KEY_UP]) + if(bb_func_handle_isvalid(exports[G_EXPORT_KEY_UP])) { - const void* args[1] = {&event->key.code}; - m3_Call(exports[G_EXPORT_KEY_UP], 1, args); + const bb_val key = {.i32_val = event->key.code }; + // const void* args[1] = {&event->key.code}; + // m3_Call(exports[G_EXPORT_KEY_UP], 1, args); + bb_module_instance_invoke(app->runtime.bbModuleInst, exports[G_EXPORT_KEY_UP], &key, 1, 0, 0, (bb_module_instance_invoke_opts){0}); } } } break; @@ -773,10 +838,11 @@ i32 orca_runloop(void* user) mg_render(app->debugOverlay.surface, app->debugOverlay.canvas); } - if(exports[G_EXPORT_FRAME_REFRESH]) + if(bb_func_handle_isvalid(exports[G_EXPORT_FRAME_REFRESH])) { mg_surface_prepare(app->surface); - m3_Call(exports[G_EXPORT_FRAME_REFRESH], 0, 0); + // m3_Call(exports[G_EXPORT_FRAME_REFRESH], 0, 0); + bb_module_instance_invoke(app->runtime.bbModuleInst, exports[G_EXPORT_FRAME_REFRESH], 0, 0, 0, 0, (bb_module_instance_invoke_opts){0}); } if(app->debugOverlay.show) diff --git a/src/memory_impl.c b/src/memory_impl.c index e1bff89..8a607ab 100644 --- a/src/memory_impl.c +++ b/src/memory_impl.c @@ -10,62 +10,61 @@ void* wasm_memory_resize_callback(void* p, unsigned long size, void* userData) { - // wasm_memory* memory = (wasm_memory*)userData; + wasm_memory* memory = (wasm_memory*)userData; - // if(memory->committed >= size) - // { - // return(memory->ptr); - // } - // else if(memory->committed < memory->reserved) - // { - // u32 commitSize = size - memory->committed; + if(memory->committed >= size) + { + return(memory->ptr); + } + else if(memory->committed < memory->reserved) + { + u32 commitSize = size - memory->committed; - // mem_base_allocator* allocator = mem_base_allocator_default(); - // mem_base_commit(allocator, memory->ptr + memory->committed, commitSize); - // memory->committed += commitSize; - // return(memory->ptr); - // } - // else - // { - // DEBUG_ASSERT(0, "Out of memory"); - // return(0); - // } + mem_base_allocator* allocator = mem_base_allocator_default(); + mem_base_commit(allocator, memory->ptr + memory->committed, commitSize); + memory->committed += commitSize; + return(memory->ptr); + } + else + { + DEBUG_ASSERT(0, "Out of memory"); + return(0); + } return NULL; } void wasm_memory_free_callback(void* p, void* userData) { -// wasm_memory* memory = (wasm_memory*)userData; + wasm_memory* memory = (wasm_memory*)userData; -// mem_base_allocator* allocator = mem_base_allocator_default(); -// mem_base_release(allocator, memory->ptr, memory->reserved); -// memset(memory, 0, sizeof(wasm_memory)); + mem_base_allocator* allocator = mem_base_allocator_default(); + mem_base_release(allocator, memory->ptr, memory->reserved); + memset(memory, 0, sizeof(wasm_memory)); } extern u32 orca_mem_grow(u64 size) { - return 0; - // const size_t PAGE_SIZE = 65536; + const size_t PAGE_SIZE = 65536; - // orca_runtime* runtime = orca_runtime_get(); - // wasm_memory* memory = &runtime->wasmMemory; + orca_runtime* runtime = orca_runtime_get(); + wasm_memory* memory = &runtime->wasmMemory; - // size = AlignUpOnPow2(size, PAGE_SIZE); - // u64 totalSize = size + m3_GetMemorySize(runtime->m3Runtime); + size = AlignUpOnPow2(size, PAGE_SIZE); + u64 totalSize = size + m3_GetMemorySize(runtime->m3Runtime); - // u32 addr = memory->committed; + u32 addr = memory->committed; - // //NOTE: call resize memory, which will call our custom resize callback... this is a bit involved because - // // wasm3 doesn't allow resizing the memory directly - // M3Result res = ResizeMemory(runtime->m3Runtime, totalSize/PAGE_SIZE); + //NOTE: call resize memory, which will call our custom resize callback... this is a bit involved because + // wasm3 doesn't allow resizing the memory directly + M3Result res = ResizeMemory(runtime->m3Runtime, totalSize/PAGE_SIZE); - // return(addr); + return(addr); } void* wasm_memory_offset_to_ptr(wasm_memory* memory, u32 offset) { - // M3MemoryHeader* header = (M3MemoryHeader*)(memory->ptr); - // DEBUG_ASSERT(offset < header->length, "Wasm offset exceeds memory length") - // return memory->ptr + sizeof(M3MemoryHeader) + offset; + M3MemoryHeader* header = (M3MemoryHeader*)(memory->ptr); + DEBUG_ASSERT(offset < header->length, "Wasm offset exceeds memory length") + return memory->ptr + sizeof(M3MemoryHeader) + offset; return NULL; } diff --git a/src/orca_app.h b/src/orca_app.h index a80e111..6c9f2fd 100644 --- a/src/orca_app.h +++ b/src/orca_app.h @@ -68,9 +68,11 @@ typedef struct orca_runtime wasm_memory wasmMemory; // bytebox data - bb_module_definition bbModuleDef; - bb_module_instance bbModuleInst; - + bb_module_definition* bbModuleDef; + bb_module_instance* bbModuleInst; + bb_func_handle exports[G_EXPORT_COUNT]; + u32 rawEventOffset; + // wasm3 data // IM3Environment m3Env; // IM3Runtime m3Runtime;