Add no-entry to project/command line. Add "link-args" to project. Add @wasm and @extern attributes. Added $$wasm_memory_size and $$wasm_memory_grow builtins.

This commit is contained in:
Christoffer Lerno
2023-01-26 11:46:38 +01:00
committed by Christoffer Lerno
parent 39801a304d
commit a95710c93f
18 changed files with 153 additions and 19 deletions

View File

@@ -178,4 +178,33 @@ fn void ArenaAllocator.init(ArenaAllocator* this, char[] data)
fn void ArenaAllocator.reset(ArenaAllocator* this)
{
this.used = 0;
}
const usz WASM_BLOCK_SIZE = 65536;
WasmMemory wasm_memory;
struct WasmMemory
{
usz allocation;
uptr use;
}
fn void*! WasmMemory.allocate_block(WasmMemory* this, usz bytes)
{
if (!this.allocation)
{
this.allocation = $$wasm_memory_size(0) * WASM_BLOCK_SIZE;
}
isz bytes_required = bytes + this.use - this.allocation;
if (bytes_required <= 0)
{
defer this.use += bytes;
return (void*)this.use;
}
usz blocks_required = (bytes_required + WASM_BLOCK_SIZE + 1) / WASM_BLOCK_SIZE;
if (!$$wasm_memory_grow(0, blocks_required)) return AllocationFailure.OUT_OF_MEMORY!;
this.allocation = $$wasm_memory_size(0) * WASM_BLOCK_SIZE;
defer this.use += bytes;
return (void*)this.use;
}