forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSIntrinsicRegistry.swift
More file actions
39 lines (34 loc) · 1.01 KB
/
Copy pathJSIntrinsicRegistry.swift
File metadata and controls
39 lines (34 loc) · 1.01 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
#if canImport(BridgeJSUtilities)
import BridgeJSUtilities
#endif
/// Registry for JS helper intrinsics used during code generation.
final class JSIntrinsicRegistry {
private var entries: [String: [String]] = [:]
var classNamespaces: [String: [String]] = [:]
var isEmpty: Bool {
entries.isEmpty
}
func register(name: String, build: (CodeFragmentPrinter) throws -> Void) rethrows {
guard entries[name] == nil else { return }
let printer = CodeFragmentPrinter()
try build(printer)
entries[name] = printer.lines
}
func reset() {
entries.removeAll()
classNamespaces.removeAll()
}
func emitLines() -> [String] {
var emitted: [String] = []
for key in entries.keys.sorted() {
if let lines = entries[key] {
emitted.append(contentsOf: lines)
emitted.append("")
}
}
if emitted.last == "" {
emitted.removeLast()
}
return emitted
}
}