SpecialSymbolCapture.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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. }
  474. },
  475. {
  476. name: "Derived class 'this' binding",
  477. body: function() {
  478. class Base { }
  479. class DerivedReturnThis extends Base {
  480. constructor() {
  481. super();
  482. return this;
  483. }
  484. }
  485. assert.areEqual(DerivedReturnThis, new DerivedReturnThis().constructor, "Derived class with constructor that returns this");
  486. class DerivedReturnThisEval extends Base {
  487. constructor() {
  488. super();
  489. return eval('this');
  490. }
  491. }
  492. assert.areEqual(DerivedReturnThisEval, new DerivedReturnThisEval().constructor, "Derived class with constructor that returns this captured by eval");
  493. class DerivedReturnThisArrow extends Base {
  494. constructor() {
  495. super();
  496. return (() => this)();
  497. }
  498. }
  499. assert.areEqual(DerivedReturnThisArrow, new DerivedReturnThisArrow().constructor, "Derived class with constructor that returns this captured by arrow");
  500. class DerivedReturnThisEvalArrow extends Base {
  501. constructor() {
  502. super();
  503. return eval('(() => this)()');
  504. }
  505. }
  506. assert.areEqual(DerivedReturnThisEvalArrow, new DerivedReturnThisEvalArrow().constructor, "Derived class with constructor that returns this captured by arrow inside eval");
  507. class DerivedReturnThisArrowEval extends Base {
  508. constructor() {
  509. super();
  510. return (() => eval('this'))();
  511. }
  512. }
  513. assert.areEqual(DerivedReturnThisArrowEval, new DerivedReturnThisArrowEval().constructor, "Derived class with constructor that returns this captured by eval inside arrow");
  514. eval(`
  515. class DerivedEvalReturnThis extends Base {
  516. constructor() {
  517. super();
  518. return this;
  519. }
  520. }
  521. assert.areEqual(DerivedEvalReturnThis, new DerivedEvalReturnThis().constructor, "Derived class defined in eval with constructor that returns this");
  522. `);
  523. eval(`
  524. class DerivedEvalReturnThisEval extends Base {
  525. constructor() {
  526. super();
  527. return eval('this');
  528. }
  529. }
  530. assert.areEqual(DerivedEvalReturnThisEval, new DerivedEvalReturnThisEval().constructor, "Derived class defined in eval with constructor that returns this captured by eval");
  531. `);
  532. eval(`
  533. class DerivedEvalReturnThisLambda extends Base {
  534. constructor() {
  535. super();
  536. return (() => this)();
  537. }
  538. }
  539. assert.areEqual(DerivedEvalReturnThisLambda, new DerivedEvalReturnThisLambda().constructor, "Derived class defined in eval with constructor that returns this captured by arrow");
  540. `);
  541. }
  542. },
  543. {
  544. name: "Derived class verifying 'this' binding",
  545. body: function() {
  546. class Base { }
  547. var called = false;
  548. class DerivedVerifyThis extends Base {
  549. constructor() {
  550. super();
  551. assert.areEqual(DerivedVerifyThis, this.constructor, "Derived class with constructor that verifies this");
  552. called = true;
  553. }
  554. }
  555. assert.areEqual(DerivedVerifyThis, new DerivedVerifyThis().constructor, "Derived class with constructor that verifies this");
  556. assert.isTrue(called, "Constructor was called");
  557. called = false;
  558. class DerivedVerifyThisLambda extends Base {
  559. constructor() {
  560. super();
  561. (() => {
  562. assert.areEqual(DerivedVerifyThisLambda, this.constructor, "Derived class with constructor that verifies this captured in lambda");
  563. called = true;
  564. })();
  565. }
  566. }
  567. assert.areEqual(DerivedVerifyThisLambda, new DerivedVerifyThisLambda().constructor, "Derived class with constructor that verifies this captured by arrow");
  568. assert.isTrue(called, "Constructor was called");
  569. eval(`
  570. called = false;
  571. class DerivedEvalVerifyThis extends Base {
  572. constructor() {
  573. super();
  574. assert.areEqual(DerivedEvalVerifyThis, this.constructor, "Derived class defined in eval with constructor that verifies this");
  575. called = true;
  576. }
  577. }
  578. assert.areEqual(DerivedEvalVerifyThis, new DerivedEvalVerifyThis().constructor, "Derived class defined in eval with constructor that verifies this");
  579. assert.isTrue(called, "Constructor was called");
  580. `);
  581. }
  582. },
  583. {
  584. name: "Assignment to special symbols is invalid",
  585. body: function() {
  586. function test(code, message) {
  587. assert.throws(() => WScript.LoadScript(code), ReferenceError, message, "Invalid left-hand side in assignment");
  588. }
  589. function testglobal(symname) {
  590. test(`${symname} = 'something'`, `Assignment to global '${symname}'`);
  591. test(`(() => ${symname} = 'something')()`, `Assignment to global '${symname}' captured by lambda`);
  592. test(`eval("${symname} = 'something'")`, `Assignment to global '${symname}' in eval`);
  593. test(`(() => eval("${symname} = 'something'"))()`, `Assignment to global '${symname}' captured by lambda in eval`);
  594. test(`eval("(() => ${symname} = 'something')()")`, `Assignment to global '${symname}' captured by eval in lambda`);
  595. }
  596. function testlocal(symname) {
  597. test(`function foo() { ${symname} = 'something'; }; foo();`, `Assignment to function '${symname}'`);
  598. test(`function foo() { eval("${symname} = 'something'"); }; foo();`, `Assignment to function '${symname}' in nested eval`);
  599. test(`function foo() { (() => ${symname} = 'something')(); }; foo();`, `Assignment to function '${symname}' in nested lambda`);
  600. }
  601. testglobal('this');
  602. testlocal('this');
  603. testlocal('new.target');
  604. }
  605. },
  606. {
  607. name: "Eval 'this' binding",
  608. body: function() {
  609. function SloppyModeCapture() {
  610. assert.areEqual('object', typeof this, "'this' object given to functions in sloppy mode");
  611. this.o = 'SloppyModeCapture';
  612. eval(`assert.areEqual('SloppyModeCapture', this.o, "Direct eval captures 'this' in sloppy mode")`);
  613. eval(`'use strict'; assert.areEqual('SloppyModeCapture', this.o, "Direct strict mode eval captures 'this' in sloppy mode")`);
  614. var not_eval = eval;
  615. not_eval(`assert.areEqual('SloppyModeCapture', this.o, "Indirect eval captures 'this' in sloppy mode")`);
  616. not_eval(`'use strict'; assert.areEqual('SloppyModeCapture', this.o, "Indirect strict mode eval captures 'this' in sloppy mode")`);
  617. }
  618. SloppyModeCapture();
  619. function StrictModeSanity() {
  620. 'use strict';
  621. assert.areEqual(undefined, this, "Strict mode function doesn't get a 'this' object");
  622. }
  623. StrictModeSanity();
  624. lambdaForGlobalThis().o = 'global';
  625. function StrictModeCapture() {
  626. 'use strict';
  627. assert.areEqual('object', typeof this, "'this' object should be passed-in");
  628. eval(`assert.areEqual('StrictModeCapture', this.o, "Direct eval captures 'this' in strict mode")`);
  629. eval(`'use strict'; assert.areEqual('StrictModeCapture', this.o, "Direct strict mode eval captures 'this' in strict mode")`);
  630. var not_eval = eval;
  631. not_eval(`assert.areEqual('global', this.o, "Indirect eval captures global 'this' in strict mode")`);
  632. not_eval(`'use strict'; assert.areEqual('global', this.o, "Indirect strict mode eval captures global 'this' in strict mode")`);
  633. }
  634. StrictModeCapture.call({o:'StrictModeCapture'});
  635. }
  636. },
  637. {
  638. name: "Super reference patterns",
  639. body: function() {
  640. var key_bar = 'bar';
  641. var key_foo = 'foo';
  642. function verifyThis() {
  643. assert.areEqual(obj, this, "'this' passed via super method call should be the object");
  644. return 'bar';
  645. }
  646. var obj = {
  647. callSuperMethodViaDot() { return super.bar(); },
  648. callSuperMethodViaDotArrow() { return (() => super.bar())(); },
  649. callSuperMethodViaDotEval() { return eval('super.bar();'); },
  650. getSuperPropertyViaDot() { return super.foo; },
  651. getSuperPropertyViaDotArrow() { return (() => super.foo)(); },
  652. getSuperPropertyViaDotEval() { return eval('super.foo;'); },
  653. callSuperMethodViaIndex() { return super[key_bar](); },
  654. callSuperMethodViaIndexArrow() { return (() => super[key_bar]())(); },
  655. callSuperMethodViaIndexEval() { return eval('super[key_bar]();'); },
  656. getSuperPropertyViaIndex() { return super[key_foo]; },
  657. getSuperPropertyViaIndexArrow() { return (() => super[key_foo])(); },
  658. getSuperPropertyViaIndexEval() { return eval('super[key_foo];'); },
  659. assignSuperPropertyViaDot() { super.foo = 'something'; return super.foo; },
  660. assignSuperPropertyViaDotArrow() { (() => super.foo = 'something')(); return super.foo; },
  661. assignSuperPropertyViaDotEval() { eval("super.foo = 'something';"); return super.foo; },
  662. assignSuperPropertyViaIndex() { super[key_foo] = 'something'; return super.foo; },
  663. assignSuperPropertyViaIndexArrow() { (() => super[key_foo] = 'something')(); return super.foo; },
  664. assignSuperPropertyViaIndexEval() { eval("super[key_foo] = 'something';"); return super.foo; },
  665. deleteSuperPropertyViaDot() { delete super.foo; },
  666. deleteSuperPropertyViaDotArrow() { (() => delete super.foo)(); },
  667. deleteSuperPropertyViaDotEval() { eval('delete super.foo;'); },
  668. }
  669. var proto = {
  670. bar: verifyThis,
  671. foo: 'foo',
  672. }
  673. Object.setPrototypeOf(obj, proto);
  674. assert.areEqual('bar', obj.callSuperMethodViaDot(), "Call method via 'super.method()'");
  675. assert.areEqual('bar', obj.callSuperMethodViaDotArrow(), "Call method via 'super.method()' in arrow");
  676. assert.areEqual('bar', obj.callSuperMethodViaDotEval(), "Call method via 'super.method()' in eval");
  677. assert.areEqual('foo', obj.getSuperPropertyViaDot(), "Get property via 'super.property'");
  678. assert.areEqual('foo', obj.getSuperPropertyViaDotArrow(), "Get property via 'super.property' in arrow");
  679. assert.areEqual('foo', obj.getSuperPropertyViaDotEval(), "Get property via 'super.property' in eval");
  680. assert.areEqual('bar', obj.callSuperMethodViaIndex(), "Call method via 'super[method]()'");
  681. assert.areEqual('bar', obj.callSuperMethodViaIndexArrow(), "Call method via 'super[method]()' in arrow");
  682. assert.areEqual('bar', obj.callSuperMethodViaIndexEval(), "Call method via 'super[method]()' in eval");
  683. assert.areEqual('foo', obj.getSuperPropertyViaIndex(), "Get property via 'super[property]'");
  684. assert.areEqual('foo', obj.getSuperPropertyViaIndexArrow(), "Get property via 'super[property]' in arrow");
  685. assert.areEqual('foo', obj.getSuperPropertyViaIndexEval(), "Get property via 'super[property]' in eval");
  686. assert.areEqual('foo', obj.assignSuperPropertyViaDot(), "Assign property via 'super.property'");
  687. assert.areEqual('foo', obj.assignSuperPropertyViaDotArrow(), "Assign property via 'super.property' in arrow");
  688. assert.areEqual('foo', obj.assignSuperPropertyViaDotEval(), "Assign property via 'super.property' in eval");
  689. assert.areEqual('something', obj.assignSuperPropertyViaIndex(), "Assign property via 'super[property]'");
  690. assert.areEqual('something', obj.assignSuperPropertyViaIndexArrow(), "Assign property via 'super[property]' in arrow");
  691. assert.areEqual('something', obj.assignSuperPropertyViaIndexEval(), "Assign property via 'super[property]' in eval");
  692. assert.throws(() => obj.deleteSuperPropertyViaDot(), ReferenceError, "Delete property via 'super.property'", "Unable to delete property with a super reference");
  693. assert.throws(() => obj.deleteSuperPropertyViaDotArrow(), ReferenceError, "Delete property via 'super.property' in arrow", "Unable to delete property with a super reference");
  694. assert.throws(() => obj.deleteSuperPropertyViaDotEval(), ReferenceError, "Delete property via 'super.property' in eval", "Unable to delete property with a super reference");
  695. }
  696. },
  697. {
  698. name: "Split-scope with eval in parameter scope",
  699. body: function() {
  700. function f1({a:a}, c = eval("a")) {
  701. return c;
  702. }
  703. assert.areEqual('ok', f1({a:'ok'}), "Eval references previous non-simple formal");
  704. function f2({a:a}, c = eval('this')) {
  705. return c;
  706. }
  707. var _this = {};
  708. assert.areEqual(_this, f2.call(_this, {}), "Eval references 'this' binding");
  709. function f3({a:a}, c = eval('new.target')) {
  710. return c;
  711. }
  712. assert.areEqual(f3, new f3({}), "Eval references 'new.target' binding");
  713. class base {
  714. prop() {
  715. assert.areEqual(c1, this.constructor, "Be sure the 'this' binding flows down the super reference calls")
  716. return 'prop';
  717. }
  718. returnThis() {
  719. assert.areEqual(c2, this.constructor, "Be sure the 'this' binding flows down the super reference calls")
  720. return this;
  721. }
  722. }
  723. var prop_name = 'prop';
  724. class c1 extends base {
  725. constructor({a:a}, c = eval('super()')) {
  726. assert.areEqual(c1, new.target, "'new.target' binding is correct in class constructor with split-scope");
  727. assert.areEqual(this, c, "Result of super() should be the same as 'this'");
  728. assert.areEqual('prop', super.prop(), "Super reference should bind correctly");
  729. }
  730. doSuperRefViaDot({a:a}, c = eval('super.prop()')) {
  731. return c;
  732. }
  733. doSuperRefViaIndex({a:a}, c = eval('super[prop_name]()')) {
  734. return c;
  735. }
  736. }
  737. var inst = new c1({});
  738. assert.areEqual(c1, inst.constructor, "Eval references 'super' in a constructor call");
  739. assert.areEqual('prop', inst.doSuperRefViaDot({}), "Eval references 'super' as a super property reference via dot");
  740. assert.areEqual('prop', inst.doSuperRefViaIndex({}), "Eval references 'super' as a super property reference via index");
  741. class c2 extends base {
  742. constructor({a:a}, c = eval('super(); super.returnThis();')) {
  743. assert.areEqual(c2, new.target, "'new.target' binding is correct in class constructor with split-scope");
  744. assert.areEqual(this, c, "Result of super.returnThis() should be the same as 'this'");
  745. }
  746. }
  747. var inst = new c2({});
  748. assert.areEqual(c2, inst.constructor, "Eval references 'super' in a constructor call");
  749. }
  750. },
  751. {
  752. name: "Formal argument named 'arguments'",
  753. body: function() {
  754. function foo(arguments) {
  755. assert.areEqual('arguments', arguments, "Arguments named formal accessed directly");
  756. assert.areEqual('arguments', (() => arguments)(), "Arguments named formal accessed through a lambda capture");
  757. assert.areEqual('arguments', eval('arguments'), "Arguments named formal accessed through an eval");
  758. }
  759. foo('arguments');
  760. }
  761. },
  762. {
  763. name: "Global 'this' binding captured by strict-mode arrow",
  764. body: function() {
  765. WScript.LoadScript(`"use strict";
  766. assert.areEqual(this, (() => this)(), "Lambda should load the global 'this' value itself via LdThis (not StrictLdThis)");
  767. `);
  768. WScript.LoadScript(`
  769. assert.areEqual(this, (() => { "use strict"; return this; })(), "Lambda which has a 'use strict' clause inside");
  770. `);
  771. WScript.LoadScript(`"use strict";
  772. assert.areEqual('object', typeof (() => this)(), "Verify lambda can load global 'this' value even if the global body itself does not have a 'this' binding");
  773. `);
  774. }
  775. },
  776. {
  777. name: "PidRefStack might be out-of-order when we try to add special symbol var decl",
  778. body: function() {
  779. // Failure causes an assert to fire
  780. WScript.LoadScript(`(a = function() { this }, b = (this)) => {}`);
  781. 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");
  782. }
  783. },
  784. {
  785. name: "Non-split scope with default arguments referencing special names",
  786. body: function() {
  787. var _this = {}
  788. function foo(a = this) {
  789. eval('');
  790. assert.areEqual(_this, a, "Correct default value was assigned");
  791. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  792. }
  793. foo.call(_this)
  794. function bar(a = this) {
  795. eval('');
  796. assert.areEqual(_this, a, "Correct default value was assigned");
  797. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  798. function b() { return 'b'; }
  799. assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
  800. }
  801. bar.call(_this)
  802. function baz(a = this, c = function() { return 'c'; }) {
  803. eval('');
  804. assert.areEqual(_this, a, "Correct default value was assigned");
  805. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  806. function b() { return 'b'; }
  807. assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
  808. assert.areEqual('c', c(), "Function expression in param scope default argument is bound to the correct slot");
  809. }
  810. baz.call(_this)
  811. function baz2(a = this, c = function() { function nested() { return 'c' }; return nested; }) {
  812. eval('');
  813. assert.areEqual(_this, a, "Correct default value was assigned");
  814. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  815. function b() { return 'b'; }
  816. assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
  817. assert.areEqual('c', c()(), "Function decl nested in function expression assigned to default argument");
  818. }
  819. baz2.call(_this)
  820. function baz3(c = function() { return 'c'; }, a = this) {
  821. eval('');
  822. assert.areEqual(_this, a, "Correct default value was assigned");
  823. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  824. function b() { return 'b'; }
  825. assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
  826. assert.areEqual('c', c(), "Function expression in param scope default argument is bound to the correct slot");
  827. }
  828. baz3.call(_this)
  829. function bat(a = this, c = () => 'c') {
  830. eval('');
  831. assert.areEqual(_this, a, "Correct default value was assigned");
  832. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  833. function b() { return 'b'; }
  834. assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
  835. assert.areEqual('c', c(), "Lambda expression in param scope default argument is bound to the correct slot");
  836. }
  837. bat.call(_this)
  838. function bat2(a = this, c = () => () => 'c') {
  839. eval('');
  840. assert.areEqual(_this, a, "Correct default value was assigned");
  841. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  842. function b() { return 'b'; }
  843. assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
  844. assert.areEqual('c', c()(), "Lambda function decl nested in lambda expression assigned to default argument");
  845. }
  846. bat2.call(_this)
  847. function bat3(c = () => () => 'c', a = this) {
  848. eval('');
  849. assert.areEqual(_this, a, "Correct default value was assigned");
  850. assert.areEqual(_this, this, "Regular 'this' binding is correct");
  851. function b() { return 'b'; }
  852. assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
  853. assert.areEqual('c', c()(), "Lambda function decl nested in lambda expression assigned to default argument");
  854. }
  855. bat3.call(_this)
  856. }
  857. },
  858. {
  859. name: "Loading 'this' binding as a call target",
  860. body: function() {
  861. assert.throws(() => WScript.LoadScript(`with({}) { this() }`), TypeError, "Loading global 'this' binding as a call target should always throw - 'this' is an object", "Function expected");
  862. assert.throws(() => this(), TypeError, "Capturing function 'this' binding and emitting as a call target should throw if 'this' is not a function", "Function expected");
  863. }
  864. },
  865. {
  866. name: "Class expression as call target without 'this' binding",
  867. body: function() {
  868. assert.throws(() => WScript.LoadScript(`(class classExpr {}())`), TypeError, "Class expression called at global scope", "Class constructor cannot be called without the new keyword");
  869. assert.throws(() => WScript.LoadScript(`(() => (class classExpr {}()))()`), TypeError, "Class expression called in global lambda", "Class constructor cannot be called without the new keyword");
  870. }
  871. },
  872. {
  873. name: "Indirect eval should not create a 'this' binding",
  874. body: function() {
  875. WScript.LoadScript(`
  876. this.eval("(() => assert.areEqual('global', this.o, 'Lambda in indirect eval called off of this capturing this'))()");
  877. this['eval']("(() => assert.areEqual('global', this.o, 'Lambda in indirect eval called from a property index capturing this'))()");
  878. var _eval = 'eval';
  879. this[_eval]("(() => assert.areEqual('global', this.o, 'Lambda in indirect eval called from a property index capturing this'))()");
  880. _eval = eval;
  881. _eval("(() => assert.areEqual('global', this.o, 'Lambda in indirect eval capturing this'))()");
  882. `);
  883. }
  884. }
  885. ]
  886. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });