Skip to content

Commit 103c39e

Browse files
committed
Merge branch 'worker' into constructors
1 parent ab91a07 commit 103c39e

File tree

6 files changed

+1344
-648
lines changed

6 files changed

+1344
-648
lines changed

Diff for: TS.fsx

+92-85
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ open System.Text.RegularExpressions
99
open System.Web
1010
open Microsoft.FSharp.Reflection
1111
open FSharp.Data
12+
open System.Linq
1213

1314
module GlobalVars =
1415
let inputFolder = Path.Combine(__SOURCE_DIRECTORY__, "inputfiles")
@@ -122,6 +123,22 @@ module Types =
122123
type InterfaceOrNamespace =
123124
| Interface of Browser.Interface
124125
| Namespace of Browser.Namespace
126+
member self.Name =
127+
match self with
128+
| Interface i -> i.Name
129+
| Namespace n -> n.Name
130+
member self.Exposed =
131+
match self with
132+
| Interface i -> i.Exposed
133+
| Namespace n -> n.Exposed
134+
member self.Properties =
135+
match self with
136+
| Interface i -> i.Properties
137+
| Namespace n -> n.Properties
138+
member self.Methods =
139+
match self with
140+
| Interface i -> i.Methods
141+
| Namespace n -> n.Methods
125142

126143
// Note:
127144
// Eventhandler's name and the eventName are not just off by "on".
@@ -277,8 +294,19 @@ module Data =
277294
let browser =
278295
(new StreamReader(Path.Combine(GlobalVars.inputFolder, "browser.webidl.xml"))).ReadToEnd() |> Browser.Parse
279296

280-
let worker =
281-
(new StreamReader(Path.Combine(GlobalVars.inputFolder, "webworkers.specidl.xml"))).ReadToEnd() |> Browser.Parse
297+
let knownExposures =
298+
let asArray (k: string, v: JsonValue) =
299+
match v with
300+
| JsonValue.String s -> (k, s.Split [| ' ' |])
301+
| _ -> failwith "Incorrect format"
302+
303+
File.ReadAllText(Path.Combine(GlobalVars.inputFolder, "knownExposures.json"))
304+
|> JsonValue.Parse
305+
|> JsonExtensions.Properties
306+
|> Array.map asArray
307+
|> Map.ofArray
308+
309+
let WorkerTypes = [| "Worker"; "DedicatedWorker"; "SharedWorker"; "ServiceWorker" |]
282310

283311
/// Check if the given element should be disabled or not
284312
/// reason is that ^a can be an interface, property or method, but they
@@ -291,22 +319,40 @@ module Data =
291319
match flavor with
292320
| Flavor.All -> true
293321
| Flavor.Web -> Array.contains "Window" exposedArray
294-
| Flavor.Worker -> true
295-
| _ -> true
322+
| Flavor.Worker -> (exposedArray.Intersect WorkerTypes).Count() <> 0
323+
| _ ->
324+
match flavor with
325+
| Flavor.All | Flavor.Web -> true
326+
| Flavor.Worker -> false
296327
filterByTag
297328

329+
let inline ShouldKeepInherit flavor inheritedExposure (i: ^a when ^a: (member Exposed: string option)) =
330+
let exposure = (^a: (member Exposed: string option) i);
331+
if exposure.IsSome then ShouldKeep flavor i
332+
else ShouldKeep flavor inheritedExposure
333+
334+
let inline ShouldKeepUnexposed flavor name =
335+
let exposure = knownExposures.TryFind name
336+
match flavor with
337+
| Flavor.All -> true
338+
| Flavor.Web -> exposure.IsNone || Array.contains "Window" exposure.Value
339+
| Flavor.Worker -> exposure.IsSome
340+
341+
let inline getName ((i: ^a when ^a: (member Name: string))) =
342+
(^a: (member Name: string) i)
343+
344+
let inline getTypedefName ((i: ^a when ^a: (member NewType: string))) =
345+
(^a: (member NewType: string) i)
346+
298347
// Global interfacename to interface object map
299348
let allWebNonCallbackInterfaces =
300349
Array.concat [| browser.Interfaces; browser.MixinInterfaces.Interfaces |]
301350

