Skip to content

Commit aafd2ac

Browse files
committed
Refactor
Change the word "dump" to "emit"; Move all the special definitions out of the source code to external json files, to make sending PRs easier; other clean-ups.
1 parent 4aade26 commit aafd2ac

File tree

7 files changed

+521
-391
lines changed

7 files changed

+521
-391
lines changed

JS.fs

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ open Shared
99
// Global Pt.print target
1010
let Pt = StringPrinter()
1111

12-
// When dump webworker interfaces dom types are ignored
12+
// When emit webworker interfaces dom types are ignored
1313
let mutable ignoreDomType = false
1414

1515
/// Return a stringbuilder
@@ -117,8 +117,8 @@ let GetDefaultValue jsType =
117117

118118
let GetJsDefaultValueForDomType (domType:string) = domType |> DomTypeToJsType |> GetDefaultValue
119119

120-
/// Dump event handlers that associated with an interface
121-
let DumpEvents (i:Browser.Interface) =
120+
/// Emit event handlers that associated with an interface
121+
let EmitEvents (i:Browser.Interface) =
122122
match iNameToEhList.TryFind i.Name with
123123
| Some ehList ->
124124
ehList
@@ -132,7 +132,7 @@ let DumpEvents (i:Browser.Interface) =
132132
else ())
133133
| None -> ()
134134

135-
let DumpProperties flavor (i:Browser.Interface) =
135+
let EmitProperties flavor (i:Browser.Interface) =
136136
let propNameToElmentMap = function
137137
| "images" -> Some "img"
138138
| "rows" -> Some "tr"
@@ -146,7 +146,7 @@ let DumpProperties flavor (i:Browser.Interface) =
146146
| "tBodies" -> Some "tbody"
147147
| _ -> None
148148

149-
let DumpProperty (p: Browser.Property) =
149+
let EmitProperty (p: Browser.Property) =
150150
let value = p.Type |> DomTypeToJsType |> GetDefaultValue
151151
match p with
152152
| _ when p.Type = "EventHandler" -> ()
@@ -179,10 +179,10 @@ let DumpProperties flavor (i:Browser.Interface) =
179179
| Some propCollection ->
180180
propCollection.Properties
181181
|> Array.filter (ShouldKeep flavor)
182-
|> Array.iter DumpProperty
182+
|> Array.iter EmitProperty
183183
| None -> ()
184184

185-
let DumpConstants suffix (i:Browser.Interface) =
185+
let EmitConstants suffix (i:Browser.Interface) =
186186
match i.Constants with
187187
| Some cCollection ->
188188
for c in cCollection.Constants do
@@ -192,26 +192,26 @@ let DumpConstants suffix (i:Browser.Interface) =
192192
| _ -> Pt.printl "%s%s.%s = %s;" i.Name suffix c.Name c.Value.Value
193193
| None -> ()
194194

195-
let DumpSignatureCommentDocs (jsFunction:Function) =
196-
let DumpSignatureDocForSingleParam (p: Param) =
195+
let EmitSignatureCommentDocs (jsFunction:Function) =
196+
let EmitSignatureDocForSingleParam (p: Param) =
197197
let pJsType = DomTypeToJsType p.Type
198198

199199
(sprintf "/// <param name='%s' type='%s'" p.Name pJsType) +
200200
(if pJsType = "Array" then sprintf " elementType='%s' " (GetElementTypeForArray p.Type) else "") +
201201
(if p.Optional then " optional='true' " else "" ) + "/>"
202202
|> Pt.printl "%s"
203203

