GetIntrinsic.js 8.3 KB

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