-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
attribute.js
132 lines (113 loc) · 4.44 KB
/
attribute.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"use strict";
const conversions = require("webidl-conversions");
const utils = require("../utils");
const reflector = require("../reflector");
const Types = require("../types");
class Attribute {
constructor(ctx, I, idl) {
this.ctx = ctx;
this.interface = I;
this.idl = idl;
this.static = idl.special === "static";
}
generate() {
const requires = new utils.RequiresMap(this.ctx);
const configurable = !utils.getExtAttr(this.idl.extAttrs, "Unforgeable");
const shouldReflect = utils.getExtAttr(this.idl.extAttrs, "Reflect");
const sameObject = utils.getExtAttr(this.idl.extAttrs, "SameObject");
const onInstance = utils.isOnInstance(this.idl, this.interface.idl);
let objName = `this`;
if (onInstance) { // we're in a setup method
objName = `obj`;
}
let brandCheck = `
if (!this || !module.exports.is(this)) {
throw new TypeError("Illegal invocation");
}
`;
let getterBody = `return utils.tryWrapperForImpl(${objName}[impl]["${this.idl.name}"]);`;
let setterBody = `${objName}[impl]["${this.idl.name}"] = V;`;
if (conversions[this.idl.idlType.idlType]) {
getterBody = `return ${objName}[impl]["${this.idl.name}"];`;
}
const addMethod = this.static ?
this.interface.addStaticMethod.bind(this.interface) :
this.interface.addMethod.bind(this.interface, onInstance ? "instance" : "prototype");
if (this.static) {
brandCheck = "";
getterBody = `return Impl.implementation["${this.idl.name}"];`;
setterBody = `Impl.implementation["${this.idl.name}"] = V;`;
} else if (shouldReflect) {
if (!reflector[this.idl.idlType.idlType]) {
throw new Error("Unknown reflector type: " + this.idl.idlType.idlType);
}
const attrName = shouldReflect.rhs && shouldReflect.rhs.value.replace(/_/g, "-") || this.idl.name;
getterBody = reflector[this.idl.idlType.idlType].get(objName, attrName.toLowerCase());
setterBody = reflector[this.idl.idlType.idlType].set(objName, attrName.toLowerCase());
}
if (utils.getExtAttr(this.idl.extAttrs, "LenientThis")) {
brandCheck = "";
}
if (sameObject) {
getterBody = `return utils.getSameObject(this, "${this.idl.name}", () => { ${getterBody} });`;
}
if (utils.hasCEReactions(this.idl)) {
const processorConfig = { requires };
getterBody = this.ctx.invokeProcessCEReactions(getterBody, processorConfig);
setterBody = this.ctx.invokeProcessCEReactions(setterBody, processorConfig);
}
addMethod(this.idl.name, [], `
${brandCheck}
${getterBody}
`, "get", { configurable });
if (!this.idl.readonly) {
let idlConversion;
if (typeof this.idl.idlType.idlType === "string" && !this.idl.idlType.nullable &&
this.ctx.enumerations.has(this.idl.idlType.idlType)) {
requires.add(this.idl.idlType.idlType);
idlConversion = `
V = \`\${V}\`;
if (!${this.idl.idlType.idlType}.enumerationValues.has(V)) {
return;
}
`;
} else {
const conv = Types.generateTypeConversion(
this.ctx, "V", this.idl.idlType, this.idl.extAttrs, this.interface.name,
`"Failed to set the '${this.idl.name}' property on '${this.interface.name}': The provided value"`);
requires.merge(conv.requires);
idlConversion = conv.body;
}
addMethod(this.idl.name, ["V"], `
${brandCheck}
${idlConversion}
${setterBody}
`, "set", { configurable });
} else if (utils.getExtAttr(this.idl.extAttrs, "PutForwards")) {
addMethod(this.idl.name, ["V"], `
${brandCheck}
this.${this.idl.name}.${utils.getExtAttr(this.idl.extAttrs, "PutForwards").rhs.value} = V;
`, "set", { configurable });
} else if (utils.getExtAttr(this.idl.extAttrs, "Replaceable")) {
addMethod(this.idl.name, ["V"], `
${brandCheck}
Object.defineProperty(this, "${this.idl.name}", {
configurable: true,
enumerable: true,
value: V,
writable: true
});
`, "set", { configurable });
}
if (!this.static && this.idl.special === "stringifier") {
addMethod("toString", [], `
if (!this || !module.exports.is(this)) {
throw new TypeError("Illegal invocation");
}
${getterBody};
`, "regular", { configurable, writable: configurable });
}
return { requires };
}
}
module.exports = Attribute;