DOMException.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 = "DOMException";
  7. exports.is = value => {
  8. return utils.isObject(value) && utils.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 'DOMException'.`);
  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]["DOMException"].prototype;
  26. }
  27. return Object.create(proto);
  28. }
  29. exports.create = (globalObject, constructorArgs, privateData) => {
  30. const wrapper = makeWrapper(globalObject);
  31. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  32. };
  33. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  34. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  35. return utils.implForWrapper(wrapper);
  36. };
  37. exports._internalSetup = (wrapper, globalObject) => {};
  38. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  39. privateData.wrapper = wrapper;
  40. exports._internalSetup(wrapper, globalObject);
  41. Object.defineProperty(wrapper, implSymbol, {
  42. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  43. configurable: true
  44. });
  45. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  46. if (Impl.init) {
  47. Impl.init(wrapper[implSymbol]);
  48. }
  49. return wrapper;
  50. };
  51. exports.new = (globalObject, newTarget) => {
  52. const wrapper = makeWrapper(globalObject, newTarget);
  53. exports._internalSetup(wrapper, globalObject);
  54. Object.defineProperty(wrapper, implSymbol, {
  55. value: Object.create(Impl.implementation.prototype),
  56. configurable: true
  57. });
  58. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  59. if (Impl.init) {
  60. Impl.init(wrapper[implSymbol]);
  61. }
  62. return wrapper[implSymbol];
  63. };
  64. const exposed = new Set(["Window", "Worker"]);
  65. exports.install = (globalObject, globalNames) => {
  66. if (!globalNames.some(globalName => exposed.has(globalName))) {
  67. return;
  68. }
  69. const ctorRegistry = utils.initCtorRegistry(globalObject);
  70. class DOMException {
  71. constructor() {
  72. const args = [];
  73. {
  74. let curArg = arguments[0];
  75. if (curArg !== undefined) {
  76. curArg = conversions["DOMString"](curArg, {
  77. context: "Failed to construct 'DOMException': parameter 1",
  78. globals: globalObject
  79. });
  80. } else {
  81. curArg = "";
  82. }
  83. args.push(curArg);
  84. }
  85. {
  86. let curArg = arguments[1];
  87. if (curArg !== undefined) {
  88. curArg = conversions["DOMString"](curArg, {
  89. context: "Failed to construct 'DOMException': parameter 2",
  90. globals: globalObject
  91. });
  92. } else {
  93. curArg = "Error";
  94. }
  95. args.push(curArg);
  96. }
  97. return exports.setup(Object.create(new.target.prototype), globalObject, args);
  98. }
  99. get name() {
  100. const esValue = this !== null && this !== undefined ? this : globalObject;
  101. if (!exports.is(esValue)) {
  102. throw new globalObject.TypeError(
  103. "'get name' called on an object that is not a valid instance of DOMException."
  104. );
  105. }
  106. return esValue[implSymbol]["name"];
  107. }
  108. get message() {
  109. const esValue = this !== null && this !== undefined ? this : globalObject;
  110. if (!exports.is(esValue)) {
  111. throw new globalObject.TypeError(
  112. "'get message' called on an object that is not a valid instance of DOMException."
  113. );
  114. }
  115. return esValue[implSymbol]["message"];
  116. }
  117. get code() {
  118. const esValue = this !== null && this !== undefined ? this : globalObject;
  119. if (!exports.is(esValue)) {
  120. throw new globalObject.TypeError(
  121. "'get code' called on an object that is not a valid instance of DOMException."
  122. );
  123. }
  124. return esValue[implSymbol]["code"];
  125. }
  126. }
  127. Object.defineProperties(DOMException.prototype, {
  128. name: { enumerable: true },
  129. message: { enumerable: true },
  130. code: { enumerable: true },
  131. [Symbol.toStringTag]: { value: "DOMException", configurable: true },
  132. INDEX_SIZE_ERR: { value: 1, enumerable: true },
  133. DOMSTRING_SIZE_ERR: { value: 2, enumerable: true },
  134. HIERARCHY_REQUEST_ERR: { value: 3, enumerable: true },
  135. WRONG_DOCUMENT_ERR: { value: 4, enumerable: true },
  136. INVALID_CHARACTER_ERR: { value: 5, enumerable: true },
  137. NO_DATA_ALLOWED_ERR: { value: 6, enumerable: true },
  138. NO_MODIFICATION_ALLOWED_ERR: { value: 7, enumerable: true },
  139. NOT_FOUND_ERR: { value: 8, enumerable: true },
  140. NOT_SUPPORTED_ERR: { value: 9, enumerable: true },
  141. INUSE_ATTRIBUTE_ERR: { value: 10, enumerable: true },
  142. INVALID_STATE_ERR: { value: 11, enumerable: true },
  143. SYNTAX_ERR: { value: 12, enumerable: true },
  144. INVALID_MODIFICATION_ERR: { value: 13, enumerable: true },
  145. NAMESPACE_ERR: { value: 14, enumerable: true },
  146. INVALID_ACCESS_ERR: { value: 15, enumerable: true },
  147. VALIDATION_ERR: { value: 16, enumerable: true },
  148. TYPE_MISMATCH_ERR: { value: 17, enumerable: true },
  149. SECURITY_ERR: { value: 18, enumerable: true },
  150. NETWORK_ERR: { value: 19, enumerable: true },
  151. ABORT_ERR: { value: 20, enumerable: true },
  152. URL_MISMATCH_ERR: { value: 21, enumerable: true },
  153. QUOTA_EXCEEDED_ERR: { value: 22, enumerable: true },
  154. TIMEOUT_ERR: { value: 23, enumerable: true },
  155. INVALID_NODE_TYPE_ERR: { value: 24, enumerable: true },
  156. DATA_CLONE_ERR: { value: 25, enumerable: true }
  157. });
  158. Object.defineProperties(DOMException, {
  159. INDEX_SIZE_ERR: { value: 1, enumerable: true },
  160. DOMSTRING_SIZE_ERR: { value: 2, enumerable: true },
  161. HIERARCHY_REQUEST_ERR: { value: 3, enumerable: true },
  162. WRONG_DOCUMENT_ERR: { value: 4, enumerable: true },
  163. INVALID_CHARACTER_ERR: { value: 5, enumerable: true },
  164. NO_DATA_ALLOWED_ERR: { value: 6, enumerable: true },
  165. NO_MODIFICATION_ALLOWED_ERR: { value: 7, enumerable: true },
  166. NOT_FOUND_ERR: { value: 8, enumerable: true },
  167. NOT_SUPPORTED_ERR: { value: 9, enumerable: true },
  168. INUSE_ATTRIBUTE_ERR: { value: 10, enumerable: true },
  169. INVALID_STATE_ERR: { value: 11, enumerable: true },
  170. SYNTAX_ERR: { value: 12, enumerable: true },
  171. INVALID_MODIFICATION_ERR: { value: 13, enumerable: true },
  172. NAMESPACE_ERR: { value: 14, enumerable: true },
  173. INVALID_ACCESS_ERR: { value: 15, enumerable: true },
  174. VALIDATION_ERR: { value: 16, enumerable: true },
  175. TYPE_MISMATCH_ERR: { value: 17, enumerable: true },
  176. SECURITY_ERR: { value: 18, enumerable: true },
  177. NETWORK_ERR: { value: 19, enumerable: true },
  178. ABORT_ERR: { value: 20, enumerable: true },
  179. URL_MISMATCH_ERR: { value: 21, enumerable: true },
  180. QUOTA_EXCEEDED_ERR: { value: 22, enumerable: true },
  181. TIMEOUT_ERR: { value: 23, enumerable: true },
  182. INVALID_NODE_TYPE_ERR: { value: 24, enumerable: true },
  183. DATA_CLONE_ERR: { value: 25, enumerable: true }
  184. });
  185. ctorRegistry[interfaceName] = DOMException;
  186. Object.defineProperty(globalObject, interfaceName, {
  187. configurable: true,
  188. writable: true,
  189. value: DOMException
  190. });
  191. };
  192. const Impl = require("./DOMException-impl.js");