Skip to content

Commit 17b9a64

Browse files
Merge pull request #95 from swiftwasm/katei/add-atomics-feature
Add parser support for the threads proposal
2 parents 2606823 + 7300227 commit 17b9a64

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

Sources/WasmParser/WasmParser.swift

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ public struct WasmFeatureSet: OptionSet {
5757
public static let memory64 = WasmFeatureSet(rawValue: 1 << 0)
5858
/// The WebAssembly reference types proposal
5959
public static let referenceTypes = WasmFeatureSet(rawValue: 1 << 1)
60+
/// The WebAssembly threads proposal
61+
public static let threads = WasmFeatureSet(rawValue: 1 << 2)
6062

6163
/// The default feature set
6264
public static let `default`: WasmFeatureSet = [.referenceTypes]
6365
/// The feature set with all features enabled
64-
public static let all: WasmFeatureSet = [.memory64, .referenceTypes]
66+
public static let all: WasmFeatureSet = [.memory64, .referenceTypes, .threads]
6567
}
6668

6769
public enum WasmParserError: Swift.Error {
@@ -274,19 +276,39 @@ extension Parser {
274276
/// <https://webassembly.github.io/spec/core/binary/types.html#limits>
275277
func parseLimits() throws -> Limits {
276278
let b = try stream.consumeAny()
279+
let sharedMask : UInt8 = 0b0010
280+
let isMemory64Mask: UInt8 = 0b0100
277281

278-
switch b {
279-
case 0x00:
280-
return try Limits(min: UInt64(parseUnsigned(UInt32.self)), max: nil)
281-
case 0x01:
282-
return try Limits(min: UInt64(parseUnsigned(UInt32.self)), max: UInt64(parseUnsigned(UInt32.self)))
283-
case 0x04 where features.contains(.memory64):
284-
return try Limits(min: parseUnsigned(UInt64.self), max: nil, isMemory64: true)
285-
case 0x05 where features.contains(.memory64):
286-
return try Limits(min: parseUnsigned(UInt64.self), max: parseUnsigned(UInt64.self), isMemory64: true)
287-
default:
282+
let hasMax = b & 0b0001 != 0
283+
let shared = b & sharedMask != 0
284+
let isMemory64 = b & isMemory64Mask != 0
285+
286+
var flagMask: UInt8 = 0b0001
287+
if features.contains(.threads) {
288+
flagMask |= sharedMask
289+
}
290+
if features.contains(.memory64) {
291+
flagMask |= isMemory64Mask
292+
}
293+
guard (b & ~flagMask) == 0 else {
288294
throw WasmParserError.malformedLimit(b)
289295
}
296+
297+
let min: UInt64
298+
if isMemory64 {
299+
min = try parseUnsigned(UInt64.self)
300+
} else {
301+
min = try UInt64(parseUnsigned(UInt32.self))
302+
}
303+
var max: UInt64?
304+
if hasMax {
305+
if isMemory64 {
306+
max = try parseUnsigned(UInt64.self)
307+
} else {
308+
max = try UInt64(parseUnsigned(UInt32.self))
309+
}
310+
}
311+
return Limits(min: min, max: max, isMemory64: isMemory64, shared: shared)
290312
}
291313

292314
/// > Note:

Sources/WasmParser/WasmTypes.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ public struct Limits: Equatable {
3535
public let min: UInt64
3636
public let max: UInt64?
3737
public let isMemory64: Bool
38+
public let shared: Bool
3839

39-
public init(min: UInt64, max: UInt64?, isMemory64: Bool = false) {
40+
public init(min: UInt64, max: UInt64?, isMemory64: Bool = false, shared: Bool = false) {
4041
self.min = min
4142
self.max = max
4243
self.isMemory64 = isMemory64
44+
self.shared = shared
4345
}
4446
}
4547

0 commit comments

Comments
 (0)