Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0a4bd589ee | ||
|
|
eda3669ab2 | ||
|
|
a5b532d0c1 | ||
|
|
a8817e0350 | ||
|
|
b937e60838 | ||
|
|
255ad4806e | ||
|
|
93dfff358d | ||
|
|
3ed5c0a847 |
No files matched your search
@@ -1,3 +1,4 @@
|
|||||||
bin
|
bin
|
||||||
lib
|
lib
|
||||||
AGENTS.md
|
AGENTS.md
|
||||||
|
GAP_ANALYSIS.md
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
@echo off
|
|
||||||
setlocal
|
|
||||||
|
|
||||||
set ROOT=%~dp0
|
|
||||||
set SRC=%ROOT%src
|
|
||||||
set BIN=%ROOT%bin
|
|
||||||
set LIB=%ROOT%lib
|
|
||||||
set FPC=C:\Lazarus\fpc\3.2.2\bin\x86_64-win64\fpc.exe
|
|
||||||
|
|
||||||
set TESTFLAGS=
|
|
||||||
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,93 @@
|
|||||||
|
# build.ps1 — Build script for the WASURO project
|
||||||
|
param(
|
||||||
|
[switch]$Test,
|
||||||
|
[switch]$Clean,
|
||||||
|
[switch]$Run,
|
||||||
|
[switch]$Help,
|
||||||
|
[switch]$Debug
|
||||||
|
)
|
||||||
|
|
||||||
|
# Also accept positional string args (e.g. .\build.ps1 clean test run)
|
||||||
|
foreach ($a in $args) {
|
||||||
|
switch ($a.ToLower()) {
|
||||||
|
'test' { $Test = $true }
|
||||||
|
'clean' { $Clean = $true }
|
||||||
|
'run' { $Run = $true }
|
||||||
|
'debug' { $Debug = $true }
|
||||||
|
'help' { $Help = $true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($Help) {
|
||||||
|
Write-Host 'Usage: .\build.ps1 [options]'
|
||||||
|
Write-Host 'Options:'
|
||||||
|
Write-Host ' test - Build with tests enabled'
|
||||||
|
Write-Host ' clean - Clean build outputs'
|
||||||
|
Write-Host ' run - Run the built executable after building'
|
||||||
|
Write-Host ' debug - Build with debug info and assertions enabled'
|
||||||
|
Write-Host ' help - Show this help message'
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
$Root = $PSScriptRoot
|
||||||
|
$Src = Join-Path $Root 'src'
|
||||||
|
$Bin = Join-Path $Root 'bin'
|
||||||
|
$Lib = Join-Path $Root 'lib'
|
||||||
|
$FPC = 'C:\Lazarus\fpc\3.2.2\bin\x86_64-win64\fpc.exe'
|
||||||
|
|
||||||
|
# ── Clean ────────────────────────────────────────────────────────────
|
||||||
|
if ($Clean) {
|
||||||
|
Write-Host 'Cleaning build outputs...'
|
||||||
|
if (Test-Path $Bin) { Remove-Item $Bin -Recurse -Force }
|
||||||
|
if (Test-Path $Lib) { Remove-Item $Lib -Recurse -Force }
|
||||||
|
Write-Host 'Clean complete.'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-not (Test-Path $Bin)) { New-Item $Bin -ItemType Directory | Out-Null }
|
||||||
|
if (-not (Test-Path $Lib)) { New-Item $Lib -ItemType Directory | Out-Null }
|
||||||
|
|
||||||
|
# ── Lint ─────────────────────────────────────────────────────────────
|
||||||
|
& (Join-Path $Root 'lint.ps1') -SrcRoot $Src
|
||||||
|
if ($LASTEXITCODE -ne 0) { exit 1 }
|
||||||
|
|
||||||
|
# ── Build ────────────────────────────────────────────────────────────
|
||||||
|
$TestFlags = @()
|
||||||
|
if ($Test) { $TestFlags += '-dRUN_TESTS' }
|
||||||
|
if ($Debug) { $TestFlags += '-dDEBUG_OUTPUT' }
|
||||||
|
|
||||||
|
$fpcArgs = @(
|
||||||
|
$TestFlags
|
||||||
|
"-FE$Bin"
|
||||||
|
"-FU$Lib"
|
||||||
|
"-Fu$Src\emu"
|
||||||
|
"-Fu$Src\wasm"
|
||||||
|
"-Fu$Src\wasm\parser"
|
||||||
|
"-Fu$Src\wasm\parser\sections"
|
||||||
|
"-Fu$Src\wasm\test"
|
||||||
|
"-Fu$Src\wasm\test\opcodes"
|
||||||
|
"-Fu$Src\wasm\test\parsers"
|
||||||
|
"-Fu$Src\wasm\test\binaries"
|
||||||
|
"-Fu$Src\wasm\test\infra"
|
||||||
|
"-Fu$Src\wasm\types"
|
||||||
|
"-Fu$Src\wasm\vm"
|
||||||
|
"-Fu$Src\wasm\vm\opcodes"
|
||||||
|
'-gw3'
|
||||||
|
"-o$Bin\WASURO.exe"
|
||||||
|
"$Src\project\WASURO.lpr"
|
||||||
|
)
|
||||||
|
|
||||||
|
Write-Output $fpcArgs
|
||||||
|
|
||||||
|
& $FPC @fpcArgs
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
Write-Host "Build failed with error code $LASTEXITCODE"
|
||||||
|
exit $LASTEXITCODE
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "Build succeeded: $Bin\WASURO.exe"
|
||||||
|
|
||||||
|
# ── Run ──────────────────────────────────────────────────────────────
|
||||||
|
if ($Run) {
|
||||||
|
Write-Host 'Running WASURO...'
|
||||||
|
& "$Bin\WASURO.exe"
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
# lint.ps1 — Static lint rules for the WASURO project
|
# lint.ps1 — Static lint rules for the WASURO project
|
||||||
# Called by build.bat before compilation. Exit 1 on any violation.
|
# Called by build.ps1 before compilation. Exit 1 on any violation.
|
||||||
param(
|
param(
|
||||||
[string]$SrcRoot = (Join-Path $PSScriptRoot 'src')
|
[string]$SrcRoot = (Join-Path $PSScriptRoot 'src')
|
||||||
)
|
)
|
||||||
@@ -43,6 +43,6 @@ if ($ruleHits -gt 0) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# ── Add more rules here ─────────────────────────────────────────────
|
# ── Add more rules here ─────────────────────────────────────────────
|
||||||
|
|
||||||
if ($fail) { exit 1 }
|
if ($fail) { exit 1 }
|
||||||
Write-Host 'Lint passed.'
|
Write-Host 'Lint passed.'
|
||||||
|
exit 0
|
||||||
+43
-18
@@ -5,12 +5,11 @@ uses
|
|||||||
|
|
||||||
console,
|
console,
|
||||||
|
|
||||||
types,
|
wasm.types.builtin,
|
||||||
|
|
||||||
wasm.types.context,
|
wasm.types.context,
|
||||||
|
wasm.types.stack,
|
||||||
wasm.parser,
|
wasm.parser,
|
||||||
wasm.vm, wasm.test.binary.return42,
|
wasm.vm
|
||||||
wasm.types.stack
|
|
||||||
{$IFDEF RUN_TESTS}
|
{$IFDEF RUN_TESTS}
|
||||||
, wasm.test
|
, wasm.test
|
||||||
, wasm.test.framework
|
, wasm.test.framework
|
||||||
@@ -19,6 +18,9 @@ uses
|
|||||||
|
|
||||||
var
|
var
|
||||||
Context : PWASMProcessContext;
|
Context : PWASMProcessContext;
|
||||||
|
ModuleSize : Int64;
|
||||||
|
ModuleBuffer : TWASMPUInt8;
|
||||||
|
ModuleFile : File;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
{$IFDEF RUN_TESTS}
|
{$IFDEF RUN_TESTS}
|
||||||
@@ -26,22 +28,45 @@ begin
|
|||||||
wasm.test.run_all_tests;
|
wasm.test.run_all_tests;
|
||||||
halt(wasm.test.framework.FailedTests);
|
halt(wasm.test.framework.FailedTests);
|
||||||
{$ELSE}
|
{$ELSE}
|
||||||
writestringln('[main] Initializing VM');
|
console.writestringln('WASURO - WebAssembly Runtime in Object Pascal');
|
||||||
wasm.vm.init();
|
wasm.vm.init();
|
||||||
|
if ParamCount > 0 then begin
|
||||||
writestringln('[main] Parsing WASM Binary');
|
if not FileExists(ParamStr(1)) then begin
|
||||||
Context:= wasm.parser.parse(@wasm.test.binary.return42.BINARY[0], puint8(@wasm.test.binary.return42.BINARY[0] + wasm.test.binary.return42.BINARY_SIZE));
|
console.writestringln('File not found.');
|
||||||
|
halt(1);
|
||||||
writestringln('[main] Running...');
|
end;
|
||||||
Context^.ExecutionState.Running:= true;
|
Assign(ModuleFile, ParamStr(1));
|
||||||
while (wasm.vm.tick(Context)) do;
|
Reset(ModuleFile, 1);
|
||||||
writestringln('[main] Finished Execution');
|
ModuleSize := FileSize(ModuleFile);
|
||||||
writestring('[main] Returns: ');
|
GetMem(ModuleBuffer, ModuleSize);
|
||||||
while Context^.ExecutionState.Operand_Stack^.Top > 0 do begin
|
BlockRead(ModuleFile, ModuleBuffer^, ModuleSize);
|
||||||
write(wasm.types.stack.popi32(Context^.ExecutionState.Operand_Stack),', ');
|
Close(ModuleFile);
|
||||||
|
console.writestringln('Module loaded.');
|
||||||
|
Context := wasm.parser.parse(ModuleBuffer, TWASMPUInt8(ModuleBuffer + ModuleSize));
|
||||||
|
while wasm.vm.tick(Context) do ;
|
||||||
|
console.writestringln('Execution finished.');
|
||||||
|
console.writestringln('');
|
||||||
|
console.writestringln('--- VM State ---');
|
||||||
|
writeln('IP: ', Context^.ExecutionState.IP);
|
||||||
|
writeln('Running: ', Context^.ExecutionState.Running);
|
||||||
|
writeln('');
|
||||||
|
writeln('Operand Stack (', Context^.ExecutionState.Operand_Stack^.Top, ' entries):');
|
||||||
|
if Context^.ExecutionState.Operand_Stack^.Top > 0 then
|
||||||
|
wasm.types.stack.walk(Context^.ExecutionState.Operand_Stack)
|
||||||
|
else
|
||||||
|
console.writestringln(' (empty)');
|
||||||
|
writeln('');
|
||||||
|
writeln('Control Stack (', Context^.ExecutionState.Control_Stack^.Top, ' entries):');
|
||||||
|
if Context^.ExecutionState.Control_Stack^.Top > 0 then
|
||||||
|
wasm.types.stack.walk(Context^.ExecutionState.Control_Stack)
|
||||||
|
else
|
||||||
|
console.writestringln(' (empty)');
|
||||||
|
console.writestringln('----------------');
|
||||||
|
FreeMem(ModuleBuffer);
|
||||||
|
end else begin
|
||||||
|
console.writestringln('Usage: WASURO <module.wasm>');
|
||||||
|
halt(1);
|
||||||
end;
|
end;
|
||||||
writeln();
|
|
||||||
while true do sleep(1000);
|
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@@ -72,8 +72,10 @@ var
|
|||||||
totalSize: TWASMUInt32;
|
totalSize: TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Code - Size: ');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
writeintlnWND(section_length, 0);
|
console.writestring('[wasm.parser] Handle Section: Code - Size: ');
|
||||||
|
console.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
// Initialize the read/end pointers
|
// Initialize the read/end pointers
|
||||||
pos:= buffer;
|
pos:= buffer;
|
||||||
@@ -183,8 +185,9 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
walk(ctx);
|
walk(ctx);
|
||||||
|
{$ENDIF}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -12,8 +12,12 @@ implementation
|
|||||||
|
|
||||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Custom - Size: ');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
writeintlnWND(section_length, 0);
|
console.writestring('[wasm.parser] Handle Section: Custom - Size: ');
|
||||||
|
console.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
{ Custom sections are ignored in this implementation }
|
||||||
|
console.writestringln('[wasm.parser] TODO: Custom section skipped.');
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
unit wasm.parser.sections.dataCountSection;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
uses
|
||||||
|
wasm.types.builtin, wasm.types.context;
|
||||||
|
|
||||||
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses console, leb128, lmemorymanager, wasm.types.sections;
|
||||||
|
|
||||||
|
{ DataCount section (ID 12): contains a single u32 count of data segments.
|
||||||
|
Pre-allocates the DataSegments array so that bulk memory opcodes
|
||||||
|
can reference segments by index during validation. }
|
||||||
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos, bend : TWASMPUInt8;
|
||||||
|
bytesRead : TWASMUInt8;
|
||||||
|
dataCount : TWASMUInt32;
|
||||||
|
begin
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
console.writestring('[wasm.parser] Handle Section: DataCount - Size: ');
|
||||||
|
console.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
|
pos := buffer;
|
||||||
|
bend := TWASMPUInt8(buffer + section_length);
|
||||||
|
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @dataCount);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
writestring('[wasm.parser] Data Count: ');
|
||||||
|
writeintlnWND(dataCount, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
|
{ Pre-allocate data segment storage with correct count.
|
||||||
|
The actual segment data will be filled when the Data section is parsed. }
|
||||||
|
ctx^.ExecutionState.DataSegments^.SegmentCount := dataCount;
|
||||||
|
if dataCount > 0 then
|
||||||
|
ctx^.ExecutionState.DataSegments^.Segments := PWASMDataSegment(kalloc(dataCount * sizeof(TWASMDataSegment)))
|
||||||
|
else
|
||||||
|
ctx^.ExecutionState.DataSegments^.Segments := nil;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -36,8 +36,10 @@ var
|
|||||||
i,j : TWASMUInt32;
|
i,j : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Export - Size: ');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
writeintlnWND(section_length, 0);
|
console.writestring('[wasm.parser] Handle Section: Export - Size: ');
|
||||||
|
console.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
// Initialize the read/end pointers
|
// Initialize the read/end pointers
|
||||||
pos:= buffer;
|
pos:= buffer;
|
||||||
@@ -85,8 +87,9 @@ begin
|
|||||||
inc(pos, bytesRead);
|
inc(pos, bytesRead);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
walk(ctx);
|
walk(ctx);
|
||||||
|
{$ENDIF}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -32,8 +32,10 @@ var
|
|||||||
i : TWASMUInt32;
|
i : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Function - Size: ');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
writeintlnWND(section_length, 0);
|
console.writestring('[wasm.parser] Handle Section: Function - Size: ');
|
||||||
|
console.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
// Initialize the read/end pointers
|
// Initialize the read/end pointers
|
||||||
pos:= buffer;
|
pos:= buffer;
|
||||||
@@ -64,8 +66,9 @@ begin
|
|||||||
inc(pos, bytesRead);
|
inc(pos, bytesRead);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
walk(ctx);
|
walk(ctx);
|
||||||
|
{$ENDIF}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -49,9 +49,11 @@ var
|
|||||||
tmpU64 : TWASMUInt64;
|
tmpU64 : TWASMUInt64;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Global - Size: ');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
writeintlnWND(section_length, 0);
|
console.writestring('[wasm.parser] Handle Section: Global - Size: ');
|
||||||
|
console.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
pos := buffer;
|
pos := buffer;
|
||||||
bend := TWASMPUInt8(buffer + section_length);
|
bend := TWASMPUInt8(buffer + section_length);
|
||||||
|
|
||||||
@@ -115,8 +117,9 @@ begin
|
|||||||
if (pos < bend) and (pos^ = $0B) then
|
if (pos < bend) and (pos^ = $0B) then
|
||||||
Inc(pos);
|
Inc(pos);
|
||||||
end;
|
end;
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
walk(ctx);
|
walk(ctx);
|
||||||
|
{$ENDIF}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -39,8 +39,10 @@ var
|
|||||||
flags : TWASMUInt32;
|
flags : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Memory - Size: ');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
writeintlnWND(section_length, 0);
|
console.writestring('[wasm.parser] Handle Section: Memory - Size: ');
|
||||||
|
console.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
pos := buffer;
|
pos := buffer;
|
||||||
bend := TWASMPUInt8(buffer + section_length);
|
bend := TWASMPUInt8(buffer + section_length);
|
||||||
@@ -77,8 +79,9 @@ begin
|
|||||||
while ctx^.ExecutionState.Memory^.PageCount < mem^.InitialPages do
|
while ctx^.ExecutionState.Memory^.PageCount < mem^.InitialPages do
|
||||||
wasm.types.heap.expand_heap(ctx^.ExecutionState.Memory);
|
wasm.types.heap.expand_heap(ctx^.ExecutionState.Memory);
|
||||||
end;
|
end;
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
walk(ctx);
|
walk(ctx);
|
||||||
|
{$ENDIF}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -16,15 +16,19 @@ var
|
|||||||
funcIdx : TWASMUInt32;
|
funcIdx : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Start - Size: ');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
writeintlnWND(section_length, 0);
|
console.writestring('[wasm.parser] Handle Section: Start - Size: ');
|
||||||
|
console.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
{ Read the start function index }
|
{ Read the start function index }
|
||||||
bytesRead := read_leb128_to_uint32(buffer, TWASMPUInt8(buffer + section_length), @funcIdx);
|
bytesRead := read_leb128_to_uint32(buffer, TWASMPUInt8(buffer + section_length), @funcIdx);
|
||||||
ctx^.Sections.StartIndex := TWASMInt32(funcIdx);
|
ctx^.Sections.StartIndex := TWASMInt32(funcIdx);
|
||||||
|
|
||||||
writestring('[wasm.parser] Start Function Index: ');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
writeintlnWND(funcIdx, 0);
|
console.writestring('[wasm.parser] Start Function Index: ');
|
||||||
|
console.writeintlnWND(funcIdx, 0);
|
||||||
|
{$ENDIF}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -4,16 +4,91 @@ interface
|
|||||||
|
|
||||||
uses
|
uses
|
||||||
wasm.types.builtin, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types.context;
|
wasm.types.sections, wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos: TWASMPUInt8;
|
||||||
|
buf_end: TWASMPUInt8;
|
||||||
|
bytesRead: TWASMUInt8;
|
||||||
|
table_count, i, j, min_size, max_size: TWASMUInt32;
|
||||||
|
elem_type, limits_flag: TWASMUInt8;
|
||||||
|
tables: PWASMTables;
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Table - Size: ');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
writeintlnWND(section_length, 0);
|
console.writestring('[wasm.parser] Handle Section: Table - Size: ');
|
||||||
|
console.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
|
pos := buffer;
|
||||||
|
buf_end := buffer;
|
||||||
|
Inc(buf_end, section_length);
|
||||||
|
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, buf_end, @table_count);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
writestring('[wasm.parser] Tables: ');
|
||||||
|
writeintlnWND(table_count, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
|
tables := ctx^.ExecutionState.Tables;
|
||||||
|
tables^.TableCount := table_count;
|
||||||
|
if table_count > 0 then
|
||||||
|
tables^.Tables := PWASMTableInstance(kalloc(sizeof(TWASMTableInstance) * table_count))
|
||||||
|
else
|
||||||
|
tables^.Tables := nil;
|
||||||
|
|
||||||
|
for i := 0 to table_count - 1 do begin
|
||||||
|
elem_type := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
|
||||||
|
limits_flag := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, buf_end, @min_size);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
max_size := 0;
|
||||||
|
if limits_flag = 1 then begin
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, buf_end, @max_size);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
end;
|
||||||
|
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
console.writestring('[wasm.parser] Table ');
|
||||||
|
console.writeintWND(i, 0);
|
||||||
|
console.writestring(' - ElemType: $');
|
||||||
|
console.writeintWND(elem_type, 0);
|
||||||
|
console.writestring(' Initial: ');
|
||||||
|
console.writeintWND(min_size, 0);
|
||||||
|
if limits_flag = 1 then begin
|
||||||
|
console.writestring(' Max: ');
|
||||||
|
console.writeintWND(max_size, 0);
|
||||||
|
end;
|
||||||
|
console.writestringln('');
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
|
tables^.Tables[i].ElementType := elem_type;
|
||||||
|
tables^.Tables[i].Size := min_size;
|
||||||
|
tables^.Tables[i].HasMax := (limits_flag = 1);
|
||||||
|
if limits_flag = 1 then
|
||||||
|
tables^.Tables[i].MaxSize := max_size
|
||||||
|
else
|
||||||
|
tables^.Tables[i].MaxSize := 0;
|
||||||
|
|
||||||
|
{ Allocate elements and initialize to $FFFFFFFF (uninitialized) }
|
||||||
|
if min_size > 0 then begin
|
||||||
|
tables^.Tables[i].Elements := TWASMPUInt32(kalloc(sizeof(TWASMUInt32) * min_size));
|
||||||
|
for j := 0 to min_size - 1 do
|
||||||
|
tables^.Tables[i].Elements[j] := $FFFFFFFF;
|
||||||
|
end else
|
||||||
|
tables^.Tables[i].Elements := nil;
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -59,8 +59,11 @@ var
|
|||||||
i, parsedTypes : TWASMUInt32;
|
i, parsedTypes : TWASMUInt32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Type - Size: ');
|
|
||||||
writeintlnWND(section_length, 0);
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
console.writestring('[wasm.parser] Handle Section: Type - Size: ');
|
||||||
|
console.writeintlnWND(section_length, 0);
|
||||||
|
{$ENDIF}
|
||||||
pos:= buffer;
|
pos:= buffer;
|
||||||
|
|
||||||
// Initialize the type section
|
// Initialize the type section
|
||||||
@@ -118,7 +121,9 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
// Walk the wasm.types.builtin
|
// Walk the wasm.types.builtin
|
||||||
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
walk(ctx);
|
walk(ctx);
|
||||||
|
{$ENDIF}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -36,6 +36,7 @@ begin
|
|||||||
|
|
||||||
// Initialize the sections
|
// Initialize the sections
|
||||||
ctx^.Sections.TypeSection:= nil;
|
ctx^.Sections.TypeSection:= nil;
|
||||||
|
ctx^.Sections.ImportSection:= nil;
|
||||||
ctx^.Sections.FunctionSection:= nil;
|
ctx^.Sections.FunctionSection:= nil;
|
||||||
ctx^.Sections.ExportSection:= nil;
|
ctx^.Sections.ExportSection:= nil;
|
||||||
ctx^.Sections.CodeSection:= nil;
|
ctx^.Sections.CodeSection:= nil;
|
||||||
@@ -47,6 +48,21 @@ begin
|
|||||||
ctx^.ExecutionState.Globals^.GlobalCount:= 0;
|
ctx^.ExecutionState.Globals^.GlobalCount:= 0;
|
||||||
ctx^.ExecutionState.Globals^.Globals:= nil;
|
ctx^.ExecutionState.Globals^.Globals:= nil;
|
||||||
|
|
||||||
|
// Initialize tables (empty until table section is parsed)
|
||||||
|
ctx^.ExecutionState.Tables:= PWASMTables(kalloc(sizeof(TWASMTables)));
|
||||||
|
ctx^.ExecutionState.Tables^.TableCount:= 0;
|
||||||
|
ctx^.ExecutionState.Tables^.Tables:= nil;
|
||||||
|
|
||||||
|
// Initialize data segments (empty until data section is parsed)
|
||||||
|
ctx^.ExecutionState.DataSegments:= PWASMDataSegments(kalloc(sizeof(TWASMDataSegments)));
|
||||||
|
ctx^.ExecutionState.DataSegments^.SegmentCount:= 0;
|
||||||
|
ctx^.ExecutionState.DataSegments^.Segments:= nil;
|
||||||
|
|
||||||
|
// Initialize element segments (empty until element section is parsed)
|
||||||
|
ctx^.ExecutionState.ElementSegments:= PWASMElementSegments(kalloc(sizeof(TWASMElementSegments)));
|
||||||
|
ctx^.ExecutionState.ElementSegments^.SegmentCount:= 0;
|
||||||
|
ctx^.ExecutionState.ElementSegments^.Segments:= nil;
|
||||||
|
|
||||||
newContext:= ctx;
|
newContext:= ctx;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -63,11 +79,15 @@ begin
|
|||||||
ctx:= newContext();
|
ctx:= newContext();
|
||||||
pos:= buffer;
|
pos:= buffer;
|
||||||
|
|
||||||
writestringln('[wasm.parser] Parsing WASM Binary');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
console.writestringln('[wasm.parser] Starting parse of WASM binary.');
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
// Check for the WASM Magic
|
// Check for the WASM Magic
|
||||||
if(TWASMPUInt32(pos)^ = wasm.types.constants.WASM_HDR_MAGIC) then begin
|
if(TWASMPUInt32(pos)^ = wasm.types.constants.WASM_HDR_MAGIC) then begin
|
||||||
writestringln('[wasm.parser] Binary is valid.');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
|
console.writestringln('[wasm.parser] Binary is valid.');
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
// Set binary to valid
|
// Set binary to valid
|
||||||
ctx^.ValidBinary:= true;
|
ctx^.ValidBinary:= true;
|
||||||
@@ -76,8 +96,10 @@ begin
|
|||||||
// Read the version
|
// Read the version
|
||||||
ctx^.Version:= TWASMPUInt32(pos)^;
|
ctx^.Version:= TWASMPUInt32(pos)^;
|
||||||
inc(pos, 4);
|
inc(pos, 4);
|
||||||
writestring('[wasm.parser] Version: ');
|
{$IFDEF DEBUG_OUTPUT}
|
||||||
writeintlnWND(ctx^.Version, 0);
|
console.writestring('[wasm.parser] Version: ');
|
||||||
|
console.writeintlnWND(ctx^.Version, 0);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
// Read the sections
|
// Read the sections
|
||||||
while (pos < buffer_end) do begin
|
while (pos < buffer_end) do begin
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ uses
|
|||||||
wasm.parser.sections.startSection,
|
wasm.parser.sections.startSection,
|
||||||
wasm.parser.sections.elementSection,
|
wasm.parser.sections.elementSection,
|
||||||
wasm.parser.sections.codeSection,
|
wasm.parser.sections.codeSection,
|
||||||
wasm.parser.sections.dataSection;
|
wasm.parser.sections.dataSection,
|
||||||
|
wasm.parser.sections.dataCountSection;
|
||||||
|
|
||||||
procedure handle(sectionId : TWASMUInt8; buffer : TWASMPUInt8; section_length : TWASMUInt32; ctx : PWASMProcessContext);
|
procedure handle(sectionId : TWASMUInt8; buffer : TWASMPUInt8; section_length : TWASMUInt32; ctx : PWASMProcessContext);
|
||||||
|
|
||||||
@@ -50,6 +51,8 @@ begin
|
|||||||
ord(sidCode): wasm.parser.sections.codeSection.handle (buffer, section_length, ctx);
|
ord(sidCode): wasm.parser.sections.codeSection.handle (buffer, section_length, ctx);
|
||||||
// Data Section
|
// Data Section
|
||||||
ord(sidData): wasm.parser.sections.dataSection.handle (buffer, section_length, ctx);
|
ord(sidData): wasm.parser.sections.dataSection.handle (buffer, section_length, ctx);
|
||||||
|
// DataCount Section
|
||||||
|
ord(sidDataCount): wasm.parser.sections.dataCountSection.handle (buffer, section_length, ctx);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|||||||
Loaded 20 of 335 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user