-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwat.ps1
More file actions
158 lines (131 loc) · 4.69 KB
/
wat.ps1
File metadata and controls
158 lines (131 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<#
.SYNOPSIS
Compile all .wat files under wat\ to .wasm in bin\, then run each
through WASURO.exe and assert the expected output.
.DESCRIPTION
Assertion metadata is embedded in the .wat source as comments:
;; @expect-exit <code>
;; @expect-stdout <text>
Multiple @expect-stdout lines are joined with newlines.
If no @expect-exit is specified, exit code 0 is assumed.
.EXAMPLE
.\wat.ps1
.\wat.ps1 -Verbose
#>
[CmdletBinding()]
param()
$ErrorActionPreference = 'Continue'
$Root = Split-Path -Parent $MyInvocation.MyCommand.Path
$WatDir = Join-Path $Root 'wat'
$BinDir = Join-Path $Root 'bin'
$Exe = Join-Path $BinDir 'WASURO.exe'
# ---------- pre-flight checks ----------
if (-not (Get-Command 'wat2wasm' -ErrorAction SilentlyContinue)) {
Write-Host 'Error: wat2wasm not found in PATH. Install wabt first.' -ForegroundColor Red
exit 1
}
if (-not (Test-Path $Exe)) {
Write-Host "Error: $Exe not found. Run .\build.ps1 first." -ForegroundColor Red
exit 1
}
if (-not (Test-Path $WatDir)) {
Write-Host "Error: $WatDir directory not found." -ForegroundColor Red
exit 1
}
# ---------- helpers ----------
function Parse-Expectations {
param([string]$WatPath)
$exit = 0
$stdout = @()
foreach ($line in (Get-Content $WatPath)) {
if ($line -match '^\s*;;\s*@expect-exit\s+(\d+)') {
$exit = [int]$Matches[1]
}
elseif ($line -match '^\s*;;\s*@expect-stdout\s+(.*)$') {
$stdout += $Matches[1]
}
}
return @{
ExitCode = $exit
Stdout = ($stdout -join "`n")
}
}
# ---------- main ----------
$watFiles = Get-ChildItem -Path $WatDir -Recurse -Filter '*.wat'
if ($watFiles.Count -eq 0) {
Write-Host 'No .wat files found.' -ForegroundColor Yellow
exit 0
}
Write-Host '========================================' -ForegroundColor Cyan
Write-Host ' WASURO WAT Test Runner' -ForegroundColor Cyan
Write-Host '========================================' -ForegroundColor Cyan
Write-Host ''
$total = 0
$passed = 0
$failed = 0
foreach ($wat in $watFiles) {
$total++
$wasmName = [System.IO.Path]::ChangeExtension($wat.Name, '.wasm')
$wasmPath = Join-Path $BinDir $wasmName
$relPath = $wat.FullName.Substring($Root.Length + 1)
# ---- compile ----
Write-Verbose "Compiling $relPath -> bin\$wasmName"
$compileOutput = cmd /c "wat2wasm `"$($wat.FullName)`" -o `"$wasmPath`" 2>&1"
if ($LASTEXITCODE -ne 0) {
Write-Host " FAIL: $relPath (wat2wasm failed)" -ForegroundColor Red
if ($compileOutput) { Write-Host " $compileOutput" -ForegroundColor DarkRed }
$failed++
continue
}
# ---- parse expectations ----
$expect = Parse-Expectations $wat.FullName
# ---- run ----
$actualStdout = cmd /c "`"$Exe`" `"$wasmPath`" 2>nul"
$actualExit = $LASTEXITCODE
# Strip the WASURO banner line from stdout for assertion
$lines = @()
if ($actualStdout) {
if ($actualStdout -is [string]) {
$lines = $actualStdout -split "`r?`n"
} else {
$lines = @($actualStdout | ForEach-Object { "$_" })
}
}
$contentLines = @()
foreach ($line in $lines) {
# Skip banner and empty trailing lines
if ($line -match '^WASURO - WebAssembly Runtime') { continue }
if ($line -match '^Module loaded') { continue }
$contentLines += $line
}
# Trim trailing empty lines
while ($contentLines.Count -gt 0 -and $contentLines[-1] -eq '') {
$contentLines = $contentLines[0..($contentLines.Count - 2)]
}
$actualContent = $contentLines -join "`n"
# ---- assert ----
$exitOk = ($actualExit -eq $expect.ExitCode)
$stdoutOk = ($actualContent -eq $expect.Stdout)
if ($exitOk -and $stdoutOk) {
Write-Host " PASS: $relPath" -ForegroundColor Green
$passed++
}
else {
Write-Host " FAIL: $relPath" -ForegroundColor Red
if (-not $exitOk) {
Write-Host " exit: expected $($expect.ExitCode), got $actualExit" -ForegroundColor DarkRed
}
if (-not $stdoutOk) {
Write-Host " stdout expected: [$($expect.Stdout)]" -ForegroundColor DarkRed
Write-Host " stdout actual: [$actualContent]" -ForegroundColor DarkRed
}
$failed++
}
}
Write-Host ''
Write-Host '========================================' -ForegroundColor Cyan
Write-Host " Total: $total" -ForegroundColor Cyan
Write-Host " Passed: $passed" -ForegroundColor Green
Write-Host " Failed: $failed" -ForegroundColor $(if ($failed -gt 0) { 'Red' } else { 'Green' })
Write-Host '========================================' -ForegroundColor Cyan
exit $failed