File tree Expand file tree Collapse file tree 3 files changed +80
-80
lines changed
Expand file tree Collapse file tree 3 files changed +80
-80
lines changed Original file line number Diff line number Diff line change @@ -254,3 +254,50 @@ public struct BridgeJSConfig: Codable {
254254 )
255255 }
256256}
257+
258+ /// A printer for code fragments.
259+ public final class CodeFragmentPrinter {
260+ private( set) var lines : [ String ] = [ ]
261+ private var indentLevel : Int = 0
262+
263+ init ( header: String = " " ) {
264+ self . lines. append ( contentsOf: header. split ( separator: " \n " ) . map { String ( $0) } )
265+ }
266+
267+ func nextLine( ) {
268+ lines. append ( " " )
269+ }
270+
271+ func write< S: StringProtocol > ( _ line: S ) {
272+ if line. isEmpty {
273+ // Empty lines should not have trailing spaces
274+ lines. append ( " " )
275+ return
276+ }
277+ lines. append ( String ( repeating: " " , count: indentLevel * 4 ) + String( line) )
278+ }
279+
280+ func write( lines: [ String ] ) {
281+ for line in lines {
282+ write ( line)
283+ }
284+ }
285+
286+ func write( contentsOf printer: CodeFragmentPrinter ) {
287+ self . write ( lines: printer. lines)
288+ }
289+
290+ func indent( ) {
291+ indentLevel += 1
292+ }
293+
294+ func unindent( ) {
295+ indentLevel -= 1
296+ }
297+
298+ func indent( _ body: ( ) throws -> Void ) rethrows {
299+ indentLevel += 1
300+ try body ( )
301+ indentLevel -= 1
302+ }
303+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ /// Registry for JS helper intrinsics used during code generation.
2+ final class JSIntrinsicRegistry {
3+ private var entries : [ String : [ String ] ] = [ : ]
4+
5+ var isEmpty : Bool {
6+ entries. isEmpty
7+ }
8+
9+ func register( name: String , build: ( CodeFragmentPrinter ) throws -> Void ) rethrows {
10+ guard entries [ name] == nil else { return }
11+ let printer = CodeFragmentPrinter ( )
12+ try build ( printer)
13+ entries [ name] = printer. lines
14+ }
15+
16+ func reset( ) {
17+ entries. removeAll ( )
18+ }
19+
20+ func emitLines( ) -> [ String ] {
21+ var emitted : [ String ] = [ ]
22+ for key in entries. keys. sorted ( ) {
23+ if let lines = entries [ key] {
24+ emitted. append ( contentsOf: lines)
25+ emitted. append ( " " )
26+ }
27+ }
28+ if emitted. last == " " {
29+ emitted. removeLast ( )
30+ }
31+ return emitted
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments