native-objects.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. "indexOf",
  66. "join",
  67. "lastIndexOf",
  68. "slice",
  69. ...object_methods,
  70. ],
  71. Boolean: object_methods,
  72. Function: object_methods,
  73. Number: [
  74. "toExponential",
  75. "toFixed",
  76. "toPrecision",
  77. ...object_methods,
  78. ],
  79. Object: object_methods,
  80. RegExp: [
  81. "test",
  82. ...object_methods,
  83. ],
  84. String: [
  85. "charAt",
  86. "charCodeAt",
  87. "concat",
  88. "indexOf",
  89. "italics",
  90. "lastIndexOf",
  91. "match",
  92. "replace",
  93. "search",
  94. "slice",
  95. "split",
  96. "substr",
  97. "substring",
  98. "toLowerCase",
  99. "toUpperCase",
  100. "trim",
  101. ...object_methods,
  102. ],
  103. });
  104. export const is_pure_native_fn = make_nested_lookup({
  105. Array: [
  106. "isArray",
  107. ],
  108. Math: [
  109. "abs",
  110. "acos",
  111. "asin",
  112. "atan",
  113. "ceil",
  114. "cos",
  115. "exp",
  116. "floor",
  117. "log",
  118. "round",
  119. "sin",
  120. "sqrt",
  121. "tan",
  122. "atan2",
  123. "pow",
  124. "max",
  125. "min",
  126. ],
  127. Number: [
  128. "isFinite",
  129. "isNaN",
  130. ],
  131. Object: [
  132. "create",
  133. "getOwnPropertyDescriptor",
  134. "getOwnPropertyNames",
  135. "getPrototypeOf",
  136. "isExtensible",
  137. "isFrozen",
  138. "isSealed",
  139. "hasOwn",
  140. "keys",
  141. ],
  142. String: [
  143. "fromCharCode",
  144. ],
  145. });
  146. // Known numeric values which come with JS environments
  147. export const is_pure_native_value = make_nested_lookup({
  148. Math: [
  149. "E",
  150. "LN10",
  151. "LN2",
  152. "LOG2E",
  153. "LOG10E",
  154. "PI",
  155. "SQRT1_2",
  156. "SQRT2",
  157. ],
  158. Number: [
  159. "MAX_VALUE",
  160. "MIN_VALUE",
  161. "NaN",
  162. "NEGATIVE_INFINITY",
  163. "POSITIVE_INFINITY",
  164. ],
  165. });