SpecialSymbolCapture.js 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  6. var lambdaForGlobalThis = () => this;
  7. var tests = [
  8. {
  9. name: "Simple function defined at global scope with a this binding and global has no this reference",
  10. body: function () {
  11. WScript.LoadScript(`
  12. function bar() {
  13. this.o = 'bar';
  14. }
  15. var _this = { o: 'global' };
  16. bar.call(_this);
  17. assert.areEqual('bar', _this.o, "Function bar mutated _this, which was loaded as the 'this' binding inside bar");
  18. `);
  19. }
  20. },
  21. {
  22. name: "Simple binding of 'this' in global function",
  23. body: function () {
  24. WScript.LoadScript(`
  25. assert.areEqual(undefined, this.o, "Initial value of 'this.o' is undefined");
  26. this.o = 'global';
  27. assert.areEqual('global', this.o, "Writing property to 'this' binding in global function");
  28. `);
  29. }
  30. },
  31. {
  32. name: "Lambda capturing global 'this' binding. Note: This isn't really a capture.",
  33. body: function () {
  34. WScript.LoadScript(`
  35. var lambdaCapturesGlobalThis = str => this.o = str
  36. this.o = 'global';
  37. lambdaCapturesGlobalThis('called at global');
  38. assert.areEqual('called at global', this.o, "Lambda modifying global this binding called from global function");
  39. this.o = 'global';
  40. function foo() {
  41. lambdaCapturesGlobalThis('called from nested function');
  42. }
  43. foo();
  44. assert.areEqual('called from nested function', this.o, "Lambda modifying global this binding called from nested function");
  45. this.o = 'global';
  46. var globalLambda = () => lambdaCapturesGlobalThis('called from global lambda')
  47. globalLambda();
  48. assert.areEqual('called from global lambda', this.o, "Lambda modifying global this binding called from global lambda");
  49. this.o = 'global';
  50. eval("lambdaCapturesGlobalThis('called from global eval');");
  51. assert.areEqual('called from global eval', this.o, "Lambda modifying global this binding called from global eval");
  52. `);
  53. }
  54. },
  55. {
  56. name: "Function with eval and lambda both modifying 'this'",
  57. body: function () {
  58. WScript.LoadScript(`
  59. function bar() {
  60. eval('this.o = "eval bar"');
  61. (() => this.o = 'lambda bar')()
  62. }
  63. var _this = { o: 'global' };
  64. bar.call(_this);
  65. assert.areEqual('lambda bar', _this.o, "Lambda was the last thing to modify 'this' binding inside bar");
  66. `);
  67. }
  68. },
  69. {
  70. name: "Capture of nested 'this' object",
  71. body: function () {
  72. function foo() {
  73. var count = 0;
  74. this.o = 'foo';
  75. eval("count++; assert.areEqual('foo', this.o, 'Eval capture of nested-function this binding')");
  76. eval(`count++; eval("assert.areEqual('foo', this.o, 'Nested eval capture of nested-function this binding')")`);
  77. (() => {count++; assert.areEqual('foo', this.o, 'Lambda capture of nested function this binding')})();
  78. (() => () => {count++; assert.areEqual('foo', this.o, 'Nested lambda capture of nested function this binding')})()();
  79. (() => eval(`count++; assert.areEqual('foo', this.o, 'Lambda with eval capture of nested function this binding')`))();
  80. (() => eval(`count++; eval("assert.areEqual('foo', this.o, 'Lambda with nested eval capture of nested function this binding')")`))();
  81. eval(`(() => {count++; assert.areEqual('foo', this.o, 'Eval with lambda capture of nested-function this binding')})()`);
  82. eval("eval(`(() => {count++; assert.areEqual('foo', this.o, 'Nested eval with lambda capture of nested-function this binding')})()`)");
  83. eval("(() => eval(`(() => {count++; assert.areEqual('foo', this.o, 'Nested eval and nested lambda capture of nested-function this binding')})()`))()");
  84. eval("count++; assert.areEqual('foo', this.o, 'Eval with nested lambda in which the eval also references this binding'); (() => assert.areEqual('foo', this.o, 'Eval with lambda capture of nested-function this binding'))();");
  85. assert.areEqual(10, count, 'Called correct number of test functions');
  86. return true;
  87. }
  88. assert.isTrue(foo.call({o:'test'}), "Test function completes");
  89. }
  90. },
  91. {
  92. name: "Eval modifying 'this' inside a nested function which doesn't itself reference 'this' binding",
  93. body: function () {
  94. WScript.LoadScript(`
  95. function foo() {
  96. this.o = 'foo';
  97. function bar() {
  98. eval('this.o = "eval bar"');
  99. }
  100. bar();
  101. }
  102. this.o = 'global';
  103. foo();
  104. assert.areEqual('eval bar', this.o, "Eval was the last thing to modify 'this' binding inside bar");
  105. `);
  106. }
  107. },
  108. {
  109. name: "Various usage patterns for 'this'",
  110. body: function () {
  111. WScript.LoadScript(`
  112. eval('this.o = "global eval"');
  113. assert.areEqual('global eval', this.o, "Global eval modifying this");
  114. (() => this.o = 'global lambda')();
  115. assert.areEqual('global lambda', this.o, "Global lambda modifying this");
  116. function bar () {
  117. this.o = 'bar';
  118. return this;
  119. }
  120. var _this = bar();
  121. assert.areEqual('bar', this.o, "Nested function modifies it's own this binding");
  122. assert.areEqual(_this, this, "bar returns local this which is the same object as global this");
  123. function baz() {
  124. this.o = 'baz';
  125. return this;
  126. }
  127. _this = baz.call({});
  128. assert.areEqual('bar', this.o, "Function baz doesn't modify global this object");
  129. assert.isFalse(_this === this, "baz returns local this which is not the same object as global this");
  130. function bot(_this) {
  131. _this.o = 'bot';
  132. return _this;
  133. }
  134. _this = bot(this);
  135. assert.areEqual('bot', this.o, "Nested function modifies global this object via a different binding");
  136. assert.areEqual(_this, this, "bot returns local this which is the same object as global this");
  137. function foo() {
  138. assert.areEqual('global', this.o, "The global this binding is passed as the this argument to foo");
  139. this.o = 'foo';
  140. assert.areEqual('foo', this.o, "Simple property assignment");
  141. function bar() {
  142. this.o = 'bar';
  143. return this;
  144. }
  145. _this = bar.call(this);
  146. assert.areEqual('bar', this.o, "Nested function modifies outside this via it's own this binding");
  147. assert.areEqual(_this, this, "bar returns local this which is the same object as foo's this");
  148. function baz() {
  149. this.o = 'baz';
  150. return this;
  151. }
  152. _this = baz.call({});
  153. assert.areEqual('bar', this.o, "Nested function modifies it's own this binding");
  154. assert.isFalse(_this === this, "baz returns local this which is not the same object as foo's this");
  155. function bot(_this) {
  156. _this.o = 'bot';
  157. return _this;
  158. }
  159. _this = bot(this);
  160. assert.areEqual('bot', this.o, "Nested function modifies outside this via a different binding");
  161. assert.areEqual(_this, this, "bot returns local this which is the same object as foo's this");
  162. return this;
  163. }
  164. this.o = 'global';
  165. _this = foo.call(this);
  166. assert.areEqual(_this, this, "foo returns local this which is the same object as global this");
  167. `);
  168. }
  169. },
  170. {
  171. name: "Strict mode outside 'this' object is not captured by nested functions",
  172. body: function () {
  173. WScript.LoadScript(`
  174. "use strict";
  175. this.o = 'global';
  176. function bar() {
  177. assert.areEqual(undefined, this, "Global this object is not loaded by default in nested functions in strict mode");
  178. }
  179. bar();
  180. function baz() {
  181. this.o = 'baz';
  182. return this;
  183. }
  184. var _this = baz.call({});
  185. assert.isFalse(_this === this, "baz returns this which is not bound to global this");
  186. assert.areEqual('baz', _this.o, "baz returned this which was passed to it via call()");
  187. function foo() {
  188. assert.isTrue(this !== undefined, "foo must be called with an object or something");
  189. function bar() {
  190. assert.areEqual(undefined, this, "Outside this object is not loaded by default in nested functions in strict mode");
  191. }
  192. bar();
  193. }
  194. foo.call({});
  195. `);
  196. }
  197. },
  198. {
  199. name: "Global-scope `new.target` keyword is early syntax error",
  200. body: function () {
  201. var syntaxError;
  202. assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; new.target;`), syntaxError, "'new.target' is invalid syntax at global scope", "Invalid use of the 'new.target' keyword");
  203. assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; () => new.target`), syntaxError, "'new.target' is invalid in arrow at global scope", "Invalid use of the 'new.target' keyword");
  204. assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; () => () => new.target`), syntaxError, "'new.target' is invalid in nested arrow at global scope", "Invalid use of the 'new.target' keyword");
  205. assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; eval('new.target');`), syntaxError, "'new.target' is invalid in eval at global scope", "Invalid use of the 'new.target' keyword");
  206. assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; eval("eval('new.target');")`), syntaxError, "'new.target' is invalid in nested eval at global scope", "Invalid use of the 'new.target' keyword");
  207. assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; eval('() => new.target')`), syntaxError, "'new.target' is invalid in arrow nested in eval at global scope", "Invalid use of the 'new.target' keyword");
  208. assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; (() => eval('new.target'))()`), syntaxError, "'new.target' is invalid in eval nested in arrow at global scope", "Invalid use of the 'new.target' keyword");
  209. }
  210. },
  211. {
  212. name: "Simple captures of new.target within ordinary functions",
  213. body: function () {
  214. function foo() {
  215. return new.target;
  216. }
  217. assert.areEqual(foo, new foo(), "Simple function returning new.target binding");
  218. assert.areEqual(undefined, foo(), "Simple function binds new.target to undefined without new keyword");
  219. function foo2() {
  220. var a = () => new.target;
  221. return a();
  222. }
  223. assert.areEqual(foo2, new foo2(), "Function returning new.target captured by nested arrow");
  224. assert.areEqual(undefined, foo2(), "Function with arrow capturing new.target binds new.target to undefined without new keyword");
  225. function foo3() {
  226. return eval('new.target');
  227. }
  228. assert.areEqual(foo3, new foo3(), "Function returning new.target captured by nested eval");
  229. assert.areEqual(undefined, foo3(), "Function with eval capturing new.target binds new.target to undefined without new keyword");
  230. function foo4() {
  231. var a = () => () => new.target;
  232. return a()();
  233. }
  234. assert.areEqual(foo4, new foo4(), "Function returning new.target captured by nested arrows");
  235. assert.areEqual(undefined, foo4(), "Function with nested arrows capturing new.target binds new.target to undefined without new keyword");
  236. function foo5() {
  237. return eval("eval('new.target');");
  238. }
  239. assert.areEqual(foo5, new foo5(), "Function returning new.target captured by nested evals");
  240. assert.areEqual(undefined, foo5(), "Function with nested evals capturing new.target binds new.target to undefined without new keyword");
  241. function foo6() {
  242. var a = () => eval('new.target');
  243. return a();
  244. }
  245. assert.areEqual(foo6, new foo6(), "Function returning new.target captured by arrow captured by eval");
  246. assert.areEqual(undefined, foo6(), "Function with nested evals capturing new.target binds new.target to undefined without new keyword");
  247. }
  248. },
  249. {
  250. name: "Base class empty constructor symbol binding",
  251. body: function() {
  252. class BaseDefault { }
  253. assert.areEqual(BaseDefault, new BaseDefault().constructor, "Base class with default constructor");
  254. eval(`
  255. class BaseEvalDefault { }
  256. assert.areEqual(BaseEvalDefault, new BaseEvalDefault().constructor, "Base class defined in eval with default constructor");
  257. `);
  258. class BaseEmpty {
  259. constructor() { }
  260. }
  261. assert.areEqual(BaseEmpty, new BaseEmpty().constructor, "Base class with empty constructor");
  262. eval(`
  263. class BaseEvalEmpty {
  264. constructor() { }
  265. }
  266. assert.areEqual(BaseEvalEmpty, new BaseEvalEmpty().constructor, "Base class defined in eval with empty constructor");
  267. `);
  268. }
  269. },
  270. {
  271. name: "Base class 'this' binding",
  272. body: function() {
  273. class BaseReturnThis {
  274. constructor() {
  275. return this;
  276. }
  277. }
  278. assert.areEqual(BaseReturnThis, new BaseReturnThis().constructor, "Base class with constructor that returns this");
  279. class BaseReturnThisEval {
  280. constructor() {
  281. return eval('this');
  282. }
  283. }
  284. assert.areEqual(BaseReturnThisEval, new BaseReturnThisEval().constructor, "Base class with constructor that returns this captured by eval");
  285. class BaseReturnThisArrow {
  286. constructor() {
  287. return (() => this)();
  288. }
  289. }
  290. assert.areEqual(BaseReturnThisArrow, new BaseReturnThisArrow().constructor, "Base class with constructor that returns this captured by arrow");
  291. eval(`
  292. class BaseEvalReturnThis {
  293. constructor() {
  294. return this;
  295. }
  296. }
  297. assert.areEqual(BaseEvalReturnThis, new BaseEvalReturnThis().constructor, "Base class defined in eval with constructor that returns this");
  298. `);
  299. eval(`
  300. class BaseEvalReturnThisEval {
  301. constructor() {
  302. return eval('this');
  303. }
  304. }
  305. assert.areEqual(BaseEvalReturnThisEval, new BaseEvalReturnThisEval().constructor, "Base class defined in eval with constructor that returns this captured via eval");
  306. `);
  307. eval(`
  308. class BaseEvalReturnThisLambda {
  309. constructor() {
  310. return (() => this)();
  311. }
  312. }
  313. assert.areEqual(BaseEvalReturnThisLambda, new BaseEvalReturnThisLambda().constructor, "Base class defined in eval with constructor that returns this captured via arrow");
  314. `);
  315. eval(`
  316. class BaseEvalReturnThisEvalLambda {
  317. constructor() {
  318. return eval('(() => this)()');
  319. }
  320. }
  321. assert.areEqual(BaseEvalReturnThisEvalLambda, new BaseEvalReturnThisEvalLambda().constructor, "Base class defined in eval with constructor that returns this captured via arrow inside eval");
  322. `);
  323. eval(`
  324. class BaseEvalReturnThisLambdaEval {
  325. constructor() {
  326. return (() => eval('this'))();
  327. }
  328. }
  329. assert.areEqual(BaseEvalReturnThisLambdaEval, new BaseEvalReturnThisLambdaEval().constructor, "Base class defined in eval with constructor that returns this captured via eval inside arrow");
  330. `);
  331. var called = false;
  332. class BaseVerifyThis {
  333. constructor() {
  334. assert.areEqual(BaseVerifyThis, this.constructor, "Base class this binding is an instance of the class");
  335. called = true;
  336. }
  337. }
  338. assert.areEqual(BaseVerifyThis, new BaseVerifyThis().constructor, "Base class constructor uses this object");
  339. assert.isTrue(called, "Constructor was actually called");
  340. called = false;
  341. class BaseVerifyThisEval {
  342. constructor() {
  343. eval(`
  344. assert.areEqual(BaseVerifyThisEval, this.constructor, "Base class this binding is an instance of the class");
  345. called = true;
  346. `);
  347. }
  348. }
  349. assert.areEqual(BaseVerifyThisEval, new BaseVerifyThisEval().constructor, "Base class constructor uses this object inside eval");
  350. assert.isTrue(called, "Constructor was actually called");
  351. called = false;
  352. class BaseVerifyThisArrow {
  353. constructor() {
  354. assert.areEqual(BaseVerifyThisArrow, (() => this)().constructor, "Base class this binding is an instance of the class");
  355. called = true;
  356. }
  357. }
  358. assert.areEqual(BaseVerifyThisArrow, new BaseVerifyThisArrow().constructor, "Base class constructor uses this object inside arrow");
  359. assert.isTrue(called, "Constructor was actually called");
  360. called = false;
  361. class BaseVerifyThisEvalArrow {
  362. constructor() {
  363. assert.areEqual(BaseVerifyThisEvalArrow, eval('(() => this)()').constructor, "Base class this binding is an instance of the class");
  364. called = true;
  365. }
  366. }
  367. assert.areEqual(BaseVerifyThisEvalArrow, new BaseVerifyThisEvalArrow().constructor, "Base class constructor uses this object inside arrow inside eval");
  368. assert.isTrue(called, "Constructor was actually called");
  369. called = false;
  370. class BaseVerifyThisArrowEval {
  371. constructor() {
  372. assert.areEqual(BaseVerifyThisArrowEval, (() => eval('this'))().constructor, "Base class this binding is an instance of the class");
  373. called = true;
  374. }
  375. }
  376. assert.areEqual(BaseVerifyThisArrowEval, new BaseVerifyThisArrowEval().constructor, "Base class constructor uses this object inside eval inside arrow");
  377. assert.isTrue(called, "Constructor was actually called");
  378. }
  379. },
  380. {
  381. name: "Base class new.target binding",
  382. body: function() {
  383. var called = false;
  384. class BaseVerifyNewTarget {
  385. constructor() {
  386. assert.areEqual(BaseVerifyNewTarget, new.target, "Base class called as new expression gets new.target");
  387. called = true;
  388. }
  389. }
  390. assert.areEqual(BaseVerifyNewTarget, new BaseVerifyNewTarget().constructor, "Base class constructor uses new.target");
  391. assert.isTrue(called, "Constructor was actually called");
  392. called = false;
  393. class BaseVerifyNewTargetEval {
  394. constructor() {
  395. eval(`
  396. assert.areEqual(BaseVerifyNewTargetEval, new.target, "Base class called as new expression gets new.target");
  397. called = true;
  398. `);
  399. }
  400. }
  401. assert.areEqual(BaseVerifyNewTargetEval, new BaseVerifyNewTargetEval().constructor, "Base class constructor uses new.target inside eval");
  402. assert.isTrue(called, "Constructor was actually called");
  403. called = false;
  404. class BaseVerifyNewTargetLambda {
  405. constructor() {
  406. (() => {
  407. assert.areEqual(BaseVerifyNewTargetLambda, new.target, "Base class called as new expression gets new.target");
  408. called = true;
  409. })();
  410. }
  411. }
  412. assert.areEqual(BaseVerifyNewTargetLambda, new BaseVerifyNewTargetLambda().constructor, "Base class constructor uses new.target inside lambda");
  413. assert.isTrue(called, "Constructor was actually called");
  414. called = false;
  415. class BaseVerifyNewTargetEvalLambda {
  416. constructor() {
  417. eval(`
  418. (() => {
  419. assert.areEqual(BaseVerifyNewTargetEvalLambda, new.target, "Base class called as new expression gets new.target");
  420. called = true;
  421. })();
  422. `);
  423. }
  424. }
  425. assert.areEqual(BaseVerifyNewTargetEvalLambda, new BaseVerifyNewTargetEvalLambda().constructor, "Base class constructor uses new.target inside lambda inside eval");
  426. assert.isTrue(called, "Constructor was actually called");
  427. called = false;
  428. class BaseVerifyNewTargetLambdaEval {
  429. constructor() {
  430. (() => {
  431. eval(`
  432. assert.areEqual(BaseVerifyNewTargetLambdaEval, new.target, "Base class called as new expression gets new.target");
  433. called = true;
  434. `);
  435. })();
  436. }
  437. }
  438. assert.areEqual(BaseVerifyNewTargetLambdaEval, new BaseVerifyNewTargetLambdaEval().constructor, "Base class constructor uses new.target inside eval inside lambda");
  439. assert.isTrue(called, "Constructor was actually called");
  440. called = false;
  441. eval(`
  442. class BaseEvalVerifyNewTarget {
  443. constructor() {
  444. assert.areEqual(BaseEvalVerifyNewTarget, new.target, "Base class called as new expression gets new.target");
  445. called = true;
  446. }
  447. }
  448. assert.areEqual(BaseEvalVerifyNewTarget, new BaseEvalVerifyNewTarget().constructor, "Base class defined in eval constructor uses new.target");
  449. assert.isTrue(called, "Constructor was actually called");
  450. `);
  451. }
  452. },
  453. {
  454. name: "Base class super call is syntax error",
  455. body: function() {
  456. var syntaxError;
  457. assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; class Base { constructor() { super(); } }`), syntaxError, "'super' call is invalid syntax in a base class", "Invalid use of the 'super' keyword");
  458. assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; class Base { constructor() { eval("super();"); } }; new Base();`), syntaxError, "'super' call is invalid syntax in a base class", "Invalid use of the 'super' keyword");
  459. assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; class Base { constructor() { () => super(); } }`), syntaxError, "'super' call is invalid syntax in a base class", "Invalid use of the 'super' keyword");
  460. }
  461. },
  462. {
  463. name: "Class derived from null special symbol binding",
  464. body: function() {
  465. class DerivedDefault extends null { }
  466. assert.throws(() => new DerivedDefault(), TypeError, "Class derived from null can't be instantiated", "Function is not a constructor");
  467. class DerivedSuper extends null {
  468. constructor() {
  469. super();
  470. }
  471. }
  472. assert.throws(() => new DerivedSuper(), TypeError, "Class derived from null can't make a super call", "Function is not a constructor");
  473. class DerivedEmpty extends null {
  474. constructor() { }
  475. }
  476. assert.areEqual(DerivedEmpty, new DerivedEmpty().constructor, "Default instance for 'extends null' case is a real instance of the derived class");
  477. var called = false;
  478. class DerivedVerifyNewTarget extends null {
  479. constructor() {
  480. assert.areEqual(DerivedVerifyNewTarget, new.target, "Derived class called as new expression gets new.target");
  481. called = true;
  482. }
  483. }
  484. assert.areEqual(DerivedVerifyNewTarget, new DerivedVerifyNewTarget().constructor, "Default instance for 'extends null' case is a real instance of the derived class");
  485. assert.isTrue(called, "Constructor was actually called");
  486. called = false;
  487. class DerivedVerifyThis extends null {
  488. constructor() {
  489. assert.areEqual(DerivedVerifyThis, this.constructor, "Derived from null class called as new expression gets this instance of the derived class");
  490. called = true;
  491. }
  492. }
  493. assert.areEqual(DerivedVerifyThis, new DerivedVerifyThis().constructor, "Default instance for 'extends null' case is a real instance of the derived class");
  494. assert.isTrue(called, "Constructor was actually called");
  495. }
  496. },
  497. {
  498. name: "Derived class 'this' binding",
  499. body: function() {
  500. class Base { }
  501. class DerivedReturnThis extends Base {
  502. constructor() {
  503. super();
  504. return this;
  505. }
  506. }
  507. assert.areEqual(DerivedReturnThis, new DerivedReturnThis().constructor, "Derived class with constructor that returns this");
  508. class DerivedReturnThisEval extends Base {
  509. constructor() {
  510. super();
  511. return eval('this');
  512. }
  513. }
  514. assert.areEqual(DerivedReturnThisEval, new DerivedReturnThisEval().constructor, "Derived class with constructor that returns this captured by eval");
  515. class DerivedReturnThisArrow extends Base {
  516. constructor() {
  517. super();
  518. return (() => this)();
  519. }
  520. }
  521. assert.areEqual(DerivedReturnThisArrow, new DerivedReturnThisArrow().constructor, "Derived class with constructor that returns this captured by arrow");
  522. class DerivedReturnThisEvalArrow extends Base {
  523. constructor() {
  524. super();
  525. return eval('(() => this)()');
  526. }
  527. }
  528. assert.areEqual(DerivedReturnThisEvalArrow, new DerivedReturnThisEvalArrow().constructor, "Derived class with constructor that returns this captured by arrow inside eval");
  529. class DerivedReturnThisArrowEval extends Base {
  530. constructor() {
  531. super();
  532. return (() => eval('this'))();
  533. }
  534. }
  535. assert.areEqual(DerivedReturnThisArrowEval, new DerivedReturnThisArrowEval().constructor, "Derived class with constructor that returns this captured by eval inside arrow");
  536. eval(`
  537. class DerivedEvalReturnThis extends Base {
  538. constructor() {
  539. super();
  540. return this;
  541. }
  542. }
  543. assert.areEqual(DerivedEvalReturnThis, new DerivedEvalReturnThis().constructor, "Derived class defined in eval with constructor that returns this");
  544. `);
  545. eval(`
  546. class DerivedEvalReturnThisEval extends Base {
  547. constructor() {
  548. super();
  549. return eval('this');
  550. }
  551. }
  552. assert.areEqual(DerivedEvalReturnThisEval, new DerivedEvalReturnThisEval().constructor, "Derived class defined in eval with constructor that returns this captured by eval");
  553. `);
  554. eval(`
  555. class DerivedEvalReturnThisLambda extends Base {
  556. constructor() {
  557. super();
  558. return (() => this)();
  559. }
  560. }
  561. assert.areEqual(DerivedEvalReturnThisLambda, new DerivedEvalReturnThisLambda().constructor, "Derived class defined in eval with constructor that returns this captured by arrow");
  562. `);
  563. }
  564. },
  565. {
  566. name: "Derived class verifying 'this' binding",
  567. body: function() {
  568. class Base { }
  569. var called = false;
  570. class DerivedVerifyThis extends Base {
  571. constructor() {
  572. super();
  573. assert.areEqual(DerivedVerifyThis, this.constructor, "Derived class with constructor that verifies this");
  574. called = true;
  575. }
  576. }
  577. assert.areEqual(DerivedVerifyThis, new DerivedVerifyThis().constructor, "Derived class with constructor that verifies this");
  578. assert.isTrue(called, "Constructor was called");
  579. called = false;
  580. class DerivedVerifyThisLambda extends Base {
  581. constructor() {
  582. super();
  583. (() => {
  584. assert.areEqual(DerivedVerifyThisLambda, this.constructor, "Derived class with constructor that verifies this captured in lambda");
  585. called = true;
  586. })();
  587. }
  588. }
  589. assert.areEqual(DerivedVerifyThisLambda, new DerivedVerifyThisLambda().constructor, "Derived class with constructor that verifies this captured by arrow");
  590. assert.isTrue(called, "Constructor was called");
  591. eval(`
  592. called = false;
  593. class DerivedEvalVerifyThis extends Base {
  594. constructor() {
  595. super();
  596. assert.areEqual(DerivedEvalVerifyThis, this.constructor, "Derived class defined in eval with constructor that verifies this");
  597. called = true;
  598. }
  599. }
  600. assert.areEqual(DerivedEvalVerifyThis, new DerivedEvalVerifyThis().constructor, "Derived class defined in eval with constructor that verifies this");
  601. assert.isTrue(called, "Constructor was called");
  602. `);
  603. }
  604. },
  605. {
  606. name: "Assignment to special symbols is invalid",
  607. body: function() {
  608. function test(code, message) {
  609. assert.throws(() => WScript.LoadScript(code), ReferenceError, message, "Invalid left-hand side in assignment");
  610. }
  611. function testglobal(symname) {
  612. test(`${symname} = 'something'`, `Assignment to global '${symname}'`);
  613. test(`(() => ${symname} = 'something')()`, `Assignment to global '${symname}' captured by lambda`);
  614. test(`eval("${symname} = 'something'")`, `Assignment to global '${symname}' in eval`);
  615. test(`(() => eval("${symname} = 'something'"))()`, `Assignment to global '${symname}' captured by lambda in eval`);
  616. test(`eval("(() => ${symname} = 'something')()")`, `Assignment to global '${symname}' captured by eval in lambda`);
  617. }
  618. function testlocal(symname) {
  619. test(`function foo() { ${symname} = 'something'; }; foo();`, `Assignment to function '${symname}'`);
  620. test(`function foo() { eval("${symname} = 'something'"); }; foo();`, `Assignment to function '${symname}' in nested eval`);
  621. test(`function foo() { (() => ${symname} = 'something')(); }; foo();`, `Assignment to function '${symname}' in nested lambda`);
  622. }
  623. testglobal('this');
  624. testlocal('this');
  625. testlocal('new.target');
  626. }
  627. },
  628. {
  629. name: "Eval 'this' binding",
  630. body: function() {
  631. function SloppyModeCapture() {
  632. assert.areEqual('object', typeof this, "'this' object given to functions in sloppy mode");
  633. this.o = 'SloppyModeCapture';
  634. eval(`assert.areEqual('SloppyModeCapture', this.o, "Direct eval captures 'this' in sloppy mode")`);
  635. eval(`'use strict'; assert.areEqual('SloppyModeCapture', this.o, "Direct strict mode eval captures 'this' in sloppy mode")`);
  636. var not_eval = eval;
  637. not_eval(`assert.areEqual('SloppyModeCapture', this.o, "Indirect eval captures 'this' in sloppy mode")`);
  638. not_eval(`'use strict'; assert.areEqual('SloppyModeCapture', this.o, "Indirect strict mode eval captures 'this' in sloppy mode")`);
  639. }
  640. SloppyModeCapture();
  641. function StrictModeSanity() {
  642. 'use strict';
  643. assert.areEqual(undefined, this, "Strict mode function doesn't get a 'this' object");
  644. }
  645. StrictModeSanity();
  646. lambdaForGlobalThis().o = 'global';
  647. function StrictModeCapture() {
  648. 'use strict';
  649. assert.areEqual('object', typeof this, "'this' object should be passed-in");
  650. eval(`assert.areEqual('StrictModeCapture', this.o, "Direct eval captures 'this' in strict mode")`);
  651. eval(`'use strict'; assert.areEqual('StrictModeCapture', this.o, "Direct strict mode eval captures 'this' in strict mode")`);
  652. var not_eval = eval;
  653. not_eval(`assert.areEqual('global', this.o, "Indirect eval captures global 'this' in strict mode")`);
  654. not_eval(`'use strict'; assert.areEqual('global', this.o, "Indirect strict mode eval captures global 'this' in strict mode")`);
  655. }
  656. StrictModeCapture.call({o:'StrictModeCapture'});
  657. }
  658. },
  659. {
  660. name: "Super reference patterns",
  661. body: function() {
  662. var key_bar = 'bar';
  663. var key_foo = 'foo';
  664. function verifyThis() {
  665. assert.areEqual(obj, this, "'this' passed via super method call should be the object");
  666. return 'bar';
  667. }
  668. var obj = {
  669. callSuperMethodViaDot() { return super.bar(); },
  670. callSuperMethodViaDotArrow() { return (() => super.bar())(); },
  671. callSuperMethodViaDotEval() { return eval('super.bar();'); },
  672. getSuperPropertyViaDot() { return super.foo; },
  673. getSuperPropertyViaDotArrow() { return (() => super.foo)(); },
  674. getSuperPropertyViaDotEval() { return eval('super.foo;'); },
  675. callSuperMethodViaIndex() { return super[key_bar](); },
  676. callSuperMethodViaIndexArrow() { return (() => super[key_bar]())(); },
  677. callSuperMethodViaIndexEval() { return eval('super[key_bar]();'); },
  678. getSuperPropertyViaIndex() { return super[key_foo]; },
  679. getSuperPropertyViaIndexArrow() { return (() => super[key_foo])(); },
  680. getSuperPropertyViaIndexEval() { return eval('super[key_foo];'); },
  681. assignSuperPropertyViaDot() { super.foo = 'something'; return super.foo; },
  682. assignSuperPropertyViaDotArrow() { (() => super.foo = 'something')(); return super.foo; },
  683. assignSuperPropertyViaDotEval() { eval("super.foo = 'something';"); return super.foo; },
  684. assignSuperPropertyViaIndex() { super[key_foo] = 'something'; return super.foo; },
  685. assignSuperPropertyViaIndexArrow() { (() => super[key_foo] = 'something')(); return super.foo; },
  686. assignSuperPropertyViaIndexEval() { eval("super[key_foo] = 'something';"); return super.foo; },
  687. deleteSuperPropertyViaDot() { delete super.foo; },
  688. deleteSuperPropertyViaDotArrow() { (() => delete super.foo)(); },
  689. deleteSuperPropertyViaDotEval() { eval('delete super.foo;'); },
  690. }
  691. var proto = {
  692. bar: verifyThis,
  693. foo: 'foo',
  694. }
  695. Object.setPrototypeOf(obj, proto);
  696. assert.areEqual('bar', obj.callSuperMethodViaDot(), "Call method via 'super.method()'");
  697. assert.areEqual('bar', obj.callSuperMethodViaDotArrow(), "Call method via 'super.method()' in arrow");
  698. assert.areEqual('bar', obj.callSuperMethodViaDotEval(), "Call method via 'super.method()' in eval");
  699. assert.areEqual('foo', obj.getSuperPropertyViaDot(), "Get property via 'super.property'");
  700. assert.areEqual('foo', obj.getSuperPropertyViaDotArrow(), "Get property via 'super.property' in arrow");
  701. assert.areEqual('foo', obj.getSuperPropertyViaDotEval(), "Get property via 'super.property' in eval");
  702. assert.areEqual('bar', obj.callSuperMethodViaIndex(), "Call method via 'super[method]()'");
  703. assert.areEqual('bar', obj.callSuperMethodViaIndexArrow(), "Call method via 'super[method]()' in arrow");
  704. assert.areEqual('bar', obj.callSuperMethodViaIndexEval(), "Call method via 'super[method]()' in eval");
  705. assert.areEqual('foo', obj.getSuperPropertyViaIndex(), "Get property via 'super[property]'");
  706. assert.areEqual('foo', obj.getSuperPropertyViaIndexArrow(), "Get property via 'super[property]' in arrow");
  707. assert.areEqual('foo', obj.getSuperPropertyViaIndexEval(), "Get property via 'super[property]' in eval");
  708. assert.areEqual('foo', obj.assignSuperPropertyViaDot(), "Assign property via 'super.property'");
  709. assert.areEqual('foo', obj.assignSuperPropertyViaDotArrow(), "Assign property via 'super.property' in arrow");
  710. assert.areEqual('foo', obj.assignSuperPropertyViaDotEval(), "Assign property via 'super.property' in eval");
  711. assert.areEqual('something', obj.assignSuperPropertyViaIndex(), "Assign property via 'super[property]'");
  712. assert.areEqual('something', obj.assignSuperPropertyViaIndexArrow(), "Assign property via 'super[property]' in arrow");
  713. assert.areEqual('something', obj.assignSuperPropertyViaIndexEval(), "Assign property via 'super[property]' in eval");
  714. assert.throws(() => obj.deleteSuperPropertyViaDot(), ReferenceError, "Delete property via 'super.property'", "Unable to delete property with a super reference");
  715. assert.throws(() => obj.deleteSuperPropertyViaDotArrow(), ReferenceError, "Delete property via 'super.property' in arrow", "Unable to delete property with a super reference");
  716. assert.throws(() => obj.deleteSuperPropertyViaDotEval(), ReferenceError, "Delete property via 'super.property' in eval", "Unable to delete property with a super reference");
  717. }
  718. },
  719. {
  720. name: "Split-scope with eval in parameter scope",
  721. body: function() {
  722. function f1({a:a}, c = eval("a")) {
  723. return c;
  724. }
  725. assert.areEqual('ok', f1({a:'ok'}), "Eval references previous non-simple formal");
  726. function f2({a:a}, c = eval('this')) {
  727. return c;
  728. }
  729. var _this = {};
  730. assert.areEqual(_this, f2.call(_this, {}), "Eval references 'this' binding");
  731. function f3({a:a}, c = eval('new.target')) {
  732. return c;
  733. }
  734. assert.areEqual(f3, new f3({}), "Eval references 'new.target' binding");
  735. class base {
  736. prop() {
  737. assert.areEqual(c1, this.constructor, "Be sure the 'this' binding flows down the super reference calls")
  738. return 'prop';
  739. }
  740. returnThis() {
  741. assert.areEqual(c2, this.constructor, "Be sure the 'this' binding flows down the super reference calls")
  742. return this;
  743. }
  744. }
  745. var prop_name = 'prop';
  746. class c1 extends base {
  747. constructor({a:a}, c = eval('super()')) {
  748. assert.areEqual(c1, new.target, "'new.target' binding is correct in class constructor with split-scope");
  749. assert.areEqual(this, c, "Result of super() should be the same as 'this'");
  750. assert.areEqual('prop', super.prop(), "Super reference should bind correctly");
  751. }
  752. doSuperRefViaDot({a:a}, c = eval('super.prop()')) {
  753. return c;
  754. }
  755. doSuperRefViaIndex({a:a}, c = eval('super[prop_name]()')) {
  756. return c;
  757. }
  758. }
  759. var inst = new c1({});
  760. assert.areEqual(c1, inst.constructor, "Eval references 'super' in a constructor call");
  761. assert.areEqual('prop', inst.doSuperRefViaDot({}), "Eval references 'super' as a super property reference via dot");
  762. assert.areEqual('prop', inst.doSuperRefViaIndex({}), "Eval references 'super' as a super property reference via index");
  763. class c2 extends base {
  764. constructor({a:a}, c = eval('super(); super.returnThis();')) {
  765. assert.areEqual(c2, new.target, "'new.target' binding is correct in class constructor with split-scope");
  766. assert.areEqual(this, c, "Result of super.returnThis() should be the same as 'this'");
  767. }
  768. }
  769. var inst = new c2({});
  770. assert.areEqual(c2, inst.constructor, "Eval references 'super' in a constructor call");
  771. }
  772. },
  773. {
  774. name: "Formal argument named 'arguments'",
  775. body: function() {
  776. function foo(arguments) {
  777. assert.areEqual('arguments', arguments, "Arguments named formal accessed directly");
  778. assert.areEqual('arguments', (() => arguments)(), "Arguments named formal accessed through a lambda capture");
  779. assert.areEqual('arguments', eval('arguments'), "Arguments named formal accessed through an eval");
  780. }
  781. foo('arguments');
  782. }
  783. },
  784. {
  785. name: "Global 'this' binding captured by strict-mode arrow",
  786. body: function() {
  787. WScript.LoadScript(`"use strict";
  788. assert.areEqual(this, (() => this)(), "Lambda should load the global 'this' value itself via LdThis (not StrictLdThis)");
  789. `);
  790. WScript.LoadScript(`
  791. assert.areEqual(this, (() => { "use strict"; return this; })(), "Lambda which has a 'use strict' clause inside");
  792. `);
  793. WScript.LoadScript(`"use strict";
  794. assert.areEqual('object', typeof (() => this)(), "Verify lambda can load global 'this' value even if the global body itself does not have a 'this' binding");
  795. `);
  796. }
  797. },
  798. {
  799. name: "PidRefStack might be out-of-order when we try to add special symbol var decl",
  800. body: function() {
  801. // Failure causes an assert to fire
  802. WScript.LoadScript(`(a = function() { this }, b = (this)) => {}`);
  803. assert.throws(() => WScript.LoadScript(`[ a = function () { this; } ((this)) = 1 ] = []`), ReferenceError, "Not a valid destructuring assignment but should not fire assert", "Invalid left-hand side in assignment");
  804. }
  805. },
  806. {
  807. name: "Non-split scope with default arguments referencing special names",
  808. body: function() {
  809. var _this = {}
  810. function foo(a = this) {
  811. eval('');
  812. assert.areEqual(_this, a, "Correct default value was assigned");
  813. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  814. }
  815. foo.call(_this)
  816. function bar(a = this) {
  817. eval('');
  818. assert.areEqual(_this, a, "Correct default value was assigned");
  819. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  820. function b() { return 'b'; }
  821. assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
  822. }
  823. bar.call(_this)
  824. function baz(a = this, c = function() { return 'c'; }) {
  825. eval('');
  826. assert.areEqual(_this, a, "Correct default value was assigned");
  827. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  828. function b() { return 'b'; }
  829. assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
  830. assert.areEqual('c', c(), "Function expression in param scope default argument is bound to the correct slot");
  831. }
  832. baz.call(_this)
  833. function baz2(a = this, c = function() { function nested() { return 'c' }; return nested; }) {
  834. eval('');
  835. assert.areEqual(_this, a, "Correct default value was assigned");
  836. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  837. function b() { return 'b'; }
  838. assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
  839. assert.areEqual('c', c()(), "Function decl nested in function expression assigned to default argument");
  840. }
  841. baz2.call(_this)
  842. function baz3(c = function() { return 'c'; }, a = this) {
  843. eval('');
  844. assert.areEqual(_this, a, "Correct default value was assigned");
  845. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  846. function b() { return 'b'; }
  847. assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
  848. assert.areEqual('c', c(), "Function expression in param scope default argument is bound to the correct slot");
  849. }
  850. baz3.call(_this)
  851. function bat(a = this, c = () => 'c') {
  852. eval('');
  853. assert.areEqual(_this, a, "Correct default value was assigned");
  854. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  855. function b() { return 'b'; }
  856. assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
  857. assert.areEqual('c', c(), "Lambda expression in param scope default argument is bound to the correct slot");
  858. }
  859. bat.call(_this)
  860. function bat2(a = this, c = () => () => 'c') {
  861. eval('');
  862. assert.areEqual(_this, a, "Correct default value was assigned");
  863. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  864. function b() { return 'b'; }
  865. assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
  866. assert.areEqual('c', c()(), "Lambda function decl nested in lambda expression assigned to default argument");
  867. }
  868. bat2.call(_this)
  869. function bat3(c = () => () => 'c', a = this) {
  870. eval('');
  871. assert.areEqual(_this, a, "Correct default value was assigned");
  872. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  873. function b() { return 'b'; }
  874. assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
  875. assert.areEqual('c', c()(), "Lambda function decl nested in lambda expression assigned to default argument");
  876. }
  877. bat3.call(_this)
  878. }
  879. },
  880. {
  881. name: "Loading 'this' binding as a call target",
  882. body: function() {
  883. assert.throws(() => WScript.LoadScript(`with({}) { this() }`), TypeError, "Loading global 'this' binding as a call target should always throw - 'this' is an object", "Function expected");
  884. assert.throws(() => this(), TypeError, "Capturing function 'this' binding and emitting as a call target should throw if 'this' is not a function", "Function expected");
  885. }
  886. },
  887. {
  888. name: "Class expression as call target without 'this' binding",
  889. body: function() {
  890. assert.throws(() => WScript.LoadScript(`(class classExpr {}())`), TypeError, "Class expression called at global scope", "Class constructor cannot be called without the new keyword");
  891. assert.throws(() => WScript.LoadScript(`(() => (class classExpr {}()))()`), TypeError, "Class expression called in global lambda", "Class constructor cannot be called without the new keyword");
  892. }
  893. }
  894. ]
  895. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });