From ee9e70e52d7f26e1949e34c3e3a74601d1085055 Mon Sep 17 00:00:00 2001 From: David Konsumer Date: Mon, 14 Oct 2024 14:34:31 -0700 Subject: [PATCH] make c & as match for file_read --- cart/as/build.js | 2 +- cart/as/null0.ts | 27 +++++++++++++++++---------- cart/as/src/assets/cyber.txt | 2 ++ cart/as/src/main.ts | 2 +- cart/c/null0.h | 16 +++++++++++----- null0_api/src/null0_api_wamr.h | 15 +++++++++++---- 6 files changed, 43 insertions(+), 21 deletions(-) diff --git a/cart/as/build.js b/cart/as/build.js index 003219d..cd33165 100644 --- a/cart/as/build.js +++ b/cart/as/build.js @@ -19,7 +19,7 @@ await writeFile('build/tmp.ts', (await readFile('null0.ts')) + '\n// user-code:\ const { error, stdout, stderr, stats } = await asc.main([ 'build/tmp.ts', // '--lowMemoryLimit', - '--stackSize', '8092', + '--stackSize', '2097152', '--runtime', 'stub', '--config', './node_modules/@assemblyscript/wasi-shim/asconfig.json', '--optimizeLevel', '3', diff --git a/cart/as/null0.ts b/cart/as/null0.ts index e176ff5..42b0e8b 100644 --- a/cart/as/null0.ts +++ b/cart/as/null0.ts @@ -1,5 +1,15 @@ // this code will be injected at the top of your cart-code +export function malloc(size: usize, id: u32 = 0): usize { + const pout = __new(size, id) + __pin(pout) + return pout +} + +export function free(pointer:usize): void { + __unpin(pointer) +} + // ENUMS enum ImageFilter { @@ -246,7 +256,7 @@ class Rectangle { } class FileInfo { - constructor(filesize:i64 = 0, modtime:i64 = 0, createtime:i64 = 0, accesstime:i64 = 0, filetype:FileType = FILETYPE_REGULAR, readonly:boolean=false) { + constructor(filesize:i64 = 0, modtime:i64 = 0, createtime:i64 = 0, accesstime:i64 = 0, filetype:FileType = FileType.FILETYPE_REGULAR, readonly:boolean=false) { this.filesize = filesize this.modtime = modtime this.createtime = createtime @@ -811,16 +821,13 @@ declare function draw_rectangle_rounded_outline_on_image(destination: u32, x: i3 // Read a file from cart (or local persistant) @external("null0", "file_read") -declare function _null0_file_read(filename: ArrayBuffer, bytesRead: usize): ArrayBuffer +declare function _null0_file_read(filename: ArrayBuffer, byteSize:UsizePointer, retBytes:ArrayBuffer): void function file_read(filename: string): ArrayBuffer { + const i = file_info(filename) + const ret = new ArrayBuffer(i.filesize) const b = new UsizePointer() - const d = _null0_file_read(String.UTF8.encode(filename, true), changetype(b)) - - // TODO: do I really need to copy to get it the right length? - const o = new ArrayBuffer(b.value) - memory.copy(changetype(o), changetype(d), b.value) - - return o + _null0_file_read(String.UTF8.encode(filename, true), b, ret) + return ret } // Write a file to persistant storage @@ -840,7 +847,7 @@ function file_append(filename: string, data: ArrayBuffer): boolean { // Get info about a single file @external("null0", "file_info") declare function _null0_file_info(ret:FileInfo, filename: ArrayBuffer): void -function file_info(): FileInfo { +function file_info(filename:string): FileInfo { const r = new FileInfo() _null0_file_info(r, String.UTF8.encode(filename, true)) return r diff --git a/cart/as/src/assets/cyber.txt b/cart/as/src/assets/cyber.txt index 8c8b365..0444eaa 100755 --- a/cart/as/src/assets/cyber.txt +++ b/cart/as/src/assets/cyber.txt @@ -1 +1,3 @@ Disposable voodoo god crypto-sensory corporation beef noodles sprawl human engine monofilament neural. Marketing sensory office footage Legba alcohol man hotdog modem-space sentient. Office sign futurity silent gang industrial grade post. Kanji pre-franchise meta-render-farm corrupted spook city shanty town claymore mine-ware systema savant plastic Shibuya fluidity grenade. Plastic jeans post-courier systema film network convenience store. Car engine rebar into weathered refrigerator Shibuya realism assault-space hacker pre-Kowloon footage media network pen. Cartel tanto sentient knife hotdog network market tank-traps crypto-range-rover towards. Dome shrine stimulate nodality katana meta-pen futurity math-receding. + +Sprawl DIY construct dolphin jeans-space fetishism gang Shibuya sunglasses market. Alcohol cyber-bomb dolphin soul-delay drone bridge into market convenience store render-farm BASE jump long-chain hydrocarbons pre-nodal point. Nodal point shoes skyscraper tiger-team convenience store stimulate papier-mache car katana. Skyscraper tank-traps shoes math-convenience store beef noodles face forwards assassin sub-orbital narrative semiotics weathered silent monofilament kanji RAF. Crypto-paranoid sensory uplink San Francisco rain knife systemic courier garage jeans. Military-grade shoes physical sensory cardboard geodesic Chiba semiotics motion futurity katana sprawl BASE jump. Bomb semiotics assassin A.I. 3D-printed augmented reality dolphin San Francisco meta. Systema tattoo construct neon into order-flow stimulate drugs 8-bit sign Legba. Fetishism cartel gang urban shrine nano-saturation point woman receding otaku monofilament 8-bit free-market rebar j-pop. \ No newline at end of file diff --git a/cart/as/src/main.ts b/cart/as/src/main.ts index ee466f8..2c2367e 100644 --- a/cart/as/src/main.ts +++ b/cart/as/src/main.ts @@ -10,7 +10,7 @@ export function load(): void { y = 120 - (d.height/2) const t = file_read("assets/cyber.txt") - trace(t.byteLength.toString()) + trace(`WASM ArrayBuffer size: ${t.byteLength.toString()}`) trace(String.UTF8.decode(t)) } diff --git a/cart/c/null0.h b/cart/c/null0.h index 54a4857..62e5cd2 100755 --- a/cart/c/null0.h +++ b/cart/c/null0.h @@ -770,9 +770,19 @@ void draw_rectangle_rounded_outline_on_image(u32 destination, i32 x, i32 y, i32 /////////// FILESYSTEM /////////// +// Get info about a single file +NULL0_IMPORT("file_info") +FileInfo file_info(char* filename); + // Read a file from cart NULL0_IMPORT("file_read") -u8* file_read(char* filename, u32* bytesRead); +void _null0_file_read(char* filename, u32* bytesRead, u8* ret); +u8* file_read(char* filename, u32* bytesRead) { + FileInfo i = file_info(filename); + u8* ret = malloc(i.filesize); + _null0_file_read(filename, bytesRead, ret); + return ret; +} // Write a file to persistant storage NULL0_IMPORT("file_write") @@ -782,10 +792,6 @@ bool file_write(char* filename, u8* data, u32 byteSize); NULL0_IMPORT("file_append") bool file_append(char* filename, u8* data, u32 byteSize); -// Get info about a single file -NULL0_IMPORT("file_info") -FileInfo file_info(char* filename); - // Get list of files in a directory NULL0_IMPORT("file_list") char** file_list(char* dir); diff --git a/null0_api/src/null0_api_wamr.h b/null0_api/src/null0_api_wamr.h index 29b4388..e37ddb8 100755 --- a/null0_api/src/null0_api_wamr.h +++ b/null0_api/src/null0_api_wamr.h @@ -8,6 +8,8 @@ wasm_exec_env_t exec_env; wasm_module_inst_t module_inst; // these callbacks are from the cart +wasm_function_inst_t cart_malloc_real = NULL; +wasm_function_inst_t cart_free_real = NULL; wasm_function_inst_t cart_update = NULL; wasm_function_inst_t cart_unload = NULL; wasm_function_inst_t cart_buttonUp = NULL; @@ -58,9 +60,11 @@ void params_from_sfx_to_wasm(SfxParams* s, SfxParams* sOut) { } // Read a file from cart (or local persistant) -static uint32_t wamr_null0_file_read(wasm_exec_env_t exec_env, char* filename, uint32_t* bytesRead) { - char* bytes = null0_file_read(filename, bytesRead); - return wasm_runtime_module_dup_data(module_inst, bytes, *bytesRead); +static void wamr_null0_file_read(wasm_exec_env_t exec_env, char* filename, uint32_t* bytesRead, char* retBytes) { + char* bytesHost = null0_file_read(filename, bytesRead); + memcpy(retBytes, bytesHost, *bytesRead); + printf("HOST bytesRead: %u\n", *bytesRead); + free(bytesHost); } // Get the user's writable dir (where file writes or appends go) @@ -653,7 +657,7 @@ static void wamr_null0_color_bilinear_interpolate(wasm_exec_env_t exec_env, Null } static NativeSymbol null0_wamr_callbacks[] = { - {"file_read", wamr_null0_file_read, "($*)i"}, + {"file_read", wamr_null0_file_read, "($**)"}, {"get_write_dir", wamr_null0_get_write_dir, "()i"}, {"trace", wamr_null0_trace, "($)"}, {"current_time", wamr_null0_current_time, "()I"}, @@ -825,6 +829,9 @@ bool null0_init() { exec_env = wasm_runtime_create_exec_env(module_inst, stack_size); + cart_malloc_real = wasm_runtime_lookup_function(module_inst, "malloc"); + cart_free_real = wasm_runtime_lookup_function(module_inst, "free"); + cart_update = wasm_runtime_lookup_function(module_inst, "update"); cart_buttonUp = wasm_runtime_lookup_function(module_inst, "buttonUp"); cart_buttonDown = wasm_runtime_lookup_function(module_inst, "buttonDown");