Compare commits
20
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff65a0d5f0 | ||
|
|
76d7d737c0 | ||
|
|
d12ad11746 | ||
|
|
679973c6e9 | ||
|
|
68df3565cb | ||
|
|
862e3254df | ||
|
|
6794c52a77 | ||
|
|
7cc4459dfd | ||
|
|
ae344e7495 | ||
|
|
430ebcd203 | ||
|
|
835f6f86cc | ||
|
|
a5192366ed | ||
|
|
d3cd71072b | ||
|
|
e40d8e9ed5 | ||
|
|
fca24af1b0 | ||
|
|
8c16550bd4 | ||
|
|
9a3b813c96 | ||
|
|
8a4bb2ca35 | ||
|
|
ff28299132 | ||
|
|
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://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,78 @@
|
|||||||
|
@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=
|
||||||
|
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%"
|
||||||
|
|
||||||
|
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%" ^
|
||||||
|
-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
|
||||||
|
|
||||||
|
REM ── Run ────────────────────────────────────────────────────────────
|
||||||
|
if %DO_RUN%==1 (
|
||||||
|
echo Running WASURO...
|
||||||
|
"%BIN%\WASURO.exe"
|
||||||
|
)
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
# lint.ps1 — Static lint rules for the WASURO project
|
||||||
|
# Called by build.bat before compilation. Exit 1 on any violation.
|
||||||
|
param(
|
||||||
|
[string]$SrcRoot = (Join-Path $PSScriptRoot 'src')
|
||||||
|
)
|
||||||
|
|
||||||
|
$fail = $false
|
||||||
|
|
||||||
|
# ── Rule: No native types in src\wasm ────────────────────────────────
|
||||||
|
# All code under src\wasm must use TWASM* aliases from wasm.types.builtin.
|
||||||
|
# Only wasm.types.builtin.pas itself may reference the raw types.
|
||||||
|
$forbiddenTypes = @(
|
||||||
|
'uint8','uint16','uint32','uint64',
|
||||||
|
'sint8','sint16',
|
||||||
|
'int32','int64',
|
||||||
|
'float','double',
|
||||||
|
'puint8','puint16','puint32','puint64',
|
||||||
|
'pfloat','pdouble','pchar',
|
||||||
|
'boolean','char','void','integer'
|
||||||
|
)
|
||||||
|
$pattern = '(?i)(?<![a-z0-9_])(' + ($forbiddenTypes -join '|') + ')(?![a-z0-9_])'
|
||||||
|
|
||||||
|
$files = Get-ChildItem (Join-Path $SrcRoot 'wasm') -Recurse -Filter '*.pas' |
|
||||||
|
Where-Object { $_.Name -ne 'wasm.types.builtin.pas' }
|
||||||
|
|
||||||
|
$ruleHits = 0
|
||||||
|
foreach ($file in $files) {
|
||||||
|
$content = [IO.File]::ReadAllText($file.FullName)
|
||||||
|
$matches = [regex]::Matches($content, $pattern)
|
||||||
|
if ($matches.Count -gt 0) {
|
||||||
|
$found = @{}
|
||||||
|
foreach ($m in $matches) { $found[$m.Value.ToLower()] = $true }
|
||||||
|
$list = ($found.Keys | Sort-Object) -join ', '
|
||||||
|
Write-Host " $($file.Name): $list"
|
||||||
|
$ruleHits++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($ruleHits -gt 0) {
|
||||||
|
Write-Host ''
|
||||||
|
Write-Host "Lint failed: $ruleHits file(s) use native types. Use TWASM* from wasm.types.builtin instead."
|
||||||
|
$fail = $true
|
||||||
|
}
|
||||||
|
|
||||||
|
# ── Add more rules here ─────────────────────────────────────────────
|
||||||
|
|
||||||
|
if ($fail) { exit 1 }
|
||||||
|
Write-Host 'Lint passed.'
|
||||||
@@ -7,9 +7,9 @@ uses
|
|||||||
|
|
||||||
types,
|
types,
|
||||||
|
|
||||||
wasm.types,
|
wasm.types.context,
|
||||||
wasm.parser,
|
wasm.parser,
|
||||||
wasm.vm, wasm.test.binary,
|
wasm.vm, wasm.test.binary.return42,
|
||||||
wasm.types.stack
|
wasm.types.stack
|
||||||
{$IFDEF RUN_TESTS}
|
{$IFDEF RUN_TESTS}
|
||||||
, wasm.test
|
, wasm.test
|
||||||
@@ -30,7 +30,7 @@ begin
|
|||||||
wasm.vm.init();
|
wasm.vm.init();
|
||||||
|
|
||||||
writestringln('[main] Parsing WASM Binary');
|
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...');
|
writestringln('[main] Running...');
|
||||||
Context^.ExecutionState.Running:= true;
|
Context^.ExecutionState.Running:= true;
|
||||||
|
|||||||
@@ -3,16 +3,16 @@ unit wasm.parser.sections.codeSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types.enums, wasm.types.values, wasm.types.sections, wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure printCtxCode(ctx : PWASMProcessContext);
|
procedure printCtxCode(ctx : PWASMProcessContext);
|
||||||
var
|
var
|
||||||
i : uint32;
|
i : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Flat Code: ');
|
writestring('[wasm.parser] Flat Code: ');
|
||||||
@@ -25,15 +25,15 @@ end;
|
|||||||
|
|
||||||
procedure walk(ctx : PWASMProcessContext);
|
procedure walk(ctx : PWASMProcessContext);
|
||||||
var
|
var
|
||||||
localpos, pos, bend : puint8;
|
localpos, pos, bend : TWASMPUInt8;
|
||||||
bytesRead : uint8;
|
bytesRead : TWASMUInt8;
|
||||||
currentEntry : PWASMCodeEntry;
|
currentEntry : PWASMCodeEntry;
|
||||||
i,j,l : uint32;
|
i,j,l : TWASMUInt32;
|
||||||
localCount,localTemp,localIndex : uint32;
|
localCount,localTemp,localIndex : TWASMUInt32;
|
||||||
localType: TWasmValueType;
|
localType: TWasmValueType;
|
||||||
codeLength: uint32;
|
codeLength: TWASMUInt32;
|
||||||
codeOffset: uint32;
|
codeOffset: TWASMUInt32;
|
||||||
totalSize: uint32;
|
totalSize: TWASMUInt32;
|
||||||
begin
|
begin
|
||||||
// Walk the code entries
|
// Walk the code entries
|
||||||
for i:=1 to ctx^.Sections.CodeSection^.CodeCount do begin
|
for i:=1 to ctx^.Sections.CodeSection^.CodeCount do begin
|
||||||
@@ -59,17 +59,17 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
var
|
var
|
||||||
localpos, pos, bend : puint8;
|
localpos, pos, bend : TWASMPUInt8;
|
||||||
bytesRead : uint8;
|
bytesRead : TWASMUInt8;
|
||||||
currentEntry : PWASMCodeEntry;
|
currentEntry : PWASMCodeEntry;
|
||||||
i,j,l : uint32;
|
i,j,l : TWASMUInt32;
|
||||||
localCount,localTemp,localIndex : uint32;
|
localCount,localTemp,localIndex : TWASMUInt32;
|
||||||
localType: TWasmValueType;
|
localType: TWasmValueType;
|
||||||
codeLength: uint32;
|
codeLength: TWASMUInt32;
|
||||||
codeOffset: uint32;
|
codeOffset: TWASMUInt32;
|
||||||
totalSize: uint32;
|
totalSize: TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Code - Size: ');
|
writestring('[wasm.parser] Handle Section: Code - Size: ');
|
||||||
@@ -77,7 +77,7 @@ begin
|
|||||||
|
|
||||||
// Initialize the read/end pointers
|
// Initialize the read/end pointers
|
||||||
pos:= buffer;
|
pos:= buffer;
|
||||||
bend:= puint8(pos + section_length);
|
bend:= TWASMPUInt8(pos + section_length);
|
||||||
|
|
||||||
// Initialize the code section
|
// Initialize the code section
|
||||||
ctx^.Sections.CodeSection:= PWASMCodeSection(kalloc(sizeof(TWASMCodeSection)));
|
ctx^.Sections.CodeSection:= PWASMCodeSection(kalloc(sizeof(TWASMCodeSection)));
|
||||||
@@ -158,7 +158,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
// Read the code
|
// Read the code
|
||||||
currentEntry^.Code:= puint8(kalloc(currentEntry^.CodeLength));
|
currentEntry^.Code:= TWASMPUInt8(kalloc(currentEntry^.CodeLength));
|
||||||
for j:=1 to currentEntry^.CodeLength do begin
|
for j:=1 to currentEntry^.CodeLength do begin
|
||||||
currentEntry^.Code[j - 1]:= pos^;
|
currentEntry^.Code[j - 1]:= pos^;
|
||||||
inc(pos);
|
inc(pos);
|
||||||
@@ -171,12 +171,12 @@ begin
|
|||||||
totalSize:= totalSize + currentEntry^.CodeLength;
|
totalSize:= totalSize + currentEntry^.CodeLength;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
ctx^.ExecutionState.Code:= puint8(kalloc(totalSize));
|
ctx^.ExecutionState.Code:= TWASMPUInt8(kalloc(totalSize));
|
||||||
ctx^.ExecutionState.Limit:= totalSize;
|
ctx^.ExecutionState.Limit:= totalSize;
|
||||||
localpos:= ctx^.ExecutionState.Code;
|
localpos:= ctx^.ExecutionState.Code;
|
||||||
for i:=1 to ctx^.Sections.CodeSection^.CodeCount do begin
|
for i:=1 to ctx^.Sections.CodeSection^.CodeCount do begin
|
||||||
currentEntry:= @ctx^.Sections.CodeSection^.Entries[i - 1];
|
currentEntry:= @ctx^.Sections.CodeSection^.Entries[i - 1];
|
||||||
currentEntry^.CodeIndex:= uint32(localpos - ctx^.ExecutionState.Code);
|
currentEntry^.CodeIndex:= TWASMUInt32(localpos - ctx^.ExecutionState.Code);
|
||||||
for j:=1 to currentEntry^.CodeLength do begin
|
for j:=1 to currentEntry^.CodeLength do begin
|
||||||
localpos^:= currentEntry^.Code[j - 1];
|
localpos^:= currentEntry^.Code[j - 1];
|
||||||
inc(localpos);
|
inc(localpos);
|
||||||
|
|||||||
@@ -3,14 +3,14 @@ unit wasm.parser.sections.customSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Custom - Size: ');
|
writestring('[wasm.parser] Handle Section: Custom - Size: ');
|
||||||
writeintlnWND(section_length, 0);
|
writeintlnWND(section_length, 0);
|
||||||
|
|||||||
@@ -3,17 +3,77 @@ unit wasm.parser.sections.dataSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types.context, wasm.types.heap;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos, bend : TWASMPUInt8;
|
||||||
|
bytesRead : TWASMUInt8;
|
||||||
|
segCount : TWASMUInt32;
|
||||||
|
i, j : TWASMUInt32;
|
||||||
|
memIdx : TWASMUInt32;
|
||||||
|
offset : TWASMUInt32;
|
||||||
|
dataSize : TWASMUInt32;
|
||||||
|
opcode : TWASMUInt8;
|
||||||
|
tmpU32 : TWASMUInt32;
|
||||||
|
|
||||||
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 := TWASMPUInt8(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.
|
||||||
@@ -3,14 +3,14 @@ unit wasm.parser.sections.elementSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Element - Size: ');
|
writestring('[wasm.parser] Handle Section: Element - Size: ');
|
||||||
writeintlnWND(section_length, 0);
|
writeintlnWND(section_length, 0);
|
||||||
|
|||||||
@@ -3,17 +3,17 @@ unit wasm.parser.sections.exportSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types.enums, wasm.types.sections, wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure walk(ctx : PWASMProcessContext);
|
procedure walk(ctx : PWASMProcessContext);
|
||||||
var
|
var
|
||||||
currentEntry : PWASMExportEntry;
|
currentEntry : PWASMExportEntry;
|
||||||
i : uint32;
|
i : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
// Walk the export entries
|
// Walk the export entries
|
||||||
@@ -28,12 +28,12 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
var
|
var
|
||||||
pos, bend : puint8;
|
pos, bend : TWASMPUInt8;
|
||||||
bytesRead : uint8;
|
bytesRead : TWASMUInt8;
|
||||||
currentEntry : PWASMExportEntry;
|
currentEntry : PWASMExportEntry;
|
||||||
i,j : uint32;
|
i,j : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Export - Size: ');
|
writestring('[wasm.parser] Handle Section: Export - Size: ');
|
||||||
@@ -41,7 +41,7 @@ begin
|
|||||||
|
|
||||||
// Initialize the read/end pointers
|
// Initialize the read/end pointers
|
||||||
pos:= buffer;
|
pos:= buffer;
|
||||||
bend:= puint8(pos + section_length);
|
bend:= TWASMPUInt8(pos + section_length);
|
||||||
|
|
||||||
// Initialize the export section
|
// Initialize the export section
|
||||||
ctx^.Sections.ExportSection:= PWASMExportSection(kalloc(sizeof(TWASMExportSection)));
|
ctx^.Sections.ExportSection:= PWASMExportSection(kalloc(sizeof(TWASMExportSection)));
|
||||||
@@ -69,12 +69,12 @@ begin
|
|||||||
inc(pos, bytesRead);
|
inc(pos, bytesRead);
|
||||||
|
|
||||||
// Read the export name
|
// Read the export name
|
||||||
currentEntry^.Name:= pchar(kalloc(currentEntry^.NameLength + 1));
|
currentEntry^.Name:= TWASMPChar(kalloc(currentEntry^.NameLength + 1));
|
||||||
for j:=0 to currentEntry^.NameLength - 1 do begin
|
for j:=0 to currentEntry^.NameLength - 1 do begin
|
||||||
currentEntry^.Name[j]:= char(pos^);
|
currentEntry^.Name[j]:= TWASMChar(pos^);
|
||||||
inc(pos);
|
inc(pos);
|
||||||
end;
|
end;
|
||||||
currentEntry^.Name[currentEntry^.NameLength]:= char($00);
|
currentEntry^.Name[currentEntry^.NameLength]:= TWASMChar($00);
|
||||||
|
|
||||||
// Read the export type
|
// Read the export type
|
||||||
currentEntry^.ExportType:= TWasmExportType(pos^);
|
currentEntry^.ExportType:= TWasmExportType(pos^);
|
||||||
|
|||||||
@@ -3,17 +3,17 @@ unit wasm.parser.sections.functionSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types.sections, wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure walk(ctx : PWASMProcessContext);
|
procedure walk(ctx : PWASMProcessContext);
|
||||||
var
|
var
|
||||||
currentFunc : PWASMFunction;
|
currentFunc : PWASMFunction;
|
||||||
i : uint32;
|
i : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
// Walk the functions
|
// Walk the functions
|
||||||
@@ -24,12 +24,12 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
var
|
var
|
||||||
pos, bend : puint8;
|
pos, bend : TWASMPUInt8;
|
||||||
bytesRead : uint8;
|
bytesRead : TWASMUInt8;
|
||||||
currentFunc : PWASMFunction;
|
currentFunc : PWASMFunction;
|
||||||
i : uint32;
|
i : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Function - Size: ');
|
writestring('[wasm.parser] Handle Section: Function - Size: ');
|
||||||
@@ -37,7 +37,7 @@ begin
|
|||||||
|
|
||||||
// Initialize the read/end pointers
|
// Initialize the read/end pointers
|
||||||
pos:= buffer;
|
pos:= buffer;
|
||||||
bend:= puint8(pos + section_length);
|
bend:= TWASMPUInt8(pos + section_length);
|
||||||
|
|
||||||
// Initialize the function section
|
// Initialize the function section
|
||||||
ctx^.Sections.FunctionSection:= PWASMFunctionSection(kalloc(sizeof(TWASMFunctionSection)));
|
ctx^.Sections.FunctionSection:= PWASMFunctionSection(kalloc(sizeof(TWASMFunctionSection)));
|
||||||
|
|||||||
@@ -3,17 +3,120 @@ unit wasm.parser.sections.globalSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types.enums, wasm.types.sections, wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure walk(ctx : PWASMProcessContext);
|
||||||
|
var
|
||||||
|
i : TWASMUInt32;
|
||||||
|
entry : PWASMGlobalEntry;
|
||||||
|
begin
|
||||||
|
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: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos, bend : TWASMPUInt8;
|
||||||
|
bytesRead : TWASMUInt8;
|
||||||
|
globalCount : TWASMUInt32;
|
||||||
|
i : TWASMUInt32;
|
||||||
|
entry : PWASMGlobalEntry;
|
||||||
|
valType : TWASMUInt8;
|
||||||
|
mutFlag : TWASMUInt8;
|
||||||
|
opcode : TWASMUInt8;
|
||||||
|
tmpU32 : TWASMUInt32;
|
||||||
|
tmpU64 : TWASMUInt64;
|
||||||
|
|
||||||
begin
|
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 := TWASMPUInt8(buffer + section_length);
|
||||||
|
|
||||||
|
{ Read global count }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @globalCount);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
{ Allocate globals on ExecutionState }
|
||||||
|
ctx^.ExecutionState.Globals := PWASMGlobals(kalloc(sizeof(TWASMGlobals)));
|
||||||
|
ctx^.ExecutionState.Globals^.GlobalCount := globalCount;
|
||||||
|
ctx^.ExecutionState.Globals^.Globals := PWASMGlobalEntry(kalloc(sizeof(TWASMGlobalEntry) * globalCount));
|
||||||
|
|
||||||
|
for i := 0 to globalCount - 1 do begin
|
||||||
|
entry := @ctx^.ExecutionState.Globals^.Globals[i];
|
||||||
|
|
||||||
|
{ Read value type }
|
||||||
|
valType := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
entry^.ValueType := TWasmValueType(valType);
|
||||||
|
|
||||||
|
{ Read mutability }
|
||||||
|
mutFlag := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
entry^.Mutable := (mutFlag = 1);
|
||||||
|
|
||||||
|
{ Initialize value to zero }
|
||||||
|
entry^.Value.i64Value := 0;
|
||||||
|
entry^.Value.ValueType := entry^.ValueType;
|
||||||
|
|
||||||
|
{ Evaluate constant init expression }
|
||||||
|
opcode := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
case opcode of
|
||||||
|
$41: begin { i32.const }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @tmpU32);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
entry^.Value.i32Value := TWASMInt32(tmpU32);
|
||||||
|
end;
|
||||||
|
$42: begin { i64.const }
|
||||||
|
bytesRead := read_leb128_to_uint64(pos, bend, @tmpU64);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
entry^.Value.i64Value := TWASMInt64(tmpU64);
|
||||||
|
end;
|
||||||
|
$43: begin { f32.const }
|
||||||
|
entry^.Value.f32Value := TWASMPFloat(pos)^;
|
||||||
|
Inc(pos, 4);
|
||||||
|
end;
|
||||||
|
$44: begin { f64.const }
|
||||||
|
entry^.Value.f64Value := TWASMPDouble(pos)^;
|
||||||
|
Inc(pos, 8);
|
||||||
|
end;
|
||||||
|
else
|
||||||
|
writestring('[wasm.parser] Warning: unsupported global init opcode: ');
|
||||||
|
writehexpair(opcode);
|
||||||
|
writestringln(' ');
|
||||||
|
{ Skip to end opcode }
|
||||||
|
while (pos < bend) and (pos^ <> $0B) do Inc(pos);
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ Consume the end ($0B) opcode }
|
||||||
|
if (pos < bend) and (pos^ = $0B) then
|
||||||
|
Inc(pos);
|
||||||
|
end;
|
||||||
|
|
||||||
|
walk(ctx);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -3,14 +3,14 @@ unit wasm.parser.sections.importSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Import - Size: ');
|
writestring('[wasm.parser] Handle Section: Import - Size: ');
|
||||||
writeintlnWND(section_length, 0);
|
writeintlnWND(section_length, 0);
|
||||||
|
|||||||
@@ -3,17 +3,82 @@ unit wasm.parser.sections.memorySection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types.sections, wasm.types.context, wasm.types.heap;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure walk(ctx : PWASMProcessContext);
|
||||||
|
var
|
||||||
|
i : TWASMUInt32;
|
||||||
|
mem : PWASMMemoryLimits;
|
||||||
|
begin
|
||||||
|
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: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos, bend : TWASMPUInt8;
|
||||||
|
bytesRead : TWASMUInt8;
|
||||||
|
memCount : TWASMUInt32;
|
||||||
|
i : TWASMUInt32;
|
||||||
|
mem : PWASMMemoryLimits;
|
||||||
|
flags : TWASMUInt32;
|
||||||
|
|
||||||
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 := TWASMPUInt8(buffer + section_length);
|
||||||
|
|
||||||
|
{ Read memory count }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @memCount);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
{ Allocate memory section }
|
||||||
|
ctx^.Sections.MemorySection := PWASMMemorySection(kalloc(sizeof(TWASMMemorySection)));
|
||||||
|
ctx^.Sections.MemorySection^.MemoryCount := memCount;
|
||||||
|
ctx^.Sections.MemorySection^.Memories := PWASMMemoryLimits(kalloc(sizeof(TWASMMemoryLimits) * memCount));
|
||||||
|
|
||||||
|
for i := 0 to memCount - 1 do begin
|
||||||
|
mem := @ctx^.Sections.MemorySection^.Memories[i];
|
||||||
|
|
||||||
|
{ Read limits flags: 0 = no max, 1 = has max }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @flags);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
mem^.HasMax := (flags = 1);
|
||||||
|
|
||||||
|
{ Read initial page count }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @mem^.InitialPages);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
{ Read max page count if present }
|
||||||
|
if mem^.HasMax then begin
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @mem^.MaxPages);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
end else
|
||||||
|
mem^.MaxPages := $FFFF; { no limit }
|
||||||
|
|
||||||
|
{ Expand heap to match initial pages (heap starts with 1 page) }
|
||||||
|
while ctx^.ExecutionState.Memory^.PageCount < mem^.InitialPages do
|
||||||
|
wasm.types.heap.expand_heap(ctx^.ExecutionState.Memory);
|
||||||
|
end;
|
||||||
|
|
||||||
|
walk(ctx);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -3,17 +3,28 @@ unit wasm.parser.sections.startSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
bytesRead : TWASMUInt8;
|
||||||
|
funcIdx : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Start - Size: ');
|
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, TWASMPUInt8(buffer + section_length), @funcIdx);
|
||||||
|
ctx^.Sections.StartIndex := TWASMInt32(funcIdx);
|
||||||
|
|
||||||
|
writestring('[wasm.parser] Start Function Index: ');
|
||||||
|
writeintlnWND(funcIdx, 0);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -3,14 +3,14 @@ unit wasm.parser.sections.tableSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Table - Size: ');
|
writestring('[wasm.parser] Handle Section: Table - Size: ');
|
||||||
writeintlnWND(section_length, 0);
|
writeintlnWND(section_length, 0);
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ unit wasm.parser.sections.typeSection;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types.enums, wasm.types.values, wasm.types.sections, wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
@@ -14,10 +14,10 @@ procedure walk(ctx : PWASMProcessContext);
|
|||||||
var
|
var
|
||||||
currentType : PWASMType;
|
currentType : PWASMType;
|
||||||
currentParam : PWASMParam;
|
currentParam : PWASMParam;
|
||||||
i,j : uint32;
|
i,j : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
// Walk the types
|
// Walk the wasm.types.builtin
|
||||||
for i:=1 to ctx^.Sections.TypeSection^.TypeCount do begin
|
for i:=1 to ctx^.Sections.TypeSection^.TypeCount do begin
|
||||||
currentType:= @ctx^.Sections.TypeSection^.Types[i - 1];
|
currentType:= @ctx^.Sections.TypeSection^.Types[i - 1];
|
||||||
writestring('[wasm.parser] Index: ');
|
writestring('[wasm.parser] Index: ');
|
||||||
@@ -50,13 +50,13 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
var
|
var
|
||||||
pos : puint8;
|
pos : TWASMPUInt8;
|
||||||
bytesRead : uint8;
|
bytesRead : TWASMUInt8;
|
||||||
currentType : PWASMType;
|
currentType : PWASMType;
|
||||||
currentParam : PWASMParam;
|
currentParam : PWASMParam;
|
||||||
i, parsedTypes : uint32;
|
i, parsedTypes : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Type - Size: ');
|
writestring('[wasm.parser] Handle Section: Type - Size: ');
|
||||||
@@ -67,31 +67,31 @@ begin
|
|||||||
ctx^.Sections.TypeSection:= PWASMTypeSection(kalloc(sizeof(TWASMTypeSection)));
|
ctx^.Sections.TypeSection:= PWASMTypeSection(kalloc(sizeof(TWASMTypeSection)));
|
||||||
ctx^.Sections.TypeSection^.TypeCount:= 0;
|
ctx^.Sections.TypeSection^.TypeCount:= 0;
|
||||||
|
|
||||||
// Read the number of function types
|
// Read the number of function wasm.types.builtin
|
||||||
bytesRead:= read_leb128_to_uint32(buffer, puint8(buffer + section_length), @ctx^.Sections.TypeSection^.TypeCount);
|
bytesRead:= read_leb128_to_uint32(buffer, TWASMPUInt8(buffer + section_length), @ctx^.Sections.TypeSection^.TypeCount);
|
||||||
inc(pos, bytesRead);
|
inc(pos, bytesRead);
|
||||||
|
|
||||||
// Read the function types
|
// Read the function wasm.types.builtin
|
||||||
if(ctx^.Sections.TypeSection^.TypeCount > 0) then begin
|
if(ctx^.Sections.TypeSection^.TypeCount > 0) then begin
|
||||||
// Allocate the first type
|
// Allocate the first type
|
||||||
ctx^.Sections.TypeSection^.Types:= PWASMType(kalloc(sizeof(TWASMType) * ctx^.Sections.TypeSection^.TypeCount));
|
ctx^.Sections.TypeSection^.Types:= PWASMType(kalloc(sizeof(TWASMType) * ctx^.Sections.TypeSection^.TypeCount));
|
||||||
|
|
||||||
// Read the function types
|
// Read the function wasm.types.builtin
|
||||||
for parsedTypes:=1 to ctx^.Sections.TypeSection^.TypeCount do begin
|
for parsedTypes:=1 to ctx^.Sections.TypeSection^.TypeCount do begin
|
||||||
currentType:= @ctx^.Sections.TypeSection^.Types[parsedTypes - 1];
|
currentType:= @ctx^.Sections.TypeSection^.Types[parsedTypes - 1];
|
||||||
|
|
||||||
// Check if we are at the end of the buffer
|
// Check if we are at the end of the buffer
|
||||||
if pos >= puint8(buffer + section_length) then Exit;
|
if pos >= TWASMPUInt8(buffer + section_length) then Exit;
|
||||||
|
|
||||||
// Read the function type
|
// Read the function type
|
||||||
currentType^._type:= pos^;
|
currentType^._type:= pos^;
|
||||||
inc(pos);
|
inc(pos);
|
||||||
|
|
||||||
// Read the number of parameters
|
// Read the number of parameters
|
||||||
bytesRead:= read_leb128_to_uint32(pos, puint8(buffer + section_length), @currentType^.ParamCount);
|
bytesRead:= read_leb128_to_uint32(pos, TWASMPUInt8(buffer + section_length), @currentType^.ParamCount);
|
||||||
inc(pos, bytesRead);
|
inc(pos, bytesRead);
|
||||||
|
|
||||||
// Read the parameter types
|
// Read the parameter wasm.types.builtin
|
||||||
if(currentType^.ParamCount > 0) then begin
|
if(currentType^.ParamCount > 0) then begin
|
||||||
currentType^.ParamTypes:= PWASMParam(kalloc(sizeof(TWASMParam) * currentType^.ParamCount));
|
currentType^.ParamTypes:= PWASMParam(kalloc(sizeof(TWASMParam) * currentType^.ParamCount));
|
||||||
for i:=1 to currentType^.ParamCount do begin
|
for i:=1 to currentType^.ParamCount do begin
|
||||||
@@ -102,10 +102,10 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
// Read the number of return values
|
// Read the number of return values
|
||||||
bytesRead:= read_leb128_to_uint32(pos, puint8(buffer + section_length), @currentType^.ReturnCount);
|
bytesRead:= read_leb128_to_uint32(pos, TWASMPUInt8(buffer + section_length), @currentType^.ReturnCount);
|
||||||
inc(pos, bytesRead);
|
inc(pos, bytesRead);
|
||||||
|
|
||||||
// Read the return types
|
// Read the return wasm.types.builtin
|
||||||
if(currentType^.ReturnCount > 0) then begin
|
if(currentType^.ReturnCount > 0) then begin
|
||||||
currentType^.ReturnTypes:= PWASMParam(kalloc(sizeof(TWASMParam) * currentType^.ReturnCount));
|
currentType^.ReturnTypes:= PWASMParam(kalloc(sizeof(TWASMParam) * currentType^.ReturnCount));
|
||||||
for i:=1 to currentType^.ReturnCount do begin
|
for i:=1 to currentType^.ReturnCount do begin
|
||||||
@@ -117,7 +117,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Walk the types
|
// Walk the wasm.types.builtin
|
||||||
walk(ctx);
|
walk(ctx);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ unit wasm.parser;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types, wasm.types.heap, wasm.types.stack,
|
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: puint8; buffer_end: puint8) : PWASMProcessContext;
|
function parse(buffer: TWASMPUInt8; buffer_end: TWASMPUInt8) : PWASMProcessContext;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
@@ -39,17 +39,24 @@ 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;
|
||||||
|
|
||||||
function parse(buffer: puint8; buffer_end: puint8): PWASMProcessContext;
|
function parse(buffer: TWASMPUInt8; buffer_end: TWASMPUInt8): PWASMProcessContext;
|
||||||
var
|
var
|
||||||
ctx : PWASMProcessContext;
|
ctx : PWASMProcessContext;
|
||||||
pos : puint8;
|
pos : TWASMPUInt8;
|
||||||
bytesRead : uint8;
|
bytesRead : TWASMUInt8;
|
||||||
section_id : uint8;
|
section_id : TWASMUInt8;
|
||||||
section_length : uint32;
|
section_length : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
// Allocate the context
|
// Allocate the context
|
||||||
@@ -59,7 +66,7 @@ begin
|
|||||||
writestringln('[wasm.parser] Parsing WASM Binary');
|
writestringln('[wasm.parser] Parsing WASM Binary');
|
||||||
|
|
||||||
// Check for the WASM Magic
|
// Check for the WASM Magic
|
||||||
if(puint32(pos)^ = wasm.types.WASM_HDR_MAGIC) then begin
|
if(TWASMPUInt32(pos)^ = wasm.types.constants.WASM_HDR_MAGIC) then begin
|
||||||
writestringln('[wasm.parser] Binary is valid.');
|
writestringln('[wasm.parser] Binary is valid.');
|
||||||
|
|
||||||
// Set binary to valid
|
// Set binary to valid
|
||||||
@@ -67,7 +74,7 @@ begin
|
|||||||
inc(pos, 4);
|
inc(pos, 4);
|
||||||
|
|
||||||
// Read the version
|
// Read the version
|
||||||
ctx^.Version:= puint32(pos)^;
|
ctx^.Version:= TWASMPUInt32(pos)^;
|
||||||
inc(pos, 4);
|
inc(pos, 4);
|
||||||
writestring('[wasm.parser] Version: ');
|
writestring('[wasm.parser] Version: ');
|
||||||
writeintlnWND(ctx^.Version, 0);
|
writeintlnWND(ctx^.Version, 0);
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ unit wasm.parser.sections;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
types,
|
wasm.types.builtin,
|
||||||
wasm.types,
|
wasm.types.enums, wasm.types.context,
|
||||||
wasm.parser.sections.customSection,
|
wasm.parser.sections.customSection,
|
||||||
wasm.parser.sections.typeSection,
|
wasm.parser.sections.typeSection,
|
||||||
wasm.parser.sections.importSection,
|
wasm.parser.sections.importSection,
|
||||||
@@ -18,11 +18,11 @@ uses
|
|||||||
wasm.parser.sections.codeSection,
|
wasm.parser.sections.codeSection,
|
||||||
wasm.parser.sections.dataSection;
|
wasm.parser.sections.dataSection;
|
||||||
|
|
||||||
procedure handle(sectionId : uint8; buffer : puint8; section_length : uint32; ctx : PWASMProcessContext);
|
procedure handle(sectionId : TWASMUInt8; buffer : TWASMPUInt8; section_length : TWASMUInt32; ctx : PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(sectionId : uint8; buffer : puint8; section_length : uint32; ctx : PWASMProcessContext);
|
procedure handle(sectionId : TWASMUInt8; buffer : TWASMPUInt8; section_length : TWASMUInt32; ctx : PWASMProcessContext);
|
||||||
begin
|
begin
|
||||||
// Handle the section
|
// Handle the section
|
||||||
case sectionId of
|
case sectionId of
|
||||||
|
|||||||
@@ -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
|
||||||
|
wasm.types.builtin, console, wasm.types.context, wasm.types.stack,
|
||||||
|
wasm.parser, wasm.vm, wasm.test.framework;
|
||||||
|
|
||||||
|
const
|
||||||
|
BINARY_SIZE = 41;
|
||||||
|
BINARY : Array[$00..$28] of TWASMUInt8 = (
|
||||||
|
$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], TWASMPUInt8(@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.
|
||||||
Loaded 20 of 168 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user