204-
let DumpSignatureDocForSingleOverload (ol: Overload) =
204+
let EmitSignatureDocForSingleOverload (ol: Overload) =
205205
if not ol.IsEmpty then
206206
Pt.increaseIndent()
207207
match ol.ReturnTypes.Length with
208208
| 0 ->
209209
Pt.printl "/// <signature>"
210-
ol.ParamCombinations |> List.iter DumpSignatureDocForSingleParam
210+
ol.ParamCombinations |> List.iter EmitSignatureDocForSingleParam
211211
Pt.printl "/// </signature>"
212212
| 1 ->
213213
Pt.printl "/// <signature>"
214-
ol.ParamCombinations |> List.iter DumpSignatureDocForSingleParam
214+
ol.ParamCombinations |> List.iter EmitSignatureDocForSingleParam
215215
match ol.ReturnTypes.[0] with
216216
| "void" | "" -> ()
217217
| arrayType when arrayType.StartsWith("sequence<") -> Pt.printl "/// <returns type='Array' elementType='%s'/>" (GetElementTypeForArray arrayType)
@@ -222,7 +222,7 @@ let DumpSignatureCommentDocs (jsFunction:Function) =
222222
|> List.iter
223223
(fun r ->
224224
Pt.printl "/// <signature>"
225-
ol.ParamCombinations |> List.iter DumpSignatureDocForSingleParam
225+
ol.ParamCombinations |> List.iter EmitSignatureDocForSingleParam
226226
match r with
227227
| "void" | "" -> ()
228228
| arrayType when arrayType.StartsWith("sequence<") -> Pt.printl "/// <returns type='Array' elementType='%s'/>" (GetElementTypeForArray arrayType)
@@ -233,15 +233,15 @@ let DumpSignatureCommentDocs (jsFunction:Function) =
233233
else ()
234234

235235
let overloads = GetOverloads jsFunction true
236-
if not overloads.IsEmpty then List.iter DumpSignatureDocForSingleOverload overloads
236+
if not overloads.IsEmpty then List.iter EmitSignatureDocForSingleOverload overloads
237237

238-
let DumpMethods (i:Browser.Interface) =
239-
let DumpMethod (m:Browser.Method) =
238+
let EmitMethods (i:Browser.Interface) =
239+
let EmitMethod (m:Browser.Method) =
240240
// print declaration
241241
let paramsStr = String.concat ", " [for p in m.Params do yield AdjustParamName p.Name]
242242
Pt.printl "%s.%s = function(%s) {" i.Name m.Name.Value paramsStr
243243
// print comment docs
244-
DumpSignatureCommentDocs (Method m)
244+
EmitSignatureCommentDocs (Method m)
245245
// print body
246246
match i.Name, m.Name.Value with
247247
| "EventTarget", "addEventListener" -> Pt.printWithAddedIndent "_eventManager.add(this, type, listener);"
@@ -337,7 +337,7 @@ let DumpMethods (i:Browser.Interface) =
337337
Pt.printl "};"
338338

339339
match i.Methods with
340-
| Some ms -> Seq.iter DumpMethod ms.Methods
340+
| Some ms -> Seq.iter EmitMethod ms.Methods
341341
| _ -> ()
342342

343343
// Explicitly expose 'toString' method for 'window'
@@ -353,7 +353,7 @@ let DumpMethods (i:Browser.Interface) =
353353
return '';
354354
};"
355355

356-
let DumpInterfaceInit (i:Browser.Interface) =
356+
let EmitInterfaceInit (i:Browser.Interface) =
357357
let nodeType, nodeName =
358358
match i.Name with
359359
| "Text" -> "TEXT_NODE", "#text"
@@ -431,24 +431,24 @@ let RegisterPublicInterfaces flavor =
431431
i.NoInterfaceObject.IsNone then
432432
Pt.printl "_publicInterface('%s', {" i.Name
433433

434-
// Dump constants
435-
let cDump =
434+
// Emit constants
435+
let cEmit =
436436
match i.Constants with
437437
| Some (cs) ->
438438
[for c in cs.Constants do
439439
yield "'" + c.Name + "' : " + c.Value.String.Value]
440440
| _ -> []
441441

442-
// Dump static methods
443-
let mDump =
442+
// Emit static methods
443+
let mEmit =
444444
match i.Methods with
445445
| Some (ms) ->
446446
[for m in ms.Methods do
447447
if m.Static.IsSome then
448448
yield String.Format("'{0}' : {1}.{0}", m.Name.Value, i.Name)]
449449
| _ -> []
450450

