Compare commits

...
Author SHA1 Message Date
t3hn3rd 302dcb66f0 feature: WASI preview1 support, per-context isolation & glue API
Implement full WASI snapshot_preview1 with 46 registered host functions,
per-context hook/registry isolation, and a single-unit public API.

WASI Preview1 (15 new handler units):
- Tier 1: fd_write, fd_read, fd_close, fd_seek, fd_prestat_get,
  fd_prestat_dir_name, fd_fdstat_get, proc_exit, environ_*, args_*
- Tier 2: clock_time_get, clock_res_get, random_get
- Tier 3: 14 fd_* stubs, 10 path_* stubs (ENOSYS)
- Tier 4: poll_oneoff, proc_raise, sched_yield, 4 sock_* stubs

Per-context isolation:
- Move WASIHooks (12 hook table) and HostFuncRegistry onto
  TWASMProcessContext — each context is fully independent
- Dynamic registry: starts at 16 entries, doubles on demand, no cap
- All 15 preview1 handlers read hooks from context, not globals

Glue API (wasm.pas):
- Single entry point: wasm_init, wasm_load, wasm_set_*,
  wasm_register_wasi_preview1, wasm_register_host_func,
  wasm_resolve_imports, wasm_start, wasm_prepare_start, wasm_tick,
  wasm_dump_state
- WASURO.lpr simplified to use only the wasm unit

Refactoring:
- Rename leb128.pas -> wasm.types.leb128.pas (updates 40+ opcode units)
- New wasm.types.wasi.pas with WASI errno/filetype constants
- New wasm.types.context.pas fields for hooks + registry types
- New wasm.vm.setup unit (find_start, count_func_imports)
- New wasi.emu.hooks for emulator-side hook implementations
- Add read_uint16, write_uint16 to wasm.types.heap

Testing:
- 10 new test units: hooks, registry, glue, stubs, clock, random,
  extensibility, call, types, vm.setup (1087 tests / 232 units)
- E2E WAT test runner (wat.ps1) with 9 integration tests:
  hello, exit42, multiwrite, prestat, fdstat, environ, clock,
  clockres, random

121 files changed, 5391 insertions(+), 267 deletions(-)
2026-02-25 21:44:00 +00:00
t3hn3rd 378f999143 Merge branch 'hotfix/readme.2' into develop 2026-02-24 23:18:43 +00:00
t3hn3rd c2e47ffd7a ReadMe updated to reflect current project status. 2026-02-24 23:18:35 +00:00
t3hn3rd 1f27b99284 Merge branch 'feature/debug-output' into develop 2026-02-24 23:08:12 +00:00
t3hn3rd 0a4bd589ee feature: Compiler flag added for verbose debug output
VM will now only output verbose debugging information if -dDEBUG_OUTPUT is supplied.
2026-02-24 23:07:58 +00:00
t3hn3rd eda3669ab2 Merge branch 'feature/build-script-conversion-ps' into develop 2026-02-24 21:52:51 +00:00
130 changed files with 5564 additions and 288 deletions

No files matched your search

