Skip to content

Commit

Permalink
Use ValueType from WasmParser
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Apr 7, 2024
1 parent 9acaa21 commit c6169d4
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 144 deletions.
1 change: 1 addition & 0 deletions Sources/WASI/WASI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import struct WasmParser.MemArg
import enum WasmParser.BlockType
import enum WasmParser.WasmParserError
import WasmParser

extension Instruction {
typealias Memarg = MemArg
Expand Down
2 changes: 2 additions & 0 deletions Sources/WasmKit/Execution/Runtime/Function.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import WasmParser

/// A WebAssembly guest function or host function
public struct Function: Equatable {
internal let address: FunctionAddress
Expand Down
2 changes: 1 addition & 1 deletion Sources/WasmKit/Execution/Types/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
121 changes: 7 additions & 114 deletions Sources/WasmKit/Execution/Types/Value.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -434,43 +364,6 @@ public enum FloatValueType {
}

extension Value {
init?<T: RandomAccessCollection>(_ 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
Expand Down
12 changes: 1 addition & 11 deletions Sources/WasmKit/Translator.swift
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down
2 changes: 1 addition & 1 deletion Sources/WasmKit/Types/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct GuestFunction {
// TODO: Deallocate const default locals after the module is deallocated
let defaultLocals = UnsafeMutableBufferPointer<Value>.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
Expand Down
11 changes: 0 additions & 11 deletions Sources/WasmKit/Types/Types.swift

This file was deleted.

3 changes: 1 addition & 2 deletions Sources/WasmParser/Stream/Stream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ extension Stream {
try consume(Set([expected]))
}

// TODO: Internalize
public func hasReachedEnd() throws -> Bool {
func hasReachedEnd() throws -> Bool {
try peek() == nil
}
}
2 changes: 1 addition & 1 deletion Tests/WasmKitTests/Execution/HostModuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down

0 comments on commit c6169d4

Please sign in to comment.