PluginArray.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const implSymbol = utils.implSymbol;
  5. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  6. const interfaceName = "PluginArray";
  7. exports.is = value => {
  8. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  9. };
  10. exports.isImpl = value => {
  11. return utils.isObject(value) && value instanceof Impl.implementation;
  12. };
  13. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  14. if (exports.is(value)) {
  15. return utils.implForWrapper(value);
  16. }
  17. throw new globalObject.TypeError(`${context} is not of type 'PluginArray'.`);
  18. };
  19. function makeWrapper(globalObject, newTarget) {
  20. let proto;
  21. if (newTarget !== undefined) {
  22. proto = newTarget.prototype;
  23. }
  24. if (!utils.isObject(proto)) {
  25. proto = globalObject[ctorRegistrySymbol]["PluginArray"].prototype;
  26. }
  27. return Object.create(proto);
  28. }
  29. function makeProxy(wrapper, globalObject) {
  30. let proxyHandler = proxyHandlerCache.get(globalObject);
  31. if (proxyHandler === undefined) {
  32. proxyHandler = new ProxyHandler(globalObject);
  33. proxyHandlerCache.set(globalObject, proxyHandler);
  34. }
  35. return new Proxy(wrapper, proxyHandler);
  36. }
  37. exports.create = (globalObject, constructorArgs, privateData) => {
  38. const wrapper = makeWrapper(globalObject);
  39. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  40. };
  41. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  42. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  43. return utils.implForWrapper(wrapper);
  44. };
  45. exports._internalSetup = (wrapper, globalObject) => {};
  46. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  47. privateData.wrapper = wrapper;
  48. exports._internalSetup(wrapper, globalObject);
  49. Object.defineProperty(wrapper, implSymbol, {
  50. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  51. configurable: true
  52. });
  53. wrapper = makeProxy(wrapper, globalObject);
  54. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  55. if (Impl.init) {
  56. Impl.init(wrapper[implSymbol]);
  57. }
  58. return wrapper;
  59. };
  60. exports.new = (globalObject, newTarget) => {
  61. let wrapper = makeWrapper(globalObject, newTarget);
  62. exports._internalSetup(wrapper, globalObject);
  63. Object.defineProperty(wrapper, implSymbol, {
  64. value: Object.create(Impl.implementation.prototype),
  65. configurable: true
  66. });
  67. wrapper = makeProxy(wrapper, globalObject);
  68. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  69. if (Impl.init) {
  70. Impl.init(wrapper[implSymbol]);
  71. }
  72. return wrapper[implSymbol];
  73. };
  74. const exposed = new Set(["Window"]);
  75. exports.install = (globalObject, globalNames) => {
  76. if (!globalNames.some(globalName => exposed.has(globalName))) {
  77. return;
  78. }
  79. const ctorRegistry = utils.initCtorRegistry(globalObject);
  80. class PluginArray {
  81. constructor() {
  82. throw new globalObject.TypeError("Illegal constructor");
  83. }
  84. refresh() {
  85. const esValue = this !== null && this !== undefined ? this : globalObject;
  86. if (!exports.is(esValue)) {
  87. throw new globalObject.TypeError("'refresh' called on an object that is not a valid instance of PluginArray.");
  88. }
  89. return esValue[implSymbol].refresh();
  90. }
  91. item(index) {
  92. const esValue = this !== null && this !== undefined ? this : globalObject;
  93. if (!exports.is(esValue)) {
  94. throw new globalObject.TypeError("'item' called on an object that is not a valid instance of PluginArray.");
  95. }
  96. if (arguments.length < 1) {
  97. throw new globalObject.TypeError(
  98. `Failed to execute 'item' on 'PluginArray': 1 argument required, but only ${arguments.length} present.`
  99. );
  100. }
  101. const args = [];
  102. {
  103. let curArg = arguments[0];
  104. curArg = conversions["unsigned long"](curArg, {
  105. context: "Failed to execute 'item' on 'PluginArray': parameter 1",
  106. globals: globalObject
  107. });
  108. args.push(curArg);
  109. }
  110. return esValue[implSymbol].item(...args);
  111. }
  112. namedItem(name) {
  113. const esValue = this !== null && this !== undefined ? this : globalObject;
  114. if (!exports.is(esValue)) {
  115. throw new globalObject.TypeError(
  116. "'namedItem' called on an object that is not a valid instance of PluginArray."
  117. );
  118. }
  119. if (arguments.length < 1) {
  120. throw new globalObject.TypeError(
  121. `Failed to execute 'namedItem' on 'PluginArray': 1 argument required, but only ${arguments.length} present.`
  122. );
  123. }
  124. const args = [];
  125. {
  126. let curArg = arguments[0];
  127. curArg = conversions["DOMString"](curArg, {
  128. context: "Failed to execute 'namedItem' on 'PluginArray': parameter 1",
  129. globals: globalObject
  130. });
  131. args.push(curArg);
  132. }
  133. return esValue[implSymbol].namedItem(...args);
  134. }
  135. get length() {
  136. const esValue = this !== null && this !== undefined ? this : globalObject;
  137. if (!exports.is(esValue)) {
  138. throw new globalObject.TypeError(
  139. "'get length' called on an object that is not a valid instance of PluginArray."
  140. );
  141. }
  142. return esValue[implSymbol]["length"];
  143. }
  144. }
  145. Object.defineProperties(PluginArray.prototype, {
  146. refresh: { enumerable: true },
  147. item: { enumerable: true },
  148. namedItem: { enumerable: true },
  149. length: { enumerable: true },
  150. [Symbol.toStringTag]: { value: "PluginArray", configurable: true },
  151. [Symbol.iterator]: { value: globalObject.Array.prototype[Symbol.iterator], configurable: true, writable: true }
  152. });
  153. ctorRegistry[interfaceName] = PluginArray;
  154. Object.defineProperty(globalObject, interfaceName, {
  155. configurable: true,
  156. writable: true,
  157. value: PluginArray
  158. });
  159. };
  160. const proxyHandlerCache = new WeakMap();
  161. class ProxyHandler {
  162. constructor(globalObject) {
  163. this._globalObject = globalObject;
  164. }
  165. get(target, P, receiver) {
  166. if (typeof P === "symbol") {
  167. return Reflect.get(target, P, receiver);
  168. }
  169. const desc = this.getOwnPropertyDescriptor(target, P);
  170. if (desc === undefined) {
  171. const parent = Object.getPrototypeOf(target);
  172. if (parent === null) {
  173. return undefined;
  174. }
  175. return Reflect.get(target, P, receiver);
  176. }
  177. if (!desc.get && !desc.set) {
  178. return desc.value;
  179. }
  180. const getter = desc.get;
  181. if (getter === undefined) {
  182. return undefined;
  183. }
  184. return Reflect.apply(getter, receiver, []);
  185. }
  186. has(target, P) {
  187. if (typeof P === "symbol") {
  188. return Reflect.has(target, P);
  189. }
  190. const desc = this.getOwnPropertyDescriptor(target, P);
  191. if (desc !== undefined) {
  192. return true;
  193. }
  194. const parent = Object.getPrototypeOf(target);
  195. if (parent !== null) {
  196. return Reflect.has(parent, P);
  197. }
  198. return false;
  199. }
  200. ownKeys(target) {
  201. const keys = new Set();
  202. for (const key of target[implSymbol][utils.supportedPropertyIndices]) {
  203. keys.add(`${key}`);
  204. }
  205. for (const key of Reflect.ownKeys(target)) {
  206. keys.add(key);
  207. }
  208. return [...keys];
  209. }
  210. getOwnPropertyDescriptor(target, P) {
  211. if (typeof P === "symbol") {
  212. return Reflect.getOwnPropertyDescriptor(target, P);
  213. }
  214. let ignoreNamedProps = false;
  215. if (utils.isArrayIndexPropName(P)) {
  216. const index = P >>> 0;
  217. const indexedValue = target[implSymbol].item(index);
  218. if (indexedValue !== null) {
  219. return {
  220. writable: false,
  221. enumerable: true,
  222. configurable: true,
  223. value: utils.tryWrapperForImpl(indexedValue)
  224. };
  225. }
  226. ignoreNamedProps = true;
  227. }
  228. return Reflect.getOwnPropertyDescriptor(target, P);
  229. }
  230. set(target, P, V, receiver) {
  231. if (typeof P === "symbol") {
  232. return Reflect.set(target, P, V, receiver);
  233. }
  234. // The `receiver` argument refers to the Proxy exotic object or an object
  235. // that inherits from it, whereas `target` refers to the Proxy target:
  236. if (target[implSymbol][utils.wrapperSymbol] === receiver) {
  237. const globalObject = this._globalObject;
  238. }
  239. let ownDesc;
  240. if (utils.isArrayIndexPropName(P)) {
  241. const index = P >>> 0;
  242. const indexedValue = target[implSymbol].item(index);
  243. if (indexedValue !== null) {
  244. ownDesc = {
  245. writable: false,
  246. enumerable: true,
  247. configurable: true,
  248. value: utils.tryWrapperForImpl(indexedValue)
  249. };
  250. }
  251. }
  252. if (ownDesc === undefined) {
  253. ownDesc = Reflect.getOwnPropertyDescriptor(target, P);
  254. }
  255. return utils.ordinarySetWithOwnDescriptor(target, P, V, receiver, ownDesc);
  256. }
  257. defineProperty(target, P, desc) {
  258. if (typeof P === "symbol") {
  259. return Reflect.defineProperty(target, P, desc);
  260. }
  261. const globalObject = this._globalObject;
  262. if (utils.isArrayIndexPropName(P)) {
  263. return false;
  264. }
  265. return Reflect.defineProperty(target, P, desc);
  266. }
  267. deleteProperty(target, P) {
  268. if (typeof P === "symbol") {
  269. return Reflect.deleteProperty(target, P);
  270. }
  271. const globalObject = this._globalObject;
  272. if (utils.isArrayIndexPropName(P)) {
  273. const index = P >>> 0;
  274. return !(target[implSymbol].item(index) !== null);
  275. }
  276. return Reflect.deleteProperty(target, P);
  277. }
  278. preventExtensions() {
  279. return false;
  280. }
  281. }
  282. const Impl = require("../../jsdom/living/navigator/PluginArray-impl.js");