forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMisc.swift
More file actions
427 lines (368 loc) · 15 KB
/
Misc.swift
File metadata and controls
427 lines (368 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// MARK: - ProgressReporting
public struct ProgressReporting {
let print: (String) -> Void
public init(verbose: Bool) {
self.init(print: verbose ? { Swift.print($0) } : { _ in })
}
private init(print: @escaping (String) -> Void) {
self.print = print
}
public static var silent: ProgressReporting {
return ProgressReporting(print: { _ in })
}
public func print(_ message: String) {
self.print(message)
}
}
// MARK: - Profiling
/// A simple time-profiler API
public final class Profiling {
nonisolated(unsafe) static var current: Profiling?
let beginEntry: (_ label: String) -> Void
let endEntry: (_ label: String) -> Void
let finalize: () -> Void
/// Create a profiling instance that outputs Trace Event Format, which
/// can be viewed in chrome://tracing or other compatible viewers.
/// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/edit?usp=sharing
public static func traceEvent(output: @escaping (String) -> Void) -> Profiling {
let clock = ContinuousClock()
let startTime = clock.now
var firstEntry = true
func formatTimestamp() -> Int {
let duration = startTime.duration(to: .now)
let (seconds, attoseconds) = duration.components
// Convert to microseconds
return Int(seconds * 1_000_000 + attoseconds / 1_000_000_000_000)
}
return Profiling(
beginEntry: { label in
let entry = #"{"ph":"B","pid":1,"name":\#(JSON.serialize(label)),"ts":\#(formatTimestamp())}"#
if firstEntry {
firstEntry = false
output("[\n\(entry)")
} else {
output(",\n\(entry)")
}
},
endEntry: { label in
output(#",\#n{"ph":"E","pid":1,"name":\#(JSON.serialize(label)),"ts":\#(formatTimestamp())}"#)
},
finalize: {
output("]\n")
}
)
}
public init(
beginEntry: @escaping (_ label: String) -> Void,
endEntry: @escaping (_ label: String) -> Void,
finalize: @escaping () -> Void
) {
self.beginEntry = beginEntry
self.endEntry = endEntry
self.finalize = finalize
}
public static func with(_ makeCurrent: () -> Profiling?, body: @escaping () throws -> Void) rethrows -> Void {
guard let current = makeCurrent() else {
return try body()
}
defer { current.finalize() }
Profiling.current = current
defer { Profiling.current = nil }
return try body()
}
}
/// Mark a span of code with a label and measure the duration.
public func withSpan<T>(_ label: String, body: @escaping () throws -> T) rethrows -> T {
guard let profiling = Profiling.current else {
return try body()
}
profiling.beginEntry(label)
defer {
profiling.endEntry(label)
}
return try body()
}
/// Foundation-less JSON serialization
private enum JSON {
static func serialize(_ value: String) -> String {
// https://www.ietf.org/rfc/rfc4627.txt
var output = "\""
for scalar in value.unicodeScalars {
switch scalar {
case "\"":
output += "\\\""
case "\\":
output += "\\\\"
case "\u{08}":
output += "\\b"
case "\u{0C}":
output += "\\f"
case "\n":
output += "\\n"
case "\r":
output += "\\r"
case "\t":
output += "\\t"
case "\u{20}"..."\u{21}", "\u{23}"..."\u{5B}", "\u{5D}"..."\u{10FFFF}":
output.unicodeScalars.append(scalar)
default:
var hex = String(scalar.value, radix: 16, uppercase: true)
hex = String(repeating: "0", count: 4 - hex.count) + hex
output += "\\u" + hex
}
}
output += "\""
return output
}
}
// MARK: - DiagnosticError
import SwiftSyntax
import class Foundation.ProcessInfo
public struct DiagnosticError: Error {
public enum Severity: String, Sendable {
case error
case warning
}
public let node: Syntax
public let message: String
public let hint: String?
public let severity: Severity
public init(node: some SyntaxProtocol, message: String, hint: String? = nil, severity: Severity = .error) {
self.node = Syntax(node)
self.message = message
self.hint = hint
self.severity = severity
}
/// Formats the diagnostic error as a string.
///
/// - Parameters:
/// - fileName: The name of the file to display in the output.
/// - colorize: Whether to colorize the output with ANSI escape sequences.
/// - Returns: The formatted diagnostic error string.
public func formattedDescription(fileName: String, colorize: Bool = Self.shouldColorize) -> String {
let displayFileName = fileName == "-" ? "<stdin>" : fileName
let converter = SourceLocationConverter(fileName: displayFileName, tree: node.root)
let startLocation = converter.location(for: node.positionAfterSkippingLeadingTrivia)
let endLocation = converter.location(for: node.endPositionBeforeTrailingTrivia)
let sourceText = node.root.description
let lines = sourceText.split(omittingEmptySubsequences: false, whereSeparator: \.isNewline)
let startLineIndex = max(0, min(lines.count - 1, startLocation.line - 1))
let mainLine = String(lines[startLineIndex])
let lineNumberWidth = max(3, String(lines.count).count)
let severityLabel = severity.rawValue
let severityColor = severity == .warning ? ANSI.boldYellow : ANSI.boldRed
let header: String = {
guard colorize else {
return "\(displayFileName):\(startLocation.line):\(startLocation.column): \(severityLabel): \(message)"
}
return
"\(displayFileName):\(startLocation.line):\(startLocation.column): \(severityColor)\(severityLabel): \(ANSI.boldDefault)\(message)\(ANSI.reset)"
}()
let highlightStartColumn = min(max(1, startLocation.column), mainLine.utf8.count + 1)
let availableColumns = max(0, mainLine.utf8.count - (highlightStartColumn - 1))
let rawHighlightLength: Int = {
guard availableColumns > 0 else { return 0 }
if startLocation.line == endLocation.line {
return max(1, min(endLocation.column - startLocation.column, availableColumns))
} else {
return min(1, availableColumns)
}
}()
let highlightLength = min(rawHighlightLength, availableColumns)
let formattedMainLine: String = {
guard colorize, highlightLength > 0 else { return mainLine }
let startIndex = Self.index(atUTF8Offset: highlightStartColumn - 1, in: mainLine)
let endIndex = Self.index(atUTF8Offset: highlightStartColumn - 1 + highlightLength, in: mainLine)
let prefix = String(mainLine[..<startIndex])
let highlighted = String(mainLine[startIndex..<endIndex])
let suffix = String(mainLine[endIndex...])
return prefix + ANSI.underline + highlighted + ANSI.reset + suffix
}()
var descriptionParts = [header]
// Include up to the previous three lines for context
for offset in (-3)...(-1) {
let lineIndex = startLineIndex + offset
guard lineIndex >= 0, lineIndex < lines.count else { continue }
descriptionParts.append(
Self.formatSourceLine(
number: lineIndex + 1,
text: String(lines[lineIndex]),
width: lineNumberWidth,
colorize: colorize
)
)
}
descriptionParts.append(
Self.formatSourceLine(
number: startLocation.line,
text: formattedMainLine,
width: lineNumberWidth,
colorize: colorize
)
)
let pointerSpacing = max(0, highlightStartColumn - 1)
let pointerMessage: String = {
let pointer = String(repeating: " ", count: pointerSpacing) + "`- "
guard colorize else { return pointer + "\(severityLabel): \(message)" }
return pointer + "\(severityColor)\(severityLabel): \(ANSI.boldDefault)\(message)\(ANSI.reset)"
}()
descriptionParts.append(
Self.formatSourceLine(
number: nil,
text: pointerMessage,
width: lineNumberWidth,
colorize: colorize
)
)
if startLineIndex + 1 < lines.count {
descriptionParts.append(
Self.formatSourceLine(
number: startLocation.line + 1,
text: String(lines[startLineIndex + 1]),
width: lineNumberWidth,
colorize: colorize
)
)
}
if let hint {
descriptionParts.append("Hint: \(hint)")
}
return descriptionParts.joined(separator: "\n")
}
private static func formatSourceLine(
number: Int?,
text: String,
width: Int,
colorize: Bool
) -> String {
let gutter: String
if let number {
let paddedNumber = String(repeating: " ", count: max(0, width - String(number).count)) + String(number)
gutter = colorize ? ANSI.cyan + paddedNumber + ANSI.reset : paddedNumber
} else {
gutter = String(repeating: " ", count: width)
}
return "\(gutter) | \(text)"
}
public static var shouldColorize: Bool {
let env = ProcessInfo.processInfo.environment
let termIsDumb = env["TERM"] == "dumb"
return env["NO_COLOR"] == nil && !termIsDumb
}
private static func index(atUTF8Offset offset: Int, in line: String) -> String.Index {
let clamped = max(0, min(offset, line.utf8.count))
let utf8Index = line.utf8.index(line.utf8.startIndex, offsetBy: clamped)
// String.Index initializer is guaranteed to succeed because the UTF8 index comes from the same string.
return String.Index(utf8Index, within: line)!
}
}
/// Carries the diagnostics produced during SwiftToSkeleton.
public struct BridgeJSCoreDiagnosticError: Swift.Error, CustomStringConvertible {
public let diagnostics: [(file: String, diagnostic: DiagnosticError)]
public init(diagnostics: [(file: String, diagnostic: DiagnosticError)]) {
self.diagnostics = diagnostics
}
public var description: String {
diagnostics
.map { (file, diag) in diag.formattedDescription(fileName: file, colorize: false) }
.joined(separator: "\n")
}
}
private enum ANSI {
static let reset = "\u{001B}[0;0m"
static let boldRed = "\u{001B}[1;31m"
static let boldYellow = "\u{001B}[1;33m"
static let boldDefault = "\u{001B}[1;39m"
static let cyan = "\u{001B}[0;36m"
static let underline = "\u{001B}[4;39m"
}
// MARK: - BridgeJSCoreError
public struct BridgeJSCoreError: Swift.Error, CustomStringConvertible {
public let description: String
public init(_ message: String) {
self.description = message
}
}
// MARK: - BridgeJSConfig
import struct Foundation.URL
import struct Foundation.Data
import class Foundation.FileManager
import class Foundation.JSONDecoder
/// Configuration file representation for BridgeJS.
public struct BridgeJSConfig: Codable {
/// A mapping of tool names to their override paths.
///
/// If not present, the tool will be searched for in the system PATH.
public var tools: [String: String]?
/// Whether to expose exported Swift APIs to the global namespace.
///
/// When `true`, exported functions, classes, and namespaces are available
/// via `globalThis` in JavaScript. When `false`, they are only available
/// through the exports object returned by `createExports()`.
///
/// Default: `false`
public var exposeToGlobal: Bool
/// The identity mode to use for exported Swift heap objects.
///
/// When `"pointer"`, Swift heap objects are tracked by pointer identity,
/// enabling identity-based caching. When `"none"` or `nil`, no identity
/// tracking is performed.
///
/// Default: `nil` (treated as `"none"`)
public var identityMode: String?
public init(tools: [String: String]? = nil, exposeToGlobal: Bool = false, identityMode: String? = nil) {
self.tools = tools
self.exposeToGlobal = exposeToGlobal
self.identityMode = identityMode
}
enum CodingKeys: String, CodingKey {
case tools
case exposeToGlobal
case identityMode
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
tools = try container.decodeIfPresent([String: String].self, forKey: .tools)
exposeToGlobal = try container.decodeIfPresent(Bool.self, forKey: .exposeToGlobal) ?? false
identityMode = try container.decodeIfPresent(String.self, forKey: .identityMode)
}
/// Load the configuration file from the SwiftPM package target directory.
///
/// Files are loaded **in this order** and merged (later files override earlier ones):
/// 1. `bridge-js.config.json`
/// 2. `bridge-js.config.local.json`
public static func load(targetDirectory: URL) throws -> BridgeJSConfig {
// Define file paths in priority order: base first, then local overrides
let files = [
targetDirectory.appendingPathComponent("bridge-js.config.json"),
targetDirectory.appendingPathComponent("bridge-js.config.local.json"),
]
var config = BridgeJSConfig()
for file in files {
do {
if let loaded = try loadConfig(from: file) {
config = config.merging(overrides: loaded)
}
} catch {
throw BridgeJSCoreError("Failed to parse \(file.path): \(error)")
}
}
return config
}
/// Load a config file from the given URL if it exists, otherwise return nil
private static func loadConfig(from url: URL) throws -> BridgeJSConfig? {
guard FileManager.default.fileExists(atPath: url.path) else {
return nil
}
let data = try Data(contentsOf: url)
return try JSONDecoder().decode(BridgeJSConfig.self, from: data)
}
/// Merge the current configuration with the overrides.
func merging(overrides: BridgeJSConfig) -> BridgeJSConfig {
return BridgeJSConfig(
tools: (tools ?? [:]).merging(overrides.tools ?? [:], uniquingKeysWith: { $1 }),
exposeToGlobal: overrides.exposeToGlobal,
identityMode: overrides.identityMode ?? identityMode
)
}
}