Skip to content

Commit 408592a

Browse files
./Utilities/format.py
1 parent 6ebbfaa commit 408592a

30 files changed

+233
-203
lines changed

Sources/CLI/Commands/Explore.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ struct Explore: ParsableCommand {
77
static let configuration = CommandConfiguration(
88
abstract: "Explore the compiled functions of a WebAssembly module",
99
discussion: """
10-
This command will parse a WebAssembly module and dump the compiled functions.
11-
""",
10+
This command will parse a WebAssembly module and dump the compiled functions.
11+
""",
1212
// This command is just for debugging purposes, so it should be hidden by default
1313
shouldDisplay: false
1414
)

Sources/CLI/Commands/Run.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import ArgumentParser
22
import SystemPackage
33
import WasmKit
44
import WasmKitWASI
5+
56
#if canImport(os.signpost)
6-
import os.signpost
7+
import os.signpost
78
#endif
89

910
struct Run: ParsableCommand {
1011
static let configuration = CommandConfiguration(
1112
abstract: "Run a WebAssembly module",
1213
discussion: """
13-
This command will parse a WebAssembly module and run it.
14-
"""
14+
This command will parse a WebAssembly module and run it.
15+
"""
1516
)
1617

1718
@Flag(
@@ -97,7 +98,7 @@ struct Run: ParsableCommand {
9798
"The size of the interpreter stack in bytes",
9899
valueName: "bytes"
99100
)
100-
)
101+
)
101102
var stackSize: Int?
102103

103104
@Argument
@@ -178,10 +179,10 @@ struct Run: ParsableCommand {
178179

179180
private func deriveSignpostTracer() -> EngineInterceptor? {
180181
#if canImport(os.signpost)
181-
if #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) {
182-
let signposter = SignpostTracer(signposter: OSSignposter())
183-
return signposter
184-
}
182+
if #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) {
183+
let signposter = SignpostTracer(signposter: OSSignposter())
184+
return signposter
185+
}
185186
#endif
186187
log("warning: Signpost logging is not supported on this platform. Ignoring --enable-signpost")
187188
return nil

Sources/WAT/InstructionEncoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protocol InstructionEncoder: InstructionVisitor {
3434
mutating func encodeImmediates(typeIndex: UInt32, tableIndex: UInt32) throws
3535
}
3636

37-
/// NOTE: InstructionEncoder implements the InstructionVisitor protocol to call the corresponding encode method.
37+
// InstructionEncoder implements the InstructionVisitor protocol to call the corresponding encode method.
3838
extension InstructionEncoder {
3939
mutating func visitUnreachable() throws { try encodeInstruction(0x00, nil) }
4040
mutating func visitNop() throws { try encodeInstruction(0x01, nil) }

Sources/WAT/WAT.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ func parseWAT(_ parser: inout Parser, features: WasmFeatureSet) throws -> Wat {
200200
}
201201
}
202202

