File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Load Diff This file was deleted.
Original file line number Diff line number Diff line change 11#if canImport(BridgeJSSkeleton)
22import BridgeJSSkeleton
33#endif
4+ #if canImport(BridgeJSUtilities)
5+ import BridgeJSUtilities
6+ #endif
47
58/// A scope for variables for JS glue code
69final class JSGlueVariableScope {
Original file line number Diff line number Diff line change 1+ #if canImport(BridgeJSUtilities)
2+ import BridgeJSUtilities
3+ #endif
4+
5+ /// Registry for JS helper intrinsics used during code generation.
6+ final class JSIntrinsicRegistry {
7+ private var entries : [ String : [ String ] ] = [ : ]
8+
9+ var isEmpty : Bool {
10+ entries. isEmpty
11+ }
12+
13+ func register( name: String , build: ( CodeFragmentPrinter ) throws -> Void ) rethrows {
14+ guard entries [ name] == nil else { return }
15+ let printer = CodeFragmentPrinter ( )
16+ try build ( printer)
17+ entries [ name] = printer. lines
18+ }
19+
20+ func reset( ) {
21+ entries. removeAll ( )
22+ }
23+
24+ func emitLines( ) -> [ String ] {
25+ var emitted : [ String ] = [ ]
26+ for key in entries. keys. sorted ( ) {
27+ if let lines = entries [ key] {
28+ emitted. append ( contentsOf: lines)
29+ emitted. append ( " " )
30+ }
31+ }
32+ if emitted. last == " " {
33+ emitted. removeLast ( )
34+ }
35+ return emitted
36+ }
37+ }
Original file line number Diff line number Diff line change @@ -27,3 +27,50 @@ public enum BridgeJSGeneratedFile {
2727 """
2828 }
2929}
30+
31+ /// A printer for code fragments.
32+ public final class CodeFragmentPrinter {
33+ public private( set) var lines : [ String ] = [ ]
34+ private var indentLevel : Int = 0
35+
36+ public init ( header: String = " " ) {
37+ self . lines. append ( contentsOf: header. split ( separator: " \n " ) . map { String ( $0) } )
38+ }
39+
40+ public func nextLine( ) {
41+ lines. append ( " " )
42+ }
43+
44+ public func write< S: StringProtocol > ( _ line: S ) {
45+ if line. isEmpty {
46+ // Empty lines should not have trailing spaces
47+ lines. append ( " " )
48+ return
49+ }
50+ lines. append ( String ( repeating: " " , count: indentLevel * 4 ) + String( line) )
51+ }
52+
53+ public func write( lines: [ String ] ) {
54+ for line in lines {
55+ write ( line)
56+ }
57+ }
58+
59+ public func write( contentsOf printer: CodeFragmentPrinter ) {
60+ self . write ( lines: printer. lines)
61+ }
62+
63+ public func indent( ) {
64+ indentLevel += 1
65+ }
66+
67+ public func unindent( ) {
68+ indentLevel -= 1
69+ }
70+
71+ public func indent( _ body: ( ) throws -> Void ) rethrows {
72+ indentLevel += 1
73+ try body ( )
74+ indentLevel -= 1
75+ }
76+ }
You can’t perform that action at this time.
0 commit comments