-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
webidl2js‑globals.js
to handle setup of [Global]s
- Loading branch information
Showing
16 changed files
with
6,717 additions
and
3,956 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
"use strict"; | ||
const utils = require("./utils.js"); | ||
|
||
class Globals { | ||
constructor(ctx) { | ||
this.ctx = ctx; | ||
this.requires = new utils.RequiresMap(ctx); | ||
|
||
this.str = null; | ||
this._globalNames = new Set(); | ||
} | ||
|
||
addGlobalNames(globalNames) { | ||
if (typeof globalNames === "string") { | ||
this._globalNames.add(globalNames); | ||
} else { | ||
for (const globalName of globalNames) { | ||
this._globalNames.add(globalName.value); | ||
} | ||
} | ||
} | ||
|
||
getConstructsForGlobal(globalName) { | ||
const { ctx } = this; | ||
|
||
const constructs = []; | ||
|
||
for (const iface of [...ctx.interfaces.values(), ...ctx.callbackInterfaces.values()]) { | ||
const exposed = utils.getExtAttr(iface.idl.extAttrs, "Exposed"); | ||
|
||
if (!exposed) { | ||
continue; | ||
} else { | ||
const { rhs } = exposed; | ||
switch (rhs.type) { | ||
case "identifier": | ||
if (rhs.value !== globalName) { | ||
continue; | ||
} | ||
break; | ||
case "identifier-list": | ||
if (!rhs.value.reduce((r, t) => (t.value === globalName) || r, false)) { | ||
continue; | ||
} | ||
break; | ||
default: | ||
if (!ctx.options.suppressErrors) { | ||
throw new Error( | ||
`Unsupported ${iface.type} ${iface.name}'s [Exposed] extended attribute value type "${rhs.type}"` | ||
); | ||
} | ||
continue; | ||
} | ||
} | ||
|
||
constructs.push(iface); | ||
} | ||
|
||
// FIXME: `Element` currently gets incorrectly placed before `Node` and `EventTarget` | ||
constructs.sort((a, b) => { | ||
if (a.idl.inheritance === b.name) { | ||
return 1; | ||
} | ||
|
||
if (b.idl.inheritance === a.name) { | ||
return -1; | ||
} | ||
|
||
return 0; | ||
}); | ||
|
||
return constructs; | ||
} | ||
|
||
generate() { | ||
this.generateGlobals(); | ||
this.generateRequires(); | ||
} | ||
|
||
generateGlobals() { | ||
this.str += ` | ||
/** | ||
* Initialises the passed object as a new global. | ||
* | ||
* The object is expected to contain all the global object properties | ||
* as specified in the ECMAScript specification. | ||
* | ||
* This function has to be added to the exports object | ||
* to avoid circular dependency issues. | ||
* | ||
* @param {object} globalObject | ||
* @param {string} globalName | ||
*/ | ||
exports.setupGlobal = (globalObject, globalName) => { | ||
Object.defineProperty(globalObject, utils.ctorRegistrySymbol, { value: Object.create(null) }); | ||
switch(globalName) { | ||
`; | ||
|
||
|
||
for (const globalName of this._globalNames) { | ||
this.generateGlobal(globalName); | ||
} | ||
|
||
this.str += ` | ||
default: | ||
throw new Error(\`Internal error: Unknown global name "\${globalName}"\`); | ||
} | ||
}; | ||
`; | ||
} | ||
|
||
generateGlobal(globalName) { | ||
this.str += ` | ||
case "${globalName}": { | ||
`; | ||
|
||
for (const construct of this.getConstructsForGlobal(globalName)) { | ||
const imported = this.requires.addRelative(construct.name); | ||
this.str += `${imported}.install(globalObject);\n`; | ||
} | ||
|
||
this.str += `break; | ||
} | ||
`; | ||
} | ||
|
||
generateRequires() { | ||
this.str = ` | ||
${this.requires.generate()} | ||
${this.str} | ||
`; | ||
} | ||
|
||
toString() { | ||
this.str = ""; | ||
this.generate(); | ||
return this.str; | ||
} | ||
} | ||
|
||
module.exports = Globals; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.