native-objects.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS2
  4. -------------------------------- (C) ---------------------------------
  5. Author: Mihai Bazon
  6. <mihai.bazon@gmail.com>
  7. http://mihai.bazon.net/blog
  8. Distributed under the BSD license:
  9. Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. * Redistributions of source code must retain the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer.
  16. * Redistributions in binary form must reproduce the above
  17. copyright notice, this list of conditions and the following
  18. disclaimer in the documentation and/or other materials
  19. provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  21. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  29. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. SUCH DAMAGE.
  32. ***********************************************************************/
  33. import { makePredicate } from "../utils/index.js";
  34. // Lists of native methods, useful for `unsafe` option which assumes they exist.
  35. // Note: Lots of methods and functions are missing here, in case they aren't pure
  36. // or not available in all JS environments.
  37. function make_nested_lookup(obj) {
  38. const out = new Map();
  39. for (var key of Object.keys(obj)) {
  40. out.set(key, makePredicate(obj[key]));
  41. }
  42. const does_have = (global_name, fname) => {
  43. const inner_map = out.get(global_name);
  44. return inner_map != null && inner_map.has(fname);
  45. };
  46. return does_have;
  47. }
  48. // Objects which are safe to access without throwing or causing a side effect.
  49. // Usually we'd check the `unsafe` option first but these are way too common for that
  50. export const pure_prop_access_globals = new Set([
  51. "Number",
  52. "String",
  53. "Array",
  54. "Object",
  55. "Function",
  56. "Promise",
  57. ]);
  58. const object_methods = [
  59. "constructor",
  60. "toString",
  61. "valueOf",
  62. ];
  63. export const is_pure_native_method = make_nested_lookup({
  64. Array: [
  65. "at",
  66. "flat",
  67. "includes",
  68. "indexOf",
  69. "join",
  70. "lastIndexOf",
  71. "slice",
  72. ...object_methods,
  73. ],
  74. Boolean: object_methods,
  75. Function: object_methods,
  76. Number: [
  77. "toExponential",
  78. "toFixed",
  79. "toPrecision",
  80. ...object_methods,
  81. ],
  82. Object: object_methods,
  83. RegExp: [
  84. "test",
  85. ...object_methods,
  86. ],
  87. String: [
  88. "at",
  89. "charAt",
  90. "charCodeAt",
  91. "charPointAt",
  92. "concat",
  93. "endsWith",
  94. "fromCharCode",
  95. "fromCodePoint",
  96. "includes",
  97. "indexOf",
  98. "italics",
  99. "lastIndexOf",
  100. "localeCompare",
  101. "match",
  102. "matchAll",
  103. "normalize",
  104. "padStart",
  105. "padEnd",
  106. "repeat",
  107. "replace",
  108. "replaceAll",
  109. "search",
  110. "slice",
  111. "split",
  112. "startsWith",
  113. "substr",
  114. "substring",
  115. "repeat",
  116. "toLocaleLowerCase",
  117. "toLocaleUpperCase",
  118. "toLowerCase",
  119. "toUpperCase",
  120. "trim",
  121. "trimEnd",
  122. "trimStart",
  123. ...object_methods,
  124. ],
  125. });
  126. export const is_pure_native_fn = make_nested_lookup({
  127. Array: [
  128. "isArray",
  129. ],
  130. Math: [
  131. "abs",
  132. "acos",
  133. "asin",
  134. "atan",
  135. "ceil",
  136. "cos",
  137. "exp",
  138. "floor",
  139. "log",
  140. "round",
  141. "sin",
  142. "sqrt",
  143. "tan",
  144. "atan2",
  145. "pow",
  146. "max",
  147. "min",
  148. ],
  149. Number: [
  150. "isFinite",
  151. "isNaN",
  152. ],
  153. Object: [
  154. "create",
  155. "getOwnPropertyDescriptor",
  156. "getOwnPropertyNames",
  157. "getPrototypeOf",
  158. "isExtensible",
  159. "isFrozen",
  160. "isSealed",
  161. "hasOwn",
  162. "keys",
  163. ],
  164. String: [
  165. "fromCharCode",
  166. ],
  167. });
  168. // Known numeric values which come with JS environments
  169. export const is_pure_native_value = make_nested_lookup({
  170. Math: [
  171. "E",
  172. "LN10",
  173. "LN2",
  174. "LOG2E",
  175. "LOG10E",
  176. "PI",
  177. "SQRT1_2",
  178. "SQRT2",
  179. ],
  180. Number: [
  181. "MAX_VALUE",
  182. "MIN_VALUE",
  183. "NaN",
  184. "NEGATIVE_INFINITY",
  185. "POSITIVE_INFINITY",
  186. ],
  187. });