Compare commits
7
Commits
master
...
feature/readme
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fca24af1b0 | ||
|
|
8c16550bd4 | ||
|
|
9a3b813c96 | ||
|
|
8a4bb2ca35 | ||
|
|
ff28299132 | ||
|
|
dc67262477 | ||
|
|
d34997aa4e |
No files matched your search
@@ -1,2 +1,3 @@
|
|||||||
bin
|
bin
|
||||||
lib
|
lib
|
||||||
|
AGENTS.md
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
# WASURO
|
||||||
|
|
||||||
|
A WebAssembly virtual machine for the [Asuro](https://github.com/Nosfex/Asuro) operating system, written in Standard Pascal.
|
||||||
|
|
||||||
|
WASURO parses and executes WASM binaries directly in a bare-metal kernel environment — no runtime library, no classes, no heap manager beyond what the OS provides.
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
```
|
||||||
|
src/
|
||||||
|
├── emu/ # OS emulation layer (types, console, memory, LEB128)
|
||||||
|
├── wasm/
|
||||||
|
│ ├── parser/ # WASM binary parser
|
||||||
|
│ │ └── sections/ # Section-specific parsers (type, function, export,
|
||||||
|
│ │ # code, global, memory, start, data, etc.)
|
||||||
|
│ ├── types/ # WASM type system, stack, and linear memory (heap)
|
||||||
|
│ ├── vm/ # VM tick loop and opcode jump table
|
||||||
|
│ └── test/ # Test suite
|
||||||
|
│ ├── infra/ # Stack, heap, LEB128 tests
|
||||||
|
│ ├── opcodes/ # Per-opcode test units
|
||||||
|
│ └── parsers/ # Per-section parser tests
|
||||||
|
└── project/ # Lazarus project files
|
||||||
|
```
|
||||||
|
|
||||||
|
### Key components
|
||||||
|
|
||||||
|
- **VM** (`wasm.vm`) — Tick-based execution loop dispatching opcodes through a 256-entry jump table.
|
||||||
|
- **Parser** (`wasm.parser`) — Validates the WASM magic/version header, then routes each binary section to its handler.
|
||||||
|
- **Stack** (`wasm.types.stack`) — Operand and control stacks supporting i32, i64, f32, f64 push/pop.
|
||||||
|
- **Heap** (`wasm.types.heap`) — Paged linear memory with read/write at 8/16/32/64-bit widths.
|
||||||
|
- **Emulation layer** (`src/emu/`) — Provides the same `types`, `console`, `lmemorymanager`, and `leb128` interfaces that exist in the real Asuro kernel, allowing development and testing on a host OS.
|
||||||
|
|
||||||
|
### Implemented opcodes
|
||||||
|
|
||||||
|
| Category | Opcodes |
|
||||||
|
|---|---|
|
||||||
|
| Control | `nop`, `unreachable`, `drop`, `select`, `return` |
|
||||||
|
| Variables | `local.get/set/tee`, `global.get/set` |
|
||||||
|
| Constants | `i32.const`, `i64.const`, `f32.const`, `f64.const` |
|
||||||
|
| i32 Arithmetic | `add`, `sub`, `mul`, `div_s/u`, `rem_s/u`, `clz`, `ctz`, `popcnt` |
|
||||||
|
| i32 Bitwise | `and`, `or`, `xor`, `shl`, `shr_s/u`, `rotl`, `rotr` |
|
||||||
|
| i32 Comparison | `eqz`, `eq`, `ne`, `lt_s/u`, `gt_s/u`, `le_s/u`, `ge_s/u` |
|
||||||
|
| i64 Arithmetic | `add`, `sub`, `mul`, `div_s/u`, `rem_s/u`, `clz`, `ctz`, `popcnt` |
|
||||||
|
| i64 Bitwise | `and`, `or`, `xor`, `shl`, `shr_s/u`, `rotl`, `rotr` |
|
||||||
|
| i64 Comparison | `eqz`, `eq`, `ne`, `lt_s/u`, `gt_s/u`, `le_s/u`, `ge_s/u` |
|
||||||
|
| f32/f64 Comparison | `eq`, `ne`, `lt`, `gt`, `le`, `ge` |
|
||||||
|
| Memory Loads | `i32/i64/f32/f64.load`, sign/zero-extending variants (8/16/32-bit) |
|
||||||
|
| Memory Stores | `i32/i64/f32/f64.store`, narrowing variants (8/16/32-bit) |
|
||||||
|
| Memory Mgmt | `memory.size`, `memory.grow` |
|
||||||
|
|
||||||
|
### Parsed sections
|
||||||
|
|
||||||
|
Type, Function, Export, Code, Global, Memory, Start, Data, Import (stub), Table (stub), Element (stub), Custom (stub).
|
||||||
|
|
||||||
|
## Constraints
|
||||||
|
|
||||||
|
- **Standard Pascal only** — no runtime library, no classes.
|
||||||
|
- **Kernel-compatible** — all arithmetic (including `shr_s`) uses manual implementations, no RTL intrinsics.
|
||||||
|
- **Emulation layer** — `src/emu/` mirrors the real Asuro kernel API; only these interfaces are available.
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- [Free Pascal Compiler](https://www.freepascal.org/) 3.2.2+ (x86_64-win64)
|
||||||
|
- Windows (build script is a `.bat` file)
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
```bat
|
||||||
|
# Normal build
|
||||||
|
build.bat
|
||||||
|
|
||||||
|
# Build with test suite
|
||||||
|
build.bat test
|
||||||
|
```
|
||||||
|
|
||||||
|
Output: `bin\WASURO.exe`
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
```bat
|
||||||
|
build.bat test
|
||||||
|
bin\WASURO.exe
|
||||||
|
```
|
||||||
|
|
||||||
|
Runs 374 tests across 108 test units. Exit code equals the number of failed tests (0 on success), making it CI/CD-friendly.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
See repository for license details.
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
@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%"
|
||||||
|
|
||||||
|
"%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\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
|
||||||
+12
-1
@@ -10,12 +10,22 @@ uses
|
|||||||
wasm.types,
|
wasm.types,
|
||||||
wasm.parser,
|
wasm.parser,
|
||||||
wasm.vm, wasm.test.binary,
|
wasm.vm, wasm.test.binary,
|
||||||
wasm.types.stack;
|
wasm.types.stack
|
||||||
|
{$IFDEF RUN_TESTS}
|
||||||
|
, wasm.test
|
||||||
|
, wasm.test.framework
|
||||||
|
{$ENDIF}
|
||||||
|
;
|
||||||
|
|
||||||
var
|
var
|
||||||
Context : PWASMProcessContext;
|
Context : PWASMProcessContext;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
{$IFDEF RUN_TESTS}
|
||||||
|
wasm.vm.init();
|
||||||
|
wasm.test.run_all_tests;
|
||||||
|
halt(wasm.test.framework.FailedTests);
|
||||||
|
{$ELSE}
|
||||||
writestringln('[main] Initializing VM');
|
writestringln('[main] Initializing VM');
|
||||||
wasm.vm.init();
|
wasm.vm.init();
|
||||||
|
|
||||||
@@ -32,5 +42,6 @@ begin
|
|||||||
end;
|
end;
|
||||||
writeln();
|
writeln();
|
||||||
while true do sleep(1000);
|
while true do sleep(1000);
|
||||||
|
{$ENDIF}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@@ -4,16 +4,76 @@ interface
|
|||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
types, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types, wasm.types.heap;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos, bend : puint8;
|
||||||
|
bytesRead : uint8;
|
||||||
|
segCount : uint32;
|
||||||
|
i, j : uint32;
|
||||||
|
memIdx : uint32;
|
||||||
|
offset : uint32;
|
||||||
|
dataSize : uint32;
|
||||||
|
opcode : uint8;
|
||||||
|
tmpU32 : uint32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Data - Size: ');
|
writestring('[wasm.parser] Handle Section: Data - Size: ');
|
||||||
writeintlnWND(section_length, 0);
|
writeintlnWND(section_length, 0);
|
||||||
|
|
||||||
|
pos := buffer;
|
||||||
|
bend := puint8(buffer + section_length);
|
||||||
|
|
||||||
|
{ Read segment count }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @segCount);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
for i := 0 to segCount - 1 do begin
|
||||||
|
{ Read memory index }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @memIdx);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
{ Evaluate offset init expression (expect i32.const N, end) }
|
||||||
|
offset := 0;
|
||||||
|
opcode := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
if opcode = $41 then begin { i32.const }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @tmpU32);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
offset := tmpU32;
|
||||||
|
end else begin
|
||||||
|
writestring('[wasm.parser] Warning: unsupported data offset opcode: ');
|
||||||
|
writehexpair(opcode);
|
||||||
|
writestringln(' ');
|
||||||
|
while (pos < bend) and (pos^ <> $0B) do Inc(pos);
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ Consume end ($0B) }
|
||||||
|
if (pos < bend) and (pos^ = $0B) then
|
||||||
|
Inc(pos);
|
||||||
|
|
||||||
|
{ Read data byte count }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @dataSize);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
writestring('[wasm.parser] Data Segment ');
|
||||||
|
writeintWND(i, 0);
|
||||||
|
writestring(' - Offset: ');
|
||||||
|
writeintWND(offset, 0);
|
||||||
|
writestring(' Size: ');
|
||||||
|
writeintlnWND(dataSize, 0);
|
||||||
|
|
||||||
|
{ Copy bytes into linear memory }
|
||||||
|
for j := 0 to dataSize - 1 do begin
|
||||||
|
wasm.types.heap.write_uint8(offset + j, ctx^.ExecutionState.Memory, pos^);
|
||||||
|
Inc(pos);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -10,10 +10,113 @@ procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContex
|
|||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
|
procedure walk(ctx : PWASMProcessContext);
|
||||||
|
var
|
||||||
|
i : uint32;
|
||||||
|
entry : PWASMGlobalEntry;
|
||||||
|
begin
|
||||||
|
for i := 0 to ctx^.ExecutionState.Globals^.GlobalCount - 1 do begin
|
||||||
|
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: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos, bend : puint8;
|
||||||
|
bytesRead : uint8;
|
||||||
|
globalCount : uint32;
|
||||||
|
i : uint32;
|
||||||
|
entry : PWASMGlobalEntry;
|
||||||
|
valType : uint8;
|
||||||
|
mutFlag : uint8;
|
||||||
|
opcode : uint8;
|
||||||
|
tmpU32 : uint32;
|
||||||
|
tmpU64 : uint64;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Global - Size: ');
|
writestring('[wasm.parser] Handle Section: Global - Size: ');
|
||||||
writeintlnWND(section_length, 0);
|
writeintlnWND(section_length, 0);
|
||||||
|
|
||||||
|
pos := buffer;
|
||||||
|
bend := puint8(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 := int32(tmpU32);
|
||||||
|
end;
|
||||||
|
$42: begin { i64.const }
|
||||||
|
bytesRead := read_leb128_to_uint64(pos, bend, @tmpU64);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
entry^.Value.i64Value := int64(tmpU64);
|
||||||
|
end;
|
||||||
|
$43: begin { f32.const }
|
||||||
|
entry^.Value.f32Value := pfloat(pos)^;
|
||||||
|
Inc(pos, 4);
|
||||||
|
end;
|
||||||
|
$44: begin { f64.const }
|
||||||
|
entry^.Value.f64Value := pdouble(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;
|
||||||
|
|
||||||
|
walk(ctx);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -4,16 +4,81 @@ interface
|
|||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
types, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types, wasm.types.heap;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
|
procedure walk(ctx : PWASMProcessContext);
|
||||||
|
var
|
||||||
|
i : uint32;
|
||||||
|
mem : PWASMMemoryLimits;
|
||||||
|
begin
|
||||||
|
for i := 0 to ctx^.Sections.MemorySection^.MemoryCount - 1 do begin
|
||||||
|
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: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos, bend : puint8;
|
||||||
|
bytesRead : uint8;
|
||||||
|
memCount : uint32;
|
||||||
|
i : uint32;
|
||||||
|
mem : PWASMMemoryLimits;
|
||||||
|
flags : uint32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Memory - Size: ');
|
writestring('[wasm.parser] Handle Section: Memory - Size: ');
|
||||||
writeintlnWND(section_length, 0);
|
writeintlnWND(section_length, 0);
|
||||||
|
|
||||||
|
pos := buffer;
|
||||||
|
bend := puint8(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;
|
||||||
|
|
||||||
|
walk(ctx);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -11,9 +11,20 @@ procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContex
|
|||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
bytesRead : uint8;
|
||||||
|
funcIdx : uint32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Start - Size: ');
|
writestring('[wasm.parser] Handle Section: Start - Size: ');
|
||||||
writeintlnWND(section_length, 0);
|
writeintlnWND(section_length, 0);
|
||||||
|
|
||||||
|
{ Read the start function index }
|
||||||
|
bytesRead := read_leb128_to_uint32(buffer, puint8(buffer + section_length), @funcIdx);
|
||||||
|
ctx^.Sections.StartIndex := int32(funcIdx);
|
||||||
|
|
||||||
|
writestring('[wasm.parser] Start Function Index: ');
|
||||||
|
writeintlnWND(funcIdx, 0);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -39,6 +39,13 @@ begin
|
|||||||
ctx^.Sections.FunctionSection:= nil;
|
ctx^.Sections.FunctionSection:= nil;
|
||||||
ctx^.Sections.ExportSection:= nil;
|
ctx^.Sections.ExportSection:= nil;
|
||||||
ctx^.Sections.CodeSection:= nil;
|
ctx^.Sections.CodeSection:= nil;
|
||||||
|
ctx^.Sections.MemorySection:= nil;
|
||||||
|
ctx^.Sections.StartIndex:= -1;
|
||||||
|
|
||||||
|
// Initialize globals (empty until global section is parsed)
|
||||||
|
ctx^.ExecutionState.Globals:= PWASMGlobals(kalloc(sizeof(TWASMGlobals)));
|
||||||
|
ctx^.ExecutionState.Globals^.GlobalCount:= 0;
|
||||||
|
ctx^.ExecutionState.Globals^.Globals:= nil;
|
||||||
|
|
||||||
newContext:= ctx;
|
newContext:= ctx;
|
||||||
end;
|
end;
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
unit wasm.test.heap;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses
|
||||||
|
types, wasm.types.heap, wasm.test.framework;
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
var
|
||||||
|
h : PWasmHeap;
|
||||||
|
v8 : uint8;
|
||||||
|
v16 : uint16;
|
||||||
|
v32 : uint32;
|
||||||
|
v64 : uint64;
|
||||||
|
ok : boolean;
|
||||||
|
begin
|
||||||
|
test_begin('heap');
|
||||||
|
|
||||||
|
h := new_heap();
|
||||||
|
|
||||||
|
{ Test: new heap has 1 page }
|
||||||
|
assert_u32('new heap pagecount=1', h^.PageCount, 1);
|
||||||
|
|
||||||
|
{ Test: write/read uint8 }
|
||||||
|
ok := write_uint8(0, h, $AB);
|
||||||
|
assert_bool('write_uint8 success', ok, true);
|
||||||
|
ok := read_uint8(0, h, @v8);
|
||||||
|
assert_bool('read_uint8 success', ok, true);
|
||||||
|
assert_u32('read_uint8 value', uint32(v8), $AB);
|
||||||
|
|
||||||
|
{ Test: write/read uint16 }
|
||||||
|
ok := write_uint16(4, h, $1234);
|
||||||
|
assert_bool('write_uint16 success', ok, true);
|
||||||
|
ok := read_uint16(4, h, @v16);
|
||||||
|
assert_bool('read_uint16 success', ok, true);
|
||||||
|
assert_u32('read_uint16 value', uint32(v16), $1234);
|
||||||
|
|
||||||
|
{ Test: write/read uint32 }
|
||||||
|
ok := write_uint32(8, h, $DEADBEEF);
|
||||||
|
assert_bool('write_uint32 success', ok, true);
|
||||||
|
ok := read_uint32(8, h, @v32);
|
||||||
|
assert_bool('read_uint32 success', ok, true);
|
||||||
|
assert_u32('read_uint32 value', v32, $DEADBEEF);
|
||||||
|
|
||||||
|
{ Test: write/read uint64 }
|
||||||
|
ok := write_uint64(16, h, uint64($CAFEBABE12345678));
|
||||||
|
assert_bool('write_uint64 success', ok, true);
|
||||||
|
ok := read_uint64(16, h, @v64);
|
||||||
|
assert_bool('read_uint64 success', ok, true);
|
||||||
|
assert_u64('read_uint64 value', v64, uint64($CAFEBABE12345678));
|
||||||
|
|
||||||
|
{ Test: out of bounds read fails }
|
||||||
|
ok := read_uint8($10000, h, @v8);
|
||||||
|
assert_bool('oob read fails', ok, false);
|
||||||
|
|
||||||
|
{ Test: out of bounds write fails }
|
||||||
|
ok := write_uint8($10000, h, $FF);
|
||||||
|
assert_bool('oob write fails', ok, false);
|
||||||
|
|
||||||
|
{ Test: expand heap }
|
||||||
|
ok := expand_heap(h);
|
||||||
|
assert_bool('expand_heap success', ok, true);
|
||||||
|
assert_u32('expanded pagecount=2', h^.PageCount, 2);
|
||||||
|
|
||||||
|
{ Test: can now access page 2 }
|
||||||
|
ok := write_uint32($10000, h, $BAADF00D);
|
||||||
|
assert_bool('write page2 success', ok, true);
|
||||||
|
ok := read_uint32($10000, h, @v32);
|
||||||
|
assert_bool('read page2 success', ok, true);
|
||||||
|
assert_u32('read page2 value', v32, $BAADF00D);
|
||||||
|
|
||||||
|
{ Test: data persists after expand on page 1 }
|
||||||
|
ok := read_uint32(8, h, @v32);
|
||||||
|
assert_bool('page1 data persists', ok, true);
|
||||||
|
assert_u32('page1 data value', v32, $DEADBEEF);
|
||||||
|
|
||||||
|
{ Test: cross-page boundary read }
|
||||||
|
ok := write_uint8($FFFE, h, $11);
|
||||||
|
ok := write_uint8($FFFF, h, $22);
|
||||||
|
ok := write_uint8($10000, h, $33);
|
||||||
|
ok := write_uint8($10001, h, $44);
|
||||||
|
ok := read_uint32($FFFE, h, @v32);
|
||||||
|
assert_bool('cross-page read success', ok, true);
|
||||||
|
assert_u32('cross-page read value', v32, $44332211);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
unit wasm.test.leb128;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses
|
||||||
|
types, leb128, wasm.test.framework;
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
var
|
||||||
|
buf : array[0..9] of uint8;
|
||||||
|
buf_end : puint8;
|
||||||
|
res32 : uint32;
|
||||||
|
res64 : uint64;
|
||||||
|
bytesRead : uint8;
|
||||||
|
begin
|
||||||
|
test_begin('leb128');
|
||||||
|
buf_end := @buf[10];
|
||||||
|
|
||||||
|
{ Test: single byte, value 0 }
|
||||||
|
buf[0] := $00;
|
||||||
|
bytesRead := read_leb128_to_uint32(@buf[0], buf_end, @res32);
|
||||||
|
assert_u32('u32 value=0', res32, 0);
|
||||||
|
assert_u32('u32 value=0 bytes=1', uint32(bytesRead), 1);
|
||||||
|
|
||||||
|
{ Test: single byte, value 42 }
|
||||||
|
buf[0] := $2A;
|
||||||
|
bytesRead := read_leb128_to_uint32(@buf[0], buf_end, @res32);
|
||||||
|
assert_u32('u32 value=42', res32, 42);
|
||||||
|
assert_u32('u32 value=42 bytes=1', uint32(bytesRead), 1);
|
||||||
|
|
||||||
|
{ Test: single byte, value 127 }
|
||||||
|
buf[0] := $7F;
|
||||||
|
bytesRead := read_leb128_to_uint32(@buf[0], buf_end, @res32);
|
||||||
|
assert_u32('u32 value=127', res32, 127);
|
||||||
|
|
||||||
|
{ Test: two bytes, value 128 }
|
||||||
|
buf[0] := $80;
|
||||||
|
buf[1] := $01;
|
||||||
|
bytesRead := read_leb128_to_uint32(@buf[0], buf_end, @res32);
|
||||||
|
assert_u32('u32 value=128', res32, 128);
|
||||||
|
assert_u32('u32 value=128 bytes=2', uint32(bytesRead), 2);
|
||||||
|
|
||||||
|
{ Test: two bytes, value 255 }
|
||||||
|
buf[0] := $FF;
|
||||||
|
buf[1] := $01;
|
||||||
|
bytesRead := read_leb128_to_uint32(@buf[0], buf_end, @res32);
|
||||||
|
assert_u32('u32 value=255', res32, 255);
|
||||||
|
|
||||||
|
{ Test: two bytes, value 256 }
|
||||||
|
buf[0] := $80;
|
||||||
|
buf[1] := $02;
|
||||||
|
bytesRead := read_leb128_to_uint32(@buf[0], buf_end, @res32);
|
||||||
|
assert_u32('u32 value=256', res32, 256);
|
||||||
|
|
||||||
|
{ Test: larger value 624485 = 0xE5 0x8E 0x26 }
|
||||||
|
buf[0] := $E5;
|
||||||
|
buf[1] := $8E;
|
||||||
|
buf[2] := $26;
|
||||||
|
bytesRead := read_leb128_to_uint32(@buf[0], buf_end, @res32);
|
||||||
|
assert_u32('u32 value=624485', res32, 624485);
|
||||||
|
assert_u32('u32 value=624485 bytes=3', uint32(bytesRead), 3);
|
||||||
|
|
||||||
|
{ Test: uint64 single byte }
|
||||||
|
buf[0] := $2A;
|
||||||
|
bytesRead := read_leb128_to_uint64(@buf[0], buf_end, @res64);
|
||||||
|
assert_u64('u64 value=42', res64, 42);
|
||||||
|
assert_u32('u64 value=42 bytes=1', uint32(bytesRead), 1);
|
||||||
|
|
||||||
|
{ Test: uint64 two bytes }
|
||||||
|
buf[0] := $80;
|
||||||
|
buf[1] := $01;
|
||||||
|
bytesRead := read_leb128_to_uint64(@buf[0], buf_end, @res64);
|
||||||
|
assert_u64('u64 value=128', res64, 128);
|
||||||
|
|
||||||
|
{ Test: uint64 multi-byte }
|
||||||
|
buf[0] := $E5;
|
||||||
|
buf[1] := $8E;
|
||||||
|
buf[2] := $26;
|
||||||
|
bytesRead := read_leb128_to_uint64(@buf[0], buf_end, @res64);
|
||||||
|
assert_u64('u64 value=624485', res64, 624485);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
unit wasm.test.stack;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses
|
||||||
|
types, wasm.types, wasm.types.stack, wasm.test.framework;
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
var
|
||||||
|
s : PWASMStack;
|
||||||
|
v128 : uint128;
|
||||||
|
begin
|
||||||
|
test_begin('stack');
|
||||||
|
|
||||||
|
{ Test: push/pop i32 }
|
||||||
|
s := newStack(16);
|
||||||
|
pushi32(s, 42);
|
||||||
|
assert_i32('pushi32/popi32 basic', popi32(s), 42);
|
||||||
|
|
||||||
|
{ Test: push/pop i32 negative }
|
||||||
|
pushi32(s, -100);
|
||||||
|
assert_i32('pushi32/popi32 negative', popi32(s), -100);
|
||||||
|
|
||||||
|
{ Test: push/pop i64 }
|
||||||
|
pushi64(s, 1234567890123);
|
||||||
|
assert_i64('pushi64/popi64 basic', popi64(s), 1234567890123);
|
||||||
|
|
||||||
|
{ Test: push/pop i64 negative }
|
||||||
|
pushi64(s, -9876543210);
|
||||||
|
assert_i64('pushi64/popi64 negative', popi64(s), -9876543210);
|
||||||
|
|
||||||
|
{ Test: push/pop f32 }
|
||||||
|
pushf32(s, 3.14);
|
||||||
|
assert_f32('pushf32/popf32 basic', popf32(s), 3.14);
|
||||||
|
|
||||||
|
{ Test: push/pop f64 }
|
||||||
|
pushf64(s, 2.718281828);
|
||||||
|
assert_f64('pushf64/popf64 basic', popf64(s), 2.718281828);
|
||||||
|
|
||||||
|
{ Test: push/pop func ref }
|
||||||
|
pushfunc(s, 7);
|
||||||
|
assert_u32('pushfunc/popfunc basic', popfunc(s), 7);
|
||||||
|
|
||||||
|
{ Test: push/pop extn ref }
|
||||||
|
pushextn(s, 99);
|
||||||
|
assert_u32('pushextn/popextn basic', popextn(s), 99);
|
||||||
|
|
||||||
|
{ Test: push/pop v128 }
|
||||||
|
v128.low := $DEADBEEF;
|
||||||
|
v128.high := $CAFEBABE;
|
||||||
|
pushv128(s, v128);
|
||||||
|
v128 := popv128(s);
|
||||||
|
assert_u64('pushv128/popv128 low', v128.low, $DEADBEEF);
|
||||||
|
assert_u64('pushv128/popv128 high', v128.high, $CAFEBABE);
|
||||||
|
|
||||||
|
{ Test: LIFO order }
|
||||||
|
pushi32(s, 1);
|
||||||
|
pushi32(s, 2);
|
||||||
|
pushi32(s, 3);
|
||||||
|
assert_i32('LIFO order 3', popi32(s), 3);
|
||||||
|
assert_i32('LIFO order 2', popi32(s), 2);
|
||||||
|
assert_i32('LIFO order 1', popi32(s), 1);
|
||||||
|
|
||||||
|
{ Test: stack top tracking }
|
||||||
|
assert_u32('empty stack top=0', s^.Top, 0);
|
||||||
|
pushi32(s, 10);
|
||||||
|
assert_u32('one item top=1', s^.Top, 1);
|
||||||
|
pushi32(s, 20);
|
||||||
|
assert_u32('two items top=2', s^.Top, 2);
|
||||||
|
popi32(s);
|
||||||
|
assert_u32('after pop top=1', s^.Top, 1);
|
||||||
|
popi32(s);
|
||||||
|
assert_u32('after pop top=0', s^.Top, 0);
|
||||||
|
|
||||||
|
{ Test: stack full flag }
|
||||||
|
s := newStack(2);
|
||||||
|
assert_bool('new stack not full', s^.Full, false);
|
||||||
|
pushi32(s, 1);
|
||||||
|
pushi32(s, 2);
|
||||||
|
assert_bool('full stack', s^.Full, true);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
unit wasm.test.opcode.drop;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses
|
||||||
|
types, wasm.types, wasm.types.stack, wasm.vm, wasm.test.framework;
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
var
|
||||||
|
code : array[0..0] of uint8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.drop');
|
||||||
|
|
||||||
|
{ Test: drop removes top of stack }
|
||||||
|
code[0] := $1A; { drop }
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushi32(ctx^.ExecutionState.Operand_Stack, 42);
|
||||||
|
pushi32(ctx^.ExecutionState.Operand_Stack, 99);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_u32('stack top decreased', ctx^.ExecutionState.Operand_Stack^.Top, 1);
|
||||||
|
assert_i32('remaining value', popi32(ctx^.ExecutionState.Operand_Stack), 42);
|
||||||
|
|
||||||
|
{ Test: drop on empty stack traps }
|
||||||
|
code[0] := $1A;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_bool('empty drop traps', ctx^.ExecutionState.Running, false);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
unit wasm.test.opcode.f32const;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses
|
||||||
|
types, wasm.types, wasm.types.stack, wasm.vm, wasm.test.framework;
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
var
|
||||||
|
code : array[0..4] of uint8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
f : float;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.f32const');
|
||||||
|
|
||||||
|
{ Test: f32.const 3.14 }
|
||||||
|
code[0] := $43; { f32.const }
|
||||||
|
f := 3.14;
|
||||||
|
code[1] := puint8(@f)[0];
|
||||||
|
code[2] := puint8(@f)[1];
|
||||||
|
code[3] := puint8(@f)[2];
|
||||||
|
code[4] := puint8(@f)[3];
|
||||||
|
ctx := make_test_context(@code[0], 5);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('const 3.14', popf32(ctx^.ExecutionState.Operand_Stack), 3.14);
|
||||||
|
|
||||||
|
{ Test: f32.const 0.0 }
|
||||||
|
f := 0.0;
|
||||||
|
code[1] := puint8(@f)[0];
|
||||||
|
code[2] := puint8(@f)[1];
|
||||||
|
code[3] := puint8(@f)[2];
|
||||||
|
code[4] := puint8(@f)[3];
|
||||||
|
ctx := make_test_context(@code[0], 5);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('const 0.0', popf32(ctx^.ExecutionState.Operand_Stack), 0.0);
|
||||||
|
|
||||||
|
{ Test: f32.const -1.0 }
|
||||||
|
f := -1.0;
|
||||||
|
code[1] := puint8(@f)[0];
|
||||||
|
code[2] := puint8(@f)[1];
|
||||||
|
code[3] := puint8(@f)[2];
|
||||||
|
code[4] := puint8(@f)[3];
|
||||||
|
ctx := make_test_context(@code[0], 5);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('const -1.0', popf32(ctx^.ExecutionState.Operand_Stack), -1.0);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
unit wasm.test.opcode.f32eq;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses
|
||||||
|
types, wasm.types, wasm.types.stack, wasm.vm, wasm.test.framework;
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
var
|
||||||
|
code : array[0..0] of uint8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.f32.eq');
|
||||||
|
|
||||||
|
code[0] := $5B;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('1.0==1.0', popi32(ctx^.ExecutionState.Operand_Stack), 1);
|
||||||
|
|
||||||
|
code[0] := $5B;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 2.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('1.0!=2.0', popi32(ctx^.ExecutionState.Operand_Stack), 0);
|
||||||
|
|
||||||
|
code[0] := $5B;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, -0.5);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, -0.5);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('-0.5==-0.5', popi32(ctx^.ExecutionState.Operand_Stack), 1);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
unit wasm.test.opcode.f32ge;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses
|
||||||
|
types, wasm.types, wasm.types.stack, wasm.vm, wasm.test.framework;
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
var
|
||||||
|
code : array[0..0] of uint8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.f32.ge');
|
||||||
|
|
||||||
|
code[0] := $60;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 2.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('2.0>=1.0', popi32(ctx^.ExecutionState.Operand_Stack), 1);
|
||||||
|
|
||||||
|
code[0] := $60;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('1.0>=1.0', popi32(ctx^.ExecutionState.Operand_Stack), 1);
|
||||||
|
|
||||||
|
code[0] := $60;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 2.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('1.0>=2.0', popi32(ctx^.ExecutionState.Operand_Stack), 0);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
unit wasm.test.opcode.f32gt;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses
|
||||||
|
types, wasm.types, wasm.types.stack, wasm.vm, wasm.test.framework;
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
var
|
||||||
|
code : array[0..0] of uint8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.f32.gt');
|
||||||
|
|
||||||
|
code[0] := $5E;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 2.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('2.0>1.0', popi32(ctx^.ExecutionState.Operand_Stack), 1);
|
||||||
|
|
||||||
|
code[0] := $5E;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 2.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('1.0>2.0', popi32(ctx^.ExecutionState.Operand_Stack), 0);
|
||||||
|
|
||||||
|
code[0] := $5E;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('1.0>1.0', popi32(ctx^.ExecutionState.Operand_Stack), 0);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
unit wasm.test.opcode.f32le;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses
|
||||||
|
types, wasm.types, wasm.types.stack, wasm.vm, wasm.test.framework;
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
var
|
||||||
|
code : array[0..0] of uint8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.f32.le');
|
||||||
|
|
||||||
|
code[0] := $5F;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 2.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('1.0<=2.0', popi32(ctx^.ExecutionState.Operand_Stack), 1);
|
||||||
|
|
||||||
|
code[0] := $5F;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('1.0<=1.0', popi32(ctx^.ExecutionState.Operand_Stack), 1);
|
||||||
|
|
||||||
|
code[0] := $5F;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 2.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('2.0<=1.0', popi32(ctx^.ExecutionState.Operand_Stack), 0);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
unit wasm.test.opcode.f32load;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses
|
||||||
|
types, wasm.types, wasm.types.heap, wasm.types.stack, wasm.vm, wasm.test.framework;
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
var
|
||||||
|
code : array[0..2] of uint8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
f : float;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.f32.load');
|
||||||
|
|
||||||
|
code[0] := $2A; { f32.load }
|
||||||
|
code[1] := $00; { align }
|
||||||
|
code[2] := $00; { offset }
|
||||||
|
ctx := make_test_context(@code[0], 3);
|
||||||
|
f := 3.14;
|
||||||
|
wasm.types.heap.write_uint32(0, ctx^.ExecutionState.Memory, puint32(@f)^);
|
||||||
|
pushi32(ctx^.ExecutionState.Operand_Stack, 0); { base address }
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('load 3.14', popf32(ctx^.ExecutionState.Operand_Stack), 3.14);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
unit wasm.test.opcode.f32lt;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses
|
||||||
|
types, wasm.types, wasm.types.stack, wasm.vm, wasm.test.framework;
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
var
|
||||||
|
code : array[0..0] of uint8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.f32.lt');
|
||||||
|
|
||||||
|
code[0] := $5D;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 2.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('1.0<2.0', popi32(ctx^.ExecutionState.Operand_Stack), 1);
|
||||||
|
|
||||||
|
code[0] := $5D;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 2.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('2.0<1.0', popi32(ctx^.ExecutionState.Operand_Stack), 0);
|
||||||
|
|
||||||
|
code[0] := $5D;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_i32('1.0<1.0', popi32(ctx^.ExecutionState.Operand_Stack), 0);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
Loaded 20 of 133 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user