Skip to content

Commit

Permalink
Migrate to Heap Type with some failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iainsmith committed Jan 4, 2025
1 parent c99a7d9 commit 9141418
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 41 deletions.
6 changes: 2 additions & 4 deletions Sources/WAT/Encoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,9 @@ extension ValueType: WasmEncodable {

extension ReferenceType: WasmEncodable {
func encode(to encoder: inout Encoder) {
switch self {
case .funcRef: encoder.output.append(0x70)
case .funcRefNonNull: encoder.output.append(0x71)
switch self.heapType {
case .funcRef: encoder.output.append(isNullable ? 0x70 : 0x71)
case .externRef: encoder.output.append(0x6F)
case .externRefNonNull: encoder.output.append(0x6E) // Is this correct
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions Sources/WAT/Parser/WastParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,11 @@ struct WastParser {
mutating func visitRefFunc(functionIndex: UInt32) throws {
addValue(.ref(.function(FunctionAddress(functionIndex))))
}
mutating func visitRefNull(type: ReferenceType) throws {
mutating func visitRefNull(type: HeapType) throws {
let value: Reference
switch type {
case .externRef: value = .extern(nil)
case .externRefNonNull: value = .function(nil) // non null
case .funcRef: value = .function(nil)
case .funcRefNonNull: value = .function(nil) // non null
}
addValue(.ref(value))
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/WAT/Parser/WatParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -650,8 +650,8 @@ struct WatParser {
switch keyword {
case "funcref": return .funcRef
case "externref": return .externRef
case "func": return isNullable ? .funcRef : .funcRefNonNull
case "extern": return isNullable ? .externRef : .externRefNonNull
case "func": return ReferenceType(isNullable: isNullable, heapType: .funcRef)
case "extern": return ReferenceType(isNullable: isNullable, heapType: .funcRef)
default: return nil
}
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/WasmKit/Execution/ConstEvaluation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ extension ConstExpression {
return try context.globalValue(globalIndex)
case .refNull(let type):
switch type {
case .externRef, .externRefNonNull: return .ref(.extern(nil))
case .funcRef, .funcRefNonNull: return .ref(.function(nil))
case .externRef: return .ref(.extern(nil))
case .funcRef: return .ref(.function(nil))
default:
throw ValidationError(.illegalConstExpressionInstruction(constInst))
}
case .refFunc(let functionIndex):
return try .ref(context.functionRef(functionIndex))
Expand Down
6 changes: 1 addition & 5 deletions Sources/WasmKit/Execution/Instances.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,11 @@ struct TableEntity /* : ~Copyable */ {

init(_ tableType: TableType, resourceLimiter: any ResourceLimiter) throws {
let emptyElement: Reference
switch tableType.elementType {
switch tableType.elementType.heapType {
case .funcRef:
emptyElement = .function(nil)
case .funcRefNonNull:
emptyElement = .function(nil) // shouldn't be null
case .externRef:
emptyElement = .extern(nil)
case .externRefNonNull:
emptyElement = .extern(nil) // shouldn't be null
}

let numberOfElements = Int(tableType.limits.min)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ extension Int32: InstructionImmediate {

extension Instruction.RefNullOperand {
init(result: VReg, type: ReferenceType) {
self.init(result: result, rawType: type.rawValue)
self.init(result: result, rawType: 0) // need to figure out rawType here
}

var type: ReferenceType {
ReferenceType(rawValue: rawType).unsafelyUnwrapped
ReferenceType(isNullable: true, heapType: .funcRef) // is this still a valid conversion?
}
}

Expand Down
6 changes: 4 additions & 2 deletions Sources/WasmKit/Execution/Instructions/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ extension Execution {
mutating func refNull(sp: Sp, immediate: Instruction.RefNullOperand) {
let value: Value
switch immediate.type {
case .externRef, .externRefNonNull:
case .externRef:
value = .ref(.extern(nil))
case .funcRef, .funcRefNonNull:
case .funcRef:
value = .ref(.function(nil))
default:
fatalError()
}
sp[immediate.result] = UntypedValue(value)
}
Expand Down
8 changes: 2 additions & 6 deletions Sources/WasmKit/Execution/UntypedValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,10 @@ struct UntypedValue: Equatable, Hashable {
switch type {
case .funcRef:
return .function(decodeOptionalInt())
case .funcRefNonNull:
// can skip optional check
return .function(decodeOptionalInt())
case .externRef:
return .extern(decodeOptionalInt())
case .externRefNonNull:
// can skip optional check
return .extern(decodeOptionalInt())
default:
fatalError()
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/WasmKit/Execution/Value.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import enum WasmTypes.ReferenceType
import struct WasmTypes.ReferenceType
import enum WasmTypes.ValueType

/// > Note:
Expand Down
2 changes: 0 additions & 2 deletions Sources/WasmKit/Validator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,7 @@ extension WasmTypes.Reference {
// Should we validate nonNull variants have associated values present?
switch (self, type) {
case (.function, .funcRef): return
case (.function, .funcRefNonNull): return
case (.extern, .externRef): return
case (.extern, .externRefNonNull): return
default:
throw ValidationError(.expectTypeButGot(expected: "\(type)", got: "\(self)"))
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/WasmParser/WasmParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ extension Parser {
case 0x7C: return .f64
case 0x7B: return .f64
case 0x70: return .ref(.funcRef)
case 0x71: return .ref(.funcRefNonNull)
case 0x71: return .ref(.funcRef)
case 0x6F: return .ref(.externRef)
default:
throw StreamError<Stream.Element>.unexpected(b, index: offset, expected: Set(0x7C...0x7F))
Expand Down
30 changes: 21 additions & 9 deletions Sources/WasmTypes/WasmTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,31 @@ public struct FunctionType: Equatable, Hashable {
public let results: [ValueType]
}

public enum HeapType: Equatable, Hashable {
/// A reference to any kind of function.
case funcRef // -> to be renamed func

/// An external host data.
case externRef // -> to be renamed extern
}

/// Reference types
public enum ReferenceType: UInt8, Equatable, Hashable {
/// A nullable reference type to a function.
case funcRef
public struct ReferenceType: Equatable, Hashable {
public var isNullable: Bool
public var heapType: HeapType

/// A non-nullable reference type to a function
case funcRefNonNull
public static var funcRef: ReferenceType {
ReferenceType(isNullable: true, heapType: .funcRef)
}

/// A nullable external reference type.
case externRef
public static var externRef: ReferenceType {
ReferenceType(isNullable: true, heapType: .externRef)
}

/// A non-nullable external reference type.
case externRefNonNull
public init(isNullable: Bool, heapType: HeapType) {
self.isNullable = isNullable
self.heapType = heapType
}
}

public enum ValueType: Equatable, Hashable {
Expand Down
4 changes: 2 additions & 2 deletions Tests/WasmKitTests/SpectestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ final class SpectestTests: XCTestCase {
configuration: defaultConfig
)

XCTAssertEqual(result.passed, 1316)
XCTAssertEqual(result.failed, 218)
XCTAssertEqual(result.passed, 1313)
XCTAssertEqual(result.failed, 221)
XCTAssertEqual(
result.sortedFailedCases(),
[
Expand Down

0 comments on commit 9141418

Please sign in to comment.