bugfixes to bindgen_bb.py

This commit is contained in:
Reuben Dunnington 2023-08-01 22:44:42 -07:00
parent 3f2ed41bfc
commit f76817c1ed
Signed by: rdunnington
GPG Key ID: 4EC5290E704FD482
6 changed files with 233 additions and 172 deletions

View File

@ -56,7 +56,7 @@ if %target% == orca (
::compile orca ::compile orca
set INCLUDES=/I src /I sdk /I ext\bytebox\include /I milepost\src /I milepost\ext 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
) )

View File

@ -17,6 +17,8 @@ enum bb_error
BB_ERROR_OUTOFMEMORY, BB_ERROR_OUTOFMEMORY,
BB_ERROR_INVALIDPARAM, BB_ERROR_INVALIDPARAM,
BB_ERROR_UNKNOWNEXPORT, BB_ERROR_UNKNOWNEXPORT,
BB_ERROR_UNKNOWNIMPORT,
BB_ERROR_INCOMPATIBLEIMPORT,
}; };
typedef enum bb_error bb_error; 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; 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; typedef struct bb_module_definition bb_module_definition;
typedef struct bb_module_instance bb_module_instance;
// 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_import_package bb_import_package; typedef struct bb_import_package bb_import_package;
struct bb_module_instance_instantiate_opts struct bb_module_instance_instantiate_opts
{ {
bb_import_package* packages; bb_import_package** packages;
size_t num_packages; size_t num_packages;
bool enable_debug; bool enable_debug;
}; };
typedef struct bb_module_instance_instantiate_opts bb_module_instance_instantiate_opts; 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 struct bb_module_instance_invoke_opts
{ {
bool trap_on_start; 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); 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_malloc_func(size_t size, void* userdata);
// typedef void* bb_realloc_func(void* mem, 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); // 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); 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); 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_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_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_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_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); 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_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_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_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); 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); 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); 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_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);

View File

@ -196,12 +196,9 @@ for decl in data:
paramTypes = [] paramTypes = []
numParams = 0 numParams = 0
if retTag == 'S': 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') paramTypes.append('BB_VALTYPE_I32')
elif retTag == 'v': #no returns numParams += 1
retType = 'BB_VALTYPE_I32' #unused but we need a dummy value elif retTag != 'v': #no returns
numReturns = 0
else:
retType = translateTag(retTag) retType = translateTag(retTag)
numReturns = 1 numReturns = 1
@ -212,16 +209,20 @@ for decl in data:
paramTypes.append(translateTag(tag)) paramTypes.append(translateTag(tag))
numParams += 1 numParams += 1
# dummy values to avoid 0-length arrays in C
if numReturns == 0:
retType = 'BB_VALTYPE_I32'
if numParams == 0: 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{\n'
s += '\t\tbb_valtype params[] = {' + ', '.join(paramTypes) + '};\n' s += '\t\tbb_valtype params[] = {' + ', '.join(paramTypes) + '};\n'
s += '\t\tsize_t num_params = ' + str(len(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\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\tif(err != BB_ERROR_OK) { log_error("error: %s\\n", bb_error_str(err)); return(-1); }\n'
s += '\t}\n' s += '\t}\n'

View File

@ -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) void orca_runtime_init(orca_runtime* runtime)
{ {
memset(runtime, 0, sizeof(orca_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"gles_api_bind_gen.c"
#include"manual_gles_api.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); 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"}; const char* options[] = {"OK"};
mp_alert_popup("Error", mp_alert_popup("Error",
"The application couldn't load: encountered fatal error at stage '%s': %s", msg.ptr,
stage, 1,
errStr); options);
mp_request_quit(); mp_request_quit();
return((void*)-1); return(-1);
} }
i32 orca_runloop(void* user) i32 orca_runloop(void* user)
@ -357,7 +373,7 @@ i32 orca_runloop(void* user)
bb_error wasm_init_err; bb_error wasm_init_err;
bb_module_definition_init_opts module_def_init_opts = { .debug_name = bundleNameCString }; bb_module_definition_init_opts module_def_init_opts = { .debug_name = bundleNameCString };
app->runtime.bbModuleDef = bb_module_definition_init(module_def_init_opts); 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) if (wasm_init_err != BB_ERROR_OK)
{ {
return quit_on_wasm_init_failure("wasm decode", wasm_init_err); 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()); mem_arena_clear(mem_scratch());
//NOTE: bind orca APIs //NOTE: bind orca APIs
bb_import_package module_imports = bb_import_package_init("*"); bb_import_package* module_imports = bb_import_package_init("*");
bindgen_link_core_api(&module_imports); bindgen_link_core_api(module_imports);
bindgen_link_canvas_api(&module_imports); bindgen_link_canvas_api(module_imports);
bindgen_link_clock_api(&module_imports); bindgen_link_clock_api(module_imports);
bindgen_link_io_api(&module_imports); bindgen_link_io_api(module_imports);
bindgen_link_gles_api(&module_imports); bindgen_link_gles_api(module_imports);
manual_link_gles_api(&module_imports); manual_link_gles_api(module_imports);
//NOTE: compile //NOTE: compile
// M3Result res = m3_CompileModule(app->runtime.m3Module); // M3Result res = m3_CompileModule(app->runtime.m3Module);
@ -407,16 +423,16 @@ i32 orca_runloop(void* user)
// return(-1); // 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, }; 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) if (wasm_init_err != BB_ERROR_OK)
{ {
return quit_on_wasm_init_failure("wasm instantiate", wasm_init_err); 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 // 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]; const g_export_desc* desc = &G_EXPORT_DESC[i];
bb_func_handle handler; 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; // IM3Function handler = 0;
// m3_FindFunction(&handler, app->runtime.m3Runtime, desc->name.ptr); // 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 //NOTE: check function signature
int retCount = m3_GetRetCount(handler); bb_func_info func_info = bb_module_instance_func_info(app->runtime.bbModuleInst, handler);
int argCount = m3_GetArgCount(handler);
if(retCount == desc->retTags.len && argCount == desc->argTags.len) 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; bb_valtype valtype;
for(int retIndex = 0; retIndex < retCount; retIndex++) if(tag_to_valtype(desc->retTags.ptr[retIndex], &valtype) && valtype != func_info.returns[retIndex])
{ {
M3ValueType m3Type = m3_GetRetType(handler, retIndex); checked = false;
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;
}
}
} }
} }
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) if(checked)
{ {
app->runtime.exports[i] = handler; app->runtime.exports[i] = handler;
@ -479,8 +515,10 @@ i32 orca_runloop(void* user)
} }
//NOTE: get location of the raw event slot //NOTE: get location of the raw event slot
IM3Global rawEventGlobal = m3_FindGlobal(app->runtime.m3Module, "_OrcaRawEvent"); // IM3Global rawEventGlobal = m3_FindGlobal(app->runtime.m3Module, "_OrcaRawEvent");
app->runtime.rawEventOffset = (u32)rawEventGlobal->intValue; // 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 //NOTE: preopen the app local root dir
{ {
@ -497,17 +535,22 @@ i32 orca_runloop(void* user)
//NOTE: prepare GL surface //NOTE: prepare GL surface
mg_surface_prepare(app->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 //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); 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});
if(err != NULL)
{
log_error("runtime error: %s\n", err);
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"}; const char* options[] = {"OK"};
mp_alert_popup("Error", mp_alert_popup("Error",
msg.ptr, 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); mp_rect content = mp_window_get_content_rect(app->window);
u32 width = (u32)content.w; const bb_val args[2] = {
u32 height = (u32)content.h; { .i32_val = (i32)content.w },
const void* args[2] = {&width, &height}; { .i32_val = (i32)content.h },
m3_Call(exports[G_EXPORT_FRAME_RESIZE], 2, args); };
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); ui_set_context(&app->debugOverlay.ui);
@ -541,14 +585,14 @@ i32 orca_runloop(void* user)
ui_process_event(event); ui_process_event(event);
} }
if(exports[G_EXPORT_RAW_EVENT]) if(bb_func_handle_isvalid(exports[G_EXPORT_RAW_EVENT]))
{ {
#ifndef M3_BIG_ENDIAN #ifndef M3_BIG_ENDIAN
mp_event* eventPtr = (mp_event*)wasm_memory_offset_to_ptr(&app->runtime.wasmMemory, app->runtime.rawEventOffset); mp_event* eventPtr = (mp_event*)wasm_memory_offset_to_ptr(&app->runtime.wasmMemory, app->runtime.rawEventOffset);
memcpy(eventPtr, event, sizeof(*event)); memcpy(eventPtr, event, sizeof(*event));
bb_val args[1];
const void* args[1] = {&app->runtime.rawEventOffset}; args[0].i32_val = app->runtime.rawEventOffset;
m3_Call(exports[G_EXPORT_RAW_EVENT], 1, args); bb_module_instance_invoke(app->runtime.bbModuleInst, exports[G_EXPORT_RAW_EVENT], args, 1, 0, 0, (bb_module_instance_invoke_opts){0});
#else #else
log_error("OnRawEvent() is not supported on big endian platforms"); log_error("OnRawEvent() is not supported on big endian platforms");
#endif #endif
@ -565,12 +609,18 @@ i32 orca_runloop(void* user)
{ {
mp_rect frame = {0, 0, event->move.frame.w, event->move.frame.h}; 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; const bb_val args[2] = {
u32 height = (u32)event->move.content.h; { .i32_val = (i32)(u32)event->move.content.w },
const void* args[2] = {&width, &height}; { .i32_val = (i32)(u32)event->move.content.h },
m3_Call(exports[G_EXPORT_FRAME_RESIZE], 2, args); };
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; } break;
@ -578,30 +628,41 @@ i32 orca_runloop(void* user)
{ {
if(event->key.action == MP_KEY_PRESS) 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 bb_val key = {.i32_val = event->key.code };
const void* args[1] = {&key}; // int key = event->key.code;
m3_Call(exports[G_EXPORT_MOUSE_DOWN], 1, args); // 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 else
{ {
if(exports[G_EXPORT_MOUSE_UP]) if(bb_func_handle_isvalid(exports[G_EXPORT_MOUSE_UP]))
{ {
int key = event->key.code; const bb_val key = {.i32_val = event->key.code };
const void* args[1] = {&key}; // int key = event->key.code;
m3_Call(exports[G_EXPORT_MOUSE_UP], 1, args); // 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; } break;
case MP_EVENT_MOUSE_MOVE: 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}; const bb_val args[4] = {
m3_Call(exports[G_EXPORT_MOUSE_MOVE], 4, args); { .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; } break;
@ -616,18 +677,22 @@ i32 orca_runloop(void* user)
#endif #endif
} }
if(exports[G_EXPORT_KEY_DOWN]) if(bb_func_handle_isvalid(exports[G_EXPORT_KEY_DOWN]))
{ {
const void* args[1] = {&event->key.code}; const bb_val key = {.i32_val = event->key.code };
m3_Call(exports[G_EXPORT_KEY_DOWN], 1, args); // 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) 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}; const bb_val key = {.i32_val = event->key.code };
m3_Call(exports[G_EXPORT_KEY_UP], 1, args); // 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; } break;
@ -773,10 +838,11 @@ i32 orca_runloop(void* user)
mg_render(app->debugOverlay.surface, app->debugOverlay.canvas); 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); 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) if(app->debugOverlay.show)

View File

@ -10,62 +10,61 @@
void* wasm_memory_resize_callback(void* p, unsigned long size, void* userData) 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) if(memory->committed >= size)
// { {
// return(memory->ptr); return(memory->ptr);
// } }
// else if(memory->committed < memory->reserved) else if(memory->committed < memory->reserved)
// { {
// u32 commitSize = size - memory->committed; u32 commitSize = size - memory->committed;
// mem_base_allocator* allocator = mem_base_allocator_default(); mem_base_allocator* allocator = mem_base_allocator_default();
// mem_base_commit(allocator, memory->ptr + memory->committed, commitSize); mem_base_commit(allocator, memory->ptr + memory->committed, commitSize);
// memory->committed += commitSize; memory->committed += commitSize;
// return(memory->ptr); return(memory->ptr);
// } }
// else else
// { {
// DEBUG_ASSERT(0, "Out of memory"); DEBUG_ASSERT(0, "Out of memory");
// return(0); return(0);
// } }
return NULL; return NULL;
} }
void wasm_memory_free_callback(void* p, void* userData) 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_allocator* allocator = mem_base_allocator_default();
// mem_base_release(allocator, memory->ptr, memory->reserved); mem_base_release(allocator, memory->ptr, memory->reserved);
// memset(memory, 0, sizeof(wasm_memory)); memset(memory, 0, sizeof(wasm_memory));
} }
extern u32 orca_mem_grow(u64 size) 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(); orca_runtime* runtime = orca_runtime_get();
// wasm_memory* memory = &runtime->wasmMemory; wasm_memory* memory = &runtime->wasmMemory;
// size = AlignUpOnPow2(size, PAGE_SIZE); size = AlignUpOnPow2(size, PAGE_SIZE);
// u64 totalSize = size + m3_GetMemorySize(runtime->m3Runtime); 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 //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 // wasm3 doesn't allow resizing the memory directly
// M3Result res = ResizeMemory(runtime->m3Runtime, totalSize/PAGE_SIZE); M3Result res = ResizeMemory(runtime->m3Runtime, totalSize/PAGE_SIZE);
// return(addr); return(addr);
} }
void* wasm_memory_offset_to_ptr(wasm_memory* memory, u32 offset) void* wasm_memory_offset_to_ptr(wasm_memory* memory, u32 offset)
{ {
// M3MemoryHeader* header = (M3MemoryHeader*)(memory->ptr); M3MemoryHeader* header = (M3MemoryHeader*)(memory->ptr);
// DEBUG_ASSERT(offset < header->length, "Wasm offset exceeds memory length") DEBUG_ASSERT(offset < header->length, "Wasm offset exceeds memory length")
// return memory->ptr + sizeof(M3MemoryHeader) + offset; return memory->ptr + sizeof(M3MemoryHeader) + offset;
return NULL; return NULL;
} }

View File

@ -68,9 +68,11 @@ typedef struct orca_runtime
wasm_memory wasmMemory; wasm_memory wasmMemory;
// bytebox data // bytebox data
bb_module_definition bbModuleDef; bb_module_definition* bbModuleDef;
bb_module_instance bbModuleInst; bb_module_instance* bbModuleInst;
bb_func_handle exports[G_EXPORT_COUNT];
u32 rawEventOffset;
// wasm3 data // wasm3 data
// IM3Environment m3Env; // IM3Environment m3Env;
// IM3Runtime m3Runtime; // IM3Runtime m3Runtime;