Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b6115c88eb | ||
|
|
152e6dc241 | ||
|
|
ac4c631a5c | ||
|
|
5bb7107997 | ||
|
|
757d762faf | ||
|
|
302dcb66f0 | ||
|
|
378f999143 | ||
|
|
c2e47ffd7a | ||
|
|
1f27b99284 | ||
|
|
0a4bd589ee | ||
|
|
eda3669ab2 | ||
|
|
a5b532d0c1 | ||
|
|
a8817e0350 | ||
|
|
b937e60838 | ||
|
|
255ad4806e | ||
|
|
93dfff358d | ||
|
|
3ed5c0a847 | ||
|
|
ff65a0d5f0 | ||
|
|
76d7d737c0 | ||
|
|
d12ad11746 | ||
|
|
679973c6e9 | ||
|
|
68df3565cb | ||
|
|
862e3254df | ||
|
|
6794c52a77 | ||
|
|
7cc4459dfd | ||
|
|
ae344e7495 | ||
|
|
430ebcd203 | ||
|
|
835f6f86cc | ||
|
|
a5192366ed | ||
|
|
d3cd71072b | ||
|
|
e40d8e9ed5 | ||
|
|
fca24af1b0 | ||
|
|
8c16550bd4 | ||
|
|
9a3b813c96 | ||
|
|
8a4bb2ca35 | ||
|
|
ff28299132 | ||
|
|
dc67262477 | ||
|
|
d34997aa4e |
No files matched your search
@@ -1,2 +1,6 @@
|
|||||||
bin
|
bin
|
||||||
lib
|
lib
|
||||||
|
AGENTS.md
|
||||||
|
GAP_ANALYSIS.md
|
||||||
|
*.pdf
|
||||||
|
doc/WASI.md
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
# build.ps1 — Build script for the WASURO project
|
||||||
|
param(
|
||||||
|
[switch]$Test,
|
||||||
|
[switch]$Clean,
|
||||||
|
[switch]$Run,
|
||||||
|
[switch]$Help,
|
||||||
|
[switch]$Debug,
|
||||||
|
[switch]$E2E
|
||||||
|
)
|
||||||
|
|
||||||
|
# Also accept positional string args (e.g. .\build.ps1 clean test run)
|
||||||
|
foreach ($a in $args) {
|
||||||
|
switch ($a.ToLower()) {
|
||||||
|
'test' { $Test = $true }
|
||||||
|
'clean' { $Clean = $true }
|
||||||
|
'run' { $Run = $true }
|
||||||
|
'debug' { $Debug = $true }
|
||||||
|
'help' { $Help = $true }
|
||||||
|
'e2e' { $E2E = $true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($Help) {
|
||||||
|
Write-Host 'Usage: .\build.ps1 [options]'
|
||||||
|
Write-Host 'Options:'
|
||||||
|
Write-Host ' test - Build with tests enabled'
|
||||||
|
Write-Host ' clean - Clean build outputs'
|
||||||
|
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'
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
$Root = $PSScriptRoot
|
||||||
|
$Src = Join-Path $Root 'src'
|
||||||
|
$Bin = Join-Path $Root 'bin'
|
||||||
|
$Lib = Join-Path $Root 'lib'
|
||||||
|
$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 ────────────────────────────────────────────────────────────
|
||||||
|
if ($Clean) {
|
||||||
|
Write-Host 'Cleaning build outputs...'
|
||||||
|
if (Test-Path $Bin) { Remove-Item "$Bin\*" -Recurse -Force }
|
||||||
|
if (Test-Path $Lib) { Remove-Item "$Lib\*" -Recurse -Force }
|
||||||
|
Write-Host 'Clean complete.'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-not (Test-Path $Bin)) { New-Item $Bin -ItemType Directory | Out-Null }
|
||||||
|
if (-not (Test-Path $Lib)) { New-Item $Lib -ItemType Directory | Out-Null }
|
||||||
|
|
||||||
|
# ── Lint ─────────────────────────────────────────────────────────────
|
||||||
|
& (Join-Path $Root 'lint.ps1') -SrcRoot $Src
|
||||||
|
if ($LASTEXITCODE -ne 0) { exit 1 }
|
||||||
|
|
||||||
|
# ── Build ────────────────────────────────────────────────────────────
|
||||||
|
$TestFlags = @()
|
||||||
|
if ($Test) { $TestFlags += '-dRUN_TESTS' }
|
||||||
|
if ($Debug) { $TestFlags += '-dDEBUG_OUTPUT' }
|
||||||
|
|
||||||
|
$fpcArgs = @(
|
||||||
|
$TestFlags
|
||||||
|
"-v0ew"
|
||||||
|
"-FE$Bin"
|
||||||
|
"-FU$Lib"
|
||||||
|
"-Fu$Src\emu"
|
||||||
|
"-Fu$Src\wasm"
|
||||||
|
"-Fu$Src\wasm\parser"
|
||||||
|
"-Fu$Src\wasm\parser\sections"
|
||||||
|
"-Fu$Src\wasm\test"
|
||||||
|
"-Fu$Src\wasm\test\opcodes"
|
||||||
|
"-Fu$Src\wasm\test\parsers"
|
||||||
|
"-Fu$Src\wasm\test\binaries"
|
||||||
|
"-Fu$Src\wasm\test\infra"
|
||||||
|
"-Fu$Src\wasm\types"
|
||||||
|
"-Fu$Src\wasm\vm"
|
||||||
|
"-Fu$Src\wasm\vm\opcodes"
|
||||||
|
"-Fu$Src\wasm\wasi"
|
||||||
|
"-Fu$Src\wasm\wasi\preview1"
|
||||||
|
"-Fu$Src\wasm\test\wasi"
|
||||||
|
'-gw3'
|
||||||
|
"-o$Bin\WASURO.exe"
|
||||||
|
"$Src\project\WASURO.lpr"
|
||||||
|
)
|
||||||
|
|
||||||
|
& $FPC @fpcArgs
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
Write-Host "Build failed with error code $LASTEXITCODE"
|
||||||
|
exit $LASTEXITCODE
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "Build succeeded: $Bin\WASURO.exe"
|
||||||
|
|
||||||
|
# ── Run ──────────────────────────────────────────────────────────────
|
||||||
|
if ($Run) {
|
||||||
|
Write-Host 'Running WASURO...'
|
||||||
|
& "$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
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
# lint.ps1 — Static lint rules for the WASURO project
|
||||||
|
# Called by build.ps1 before compilation. Exit 1 on any violation.
|
||||||
|
param(
|
||||||
|
[string]$SrcRoot = (Join-Path $PSScriptRoot 'src')
|
||||||
|
)
|
||||||
|
|
||||||
|
$fail = $false
|
||||||
|
|
||||||
|
# ── Rule: No native types in src\wasm ────────────────────────────────
|
||||||
|
# All code under src\wasm must use TWASM* aliases from wasm.types.builtin.
|
||||||
|
# Only wasm.types.builtin.pas itself may reference the raw types.
|
||||||
|
$forbiddenTypes = @(
|
||||||
|
'uint8','uint16','uint32','uint64',
|
||||||
|
'sint8','sint16',
|
||||||
|
'int32','int64',
|
||||||
|
'float','double',
|
||||||
|
'puint8','puint16','puint32','puint64',
|
||||||
|
'pfloat','pdouble','pchar',
|
||||||
|
'boolean','char','void','integer'
|
||||||
|
)
|
||||||
|
$pattern = '(?i)(?<![a-z0-9_])(' + ($forbiddenTypes -join '|') + ')(?![a-z0-9_])'
|
||||||
|
|
||||||
|
$files = Get-ChildItem (Join-Path $SrcRoot 'wasm') -Recurse -Filter '*.pas' |
|
||||||
|
Where-Object { $_.Name -ne 'wasm.types.builtin.pas' }
|
||||||
|
|
||||||
|
$ruleHits = 0
|
||||||
|
foreach ($file in $files) {
|
||||||
|
$content = [IO.File]::ReadAllText($file.FullName)
|
||||||
|
$matches = [regex]::Matches($content, $pattern)
|
||||||
|
if ($matches.Count -gt 0) {
|
||||||
|
$found = @{}
|
||||||
|
foreach ($m in $matches) { $found[$m.Value.ToLower()] = $true }
|
||||||
|
$list = ($found.Keys | Sort-Object) -join ', '
|
||||||
|
Write-Host " $($file.Name): $list"
|
||||||
|
$ruleHits++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($ruleHits -gt 0) {
|
||||||
|
Write-Host ''
|
||||||
|
Write-Host "Lint failed: $ruleHits file(s) use native types. Use TWASM* from wasm.types.builtin instead."
|
||||||
|
$fail = $true
|
||||||
|
}
|
||||||
|
|
||||||
|
# ── Add more rules here ─────────────────────────────────────────────
|
||||||
|
if ($fail) { exit 1 }
|
||||||
|
Write-Host 'Lint passed.'
|
||||||
|
exit 0
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -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.
|
|
||||||
|
|
||||||
+1
-1
@@ -36,7 +36,7 @@ type
|
|||||||
sInt8 = shortint;
|
sInt8 = shortint;
|
||||||
sInt16 = smallint;
|
sInt16 = smallint;
|
||||||
sInt32 = integer;
|
sInt32 = integer;
|
||||||
sInt64 = longint;
|
sInt64 = int64;
|
||||||
|
|
||||||
Float = Single;
|
Float = Single;
|
||||||
|
|
||||||
|
|||||||
@@ -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.
|
||||||
+74
-19
@@ -2,35 +2,90 @@ program WASURO;
|
|||||||
|
|
||||||
uses
|
uses
|
||||||
sysutils,
|
sysutils,
|
||||||
|
wasm.vm.io,
|
||||||
|
wasm.types.builtin,
|
||||||
|
wasm.types.context,
|
||||||
|
wasm,
|
||||||
|
|
||||||
console,
|
{ Emu-layer OS hooks }
|
||||||
|
wasi.emu.hooks
|
||||||
|
|
||||||
types,
|
{$IFDEF RUN_TESTS}
|
||||||
|
, wasm.test
|
||||||
|
, wasm.test.framework
|
||||||
|
{$ENDIF}
|
||||||
|
;
|
||||||
|
|
||||||
wasm.types,
|
{ Emu-layer writechar hook — forwards to RTL Write for host OS output }
|
||||||
wasm.parser,
|
procedure emu_writechar(ch : TWASMChar);
|
||||||
wasm.vm, wasm.test.binary,
|
begin
|
||||||
wasm.types.stack;
|
Write(ch);
|
||||||
|
end;
|
||||||
|
|
||||||
var
|
var
|
||||||
Context : PWASMProcessContext;
|
Context : PWASMProcessContext;
|
||||||
|
ModuleSize : Int64;
|
||||||
|
ModuleBuffer : TWASMPUInt8;
|
||||||
|
ModuleFile : File;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestringln('[main] Initializing VM');
|
{ Wire up the IO subsystem before anything else }
|
||||||
wasm.vm.init();
|
wasm.vm.io.io_set_writechar(@emu_writechar);
|
||||||
|
|
||||||
writestringln('[main] Parsing WASM Binary');
|
{$IFDEF RUN_TESTS}
|
||||||
Context:= wasm.parser.parse(@wasm.test.binary.TEST_BINARY_1[0], puint8(@wasm.test.binary.TEST_BINARY_1[0] + $26));
|
wasm.wasm_init;
|
||||||
|
wasm.test.run_all_tests;
|
||||||
|
halt(wasm.test.framework.FailedTests);
|
||||||
|
{$ELSE}
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
wasm.vm.io.writestringln('WASURO - WebAssembly Runtime in Object Pascal');
|
||||||
|
{$ENDIF}
|
||||||
|
wasm.wasm_init;
|
||||||
|
|
||||||
writestringln('[main] Running...');
|
if ParamCount > 0 then begin
|
||||||
Context^.ExecutionState.Running:= true;
|
if not FileExists(ParamStr(1)) then begin
|
||||||
while (wasm.vm.tick(Context)) do;
|
wasm.vm.io.writestringln('File not found.');
|
||||||
writestringln('[main] Finished Execution');
|
halt(1);
|
||||||
writestring('[main] Returns: ');
|
|
||||||
while Context^.ExecutionState.Operand_Stack^.Top > 0 do begin
|
|
||||||
write(wasm.types.stack.popi32(Context^.ExecutionState.Operand_Stack),', ');
|
|
||||||
end;
|
end;
|
||||||
writeln();
|
Assign(ModuleFile, ParamStr(1));
|
||||||
while true do sleep(1000);
|
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}
|
||||||
|
|
||||||
|
{ Load, configure, and run the WASM module }
|
||||||
|
Context := wasm.wasm_load(ModuleBuffer, TWASMPUInt8(ModuleBuffer + ModuleSize));
|
||||||
|
|
||||||
|
if not Context^.ValidBinary then begin
|
||||||
|
wasm.vm.io.writestringln('Error: Invalid WASM binary.');
|
||||||
|
halt(1);
|
||||||
|
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
|
||||||
|
wasm.vm.io.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
|
||||||
|
wasm.vm.io.writestringln('Usage: WASURO <module.wasm>');
|
||||||
|
halt(1);
|
||||||
|
end;
|
||||||
|
{$ENDIF}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@@ -3,16 +3,16 @@ unit wasm.parser.sections.codeSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, wasm.vm.io, wasm.types.leb128,
|
||||||
wasm.types;
|
wasm.types.enums, wasm.types.values, wasm.types.sections, wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure printCtxCode(ctx : PWASMProcessContext);
|
procedure printCtxCode(ctx : PWASMProcessContext);
|
||||||
var
|
var
|
||||||
i : uint32;
|
i : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Flat Code: ');
|
writestring('[wasm.parser] Flat Code: ');
|
||||||
@@ -25,15 +25,15 @@ end;
|
|||||||
|
|
||||||
procedure walk(ctx : PWASMProcessContext);
|
procedure walk(ctx : PWASMProcessContext);
|
||||||
var
|
var
|
||||||
localpos, pos, bend : puint8;
|
localpos, pos, bend : TWASMPUInt8;
|
||||||
bytesRead : uint8;
|
bytesRead : TWASMUInt8;
|
||||||
currentEntry : PWASMCodeEntry;
|
currentEntry : PWASMCodeEntry;
|
||||||
i,j,l : uint32;
|
i,j,l : TWASMUInt32;
|
||||||
localCount,localTemp,localIndex : uint32;
|
localCount,localTemp,localIndex : TWASMUInt32;
|
||||||
localType: TWasmValueType;
|
localType: TWasmValueType;
|
||||||
codeLength: uint32;
|
codeLength: TWASMUInt32;
|
||||||
codeOffset: uint32;
|
codeOffset: TWASMUInt32;
|
||||||
totalSize: uint32;
|
totalSize: TWASMUInt32;
|
||||||
begin
|
begin
|
||||||
// Walk the code entries
|
// Walk the code entries
|
||||||
for i:=1 to ctx^.Sections.CodeSection^.CodeCount do begin
|
for i:=1 to ctx^.Sections.CodeSection^.CodeCount do begin
|
||||||
@@ -59,25 +59,27 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
var
|
var
|
||||||
localpos, pos, bend : puint8;
|
localpos, pos, bend : TWASMPUInt8;
|
||||||
bytesRead : uint8;
|
bytesRead : TWASMUInt8;
|
||||||
currentEntry : PWASMCodeEntry;
|
currentEntry : PWASMCodeEntry;
|
||||||
i,j,l : uint32;
|
i,j,l : TWASMUInt32;
|
||||||
localCount,localTemp,localIndex : uint32;
|
localCount,localTemp,localIndex : TWASMUInt32;
|
||||||
localType: TWasmValueType;
|
localType: TWasmValueType;
|
||||||
codeLength: uint32;
|
codeLength: TWASMUInt32;
|
||||||
codeOffset: uint32;
|
codeOffset: TWASMUInt32;
|
||||||
totalSize: uint32;
|
totalSize: TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Code - Size: ');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
writeintlnWND(section_length, 0);
|
wasm.vm.io.writestring('[wasm.parser] Handle Section: Code - Size: ');
|
||||||
|
wasm.vm.io.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
// Initialize the read/end pointers
|
// Initialize the read/end pointers
|
||||||
pos:= buffer;
|
pos:= buffer;
|
||||||
bend:= puint8(pos + section_length);
|
bend:= TWASMPUInt8(pos + section_length);
|
||||||
|
|
||||||
// Initialize the code section
|
// Initialize the code section
|
||||||
ctx^.Sections.CodeSection:= PWASMCodeSection(kalloc(sizeof(TWASMCodeSection)));
|
ctx^.Sections.CodeSection:= PWASMCodeSection(kalloc(sizeof(TWASMCodeSection)));
|
||||||
@@ -158,7 +160,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
// Read the code
|
// Read the code
|
||||||
currentEntry^.Code:= puint8(kalloc(currentEntry^.CodeLength));
|
currentEntry^.Code:= TWASMPUInt8(kalloc(currentEntry^.CodeLength));
|
||||||
for j:=1 to currentEntry^.CodeLength do begin
|
for j:=1 to currentEntry^.CodeLength do begin
|
||||||
currentEntry^.Code[j - 1]:= pos^;
|
currentEntry^.Code[j - 1]:= pos^;
|
||||||
inc(pos);
|
inc(pos);
|
||||||
@@ -171,20 +173,21 @@ begin
|
|||||||
totalSize:= totalSize + currentEntry^.CodeLength;
|
totalSize:= totalSize + currentEntry^.CodeLength;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
ctx^.ExecutionState.Code:= puint8(kalloc(totalSize));
|
ctx^.ExecutionState.Code:= TWASMPUInt8(kalloc(totalSize));
|
||||||
ctx^.ExecutionState.Limit:= totalSize;
|
ctx^.ExecutionState.Limit:= totalSize;
|
||||||
localpos:= ctx^.ExecutionState.Code;
|
localpos:= ctx^.ExecutionState.Code;
|
||||||
for i:=1 to ctx^.Sections.CodeSection^.CodeCount do begin
|
for i:=1 to ctx^.Sections.CodeSection^.CodeCount do begin
|
||||||
currentEntry:= @ctx^.Sections.CodeSection^.Entries[i - 1];
|
currentEntry:= @ctx^.Sections.CodeSection^.Entries[i - 1];
|
||||||
currentEntry^.CodeIndex:= uint32(localpos - ctx^.ExecutionState.Code);
|
currentEntry^.CodeIndex:= TWASMUInt32(localpos - ctx^.ExecutionState.Code);
|
||||||
for j:=1 to currentEntry^.CodeLength do begin
|
for j:=1 to currentEntry^.CodeLength do begin
|
||||||
localpos^:= currentEntry^.Code[j - 1];
|
localpos^:= currentEntry^.Code[j - 1];
|
||||||
inc(localpos);
|
inc(localpos);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
walk(ctx);
|
walk(ctx);
|
||||||
|
{$ENDIF}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -3,17 +3,21 @@ unit wasm.parser.sections.customSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, wasm.vm.io, wasm.types.leb128,
|
||||||
wasm.types;
|
wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; 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);
|
wasm.vm.io.writestring('[wasm.parser] Handle Section: Custom - Size: ');
|
||||||
|
wasm.vm.io.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
{ Custom sections are ignored in this implementation }
|
||||||
|
wasm.vm.io.writestringln('[wasm.parser] TODO: Custom section skipped.');
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
unit wasm.parser.sections.dataCountSection;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
uses
|
||||||
|
wasm.types.builtin, wasm.types.context;
|
||||||
|
|
||||||
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses wasm.vm.io, wasm.types.leb128, lmemorymanager, wasm.types.sections;
|
||||||
|
|
||||||
|
{ DataCount section (ID 12): contains a single u32 count of data segments.
|
||||||
|
Pre-allocates the DataSegments array so that bulk memory opcodes
|
||||||
|
can reference segments by index during validation. }
|
||||||
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos, bend : TWASMPUInt8;
|
||||||
|
bytesRead : TWASMUInt8;
|
||||||
|
dataCount : TWASMUInt32;
|
||||||
|
begin
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
wasm.vm.io.writestring('[wasm.parser] Handle Section: DataCount - Size: ');
|
||||||
|
wasm.vm.io.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
|
pos := buffer;
|
||||||
|
bend := TWASMPUInt8(buffer + section_length);
|
||||||
|
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @dataCount);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
writestring('[wasm.parser] Data Count: ');
|
||||||
|
writeintlnWND(dataCount, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
|
{ Pre-allocate data segment storage with correct count.
|
||||||
|
The actual segment data will be filled when the Data section is parsed. }
|
||||||
|
ctx^.ExecutionState.DataSegments^.SegmentCount := dataCount;
|
||||||
|
if dataCount > 0 then
|
||||||
|
ctx^.ExecutionState.DataSegments^.Segments := PWASMDataSegment(kalloc(dataCount * sizeof(TWASMDataSegment)))
|
||||||
|
else
|
||||||
|
ctx^.ExecutionState.DataSegments^.Segments := nil;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -3,17 +3,17 @@ unit wasm.parser.sections.exportSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, wasm.vm.io, wasm.types.leb128,
|
||||||
wasm.types;
|
wasm.types.enums, wasm.types.sections, wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure walk(ctx : PWASMProcessContext);
|
procedure walk(ctx : PWASMProcessContext);
|
||||||
var
|
var
|
||||||
currentEntry : PWASMExportEntry;
|
currentEntry : PWASMExportEntry;
|
||||||
i : uint32;
|
i : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
// Walk the export entries
|
// Walk the export entries
|
||||||
@@ -28,20 +28,22 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
var
|
var
|
||||||
pos, bend : puint8;
|
pos, bend : TWASMPUInt8;
|
||||||
bytesRead : uint8;
|
bytesRead : TWASMUInt8;
|
||||||
currentEntry : PWASMExportEntry;
|
currentEntry : PWASMExportEntry;
|
||||||
i,j : uint32;
|
i,j : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Export - Size: ');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
writeintlnWND(section_length, 0);
|
wasm.vm.io.writestring('[wasm.parser] Handle Section: Export - Size: ');
|
||||||
|
wasm.vm.io.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
// Initialize the read/end pointers
|
// Initialize the read/end pointers
|
||||||
pos:= buffer;
|
pos:= buffer;
|
||||||
bend:= puint8(pos + section_length);
|
bend:= TWASMPUInt8(pos + section_length);
|
||||||
|
|
||||||
// Initialize the export section
|
// Initialize the export section
|
||||||
ctx^.Sections.ExportSection:= PWASMExportSection(kalloc(sizeof(TWASMExportSection)));
|
ctx^.Sections.ExportSection:= PWASMExportSection(kalloc(sizeof(TWASMExportSection)));
|
||||||
@@ -69,12 +71,12 @@ begin
|
|||||||
inc(pos, bytesRead);
|
inc(pos, bytesRead);
|
||||||
|
|
||||||
// Read the export name
|
// Read the export name
|
||||||
currentEntry^.Name:= pchar(kalloc(currentEntry^.NameLength + 1));
|
currentEntry^.Name:= TWASMPChar(kalloc(currentEntry^.NameLength + 1));
|
||||||
for j:=0 to currentEntry^.NameLength - 1 do begin
|
for j:=0 to currentEntry^.NameLength - 1 do begin
|
||||||
currentEntry^.Name[j]:= char(pos^);
|
currentEntry^.Name[j]:= TWASMChar(pos^);
|
||||||
inc(pos);
|
inc(pos);
|
||||||
end;
|
end;
|
||||||
currentEntry^.Name[currentEntry^.NameLength]:= char($00);
|
currentEntry^.Name[currentEntry^.NameLength]:= TWASMChar($00);
|
||||||
|
|
||||||
// Read the export type
|
// Read the export type
|
||||||
currentEntry^.ExportType:= TWasmExportType(pos^);
|
currentEntry^.ExportType:= TWasmExportType(pos^);
|
||||||
@@ -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,17 +3,17 @@ unit wasm.parser.sections.functionSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, wasm.vm.io, wasm.types.leb128,
|
||||||
wasm.types;
|
wasm.types.sections, wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure walk(ctx : PWASMProcessContext);
|
procedure walk(ctx : PWASMProcessContext);
|
||||||
var
|
var
|
||||||
currentFunc : PWASMFunction;
|
currentFunc : PWASMFunction;
|
||||||
i : uint32;
|
i : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
// Walk the functions
|
// Walk the functions
|
||||||
@@ -24,20 +24,22 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
var
|
var
|
||||||
pos, bend : puint8;
|
pos, bend : TWASMPUInt8;
|
||||||
bytesRead : uint8;
|
bytesRead : TWASMUInt8;
|
||||||
currentFunc : PWASMFunction;
|
currentFunc : PWASMFunction;
|
||||||
i : uint32;
|
i : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Function - Size: ');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
writeintlnWND(section_length, 0);
|
wasm.vm.io.writestring('[wasm.parser] Handle Section: Function - Size: ');
|
||||||
|
wasm.vm.io.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
// Initialize the read/end pointers
|
// Initialize the read/end pointers
|
||||||
pos:= buffer;
|
pos:= buffer;
|
||||||
bend:= puint8(pos + section_length);
|
bend:= TWASMPUInt8(pos + section_length);
|
||||||
|
|
||||||
// Initialize the function section
|
// Initialize the function section
|
||||||
ctx^.Sections.FunctionSection:= PWASMFunctionSection(kalloc(sizeof(TWASMFunctionSection)));
|
ctx^.Sections.FunctionSection:= PWASMFunctionSection(kalloc(sizeof(TWASMFunctionSection)));
|
||||||
@@ -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,17 +3,123 @@ unit wasm.parser.sections.globalSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, wasm.vm.io, wasm.types.leb128,
|
||||||
wasm.types;
|
wasm.types.enums, wasm.types.sections, wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure walk(ctx : PWASMProcessContext);
|
||||||
|
var
|
||||||
|
i : TWASMUInt32;
|
||||||
|
entry : PWASMGlobalEntry;
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Global - Size: ');
|
for i := 0 to ctx^.ExecutionState.Globals^.GlobalCount - 1 do begin
|
||||||
writeintlnWND(section_length, 0);
|
entry := @ctx^.ExecutionState.Globals^.Globals[i];
|
||||||
|
writestring('[wasm.parser] Global ');
|
||||||
|
writeintWND(i, 0);
|
||||||
|
writestring(' - Type: ');
|
||||||
|
writestring(GetWasmValueTypeString(entry^.ValueType));
|
||||||
|
if entry^.Mutable then
|
||||||
|
writestring(' (mut)')
|
||||||
|
else
|
||||||
|
writestring(' (const)');
|
||||||
|
writestring(' = ');
|
||||||
|
case entry^.ValueType of
|
||||||
|
vti32: writeintlnWND(entry^.Value.i32Value, 0);
|
||||||
|
vti64: writeintlnWND(entry^.Value.i64Value, 0);
|
||||||
|
else
|
||||||
|
writestringln('<value>');
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos, bend : TWASMPUInt8;
|
||||||
|
bytesRead : TWASMUInt8;
|
||||||
|
globalCount : TWASMUInt32;
|
||||||
|
i : TWASMUInt32;
|
||||||
|
entry : PWASMGlobalEntry;
|
||||||
|
valType : TWASMUInt8;
|
||||||
|
mutFlag : TWASMUInt8;
|
||||||
|
opcode : TWASMUInt8;
|
||||||
|
tmpU32 : TWASMUInt32;
|
||||||
|
tmpU64 : TWASMUInt64;
|
||||||
|
|
||||||
|
begin
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
wasm.vm.io.writestring('[wasm.parser] Handle Section: Global - Size: ');
|
||||||
|
wasm.vm.io.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
|
pos := buffer;
|
||||||
|
bend := TWASMPUInt8(buffer + section_length);
|
||||||
|
|
||||||
|
{ Read global count }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @globalCount);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
{ Allocate globals on ExecutionState }
|
||||||
|
ctx^.ExecutionState.Globals := PWASMGlobals(kalloc(sizeof(TWASMGlobals)));
|
||||||
|
ctx^.ExecutionState.Globals^.GlobalCount := globalCount;
|
||||||
|
ctx^.ExecutionState.Globals^.Globals := PWASMGlobalEntry(kalloc(sizeof(TWASMGlobalEntry) * globalCount));
|
||||||
|
|
||||||
|
for i := 0 to globalCount - 1 do begin
|
||||||
|
entry := @ctx^.ExecutionState.Globals^.Globals[i];
|
||||||
|
|
||||||
|
{ Read value type }
|
||||||
|
valType := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
entry^.ValueType := TWasmValueType(valType);
|
||||||
|
|
||||||
|
{ Read mutability }
|
||||||
|
mutFlag := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
entry^.Mutable := (mutFlag = 1);
|
||||||
|
|
||||||
|
{ Initialize value to zero }
|
||||||
|
entry^.Value.i64Value := 0;
|
||||||
|
entry^.Value.ValueType := entry^.ValueType;
|
||||||
|
|
||||||
|
{ Evaluate constant init expression }
|
||||||
|
opcode := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
case opcode of
|
||||||
|
$41: begin { i32.const }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @tmpU32);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
entry^.Value.i32Value := TWASMInt32(tmpU32);
|
||||||
|
end;
|
||||||
|
$42: begin { i64.const }
|
||||||
|
bytesRead := read_leb128_to_uint64(pos, bend, @tmpU64);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
entry^.Value.i64Value := TWASMInt64(tmpU64);
|
||||||
|
end;
|
||||||
|
$43: begin { f32.const }
|
||||||
|
entry^.Value.f32Value := TWASMPFloat(pos)^;
|
||||||
|
Inc(pos, 4);
|
||||||
|
end;
|
||||||
|
$44: begin { f64.const }
|
||||||
|
entry^.Value.f64Value := TWASMPDouble(pos)^;
|
||||||
|
Inc(pos, 8);
|
||||||
|
end;
|
||||||
|
else
|
||||||
|
writestring('[wasm.parser] Warning: unsupported global init opcode: ');
|
||||||
|
writehexpair(opcode);
|
||||||
|
writestringln(' ');
|
||||||
|
{ Skip to end opcode }
|
||||||
|
while (pos < bend) and (pos^ <> $0B) do Inc(pos);
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ Consume the end ($0B) opcode }
|
||||||
|
if (pos < bend) and (pos^ = $0B) then
|
||||||
|
Inc(pos);
|
||||||
|
end;
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
walk(ctx);
|
||||||
|
{$ENDIF}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -3,17 +3,85 @@ unit wasm.parser.sections.memorySection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, wasm.vm.io, wasm.types.leb128,
|
||||||
wasm.types;
|
wasm.types.sections, wasm.types.context, wasm.types.heap;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure walk(ctx : PWASMProcessContext);
|
||||||
|
var
|
||||||
|
i : TWASMUInt32;
|
||||||
|
mem : PWASMMemoryLimits;
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Memory - Size: ');
|
for i := 0 to ctx^.Sections.MemorySection^.MemoryCount - 1 do begin
|
||||||
writeintlnWND(section_length, 0);
|
mem := @ctx^.Sections.MemorySection^.Memories[i];
|
||||||
|
writestring('[wasm.parser] Memory ');
|
||||||
|
writeintWND(i, 0);
|
||||||
|
writestring(' - Initial: ');
|
||||||
|
writeintWND(mem^.InitialPages, 0);
|
||||||
|
if mem^.HasMax then begin
|
||||||
|
writestring(' Max: ');
|
||||||
|
writeintWND(mem^.MaxPages, 0);
|
||||||
|
end;
|
||||||
|
writestringln(' ');
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos, bend : TWASMPUInt8;
|
||||||
|
bytesRead : TWASMUInt8;
|
||||||
|
memCount : TWASMUInt32;
|
||||||
|
i : TWASMUInt32;
|
||||||
|
mem : PWASMMemoryLimits;
|
||||||
|
flags : TWASMUInt32;
|
||||||
|
|
||||||
|
begin
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
wasm.vm.io.writestring('[wasm.parser] Handle Section: Memory - Size: ');
|
||||||
|
wasm.vm.io.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
|
pos := buffer;
|
||||||
|
bend := TWASMPUInt8(buffer + section_length);
|
||||||
|
|
||||||
|
{ Read memory count }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @memCount);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
{ Allocate memory section }
|
||||||
|
ctx^.Sections.MemorySection := PWASMMemorySection(kalloc(sizeof(TWASMMemorySection)));
|
||||||
|
ctx^.Sections.MemorySection^.MemoryCount := memCount;
|
||||||
|
ctx^.Sections.MemorySection^.Memories := PWASMMemoryLimits(kalloc(sizeof(TWASMMemoryLimits) * memCount));
|
||||||
|
|
||||||
|
for i := 0 to memCount - 1 do begin
|
||||||
|
mem := @ctx^.Sections.MemorySection^.Memories[i];
|
||||||
|
|
||||||
|
{ Read limits flags: 0 = no max, 1 = has max }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @flags);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
mem^.HasMax := (flags = 1);
|
||||||
|
|
||||||
|
{ Read initial page count }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @mem^.InitialPages);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
{ Read max page count if present }
|
||||||
|
if mem^.HasMax then begin
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @mem^.MaxPages);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
end else
|
||||||
|
mem^.MaxPages := $FFFF; { no limit }
|
||||||
|
|
||||||
|
{ Expand heap to match initial pages (heap starts with 1 page) }
|
||||||
|
while ctx^.ExecutionState.Memory^.PageCount < mem^.InitialPages do
|
||||||
|
wasm.types.heap.expand_heap(ctx^.ExecutionState.Memory);
|
||||||
|
end;
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
walk(ctx);
|
||||||
|
{$ENDIF}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -3,17 +3,32 @@ unit wasm.parser.sections.startSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, wasm.vm.io, wasm.types.leb128,
|
||||||
wasm.types;
|
wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
bytesRead : TWASMUInt8;
|
||||||
|
funcIdx : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Start - Size: ');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
writeintlnWND(section_length, 0);
|
wasm.vm.io.writestring('[wasm.parser] Handle Section: Start - Size: ');
|
||||||
|
wasm.vm.io.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
|
{ Read the start function index }
|
||||||
|
bytesRead := read_leb128_to_uint32(buffer, TWASMPUInt8(buffer + section_length), @funcIdx);
|
||||||
|
ctx^.Sections.StartIndex := TWASMInt32(funcIdx);
|
||||||
|
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
wasm.vm.io.writestring('[wasm.parser] Start Function Index: ');
|
||||||
|
wasm.vm.io.writeintlnWND(funcIdx, 0);
|
||||||
|
{$ENDIF}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
Loaded 20 of 505 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user