From d4cb93c61a431afd71b9096728d758f79e5d5fe2 Mon Sep 17 00:00:00 2001 From: Reuben Dunnington Date: Wed, 23 Aug 2023 13:00:38 -0700 Subject: [PATCH] oc_runtime_window_set_title and oc_runtime_window_set_size --- scripts/dev.py | 1 + src/app/app.h | 2 +- src/orca.c | 1 + src/runtime.c | 25 +++++++++++++++++++++++-- src/runtime.h | 2 ++ src/runtime_io.c | 20 +++++++++----------- src/wasmbind/core_api.json | 2 +- 7 files changed, 38 insertions(+), 15 deletions(-) diff --git a/scripts/dev.py b/scripts/dev.py index e465ea9..6a6ad8a 100644 --- a/scripts/dev.py +++ b/scripts/dev.py @@ -397,6 +397,7 @@ def gen_all_bindings(): ) bindgen("core", "src/wasmbind/core_api.json", + guest_stubs="src/wasmbind/core_api_stubs.c", wasm3_bindings="src/wasmbind/core_api_bind_gen.c", ) diff --git a/src/app/app.h b/src/app/app.h index 34c8699..b8f8639 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -405,7 +405,7 @@ ORCA_API int oc_directory_create(oc_str8 path); void ORCA_IMPORT(oc_request_quit)(void); -void ORCA_IMPORT(oc_runtime_window_set_title)(const char* title); +void ORCA_IMPORT(oc_runtime_window_set_title)(oc_str8 title); void ORCA_IMPORT(oc_runtime_window_set_size)(f32 width, f32 height); diff --git a/src/orca.c b/src/orca.c index a74ebe0..078a89e 100644 --- a/src/orca.c +++ b/src/orca.c @@ -96,6 +96,7 @@ //NOTE: macos application layer and graphics backends are defined in orca.m #elif OC_PLATFORM_ORCA #include "app/orca_app.c" + #include "wasmbind/core_api_stubs.c" #include "graphics/graphics_common.c" #include "graphics/orca_surface_stubs.c" #else diff --git a/src/runtime.c b/src/runtime.c index f47ea3f..8a820ec 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -66,9 +66,30 @@ oc_runtime_env* oc_runtime_env_get() return (&__orcaApp.runtime); } -void oc_runtime_window_set_title(const char* title) +void* oc_runtime_ptr_to_native(oc_runtime* orca, void* wasmPtr, u32 length) { - oc_window_set_title(__orcaApp.window, OC_STR8(title)); + // We can't use the runtime's memory pointer directly because wasm3 embeds a + // header at the beginning of the block we give it. + u64 bufferIndex = (u64)wasmPtr & 0xffffffff; + u32 memSize = 0; + char* memory = (char*)m3_GetMemory(orca->runtime.m3Runtime, &memSize, 0); + + if(bufferIndex + length < memSize) + { + char* nativePtr = memory + bufferIndex; + return nativePtr; + } + + return NULL; +} + +void oc_runtime_window_set_title(oc_str8 title) +{ + title.ptr = oc_runtime_ptr_to_native(oc_runtime_get(), title.ptr, title.len); + if(title.ptr) + { + oc_window_set_title(__orcaApp.window, title); + } } void oc_runtime_window_set_size(f32 width, f32 height) diff --git a/src/runtime.h b/src/runtime.h index 92302e4..7d6b4d2 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -125,4 +125,6 @@ typedef struct oc_runtime oc_runtime* oc_runtime_get(); oc_runtime_env* oc_runtime_env_get(); +void* oc_runtime_ptr_to_native(oc_runtime* runtime, void* wasmPtr, u32 length); + #endif //__RUNTIME_H_ diff --git a/src/runtime_io.c b/src/runtime_io.c index 0aad530..aa80007 100644 --- a/src/runtime_io.c +++ b/src/runtime_io.c @@ -14,19 +14,12 @@ oc_io_cmp oc_runtime_io_wait_single_req(oc_io_req* wasmReq) oc_io_cmp cmp = { 0 }; oc_io_req req = *wasmReq; - //NOTE: convert the req->buffer wasm pointer to a native pointer - // for some reason, wasm3 memory doesn't start at the beginning of the block we give it. - u64 bufferIndex = (u64)req.buffer & 0xffffffff; - u32 memSize = 0; - char* memory = (char*)m3_GetMemory(orca->runtime.m3Runtime, &memSize, 0); - if(bufferIndex + req.size > memSize) + void* buffer = oc_runtime_ptr_to_native(orca, req.buffer, req.size); + + if(buffer) { - cmp.error = OC_IO_ERR_ARG; - } - else - { - req.buffer = memory + bufferIndex; + req.buffer = buffer; if(req.op == OC_IO_OPEN_AT) { @@ -39,5 +32,10 @@ oc_io_cmp oc_runtime_io_wait_single_req(oc_io_req* wasmReq) } cmp = oc_io_wait_single_req_with_table(&req, &orca->fileTable); } + else + { + cmp.error = OC_IO_ERR_ARG; + } + return (cmp); } diff --git a/src/wasmbind/core_api.json b/src/wasmbind/core_api.json index 15297b9..c237570 100644 --- a/src/wasmbind/core_api.json +++ b/src/wasmbind/core_api.json @@ -76,7 +76,7 @@ "ret": {"name": "void", "tag": "v"}, "args": [ { "name": "title", - "type": {"name": "const char*", "tag": "p"}} + "type": {"name": "oc_str8", "tag": "S"}} ] }, {