diff --git a/milepost b/milepost index c041b21..b2d2d2a 160000 --- a/milepost +++ b/milepost @@ -1 +1 @@ -Subproject commit c041b212ab3c3f51d7cab6bc341e6c0e6d033962 +Subproject commit b2d2d2a587f3c8a0b7dae5521f3202b92f57ae08 diff --git a/samples/pong/src/main.c b/samples/pong/src/main.c index a98799f..613e2ab 100644 --- a/samples/pong/src/main.c +++ b/samples/pong/src/main.c @@ -78,20 +78,26 @@ void OnInit(void) #endif // TEST_IMAGE //NOTE: testing file io - file_handle file = file_open(STR8("./test_write.txt"), FILE_OPEN_CREATE | FILE_OPEN_WRITE); + file_handle file = file_open(STR8("/test_write.txt"), FILE_OPEN_CREATE | FILE_OPEN_WRITE); + if(file_last_error(file) == IO_OK) + { + str8 string = STR8("Hello, file!\n"); + file_write(file, string.len, string.ptr); + file_close(file); + } + else + { + log_error("Couldn't open file test_write.txt\n"); + } - str8 string = STR8("Hello, file!\n"); - file_write(file, string.len, string.ptr); - file_close(file); -/* - file = file_open(STR8("/dir1/test_read.txt"), IO_OPEN_READ); + file = file_open(STR8("/dir1/test_read.txt"), FILE_OPEN_READ); u64 size = file_size(file); char* buffer = mem_arena_alloc(mem_scratch(), size); file_read(file, size, buffer); file_close(file); log_info("read file: %.*s", (int)size, buffer); -*/ + } void OnFrameResize(u32 width, u32 height) diff --git a/src/io_impl.c b/src/io_impl.c index 6729c40..f9b58e9 100644 --- a/src/io_impl.c +++ b/src/io_impl.c @@ -5,10 +5,12 @@ * @date: 09/05/2023 * *****************************************************************/ -#include"platform/platform_io.h" +#include"platform/platform_io_internal.h" +#include"orca_app.h" io_cmp orca_io_wait_single_req(io_req* wasmReq) { + orca_app* orca = orca_app_get(); mem_arena* scratch = mem_scratch(); io_cmp cmp = {0}; @@ -17,7 +19,7 @@ io_cmp orca_io_wait_single_req(io_req* wasmReq) // 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(__orcaApp.runtime.m3Runtime, &memSize, 0); + char* memory = (char*)m3_GetMemory(orca->runtime.m3Runtime, &memSize, 0); if(bufferIndex + req.size > memSize) { @@ -27,20 +29,16 @@ io_cmp orca_io_wait_single_req(io_req* wasmReq) { req.buffer = memory + bufferIndex; - //TODO: do some further ownership/rights checking here, and make sure we modify flags to avoid walking out the app folder - if(req.op == IO_OP_OPEN_AT) { - //////////////////////////////////////////////////////////////////////// - //TODO: should change root to app local folder - // - if file handle is null, set it to pre-opened handle to app local folder - // - if file handle is not null, check that it is valid - // --> this means we probably need a second indirection: from wasm file handle to native file handle - //////////////////////////////////////////////////////////////////////// + if(req.handle.h == 0) + { + //NOTE: change root to app local folder + req.handle = orca->rootDir; + req.openFlags |= FILE_OPEN_RESTRICT; + } } - - cmp = io_wait_single_req(&req); - + cmp = io_wait_single_req_with_table(&req, &orca->fileTable); } return(cmp); } diff --git a/src/main.c b/src/main.c index fb04fd5..32b6cd9 100644 --- a/src/main.c +++ b/src/main.c @@ -14,20 +14,14 @@ #include"milepost.h" #include"graphics_common.h" -#include"orca_runtime.h" +#include"orca_app.h" #include"memory_impl.c" +#include"io_impl.c" + #define LOG_SUBSYSTEM "Orca" - -void mg_matrix_push_flat(float a11, float a12, float a13, - float a21, float a22, float a23) -{ - mg_mat2x3 m = {a11, a12, a13, a21, a22, a23}; - mg_matrix_push(m); -} - int orca_assert(const char* file, const char* function, int line, const char* src, const char* note) { mem_arena* scratch = mem_scratch(); @@ -88,56 +82,17 @@ mg_font mg_font_create_default() } -typedef struct log_entry -{ - list_elt listElt; - u64 cap; - - log_level level; - str8 file; - str8 function; - int line; - str8 msg; - - u64 recordIndex; - -} log_entry; - -typedef struct orca_debug_overlay -{ - bool show; - mg_surface surface; - mg_canvas canvas; - mg_font fontReg; - mg_font fontBold; - ui_context ui; - - - mem_arena logArena; - list_info logEntries; - list_info logFreeList; - u32 entryCount; - u32 maxEntries; - u64 logEntryTotalCount; - bool logScrollToLast; - -} orca_debug_overlay; - -typedef struct orca_app -{ - mp_window window; - mg_surface surface; - mg_canvas canvas; - - orca_runtime runtime; - - orca_debug_overlay debugOverlay; - -} orca_app; - orca_app __orcaApp = {0}; -#include"io_impl.c" +orca_app* orca_app_get() +{ + return(&__orcaApp); +} + +orca_runtime* orca_runtime_get() +{ + return(&__orcaApp.runtime); +} void orca_log(log_level level, int fileLen, @@ -343,11 +298,6 @@ void orca_runtime_init(orca_runtime* runtime) runtime->wasmMemory.ptr = mem_base_reserve(allocator, runtime->wasmMemory.reserved); } -orca_runtime* orca_runtime_get() -{ - return(&__orcaApp.runtime); -} - #include"bindgen_core_api.c" #include"canvas_api_bind.c" #include"io_api_bind_gen.c" @@ -414,36 +364,7 @@ void* orca_runloop(void* user) return((void*)-1); } - //NOTE: Find heap base - u32 heapBase = 0; - { - IM3Global global = m3_FindGlobal(app->runtime.m3Module, "__heap_base"); - if(global) - { - M3TaggedValue val; - M3Result res = m3_GetGlobal(global, &val); - if(!res && val.type == c_m3Type_i32) - { - heapBase = val.value.i32; - } - else - { - log_error("couldn't get value of __heap_base\n"); - return((void*)-1); - } - } - else - { - log_error("couldn't locate __heap_base\n"); - return((void*)-1); - } - } - //NOTE: align heap base on 16Bytes - heapBase = AlignUpOnPow2(heapBase, 16); - log_info("mem_size = %u, __heap_base = %u\n", m3_GetMemorySize(app->runtime.m3Runtime), heapBase); - //NOTE: Find and type check event handlers. - for(int i=0; ifileTable); + app->rootDir = cmp.handle; + } //NOTE: prepare GL surface mg_surface_prepare(app->surface); @@ -780,6 +711,7 @@ int main(int argc, char** argv) orca_app* orca = &__orcaApp; + //NOTE: create window and surfaces mp_rect windowRect = {.x = 100, .y = 100, .w = 810, .h = 610}; orca->window = mp_window_create(windowRect, "orca", 0); orca->surface = mg_surface_create_for_window(orca->window, MG_CANVAS); diff --git a/src/memory_impl.c b/src/memory_impl.c index c940b9d..d2839ed 100644 --- a/src/memory_impl.c +++ b/src/memory_impl.c @@ -6,7 +6,7 @@ * *****************************************************************/ -#include"orca_runtime.h" +#include"orca_app.h" void* wasm_memory_resize_callback(void* p, unsigned long size, void* userData) { diff --git a/src/orca_runtime.h b/src/orca_app.h similarity index 68% rename from src/orca_runtime.h rename to src/orca_app.h index def757d..53c99ad 100644 --- a/src/orca_runtime.h +++ b/src/orca_app.h @@ -8,6 +8,8 @@ #ifndef __ORCA_RUNTIME_H_ #define __ORCA_RUNTIME_H_ +#include"platform/platform_io_internal.h" + #include"wasm3.h" #include"m3_env.h" #include"m3_compile.h" @@ -65,7 +67,57 @@ typedef struct orca_runtime } orca_runtime; +typedef struct log_entry +{ + list_elt listElt; + u64 cap; + log_level level; + str8 file; + str8 function; + int line; + str8 msg; + + u64 recordIndex; + +} log_entry; + +typedef struct orca_debug_overlay +{ + bool show; + mg_surface surface; + mg_canvas canvas; + mg_font fontReg; + mg_font fontBold; + ui_context ui; + + + mem_arena logArena; + list_info logEntries; + list_info logFreeList; + u32 entryCount; + u32 maxEntries; + u64 logEntryTotalCount; + bool logScrollToLast; + +} orca_debug_overlay; + +typedef struct orca_app +{ + mp_window window; + mg_surface surface; + mg_canvas canvas; + + file_table fileTable; + file_handle rootDir; + + orca_runtime runtime; + + orca_debug_overlay debugOverlay; + +} orca_app; + +orca_app* orca_app_get(); orca_runtime* orca_runtime_get();