@@ -34,6 +34,11 @@ module Helpers =
34
34
match FSharpValue.GetUnionFields( x, typeof< 'a>) with
35
35
| case, _ -> case.Name
36
36
37
+ let inline toNameMap < ^a when ^a : ( member Name : string ) > ( data : array < ^a > ) =
38
+ data
39
+ |> Array.map ( fun x -> (( ^a : ( member Name : string) x), x))
40
+ |> Map.ofArray
41
+
37
42
module Option =
38
43
let runIfSome f x =
39
44
match x with
@@ -132,6 +137,131 @@ module Types =
132
137
133
138
type ExtendConflict = { BaseType: string ; ExtendType: string list ; MemberNames: string list }
134
139
140
+ module InputIdlJson =
141
+ open Helpers
142
+ open System.Xml .Linq
143
+
144
+ type InputIdlJsonType = JsonProvider< " inputfiles/sample.webidl.json" >
145
+
146
+ let inputIdl =
147
+ File.ReadAllText( GlobalVars.inputFolder + @" /browser.webidl.json" ) |> InputIdlJsonType.Parse
148
+
149
+ let allCallbackFunctionsMap =
150
+ inputIdl.CallbackFunctions |> toNameMap
151
+
152
+ let allInterfacesMap =
153
+ inputIdl.Interfaces |> toNameMap
154
+
155
+ let allDictionariesMap =
156
+ inputIdl.Dictionaries |> toNameMap
157
+
158
+ let allTypedefsMap =
159
+ inputIdl.Typedefs |> toNameMap
160
+
161
+ let hasType itemName =
162
+ allCallbackFunctionsMap.ContainsKey itemName ||
163
+ allInterfacesMap.ContainsKey itemName ||
164
+ allDictionariesMap.ContainsKey itemName ||
165
+ allTypedefsMap.ContainsKey itemName
166
+
167
+ module Compat =
168
+ let xNamespace = XNamespace.Get " http://schemas.microsoft.com/ie/webidl-xml"
169
+ let convertArgument ( i : InputIdlJsonType.Argument ) =
170
+ let param = XElement( xNamespace + " param" , XAttribute ( XName.Get " name" , i.Name), XAttribute ( XName.Get " type" , i.Type))
171
+ if OptionCheckValue true i.Optional then
172
+ param.Add ( XAttribute( XName.Get " optional" , " 1" ))
173
+ if OptionCheckValue true i.Nullable then
174
+ param.Add ( XAttribute( XName.Get " nullable" , " 1" ))
175
+ if OptionCheckValue true i.Variadic then
176
+ param.Add ( XAttribute( XName.Get " variadic" , " 1" ))
177
+ param
178
+
179
+ let convertOperation ( i : InputIdlJsonType.Operation ) =
180
+ let method = XElement( xNamespace + " method" , XAttribute ( XName.Get " name" , i.Name), XAttribute ( XName.Get " type" , i.Type))
181
+
182
+ method.Add( i.Arguments |> Array.map convertArgument)
183
+ if OptionCheckValue true i.Static then
184
+ method.Add( XAttribute( XName.Get " static" , " 1" ))
185
+ if OptionCheckValue true i.Nullable then
186
+ method.Add( XAttribute( XName.Get " nullable" , " 1" ))
187
+
188
+ method
189
+
190
+ let convertConstructor ( i : InputIdlJsonType.Constructor ) =
191
+ let constructor = XElement( xNamespace + " constructor" )
192
+
193
+ if not ( Array.isEmpty i.Arguments) then
194
+ constructor.Add( i.Arguments |> Array.map convertArgument)
195
+
196
+ constructor
197
+
198
+ let convertAttribute ( i : InputIdlJsonType.Attribute ) =
199
+ let property = XElement( xNamespace + " property" , XAttribute ( XName.Get " name" , i.Name), XAttribute ( XName.Get " type" , i.Type))
200
+
201
+ if OptionCheckValue true i.Readonly then
202
+ property.Add( XAttribute( XName.Get " read-only" , " 1" ))
203
+ if OptionCheckValue true i.Static then
204
+ property.Add( XAttribute( XName.Get " static" , " 1" ))
205
+ if OptionCheckValue true i.Stringifier then
206
+ property.Add( XAttribute( XName.Get " stringifier" , " 1" ))
207
+ if OptionCheckValue true i.Nullable then
208
+ property.Add( XAttribute( XName.Get " nullable" , " 1" ))
209
+
210
+ property
211
+
212
+ let convertConstant ( i : InputIdlJsonType.Constant ) =
213
+ XElement( xNamespace + " constant" , XAttribute ( XName.Get " name" , i.Name), XAttribute ( XName.Get " type" , i.Type), XAttribute ( XName.Get " value" , i.Value))
214
+
215
+ let convertCallbackFunction ( i : InputIdlJsonType.CallbackFunction ) =
216
+ let callbackFunction = XElement( xNamespace + " callback-function" , XAttribute ( XName.Get " name" , i.Name), XAttribute ( XName.Get " type" , i.Type))
217
+
218
+ callbackFunction.Add( i.Arguments |> Array.map convertArgument)
219
+ if OptionCheckValue true i.Nullable then
220
+ callbackFunction.Add( XAttribute( XName.Get " nullable" , " 1" ))
221
+
222
+ Types.Browser.CallbackFunction callbackFunction
223
+
224
+ let convertInterface ( i : InputIdlJsonType.Interfacis ) =
225
+ let interfaceEl = XElement( xNamespace + " interface" , XAttribute ( XName.Get " name" , i.Name))
226
+
227
+ interfaceEl.Add ( XAttribute ( XName.Get " extends" , if i.Extends.IsSome then i.Extends.Value else " Object" ))
228
+ if not ( Array.isEmpty i.Constructors) then
229
+ interfaceEl.Add( i.Constructors |> Array.map convertConstructor)
230
+ if not ( Array.isEmpty i.Operations) then
231
+ interfaceEl.Add( XElement( xNamespace + " methods" , i.Operations |> Array.map convertOperation))
232
+ if not ( Array.isEmpty i.Attributes) then
233
+ interfaceEl.Add( XElement( xNamespace + " properties" , i.Attributes |> Array.map convertAttribute))
234
+ if not ( Array.isEmpty i.Constants) then
235
+ interfaceEl.Add( XElement( xNamespace + " constants" , i.Constants |> Array.map convertConstant))
236
+
237
+ Types.Browser.Interface interfaceEl
238
+
239
+ let convertDictionary ( i : InputIdlJsonType.Dictionary ) =
240
+ let dictionary = XElement( xNamespace + " dictionary" , XAttribute ( XName.Get " name" , i.Name))
241
+
242
+ dictionary.Add ( XAttribute ( XName.Get " extends" , if i.Extends.IsSome then i.Extends.Value else " Object" ))
243
+ let members =
244
+ [ for memberDef in i.Members do
245
+ let memberEl = XElement( xNamespace + " member" , XAttribute ( XName.Get " name" , memberDef.Name), XAttribute ( XName.Get " type" , memberDef.Type))
246
+
247
+ if OptionCheckValue true memberDef.Nullable then
248
+ memberEl.Add( XAttribute( XName.Get " nullable" , " 1" ))
249
+ if OptionCheckValue true memberDef.Required then
250
+ memberEl.Add( XAttribute( XName.Get " required" , " 1" ))
251
+
252
+ yield memberEl ]
253
+
254
+ dictionary.Add( XElement( xNamespace + " members" , members))
255
+ Types.Browser.Dictionary dictionary
256
+
257
+ let convertTypedef ( i : InputIdlJsonType.Typedef ) =
258
+ let typedef = XElement( xNamespace + " typedef" , XAttribute ( XName.Get " new-type" , i.Name), XAttribute ( XName.Get " type" , i.Type))
259
+
260
+ if OptionCheckValue true i.Nullable then
261
+ typedef.Add( XAttribute( XName.Get " nullable" , " 1" ))
262
+
263
+ Types.Browser.Typedef typedef
264
+
135
265
module InputJson =
136
266
open Helpers
137
267
open Types
@@ -301,11 +431,6 @@ module Data =
301
431
let allInterfaces =
302
432
Array.concat [| allWebInterfaces; allWorkerAdditionalInterfaces |]
303
433
304
- let inline toNameMap < ^a when ^a : ( member Name : string ) > ( data : array < ^a > ) =
305
- data
306
- |> Array.map ( fun x -> (( ^a : ( member Name : string) x), x))
307
- |> Map.ofArray
308
-
309
434
let allInterfacesMap =
310
435
allInterfaces |> toNameMap
311
436
@@ -701,7 +826,6 @@ module Emit =
701
826
| " Date" -> " Date"
702
827
| " DOMHighResTimeStamp" -> " number"
703
828
| " DOMString" -> " string"
704
- | " DOMTimeStamp" -> " number"
705
829
| " EndOfStreamError" -> " number"
706
830
| " EventListener" -> " EventListenerOrEventListenerObject"
707
831
| " double" | " float" -> " number"
@@ -721,7 +845,8 @@ module Emit =
721
845
if allInterfacesMap.ContainsKey objDomType ||
722
846
allCallbackFuncs.ContainsKey objDomType ||
723
847
allDictionariesMap.ContainsKey objDomType ||
724
- allEnumsMap.ContainsKey objDomType then
848
+ allEnumsMap.ContainsKey objDomType ||
849
+ InputIdlJson.hasType objDomType then
725
850
objDomType
726
851
// Name of a type alias. Just return itself
727
852
elif typeDefSet.Contains objDomType then objDomType
@@ -880,7 +1005,12 @@ module Emit =
880
1005
getAddedItems ItemKind.Callback flavor
881
1006
|> Array.iter emitCallbackFunctionsFromJson
882
1007
883
- GetCallbackFuncsByFlavor flavor |> Array.iter emitCallBackFunction
1008
+ let map = GetCallbackFuncsByFlavor flavor |> Array.map( fun i -> ( i.Name, i)) |> dict |> Dictionary
1009
+ InputIdlJson.inputIdl.CallbackFunctions
1010
+ |> Array.filter ( fun i -> flavor <> Worker || knownWorkerInterfaces.Contains i.Name)
1011
+ |> Array.iter ( InputIdlJson.Compat.convertCallbackFunction >> ( fun i -> map.[ i.Name] <- i))
1012
+
1013
+ map.Values |> Array.ofSeq |> Array.iter emitCallBackFunction
884
1014
885
1015
let EmitEnums flavor =
886
1016
let emitEnum ( e : Browser.Enum ) =
@@ -1376,7 +1506,7 @@ module Emit =
1376
1506
if hasNonStaticMember then emitStaticInterfaceWithNonStaticMembers() else emitPureStaticInterface()
1377
1507
1378
1508
let EmitNonCallbackInterfaces flavor =
1379
- for i in GetNonCallbackInterfacesByFlavor flavor do
1509
+ let emitNonCallbackInterface ( i : Browser.Interface ) =
1380
1510
// If the static attribute has a value, it means the type doesn't have a constructor
1381
1511
if i.Static.IsSome then
1382
1512
EmitStaticInterface flavor i
@@ -1386,6 +1516,13 @@ module Emit =
1386
1516
EmitInterface flavor i
1387
1517
EmitConstructor flavor i
1388
1518
1519
+ let map = GetNonCallbackInterfacesByFlavor flavor |> Array.map( fun i -> ( i.Name, i)) |> dict |> Dictionary
1520
+ InputIdlJson.inputIdl.Interfaces
1521
+ |> Array.filter ( fun i -> flavor <> Worker || i.Exposed |> Array.contains " Worker" )
1522
+ |> Array.iter ( InputIdlJson.Compat.convertInterface >> ( fun i -> map.[ i.Name] <- i))
1523
+
1524
+ map.Values |> Array.ofSeq |> Array.iter emitNonCallbackInterface
1525
+
1389
1526
let EmitDictionaries flavor =
1390
1527
let emitDictionary ( dict : Browser.Dictionary ) =
1391
1528
match dict.Extends with
@@ -1424,12 +1561,19 @@ module Emit =
1424
1561
Pt.Printl " }"
1425
1562
Pt.Printl " "
1426
1563
1427
- browser.Dictionaries
1428
- |> Array.filter ( fun dict -> flavor <> Worker || knownWorkerInterfaces.Contains dict.Name)
1429
- |> Array.iter emitDictionary
1564
+ let map =
1565
+ browser.Dictionaries
1566
+ |> Array.filter ( fun dict -> flavor <> Worker || knownWorkerInterfaces.Contains dict.Name)
1567
+ |> Array.map( fun i -> ( i.Name, i)) |> dict |> Dictionary
1430
1568
1431
1569
if flavor = Worker then
1432
- worker.Dictionaries |> Array.iter emitDictionary
1570
+ worker.Dictionaries |> Array.iter ( fun dict -> map.[ dict.Name] <- dict)
1571
+
1572
+ InputIdlJson.inputIdl.Dictionaries
1573
+ |> Array.filter ( fun dict -> flavor <> Worker || knownWorkerInterfaces.Contains dict.Name)
1574
+ |> Array.iter ( InputIdlJson.Compat.convertDictionary >> ( fun i -> map.[ i.Name] <- i))
1575
+
1576
+ map.Values |> Array.ofSeq |> Array.iter emitDictionary
1433
1577
1434
1578
let EmitAddedInterface ( ai : InputJsonType.Root ) =
1435
1579
match ai.Extends with
@@ -1475,15 +1619,14 @@ module Emit =
1475
1619
let emitTypeDefFromJson ( typeDef : InputJsonType.Root ) =
1476
1620
Pt.Printl " type %s = %s ;" typeDef.Name.Value typeDef.Type.Value
1477
1621
1478
- match flavor with
1479
- | Flavor.Worker ->
1480
- browser.Typedefs
1481
- |> Array.filter ( fun typedef -> knownWorkerInterfaces.Contains typedef.NewType)
1482
- |> Array.iter emitTypeDef
1483
- | _ ->
1484
- browser.Typedefs
1485
- |> Array.filter ( fun typedef -> getRemovedItemByName typedef.NewType ItemKind.TypeDef " " |> Option.isNone)
1486
- |> Array.iter emitTypeDef
1622
+ let mutable map = browser.Typedefs |> Array.map( fun i -> ( i.NewType, i)) |> Map.ofArray
1623
+ InputIdlJson.inputIdl.Typedefs
1624
+ |> Array.iter ( InputIdlJson.Compat.convertTypedef >> ( fun i -> map <- map.Add( i.NewType, i)))
1625
+
1626
+ map |> Map.toArray |> Array.map snd
1627
+ |> Array.filter ( fun typedef -> getRemovedItemByName typedef.NewType ItemKind.TypeDef " " |> Option.isNone)
1628
+ |> Array.filter ( fun i -> ( flavor <> Flavor.Worker || knownWorkerInterfaces.Contains i.NewType))
1629
+ |> Array.iter emitTypeDef
1487
1630
1488
1631
InputJson.getAddedItems ItemKind.TypeDef flavor
1489
1632
|> Array.iter emitTypeDefFromJson
0 commit comments