Compare commits

..
Author SHA1 Message Date
t3hn3rd b937e60838 feature: implement all float, conversion, table & bulk memory opcodes with IEEE 754 hardening
Complete implementation of remaining WASM MVP and post-MVP opcodes:

Float arithmetic (f32/f64):
- abs, neg (purely bitwise), ceil, floor, trunc, nearest, sqrt
- add, sub, mul, div with indeterminate form handling
  (Inf+(-Inf), Inf-Inf, Inf*0, 0/0, Inf/Inf -> NaN; x/0 -> ±Inf)
- min, max with signed-zero fix (OR for min, AND for max)
- copysign (bitwise sign transfer)

Float comparisons (f64 eq/ne/lt/gt/le/ge):
- Added bitwise NaN detection before comparisons to prevent EInvalidOp on x64 FPC

Conversion opcodes:
- i32.wrap_i64, i64.extend_i32_s/u
- i32/i64.trunc_f32/f64_s/u with NaN + overflow traps
- f32/f64.convert_i32/i64_s/u
- f32.demote_f64, f64.promote_f32
- i32/i64.reinterpret_f32/f64, f32/f64.reinterpret_i32/i64
- i32.extend8_s, i32.extend16_s, i64.extend8_s/16_s/32_s

0xFC prefix dispatch + saturating truncation (FC 00-07):
- i32/i64.trunc_sat_f32/f64_s/u with NaN->0, clamp to min/max

Bulk memory (FC 08-0B):
- memory.init, data.drop, memory.copy (overlap-safe), memory.fill

Table operations (FC 0C-11 + opcodes 0x25/0x26):
- table.get, table.set, table.init, elem.drop
- table.copy (overlap-safe), table.grow, table.size, table.fill

Control flow & infrastructure:
- select t* (typed select, 0x1C) with LEB128 type vector
- block/loop/if blocktype now parsed as LEB128 s33 (multi-byte support)
- Control flow scanner updated for all new opcodes (0xFC sub-opcodes,
  table.get/set, typed select)
- VM init: jump tables heap-allocated via kalloc, FC table initialized

All float ops use bitwise NaN detection to avoid FPC x64 EInvalidOp.
2026-02-24 21:45:10 +00:00
t3hn3rd 255ad4806e Merge branch 'feature/opcode-refactor' into develop 2026-02-24 15:38:48 +00:00
t3hn3rd 93dfff358d refactor: Split monolithic wasm.vm.opcodes.pas into 119 individual opcode units
- Extract each opcode procedure from wasm.vm.opcodes.pas into its own
unit file under src/wasm/vm/opcodes/ following the naming convention
wasm.vm.opcode.<name>.pas. The hub file now only imports the individual
units and contains initializeOpcodeJumpTable with fully qualified
procedure references.
- Add missing test coverage for the else opcode (4 tests), bringing the
total from 425 to 429 with all tests passing.

Files:
- 119 new opcode units under src/wasm/vm/opcodes/
- 1 new test: wasm.test.opcode.elseop.pas
- Modified: wasm.vm.opcodes.pas (hub-only), wasm.test.pas, build.bat
2026-02-24 15:38:36 +00:00
t3hn3rd 3ed5c0a847 Merge branch 'feature/build-clean' into develop 2026-02-24 14:57:43 +00:00
t3hn3rd ff65a0d5f0 refactor: build.bat flexible argument parsing, add run & help commands
- Replaced positional `%1` checks with a loop-based argument parser
  so flags (`test`, `clean`, `run`, `help`) work in any order/combination
- Added `run` flag: executes `bin\WASURO.exe` after a successful build
- Added `help` flag: prints usage info and exits
- Fixed `clean` early-exit to also check `DO_RUN` (not just `TESTFLAGS`),
  so `build.bat clean run` correctly continues to build+run
