Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
835f6f86cc | ||
|
|
a5192366ed | ||
|
|
d3cd71072b | ||
|
|
e40d8e9ed5 | ||
|
|
fca24af1b0 | ||
|
|
8c16550bd4 | ||
|
|
9a3b813c96 | ||
|
|
8a4bb2ca35 | ||
|
|
ff28299132 | ||
|
|
d34997aa4e |
No files matched your search
@@ -1,2 +1,3 @@
|
||||
bin
|
||||
lib
|
||||
AGENTS.md
|
||||
@@ -0,0 +1,89 @@
|
||||
# WASURO
|
||||
|
||||
A WebAssembly virtual machine for the [Asuro](https://gitea.spexeah.com/Spexeah/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/ # WASM Virtual Machine
|
||||
│ ├── 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,40 @@
|
||||
@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\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
|
||||
@@ -9,7 +9,7 @@ uses
|
||||
|
||||
wasm.types,
|
||||
wasm.parser,
|
||||
wasm.vm, wasm.test.binary,
|
||||
wasm.vm, wasm.test.binary.return42,
|
||||
wasm.types.stack
|
||||
{$IFDEF RUN_TESTS}
|
||||
, wasm.test
|
||||
@@ -30,7 +30,7 @@ begin
|
||||
wasm.vm.init();
|
||||
|
||||
writestringln('[main] Parsing WASM Binary');
|
||||
Context:= wasm.parser.parse(@wasm.test.binary.TEST_BINARY_1[0], puint8(@wasm.test.binary.TEST_BINARY_1[0] + $26));
|
||||
Context:= wasm.parser.parse(@wasm.test.binary.return42.BINARY[0], puint8(@wasm.test.binary.return42.BINARY[0] + wasm.test.binary.return42.BINARY_SIZE));
|
||||
|
||||
writestringln('[main] Running...');
|
||||
Context^.ExecutionState.Running:= true;
|
||||
|
||||
@@ -4,16 +4,76 @@ interface
|
||||
|
||||
uses
|
||||
types, lmemorymanager, console, leb128,
|
||||
wasm.types;
|
||||
wasm.types, wasm.types.heap;
|
||||
|
||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||
|
||||
implementation
|
||||
|
||||
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
|
||||
writestring('[wasm.parser] Handle Section: Data - Size: ');
|
||||
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.
|
||||
@@ -10,10 +10,113 @@ procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContex
|
||||
|
||||
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);
|
||||
var
|
||||
pos, bend : puint8;
|
||||
bytesRead : uint8;
|
||||
globalCount : uint32;
|
||||
i : uint32;
|
||||
entry : PWASMGlobalEntry;
|
||||
valType : uint8;
|
||||
mutFlag : uint8;
|
||||
opcode : uint8;
|
||||
tmpU32 : uint32;
|
||||
tmpU64 : uint64;
|
||||
|
||||
begin
|
||||
writestring('[wasm.parser] Handle Section: Global - Size: ');
|
||||
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.
|
||||
@@ -4,16 +4,81 @@ interface
|
||||
|
||||
uses
|
||||
types, lmemorymanager, console, leb128,
|
||||
wasm.types;
|
||||
wasm.types, wasm.types.heap;
|
||||
|
||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||
|
||||
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);
|
||||
var
|
||||
pos, bend : puint8;
|
||||
bytesRead : uint8;
|
||||
memCount : uint32;
|
||||
i : uint32;
|
||||
mem : PWASMMemoryLimits;
|
||||
flags : uint32;
|
||||
|
||||
begin
|
||||
writestring('[wasm.parser] Handle Section: Memory - Size: ');
|
||||
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.
|
||||
@@ -11,9 +11,20 @@ procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContex
|
||||
implementation
|
||||
|
||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||
var
|
||||
bytesRead : uint8;
|
||||
funcIdx : uint32;
|
||||
|
||||
begin
|
||||
writestring('[wasm.parser] Handle Section: Start - Size: ');
|
||||
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.
|
||||
@@ -39,6 +39,13 @@ begin
|
||||
ctx^.Sections.FunctionSection:= nil;
|
||||
ctx^.Sections.ExportSection:= 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;
|
||||
end;
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
{ E2E test: i32.const 10 + i32.const 32 = 42
|
||||
Equivalent WAT:
|
||||
(module
|
||||
(func (export "main") (result i32)
|
||||
i32.const 10
|
||||
i32.const 32
|
||||
i32.add
|
||||
return
|
||||
)
|
||||
)
|
||||
}
|
||||
unit wasm.test.binary.addnums;
|
||||
|
||||
interface
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
types, console, wasm.types, wasm.types.stack,
|
||||
wasm.parser, wasm.vm, wasm.test.framework;
|
||||
|
||||
const
|
||||
BINARY_SIZE = 41;
|
||||
BINARY : Array[$00..$28] of uint8 = (
|
||||
$00, $61, $73, $6D, { magic }
|
||||
$01, $00, $00, $00, { version 1 }
|
||||
$01, $05, $01, $60, { type section: 1 func type }
|
||||
$00, $01, $7F, { () -> (i32) }
|
||||
$03, $02, $01, $00, { function section: 1 func, type 0 }
|
||||
$07, $08, $01, $04, { export section: 1 export, name len 4 }
|
||||
$6D, $61, $69, $6E, { "main" }
|
||||
$00, $00, { export type=func, index=0 }
|
||||
$0A, $0A, $01, $08, { code section: len=10, count=1, body=8 }
|
||||
$00, { 0 locals }
|
||||
$41, $0A, { i32.const 10 }
|
||||
$41, $20, { i32.const 32 }
|
||||
$6A, { i32.add }
|
||||
$0F, $0B { return, end }
|
||||
);
|
||||
|
||||
procedure run;
|
||||
var
|
||||
ctx : PWASMProcessContext;
|
||||
begin
|
||||
test_begin('binary.addnums');
|
||||
|
||||
ctx := wasm.parser.parse(@BINARY[0], puint8(@BINARY[0] + BINARY_SIZE));
|
||||
assert_true('valid binary', ctx^.ValidBinary);
|
||||
|
||||
ctx^.ExecutionState.Running := true;
|
||||
while wasm.vm.tick(ctx) do;
|
||||
|
||||
assert_bool('execution stopped', ctx^.ExecutionState.Running, false);
|
||||
assert_true('stack has result', ctx^.ExecutionState.Operand_Stack^.Top > 0);
|
||||
assert_i32('10+32=42', popi32(ctx^.ExecutionState.Operand_Stack), 42);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -0,0 +1,66 @@
|
||||
{ E2E test: (2 + 3) * 4 = 20
|
||||
Equivalent WAT:
|
||||
(module
|
||||
(func (export "main") (result i32)
|
||||
i32.const 2
|
||||
i32.const 3
|
||||
i32.add
|
||||
i32.const 4
|
||||
i32.mul
|
||||
return
|
||||
)
|
||||
)
|
||||
}
|
||||
unit wasm.test.binary.arithmetic;
|
||||
|
||||
interface
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
types, console, wasm.types, wasm.types.stack,
|
||||
wasm.parser, wasm.vm, wasm.test.framework;
|
||||
|
||||
const
|
||||
BINARY_SIZE = 44;
|
||||
BINARY : Array[$00..$2B] of uint8 = (
|
||||
$00, $61, $73, $6D, { magic }
|
||||
$01, $00, $00, $00, { version 1 }
|
||||
$01, $05, $01, $60, { type section: 1 func type }
|
||||
$00, $01, $7F, { () -> (i32) }
|
||||
$03, $02, $01, $00, { function section: 1 func, type 0 }
|
||||
$07, $08, $01, $04, { export section: 1 export, name len 4 }
|
||||
$6D, $61, $69, $6E, { "main" }
|
||||
$00, $00, { export type=func, index=0 }
|
||||
$0A, $0D, $01, $0B, { code section: len=13, count=1, body=11 }
|
||||
$00, { 0 locals }
|
||||
$41, $02, { i32.const 2 }
|
||||
$41, $03, { i32.const 3 }
|
||||
$6A, { i32.add }
|
||||
$41, $04, { i32.const 4 }
|
||||
$6C, { i32.mul }
|
||||
$0F, $0B { return, end }
|
||||
);
|
||||
|
||||
procedure run;
|
||||
var
|
||||
ctx : PWASMProcessContext;
|
||||
begin
|
||||
test_begin('binary.arithmetic');
|
||||
|
||||
ctx := wasm.parser.parse(@BINARY[0], puint8(@BINARY[0] + BINARY_SIZE));
|
||||
assert_true('valid binary', ctx^.ValidBinary);
|
||||
|
||||
ctx^.ExecutionState.Running := true;
|
||||
while wasm.vm.tick(ctx) do;
|
||||
|
||||
assert_bool('execution stopped', ctx^.ExecutionState.Running, false);
|
||||
assert_true('stack has result', ctx^.ExecutionState.Operand_Stack^.Top > 0);
|
||||
assert_i32('(2+3)*4=20', popi32(ctx^.ExecutionState.Operand_Stack), 20);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -0,0 +1,74 @@
|
||||
{ E2E test: use local variables, local.set 0 := 10, local.set 1 := 20,
|
||||
return local.get 0 + local.get 1 = 30
|
||||
Equivalent WAT:
|
||||
(module
|
||||
(func (export "main") (result i32) (local i32 i32)
|
||||
i32.const 10
|
||||
local.set 0
|
||||
i32.const 20
|
||||
local.set 1
|
||||
local.get 0
|
||||
local.get 1
|
||||
i32.add
|
||||
return
|
||||
)
|
||||
)
|
||||
}
|
||||
unit wasm.test.binary.locals;
|
||||
|
||||
interface
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
types, console, wasm.types, wasm.types.stack,
|
||||
wasm.parser, wasm.vm, wasm.test.framework;
|
||||
|
||||
const
|
||||
BINARY_SIZE = 51;
|
||||
BINARY : Array[$00..$32] of uint8 = (
|
||||
$00, $61, $73, $6D, { magic }
|
||||
$01, $00, $00, $00, { version 1 }
|
||||
$01, $05, $01, $60, { type section: 1 func type }
|
||||
$00, $01, $7F, { () -> (i32) }
|
||||
$03, $02, $01, $00, { function section: 1 func, type 0 }
|
||||
$07, $08, $01, $04, { export section: 1 export, name len 4 }
|
||||
$6D, $61, $69, $6E, { "main" }
|
||||
$00, $00, { export type=func, index=0 }
|
||||
$0A, $14, $01, $12, { code section: len=20, count=1, body=18 }
|
||||
$01, $02, $7F, { 1 local type entry: 2 x i32 }
|
||||
$41, $0A, { i32.const 10 }
|
||||
$21, $00, { local.set 0 }
|
||||
$41, $14, { i32.const 20 }
|
||||
$21, $01, { local.set 1 }
|
||||
$20, $00, { local.get 0 }
|
||||
$20, $01, { local.get 1 }
|
||||
$6A, { i32.add }
|
||||
$0F, $0B { return, end }
|
||||
);
|
||||
|
||||
procedure run;
|
||||
var
|
||||
ctx : PWASMProcessContext;
|
||||
begin
|
||||
test_begin('binary.locals');
|
||||
|
||||
ctx := wasm.parser.parse(@BINARY[0], puint8(@BINARY[0] + BINARY_SIZE));
|
||||
assert_true('valid binary', ctx^.ValidBinary);
|
||||
|
||||
{ Set up locals from the parsed code entry for the single function }
|
||||
ctx^.ExecutionState.Locals := @ctx^.Sections.CodeSection^.Entries[0].Locals;
|
||||
|
||||
ctx^.ExecutionState.Running := true;
|
||||
while wasm.vm.tick(ctx) do;
|
||||
|
||||
assert_bool('execution stopped', ctx^.ExecutionState.Running, false);
|
||||
assert_true('stack has result', ctx^.ExecutionState.Operand_Stack^.Top > 0);
|
||||
assert_i32('10+20=30', popi32(ctx^.ExecutionState.Operand_Stack), 30);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -0,0 +1,68 @@
|
||||
{ E2E test: store 99 to linear memory, load it back = 99
|
||||
Equivalent WAT:
|
||||
(module
|
||||
(memory 1)
|
||||
(func (export "main") (result i32)
|
||||
i32.const 0
|
||||
i32.const 99
|
||||
i32.store offset=0 align=0
|
||||
i32.const 0
|
||||
i32.load offset=0 align=0
|
||||
return
|
||||
)
|
||||
)
|
||||
}
|
||||
unit wasm.test.binary.memroundtrip;
|
||||
|
||||
interface
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
types, console, wasm.types, wasm.types.stack,
|
||||
wasm.parser, wasm.vm, wasm.test.framework;
|
||||
|
||||
const
|
||||
BINARY_SIZE = 53;
|
||||
BINARY : Array[$00..$34] of uint8 = (
|
||||
$00, $61, $73, $6D, { magic }
|
||||
$01, $00, $00, $00, { version 1 }
|
||||
$01, $05, $01, $60, { type section: 1 func type }
|
||||
$00, $01, $7F, { () -> (i32) }
|
||||
$03, $02, $01, $00, { function section: 1 func, type 0 }
|
||||
$05, $03, $01, $00, $01, { memory section: 1 memory, no max, 1 page }
|
||||
$07, $08, $01, $04, { export section: 1 export, name len 4 }
|
||||
$6D, $61, $69, $6E, { "main" }
|
||||
$00, $00, { export type=func, index=0 }
|
||||
$0A, $11, $01, $0F, { code section: len=17, count=1, body=15 }
|
||||
$00, { 0 locals }
|
||||
$41, $00, { i32.const 0 (address) }
|
||||
$41, $63, { i32.const 99 (value) }
|
||||
$36, $00, $00, { i32.store align=0 offset=0 }
|
||||
$41, $00, { i32.const 0 (address) }
|
||||
$28, $00, $00, { i32.load align=0 offset=0 }
|
||||
$0F, $0B { return, end }
|
||||
);
|
||||
|
||||
procedure run;
|
||||
var
|
||||
ctx : PWASMProcessContext;
|
||||
begin
|
||||
test_begin('binary.memroundtrip');
|
||||
|
||||
ctx := wasm.parser.parse(@BINARY[0], puint8(@BINARY[0] + BINARY_SIZE));
|
||||
assert_true('valid binary', ctx^.ValidBinary);
|
||||
|
||||
ctx^.ExecutionState.Running := true;
|
||||
while wasm.vm.tick(ctx) do;
|
||||
|
||||
assert_bool('execution stopped', ctx^.ExecutionState.Running, false);
|
||||
assert_true('stack has result', ctx^.ExecutionState.Operand_Stack^.Top > 0);
|
||||
assert_i32('store+load=99', popi32(ctx^.ExecutionState.Operand_Stack), 99);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -0,0 +1,58 @@
|
||||
{ E2E test: () -> i32, returns 42
|
||||
Equivalent WAT:
|
||||
(module
|
||||
(func (export "main") (result i32)
|
||||
i32.const 42
|
||||
return
|
||||
)
|
||||
)
|
||||
}
|
||||
unit wasm.test.binary.return42;
|
||||
|
||||
interface
|
||||
|
||||
const
|
||||
BINARY_SIZE = $26;
|
||||
BINARY : Array[$0..$25] of uint8 = (
|
||||
$00, $61, $73, $6D, { magic }
|
||||
$01, $00, $00, $00, { version 1 }
|
||||
$01, $05, $01, $60, { type section: 1 func type }
|
||||
$00, $01, $7F, { () -> (i32) }
|
||||
$03, $02, $01, $00, { function section: 1 func, type 0 }
|
||||
$07, $08, $01, $04, { export section: 1 export, name len 4 }
|
||||
$6D, $61, $69, $6E, { "main" }
|
||||
$00, $00, { export type=func, index=0 }
|
||||
$0A, $07, $01, { code section: len=7, count=1 }
|
||||
$05, $00, { body_size=5, 0 locals }
|
||||
$41, $2A, $0F, $0B { i32.const 42, return, end }
|
||||
);
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
types, console, wasm.types, wasm.types.stack,
|
||||
wasm.parser, wasm.vm, wasm.test.framework;
|
||||
|
||||
procedure run;
|
||||
var
|
||||
ctx : PWASMProcessContext;
|
||||
begin
|
||||
test_begin('binary.return42');
|
||||
|
||||
ctx := wasm.parser.parse(@BINARY[0], puint8(@BINARY[0] + BINARY_SIZE));
|
||||
assert_true('valid binary', ctx^.ValidBinary);
|
||||
assert_u32('version=1', ctx^.Version, 1);
|
||||
|
||||
ctx^.ExecutionState.Running := true;
|
||||
while wasm.vm.tick(ctx) do;
|
||||
|
||||
assert_bool('execution stopped', ctx^.ExecutionState.Running, false);
|
||||
assert_true('stack has result', ctx^.ExecutionState.Operand_Stack^.Top > 0);
|
||||
assert_i32('result=42', popi32(ctx^.ExecutionState.Operand_Stack), 42);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -1,22 +0,0 @@
|
||||
unit wasm.test.binary;
|
||||
|
||||
interface
|
||||
|
||||
const
|
||||
TEST_BINARY_1 : Array[$0..$25] of uint8 = (
|
||||
$00, $61, $73, $6D,
|
||||
$01, $00, $00, $00,
|
||||
$01, $05, $01, $60,
|
||||
$00, $01, $7F, $03,
|
||||
$02, $01, $00, $07,
|
||||
$08, $01, $04, $6D,
|
||||
$61, $69, $6E, $00,
|
||||
$00, $0A, $07, $01,
|
||||
$05, $00, $41, $2A,
|
||||
$0F, $0B
|
||||
);
|
||||
|
||||
implementation
|
||||
|
||||
end.
|
||||
|
||||
@@ -142,7 +142,13 @@ uses
|
||||
wasm.test.opcode.i64store32,
|
||||
{ Memory size/grow tests }
|
||||
wasm.test.opcode.memorysize,
|
||||
wasm.test.opcode.memorygrow;
|
||||
wasm.test.opcode.memorygrow,
|
||||
{ End-to-end binary tests }
|
||||
wasm.test.binary.return42,
|
||||
wasm.test.binary.addnums,
|
||||
wasm.test.binary.arithmetic,
|
||||
wasm.test.binary.locals,
|
||||
wasm.test.binary.memroundtrip;
|
||||
|
||||
procedure run_all_tests;
|
||||
begin
|
||||
@@ -302,6 +308,13 @@ begin
|
||||
wasm.test.opcode.memorysize.run;
|
||||
wasm.test.opcode.memorygrow.run;
|
||||
|
||||
{ End-to-end binary tests }
|
||||
wasm.test.binary.return42.run;
|
||||
wasm.test.binary.addnums.run;
|
||||
wasm.test.binary.arithmetic.run;
|
||||
wasm.test.binary.locals.run;
|
||||
wasm.test.binary.memroundtrip.run;
|
||||
|
||||
{ Summary }
|
||||
writestringln('');
|
||||
writestringln('========================================');
|
||||
|
||||
@@ -275,11 +275,38 @@ type
|
||||
end;
|
||||
PWASMCodeSection = ^TWASMCodeSection;
|
||||
|
||||
TWASMGlobalEntry = record
|
||||
ValueType : TWasmValueType;
|
||||
Mutable : boolean;
|
||||
Value : TWASMValueEntry;
|
||||
end;
|
||||
PWASMGlobalEntry = ^TWASMGlobalEntry;
|
||||
|
||||
TWASMGlobals = record
|
||||
GlobalCount : uint32;
|
||||
Globals : PWASMGlobalEntry;
|
||||
end;
|
||||
PWASMGlobals = ^TWASMGlobals;
|
||||
|
||||
TWASMMemoryLimits = record
|
||||
HasMax : boolean;
|
||||
InitialPages : uint32;
|
||||
MaxPages : uint32;
|
||||
end;
|
||||
PWASMMemoryLimits = ^TWASMMemoryLimits;
|
||||
|
||||
TWASMMemorySection = record
|
||||
MemoryCount : uint32;
|
||||
Memories : PWASMMemoryLimits;
|
||||
end;
|
||||
PWASMMemorySection = ^TWASMMemorySection;
|
||||
|
||||
TWASMState = record
|
||||
Code : puint8;
|
||||
Limit : uint32;
|
||||
Locals : PWASMLocals;
|
||||
Memory : PWasmHeap;
|
||||
Globals : PWASMGlobals;
|
||||
Control_Stack : PWASMStack;
|
||||
Operand_Stack : PWASMStack;
|
||||
IP : uint32;
|
||||
@@ -292,6 +319,8 @@ type
|
||||
FunctionSection : PWASMFunctionSection;
|
||||
ExportSection : PWASMExportSection;
|
||||
CodeSection : PWASMCodeSection;
|
||||
MemorySection : PWASMMemorySection;
|
||||
StartIndex : int32; { -1 = no start function }
|
||||
end;
|
||||
|
||||
TWASMProcessContext = record
|
||||
|
||||
+963
-108
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user