This commit is contained in:
Ilia Demianenko 2023-07-15 00:15:07 -07:00
parent a683c163cb
commit bffcaa4abf
10 changed files with 81 additions and 99 deletions

View File

@ -67,6 +67,12 @@ elif [ $target = orca ] ; then
--guest-include graphics.h \ --guest-include graphics.h \
--wasm3-bindings ./src/canvas_api_bind_gen.c --wasm3-bindings ./src/canvas_api_bind_gen.c
python3 ./scripts/bindgen2.py clock \
src/clock_api.json \
--guest-stubs sdk/orca_clock.c \
--guest-include platform_clock.h \
--wasm3-bindings ./src/clock_api_bind_gen.c
python3 ./scripts/bindgen2.py io \ python3 ./scripts/bindgen2.py io \
src/io_api.json \ src/io_api.json \
--guest-stubs sdk/io_stubs.c \ --guest-stubs sdk/io_stubs.c \

View File

@ -10,7 +10,7 @@ set wasmFlags=--target=wasm32^
-O2 ^ -O2 ^
-mbulk-memory ^ -mbulk-memory ^
-D__ORCA__ ^ -D__ORCA__ ^
-isystem ..\..\cstdlib\include -I ..\..\sdk -I..\..\milepost\ext -I ..\..\milepost -I ..\..\milepost\src -I ..\..\milepost\src\util -I ..\..\milepost\src\platform -isystem ..\..\cstdlib\include -I ..\..\sdk -I..\..\milepost\ext -I ..\..\milepost -I ..\..\milepost\src
clang %wasmFlags% -o .\module.wasm ..\..\sdk\orca.c ..\..\cstdlib\src\*.c src\main.c clang %wasmFlags% -o .\module.wasm ..\..\sdk\orca.c ..\..\cstdlib\src\*.c src\main.c

View File

