Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a5b532d0c1 | ||
|
|
a8817e0350 | ||
|
|
b937e60838 | ||
|
|
255ad4806e | ||
|
|
93dfff358d | ||
|
|
3ed5c0a847 | ||
|
|
ff65a0d5f0 | ||
|
|
76d7d737c0 | ||
|
|
d12ad11746 | ||
|
|
679973c6e9 |
No files matched your search
@@ -1,3 +1,4 @@
|
||||
bin
|
||||
lib
|
||||
AGENTS.md
|
||||
GAP_ANALYSIS.md
|
||||
@@ -1,44 +0,0 @@
|
||||
@echo off
|
||||
setlocal
|
||||
|
||||
set ROOT=%~dp0
|
||||
set SRC=%ROOT%src
|
||||
set BIN=%ROOT%bin
|
||||
set LIB=%ROOT%lib
|
||||
set FPC=C:\Lazarus\fpc\3.2.2\bin\x86_64-win64\fpc.exe
|
||||
|
||||
set TESTFLAGS=
|
||||
if "%1"=="test" set TESTFLAGS=-dRUN_TESTS
|
||||
|
||||
if not exist "%BIN%" mkdir "%BIN%"
|
||||
if not exist "%LIB%" mkdir "%LIB%"
|
||||
|
||||
REM ── Lint ────────────────────────────────────────────────────────────
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -File "%ROOT%lint.ps1" -SrcRoot "%SRC%"
|
||||
if %ERRORLEVEL% NEQ 0 exit /b 1
|
||||
|
||||
"%FPC%" ^
|
||||
%TESTFLAGS% ^
|
||||
-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" ^
|
||||
-gw3 ^
|
||||
-o"%BIN%\WASURO.exe" ^
|
||||
"%SRC%\project\WASURO.lpr"
|
||||
|
||||
if %ERRORLEVEL% NEQ 0 (
|
||||
echo Build failed with error code %ERRORLEVEL%
|
||||
exit /b %ERRORLEVEL%
|
||||
)
|
||||
|
||||
echo Build succeeded: %BIN%\WASURO.exe
|
||||
@@ -0,0 +1,88 @@
|
||||
# build.ps1 — Build script for the WASURO project
|
||||
param(
|
||||
[switch]$Test,
|
||||
[switch]$Clean,
|
||||
[switch]$Run,
|
||||
[switch]$Help
|
||||
)
|
||||
|
||||
# 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 }
|
||||
'help' { $Help = $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 ' 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'
|
||||
|
||||
# ── 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 -and -not $Run) { exit 0 }
|
||||
}
|
||||
|
||||
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') }
|
||||
|
||||
$fpcArgs = @(
|
||||
$TestFlags
|
||||
"-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"
|
||||
'-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"
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
# lint.ps1 — Static lint rules for the WASURO project
|
||||
# Called by build.bat before compilation. Exit 1 on any violation.
|
||||
# Called by build.ps1 before compilation. Exit 1 on any violation.
|
||||
param(
|
||||
[string]$SrcRoot = (Join-Path $PSScriptRoot 'src')
|
||||
)
|
||||
@@ -43,6 +43,5 @@ if ($ruleHits -gt 0) {
|
||||
}
|
||||
|
||||
# ── Add more rules here ─────────────────────────────────────────────
|
||||
|
||||
if ($fail) { exit 1 }
|
||||
Write-Host 'Lint passed.'
|
||||
@@ -0,0 +1,44 @@
|
||||
unit wasm.parser.sections.dataCountSection;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
wasm.types.builtin, wasm.types.context;
|
||||
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
|
||||
implementation
|
||||
|
||||
uses console, 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
|
||||
writestring('[wasm.parser] Handle Section: DataCount - Size: ');
|
||||
writeintlnWND(section_length, 0);
|
||||
|
||||
pos := buffer;
|
||||
bend := TWASMPUInt8(buffer + section_length);
|
||||
|
||||
bytesRead := read_leb128_to_uint32(pos, bend, @dataCount);
|
||||
Inc(pos, bytesRead);
|
||||
|
||||
writestring('[wasm.parser] Data Count: ');
|
||||
writeintlnWND(dataCount, 0);
|
||||
|
||||
{ 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
File diff suppressed because it is too large.
Load diff
@@ -4,16 +4,85 @@ interface
|
||||
|
||||
uses
|
||||
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||
wasm.types.context;
|
||||
wasm.types.sections, wasm.types.context;
|
||||
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
|
||||
implementation
|
||||
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
var
|
||||
pos: TWASMPUInt8;
|
||||
buf_end: TWASMPUInt8;
|
||||
bytesRead: TWASMUInt8;
|
||||
table_count, i, j, min_size, max_size: TWASMUInt32;
|
||||
elem_type, limits_flag: TWASMUInt8;
|
||||
tables: PWASMTables;
|
||||
begin
|
||||
writestring('[wasm.parser] Handle Section: Table - Size: ');
|
||||
writeintlnWND(section_length, 0);
|
||||
|
||||
pos := buffer;
|
||||
buf_end := buffer;
|
||||
Inc(buf_end, section_length);
|
||||
|
||||
bytesRead := read_leb128_to_uint32(pos, buf_end, @table_count);
|
||||
Inc(pos, bytesRead);
|
||||
|
||||
writestring('[wasm.parser] Tables: ');
|
||||
writeintlnWND(table_count, 0);
|
||||
|
||||
tables := ctx^.ExecutionState.Tables;
|
||||
tables^.TableCount := table_count;
|
||||
if table_count > 0 then
|
||||
tables^.Tables := PWASMTableInstance(kalloc(sizeof(TWASMTableInstance) * table_count))
|
||||
else
|
||||
tables^.Tables := nil;
|
||||
|
||||
for i := 0 to table_count - 1 do begin
|
||||
elem_type := pos^;
|
||||
Inc(pos);
|
||||
|
||||
limits_flag := pos^;
|
||||
Inc(pos);
|
||||
|
||||
bytesRead := read_leb128_to_uint32(pos, buf_end, @min_size);
|
||||
Inc(pos, bytesRead);
|
||||
|
||||
max_size := 0;
|
||||
if limits_flag = 1 then begin
|
||||
bytesRead := read_leb128_to_uint32(pos, buf_end, @max_size);
|
||||
Inc(pos, bytesRead);
|
||||
end;
|
||||
|
||||
writestring('[wasm.parser] Table ');
|
||||
writeintWND(i, 0);
|
||||
writestring(' - ElemType: $');
|
||||
writeintWND(elem_type, 0);
|
||||
writestring(' Initial: ');
|
||||
writeintWND(min_size, 0);
|
||||
if limits_flag = 1 then begin
|
||||
writestring(' Max: ');
|
||||
writeintWND(max_size, 0);
|
||||
end;
|
||||
writestringln('');
|
||||
|
||||
tables^.Tables[i].ElementType := elem_type;
|
||||
tables^.Tables[i].Size := min_size;
|
||||
tables^.Tables[i].HasMax := (limits_flag = 1);
|
||||
if limits_flag = 1 then
|
||||
tables^.Tables[i].MaxSize := max_size
|
||||
else
|
||||
tables^.Tables[i].MaxSize := 0;
|
||||
|
||||
{ Allocate elements and initialize to $FFFFFFFF (uninitialized) }
|
||||
if min_size > 0 then begin
|
||||
tables^.Tables[i].Elements := TWASMPUInt32(kalloc(sizeof(TWASMUInt32) * min_size));
|
||||
for j := 0 to min_size - 1 do
|
||||
tables^.Tables[i].Elements[j] := $FFFFFFFF;
|
||||
end else
|
||||
tables^.Tables[i].Elements := nil;
|
||||
end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -5,7 +5,7 @@ interface
|
||||
uses
|
||||
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||
wasm.types.enums, wasm.types.sections, wasm.types.context, wasm.types.heap, wasm.types.stack,
|
||||
wasm.parser.sections;
|
||||
wasm.parser.sections, wasm.types.constants;
|
||||
|
||||
function parse(buffer: TWASMPUInt8; buffer_end: TWASMPUInt8) : PWASMProcessContext;
|
||||
|
||||
@@ -36,6 +36,7 @@ begin
|
||||
|
||||
// Initialize the sections
|
||||
ctx^.Sections.TypeSection:= nil;
|
||||
ctx^.Sections.ImportSection:= nil;
|
||||
ctx^.Sections.FunctionSection:= nil;
|
||||
ctx^.Sections.ExportSection:= nil;
|
||||
ctx^.Sections.CodeSection:= nil;
|
||||
@@ -47,6 +48,21 @@ begin
|
||||
ctx^.ExecutionState.Globals^.GlobalCount:= 0;
|
||||
ctx^.ExecutionState.Globals^.Globals:= nil;
|
||||
|
||||
// Initialize tables (empty until table section is parsed)
|
||||
ctx^.ExecutionState.Tables:= PWASMTables(kalloc(sizeof(TWASMTables)));
|
||||
ctx^.ExecutionState.Tables^.TableCount:= 0;
|
||||
ctx^.ExecutionState.Tables^.Tables:= nil;
|
||||
|
||||
// Initialize data segments (empty until data section is parsed)
|
||||
ctx^.ExecutionState.DataSegments:= PWASMDataSegments(kalloc(sizeof(TWASMDataSegments)));
|
||||
ctx^.ExecutionState.DataSegments^.SegmentCount:= 0;
|
||||
ctx^.ExecutionState.DataSegments^.Segments:= nil;
|
||||
|
||||
// Initialize element segments (empty until element section is parsed)
|
||||
ctx^.ExecutionState.ElementSegments:= PWASMElementSegments(kalloc(sizeof(TWASMElementSegments)));
|
||||
ctx^.ExecutionState.ElementSegments^.SegmentCount:= 0;
|
||||
ctx^.ExecutionState.ElementSegments^.Segments:= nil;
|
||||
|
||||
newContext:= ctx;
|
||||
end;
|
||||
|
||||
@@ -66,7 +82,7 @@ begin
|
||||
writestringln('[wasm.parser] Parsing WASM Binary');
|
||||
|
||||
// Check for the WASM Magic
|
||||
if(TWASMPUInt32(pos)^ = WASM_HDR_MAGIC) then begin
|
||||
if(TWASMPUInt32(pos)^ = wasm.types.constants.WASM_HDR_MAGIC) then begin
|
||||
writestringln('[wasm.parser] Binary is valid.');
|
||||
|
||||
// Set binary to valid
|
||||
|
||||
@@ -16,7 +16,8 @@ uses
|
||||
wasm.parser.sections.startSection,
|
||||
wasm.parser.sections.elementSection,
|
||||
wasm.parser.sections.codeSection,
|
||||
wasm.parser.sections.dataSection;
|
||||
wasm.parser.sections.dataSection,
|
||||
wasm.parser.sections.dataCountSection;
|
||||
|
||||
procedure handle(sectionId : TWASMUInt8; buffer : TWASMPUInt8; section_length : TWASMUInt32; ctx : PWASMProcessContext);
|
||||
|
||||
@@ -50,6 +51,8 @@ begin
|
||||
ord(sidCode): wasm.parser.sections.codeSection.handle (buffer, section_length, ctx);
|
||||
// Data Section
|
||||
ord(sidData): wasm.parser.sections.dataSection.handle (buffer, section_length, ctx);
|
||||
// DataCount Section
|
||||
ord(sidDataCount): wasm.parser.sections.dataCountSection.handle (buffer, section_length, ctx);
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
unit wasm.parser.validate;
|
||||
|
||||
interface
|
||||
|
||||
uses wasm.types.builtin, wasm.types.context;
|
||||
|
||||
{ Validates a parsed WASM module for structural consistency.
|
||||
Returns true if valid, false otherwise.
|
||||
Sets errorMsg to a description of the first error found. }
|
||||
function validate_module(ctx : PWASMProcessContext; var errorMsg : TWASMPChar) : TWASMBoolean;
|
||||
|
||||
implementation
|
||||
|
||||
uses console;
|
||||
|
||||
function validate_module(ctx : PWASMProcessContext; var errorMsg : TWASMPChar) : TWASMBoolean;
|
||||
var i : TWASMUInt32;
|
||||
begin
|
||||
validate_module := true;
|
||||
|
||||
{ Check 1: Binary must be valid (magic number was correct) }
|
||||
if not ctx^.ValidBinary then begin
|
||||
errorMsg := 'Invalid WASM binary: bad magic number';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
|
||||
{ Check 2: Version must be 1 (MVP) }
|
||||
if ctx^.Version <> 1 then begin
|
||||
errorMsg := 'Unsupported WASM version (expected 1)';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
|
||||
{ Check 3: If function section exists, code section must exist and counts must match }
|
||||
if (ctx^.Sections.FunctionSection <> nil) then begin
|
||||
if (ctx^.Sections.CodeSection = nil) then begin
|
||||
errorMsg := 'Function section present but code section missing';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
if ctx^.Sections.FunctionSection^.FunctionCount <> ctx^.Sections.CodeSection^.CodeCount then begin
|
||||
errorMsg := 'Function count does not match code count';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
|
||||
{ Check 4: If code section exists, function section must exist }
|
||||
if (ctx^.Sections.CodeSection <> nil) then begin
|
||||
if (ctx^.Sections.FunctionSection = nil) then begin
|
||||
errorMsg := 'Code section present but function section missing';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
|
||||
{ Check 5: Function type indices must be within range of type section }
|
||||
if (ctx^.Sections.FunctionSection <> nil) and (ctx^.Sections.TypeSection <> nil) then begin
|
||||
for i := 0 to ctx^.Sections.FunctionSection^.FunctionCount - 1 do begin
|
||||
if ctx^.Sections.FunctionSection^.Functions[i].Index >= ctx^.Sections.TypeSection^.TypeCount then begin
|
||||
errorMsg := 'Function type index out of range';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
{ Check 6: Start function index must be within range }
|
||||
if ctx^.Sections.StartIndex >= 0 then begin
|
||||
if (ctx^.Sections.FunctionSection = nil) then begin
|
||||
errorMsg := 'Start function specified but no function section';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
if TWASMUInt32(ctx^.Sections.StartIndex) >= ctx^.Sections.FunctionSection^.FunctionCount then begin
|
||||
errorMsg := 'Start function index out of range';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
|
||||
errorMsg := nil;
|
||||
end;
|
||||
|
||||
end.
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,61 @@
|
||||
unit wasm.test.opcode.block;
|
||||
|
||||
interface
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
wasm.types.builtin, wasm.types.context, wasm.types.stack, wasm.vm, wasm.test.framework;
|
||||
|
||||
procedure run;
|
||||
var
|
||||
code : array[0..15] of TWASMUInt8;
|
||||
ctx : PWASMProcessContext;
|
||||
begin
|
||||
test_begin('opcode.block');
|
||||
|
||||
{ Test 1: simple empty block falls through }
|
||||
{ block $40 ... nop ... end }
|
||||
code[0] := $02; { block }
|
||||
code[1] := $40; { blocktype $40 }
|
||||
code[2] := $01; { nop }
|
||||
code[3] := $0B; { end }
|
||||
ctx := make_test_context(@code[0], 4);
|
||||
while wasm.vm.tick(ctx) do;
|
||||
assert_true('empty block completes', ctx^.ExecutionState.Running = false);
|
||||
assert_u32('empty block IP at end', ctx^.ExecutionState.IP, 4);
|
||||
|
||||
{ Test 2: block with value on stack }
|
||||
{ block $40 i32.const 42 end }
|
||||
code[0] := $02; { block }
|
||||
code[1] := $40; { blocktype $40 }
|
||||
code[2] := $41; { i32.const }
|
||||
code[3] := $2A; { 42 }
|
||||
code[4] := $0B; { end }
|
||||
ctx := make_test_context(@code[0], 5);
|
||||
while wasm.vm.tick(ctx) do;
|
||||
assert_i32('block preserves stack value', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 42);
|
||||
|
||||
{ Test 3: nested blocks }
|
||||
{ block $40 block $40 i32.const 7 end i32.const 3 i32.add end }
|
||||
code[0] := $02; { outer block }
|
||||
code[1] := $40; { blocktype $40 }
|
||||
code[2] := $02; { inner block }
|
||||
code[3] := $40;
|
||||
code[4] := $41; { i32.const 7 }
|
||||
code[5] := $07;
|
||||
code[6] := $0B; { end inner }
|
||||
code[7] := $41; { i32.const 3 }
|
||||
code[8] := $03;
|
||||
code[9] := $6A; { i32.add }
|
||||
code[10] := $0B; { end outer }
|
||||
ctx := make_test_context(@code[0], 11);
|
||||
while wasm.vm.tick(ctx) do;
|
||||
assert_i32('nested blocks result', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 10);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -0,0 +1,85 @@
|
||||
unit wasm.test.opcode.br;
|
||||
|
||||
interface
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
wasm.types.builtin, wasm.types.enums, wasm.types.context, wasm.types.stack, wasm.vm, wasm.test.framework;
|
||||
|
||||
procedure run;
|
||||
var
|
||||
code : array[0..20] of TWASMUInt8;
|
||||
ctx : PWASMProcessContext;
|
||||
begin
|
||||
test_begin('opcode.br');
|
||||
|
||||
{ Test 1: br 0 exits innermost block }
|
||||
{ block $40 i32.const 42 br 0 i32.const 99 end }
|
||||
code[0] := $02; { block }
|
||||
code[1] := $40;
|
||||
code[2] := $41; { i32.const 42 }
|
||||
code[3] := $2A;
|
||||
code[4] := $0C; { br 0 }
|
||||
code[5] := $00;
|
||||
code[6] := $41; { i32.const 99 (unreachable) }
|
||||
code[7] := $63;
|
||||
code[8] := $0B; { end }
|
||||
ctx := make_test_context(@code[0], 9);
|
||||
while wasm.vm.tick(ctx) do;
|
||||
assert_i32('br 0 exits block with value', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 42);
|
||||
|
||||
{ Test 2: br 1 exits outer block from nested block }
|
||||
{ block $40 block $40 i32.const 77 br 1 end i32.const 99 end }
|
||||
code[0] := $02; { outer block }
|
||||
code[1] := $40;
|
||||
code[2] := $02; { inner block }
|
||||
code[3] := $40;
|
||||
code[4] := $41; { i32.const 77 }
|
||||
code[5] := $4D;
|
||||
code[6] := $0C; { br 1 (exit outer) }
|
||||
code[7] := $01;
|
||||
code[8] := $0B; { end inner }
|
||||
code[9] := $41; { i32.const 99 (unreachable) }
|
||||
code[10] := $63;
|
||||
code[11] := $0B; { end outer }
|
||||
ctx := make_test_context(@code[0], 12);
|
||||
while wasm.vm.tick(ctx) do;
|
||||
assert_i32('br 1 exits outer block', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 77);
|
||||
|
||||
{ Test 3: br 0 in loop re-enters loop (tested via local counter) }
|
||||
{ i32.const 2 local.set 0
|
||||
loop $40
|
||||
local.get 0 i32.const 1 i32.sub local.tee 0
|
||||
br_if 0
|
||||
end
|
||||
local.get 0 }
|
||||
code[0] := $41; { i32.const 2 }
|
||||
code[1] := $02;
|
||||
code[2] := $21; { local.set 0 }
|
||||
code[3] := $00;
|
||||
code[4] := $03; { loop }
|
||||
code[5] := $40;
|
||||
code[6] := $20; { local.get 0 }
|
||||
code[7] := $00;
|
||||
code[8] := $41; { i32.const 1 }
|
||||
code[9] := $01;
|
||||
code[10] := $6B; { i32.sub }
|
||||
code[11] := $22; { local.tee 0 }
|
||||
code[12] := $00;
|
||||
code[13] := $0D; { br_if 0 }
|
||||
code[14] := $00;
|
||||
code[15] := $0B; { end loop }
|
||||
code[16] := $20; { local.get 0 }
|
||||
code[17] := $00;
|
||||
ctx := make_test_context(@code[0], 18);
|
||||
setup_test_locals(ctx, 1, vti32);
|
||||
while wasm.vm.tick(ctx) do;
|
||||
assert_i32('br loop countdown result', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 0);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -0,0 +1,57 @@
|
||||
unit wasm.test.opcode.brif;
|
||||
|
||||
interface
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
wasm.types.builtin, wasm.types.context, wasm.types.stack, wasm.vm, wasm.test.framework;
|
||||
|
||||
procedure run;
|
||||
var
|
||||
code : array[0..15] of TWASMUInt8;
|
||||
ctx : PWASMProcessContext;
|
||||
begin
|
||||
test_begin('opcode.br_if');
|
||||
|
||||
{ Test 1: br_if with true condition exits block }
|
||||
{ block $40 i32.const 42 i32.const 1 br_if 0 i32.const 99 end }
|
||||
code[0] := $02; { block }
|
||||
code[1] := $40;
|
||||
code[2] := $41; { i32.const 42 }
|
||||
code[3] := $2A;
|
||||
code[4] := $41; { i32.const 1 (condition: true) }
|
||||
code[5] := $01;
|
||||
code[6] := $0D; { br_if 0 }
|
||||
code[7] := $00;
|
||||
code[8] := $41; { i32.const 99 (unreachable) }
|
||||
code[9] := $63;
|
||||
code[10] := $0B; { end }
|
||||
ctx := make_test_context(@code[0], 11);
|
||||
while wasm.vm.tick(ctx) do;
|
||||
assert_i32('br_if true exits block', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 42);
|
||||
|
||||
{ Test 2: br_if with false condition continues }
|
||||
{ block $40 i32.const 42 i32.const 0 br_if 0 drop i32.const 7 end }
|
||||
code[0] := $02; { block }
|
||||
code[1] := $40;
|
||||
code[2] := $41; { i32.const 42 }
|
||||
code[3] := $2A;
|
||||
code[4] := $41; { i32.const 0 (condition: false) }
|
||||
code[5] := $00;
|
||||
code[6] := $0D; { br_if 0 }
|
||||
code[7] := $00;
|
||||
code[8] := $1A; { drop (remove 42) }
|
||||
code[9] := $41; { i32.const 7 }
|
||||
code[10] := $07;
|
||||
code[11] := $0B; { end }
|
||||
ctx := make_test_context(@code[0], 12);
|
||||
while wasm.vm.tick(ctx) do;
|
||||
assert_i32('br_if false continues execution', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 7);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -0,0 +1,80 @@
|
||||
unit wasm.test.opcode.brtable;
|
||||
|
||||
interface
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
wasm.types.builtin, wasm.types.context, wasm.types.stack, wasm.vm, wasm.test.framework;
|
||||
|
||||
procedure run;
|
||||
var
|
||||
code : array[0..31] of TWASMUInt8;
|
||||
ctx : PWASMProcessContext;
|
||||
begin
|
||||
test_begin('opcode.br_table');
|
||||
|
||||
{ br_table with 2 labels + default, targeting nested blocks.
|
||||
Layout:
|
||||
block $40 ;; label 2
|
||||
block $40 ;; label 1
|
||||
block $40 ;; label 0
|
||||
i32.const <idx>
|
||||
br_table 2 0 1 2 ;; count=2, labels=[0,1], default=2
|
||||
end
|
||||
i32.const 10 ;; label 0 lands here
|
||||
return
|
||||
end
|
||||
i32.const 20 ;; label 1 lands here
|
||||
return
|
||||
end
|
||||
i32.const 30 ;; label 2 lands here
|
||||
}
|
||||
|
||||
{ Test 1: br_table index 0 selects label 0 }
|
||||
code[0] := $02; { block (label 2) }
|
||||
code[1] := $40;
|
||||
code[2] := $02; { block (label 1) }
|
||||
code[3] := $40;
|
||||
code[4] := $02; { block (label 0) }
|
||||
code[5] := $40;
|
||||
code[6] := $41; { i32.const 0 }
|
||||
code[7] := $00;
|
||||
code[8] := $0E; { br_table }
|
||||
code[9] := $02; { count = 2 labels }
|
||||
code[10] := $00; { label[0] = depth 0 }
|
||||
code[11] := $01; { label[1] = depth 1 }
|
||||
code[12] := $02; { default = depth 2 }
|
||||
code[13] := $0B; { end (label 0) }
|
||||
code[14] := $41; { i32.const 10 }
|
||||
code[15] := $0A;
|
||||
code[16] := $0F; { return }
|
||||
code[17] := $0B; { end (label 1) }
|
||||
code[18] := $41; { i32.const 20 }
|
||||
code[19] := $14;
|
||||
code[20] := $0F; { return }
|
||||
code[21] := $0B; { end (label 2) }
|
||||
code[22] := $41; { i32.const 30 }
|
||||
code[23] := $1E;
|
||||
ctx := make_test_context(@code[0], 24);
|
||||
while wasm.vm.tick(ctx) do;
|
||||
assert_i32('br_table idx 0 selects label 0', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 10);
|
||||
|
||||
{ Test 2: br_table index 1 selects label 1 }
|
||||
code[7] := $01; { change index to 1 }
|
||||
ctx := make_test_context(@code[0], 24);
|
||||
while wasm.vm.tick(ctx) do;
|
||||
assert_i32('br_table idx 1 selects label 1', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 20);
|
||||
|
||||
{ Test 3: br_table index out of range selects default }
|
||||
code[7] := $05; { index 5, out of range -> default (depth 2) }
|
||||
ctx := make_test_context(@code[0], 24);
|
||||
while wasm.vm.tick(ctx) do;
|
||||
assert_i32('br_table default for out-of-range', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 30);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,45 @@
|
||||
unit wasm.test.opcode.datadrop;
|
||||
|
||||
interface
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
wasm.types.builtin, wasm.types.sections, wasm.types.context,
|
||||
wasm.vm, wasm.test.framework, lmemorymanager;
|
||||
|
||||
procedure run;
|
||||
var
|
||||
{ FC $09, data_idx=0 }
|
||||
code : array[0..2] of TWASMUInt8;
|
||||
ctx : PWASMProcessContext;
|
||||
segData : array[0..1] of TWASMUInt8;
|
||||
begin
|
||||
test_begin('opcode.data.drop');
|
||||
|
||||
code[0] := $FC; code[1] := $09; code[2] := $00;
|
||||
|
||||
{ Drop a data segment }
|
||||
segData[0] := $11; segData[1] := $22;
|
||||
ctx := make_test_context(@code[0], 3);
|
||||
ctx^.ExecutionState.DataSegments^.SegmentCount := 1;
|
||||
ctx^.ExecutionState.DataSegments^.Segments := PWASMDataSegment(kalloc(sizeof(TWASMDataSegment)));
|
||||
ctx^.ExecutionState.DataSegments^.Segments[0].Data := @segData[0];
|
||||
ctx^.ExecutionState.DataSegments^.Segments[0].Size := 2;
|
||||
ctx^.ExecutionState.DataSegments^.Segments[0].Dropped := false;
|
||||
wasm.vm.tick(ctx);
|
||||
assert_true('segment dropped', ctx^.ExecutionState.DataSegments^.Segments[0].Dropped);
|
||||
assert_true('still running', ctx^.ExecutionState.Running);
|
||||
|
||||
{ Drop invalid index traps }
|
||||
ctx := make_test_context(@code[0], 3);
|
||||
{ SegmentCount = 0 (default), so data_idx=0 is out of range }
|
||||
wasm.vm.tick(ctx);
|
||||
assert_true('invalid_idx traps', not ctx^.ExecutionState.Running);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
Loaded 20 of 335 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user