- Added section comment headers (Build, Run) for consistency
2026-02-24 14:57:30 +00:00
t3hn3rd 76d7d737c0 Merge branch 'feature/missing-opcodes' into develop 2026-02-24 14:47:01 +00:00
t3hn3rd d12ad11746 feature: implement control flow opcodes, extract constants & helpers
## Control Flow Opcodes
- Implemented all 10 remaining stub control flow opcodes:
  block, loop, if, else, end, br, br_if, br_table, call, call_indirect
- Rewrote return opcode to walk control stack and restore call frames
- Fixed tick() hang: now halts when IP >= Limit

## Refactoring
- Extracted WASM_HDR_MAGIC and CTRL_FRAME_* constants to wasm.types.constants.pas
- Extracted push_control_frame, scan_forward, do_branch helpers to wasm.vm.control.pas
- Updated wasm.parser.pas to reference constants via wasm.types.constants

## Tests
- Added 9 new test units (25 tests):
  block, loop, if, end, br, br_if, br_table, call, call_indirect
- Added E2E binary test: 115-byte handcrafted WASM binary exercising
  all control flow opcodes (2 functions, expected result=30)
- All 425/425 tests passing
2026-02-24 14:46:30 +00:00
t3hn3rd 679973c6e9 Merge branch 'feature/type-refactoring' into develop 2026-02-24 13:24:15 +00:00
333 changed files with 14718 additions and 1770 deletions

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
bin
lib
AGENTS.md
GAP_ANALYSIS.md
+35
View File
@@ -8,7 +8,34 @@ set LIB=%ROOT%lib
set FPC=C:\Lazarus\fpc\3.2.2\bin\x86_64-win64\fpc.exe
set TESTFLAGS=
set DO_CLEAN=0
set DO_RUN=0
:parse_args
if "%1"=="" goto args_done
if "%1"=="test" set TESTFLAGS=-dRUN_TESTS
if "%1"=="clean" set DO_CLEAN=1
if "%1"=="run" set DO_RUN=1
if "%1"=="help" (
echo Usage: build.bat [options]
echo Options:
echo test - Build with tests enabled
echo clean - Clean build outputs
echo run - Run the built executable after building
echo help - Show this help message
exit /b 0
)
shift
goto parse_args
:args_done
if %DO_CLEAN%==1 (
echo Cleaning build outputs...
if exist "%BIN%" rd /s /q "%BIN%"
if exist "%LIB%" rd /s /q "%LIB%"
echo Clean complete.
if "%TESTFLAGS%"=="" if %DO_RUN%==0 exit /b 0
)
if not exist "%BIN%" mkdir "%BIN%"
if not exist "%LIB%" mkdir "%LIB%"
@@ -17,6 +44,7 @@ REM ── Lint ─────────────────────
powershell -NoProfile -ExecutionPolicy Bypass -File "%ROOT%lint.ps1" -SrcRoot "%SRC%"
if %ERRORLEVEL% NEQ 0 exit /b 1
REM ── Build ────────────────────────────────────────────────────────────
"%FPC%" ^
%TESTFLAGS% ^
-FE"%BIN%" ^
@@ -32,6 +60,7 @@ if %ERRORLEVEL% NEQ 0 exit /b 1
-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"
@@ -42,3 +71,9 @@ if %ERRORLEVEL% NEQ 0 (
)
echo Build succeeded: %BIN%\WASURO.exe
REM ── Run ────────────────────────────────────────────────────────────
if %DO_RUN%==1 (
echo Running WASURO...
"%BIN%\WASURO.exe"
)
@@ -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.
+18 -2
View File
@@ -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
+4 -1
View File
@@ -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;
+86
View File
@@ -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.
@@ -0,0 +1,43 @@
unit wasm.test.opcode.elemdrop;
interface
procedure run;
implementation
uses
wasm.types.builtin, wasm.types.sections, wasm.types.context,
wasm.vm, wasm.test.framework, lmemorymanager;
procedure run;
var
{ FC $0D, elem_idx=0 }
code : array[0..2] of TWASMUInt8;
ctx : PWASMProcessContext;
begin
test_begin('opcode.elem.drop');
code[0] := $FC; code[1] := $0D; code[2] := $00;
{ Drop an element segment }
ctx := make_test_context(@code[0], 3);
ctx^.ExecutionState.ElementSegments^.SegmentCount := 1;
ctx^.ExecutionState.ElementSegments^.Segments := PWASMElementSegment(kalloc(sizeof(TWASMElementSegment)));
ctx^.ExecutionState.ElementSegments^.Segments[0].FuncCount := 2;
ctx^.ExecutionState.ElementSegments^.Segments[0].FuncIndices := TWASMPUInt32(kalloc(2 * sizeof(TWASMUInt32)));
ctx^.ExecutionState.ElementSegments^.Segments[0].Dropped := false;
wasm.vm.tick(ctx);
assert_true('segment dropped', ctx^.ExecutionState.ElementSegments^.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 elem_idx=0 is out of range }
wasm.vm.tick(ctx);
assert_true('invalid_idx traps', not ctx^.ExecutionState.Running);
test_end;
end;
end.
@@ -0,0 +1,92 @@
unit wasm.test.opcode.elseop;
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..20] of TWASMUInt8;
ctx : PWASMProcessContext;
begin
test_begin('opcode.else');
{ Test 1: else skips else-body when if-true path taken }
{ i32.const 1 if $40 i32.const 10 else i32.const 20 end }
{ When condition is true the if-true body runs and else skips to past end }
code[0] := $41; { i32.const 1 }
code[1] := $01;
code[2] := $04; { if }
code[3] := $40; { blocktype empty }
code[4] := $41; { i32.const 10 }
code[5] := $0A;
code[6] := $05; { else }
code[7] := $41; { i32.const 20 }
code[8] := $14;
code[9] := $0B; { end }
ctx := make_test_context(@code[0], 10);
while wasm.vm.tick(ctx) do;
assert_i32('true path skips else body', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 10);
{ Test 2: else-body executes when condition is false }
{ i32.const 0 if $40 i32.const 10 else i32.const 20 end }
code[0] := $41; { i32.const 0 }
code[1] := $00;
code[2] := $04; { if }
code[3] := $40;
code[4] := $41; { i32.const 10 }
code[5] := $0A;
code[6] := $05; { else }
code[7] := $41; { i32.const 20 }
code[8] := $14;
code[9] := $0B; { end }
ctx := make_test_context(@code[0], 10);
while wasm.vm.tick(ctx) do;
assert_i32('false path executes else body', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 20);
{ Test 3: else skips nested block in else-body }
{ i32.const 1 if $40 i32.const 5 else block $40 i32.const 99 end end }
code[0] := $41; { i32.const 1 }
code[1] := $01;
code[2] := $04; { if }
code[3] := $40;
code[4] := $41; { i32.const 5 }
code[5] := $05;
code[6] := $05; { else }
code[7] := $02; { block }
code[8] := $40;
code[9] := $41; { i32.const 99 }
code[10] := $63;
code[11] := $0B; { end block }
code[12] := $0B; { end if }
ctx := make_test_context(@code[0], 13);
while wasm.vm.tick(ctx) do;
assert_i32('else skips nested block', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 5);
{ Test 4: execution continues after end when else is skipped }
{ i32.const 1 if $40 i32.const 3 else i32.const 4 end i32.const 7 }
code[0] := $41; { i32.const 1 }
code[1] := $01;
code[2] := $04; { if }
code[3] := $40;
code[4] := $41; { i32.const 3 }
code[5] := $03;
code[6] := $05; { else }
code[7] := $41; { i32.const 4 }
code[8] := $04;
code[9] := $0B; { end }
code[10] := $41; { i32.const 7 }
code[11] := $07;
ctx := make_test_context(@code[0], 12);
while wasm.vm.tick(ctx) do;
assert_i32('continues after else skip', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 7);
test_end;
end;
end.
Loaded 20 of 333 files, more files were not shown because too many files have changed in this diff. Show more