451-
let combined = String.concat "," (List.append cDump mDump)
451+
let combined = String.concat "," (List.append cEmit mEmit)
452452
Pt.print "%s" (combined.Trim(','))
453453
Pt.print "}, %s);" i.Name
454454

@@ -459,29 +459,29 @@ let RegisterConstructors flavor =
459459
| Some _ -> Pt.printl "_publicInterface('%s', %sCtor , %s);" i.Name i.Name i.Name
460460
| _ -> ()
461461

462-
let DumpConstructor (i: Browser.Interface) =
462+
let EmitConstructor (i: Browser.Interface) =
463463
match i.Constructor with
464-
| Some _ -> DumpConstants "Ctor" i
464+
| Some _ -> EmitConstants "Ctor" i
465465
| None -> ()
466466

467-
let DumpInterface flavor (i:Browser.Interface) =
467+
let EmitInterface flavor (i:Browser.Interface) =
468468
Pt.printl ""
469469
Pt.printl "/* -- type: %s -- */" i.Name
470470
Pt.printl ""
471471

472-
// Dump impletented interfaces
472+
// Emit impletented interfaces
473473
i.Implements |> Array.iter (fun im -> Pt.printl "_$implement(%s, %s);" i.Name im)
474474
if i.Name = GetGlobalPollutorName flavor then
475475
// if the interface is the global pollutor, inherits becomes implements
476476
Pt.printl "_$implement(%s, %s);" i.Name i.Extends
477477

478-
// Dump other contents
479-
DumpConstructor i
480-
DumpProperties flavor i
481-
DumpConstants "" i
482-
DumpMethods i
483-
DumpInterfaceInit i
484-
DumpEvents i
478+
// Emit other contents
479+
EmitConstructor i
480+
EmitProperties flavor i
481+
EmitConstants "" i
482+
EmitMethods i
483+
EmitInterfaceInit i
484+
EmitEvents i
485485

486486
// Deal with array types
487487
if i.Name.EndsWith("List") || i.Name.EndsWith("Collection") then
@@ -497,15 +497,15 @@ let DumpInterface flavor (i:Browser.Interface) =
497497
| None -> ()
498498
| None -> ()
499499

500-
let DumpCallBackFunctions flavor =
501-
let DumpCallBackFunction (cb: Browser.CallbackFunction) =
500+
let EmitCallBackFunctions flavor =
501+
let EmitCallBackFunction (cb: Browser.CallbackFunction) =
502502
let paramsStr = cb.Params |> Array.map (fun p -> p.Name) |> String.concat ", "
503503
Pt.printl "var %s = function(%s) {" cb.Name paramsStr
504-
DumpSignatureCommentDocs (CallBackFun cb)
504+
EmitSignatureCommentDocs (CallBackFun cb)
505505
if cb.Type <> "void" then Pt.printWithAddedIndent "return %s;" (DomTypeToJsType cb.Type)
506506
Pt.printl "};"
507507
GetCallbackFuncsByFlavor flavor
508-
|> Array.iter DumpCallBackFunction
508+
|> Array.iter EmitCallBackFunction
509509

510510
let RegisterCallBackFunctions flavor =
511511
let RegisterCallBackFunction (cb: Browser.CallbackFunction) =
@@ -521,26 +521,26 @@ let RegisterDictionaries () =
521521
browser.Dictionaries
522522
|> Array.iter RegisterDictionary
523523

524-
let DumpDictionaries () =
525-
let DumpDictionary (d:Browser.Dictionary) =
524+
let EmitDictionaries () =
525+
let EmitDictionary (d:Browser.Dictionary) =
526526
Pt.printl ""
527527
Pt.printl "/* -- type: %s -- */" d.Name
528528
Pt.printl ""
529529

530-
// Dump members
530+
// Emit members
531531
for m in d.Members do
532532
let defaultValue = match m.Default.String with
533533
| Some dv -> dv
534534
| None -> GetJsDefaultValueForDomType m.Type
535535
Pt.printl "%s.%s = %s;" d.Name m.Name defaultValue
536536