302351
let allWebInterfaces =
303352
Array.concat [| browser.Interfaces; browser.CallbackInterfaces.Interfaces; browser.MixinInterfaces.Interfaces |]
304353

305-
let allWorkerAdditionalInterfaces =
306-
Array.concat [| worker.Interfaces; worker.MixinInterfaces.Interfaces |]
307-
308354
let allInterfaces =
309-
Array.concat [| allWebInterfaces; allWorkerAdditionalInterfaces |]
355+
Array.concat [| allWebInterfaces |]
310356

311357
let inline toNameMap< ^a when ^a: (member Name: string) > (data: array< ^a > ) =
312358
data
@@ -317,15 +363,15 @@ module Data =
317363
allInterfaces |> toNameMap
318364

319365
let allDictionariesMap =
320-
Array.concat [| browser.Dictionaries; worker.Dictionaries |]
366+
Array.concat [| browser.Dictionaries |]
321367
|> toNameMap
322368

323369
let allEnumsMap =
324-
Array.concat [| browser.Enums; worker.Enums |]
370+
Array.concat [| browser.Enums |]
325371
|> toNameMap
326372

327373
let allCallbackFuncs =
328-
Array.concat [| browser.CallbackFunctions; worker.CallbackFunctions |]
374+
Array.concat [| browser.CallbackFunctions |]
329375
|> toNameMap
330376

331377
let GetInterfaceByName = allInterfacesMap.TryFind
@@ -342,28 +388,19 @@ module Data =
342388
|> set
343389

344390
let GetNonCallbackInterfacesByFlavor flavor =
345-
match flavor with
346-
| Flavor.Web -> allWebNonCallbackInterfaces |> Array.filter (ShouldKeep Flavor.Web)
347-
| Flavor.All -> allWebNonCallbackInterfaces |> Array.filter (ShouldKeep Flavor.All)
348-
| Flavor.Worker ->
349-
let isFromBrowserXml = allWebNonCallbackInterfaces |> Array.filter (fun i -> knownWorkerInterfaces.Contains i.Name)
350-
Array.append isFromBrowserXml allWorkerAdditionalInterfaces
391+
allWebNonCallbackInterfaces |> Array.filter (ShouldKeep flavor)
351392

352393
let GetCallbackFuncsByFlavor flavor =
353-
browser.CallbackFunctions
354-
|> Array.filter (fun cb -> flavor <> Flavor.Worker || knownWorkerInterfaces.Contains cb.Name)
394+
browser.CallbackFunctions |> Array.filter (getName >> ShouldKeepUnexposed flavor)
355395

356396
let GetEnumsByFlavor flavor =
357-
match flavor with
358-
| Flavor.Web | Flavor.All -> browser.Enums
359-
| Flavor.Worker ->
360-
let isFromBrowserXml = browser.Enums |> Array.filter (fun i -> knownWorkerEnums.Contains i.Name)
361-
Array.append isFromBrowserXml worker.Enums
397+
browser.Enums |> Array.filter (getName >> ShouldKeepUnexposed flavor)
362398

363399
let GetNamespacesByFlavor flavor =
364-
match flavor with
365-
| Flavor.Web | Flavor.All -> browser.Namespaces
366-
| Flavor.Worker -> worker.Namespaces
400+
browser.Namespaces |> Array.filter (ShouldKeep flavor)
401+
402+
let GetTypedefsByFlavor flavor =
403+
browser.Typedefs |> Array.filter (getTypedefName >> ShouldKeepUnexposed flavor)
367404

368405
/// Event name to event type map
369406
let eNameToEType =
@@ -461,22 +498,22 @@ module Data =
461498
| Some i -> List.ofArray i.Implements
462499
| _ -> []
463500

464-
Array.concat [| allWebNonCallbackInterfaces; worker.Interfaces; worker.MixinInterfaces.Interfaces |]
501+
Array.concat [| allWebNonCallbackInterfaces |]
465502
|> Array.map (fun i -> (i.Name, List.concat [ (getExtendList i.Name); (getImplementList i.Name) ]))
466503
|> Map.ofArray
467504

