Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

GetIntrinsic.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. 'use strict';
  2. var GetIntrinsic = require('../');
  3. var test = require('tape');
  4. var forEach = require('for-each');
  5. var debug = require('object-inspect');
  6. var generatorFns = require('make-generator-function')();
  7. var asyncFns = require('make-async-function').list();
  8. var asyncGenFns = require('make-async-generator-function')();
  9. var mockProperty = require('mock-property');
  10. var callBound = require('call-bind/callBound');
  11. var v = require('es-value-fixtures');
  12. var $gOPD = require('es-abstract/helpers/getOwnPropertyDescriptor');
  13. var DefinePropertyOrThrow = require('es-abstract/2021/DefinePropertyOrThrow');
  14. var $isProto = callBound('%Object.prototype.isPrototypeOf%');
  15. test('export', function (t) {
  16. t.equal(typeof GetIntrinsic, 'function', 'it is a function');
  17. t.equal(GetIntrinsic.length, 2, 'function has length of 2');
  18. t.end();
  19. });
  20. test('throws', function (t) {
  21. t['throws'](
  22. function () { GetIntrinsic('not an intrinsic'); },
  23. SyntaxError,
  24. 'nonexistent intrinsic throws a syntax error'
  25. );
  26. t['throws'](
  27. function () { GetIntrinsic(''); },
  28. TypeError,
  29. 'empty string intrinsic throws a type error'
  30. );
  31. t['throws'](
  32. function () { GetIntrinsic('.'); },
  33. SyntaxError,
  34. '"just a dot" intrinsic throws a syntax error'
  35. );
  36. t['throws'](
  37. function () { GetIntrinsic('%String'); },
  38. SyntaxError,
  39. 'Leading % without trailing % throws a syntax error'
  40. );
  41. t['throws'](
  42. function () { GetIntrinsic('String%'); },
  43. SyntaxError,
  44. 'Trailing % without leading % throws a syntax error'
  45. );
  46. t['throws'](
  47. function () { GetIntrinsic("String['prototype]"); },
  48. SyntaxError,
  49. 'Dynamic property access is disallowed for intrinsics (unterminated string)'
  50. );
  51. t['throws'](
  52. function () { GetIntrinsic('%Proxy.prototype.undefined%'); },
  53. TypeError,
  54. "Throws when middle part doesn't exist (%Proxy.prototype.undefined%)"
  55. );
  56. t['throws'](
  57. function () { GetIntrinsic('%Array.prototype%garbage%'); },
  58. SyntaxError,
  59. 'Throws with extra percent signs'
  60. );
  61. t['throws'](
  62. function () { GetIntrinsic('%Array.prototype%push%'); },
  63. SyntaxError,
  64. 'Throws with extra percent signs, even on an existing intrinsic'
  65. );
  66. forEach(v.nonStrings, function (nonString) {
  67. t['throws'](
  68. function () { GetIntrinsic(nonString); },
  69. TypeError,
  70. debug(nonString) + ' is not a String'
  71. );
  72. });
  73. forEach(v.nonBooleans, function (nonBoolean) {
  74. t['throws'](
  75. function () { GetIntrinsic('%', nonBoolean); },
  76. TypeError,
  77. debug(nonBoolean) + ' is not a Boolean'
  78. );
  79. });
  80. forEach([
  81. 'toString',
  82. 'propertyIsEnumerable',
  83. 'hasOwnProperty'
  84. ], function (objectProtoMember) {
  85. t['throws'](
  86. function () { GetIntrinsic(objectProtoMember); },
  87. SyntaxError,
  88. debug(objectProtoMember) + ' is not an intrinsic'
  89. );
  90. });
  91. t.end();
  92. });
  93. test('base intrinsics', function (t) {
  94. t.equal(GetIntrinsic('%Object%'), Object, '%Object% yields Object');
  95. t.equal(GetIntrinsic('Object'), Object, 'Object yields Object');
  96. t.equal(GetIntrinsic('%Array%'), Array, '%Array% yields Array');
  97. t.equal(GetIntrinsic('Array'), Array, 'Array yields Array');
  98. t.end();
  99. });
  100. test('dotted paths', function (t) {
  101. t.equal(GetIntrinsic('%Object.prototype.toString%'), Object.prototype.toString, '%Object.prototype.toString% yields Object.prototype.toString');
  102. t.equal(GetIntrinsic('Object.prototype.toString'), Object.prototype.toString, 'Object.prototype.toString yields Object.prototype.toString');
  103. t.equal(GetIntrinsic('%Array.prototype.push%'), Array.prototype.push, '%Array.prototype.push% yields Array.prototype.push');
  104. t.equal(GetIntrinsic('Array.prototype.push'), Array.prototype.push, 'Array.prototype.push yields Array.prototype.push');
  105. test('underscore paths are aliases for dotted paths', { skip: !Object.isFrozen || Object.isFrozen(Object.prototype) }, function (st) {
  106. var original = GetIntrinsic('%ObjProto_toString%');
  107. forEach([
  108. '%Object.prototype.toString%',
  109. 'Object.prototype.toString',
  110. '%ObjectPrototype.toString%',
  111. 'ObjectPrototype.toString',
  112. '%ObjProto_toString%',
  113. 'ObjProto_toString'
  114. ], function (name) {
  115. DefinePropertyOrThrow(Object.prototype, 'toString', {
  116. '[[Value]]': function toString() {
  117. return original.apply(this, arguments);
  118. }
  119. });
  120. st.equal(GetIntrinsic(name), original, name + ' yields original Object.prototype.toString');
  121. });
  122. DefinePropertyOrThrow(Object.prototype, 'toString', { '[[Value]]': original });
  123. st.end();
  124. });
  125. test('dotted paths cache', { skip: !Object.isFrozen || Object.isFrozen(Object.prototype) }, function (st) {
  126. var original = GetIntrinsic('%Object.prototype.propertyIsEnumerable%');
  127. forEach([
  128. '%Object.prototype.propertyIsEnumerable%',
  129. 'Object.prototype.propertyIsEnumerable',
  130. '%ObjectPrototype.propertyIsEnumerable%',
  131. 'ObjectPrototype.propertyIsEnumerable'
  132. ], function (name) {
  133. var restore = mockProperty(Object.prototype, 'propertyIsEnumerable', {
  134. value: function propertyIsEnumerable() {
  135. return original.apply(this, arguments);
  136. }
  137. });
  138. st.equal(GetIntrinsic(name), original, name + ' yields cached Object.prototype.propertyIsEnumerable');
  139. restore();
  140. });
  141. st.end();
  142. });
  143. test('dotted path reports correct error', function (st) {
  144. st['throws'](function () {
  145. GetIntrinsic('%NonExistentIntrinsic.prototype.property%');
  146. }, /%NonExistentIntrinsic%/, 'The base intrinsic of %NonExistentIntrinsic.prototype.property% is %NonExistentIntrinsic%');
  147. st['throws'](function () {
  148. GetIntrinsic('%NonExistentIntrinsicPrototype.property%');
  149. }, /%NonExistentIntrinsicPrototype%/, 'The base intrinsic of %NonExistentIntrinsicPrototype.property% is %NonExistentIntrinsicPrototype%');
  150. st.end();
  151. });
  152. t.end();
  153. });
  154. test('accessors', { skip: !$gOPD || typeof Map !== 'function' }, function (t) {
  155. var actual = $gOPD(Map.prototype, 'size');
  156. t.ok(actual, 'Map.prototype.size has a descriptor');
  157. t.equal(typeof actual.get, 'function', 'Map.prototype.size has a getter function');
  158. t.equal(GetIntrinsic('%Map.prototype.size%'), actual.get, '%Map.prototype.size% yields the getter for it');
  159. t.equal(GetIntrinsic('Map.prototype.size'), actual.get, 'Map.prototype.size yields the getter for it');
  160. t.end();
  161. });
  162. test('generator functions', { skip: !generatorFns.length }, function (t) {
  163. var $GeneratorFunction = GetIntrinsic('%GeneratorFunction%');
  164. var $GeneratorFunctionPrototype = GetIntrinsic('%Generator%');
  165. var $GeneratorPrototype = GetIntrinsic('%GeneratorPrototype%');
  166. forEach(generatorFns, function (genFn) {
  167. var fnName = genFn.name;
  168. fnName = fnName ? "'" + fnName + "'" : 'genFn';
  169. t.ok(genFn instanceof $GeneratorFunction, fnName + ' instanceof %GeneratorFunction%');
  170. t.ok($isProto($GeneratorFunctionPrototype, genFn), '%Generator% is prototype of ' + fnName);
  171. t.ok($isProto($GeneratorPrototype, genFn.prototype), '%GeneratorPrototype% is prototype of ' + fnName + '.prototype');
  172. });
  173. t.end();
  174. });
  175. test('async functions', { skip: !asyncFns.length }, function (t) {
  176. var $AsyncFunction = GetIntrinsic('%AsyncFunction%');
  177. var $AsyncFunctionPrototype = GetIntrinsic('%AsyncFunctionPrototype%');
  178. forEach(asyncFns, function (asyncFn) {
  179. var fnName = asyncFn.name;
  180. fnName = fnName ? "'" + fnName + "'" : 'asyncFn';
  181. t.ok(asyncFn instanceof $AsyncFunction, fnName + ' instanceof %AsyncFunction%');
  182. t.ok($isProto($AsyncFunctionPrototype, asyncFn), '%AsyncFunctionPrototype% is prototype of ' + fnName);
  183. });
  184. t.end();
  185. });
  186. test('async generator functions', { skip: asyncGenFns.length === 0 }, function (t) {
  187. var $AsyncGeneratorFunction = GetIntrinsic('%AsyncGeneratorFunction%');
  188. var $AsyncGeneratorFunctionPrototype = GetIntrinsic('%AsyncGenerator%');
  189. var $AsyncGeneratorPrototype = GetIntrinsic('%AsyncGeneratorPrototype%');
  190. forEach(asyncGenFns, function (asyncGenFn) {
  191. var fnName = asyncGenFn.name;
  192. fnName = fnName ? "'" + fnName + "'" : 'asyncGenFn';
  193. t.ok(asyncGenFn instanceof $AsyncGeneratorFunction, fnName + ' instanceof %AsyncGeneratorFunction%');
  194. t.ok($isProto($AsyncGeneratorFunctionPrototype, asyncGenFn), '%AsyncGenerator% is prototype of ' + fnName);
  195. t.ok($isProto($AsyncGeneratorPrototype, asyncGenFn.prototype), '%AsyncGeneratorPrototype% is prototype of ' + fnName + '.prototype');
  196. });
  197. t.end();
  198. });
  199. test('%ThrowTypeError%', function (t) {
  200. var $ThrowTypeError = GetIntrinsic('%ThrowTypeError%');
  201. t.equal(typeof $ThrowTypeError, 'function', 'is a function');
  202. t['throws'](
  203. $ThrowTypeError,
  204. TypeError,
  205. '%ThrowTypeError% throws a TypeError'
  206. );
  207. t.end();
  208. });
  209. test('allowMissing', { skip: asyncGenFns.length > 0 }, function (t) {
  210. t['throws'](
  211. function () { GetIntrinsic('%AsyncGeneratorPrototype%'); },
  212. TypeError,
  213. 'throws when missing'
  214. );
  215. t.equal(
  216. GetIntrinsic('%AsyncGeneratorPrototype%', true),
  217. undefined,
  218. 'does not throw when allowMissing'
  219. );
  220. t.end();
  221. });