Plugin.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 = "Plugin";
  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 'Plugin'.`);
  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]["Plugin"].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 Plugin {
  81. constructor() {
  82. throw new globalObject.TypeError("Illegal constructor");
  83. }
  84. item(index) {
  85. const esValue = this !== null && this !== undefined ? this : globalObject;
  86. if (!exports.is(esValue)) {
  87. throw new globalObject.TypeError("'item' called on an object that is not a valid instance of Plugin.");
  88. }
  89. if (arguments.length < 1) {
  90. throw new globalObject.TypeError(
  91. `Failed to execute 'item' on 'Plugin': 1 argument required, but only ${arguments.length} present.`
  92. );
  93. }
  94. const args = [];
  95. {
  96. let curArg = arguments[0];
  97. curArg = conversions["unsigned long"](curArg, {
  98. context: "Failed to execute 'item' on 'Plugin': parameter 1",
  99. globals: globalObject
  100. });
  101. args.push(curArg);
  102. }
  103. return esValue[implSymbol].item(...args);
  104. }
  105. namedItem(name) {
  106. const esValue = this !== null && this !== undefined ? this : globalObject;
  107. if (!exports.is(esValue)) {
  108. throw new globalObject.TypeError("'namedItem' called on an object that is not a valid instance of Plugin.");
  109. }
  110. if (arguments.length < 1) {
  111. throw new globalObject.TypeError(
  112. `Failed to execute 'namedItem' on 'Plugin': 1 argument required, but only ${arguments.length} present.`
  113. );
  114. }
  115. const args = [];
  116. {
  117. let curArg = arguments[0];
  118. curArg = conversions["DOMString"](curArg, {
  119. context: "Failed to execute 'namedItem' on 'Plugin': parameter 1",
  120. globals: globalObject
  121. });
  122. args.push(curArg);
  123. }
  124. return esValue[implSymbol].namedItem(...args);
  125. }
  126. get name() {
  127. const esValue = this !== null && this !== undefined ? this : globalObject;
  128. if (!exports.is(esValue)) {
  129. throw new globalObject.TypeError("'get name' called on an object that is not a valid instance of Plugin.");
  130. }
  131. return esValue[implSymbol]["name"];
  132. }
  133. get description() {
  134. const esValue = this !== null && this !== undefined ? this : globalObject;
  135. if (!exports.is(esValue)) {
  136. throw new globalObject.TypeError(
  137. "'get description' called on an object that is not a valid instance of Plugin."
  138. );
  139. }
  140. return esValue[implSymbol]["description"];
  141. }
  142. get filename() {
  143. const esValue = this !== null && this !== undefined ? this : globalObject;
  144. if (!exports.is(esValue)) {
  145. throw new globalObject.TypeError("'get filename' called on an object that is not a valid instance of Plugin.");
  146. }
  147. return esValue[implSymbol]["filename"];
  148. }
  149. get length() {
  150. const esValue = this !== null && this !== undefined ? this : globalObject;
  151. if (!exports.is(esValue)) {
  152. throw new globalObject.TypeError("'get length' called on an object that is not a valid instance of Plugin.");
  153. }
  154. return esValue[implSymbol]["length"];
  155. }
  156. }
  157. Object.defineProperties(Plugin.prototype, {
  158. item: { enumerable: true },
  159. namedItem: { enumerable: true },
  160. name: { enumerable: true },
  161. description: { enumerable: true },
  162. filename: { enumerable: true },
  163. length: { enumerable: true },
  164. [Symbol.toStringTag]: { value: "Plugin", configurable: true },
  165. [Symbol.iterator]: { value: globalObject.Array.prototype[Symbol.iterator], configurable: true, writable: true }
  166. });
  167. ctorRegistry[interfaceName] = Plugin;
  168. Object.defineProperty(globalObject, interfaceName, {
  169. configurable: true,
  170. writable: true,
  171. value: Plugin
  172. });
  173. };
  174. const proxyHandlerCache = new WeakMap();
  175. class ProxyHandler {
  176. constructor(globalObject) {
  177. this._globalObject = globalObject;
  178. }
  179. get(target, P, receiver) {
  180. if (typeof P === "symbol") {
  181. return Reflect.get(target, P, receiver);
  182. }
  183. const desc = this.getOwnPropertyDescriptor(target, P);
  184. if (desc === undefined) {
  185. const parent = Object.getPrototypeOf(target);
  186. if (parent === null) {
  187. return undefined;
  188. }
  189. return Reflect.get(target, P, receiver);
  190. }
  191. if (!desc.get && !desc.set) {
  192. return desc.value;
  193. }
  194. const getter = desc.get;
  195. if (getter === undefined) {
  196. return undefined;
  197. }
  198. return Reflect.apply(getter, receiver, []);
  199. }
  200. has(target, P) {
  201. if (typeof P === "symbol") {
  202. return Reflect.has(target, P);
  203. }
  204. const desc = this.getOwnPropertyDescriptor(target, P);
  205. if (desc !== undefined) {
  206. return true;
  207. }
  208. const parent = Object.getPrototypeOf(target);
  209. if (parent !== null) {
  210. return Reflect.has(parent, P);
  211. }
  212. return false;
  213. }
  214. ownKeys(target) {
  215. const keys = new Set();
  216. for (const key of target[implSymbol][utils.supportedPropertyIndices]) {
  217. keys.add(`${key}`);
  218. }
  219. for (const key of Reflect.ownKeys(target)) {
  220. keys.add(key);
  221. }
  222. return [...keys];
  223. }
  224. getOwnPropertyDescriptor(target, P) {
  225. if (typeof P === "symbol") {
  226. return Reflect.getOwnPropertyDescriptor(target, P);
  227. }
  228. let ignoreNamedProps = false;
  229. if (utils.isArrayIndexPropName(P)) {
  230. const index = P >>> 0;
  231. if (target[implSymbol][utils.supportsPropertyIndex](index)) {
  232. const indexedValue = target[implSymbol].item(index);
  233. return {
  234. writable: false,
  235. enumerable: true,
  236. configurable: true,
  237. value: utils.tryWrapperForImpl(indexedValue)
  238. };
  239. }
  240. ignoreNamedProps = true;
  241. }
  242. return Reflect.getOwnPropertyDescriptor(target, P);
  243. }
  244. set(target, P, V, receiver) {
  245. if (typeof P === "symbol") {
  246. return Reflect.set(target, P, V, receiver);
  247. }
  248. // The `receiver` argument refers to the Proxy exotic object or an object
  249. // that inherits from it, whereas `target` refers to the Proxy target:
  250. if (target[implSymbol][utils.wrapperSymbol] === receiver) {
  251. const globalObject = this._globalObject;
  252. }
  253. let ownDesc;
  254. if (utils.isArrayIndexPropName(P)) {
  255. const index = P >>> 0;
  256. if (target[implSymbol][utils.supportsPropertyIndex](index)) {
  257. const indexedValue = target[implSymbol].item(index);
  258. ownDesc = {
  259. writable: false,
  260. enumerable: true,
  261. configurable: true,
  262. value: utils.tryWrapperForImpl(indexedValue)
  263. };
  264. }
  265. }
  266. if (ownDesc === undefined) {
  267. ownDesc = Reflect.getOwnPropertyDescriptor(target, P);
  268. }
  269. return utils.ordinarySetWithOwnDescriptor(target, P, V, receiver, ownDesc);
  270. }
  271. defineProperty(target, P, desc) {
  272. if (typeof P === "symbol") {
  273. return Reflect.defineProperty(target, P, desc);
  274. }
  275. const globalObject = this._globalObject;
  276. if (utils.isArrayIndexPropName(P)) {
  277. return false;
  278. }
  279. return Reflect.defineProperty(target, P, desc);
  280. }
  281. deleteProperty(target, P) {
  282. if (typeof P === "symbol") {
  283. return Reflect.deleteProperty(target, P);
  284. }
  285. const globalObject = this._globalObject;
  286. if (utils.isArrayIndexPropName(P)) {
  287. const index = P >>> 0;
  288. return !target[implSymbol][utils.supportsPropertyIndex](index);
  289. }
  290. return Reflect.deleteProperty(target, P);
  291. }
  292. preventExtensions() {
  293. return false;
  294. }
  295. }
  296. const Impl = require("../../jsdom/living/navigator/Plugin-impl.js");