468505
/// Distinct event type list, used in the "createEvent" function
469506
let distinctETypeList =
470507
let usedEvents =
471-
[ for i in GetNonCallbackInterfacesByFlavor Flavor.All do
508+
[ for i in GetNonCallbackInterfacesByFlavor Flavor.Web do
472509
match i.Events with
473510
| Some es -> yield! es.Events
474511
| _ -> () ]
475512
|> List.map (fun e -> e.Type)
476513
|> List.distinct
477514

478515
let unUsedEvents =
479-
GetNonCallbackInterfacesByFlavor Flavor.All
516+
GetNonCallbackInterfacesByFlavor Flavor.Web
480517
|> Array.choose (fun i ->
481518
if i.Extends = "Event" && i.Name.EndsWith("Event") && not (List.contains i.Name usedEvents) then Some(i.Name) else None)
482519
|> Array.distinct
@@ -569,7 +606,7 @@ module Data =
569606
let GetGlobalPollutor flavor =
570607
match flavor with
571608
| Flavor.Web | Flavor.All -> browser.Interfaces |> Array.tryFind (fun i -> i.PrimaryGlobal.IsSome)
572-
| Flavor.Worker -> worker.Interfaces |> Array.tryFind (fun i -> i.Global.IsSome)
609+
| Flavor.Worker -> browser.Interfaces |> Array.tryFind (fun i -> i.Global.IsSome && i.Name = "DedicatedWorkerGlobalScope")
573610

574611
let GetGlobalPollutorName flavor =
575612
match GetGlobalPollutor flavor with
@@ -722,7 +759,8 @@ module Emit =
722759
| integerType when List.contains integerType integerTypes -> "number"
723760
| extendedType when List.contains extendedType extendedTypes -> extendedType
724761
| _ ->
725-
if ignoreDOMTypes && Seq.contains objDomType ["Element"; "Window"; "Document"] then "any"
762+
if ignoreDOMTypes && (Seq.contains objDomType ["Element"; "Window"; "Document"; "WindowProxy"; "WebGLRenderingContext"] || objDomType.StartsWith "HTML") then "never"
763+
elif objDomType = "WindowProxy" then "Window"
726764
else
727765
// Name of an interface / enum / dict. Just return itself
728766
if allInterfacesMap.ContainsKey objDomType ||
@@ -898,15 +936,6 @@ module Emit =
898936
| _ -> ""
899937

900938
let EmitProperties flavor prefix (emitScope: EmitScope) (i: InterfaceOrNamespace) (conflictedMembers: Set<string>) =
901-
let name =
902-
match i with
903-
| InterfaceOrNamespace.Interface it -> it.Name
904-
| InterfaceOrNamespace.Namespace n -> n.Name
905-
let properties =
906-
match i with
907-
| InterfaceOrNamespace.Interface it -> it.Properties
908-
| InterfaceOrNamespace.Namespace n -> n.Properties
909-
910939
let emitPropertyFromJson (p: InputJsonType.Root) =
911940
let readOnlyModifier =
912941
match p.Readonly with
@@ -915,7 +944,7 @@ module Emit =
915944
Pt.Printl "%s%s%s: %s;" prefix readOnlyModifier p.Name.Value p.Type.Value
916945

917946
let emitCommentForProperty (printLine: Printf.StringFormat<_, unit> -> _) pName =
918-
match CommentJson.GetCommentForProperty name pName with
947+
match CommentJson.GetCommentForProperty i.Name pName with
919948
| Some comment -> printLine "%s" comment
920949
| _ -> ()
921950

@@ -925,10 +954,10 @@ module Emit =
925954
emitCommentForProperty printLine p.Name
926955

927956
// Treat window.name specially because of https://github.com/Microsoft/TypeScript/issues/9850
928-
if p.Name = "name" && name = "Window" && emitScope = EmitScope.All then
957+
if p.Name = "name" && i.Name = "Window" && emitScope = EmitScope.All then
929958
printLine "declare const name: never;"
930-
elif Option.isNone (getRemovedItemByName p.Name ItemKind.Property name) then
931-
match getOverriddenItemByName p.Name ItemKind.Property name with
959+
elif Option.isNone (getRemovedItemByName p.Name ItemKind.Property i.Name) then
960+
match getOverriddenItemByName p.Name ItemKind.Property i.Name with
932961
| Some p' -> emitPropertyFromJson p'
933962
| None ->
934963
let pType =
@@ -956,29 +985,19 @@ module Emit =
956985
// Note: the schema file shows the property doesn't have "static" attribute,
957986
// therefore all properties are emited for the instance type.
958987
if emitScope <> StaticOnly then
959-
match properties with
988+
match i.Properties with
960989
| Some ps ->
961990
ps.Properties
962-
|> Array.filter (ShouldKeep flavor)
991+
|> Array.filter (ShouldKeepInherit flavor i)
963992
|> Array.iter emitProperty
964993
| None -> ()
965994

966995
for addedItem in getAddedItems ItemKind.Property flavor do
967-
if (matchInterface name addedItem) && (prefix <> "declare var " || addedItem.ExposeGlobally.IsNone || addedItem.ExposeGlobally.Value) then
996+
if (matchInterface i.Name addedItem) && (prefix <> "declare var " || addedItem.ExposeGlobally.IsNone || addedItem.ExposeGlobally.Value) then
968997
emitCommentForProperty Pt.Printl addedItem.Name.Value
969998
emitPropertyFromJson addedItem
970999

9711000
let EmitMethods flavor prefix (emitScope: EmitScope) (i: InterfaceOrNamespace) (conflictedMembers: Set<string>) =
972-
let name =
973-
match i with
974-
| InterfaceOrNamespace.Interface it -> it.Name
975-
| InterfaceOrNamespace.Namespace n -> n.Name
976-
977-
let methods =
978-
match i with
979-
| InterfaceOrNamespace.Interface it -> it.Methods
980-
| InterfaceOrNamespace.Namespace n -> n.Methods
981-
9821001
// Note: two cases:
9831002
// 1. emit the members inside a interface -> no need to add prefix
9841003
// 2. emit the members outside to expose them (for "Window") -> need to add "declare"
@@ -987,7 +1006,7 @@ module Emit =
9871006

9881007
let emitCommentForMethod (printLine: Printf.StringFormat<_, unit> -> _) (mName: string option) =
9891008
if mName.IsSome then
990-
match CommentJson.GetCommentForMethod name mName.Value with
1009+
match CommentJson.GetCommentForMethod i.Name mName.Value with
9911010
| Some comment -> printLine "%s" comment
9921011
| _ -> ()
9931012

@@ -1008,8 +1027,8 @@ module Emit =
10081027
// - removedType: meaning the type is marked as removed in the json file
10091028
// if there is any conflicts between the two, the "removedType" has a higher priority over
10101029
// the "overridenType".
1011-
let removedType = Option.bind (fun name -> InputJson.getRemovedItemByName name InputJson.ItemKind.Method name) m.Name
1012-
let overridenType = Option.bind (fun mName -> InputJson.getOverriddenItemByName mName InputJson.ItemKind.Method name) m.Name
1030+
let removedType = Option.bind (fun name -> InputJson.getRemovedItemByName name InputJson.ItemKind.Method i.Name) m.Name
1031+
let overridenType = Option.bind (fun mName -> InputJson.getOverriddenItemByName mName InputJson.ItemKind.Method i.Name) m.Name
10131032

10141033
if removedType.IsNone then
10151034
match overridenType with
@@ -1019,7 +1038,7 @@ module Emit =
10191038
| _ -> ()
10201039
t.Signatures |> Array.iter (printLine "%s%s;" prefix)
10211040
| None ->
1022-
match name, m.Name with
1041+
match i.Name, m.Name with
10231042
| _, Some "createElement" -> EmitCreateElementOverloads m
10241043
| _, Some "createEvent" -> EmitCreateEventOverloads m
10251044
| _, Some "getElementsByTagName" -> EmitGetElementsByTagNameOverloads m
@@ -1028,7 +1047,7 @@ module Emit =
10281047
| _ ->
10291048
if m.Name.IsSome then
10301049
// If there are added overloads from the json files, print them first
1031-
match getAddedItemByName m.Name.Value ItemKind.SignatureOverload name with
1050+
match getAddedItemByName m.Name.Value ItemKind.SignatureOverload i.Name with
10321051
| Some ol -> ol.Signatures |> Array.iter (printLine "%s;")
10331052
| _ -> ()
10341053

@@ -1040,29 +1059,26 @@ module Emit =
10401059
if isNullable then makeNullable returnType else returnType
10411060
printLine "%s%s(%s): %s;" prefix (if m.Name.IsSome then m.Name.Value else "") paramsString returnString
10421061

1043-
if methods.IsSome then
1044-
methods.Value.Methods
1062+
if i.Methods.IsSome then
1063+
i.Methods.Value.Methods
1064+
|> Array.filter (ShouldKeepInherit flavor i)
10451065
|> Array.filter mFilter
10461066
|> Array.iter (emitMethod flavor prefix i)
10471067

10481068
for addedItem in getAddedItems ItemKind.Method flavor do
1049-
if (matchInterface name addedItem && matchScope emitScope addedItem) then
1069+
if (matchInterface i.Name addedItem && matchScope emitScope addedItem) then
10501070
emitCommentForMethod Pt.Printl addedItem.Name
10511071
emitMethodFromJson addedItem
10521072

10531073
// The window interface inherited some methods from "Object",
10541074
// which need to explicitly exposed
1055-
if name = "Window" && prefix = "declare function " then
1075+
if i.Name = "Window" && prefix = "declare function " then
10561076
Pt.Printl "declare function toString(): string;"
10571077

10581078
/// Emit the properties and methods of a given interface
10591079
let EmitMembers flavor (prefix: string) (emitScope: EmitScope) (i: InterfaceOrNamespace) =
1060-
let name =
1061-
match i with
1062-
| InterfaceOrNamespace.Interface it -> it.Name
1063-
| InterfaceOrNamespace.Namespace n -> n.Name
10641080
let conflictedMembers =
1065-
match Map.tryFind name extendConflictsBaseTypes with
1081+
match Map.tryFind i.Name extendConflictsBaseTypes with
10661082
| Some conflict -> conflict.MemberNames
10671083
| _ -> []
10681084
|> Set.ofList
@@ -1427,12 +1443,9 @@ module Emit =
14271443
Pt.Printl ""
14281444

14291445
browser.Dictionaries
1430-
|> Array.filter (fun dict -> flavor <> Worker || knownWorkerInterfaces.Contains dict.Name)
1446+
|> Array.filter (getName >> ShouldKeepUnexposed flavor)
14311447
|> Array.iter emitDictionary
14321448

1433-
if flavor = Worker then
1434-
worker.Dictionaries |> Array.iter emitDictionary
1435-
14361449
let EmitAddedInterface (ai: InputJsonType.Root) =
14371450
match ai.Extends with
14381451
| Some e -> Pt.Printl "interface %s extends %s {" ai.Name.Value ai.Extends.Value
@@ -1481,15 +1494,9 @@ module Emit =
14811494
let emitTypeDefFromJson (typeDef: InputJsonType.Root) =
14821495
Pt.Printl "type %s = %s;" typeDef.Name.Value typeDef.Type.Value
14831496

1484-
match flavor with
1485-
| Flavor.Worker ->
1486-
browser.Typedefs
1487-
|> Array.filter (fun typedef -> knownWorkerInterfaces.Contains typedef.NewType)
1488-
|> Array.iter emitTypeDef
1489-
| _ ->
1490-
browser.Typedefs
1491-
|> Array.filter (fun typedef -> getRemovedItemByName typedef.NewType ItemKind.TypeDef "" |> Option.isNone)
1492-
|> Array.iter emitTypeDef
1497+
GetTypedefsByFlavor flavor
1498+
|> Array.filter (fun typedef -> getRemovedItemByName typedef.NewType ItemKind.TypeDef "" |> Option.isNone)
1499+
|> Array.iter emitTypeDef
14931500

14941501
InputJson.getAddedItems ItemKind.TypeDef flavor
14951502
|> Array.iter emitTypeDefFromJson

0 commit comments

Comments
 (0)