index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. "use strict";
  2. function _(message, opts) {
  3. return `${opts && opts.context ? opts.context : "Value"} ${message}.`;
  4. }
  5. function type(V) {
  6. if (V === null) {
  7. return "Null";
  8. }
  9. switch (typeof V) {
  10. case "undefined":
  11. return "Undefined";
  12. case "boolean":
  13. return "Boolean";
  14. case "number":
  15. return "Number";
  16. case "string":
  17. return "String";
  18. case "symbol":
  19. return "Symbol";
  20. case "object":
  21. // Falls through
  22. case "function":
  23. // Falls through
  24. default:
  25. // Per ES spec, typeof returns an implemention-defined value that is not any of the existing ones for
  26. // uncallable non-standard exotic objects. Yet Type() which the Web IDL spec depends on returns Object for
  27. // such cases. So treat the default case as an object.
  28. return "Object";
  29. }
  30. }
  31. // Round x to the nearest integer, choosing the even integer if it lies halfway between two.
  32. function evenRound(x) {
  33. // There are four cases for numbers with fractional part being .5:
  34. //
  35. // case | x | floor(x) | round(x) | expected | x <> 0 | x % 1 | x & 1 | example
  36. // 1 | 2n + 0.5 | 2n | 2n + 1 | 2n | > | 0.5 | 0 | 0.5 -> 0
  37. // 2 | 2n + 1.5 | 2n + 1 | 2n + 2 | 2n + 2 | > | 0.5 | 1 | 1.5 -> 2
  38. // 3 | -2n - 0.5 | -2n - 1 | -2n | -2n | < | -0.5 | 0 | -0.5 -> 0
  39. // 4 | -2n - 1.5 | -2n - 2 | -2n - 1 | -2n - 2 | < | -0.5 | 1 | -1.5 -> -2
  40. // (where n is a non-negative integer)
  41. //
  42. // Branch here for cases 1 and 4
  43. if ((x > 0 && (x % 1) === +0.5 && (x & 1) === 0) ||
  44. (x < 0 && (x % 1) === -0.5 && (x & 1) === 1)) {
  45. return censorNegativeZero(Math.floor(x));
  46. }
  47. return censorNegativeZero(Math.round(x));
  48. }
  49. function integerPart(n) {
  50. return censorNegativeZero(Math.trunc(n));
  51. }
  52. function sign(x) {
  53. return x < 0 ? -1 : 1;
  54. }
  55. function modulo(x, y) {
  56. // https://tc39.github.io/ecma262/#eqn-modulo
  57. // Note that http://stackoverflow.com/a/4467559/3191 does NOT work for large modulos
  58. const signMightNotMatch = x % y;
  59. if (sign(y) !== sign(signMightNotMatch)) {
  60. return signMightNotMatch + y;
  61. }
  62. return signMightNotMatch;
  63. }
  64. function censorNegativeZero(x) {
  65. return x === 0 ? 0 : x;
  66. }
  67. function createIntegerConversion(bitLength, typeOpts) {
  68. const isSigned = !typeOpts.unsigned;
  69. let lowerBound;
  70. let upperBound;
  71. if (bitLength === 64) {
  72. upperBound = Math.pow(2, 53) - 1;
  73. lowerBound = !isSigned ? 0 : -Math.pow(2, 53) + 1;
  74. } else if (!isSigned) {
  75. lowerBound = 0;
  76. upperBound = Math.pow(2, bitLength) - 1;
  77. } else {
  78. lowerBound = -Math.pow(2, bitLength - 1);
  79. upperBound = Math.pow(2, bitLength - 1) - 1;
  80. }
  81. const twoToTheBitLength = Math.pow(2, bitLength);
  82. const twoToOneLessThanTheBitLength = Math.pow(2, bitLength - 1);
  83. return (V, opts) => {
  84. if (opts === undefined) {
  85. opts = {};
  86. }
  87. let x = +V;
  88. x = censorNegativeZero(x); // Spec discussion ongoing: https://github.com/heycam/webidl/issues/306
  89. if (opts.enforceRange) {
  90. if (!Number.isFinite(x)) {
  91. throw new TypeError(_("is not a finite number", opts));
  92. }
  93. x = integerPart(x);
  94. if (x < lowerBound || x > upperBound) {
  95. throw new TypeError(_(
  96. `is outside the accepted range of ${lowerBound} to ${upperBound}, inclusive`, opts));
  97. }
  98. return x;
  99. }
  100. if (!Number.isNaN(x) && opts.clamp) {
  101. x = Math.min(Math.max(x, lowerBound), upperBound);
  102. x = evenRound(x);
  103. return x;
  104. }
  105. if (!Number.isFinite(x) || x === 0) {
  106. return 0;
  107. }
  108. x = integerPart(x);
  109. // Math.pow(2, 64) is not accurately representable in JavaScript, so try to avoid these per-spec operations if
  110. // possible. Hopefully it's an optimization for the non-64-bitLength cases too.
  111. if (x >= lowerBound && x <= upperBound) {
  112. return x;
  113. }
  114. // These will not work great for bitLength of 64, but oh well. See the README for more details.
  115. x = modulo(x, twoToTheBitLength);
  116. if (isSigned && x >= twoToOneLessThanTheBitLength) {
  117. return x - twoToTheBitLength;
  118. }
  119. return x;
  120. };
  121. }
  122. exports.any = V => {
  123. return V;
  124. };
  125. exports.void = function () {
  126. return undefined;
  127. };
  128. exports.boolean = function (val) {
  129. return !!val;
  130. };
  131. exports.byte = createIntegerConversion(8, { unsigned: false });
  132. exports.octet = createIntegerConversion(8, { unsigned: true });
  133. exports.short = createIntegerConversion(16, { unsigned: false });
  134. exports["unsigned short"] = createIntegerConversion(16, { unsigned: true });
  135. exports.long = createIntegerConversion(32, { unsigned: false });
  136. exports["unsigned long"] = createIntegerConversion(32, { unsigned: true });
  137. exports["long long"] = createIntegerConversion(64, { unsigned: false });
  138. exports["unsigned long long"] = createIntegerConversion(64, { unsigned: true });
  139. exports.double = (V, opts) => {
  140. const x = +V;
  141. if (!Number.isFinite(x)) {
  142. throw new TypeError(_("is not a finite floating-point value", opts));
  143. }
  144. return x;
  145. };
  146. exports["unrestricted double"] = V => {
  147. const x = +V;
  148. return x;
  149. };
  150. exports.float = (V, opts) => {
  151. const x = +V;
  152. if (!Number.isFinite(x)) {
  153. throw new TypeError(_("is not a finite floating-point value", opts));
  154. }
  155. if (Object.is(x, -0)) {
  156. return x;
  157. }
  158. const y = Math.fround(x);
  159. if (!Number.isFinite(y)) {
  160. throw new TypeError(_("is outside the range of a single-precision floating-point value", opts));
  161. }
  162. return y;
  163. };
  164. exports["unrestricted float"] = V => {
  165. const x = +V;
  166. if (isNaN(x)) {
  167. return x;
  168. }
  169. if (Object.is(x, -0)) {
  170. return x;
  171. }
  172. return Math.fround(x);
  173. };
  174. exports.DOMString = function (V, opts) {
  175. if (opts === undefined) {
  176. opts = {};
  177. }
  178. if (opts.treatNullAsEmptyString && V === null) {
  179. return "";
  180. }
  181. if (typeof V === "symbol") {
  182. throw new TypeError(_("is a symbol, which cannot be converted to a string", opts));
  183. }
  184. return String(V);
  185. };
  186. exports.ByteString = (V, opts) => {
  187. const x = exports.DOMString(V, opts);
  188. let c;
  189. for (let i = 0; (c = x.codePointAt(i)) !== undefined; ++i) {
  190. if (c > 255) {
  191. throw new TypeError(_("is not a valid ByteString", opts));
  192. }
  193. }
  194. return x;
  195. };
  196. exports.USVString = (V, opts) => {
  197. const S = exports.DOMString(V, opts);
  198. const n = S.length;
  199. const U = [];
  200. for (let i = 0; i < n; ++i) {
  201. const c = S.charCodeAt(i);
  202. if (c < 0xD800 || c > 0xDFFF) {
  203. U.push(String.fromCodePoint(c));
  204. } else if (0xDC00 <= c && c <= 0xDFFF) {
  205. U.push(String.fromCodePoint(0xFFFD));
  206. } else if (i === n - 1) {
  207. U.push(String.fromCodePoint(0xFFFD));
  208. } else {
  209. const d = S.charCodeAt(i + 1);
  210. if (0xDC00 <= d && d <= 0xDFFF) {
  211. const a = c & 0x3FF;
  212. const b = d & 0x3FF;
  213. U.push(String.fromCodePoint((2 << 15) + ((2 << 9) * a) + b));
  214. ++i;
  215. } else {
  216. U.push(String.fromCodePoint(0xFFFD));
  217. }
  218. }
  219. }
  220. return U.join("");
  221. };
  222. exports.object = (V, opts) => {
  223. if (type(V) !== "Object") {
  224. throw new TypeError(_("is not an object", opts));
  225. }
  226. return V;
  227. };
  228. // Not exported, but used in Function and VoidFunction.
  229. // Neither Function nor VoidFunction is defined with [TreatNonObjectAsNull], so
  230. // handling for that is omitted.
  231. function convertCallbackFunction(V, opts) {
  232. if (typeof V !== "function") {
  233. throw new TypeError(_("is not a function", opts));
  234. }
  235. return V;
  236. }
  237. [
  238. Error,
  239. ArrayBuffer, // The IsDetachedBuffer abstract operation is not exposed in JS
  240. DataView, Int8Array, Int16Array, Int32Array, Uint8Array,
  241. Uint16Array, Uint32Array, Uint8ClampedArray, Float32Array, Float64Array
  242. ].forEach(func => {
  243. const name = func.name;
  244. const article = /^[AEIOU]/.test(name) ? "an" : "a";
  245. exports[name] = (V, opts) => {
  246. if (!(V instanceof func)) {
  247. throw new TypeError(_(`is not ${article} ${name} object`, opts));
  248. }
  249. return V;
  250. };
  251. });
  252. // Common definitions
  253. exports.ArrayBufferView = (V, opts) => {
  254. if (!ArrayBuffer.isView(V)) {
  255. throw new TypeError(_("is not a view on an ArrayBuffer object", opts));
  256. }
  257. return V;
  258. };
  259. exports.BufferSource = (V, opts) => {
  260. if (!(ArrayBuffer.isView(V) || V instanceof ArrayBuffer)) {
  261. throw new TypeError(_("is not an ArrayBuffer object or a view on one", opts));
  262. }
  263. return V;
  264. };
  265. exports.DOMTimeStamp = exports["unsigned long long"];
  266. exports.Function = convertCallbackFunction;
  267. exports.VoidFunction = convertCallbackFunction;