@ -26,7 +26,7 @@ wasmFlags="--target=wasm32 \
-D__ORCA__ \ -D__ORCA__ \
-I $STDLIB_DIR/include \ -I $STDLIB_DIR/include \
-I $ORCA_SDK_DIR \ -I $ORCA_SDK_DIR \
-I $MILEPOST_DIR/ext -I $MILEPOST_DIR -I $MILEPOST_DIR/src -I $MILEPOST_DIR/src/util -I $MILEPOST_DIR/src/platform" -I $MILEPOST_DIR/ext -I $MILEPOST_DIR -I $MILEPOST_DIR/src"
$CLANG $wasmFlags -o ./module.wasm ../../sdk/orca.c ../../cstdlib/src/*.c src/main.c $CLANG $wasmFlags -o ./module.wasm ../../sdk/orca.c ../../cstdlib/src/*.c src/main.c

View File

@ -10,7 +10,7 @@ set wasmFlags=--target=wasm32^
-O2 ^ -O2 ^
-mbulk-memory ^ -mbulk-memory ^
-D__ORCA__ ^ -D__ORCA__ ^
-isystem ..\..\cstdlib\include -I ..\..\sdk -I..\..\milepost\ext -I ..\..\milepost -I ..\..\milepost\src -I ..\..\milepost\src\util -I ..\..\milepost\src\platform -isystem ..\..\cstdlib\include -I ..\..\sdk -I..\..\milepost\ext -I ..\..\milepost -I ..\..\milepost\src
clang %wasmFlags% -o .\module.wasm ..\..\sdk\orca.c ..\..\cstdlib\src\*.c src\main.c clang %wasmFlags% -o .\module.wasm ..\..\sdk\orca.c ..\..\cstdlib\src\*.c src\main.c

View File

@ -12,15 +12,15 @@ else
fi fi
wasmFlags="--target=wasm32 \ wasmFlags="--target=wasm32 \
--no-standard-libraries \ --no-standard-libraries \
-fno-builtin \ -fno-builtin \
-Wl,--no-entry \ -Wl,--no-entry \
-Wl,--export-dynamic \ -Wl,--export-dynamic \
-g \ -g \
-O2 \ -O2 \
-mbulk-memory \ -mbulk-memory \
-D__ORCA__ \ -D__ORCA__ \
-isystem ../../cstdlib/include -I ../../sdk -I../../milepost/ext -I ../../milepost -I ../../milepost/src -I ../../milepost/src/util -I ../../milepost/src/platform -I../.." -isystem ../../cstdlib/include -I ../../sdk -I../../milepost/ext -I ../../milepost -I ../../milepost/src"
$CLANG $wasmFlags -o ./module.wasm ../../sdk/orca.c ../../cstdlib/src/*.c src/main.c $CLANG $wasmFlags -o ./module.wasm ../../sdk/orca.c ../../cstdlib/src/*.c src/main.c

View File

@ -6,8 +6,8 @@
vec2 frameSize = {100, 100}; vec2 frameSize = {100, 100};
mg_canvas canvas;
mg_surface surface; mg_surface surface;
mg_canvas canvas;
mg_font font; mg_font font;
ui_context ui; ui_context ui;
mem_arena textArena = {0}; mem_arena textArena = {0};

View File

@ -1,12 +1,5 @@
#include"orca.h" #include"orca.h"
ORCA_EXPORT mp_event* _OrcaGetRawEventPtr() mp_event _OrcaRawEvent;
{
static mp_event event;
return &event;
}
ORCA_EXPORT void _OrcaClockInit() ORCA_EXPORT mp_event *_OrcaRawEventPtr;
{
mp_clock_init();
}

View File

@ -1,10 +1,4 @@
[ [
{
"name": "mp_clock_init",
"cname": "mp_clock_init",
"ret": {"name": "void", "tag": "v"},
"args": []
},
{ {
"name": "mp_get_time", "name": "mp_get_time",
"cname": "mp_get_time", "cname": "mp_get_time",

View File

@ -381,26 +381,26 @@ void* orca_runloop(void* user)
return((void*)-1); return((void*)-1);
} }
//NOTE: Find and type check exports. //NOTE: Find and type check event handlers.
for(int i=0; i<G_EXPORT_COUNT; i++) for(int i=0; i<G_EVENT_COUNT; i++)
{ {
const g_export_desc* desc = &G_EXPORT_DESC[i]; const g_event_handler_desc* desc = &G_EVENT_HANDLER_DESC[i];
IM3Function export = 0; IM3Function handler = 0;
m3_FindFunction(&export, app->runtime.m3Runtime, desc->name.ptr); m3_FindFunction(&handler, app->runtime.m3Runtime, desc->name.ptr);
if(export) if(handler)
{ {
bool checked = false; bool checked = false;
//NOTE: check function signature //NOTE: check function signature
int retCount = m3_GetRetCount(export); int retCount = m3_GetRetCount(handler);
int argCount = m3_GetArgCount(export); int argCount = m3_GetArgCount(handler);
if(retCount == desc->retTags.len && argCount == desc->argTags.len) if(retCount == desc->retTags.len && argCount == desc->argTags.len)
{ {
checked = true; checked = true;
for(int retIndex = 0; retIndex < retCount; retIndex++) for(int retIndex = 0; retIndex < retCount; retIndex++)
{ {
M3ValueType m3Type = m3_GetRetType(export, retIndex); M3ValueType m3Type = m3_GetRetType(handler, retIndex);
char tag = m3_type_to_tag(m3Type); char tag = m3_type_to_tag(m3Type);
if(tag != desc->retTags.ptr[retIndex]) if(tag != desc->retTags.ptr[retIndex])
@ -413,7 +413,7 @@ void* orca_runloop(void* user)
{ {
for(int argIndex = 0; argIndex < argCount; argIndex++) for(int argIndex = 0; argIndex < argCount; argIndex++)
{ {
M3ValueType m3Type = m3_GetArgType(export, argIndex); M3ValueType m3Type = m3_GetArgType(handler, argIndex);
char tag = m3_type_to_tag(m3Type); char tag = m3_type_to_tag(m3Type);
if(tag != desc->argTags.ptr[argIndex]) if(tag != desc->argTags.ptr[argIndex])
@ -427,29 +427,18 @@ void* orca_runloop(void* user)
if(checked) if(checked)
{ {
app->runtime.exports[i] = export; app->runtime.eventHandlers[i] = handler;
} }
else else
{ {
log_error("type mismatch for export %.*s\n", (int)desc->name.len, desc->name.ptr); log_error("type mismatch for event handler %.*s\n", (int)desc->name.len, desc->name.ptr);
} }
} }
} }
IM3Function* exports = app->runtime.exports;
//NOTE: get location of the raw event slot //NOTE: get location of the raw event slot
if (exports[G_EXPORT_GET_RAW_EVENT_PTR]) IM3Global rawEventGlobal = m3_FindGlobal(app->runtime.m3Module, "_OrcaRawEventPtr");
{ app->runtime.rawEventOffset = (u32)rawEventGlobal->intValue;
m3_CallV(exports[G_EXPORT_GET_RAW_EVENT_PTR]);
const void* getRawEventPtrResults[1] = {&app->runtime.rawEventOffset};
m3_GetResults(exports[G_EXPORT_GET_RAW_EVENT_PTR], 1, getRawEventPtrResults);
}
//NOTE: init clock
if (exports[G_EXPORT_CLOCK_INIT])
{
m3_CallV(exports[G_EXPORT_CLOCK_INIT]);
}
//NOTE: preopen the app local root dir //NOTE: preopen the app local root dir
{ {
@ -466,10 +455,12 @@ void* orca_runloop(void* user)
//NOTE: prepare GL surface //NOTE: prepare GL surface
mg_surface_prepare(app->surface); mg_surface_prepare(app->surface);
IM3Function* eventHandlers = app->runtime.eventHandlers;
//NOTE: call init handler //NOTE: call init handler
if(exports[G_EXPORT_ON_INIT]) if(eventHandlers[G_EVENT_START])
{ {
M3Result err = m3_Call(exports[G_EXPORT_ON_INIT], 0, 0); M3Result err = m3_Call(eventHandlers[G_EVENT_START], 0, 0);
if(err != NULL) if(err != NULL)
{ {
log_error("runtime error: %s\n", err); log_error("runtime error: %s\n", err);
@ -486,13 +477,13 @@ void* orca_runloop(void* user)
} }
} }
if(exports[G_EXPORT_ON_FRAME_RESIZE]) if(eventHandlers[G_EVENT_FRAME_RESIZE])
{ {
mp_rect frame = mg_surface_get_frame(app->surface); mp_rect frame = mg_surface_get_frame(app->surface);
u32 width = (u32)frame.w; u32 width = (u32)frame.w;
u32 height = (u32)frame.h; u32 height = (u32)frame.h;
const void* args[2] = {&width, &height}; const void* args[2] = {&width, &height};
m3_Call(exports[G_EXPORT_ON_FRAME_RESIZE], 2, args); m3_Call(eventHandlers[G_EVENT_FRAME_RESIZE], 2, args);
} }
ui_set_context(&app->debugOverlay.ui); ui_set_context(&app->debugOverlay.ui);
@ -508,7 +499,7 @@ void* orca_runloop(void* user)
ui_process_event(event); ui_process_event(event);
} }
if(exports[G_EXPORT_ON_RAW_EVENT]) if(eventHandlers[G_EVENT_RAW_EVENT])
{ {
if (app->runtime.rawEventOffset == 0) if (app->runtime.rawEventOffset == 0)
{ {
@ -520,7 +511,7 @@ void* orca_runloop(void* user)
memcpy(eventPtr, event, sizeof(*event)); memcpy(eventPtr, event, sizeof(*event));
const void* args[1] = {&app->runtime.rawEventOffset}; const void* args[1] = {&app->runtime.rawEventOffset};
m3_Call(exports[G_EXPORT_ON_RAW_EVENT], 1, args); m3_Call(eventHandlers[G_EVENT_RAW_EVENT], 1, args);
#else #else
log_error("OnRawEvent() is not supported on big endian platforms"); log_error("OnRawEvent() is not supported on big endian platforms");
#endif #endif
@ -540,12 +531,12 @@ void* orca_runloop(void* user)
// mg_surface_set_frame(app->debugOverlay.surface, frame); // mg_surface_set_frame(app->debugOverlay.surface, frame);
if(exports[G_EXPORT_ON_FRAME_RESIZE]) if(eventHandlers[G_EVENT_FRAME_RESIZE])
{ {
u32 width = (u32)event->frame.rect.w; u32 width = (u32)event->frame.rect.w;
u32 height = (u32)event->frame.rect.h; u32 height = (u32)event->frame.rect.h;
const void* args[2] = {&width, &height}; const void* args[2] = {&width, &height};
m3_Call(exports[G_EXPORT_ON_FRAME_RESIZE], 2, args); m3_Call(eventHandlers[G_EVENT_FRAME_RESIZE], 2, args);
} }
} break; } break;
@ -553,30 +544,30 @@ void* orca_runloop(void* user)
{ {
if(event->key.action == MP_KEY_PRESS) if(event->key.action == MP_KEY_PRESS)
{ {
if(exports[G_EXPORT_ON_MOUSE_DOWN]) if(eventHandlers[G_EVENT_MOUSE_DOWN])
{ {
int key = event->key.code; int key = event->key.code;
const void* args[1] = {&key}; const void* args[1] = {&key};
m3_Call(exports[G_EXPORT_ON_MOUSE_DOWN], 1, args); m3_Call(eventHandlers[G_EVENT_MOUSE_DOWN], 1, args);
} }
} }
else else
{ {
if(exports[G_EXPORT_ON_MOUSE_UP]) if(eventHandlers[G_EVENT_MOUSE_UP])
{ {
int key = event->key.code; int key = event->key.code;
const void* args[1] = {&key}; const void* args[1] = {&key};
m3_Call(exports[G_EXPORT_ON_MOUSE_UP], 1, args); m3_Call(eventHandlers[G_EVENT_MOUSE_UP], 1, args);
} }
} }
} break; } break;
case MP_EVENT_MOUSE_MOVE: case MP_EVENT_MOUSE_MOVE:
{ {
if(exports[G_EXPORT_ON_MOUSE_MOVE]) if(eventHandlers[G_EVENT_MOUSE_MOVE])
{ {
const void* args[4] = {&event->move.x, &event->move.y, &event->move.deltaX, &event->move.deltaY}; const void* args[4] = {&event->move.x, &event->move.y, &event->move.deltaX, &event->move.deltaY};
m3_Call(exports[G_EXPORT_ON_MOUSE_MOVE], 4, args); m3_Call(eventHandlers[G_EVENT_MOUSE_MOVE], 4, args);
} }
} break; } break;
@ -591,18 +582,18 @@ void* orca_runloop(void* user)
#endif #endif
} }
if(exports[G_EXPORT_ON_KEY_DOWN]) if(eventHandlers[G_EVENT_KEY_DOWN])
{ {
const void* args[1] = {&event->key.code}; const void* args[1] = {&event->key.code};
m3_Call(exports[G_EXPORT_ON_KEY_DOWN], 1, args); m3_Call(eventHandlers[G_EVENT_KEY_DOWN], 1, args);
} }
} }
else if(event->key.action == MP_KEY_RELEASE) else if(event->key.action == MP_KEY_RELEASE)
{ {
if(exports[G_EXPORT_ON_KEY_UP]) if(eventHandlers[G_EVENT_KEY_UP])
{ {
const void* args[1] = {&event->key.code}; const void* args[1] = {&event->key.code};
m3_Call(exports[G_EXPORT_ON_KEY_UP], 1, args); m3_Call(eventHandlers[G_EVENT_KEY_UP], 1, args);
} }
} }
} break; } break;
@ -749,10 +740,10 @@ void* orca_runloop(void* user)
mg_render(app->debugOverlay.surface, app->debugOverlay.canvas); mg_render(app->debugOverlay.surface, app->debugOverlay.canvas);
} }
if(exports[G_EXPORT_ON_FRAME_REFRESH]) if(eventHandlers[G_EVENT_FRAME_REFRESH])
{ {
mg_surface_prepare(app->surface); mg_surface_prepare(app->surface);
m3_Call(exports[G_EXPORT_ON_FRAME_REFRESH], 0, 0); m3_Call(eventHandlers[G_EVENT_FRAME_REFRESH], 0, 0);
} }
if(app->debugOverlay.show) if(app->debugOverlay.show)

View File

@ -14,43 +14,41 @@
#include"m3_env.h" #include"m3_env.h"
#include"m3_compile.h" #include"m3_compile.h"
#define G_EXPORTS(X) \ #define G_EVENTS(X) \
X(G_EXPORT_ON_INIT, "OnInit", "", "") \ X(G_EVENT_START, "OnInit", "", "") \
X(G_EXPORT_ON_MOUSE_DOWN, "OnMouseDown", "", "i") \ X(G_EVENT_MOUSE_DOWN, "OnMouseDown", "", "i") \
X(G_EXPORT_ON_MOUSE_UP, "OnMouseUp", "", "i") \ X(G_EVENT_MOUSE_UP, "OnMouseUp", "", "i") \
X(G_EXPORT_ON_MOUSE_ENTER, "OnMouseEnter", "", "") \ X(G_EVENT_MOUSE_ENTER, "OnMouseEnter", "", "") \
X(G_EXPORT_ON_MOUSE_LEAVE, "OnMouseLeave", "", "") \ X(G_EVENT_MOUSE_LEAVE, "OnMouseLeave", "", "") \
X(G_EXPORT_ON_MOUSE_MOVE, "OnMouseMove", "", "ffff") \ X(G_EVENT_MOUSE_MOVE, "OnMouseMove", "", "ffff") \
X(G_EXPORT_ON_MOUSE_WHEEL, "OnMouseWheel", "", "ff") \ X(G_EVENT_MOUSE_WHEEL, "OnMouseWheel", "", "ff") \
X(G_EXPORT_ON_KEY_DOWN, "OnKeyDown", "", "i") \ X(G_EVENT_KEY_DOWN, "OnKeyDown", "", "i") \
X(G_EXPORT_ON_KEY_UP, "OnKeyUp", "", "i") \ X(G_EVENT_KEY_UP, "OnKeyUp", "", "i") \
X(G_EXPORT_ON_FRAME_REFRESH, "OnFrameRefresh", "", "") \ X(G_EVENT_FRAME_REFRESH, "OnFrameRefresh", "", "") \
X(G_EXPORT_ON_FRAME_RESIZE, "OnFrameResize", "", "ii") \ X(G_EVENT_FRAME_RESIZE, "OnFrameResize", "", "ii") \
X(G_EXPORT_ON_RAW_EVENT, "OnRawEvent", "", "i") \ X(G_EVENT_RAW_EVENT, "OnRawEvent", "", "i") \
X(G_EXPORT_GET_RAW_EVENT_PTR, "_OrcaGetRawEventPtr", "i", "") \
X(G_EXPORT_CLOCK_INIT, "_OrcaClockInit", "", "")
typedef enum { typedef enum {
#define G_EXPORT_KIND(kind, ...) kind, #define G_EVENT_KIND(kind, ...) kind,
G_EXPORTS(G_EXPORT_KIND) G_EVENTS(G_EVENT_KIND)
G_EXPORT_COUNT G_EVENT_COUNT
} guest_export_kind; } guest_event_kind;
typedef struct g_export_desc typedef struct g_event_handler_desc
{ {
str8 name; str8 name;
str8 retTags; str8 retTags;
str8 argTags; str8 argTags;
} g_export_desc; } g_event_handler_desc;
const g_export_desc G_EXPORT_DESC[] = { const g_event_handler_desc G_EVENT_HANDLER_DESC[] = {
#define STR8LIT(s) {sizeof(s)-1, s} //NOTE: msvc doesn't accept STR8(s) as compile-time constant... #define STR8LIT(s) {sizeof(s)-1, s} //NOTE: msvc doesn't accept STR8(s) as compile-time constant...
#define G_EXPORT_DESC_ENTRY(kind, name, rets, args) {STR8LIT(name), STR8LIT(rets), STR8LIT(args)}, #define G_EVENT_HANDLER_DESC_ENTRY(kind, name, rets, args) {STR8LIT(name), STR8LIT(rets), STR8LIT(args)},
G_EXPORTS(G_EXPORT_DESC_ENTRY) G_EVENTS(G_EVENT_HANDLER_DESC_ENTRY)
#undef G_EXPORT_DESC_ENTRY #undef G_EVENT_HANDLER_DESC_ENTRY
#undef STR8LIT #undef STR8LIT
}; };
@ -71,7 +69,7 @@ typedef struct orca_runtime
IM3Environment m3Env; IM3Environment m3Env;
IM3Runtime m3Runtime; IM3Runtime m3Runtime;
IM3Module m3Module; IM3Module m3Module;
IM3Function exports[G_EXPORT_COUNT]; IM3Function eventHandlers[G_EVENT_COUNT];
u32 rawEventOffset; u32 rawEventOffset;
} orca_runtime; } orca_runtime;