Skip to content

Introduce WAT library #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ jobs:
"hostSdkRootPath": "$(xcrun --show-sdk-path --sdk macosx)"
}
EOS
- run: ./Vendor/checkout-dependency testsuite
- run: swift test
# TODO: Enable spectest and wasi-testsuite checks
# TODO: Enable wasi-testsuite checks

build-linux:
strategy:
Expand Down Expand Up @@ -107,6 +108,7 @@ jobs:
"hostSwiftExecutablePath": "/usr/bin/swift"
}
EOS
- run: ./Vendor/checkout-dependency testsuite
- run: ./build-exec swift test
- run: ./build-exec ./CI/check-spectest.sh
- run: ./build-exec ./CI/check-wasi-testsuite.sh
Expand All @@ -119,6 +121,7 @@ jobs:
branch: swift-5.10.1-release
tag: 5.10.1-RELEASE
- uses: actions/checkout@v4
- run: python3 ./Vendor/checkout-dependency testsuite
- run: swift test

build-cmake:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*.xcodeproj
.swiftpm

/spectest
.vscode

/Tests/default.json
Expand Down
18 changes: 0 additions & 18 deletions Examples/wasm/host.wat

This file was deleted.

18 changes: 2 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@ docs:
### Spectest (Core WebAssembly Specification Test Suite)

