Skip to content

Add WebIDL typedefs by json #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 56 additions & 16 deletions TS.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ module Helpers =
match FSharpValue.GetUnionFields(x, typeof<'a>) with
| case, _ -> case.Name

let inline toNameMap< ^a when ^a: (member Name: string) > (data: array< ^a > ) =
data
|> Array.map (fun x -> ((^a: (member Name: string) x), x))
|> Map.ofArray

module Option =
let runIfSome f x =
match x with
Expand Down Expand Up @@ -131,6 +136,42 @@ module Types =

type ExtendConflict = { BaseType: string; ExtendType: string list; MemberNames: string list }

module InputIdlJson =
open Helpers
open System.Xml.Linq

type InputIdlJsonType = JsonProvider<"inputfiles/sample.webidl.json">

let inputIdl =
let jsons =
DirectoryInfo(GlobalVars.inputFolder + @"/idls").GetFiles()
|> Array.map (fun file -> file.FullName |> File.ReadAllText |> InputIdlJsonType.Parse)

let inline extractJsonArray f =
jsons |> Array.collect f |> Array.map (fun item -> (^a: (member JsonValue: JsonValue) item)) |> JsonValue.Array;

let list = [| ("typedefs", extractJsonArray (fun json -> json.Typedefs)) |]
InputIdlJsonType.Root(JsonValue.Record list)

let allTypedefsMap =
inputIdl.Typedefs |> toNameMap

let hasType itemName =
allTypedefsMap.ContainsKey itemName

// Converts new JSON types to existing matching XML types
// to reduce code duplication before removing XML support
module Compat =
let xNamespace = XNamespace.Get "http://schemas.microsoft.com/ie/webidl-xml"

let convertTypedef (i: InputIdlJsonType.Typedef) =
let typedef = XElement(xNamespace + "typedef", XAttribute (XName.Get "new-type", i.Name), XAttribute (XName.Get "type", i.Type))

if OptionCheckValue true i.Nullable then
typedef.Add(XAttribute(XName.Get "nullable", "1"))

Types.Browser.Typedef typedef

module InputJson =
open Helpers
open Types
Expand Down Expand Up @@ -243,6 +284,7 @@ module CommentJson =
| _ -> None

module Data =
open Helpers
open Types

// Used to decide if a member should be emitted given its static property and
Expand Down Expand Up @@ -299,11 +341,6 @@ module Data =
let allInterfaces =
Array.concat [| allWebInterfaces; allWorkerAdditionalInterfaces |]

let inline toNameMap< ^a when ^a: (member Name: string) > (data: array< ^a > ) =
data
|> Array.map (fun x -> ((^a: (member Name: string) x), x))
|> Map.ofArray

let allInterfacesMap =
allInterfaces |> toNameMap

Expand Down Expand Up @@ -682,7 +719,6 @@ module Emit =
| "CanvasPixelArray" -> "number[]"
| "DOMHighResTimeStamp" -> "number"
| "DOMString" -> "string"
| "DOMTimeStamp" -> "number"
| "EndOfStreamError" -> "number"
| "double" | "float" -> "number"
| "object" -> "any"
Expand All @@ -699,7 +735,8 @@ module Emit =
if allInterfacesMap.ContainsKey objDomType ||
allCallbackFuncs.ContainsKey objDomType ||
allDictionariesMap.ContainsKey objDomType ||
allEnumsMap.ContainsKey objDomType then
allEnumsMap.ContainsKey objDomType ||
InputIdlJson.hasType objDomType then
objDomType
// Name of a type alias. Just return itself
elif typeDefSet.Contains objDomType then objDomType
Expand Down Expand Up @@ -1463,16 +1500,19 @@ module Emit =
let emitTypeDefFromJson (typeDef: InputJsonType.Root) =
Pt.Printl "type %s = %s;" typeDef.Name.Value typeDef.Type.Value

match flavor with
| Flavor.Worker ->
browser.Typedefs
|> Array.filter (fun typedef -> knownWorkerInterfaces.Contains typedef.NewType)
|> Array.iter emitTypeDef
| _ ->
browser.Typedefs
|> Array.filter (fun typedef -> getRemovedItemByName typedef.NewType ItemKind.TypeDef "" |> Option.isNone)
|> Array.iter emitTypeDef
// Load typedefs from XML input
let mutable map = browser.Typedefs |> Array.map(fun i -> (i.NewType, i)) |> Map.ofArray
// Load and merge typedefs from new JSON input
InputIdlJson.inputIdl.Typedefs
|> Array.iter (InputIdlJson.Compat.convertTypedef >> (fun i -> map <- map.Add(i.NewType, i)))

// Filter by removedType.json + knownWorkerInterfaces.json
map |> Map.toArray |> Array.map snd
|> Array.filter (fun typedef -> getRemovedItemByName typedef.NewType ItemKind.TypeDef "" |> Option.isNone)
|> Array.filter (fun i -> (flavor <> Flavor.Worker || knownWorkerInterfaces.Contains i.NewType))
|> Array.iter emitTypeDef

// Load manual additions from addedType.json
InputJson.getAddedItems ItemKind.TypeDef flavor
|> Array.iter emitTypeDefFromJson

Expand Down
5 changes: 3 additions & 2 deletions baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9069,7 +9069,7 @@ declare var PopStateEvent: {

interface Position {
readonly coords: Coordinates;
readonly timestamp: number;
readonly timestamp: DOMTimeStamp;
}

declare var Position: {
Expand Down Expand Up @@ -15318,12 +15318,14 @@ declare function removeEventListener(type: string, listener: EventListener, opti
type AAGUID = string;
type AlgorithmIdentifier = string | Algorithm;
type BodyInit = Blob | BufferSource | FormData | string;
type BufferSource = ArrayBufferView | ArrayBuffer;
type ByteString = string;
type ConstrainBoolean = boolean | ConstrainBooleanParameters;
type ConstrainDOMString = string | string[] | ConstrainDOMStringParameters;
type ConstrainDouble = number | ConstrainDoubleRange;
type ConstrainLong = number | ConstrainLongRange;
type CryptoOperationData = ArrayBufferView;
type DOMTimeStamp = number;
type GLbitfield = number;
type GLboolean = boolean;
type GLbyte = number;
Expand Down Expand Up @@ -15353,7 +15355,6 @@ type payloadtype = number;
type ScrollBehavior = "auto" | "instant" | "smooth";
type ScrollLogicalPosition = "start" | "center" | "end" | "nearest";
type IDBValidKey = number | string | Date | IDBArrayKey;
type BufferSource = ArrayBuffer | ArrayBufferView;
type MouseWheelEvent = WheelEvent;
type ScrollRestoration = "auto" | "manual";
type FormDataEntryValue = string | File;
Expand Down
5 changes: 3 additions & 2 deletions baselines/webworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ declare var PerformanceTiming: {

interface Position {
readonly coords: Coordinates;
readonly timestamp: number;
readonly timestamp: DOMTimeStamp;
}

declare var Position: {
Expand Down Expand Up @@ -1910,11 +1910,12 @@ declare function removeEventListener<K extends keyof DedicatedWorkerGlobalScopeE
declare function removeEventListener(type: string, listener: EventListener, options?: boolean | EventListenerOptions): void;
type AlgorithmIdentifier = string | Algorithm;
type BodyInit = Blob | BufferSource | FormData | string;
type BufferSource = ArrayBufferView | ArrayBuffer;
type DOMTimeStamp = number;
type IDBKeyPath = string;
type RequestInfo = Request | string;
type USVString = string;
type IDBValidKey = number | string | Date | IDBArrayKey;
type BufferSource = ArrayBuffer | ArrayBufferView;
type FormDataEntryValue = string | File;
type HeadersInit = Headers | string[][] | { [key: string]: string };
type IDBCursorDirection = "next" | "nextunique" | "prev" | "prevunique";
Expand Down
5 changes: 0 additions & 5 deletions inputfiles/addedTypes.json
Original file line number Diff line number Diff line change
Expand Up @@ -803,11 +803,6 @@
"name": "failIfMajorPerformanceCaveat?",
"type": "boolean"
},
{
"kind": "typedef",
"name": "BufferSource",
"type": "ArrayBuffer | ArrayBufferView"
},
{
"kind": "interface",
"name": "RsaKeyGenParams",
Expand Down
57 changes: 57 additions & 0 deletions inputfiles/idls/Web IDL.webidl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"callbackInterfaces": [], "dictionaries": [], "enums": [], "namespaces": [],
"callbackFunctions": [
{
"name": "Function", "type": "any",
"params": [{ "name": "arguments", "type": "any", "variadic": true }]
},
{ "name": "VoidFunction", "type": "void", "params": [] }
],
"interfaces": [{
"name": "DOMException", "extends": "Object",
"exposed": ["Window", "Worker"],
"constructors": [{
"arguments": [
{ "name": "message", "type": "DOMString", "optional": true, "default": "\"\"" },
{ "name": "name", "type": "DOMString", "optional": true, "default": "\"Error\"" }
]
}],
"constants": [
{ "name": "ABORT_ERR", "type": "unsigned short", "value": "20" },
{ "name": "DATA_CLONE_ERR", "type": "unsigned short", "value": "25" },
{ "name": "DOMSTRING_SIZE_ERR", "type": "unsigned short", "value": "2" },
{ "name": "HIERARCHY_REQUEST_ERR", "type": "unsigned short", "value": "3" },
{ "name": "INDEX_SIZE_ERR", "type": "unsigned short", "value": "1" },
{ "name": "INUSE_ATTRIBUTE_ERR", "type": "unsigned short", "value": "10" },
{ "name": "INVALID_ACCESS_ERR", "type": "unsigned short", "value": "15" },
{ "name": "INVALID_CHARACTER_ERR", "type": "unsigned short", "value": "5" },
{ "name": "INVALID_MODIFICATION_ERR", "type": "unsigned short", "value": "13" },
{ "name": "INVALID_NODE_TYPE_ERR", "type": "unsigned short", "value": "24" },
{ "name": "INVALID_STATE_ERR", "type": "unsigned short", "value": "11" },
{ "name": "NAMESPACE_ERR", "type": "unsigned short", "value": "14" },
{ "name": "NETWORK_ERR", "type": "unsigned short", "value": "19" },
{ "name": "NO_DATA_ALLOWED_ERR", "type": "unsigned short", "value": "6" },
{ "name": "NO_MODIFICATION_ALLOWED_ERR", "type": "unsigned short", "value": "7" },
{ "name": "NOT_FOUND_ERR", "type": "unsigned short", "value": "8" },
{ "name": "NOT_SUPPORTED_ERR", "type": "unsigned short", "value": "9" },
{ "name": "QUOTA_EXCEEDED_ERR", "type": "unsigned short", "value": "22" },
{ "name": "SECURITY_ERR", "type": "unsigned short", "value": "18" },
{ "name": "SYNTAX_ERR", "type": "unsigned short", "value": "12" },
{ "name": "TIMEOUT_ERR", "type": "unsigned short", "value": "23" },
{ "name": "TYPE_MISMATCH_ERR", "type": "unsigned short", "value": "17" },
{ "name": "URL_MISMATCH_ERR", "type": "unsigned short", "value": "21" },
{ "name": "VALIDATION_ERR", "type": "unsigned short", "value": "16" },
{ "name": "WRONG_DOCUMENT_ERR", "type": "unsigned short", "value": "4" }
],
"attributes": [
{ "name": "code", "type": "unsigned short", "readonly": true },
{ "name": "message", "type": "DOMString", "readonly": true },
{ "name": "name", "type": "DOMString", "readonly": true }
]
}],
"typedefs": [
{ "name": "ArrayBufferView", "type": "(Int8Array or Int16Array or Int32Array or Uint8Array or Uint16Array or Uint32Array or Uint8ClampedArray or Float32Array or Float64Array or DataView)" },
{ "name": "BufferSource", "type": "(ArrayBufferView or ArrayBuffer)" },
{ "name": "DOMTimeStamp", "type": "unsigned long long" }
]
}
4 changes: 4 additions & 0 deletions inputfiles/knownWorkerInterfaces.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Blob",
"Body",
"BodyInit",
"BufferSource",
"Cache",
"CacheQueryOptions",
"CacheStorage",
Expand All @@ -18,6 +19,7 @@
"DOMError",
"DOMException",
"DOMStringList",
"DOMTimeStamp",
"DecodeErrorCallback",
"DecodeSuccessCallback",
"ErrorEvent",
Expand Down Expand Up @@ -92,6 +94,8 @@
"ServiceWorkerRegistration",
"SyncEventInit",
"SyncManager",
"TextDecodeOptions",
"TextDecoderOptions",
"USVString",
"URL",
"URLSearchParams",
Expand Down
6 changes: 5 additions & 1 deletion inputfiles/removedTypes.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,9 @@
{
"kind": "typedef",
"name": "HeadersInit"
},
{
"kind": "typedef",
"name": "ArrayBufferView"
}
]
]
23 changes: 23 additions & 0 deletions inputfiles/sample.webidl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"callbackInterfaces": [],
"enums": [],
"namespaces": [],
"callbackFunctions": [],
"dictionaries": [],
"interfaces": [],
"typedefs": [
{
"name": "ArrayBufferView",
"type": "(Int8Array or Int16Array or Int32Array or Uint8Array or Uint16Array or Uint32Array or Uint8ClampedArray or Float32Array or Float64Array or DataView)"
},
{
"name": "BufferSource",
"type": "(ArrayBufferView or ArrayBuffer)",
"nullable": true
},
{
"name": "DOMTimeStamp",
"type": "unsigned long long"
}
]
}