+2
View File
@@ -2,3 +2,5 @@
lib lib
AGENTS.md AGENTS.md
GAP_ANALYSIS.md GAP_ANALYSIS.md
*.pdf
doc/WASI.md
+329 -62
View File
File diff suppressed because it is too large. Load diff
+30 -5
View File
@@ -3,7 +3,9 @@ param(
[switch]$Test, [switch]$Test,
[switch]$Clean, [switch]$Clean,
[switch]$Run, [switch]$Run,
[switch]$Help [switch]$Help,
[switch]$Debug,
[switch]$E2E
) )
# Also accept positional string args (e.g. .\build.ps1 clean test run) # Also accept positional string args (e.g. .\build.ps1 clean test run)
@@ -12,7 +14,9 @@ foreach ($a in $args) {
'test' { $Test = $true } 'test' { $Test = $true }
'clean' { $Clean = $true } 'clean' { $Clean = $true }
'run' { $Run = $true } 'run' { $Run = $true }
'debug' { $Debug = $true }
'help' { $Help = $true } 'help' { $Help = $true }
'e2e' { $E2E = $true }
} }
} }
@@ -22,6 +26,8 @@ if ($Help) {
Write-Host ' test - Build with tests enabled' Write-Host ' test - Build with tests enabled'
Write-Host ' clean - Clean build outputs' Write-Host ' clean - Clean build outputs'
Write-Host ' run - Run the built executable after building' Write-Host ' run - Run the built executable after building'
Write-Host ' e2e - Run end-to-end wat tests'
Write-Host ' debug - Build with debug info and assertions enabled'
Write-Host ' help - Show this help message' Write-Host ' help - Show this help message'
exit 0 exit 0
} }
@@ -32,13 +38,17 @@ $Bin = Join-Path $Root 'bin'
$Lib = Join-Path $Root 'lib' $Lib = Join-Path $Root 'lib'
$FPC = 'C:\Lazarus\fpc\3.2.2\bin\x86_64-win64\fpc.exe' $FPC = 'C:\Lazarus\fpc\3.2.2\bin\x86_64-win64\fpc.exe'
if (($Test -and $E2E) -or ($Run -and $E2E)) {
Write-Host 'Error: --e2e cannot be combined with --test or --run. E2E implies a run of E2E wat tests.' -ForegroundColor Red
exit 1
}
# ── Clean ──────────────────────────────────────────────────────────── # ── Clean ────────────────────────────────────────────────────────────
if ($Clean) { if ($Clean) {
Write-Host 'Cleaning build outputs...' Write-Host 'Cleaning build outputs...'
if (Test-Path $Bin) { Remove-Item $Bin -Recurse -Force } if (Test-Path $Bin) { Remove-Item "$Bin\*" -Recurse -Force }
if (Test-Path $Lib) { Remove-Item $Lib -Recurse -Force } if (Test-Path $Lib) { Remove-Item "$Lib\*" -Recurse -Force }
Write-Host 'Clean complete.' Write-Host 'Clean complete.'
if (-not $Test -and -not $Run) { exit 0 }
} }
if (-not (Test-Path $Bin)) { New-Item $Bin -ItemType Directory | Out-Null } if (-not (Test-Path $Bin)) { New-Item $Bin -ItemType Directory | Out-Null }
@@ -50,10 +60,12 @@ if ($LASTEXITCODE -ne 0) { exit 1 }
# ── Build ──────────────────────────────────────────────────────────── # ── Build ────────────────────────────────────────────────────────────
$TestFlags = @() $TestFlags = @()
if ($Test) { $TestFlags = @('-dRUN_TESTS') } if ($Test) { $TestFlags += '-dRUN_TESTS' }
if ($Debug) { $TestFlags += '-dDEBUG_OUTPUT' }
$fpcArgs = @( $fpcArgs = @(
$TestFlags $TestFlags
"-v0ew"
"-FE$Bin" "-FE$Bin"
"-FU$Lib" "-FU$Lib"
"-Fu$Src\emu" "-Fu$Src\emu"
@@ -68,6 +80,9 @@ $fpcArgs = @(
"-Fu$Src\wasm\types" "-Fu$Src\wasm\types"
"-Fu$Src\wasm\vm" "-Fu$Src\wasm\vm"
"-Fu$Src\wasm\vm\opcodes" "-Fu$Src\wasm\vm\opcodes"
"-Fu$Src\wasm\wasi"
"-Fu$Src\wasm\wasi\preview1"
"-Fu$Src\wasm\test\wasi"
'-gw3' '-gw3'
"-o$Bin\WASURO.exe" "-o$Bin\WASURO.exe"
"$Src\project\WASURO.lpr" "$Src\project\WASURO.lpr"
@@ -86,3 +101,13 @@ if ($Run) {
Write-Host 'Running WASURO...' Write-Host 'Running WASURO...'
& "$Bin\WASURO.exe" & "$Bin\WASURO.exe"
} }
if ($E2E) {
Write-Host 'Running end-to-end WAT tests...'
& (Join-Path $Root 'wat.ps1')
if ($LASTEXITCODE -ne 0) {
Write-Host "WAT tests failed with error code $LASTEXITCODE"
exit $LASTEXITCODE
}
exit $LASTEXITCODE
}
+1
View File
@@ -45,3 +45,4 @@ if ($ruleHits -gt 0) {
# ── Add more rules here ───────────────────────────────────────────── # ── Add more rules here ─────────────────────────────────────────────
if ($fail) { exit 1 } if ($fail) { exit 1 }
Write-Host 'Lint passed.' Write-Host 'Lint passed.'
exit 0
-66
View File
@@ -1,66 +0,0 @@
unit leb128;
interface
uses
types;
function read_leb128_to_uint64(buf : puint8; buf_end : puint8; res : puint64) : uint8;
function read_leb128_to_uint32(buf : puint8; buf_end : puint8; res : puint32) : uint8;
implementation
function read_leb128_to_uint64(buf: puint8; buf_end: puint8; res: puint64): uint8;
var
ptr : puint8;
shift : uint32;
localResult : uint64;
currentByte : uint8;
begin
ptr:= buf;
shift:= 0;
localResult:= 0;
while (true) do begin
if (ptr >= buf_end) then begin
read_leb128_to_uint64:= 0;
exit;
end;
currentByte:= ptr^;
inc(ptr);
localResult:= localResult or uint64(currentByte and $7F) SHL Shift;
if ((currentByte AND $80) = 0) then break;
Inc(Shift, 7);
end;
res^:= localResult;
read_leb128_to_uint64:= uint8(ptr - buf);
end;
function read_leb128_to_uint32(buf: puint8; buf_end: puint8; res: puint32): uint8;
var
ptr : puint8;
shift : uint32;
localResult : uint32;
currentByte : uint8;
begin
ptr:= buf;
shift:= 0;
localResult:= 0;
while (true) do begin
if (ptr >= buf_end) then begin
read_leb128_to_uint32:= 0;
exit;
end;
currentByte:= ptr^;
inc(ptr);
localResult:= localResult or uint64(currentByte and $7F) SHL Shift;
if ((currentByte AND $80) = 0) then break;
Inc(Shift, 7);
end;
res^:= localResult;
read_leb128_to_uint32:= uint8(ptr - buf);
end;
end.
+112
View File
@@ -0,0 +1,112 @@
{ Emulator-layer WASI OS hook implementations.
This unit provides native hook callbacks for running WASI programs
on a host OS (Windows/Linux). On bare-metal (Asuro), these would be
replaced by kernel-level implementations.
Hooks are registered per-context via PWASMProcessContext.
This unit has NO knowledge of WASM internals (no stacks, no linear memory). }
unit wasi.emu.hooks;
interface
uses
wasm.types.context;
procedure register_emu_hooks(ctx : PWASMProcessContext);
implementation
uses
wasm.types.builtin,
wasm.wasi.hooks,
wasm.types.wasi;
{ fd_write: write buf to stdout (fd=1) or stderr (fd=2) }
function emu_fd_write(fd: TWASMUInt32;
buf: TWASMPUInt8;
len: TWASMUInt32): TWASMUInt32;
var
i: TWASMUInt32;
begin
if (fd = 1) or (fd = 2) then begin
for i := 0 to len - 1 do
Write(TWASMChar(buf[i]));
emu_fd_write := len;
end else
emu_fd_write := 0;
end;
{ fd_read: stub — no stdin support in emu layer yet }
function emu_fd_read(fd: TWASMUInt32;
buf: TWASMPUInt8;
len: TWASMUInt32): TWASMUInt32;
begin
emu_fd_read := 0;
end;
{ fd_close: no-op for emu layer }
function emu_fd_close(fd: TWASMUInt32): TWASMUInt32;
begin
emu_fd_close := 0; { ESUCCESS }
end;
{ proc_exit: halt the program with the given exit code }
procedure emu_proc_exit(code: TWASMUInt32);
begin
{ The glue layer already sets Running := false and ExitCode.
On the emu layer, we also halt the host process. }
halt(code);
end;
{ clock_time_get: return current time in nanoseconds }
function emu_clock_time_get(clock_id: TWASMUInt32;
precision: TWASMUInt64;
var time: TWASMUInt64): TWASMUInt32;
begin
{ Stub: return 0 nanoseconds. On a real OS we would query the system clock.
Bare-metal would use RDTSC or PIT. }
time := 0;
emu_clock_time_get := WASI_ESUCCESS;
end;
{ clock_res_get: return clock resolution in nanoseconds }
function emu_clock_res_get(clock_id: TWASMUInt32;
var resolution: TWASMUInt64): TWASMUInt32;
begin
{ Stub: report 1ms resolution }
resolution := 1000000;
emu_clock_res_get := WASI_ESUCCESS;
end;
{ random_get: fill buffer with random bytes }
function emu_random_get(buf: TWASMPUInt8;
len: TWASMUInt32): TWASMUInt32;
var
i: TWASMUInt32;
state: TWASMUInt32;
begin
{ Simple xorshift PRNG — not cryptographic, but deterministic
and works without RTL. On bare-metal, use RDRAND. }
state := 2463534242; { seed }
for i := 0 to len - 1 do begin
state := state xor (state shl 13);
state := state xor (state shr 17);
state := state xor (state shl 5);
buf[i] := TWASMUInt8(state and $FF);
end;
emu_random_get := WASI_ESUCCESS;
end;
procedure register_emu_hooks(ctx : PWASMProcessContext);
begin
wasm.wasi.hooks.init_hook_table(ctx);
wasm.wasi.hooks.register_fd_write(ctx, @emu_fd_write);
wasm.wasi.hooks.register_fd_read(ctx, @emu_fd_read);
wasm.wasi.hooks.register_fd_close(ctx, @emu_fd_close);
wasm.wasi.hooks.register_proc_exit(ctx, @emu_proc_exit);
wasm.wasi.hooks.register_clock_time_get(ctx, @emu_clock_time_get);
wasm.wasi.hooks.register_clock_res_get(ctx, @emu_clock_res_get);
wasm.wasi.hooks.register_random_get(ctx, @emu_random_get);
end;
end.
+56 -21
View File
@@ -2,15 +2,14 @@ program WASURO;
uses uses
sysutils, sysutils,
console, console,
wasm.types.builtin,
types,
wasm.types.context, wasm.types.context,
wasm.parser, wasm,
wasm.vm, wasm.test.binary.return42,
wasm.types.stack { Emu-layer OS hooks }
wasi.emu.hooks
{$IFDEF RUN_TESTS} {$IFDEF RUN_TESTS}
, wasm.test , wasm.test
, wasm.test.framework , wasm.test.framework
@@ -19,29 +18,65 @@ uses
var var
Context : PWASMProcessContext; Context : PWASMProcessContext;
ModuleSize : Int64;
ModuleBuffer : TWASMPUInt8;
ModuleFile : File;
begin begin
{$IFDEF RUN_TESTS} {$IFDEF RUN_TESTS}
wasm.vm.init(); wasm.wasm_init;
wasm.test.run_all_tests; wasm.test.run_all_tests;
halt(wasm.test.framework.FailedTests); halt(wasm.test.framework.FailedTests);
{$ELSE} {$ELSE}
writestringln('[main] Initializing VM'); {$IFDEF DEBUG_OUTPUT}
wasm.vm.init(); console.writestringln('WASURO - WebAssembly Runtime in Object Pascal');
{$ENDIF}
wasm.wasm_init;
writestringln('[main] Parsing WASM Binary'); if ParamCount > 0 then begin
Context:= wasm.parser.parse(@wasm.test.binary.return42.BINARY[0], puint8(@wasm.test.binary.return42.BINARY[0] + wasm.test.binary.return42.BINARY_SIZE)); if not FileExists(ParamStr(1)) then begin
console.writestringln('File not found.');
halt(1);
end;
Assign(ModuleFile, ParamStr(1));
Reset(ModuleFile, 1);
ModuleSize := FileSize(ModuleFile);
GetMem(ModuleBuffer, ModuleSize);
BlockRead(ModuleFile, ModuleBuffer^, ModuleSize);
Close(ModuleFile);
{$IFDEF DEBUG_OUTPUT}
write('Module loaded from file: ');
writeln(ParamStr(1));
{$ENDIF}
writestringln('[main] Running...'); { Load, configure, and run the WASM module }
Context^.ExecutionState.Running:= true; Context := wasm.wasm_load(ModuleBuffer, TWASMPUInt8(ModuleBuffer + ModuleSize));
while (wasm.vm.tick(Context)) do;
writestringln('[main] Finished Execution'); if not Context^.ValidBinary then begin
writestring('[main] Returns: '); console.writestringln('Error: Invalid WASM binary.');
while Context^.ExecutionState.Operand_Stack^.Top > 0 do begin halt(1);
write(wasm.types.stack.popi32(Context^.ExecutionState.Operand_Stack),', '); end;
{ Register emu-layer OS hooks and WASI preview1 into context }
wasi.emu.hooks.register_emu_hooks(Context);
wasm.wasm_register_wasi_preview1(Context);
{ Find _start and run to completion }
if not wasm.wasm_start(Context) then begin
console.writestringln('No _start export found.');
halt(1);
end;
{$IFDEF DEBUG_OUTPUT}
wasm.wasm_dump_state(Context);
{$ENDIF}
FreeMem(ModuleBuffer);
halt(Context^.ExitCode);
end else begin
console.writestringln('Usage: WASURO <module.wasm>');
halt(1);
end; end;
writeln();
while true do sleep(1000);
{$ENDIF} {$ENDIF}
end. end.
@@ -3,7 +3,7 @@ unit wasm.parser.sections.codeSection;
interface interface
uses uses
wasm.types.builtin, lmemorymanager, console, leb128, wasm.types.builtin, lmemorymanager, console, wasm.types.leb128,
wasm.types.enums, wasm.types.values, wasm.types.sections, wasm.types.context; wasm.types.enums, wasm.types.values, wasm.types.sections, wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext); procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -72,8 +72,10 @@ var
totalSize: TWASMUInt32; totalSize: TWASMUInt32;
begin begin
writestring('[wasm.parser] Handle Section: Code - Size: '); {$IFDEF DEBUG_OUTPUT}
writeintlnWND(section_length, 0); console.writestring('[wasm.parser] Handle Section: Code - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
// Initialize the read/end pointers // Initialize the read/end pointers
pos:= buffer; pos:= buffer;
@@ -183,8 +185,9 @@ begin
end; end;
end; end;
end; end;
{$IFDEF DEBUG_OUTPUT}
walk(ctx); walk(ctx);
{$ENDIF}
end; end;
end. end.
@@ -3,7 +3,7 @@ unit wasm.parser.sections.customSection;
interface interface
uses uses
wasm.types.builtin, lmemorymanager, console, leb128, wasm.types.builtin, lmemorymanager, console, wasm.types.leb128,
wasm.types.context; wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext); procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -12,8 +12,12 @@ implementation
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext); procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
begin begin
writestring('[wasm.parser] Handle Section: Custom - Size: '); {$IFDEF DEBUG_OUTPUT}
writeintlnWND(section_length, 0); console.writestring('[wasm.parser] Handle Section: Custom - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
{ Custom sections are ignored in this implementation }
console.writestringln('[wasm.parser] TODO: Custom section skipped.');
end; end;
end. end.
@@ -9,7 +9,7 @@ procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMPro
implementation implementation
uses console, leb128, lmemorymanager, wasm.types.sections; uses console, wasm.types.leb128, lmemorymanager, wasm.types.sections;
{ DataCount section (ID 12): contains a single u32 count of data segments. { DataCount section (ID 12): contains a single u32 count of data segments.
Pre-allocates the DataSegments array so that bulk memory opcodes Pre-allocates the DataSegments array so that bulk memory opcodes
@@ -20,8 +20,10 @@ var
bytesRead : TWASMUInt8; bytesRead : TWASMUInt8;
dataCount : TWASMUInt32; dataCount : TWASMUInt32;
begin begin
writestring('[wasm.parser] Handle Section: DataCount - Size: '); {$IFDEF DEBUG_OUTPUT}
writeintlnWND(section_length, 0); console.writestring('[wasm.parser] Handle Section: DataCount - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos := buffer; pos := buffer;
bend := TWASMPUInt8(buffer + section_length); bend := TWASMPUInt8(buffer + section_length);
@@ -29,8 +31,10 @@ begin
bytesRead := read_leb128_to_uint32(pos, bend, @dataCount); bytesRead := read_leb128_to_uint32(pos, bend, @dataCount);
Inc(pos, bytesRead); Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Data Count: '); writestring('[wasm.parser] Data Count: ');
writeintlnWND(dataCount, 0); writeintlnWND(dataCount, 0);
{$ENDIF}
{ Pre-allocate data segment storage with correct count. { Pre-allocate data segment storage with correct count.
The actual segment data will be filled when the Data section is parsed. } The actual segment data will be filled when the Data section is parsed. }
@@ -3,7 +3,7 @@ unit wasm.parser.sections.dataSection;
interface interface
uses uses
wasm.types.builtin, lmemorymanager, console, leb128, wasm.types.builtin, lmemorymanager, console, wasm.types.leb128,
wasm.types.sections, wasm.types.context, wasm.types.heap; wasm.types.sections, wasm.types.context, wasm.types.heap;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext); procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -25,8 +25,10 @@ var
segData : TWASMPUInt8; segData : TWASMPUInt8;
begin begin
writestring('[wasm.parser] Handle Section: Data - Size: '); {$IFDEF DEBUG_OUTPUT}
writeintlnWND(section_length, 0); console.writestring('[wasm.parser] Handle Section: Data - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos := buffer; pos := buffer;
bend := TWASMPUInt8(buffer + section_length); bend := TWASMPUInt8(buffer + section_length);
@@ -74,12 +76,14 @@ begin
bytesRead := read_leb128_to_uint32(pos, bend, @dataSize); bytesRead := read_leb128_to_uint32(pos, bend, @dataSize);
Inc(pos, bytesRead); Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Data Segment '); writestring('[wasm.parser] Data Segment ');
writeintWND(i, 0); writeintWND(i, 0);
writestring(' - Mode: 0 Offset: '); writestring(' - Mode: 0 Offset: ');
writeintWND(offset, 0); writeintWND(offset, 0);
writestring(' Size: '); writestring(' Size: ');
writeintlnWND(dataSize, 0); writeintlnWND(dataSize, 0);
{$ENDIF}
{ Store segment data for bulk memory ops } { Store segment data for bulk memory ops }
segData := TWASMPUInt8(kalloc(dataSize)); segData := TWASMPUInt8(kalloc(dataSize));
@@ -100,10 +104,12 @@ begin
bytesRead := read_leb128_to_uint32(pos, bend, @dataSize); bytesRead := read_leb128_to_uint32(pos, bend, @dataSize);
Inc(pos, bytesRead); Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Data Segment '); writestring('[wasm.parser] Data Segment ');
writeintWND(i, 0); writeintWND(i, 0);
writestring(' - Mode: 1 (passive) Size: '); writestring(' - Mode: 1 (passive) Size: ');
writeintlnWND(dataSize, 0); writeintlnWND(dataSize, 0);
{$ENDIF}
{ Store segment data for bulk memory ops } { Store segment data for bulk memory ops }
segData := TWASMPUInt8(kalloc(dataSize)); segData := TWASMPUInt8(kalloc(dataSize));
@@ -140,6 +146,7 @@ begin
bytesRead := read_leb128_to_uint32(pos, bend, @dataSize); bytesRead := read_leb128_to_uint32(pos, bend, @dataSize);
Inc(pos, bytesRead); Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Data Segment '); writestring('[wasm.parser] Data Segment ');
writeintWND(i, 0); writeintWND(i, 0);
writestring(' - Mode: 2 MemIdx: '); writestring(' - Mode: 2 MemIdx: ');
@@ -148,6 +155,7 @@ begin
writeintWND(offset, 0); writeintWND(offset, 0);
writestring(' Size: '); writestring(' Size: ');
writeintlnWND(dataSize, 0); writeintlnWND(dataSize, 0);
{$ENDIF}
{ Store segment data for bulk memory ops } { Store segment data for bulk memory ops }
segData := TWASMPUInt8(kalloc(dataSize)); segData := TWASMPUInt8(kalloc(dataSize));
@@ -3,7 +3,7 @@ unit wasm.parser.sections.elementSection;
interface interface
uses uses
wasm.types.builtin, lmemorymanager, console, leb128, wasm.types.builtin, lmemorymanager, console, wasm.types.leb128,
wasm.types.sections, wasm.types.context; wasm.types.sections, wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext); procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -23,8 +23,10 @@ var
tables: PWASMTables; tables: PWASMTables;
indices: TWASMPUInt32; indices: TWASMPUInt32;
begin begin
writestring('[wasm.parser] Handle Section: Element - Size: '); {$IFDEF DEBUG_OUTPUT}
writeintlnWND(section_length, 0); console.writestring('[wasm.parser] Handle Section: Element - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos := buffer; pos := buffer;
buf_end := buffer; buf_end := buffer;
@@ -33,8 +35,10 @@ begin
bytesRead := read_leb128_to_uint32(pos, buf_end, @seg_count); bytesRead := read_leb128_to_uint32(pos, buf_end, @seg_count);
Inc(pos, bytesRead); Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Element Segments: '); writestring('[wasm.parser] Element Segments: ');
writeintlnWND(seg_count, 0); writeintlnWND(seg_count, 0);
{$ENDIF}
tables := ctx^.ExecutionState.Tables; tables := ctx^.ExecutionState.Tables;
@@ -75,12 +79,14 @@ begin
bytesRead := read_leb128_to_uint32(pos, buf_end, @func_count); bytesRead := read_leb128_to_uint32(pos, buf_end, @func_count);
Inc(pos, bytesRead); Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Segment '); writestring('[wasm.parser] Segment ');
writeintWND(i, 0); writeintWND(i, 0);
writestring(' - Mode: 0 Offset: '); writestring(' - Mode: 0 Offset: ');
writeintWND(offset, 0); writeintWND(offset, 0);
writestring(' Functions: '); writestring(' Functions: ');
writeintlnWND(func_count, 0); writeintlnWND(func_count, 0);
{$ENDIF}
{ Store segment data } { Store segment data }
indices := TWASMPUInt32(kalloc(func_count * sizeof(TWASMUInt32))); indices := TWASMPUInt32(kalloc(func_count * sizeof(TWASMUInt32)));
@@ -112,12 +118,14 @@ begin
bytesRead := read_leb128_to_uint32(pos, buf_end, @func_count); bytesRead := read_leb128_to_uint32(pos, buf_end, @func_count);
Inc(pos, bytesRead); Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Segment '); writestring('[wasm.parser] Segment ');
writeintWND(i, 0); writeintWND(i, 0);
writestring(' - Mode: 1 (passive) Kind: '); writestring(' - Mode: 1 (passive) Kind: ');
writeintWND(elem_kind, 0); writeintWND(elem_kind, 0);
writestring(' Functions: '); writestring(' Functions: ');
writeintlnWND(func_count, 0); writeintlnWND(func_count, 0);
{$ENDIF}
indices := TWASMPUInt32(kalloc(func_count * sizeof(TWASMUInt32))); indices := TWASMPUInt32(kalloc(func_count * sizeof(TWASMUInt32)));
for j := 0 to func_count - 1 do begin for j := 0 to func_count - 1 do begin
@@ -153,6 +161,7 @@ begin
bytesRead := read_leb128_to_uint32(pos, buf_end, @func_count); bytesRead := read_leb128_to_uint32(pos, buf_end, @func_count);
Inc(pos, bytesRead); Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Segment '); writestring('[wasm.parser] Segment ');
writeintWND(i, 0); writeintWND(i, 0);
writestring(' - Mode: 2 Table: '); writestring(' - Mode: 2 Table: ');
@@ -161,6 +170,7 @@ begin
writeintWND(offset, 0); writeintWND(offset, 0);
writestring(' Functions: '); writestring(' Functions: ');
writeintlnWND(func_count, 0); writeintlnWND(func_count, 0);
{$ENDIF}
indices := TWASMPUInt32(kalloc(func_count * sizeof(TWASMUInt32))); indices := TWASMPUInt32(kalloc(func_count * sizeof(TWASMUInt32)));
for j := 0 to func_count - 1 do begin for j := 0 to func_count - 1 do begin
@@ -3,7 +3,7 @@ unit wasm.parser.sections.exportSection;
interface interface
uses uses
wasm.types.builtin, lmemorymanager, console, leb128, wasm.types.builtin, lmemorymanager, console, wasm.types.leb128,
wasm.types.enums, wasm.types.sections, wasm.types.context; wasm.types.enums, wasm.types.sections, wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext); procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -36,8 +36,10 @@ var
i,j : TWASMUInt32; i,j : TWASMUInt32;
begin begin
writestring('[wasm.parser] Handle Section: Export - Size: '); {$IFDEF DEBUG_OUTPUT}
writeintlnWND(section_length, 0); console.writestring('[wasm.parser] Handle Section: Export - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
// Initialize the read/end pointers // Initialize the read/end pointers
pos:= buffer; pos:= buffer;
@@ -85,8 +87,9 @@ begin
inc(pos, bytesRead); inc(pos, bytesRead);
end; end;
end; end;
{$IFDEF DEBUG_OUTPUT}
walk(ctx); walk(ctx);
{$ENDIF}
end; end;
end. end.
@@ -3,7 +3,7 @@ unit wasm.parser.sections.functionSection;
interface interface
uses uses
wasm.types.builtin, lmemorymanager, console, leb128, wasm.types.builtin, lmemorymanager, console, wasm.types.leb128,
wasm.types.sections, wasm.types.context; wasm.types.sections, wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext); procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -32,8 +32,10 @@ var
i : TWASMUInt32; i : TWASMUInt32;
begin begin
writestring('[wasm.parser] Handle Section: Function - Size: '); {$IFDEF DEBUG_OUTPUT}
writeintlnWND(section_length, 0); console.writestring('[wasm.parser] Handle Section: Function - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
// Initialize the read/end pointers // Initialize the read/end pointers
pos:= buffer; pos:= buffer;
@@ -64,8 +66,9 @@ begin
inc(pos, bytesRead); inc(pos, bytesRead);
end; end;
end; end;
{$IFDEF DEBUG_OUTPUT}
walk(ctx); walk(ctx);
{$ENDIF}
end; end;
end. end.
@@ -3,7 +3,7 @@ unit wasm.parser.sections.globalSection;
interface interface
uses uses
wasm.types.builtin, lmemorymanager, console, leb128, wasm.types.builtin, lmemorymanager, console, wasm.types.leb128,
wasm.types.enums, wasm.types.sections, wasm.types.context; wasm.types.enums, wasm.types.sections, wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext); procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -49,9 +49,11 @@ var
tmpU64 : TWASMUInt64; tmpU64 : TWASMUInt64;
begin begin
writestring('[wasm.parser] Handle Section: Global - Size: '); {$IFDEF DEBUG_OUTPUT}
writeintlnWND(section_length, 0); console.writestring('[wasm.parser] Handle Section: Global - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos := buffer; pos := buffer;
bend := TWASMPUInt8(buffer + section_length); bend := TWASMPUInt8(buffer + section_length);
@@ -115,8 +117,9 @@ begin
if (pos < bend) and (pos^ = $0B) then if (pos < bend) and (pos^ = $0B) then
Inc(pos); Inc(pos);
end; end;
{$IFDEF DEBUG_OUTPUT}
walk(ctx); walk(ctx);
{$ENDIF}
end; end;
end. end.
@@ -3,7 +3,7 @@ unit wasm.parser.sections.importSection;
interface interface
uses uses
wasm.types.builtin, lmemorymanager, console, leb128, wasm.types.builtin, lmemorymanager, console, wasm.types.leb128,
wasm.types.enums, wasm.types.sections, wasm.types.context; wasm.types.enums, wasm.types.sections, wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext); procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -71,8 +71,10 @@ var
i, j : TWASMUInt32; i, j : TWASMUInt32;
limitsFlag : TWASMUInt8; limitsFlag : TWASMUInt8;
begin begin
writestring('[wasm.parser] Handle Section: Import - Size: '); {$IFDEF DEBUG_OUTPUT}
writeintlnWND(section_length, 0); console.writestring('[wasm.parser] Handle Section: Import - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos := buffer; pos := buffer;
bend := TWASMPUInt8(pos + section_length); bend := TWASMPUInt8(pos + section_length);
@@ -84,8 +86,10 @@ begin
bytesRead := read_leb128_to_uint32(pos, bend, @ctx^.Sections.ImportSection^.ImportCount); bytesRead := read_leb128_to_uint32(pos, bend, @ctx^.Sections.ImportSection^.ImportCount);
Inc(pos, bytesRead); Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Import Count: '); writestring('[wasm.parser] Import Count: ');
writeintlnWND(ctx^.Sections.ImportSection^.ImportCount, 0); writeintlnWND(ctx^.Sections.ImportSection^.ImportCount, 0);
{$ENDIF}
if ctx^.Sections.ImportSection^.ImportCount > 0 then begin if ctx^.Sections.ImportSection^.ImportCount > 0 then begin
{ Allocate entries } { Allocate entries }
@@ -165,8 +169,9 @@ begin
end; end;
end; end;
end; end;
{$IFDEF DEBUG_OUTPUT}
walk(ctx); walk(ctx);
{$ENDIF}
end; end;
end. end.
@@ -3,7 +3,7 @@ unit wasm.parser.sections.memorySection;
interface interface
uses uses
wasm.types.builtin, lmemorymanager, console, leb128, wasm.types.builtin, lmemorymanager, console, wasm.types.leb128,
wasm.types.sections, wasm.types.context, wasm.types.heap; wasm.types.sections, wasm.types.context, wasm.types.heap;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext); procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -39,8 +39,10 @@ var
flags : TWASMUInt32; flags : TWASMUInt32;
begin begin
writestring('[wasm.parser] Handle Section: Memory - Size: '); {$IFDEF DEBUG_OUTPUT}
writeintlnWND(section_length, 0); console.writestring('[wasm.parser] Handle Section: Memory - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos := buffer; pos := buffer;
bend := TWASMPUInt8(buffer + section_length); bend := TWASMPUInt8(buffer + section_length);
@@ -77,8 +79,9 @@ begin
while ctx^.ExecutionState.Memory^.PageCount < mem^.InitialPages do while ctx^.ExecutionState.Memory^.PageCount < mem^.InitialPages do
wasm.types.heap.expand_heap(ctx^.ExecutionState.Memory); wasm.types.heap.expand_heap(ctx^.ExecutionState.Memory);
end; end;
{$IFDEF DEBUG_OUTPUT}
walk(ctx); walk(ctx);
{$ENDIF}
end; end;
end. end.
@@ -3,7 +3,7 @@ unit wasm.parser.sections.startSection;
interface interface
uses uses
wasm.types.builtin, lmemorymanager, console, leb128, wasm.types.builtin, lmemorymanager, console, wasm.types.leb128,
wasm.types.context; wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext); procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -16,15 +16,19 @@ var
funcIdx : TWASMUInt32; funcIdx : TWASMUInt32;
begin begin
writestring('[wasm.parser] Handle Section: Start - Size: '); {$IFDEF DEBUG_OUTPUT}
writeintlnWND(section_length, 0); console.writestring('[wasm.parser] Handle Section: Start - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
{ Read the start function index } { Read the start function index }
bytesRead := read_leb128_to_uint32(buffer, TWASMPUInt8(buffer + section_length), @funcIdx); bytesRead := read_leb128_to_uint32(buffer, TWASMPUInt8(buffer + section_length), @funcIdx);
ctx^.Sections.StartIndex := TWASMInt32(funcIdx); ctx^.Sections.StartIndex := TWASMInt32(funcIdx);
writestring('[wasm.parser] Start Function Index: '); {$IFDEF DEBUG_OUTPUT}
writeintlnWND(funcIdx, 0); console.writestring('[wasm.parser] Start Function Index: ');
console.writeintlnWND(funcIdx, 0);
{$ENDIF}
end; end;
end. end.
@@ -3,7 +3,7 @@ unit wasm.parser.sections.tableSection;
interface interface
uses uses
wasm.types.builtin, lmemorymanager, console, leb128, wasm.types.builtin, lmemorymanager, console, wasm.types.leb128,
wasm.types.sections, wasm.types.context; wasm.types.sections, wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext); procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -19,8 +19,10 @@ var
elem_type, limits_flag: TWASMUInt8; elem_type, limits_flag: TWASMUInt8;
tables: PWASMTables; tables: PWASMTables;
begin begin
writestring('[wasm.parser] Handle Section: Table - Size: '); {$IFDEF DEBUG_OUTPUT}
writeintlnWND(section_length, 0); console.writestring('[wasm.parser] Handle Section: Table - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos := buffer; pos := buffer;
buf_end := buffer; buf_end := buffer;
@@ -29,8 +31,10 @@ begin
bytesRead := read_leb128_to_uint32(pos, buf_end, @table_count); bytesRead := read_leb128_to_uint32(pos, buf_end, @table_count);
Inc(pos, bytesRead); Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Tables: '); writestring('[wasm.parser] Tables: ');
writeintlnWND(table_count, 0); writeintlnWND(table_count, 0);
{$ENDIF}
tables := ctx^.ExecutionState.Tables; tables := ctx^.ExecutionState.Tables;
tables^.TableCount := table_count; tables^.TableCount := table_count;
@@ -55,17 +59,19 @@ begin
Inc(pos, bytesRead); Inc(pos, bytesRead);
end; end;
writestring('[wasm.parser] Table '); {$IFDEF DEBUG_OUTPUT}
writeintWND(i, 0); console.writestring('[wasm.parser] Table ');
writestring(' - ElemType: $'); console.writeintWND(i, 0);
writeintWND(elem_type, 0); console.writestring(' - ElemType: $');
writestring(' Initial: '); console.writeintWND(elem_type, 0);
writeintWND(min_size, 0); console.writestring(' Initial: ');
console.writeintWND(min_size, 0);
if limits_flag = 1 then begin if limits_flag = 1 then begin
writestring(' Max: '); console.writestring(' Max: ');
writeintWND(max_size, 0); console.writeintWND(max_size, 0);
end; end;
writestringln(''); console.writestringln('');
{$ENDIF}
tables^.Tables[i].ElementType := elem_type; tables^.Tables[i].ElementType := elem_type;
tables^.Tables[i].Size := min_size; tables^.Tables[i].Size := min_size;
@@ -3,7 +3,7 @@ unit wasm.parser.sections.typeSection;
interface interface
uses uses
wasm.types.builtin, lmemorymanager, console, leb128, wasm.types.builtin, lmemorymanager, console, wasm.types.leb128,
wasm.types.enums, wasm.types.values, wasm.types.sections, wasm.types.context; wasm.types.enums, wasm.types.values, wasm.types.sections, wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext); procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -59,8 +59,11 @@ var
i, parsedTypes : TWASMUInt32; i, parsedTypes : TWASMUInt32;
begin begin
writestring('[wasm.parser] Handle Section: Type - Size: ');
writeintlnWND(section_length, 0); {$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Handle Section: Type - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos:= buffer; pos:= buffer;
// Initialize the type section // Initialize the type section
@@ -118,7 +121,9 @@ begin
end; end;
// Walk the wasm.types.builtin // Walk the wasm.types.builtin
{$IFDEF DEBUG_OUTPUT}
walk(ctx); walk(ctx);
{$ENDIF}
end; end;
end. end.
Loaded 20 of 130 files, more files were not shown because too many files have changed in this diff. Show more