TESTSUITE_DIR = Vendor/testsuite
SPECTEST_ROOT = ./spectest
WAST_FILES = $(wildcard $(TESTSUITE_DIR)/*.wast) $(wildcard $(TESTSUITE_DIR)/proposals/memory64/*.wast)
JSON_FILES = $(WAST_FILES:$(TESTSUITE_DIR)/%.wast=$(SPECTEST_ROOT)/%.json)

.PHONY: spec
spec: $(JSON_FILES) $(SPECTEST_ROOT)/host.wasm

$(SPECTEST_ROOT)/host.wasm: ./Examples/wasm/host.wat
wat2wasm ./Examples/wasm/host.wat -o $(SPECTEST_ROOT)/host.wasm

$(TESTSUITE_DIR)/%.wast: $(TESTSUITE_DIR)
$(SPECTEST_ROOT)/%.json: $(TESTSUITE_DIR)/%.wast
@mkdir -p $(@D)
wast2json $^ -o $@

.PHONY: spectest
spectest: spec
swift run --sanitize address Spectest $(SPECTEST_ROOT)
spectest:
swift run --sanitize address Spectest $(TESTSUITE_DIR) $(TESTSUITE_DIR)/proposals/memory64


### WASI Test Suite
Expand Down
7 changes: 7 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ let package = Package(
name: "WasmParser",
targets: ["WasmParser"]
),
.library(
name: "WAT",
targets: ["WAT"]
),
.library(
name: "WIT", targets: ["WIT"]
),
Expand Down Expand Up @@ -47,6 +51,7 @@ let package = Package(
],
exclude: ["CMakeLists.txt"]
),
.target(name: "WAT", dependencies: ["WasmParser"]),
.target(
name: "WasmParser",
dependencies: [
Expand All @@ -59,10 +64,12 @@ let package = Package(
name: "Spectest",
dependencies: [
"WasmKit",
"WAT",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "SystemPackage", package: "swift-system"),
]
),
.testTarget(name: "WATTests", dependencies: ["WAT"]),
.target(name: "WIT"),
.testTarget(name: "WITTests", dependencies: ["WIT"]),
.target(name: "WITOverlayGenerator", dependencies: ["WIT"]),
Expand Down
112 changes: 98 additions & 14 deletions Sources/Spectest/Spectest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ import ArgumentParser
import Foundation
import SystemPackage
import WasmKit
import WAT

@main
@available(macOS 11, *)
struct Spectest: AsyncParsableCommand {
static let configuration = CommandConfiguration(
subcommands: [Run.self, ParseWat.self],
defaultSubcommand: Run.self
)
}

@available(macOS 11, *)
struct Run: AsyncParsableCommand {
@Argument
var path: String
var path: [String]

@Option
var include: String?
Expand Down Expand Up @@ -39,30 +48,46 @@ struct Spectest: AsyncParsableCommand {
fatalError("failed to load test: \(error)")
}

let rootPath: String
let filePath = FilePath(path)
if isDirectory(filePath) {
rootPath = path
} else {
rootPath = URL(fileURLWithPath: path).deletingLastPathComponent().path
guard !testCases.isEmpty else {
log("No test found")
return
}

// https://github.com/WebAssembly/spec/tree/8a352708cffeb71206ca49a0f743bdc57269fb1a/interpreter#spectest-host-module
let hostModulePath = FilePath(rootPath).appending("host.wasm")
let hostModule = try parseWasm(filePath: hostModulePath)
let hostModule = try parseWasm(bytes: wat2wasm("""
(module
(global (export "global_i32") i32 (i32.const 666))
(global (export "global_i64") i64 (i64.const 666))
(global (export "global_f32") f32 (f32.const 666))
(global (export "global_f64") f64 (f64.const 666))

(table (export "table") 10 20 funcref)

(memory (export "memory") 1 2)

(func (export "print"))
(func (export "print_i32") (param i32))
(func (export "print_i64") (param i64))
(func (export "print_f32") (param f32))
(func (export "print_f64") (param f64))
(func (export "print_i32_f32") (param i32 f32))
(func (export "print_f64_f64") (param f64 f64))
)
"""))

@Sendable func runTestCase(testCase: TestCase) throws -> [Result] {
var testCaseResults = [Result]()
try testCase.run(spectestModule: hostModule) { testCase, command, result in
try testCase.run(spectestModule: hostModule) { testCase, location, result in
let (line, _) = location.computeLineAndColumn()
switch result {
case let .failed(reason):
log("\(testCase.content.sourceFilename):\(command.line): \(result.banner) \(reason)")
log("\(testCase.path):\(line): \(result.banner) \(reason)")
case let .skipped(reason):
log("\(testCase.content.sourceFilename):\(command.line): \(result.banner) \(reason)", verbose: true)
log("\(testCase.path):\(line): \(result.banner) \(reason)", verbose: true)
case .passed:
log("\(testCase.content.sourceFilename):\(command.line): \(result.banner)", verbose: true)
log("\(testCase.path):\(line): \(result.banner)", verbose: true)
default:
log("\(testCase.content.sourceFilename):\(command.line): \(result.banner)")
log("\(testCase.path):\(line): \(result.banner)")
}
testCaseResults.append(result)
}
Expand Down Expand Up @@ -117,3 +142,62 @@ struct Spectest: AsyncParsableCommand {
"\(Int(Double(numerator) / Double(denominator) * 100))%"
}
}

struct ParseWat: ParsableCommand {
@Argument
var path: String

@Option
var output: String?

func run() throws {
let allFiles: [URL]
if isDirectory(FilePath(path)) {
allFiles = try FileManager.default.contentsOfDirectory(at: URL(fileURLWithPath: path), includingPropertiesForKeys: nil)
} else {
allFiles = [URL(fileURLWithPath: path)]
}
let exclude = [
"annotations.wast"
]

var failureCount = 0
var allParseCount = 0
for filePath in allFiles {
guard filePath.pathExtension == "wast" else {
continue
}
guard !exclude.contains(filePath.lastPathComponent) else { continue }
guard !filePath.lastPathComponent.starts(with: "simd_") else { continue }
print("Parsing \(filePath.path)...")
let source = try String(contentsOf: filePath)
do {
allParseCount += 1
var wast = try parseWAST(source)
var moduleIndex = 0
while let (directive, _) = try wast.nextDirective() {
guard case let .module(moduleDirective) = directive,
case var .text(wat) = moduleDirective.source else {
continue
}
if let output {
let bytes = try wat.encode()
let outputFileName = filePath.deletingPathExtension().lastPathComponent + ".\(moduleIndex).wasm"
let outputPath = URL(fileURLWithPath: output).appendingPathComponent(outputFileName)
try Data(bytes).write(to: outputPath)
}
moduleIndex += 1
}
} catch {
failureCount += 1
print("Failed to parse \(filePath.path):\(error)")
}
}

if failureCount > 0 {
print("Failed to parse \(failureCount) / \(allParseCount) files")
} else {
print("Passed")
}
}
}
Loading
Loading