537-
browser.Dictionaries |> Array.iter DumpDictionary
537+
browser.Dictionaries |> Array.iter EmitDictionary
538538

539-
let DumpInterfaces flavor =
539+
let EmitInterfaces flavor =
540540
let sortedTypes = SortInterfaces (GetAllInterfacesByFlavor flavor)
541-
for t in sortedTypes do DumpInterface flavor t
541+
for t in sortedTypes do EmitInterface flavor t
542542

543-
let DumpEventTypeToObjSwitchStatement flavor ignoreCase =
543+
let EmitEventTypeToObjSwitchStatement flavor ignoreCase =
544544
Pt.printl "switch (type) {"
545545
Pt.increaseIndent()
546546

@@ -570,7 +570,7 @@ let DumpEventTypeToObjSwitchStatement flavor ignoreCase =
570570
Pt.decreaseIndent()
571571
Pt.printl "}"
572572

573-
let DumpGetElementByTagNameSwitchStatement () =
573+
let EmitGetElementByTagNameSwitchStatement () =
574574
Pt.printl "switch (tagName.toLowerCase()) {"
575575

576576
Pt.increaseIndent()
@@ -581,9 +581,9 @@ let DumpGetElementByTagNameSwitchStatement () =
581581

582582
Pt.print "}"
583583

584-
/// Dump the _createEvent function
585-
let DumpCreateEventSwitchStatement () =
586-
// Dump the switch statements
584+
/// Emit the _createEvent function
585+
let EmitCreateEventSwitchStatement () =
586+
// Emit the switch statements
587587
Pt.printl "switch(eventType.toLowerCase()) {"
588588

589589
distinctETypeList
@@ -603,8 +603,8 @@ let DumpCreateEventSwitchStatement () =
603603

604604
Pt.printl "}"
605605

606-
let DumpDeclarations flavor =
607-
let DumpInterfaceDeclaration (i:Browser.Interface) =
606+
let EmitDeclarations flavor =
607+
let EmitInterfaceDeclaration (i:Browser.Interface) =
608608
let init =
609609
match i.Name with
610610
| name when name = GetGlobalPollutorName flavor -> "this"
@@ -623,7 +623,7 @@ let DumpDeclarations flavor =
623623
"function(" + pList + ")"
624624
Pt.printl "var %sCtor = %s { " i.Name functionDeclare
625625
if ctor.Params.Length > 0 then
626-
DumpSignatureCommentDocs (Ctor ctor)
626+
EmitSignatureCommentDocs (Ctor ctor)
627627
Pt.printWithAddedIndent "return Object.create(%s);" i.Name
628628
Pt.printl "};"
629629
else
@@ -633,30 +633,30 @@ let DumpDeclarations flavor =
633633

634634
GetAllInterfacesByFlavor flavor
635635
|> SortInterfaces
636-
|> Array.iter DumpInterfaceDeclaration
636+
|> Array.iter EmitInterfaceDeclaration
637637

638638
if flavor <> Worker then
639-
let DumpDictDeclaration (d: Browser.Dictionary) =
639+
let EmitDictDeclaration (d: Browser.Dictionary) =
640640
match d.Extends with
641641
| "Object" -> Pt.printl "var %s = {};" d.Name
642642
| _ -> Pt.printl "var %s = _$inherit(%s);" d.Name d.Extends
643643
browser.Dictionaries
644644
|> SortDicts
645-
|> Array.iter DumpDictDeclaration
645+
|> Array.iter EmitDictDeclaration
646646

647-
let DumpXmlContent flavor =
648-
DumpDeclarations flavor
649-
DumpCallBackFunctions flavor
650-
DumpInterfaces flavor
647+
let EmitXmlContent flavor =
648+
EmitDeclarations flavor
649+
EmitCallBackFunctions flavor
650+
EmitInterfaces flavor
651651
if flavor <> Worker then
652-
DumpDictionaries ()
652+
EmitDictionaries ()
653653

654654

655655
let RegisterPublicObjs flavor =
656656
RegisterPublicInterfaces flavor
657657
RegisterConstructors flavor
658658

