From c6169d40bba3f26d61a85f974c27427bdc37eb78 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 8 Apr 2024 00:56:07 +0900 Subject: [PATCH] Use ValueType from WasmParser --- Sources/WASI/WASI.swift | 1 + .../Instructions/InstructionSupport.swift | 4 +- .../WasmKit/Execution/Runtime/Function.swift | 2 + Sources/WasmKit/Execution/Types/Errors.swift | 2 +- Sources/WasmKit/Execution/Types/Value.swift | 121 +----------------- Sources/WasmKit/Translator.swift | 12 +- Sources/WasmKit/Types/Module.swift | 2 +- Sources/WasmKit/Types/Types.swift | 11 -- Sources/WasmParser/Stream/Stream.swift | 3 +- .../Execution/HostModuleTests.swift | 2 +- 10 files changed, 16 insertions(+), 144 deletions(-) delete mode 100644 Sources/WasmKit/Types/Types.swift diff --git a/Sources/WASI/WASI.swift b/Sources/WASI/WASI.swift index be1f8d5c..72b49e2f 100644 --- a/Sources/WASI/WASI.swift +++ b/Sources/WASI/WASI.swift @@ -3,6 +3,7 @@ import SwiftShims // For swift_stdlib_random import SystemExtras import SystemPackage import WasmKit +import WasmParser protocol WASI { /// Reads command-line argument data. diff --git a/Sources/WasmKit/Execution/Instructions/InstructionSupport.swift b/Sources/WasmKit/Execution/Instructions/InstructionSupport.swift index e1d3f508..cf88fc84 100644 --- a/Sources/WasmKit/Execution/Instructions/InstructionSupport.swift +++ b/Sources/WasmKit/Execution/Instructions/InstructionSupport.swift @@ -1,6 +1,4 @@ -import struct WasmParser.MemArg -import enum WasmParser.BlockType -import enum WasmParser.WasmParserError +import WasmParser extension Instruction { typealias Memarg = MemArg diff --git a/Sources/WasmKit/Execution/Runtime/Function.swift b/Sources/WasmKit/Execution/Runtime/Function.swift index 594c25f6..e9c47084 100644 --- a/Sources/WasmKit/Execution/Runtime/Function.swift +++ b/Sources/WasmKit/Execution/Runtime/Function.swift @@ -1,3 +1,5 @@ +import WasmParser + /// A WebAssembly guest function or host function public struct Function: Equatable { internal let address: FunctionAddress diff --git a/Sources/WasmKit/Execution/Types/Errors.swift b/Sources/WasmKit/Execution/Types/Errors.swift index 70d40ecb..07cd5e7e 100644 --- a/Sources/WasmKit/Execution/Types/Errors.swift +++ b/Sources/WasmKit/Execution/Types/Errors.swift @@ -10,7 +10,7 @@ public enum Trap: Error { /// Stack overflow case stackOverflow /// The stack value type does not match the expected type - case stackValueTypesMismatch(expected: ValueType, actual: WasmParser.ValueType) + case stackValueTypesMismatch(expected: WasmParser.ValueType, actual: WasmParser.ValueType) /// Too deep call stack case callStackExhausted diff --git a/Sources/WasmKit/Execution/Types/Value.swift b/Sources/WasmKit/Execution/Types/Value.swift index 3aeebdb0..2b010942 100644 --- a/Sources/WasmKit/Execution/Types/Value.swift +++ b/Sources/WasmKit/Execution/Types/Value.swift @@ -21,61 +21,20 @@ public enum NumericType: Equatable { public static let f64: Self = .float(.f64) } -/// Value types -public enum ValueType: Equatable { - /// Numeric value type. - case numeric(NumericType) - /// Reference value type. - case reference(ReferenceType) - - /// 32-bit signed or unsigned integer. - public static let i32: Self = .numeric(.int(.i32)) - /// 64-bit signed or unsigned integer. - public static let i64: Self = .numeric(.int(.i64)) - /// 32-bit IEEE 754 floating-point number. - public static let f32: Self = .numeric(.float(.f32)) - /// 64-bit IEEE 754 floating-point number. - public static let f64: Self = .numeric(.float(.f64)) - +extension WasmParser.ValueType { var defaultValue: Value { switch self { - case .numeric(.int(.i32)): - return .i32(0) - case .numeric(.int(.i64)): - return .i64(0) - case .numeric(.float(.f32)): - return .f32(0) - case .numeric(.float(.f64)): - return .f64(0) - case .reference(.externRef): + case .i32: return .i32(0) + case .i64: return .i64(0) + case .f32: return .f32(0) + case .f64: return .f64(0) + case .ref(.externRef): return .ref(.extern(nil)) - case .reference(.funcRef): + case .ref(.funcRef): return .ref(.function(nil)) } } - var float: FloatValueType { - switch self { - case let .numeric(.float(f)): - return f - default: - fatalError("unexpected value type \(self)") - } - } - - var bitWidth: Int? { - switch self { - case .numeric(.int(.i32)), .numeric(.float(.f32)): - return 32 - case .numeric(.int(.i64)), .numeric(.float(.f64)): - return 64 - case .reference: - return nil - } - } -} - -extension WasmParser.ValueType { var float: FloatValueType { switch self { case .f32: return .f32 @@ -93,35 +52,6 @@ extension WasmParser.ValueType { } } -extension ValueType: CustomStringConvertible { - public var description: String { - switch self { - case let .numeric(.int(type)): - return String(describing: type) - case let .numeric(.float(type)): - return String(describing: type) - case let .reference(type): - return String(describing: type) - } - } -} - -extension ValueType { - init(_ valueType: WasmParser.ValueType) { - switch valueType { - case .i32: self = .i32 - case .i64: self = .i64 - case .f32: self = .f32 - case .f64: self = .f64 - case .ref(.externRef): - self = .reference(.externRef) - case .ref(.funcRef): - self = .reference(.funcRef) - } - } -} - - public typealias ReferenceType = WasmParser.ReferenceType public enum Reference: Hashable { @@ -434,43 +364,6 @@ public enum FloatValueType { } extension Value { - init?(_ bytes: T, _ type: ValueType, isSigned: Bool) - where T.Element == UInt8, T.Index == Int { - switch type { - case .numeric(.int(.i32)): - switch bytes.count { - case 1: - self = isSigned ? .i32(Int32(bytes[bytes.startIndex].signed).unsigned) : .i32(UInt32(bytes[bytes.startIndex])) - case 2: - self = isSigned ? .i32(Int32(UInt16(littleEndian: bytes).signed).unsigned) : .i32(UInt32(littleEndian: bytes)) - case 4: - self = .i32(UInt32(littleEndian: bytes)) - default: - fatalError() - } - - case .numeric(.int(.i64)): - switch bytes.count { - case 1: - self = isSigned ? .i64(Int64(bytes[bytes.startIndex].signed).unsigned) : .i64(UInt64(bytes[bytes.startIndex])) - case 2: - self = isSigned ? .i64(Int64(UInt16(littleEndian: bytes).signed).unsigned) : .i64(UInt64(littleEndian: bytes)) - case 4: - self = isSigned ? .i64(Int64(UInt32(littleEndian: bytes).signed).unsigned) : .i64(UInt64(littleEndian: bytes)) - case 8: - self = .i64(UInt64(littleEndian: bytes)) - default: - fatalError() - } - - case .numeric(.float(.f32)): - self = .fromFloat32(Float32(bitPattern: UInt32(littleEndian: bytes))) - case .numeric(.float(.f64)): - self = .fromFloat64(Float64(bitPattern: UInt64(littleEndian: bytes))) - case .reference: return nil - } - } - var bytes: [UInt8]? { switch self { case let .i32(rawValue): return rawValue.littleEndianBytes diff --git a/Sources/WasmKit/Translator.swift b/Sources/WasmKit/Translator.swift index e1f09809..b4fbb1e2 100644 --- a/Sources/WasmKit/Translator.swift +++ b/Sources/WasmKit/Translator.swift @@ -1,14 +1,4 @@ -import protocol WasmParser.InstructionVisitor -import enum WasmParser.BlockType -import enum WasmParser.ValueType -import enum WasmParser.IEEE754 -import enum WasmParser.ReferenceType -import enum WasmParser.WasmParserError -import struct WasmParser.BrTable -import struct WasmParser.MemArg -import struct WasmParser.GlobalType -import struct WasmParser.Import -import struct WasmParser.Table +import WasmParser class ISeqAllocator { diff --git a/Sources/WasmKit/Types/Module.swift b/Sources/WasmKit/Types/Module.swift index 698a6b21..36df7d87 100644 --- a/Sources/WasmKit/Types/Module.swift +++ b/Sources/WasmKit/Types/Module.swift @@ -85,7 +85,7 @@ struct GuestFunction { // TODO: Deallocate const default locals after the module is deallocated let defaultLocals = UnsafeMutableBufferPointer.allocate(capacity: locals.count) for (index, localType) in locals.enumerated() { - defaultLocals[index] = ValueType(localType).defaultValue + defaultLocals[index] = localType.defaultValue } self.defaultLocals = UnsafeBufferPointer(defaultLocals) self.materializer = body diff --git a/Sources/WasmKit/Types/Types.swift b/Sources/WasmKit/Types/Types.swift deleted file mode 100644 index 73af0933..00000000 --- a/Sources/WasmKit/Types/Types.swift +++ /dev/null @@ -1,11 +0,0 @@ -public typealias FunctionType = WasmParser.FunctionType - -public typealias Limits = WasmParser.Limits - -/// > Note: -/// -public typealias MemoryType = Limits - -public typealias TableType = WasmParser.TableType - -import WasmParser diff --git a/Sources/WasmParser/Stream/Stream.swift b/Sources/WasmParser/Stream/Stream.swift index e82efad2..be6e38cd 100644 --- a/Sources/WasmParser/Stream/Stream.swift +++ b/Sources/WasmParser/Stream/Stream.swift @@ -20,8 +20,7 @@ extension Stream { try consume(Set([expected])) } - // TODO: Internalize - public func hasReachedEnd() throws -> Bool { + func hasReachedEnd() throws -> Bool { try peek() == nil } } diff --git a/Tests/WasmKitTests/Execution/HostModuleTests.swift b/Tests/WasmKitTests/Execution/HostModuleTests.swift index 5f4b9b8c..5992bd02 100644 --- a/Tests/WasmKitTests/Execution/HostModuleTests.swift +++ b/Tests/WasmKitTests/Execution/HostModuleTests.swift @@ -22,7 +22,7 @@ final class HostModuleTests: XCTestCase { func testReentrancy() throws { let runtime = Runtime() - let voidSignature = WasmKit.FunctionType(parameters: [], results: []) + let voidSignature = WasmParser.FunctionType(parameters: [], results: []) let module = Module( types: [voidSignature], functions: [