203-
204203
switch decl.kind {
205204
case let .type(decl):
206205
try typesMap.add(decl)

Sources/WITOverlayGenerator/HostGenerators/HostExportFunction.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,12 @@ struct HostExportFunction {
266266
typeResolver: typeResolver, printer: printer
267267
)
268268
let functionVar = builder.variable("function")
269-
printer.write(multiline: """
270-
guard let \(functionVar) = instance.exports[function: \"\(name.abiName)\"] else {
271-
throw CanonicalABIError(description: "Function \\"\(name.abiName)\\" not found in the instance")
272-
}
273-
""")
269+
printer.write(
270+
multiline: """
271+
guard let \(functionVar) = instance.exports[function: \"\(name.abiName)\"] else {
272+
throw CanonicalABIError(description: "Function \\"\(name.abiName)\\" not found in the instance")
273+
}
274+
""")
274275
var call = "try \(functionVar)("
275276
if !arguments.isEmpty {
276277
call += "[\(arguments.map(\.description).joined(separator: ", "))]"

Sources/WasmKit/Execution/ConstEvaluation.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ extension WasmParser.ElementSegment {
105105
}
106106
}
107107

108-
fileprivate extension WasmTypes.Reference {
109-
func checkType(_ type: WasmTypes.ReferenceType) throws {
108+
extension WasmTypes.Reference {
109+
fileprivate func checkType(_ type: WasmTypes.ReferenceType) throws {
110110
switch (self, type) {
111111
case (.function, .funcRef): return
112112
case (.extern, .externRef): return
@@ -116,8 +116,8 @@ fileprivate extension WasmTypes.Reference {
116116
}
117117
}
118118

119-
fileprivate extension Value {
120-
func checkType(_ type: WasmTypes.ValueType) throws {
119+
extension Value {
120+
fileprivate func checkType(_ type: WasmTypes.ValueType) throws {
121121
switch (self, type) {
122122
case (.i32, .i32): return
123123
case (.i64, .i64): return

Sources/WasmKit/Execution/DispatchInstruction.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// swift-format-ignore-file
12
//// Automatically generated by Utilities/Sources/VMGen.swift
23
//// DO NOT EDIT DIRECTLY
34

Sources/WasmKit/Execution/Execution.swift

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ extension Sp {
123123
return UntypedValue(storage: self[Int(index)])
124124
}
125125
nonmutating set {
126-
return self[Int(index)] = newValue.storage
126+
self[Int(index)] = newValue.storage
127+
return
127128
}
128129
}
129130

@@ -302,7 +303,10 @@ extension Execution {
302303
handle: InternalFunction,
303304
type: FunctionType
304305
) throws {
305-
var sp: Sp = sp, md: Md = nil, ms: Ms = 0, pc = pc
306+
var sp: Sp = sp
307+
var md: Md = nil
308+
var ms: Ms = 0
309+
var pc = pc
306310
(pc, sp) = try invoke(
307311
function: handle,
308312
callerInstance: nil,
@@ -334,82 +338,82 @@ extension Execution {
334338
}
335339
}
336340

337-
#if EngineStats
338-
/// A helper structure for collecting instruction statistics.
339-
/// - Note: This is used only when the `EngineStats` flag is enabled.
340-
struct StatsCollector {
341-
struct Trigram: Hashable {
342-
var a: UInt64
343-
var b: UInt64
344-
var c: UInt64
345-
}
341+
#if EngineStats
342+
/// A helper structure for collecting instruction statistics.
343+
/// - Note: This is used only when the `EngineStats` flag is enabled.
344+
struct StatsCollector {
345+
struct Trigram: Hashable {
346+
var a: UInt64
347+
var b: UInt64
348+
var c: UInt64
349+
}
346350

347-
struct CircularBuffer<T> {
348-
private var buffer: [T?]
349-
private var index: Int = 0
351+
struct CircularBuffer<T> {
352+
private var buffer: [T?]
353+
private var index: Int = 0
350354

351-
init(capacity: Int) {
352-
buffer = Array(repeating: nil, count: capacity)
353-
}
355+
init(capacity: Int) {
356+
buffer = Array(repeating: nil, count: capacity)
357+
}
354358

355-
/// Accesses the element at the specified position counted from the oldest element.
356-
subscript(_ index: Int) -> T? {
357-
get {
358-
return buffer[(self.index + index) % buffer.count]
359+
/// Accesses the element at the specified position counted from the oldest element.
360+
subscript(_ index: Int) -> T? {
361+
get {
362+
return buffer[(self.index + index) % buffer.count]
363+
}
364+
set {
365+
buffer[(self.index + index) % buffer.count] = newValue
366+
}
359367
}
360-
set {
361-
buffer[(self.index + index) % buffer.count] = newValue
368+
369+
mutating func append(_ value: T) {
370+
buffer[index] = value
371+
index = (index + 1) % buffer.count
362372
}
363373
}
364374

365-
mutating func append(_ value: T) {
366-
buffer[index] = value
367-
index = (index + 1) % buffer.count
375+
/// A dictionary that stores the count of each trigram pattern.
376+
private var countByTrigram: [Trigram: Int] = [:]
377+
/// A circular buffer that stores the last three instructions.
378+
private var buffer = CircularBuffer<UInt64>(capacity: 3)
379+
380+
/// Tracks the given instruction index. This function is called for each instruction execution.
381+
mutating func track(_ opcode: UInt64) {
382+
buffer.append(opcode)
383+
if let a = buffer[0], let b = buffer[1], let c = buffer[2] {
384+
let trigram = Trigram(a: a, b: b, c: c)
385+
countByTrigram[trigram, default: 0] += 1
386+
}
368387
}
369-
}
370388

371-
/// A dictionary that stores the count of each trigram pattern.
372-
private var countByTrigram: [Trigram: Int] = [:]
373-
/// A circular buffer that stores the last three instructions.
374-
private var buffer = CircularBuffer<UInt64>(capacity: 3)
375-
376-
/// Tracks the given instruction index. This function is called for each instruction execution.
377-
mutating func track(_ opcode: UInt64) {
378-
buffer.append(opcode)
379-
if let a = buffer[0], let b = buffer[1], let c = buffer[2] {
380-
let trigram = Trigram(a: a, b: b, c: c)
381-
countByTrigram[trigram, default: 0] += 1
389+
func dump<TargetStream: TextOutputStream>(target: inout TargetStream, limit: Int) {
390+
print("Instruction statistics:", to: &target)
391+
for (trigram, count) in countByTrigram.sorted(by: { $0.value > $1.value }).prefix(limit) {
392+
print(" \(Instruction.name(opcode: trigram.a)) -> \(Instruction.name(opcode: trigram.b)) -> \(Instruction.name(opcode: trigram.c)) = \(count)", to: &target)
393+
}
382394
}
383-
}
384395

385-
func dump<TargetStream: TextOutputStream>(target: inout TargetStream, limit: Int) {
386-
print("Instruction statistics:", to: &target)
387-
for (trigram, count) in countByTrigram.sorted(by: { $0.value > $1.value }).prefix(limit) {
388-
print(" \(Instruction.name(opcode: trigram.a)) -> \(Instruction.name(opcode: trigram.b)) -> \(Instruction.name(opcode: trigram.c)) = \(count)", to: &target)
396+
/// Dumps the instruction statistics to the standard error output stream.
397+
func dump(limit: Int = 10) {
398+
var target = _Stderr()
399+
dump(target: &target, limit: limit)
389400
}
390401
}
391-
392-
/// Dumps the instruction statistics to the standard error output stream.
393-
func dump(limit: Int = 10) {
394-
var target = _Stderr()
395-
dump(target: &target, limit: limit)
396-
}
397-
}
398-
#endif
402+
#endif
399403

400404
/// Starts the main execution loop using the token threading model.
401405
/// Be careful when modifying this function as it is performance-critical.
402406
@inline(__always)
403407
mutating func runTokenThreaded(sp: inout Sp, pc: inout Pc, md: inout Md, ms: inout Ms) throws {
404-
#if EngineStats
405-
var stats = StatsCollector()
406-
defer { stats.dump() }
407-
#endif
408+
#if EngineStats
409+
var stats = StatsCollector()
410+
defer { stats.dump() }
411+
#endif
408412
var opcode = pc.read(OpcodeID.self)
409413
while true {
410-
#if EngineStats
411-
stats.track(inst)
412-
#endif
414+
#if EngineStats
415+
stats.track(inst)
416+
#endif
413417
opcode = try doExecute(opcode, sp: &sp, pc: &pc, md: &md, ms: &ms)
414418
}
415419
}

Sources/WasmKit/Execution/Function.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import WasmParser
2+
23
import struct WasmTypes.FunctionType
34

45
/// A WebAssembly guest function or host function.
@@ -194,7 +195,6 @@ extension InternalFunction {
194195
}
195196
}
196197

197-
198198
struct WasmFunctionEntity {
199199
let type: InternedFuncType
200200
let instance: InternalInstance

Sources/WasmKit/Execution/Instances.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import WasmParser
2929
// | |
3030
// +--------------------------------------------------------------+
3131

32-
3332
/// Internal representation of a reference to a WebAssembly entity.
3433
///
3534
/// This type is designed to eliminate ARC retain/release for entities
@@ -466,7 +465,8 @@ struct MemoryEntity /* : ~Copyable */ {
466465
let (sourceEnd, sourceOverflow) = source.addingReportingOverflow(count)
467466

468467
guard !destinationOverflow, destinationEnd <= data.count,
469-
!sourceOverflow, sourceEnd <= data.count else {
468+
!sourceOverflow, sourceEnd <= data.count
469+
else {
470470
throw Trap.outOfBoundsMemoryAccess
471471
}
472472
data.withUnsafeMutableBufferPointer {
@@ -482,14 +482,16 @@ struct MemoryEntity /* : ~Copyable */ {
482482
let (sourceEnd, sourceOverflow) = source.addingReportingOverflow(count)
483483

484484
guard !destinationOverflow, destinationEnd <= data.count,
485-
!sourceOverflow, sourceEnd <= segment.data.count else {
485+
!sourceOverflow, sourceEnd <= segment.data.count
486+
else {
486487
throw Trap.outOfBoundsMemoryAccess
487488
}
488489
data.withUnsafeMutableBufferPointer { memory in
489490
segment.data.withUnsafeBufferPointer { segment in
490491
guard
491492
let memory = UnsafeMutableRawPointer(memory.baseAddress),
492-
let segment = UnsafeRawPointer(segment.baseAddress) else { return }
493+
let segment = UnsafeRawPointer(segment.baseAddress)
494+
else { return }
493495
let dest = memory.advanced(by: Int(destination))
494496
let src = segment.advanced(by: Int(source))
495497
dest.copyMemory(from: src, byteCount: Int(count))

0 commit comments

Comments
 (0)