659-
/// Adjust the indention of the printer, and dump the indented content in the printer,
659+
/// Adjust the indention of the printer, and emit the indented content in the printer,
660660
/// and then replace the place holder text with the content in printer
661661
let ReplaceWithIndentedFuncResult (placeHolder: String) func (sb: StringBuilder) =
662662
let curText = sb.ToString()
@@ -667,23 +667,23 @@ let ReplaceWithIndentedFuncResult (placeHolder: String) func (sb: StringBuilder)
667667
func() |> ignore
668668
sb.Replace(placeHolder, Pt.getResult())
669669

670-
let DumpTheWholeThing flavor (target: TextWriter) =
670+
let EmitTheWholeThing flavor (target: TextWriter) =
671671
Pt.reset()
672672

673673
let template = LoadTemplate ( __SOURCE_DIRECTORY__ + @"\inputfiles\jsTemplate.js")
674674

675675
let content =
676676
template
677677
|> ReplaceWithIndentedFuncResult "<@ EventTypeToObjSwitchStatements @>"
678-
(fun () -> DumpEventTypeToObjSwitchStatement flavor false)
678+
(fun () -> EmitEventTypeToObjSwitchStatement flavor false)
679679
|> ReplaceWithIndentedFuncResult "<@ EventTypeToObjSwitchStatementsIgnoreCase @>"
680-
(fun () -> DumpEventTypeToObjSwitchStatement flavor true)
680+
(fun () -> EmitEventTypeToObjSwitchStatement flavor true)
681681
|> ReplaceWithIndentedFuncResult "<@ CreateEventSwitchStatements @>"
682-
DumpCreateEventSwitchStatement
682+
EmitCreateEventSwitchStatement
683683
|> ReplaceWithIndentedFuncResult "<@ GetElementsByTagNameSwitchStatements @>"
684-
DumpGetElementByTagNameSwitchStatement
684+
EmitGetElementByTagNameSwitchStatement
685685
|> ReplaceWithIndentedFuncResult "<@ XMLContents @>"
686-
(fun () -> DumpXmlContent flavor)
686+
(fun () -> EmitXmlContent flavor)
687687
|> ReplaceWithIndentedFuncResult "<@ Public Interfaces @>"
688688
(fun () -> RegisterPublicObjs flavor)
689689
|> (fun sb -> sb.Replace("<@ GlobalPolluter @>", GetGlobalPollutorName flavor))
@@ -692,13 +692,13 @@ let DumpTheWholeThing flavor (target: TextWriter) =
692692
fprintf target "%s" content
693693
target.Flush()
694694

695-
let DumpDomWeb () =
696-
DumpTheWholeThing Web GlobalVars.jsWebOutput
695+
let EmitDomWeb () =
696+
EmitTheWholeThing Flavor.Web GlobalVars.jsWebOutput
697697

698-
let DumpDomWin () =
699-
DumpTheWholeThing Windows GlobalVars.jsWinOutput
698+
let EmitDomWin () =
699+
EmitTheWholeThing Flavor.All GlobalVars.jsWinOutput
700700

701-
let DumpDomWorker () =
701+
let EmitDomWorker () =
702702
Pt.reset()
703703

704704
ignoreDomType <- true
@@ -707,9 +707,9 @@ let DumpDomWorker () =
707707
let content =
708708
template
709709
|> ReplaceWithIndentedFuncResult "<@ EventTypeToObjSwitchStatements @>"
710-
(fun () -> DumpEventTypeToObjSwitchStatement Worker false)
710+
(fun () -> EmitEventTypeToObjSwitchStatement Worker false)
711711
|> ReplaceWithIndentedFuncResult "<@ XMLContents @>"
712-
(fun () -> DumpXmlContent Worker)
712+
(fun () -> EmitXmlContent Worker)
713713
|> ReplaceWithIndentedFuncResult "<@ Public Interfaces @>"
714714
(fun () -> RegisterPublicObjs Worker)
715715
|> (fun sb -> sb.Replace("<@ GlobalPolluter @>", GetGlobalPollutorName Worker))

0 commit comments

Comments
 (0)