Skip to content

Commit

Permalink
make c & as match for file_read
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumer committed Oct 14, 2024
1 parent 7e9ac2f commit ee9e70e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cart/as/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
27 changes: 17 additions & 10 deletions cart/as/null0.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(<i32>i.filesize)
const b = new UsizePointer()
const d = _null0_file_read(String.UTF8.encode(filename, true), changetype<usize>(b))

// TODO: do I really need to copy to get it the right length?
const o = new ArrayBuffer(b.value)
memory.copy(changetype<usize>(o), changetype<usize>(d), b.value)

return o
_null0_file_read(String.UTF8.encode(filename, true), b, ret)
return ret
}

// Write a file to persistant storage
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cart/as/src/assets/cyber.txt
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion cart/as/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
16 changes: 11 additions & 5 deletions cart/c/null0.h
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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);
Expand Down
15 changes: 11 additions & 4 deletions null0_api/src/null0_api_wamr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit ee9e70e

Please sign in to comment.