ES6Class_SuperChain.js 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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. // ES6 super chain tests
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. class SimpleParent {
  8. constructor() {
  9. this.foo = 'SimpleParent';
  10. }
  11. }
  12. let calls_to_ConstructorCountingParent = 0;
  13. class ConstructorCountingParent {
  14. constructor() {
  15. calls_to_ConstructorCountingParent++;
  16. }
  17. }
  18. class UninitializedThisReturningArgumentConstructor extends SimpleParent {
  19. constructor(arg) {
  20. return arg;
  21. }
  22. };
  23. class InitializedThisReturningArgumentConstructor extends SimpleParent {
  24. constructor(arg) {
  25. super();
  26. return arg;
  27. }
  28. };
  29. var tests = [
  30. {
  31. name: "Simple derived class constructor using this",
  32. body: function () {
  33. class DerivedClassUsingThis extends SimpleParent {
  34. constructor() {
  35. super();
  36. this.bar = "DerivedClassUsingThis";
  37. }
  38. };
  39. let result = new DerivedClassUsingThis();
  40. assert.areEqual("DerivedClassUsingThis", result.bar, "This is initialized with the return value from super()");
  41. assert.areEqual("SimpleParent", result.foo, "Parent class returned the object from super to derived constructor");
  42. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  43. assert.isTrue(result instanceof DerivedClassUsingThis, "Result object is instanceof derived class");
  44. }
  45. },
  46. {
  47. name: "Simple derived class constructor using this without calling super causing use before declaration error",
  48. body: function () {
  49. class DerivedClassUsingThisIllegally extends SimpleParent {
  50. constructor() {
  51. this.bar = "DerivedClassUsingThisIllegally";
  52. }
  53. };
  54. assert.throws(function() { new DerivedClassUsingThisIllegally(); }, ReferenceError, "It's a ReferenceError to access 'this' without calling super", "Use before declaration");
  55. }
  56. },
  57. {
  58. name: "Simple derived class constructor using this before calling super causing use before declaration error",
  59. body: function () {
  60. class DerivedClassUsingThisIllegally extends SimpleParent {
  61. constructor() {
  62. this.bar = "DerivedClassUsingThisIllegally";
  63. super();
  64. }
  65. };
  66. assert.throws(function() { new DerivedClassUsingThisIllegally(); }, ReferenceError, "It's a ReferenceError to access 'this' without calling super", "Use before declaration");
  67. }
  68. },
  69. {
  70. name: "Simple derived class constructor using this via a lambda",
  71. body: function () {
  72. class DerivedClassUsingThisViaLambda extends SimpleParent {
  73. constructor() {
  74. var arrow = () => { this.bar = 'DerivedClassUsingThisViaLambda'; };
  75. super();
  76. arrow();
  77. }
  78. };
  79. let result = new DerivedClassUsingThisViaLambda();
  80. assert.areEqual("DerivedClassUsingThisViaLambda", result.bar, "Arrow is defined using this (before super call) and called after super call in derived constructor");
  81. assert.areEqual("SimpleParent", result.foo, "Parent class returned the object from super to derived constructor");
  82. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  83. assert.isTrue(result instanceof DerivedClassUsingThisViaLambda, "Result object is instanceof derived class");
  84. }
  85. },
  86. {
  87. name: "Simple derived class constructor using this without calling super via a lambda causing use before declaration error",
  88. body: function () {
  89. class DerivedClassUsingThisIllegallyViaLambda extends SimpleParent {
  90. constructor() {
  91. var arrow = () => { this.bar = 'DerivedClassUsingThisIllegallyViaLambda'; };
  92. arrow();
  93. }
  94. };
  95. assert.throws(function() { new DerivedClassUsingThisIllegallyViaLambda(); }, ReferenceError, "It's a ReferenceError to access 'this' without calling super", "Use before declaration");
  96. }
  97. },
  98. {
  99. name: "Simple derived class constructor using this before calling super via a lambda causing use before declaration error",
  100. body: function () {
  101. class DerivedClassUsingThisIllegallyViaLambda extends SimpleParent {
  102. constructor() {
  103. var arrow = () => { this.bar = 'DerivedClassUsingThisIllegallyViaLambda'; };
  104. arrow();
  105. super();
  106. }
  107. };
  108. assert.throws(function() { new DerivedClassUsingThisIllegallyViaLambda(); }, ReferenceError, "It's a ReferenceError to access 'this' without calling super", "Use before declaration");
  109. }
  110. },
  111. {
  112. name: "Simple derived class constructor using this and super via a lambda",
  113. body: function () {
  114. class DerivedClassUsingThisAndSuperViaLambda extends SimpleParent {
  115. constructor() {
  116. var this_arrow = () => { this.bar = 'DerivedClassUsingThisAndSuperViaLambda'; };
  117. var super_arrow = () => { super(); }
  118. super_arrow();
  119. this_arrow();
  120. }
  121. };
  122. let result = new DerivedClassUsingThisAndSuperViaLambda();
  123. assert.areEqual("DerivedClassUsingThisAndSuperViaLambda", result.bar, "Arrow is defined using this (before super call) and called after super call in derived constructor");
  124. assert.areEqual("SimpleParent", result.foo, "Parent class returned the object from super to derived constructor");
  125. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  126. assert.isTrue(result instanceof DerivedClassUsingThisAndSuperViaLambda, "Result object is instanceof derived class");
  127. }
  128. },
  129. {
  130. name: "Simple derived class constructor using this and super via a lambda (lambda with super call defined first)",
  131. body: function () {
  132. class DerivedClassUsingThisAndSuperViaLambda extends SimpleParent {
  133. constructor() {
  134. var super_arrow = () => { super(); }
  135. var this_arrow = () => { this.bar = 'DerivedClassUsingThisAndSuperViaLambda'; };
  136. super_arrow();
  137. this_arrow();
  138. }
  139. };
  140. let result = new DerivedClassUsingThisAndSuperViaLambda();
  141. assert.areEqual("DerivedClassUsingThisAndSuperViaLambda", result.bar, "Arrow is defined using this (before super call) and called after super call in derived constructor");
  142. assert.areEqual("SimpleParent", result.foo, "Parent class returned the object from super to derived constructor");
  143. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  144. assert.isTrue(result instanceof DerivedClassUsingThisAndSuperViaLambda, "Result object is instanceof derived class");
  145. }
  146. },
  147. {
  148. name: "Derived class constructor throws ReferenceError accessing this with no super call",
  149. body: function () {
  150. class IllegalThisAccessConstructor extends SimpleParent {
  151. constructor() {
  152. this.bar = 'something';
  153. }
  154. };
  155. assert.throws(function() { new IllegalThisAccessConstructor(); }, ReferenceError, "It's a ReferenceError to access 'this' without calling super", "Use before declaration");
  156. }
  157. },
  158. {
  159. name: "Derived class constructor throws ReferenceError accessing this before super call",
  160. body: function () {
  161. class IllegalThisBeforeSuperConstructor extends SimpleParent {
  162. constructor() {
  163. this.bar = 'something';
  164. super();
  165. }
  166. };
  167. assert.throws(function() { new IllegalThisBeforeSuperConstructor(); }, ReferenceError, "It's a ReferenceError to access 'this' before calling super", "Use before declaration");
  168. }
  169. },
  170. {
  171. name: "Derived class throws ReferenceError with empty constructor (implicit access of this)",
  172. body: function () {
  173. class EmptyConstructor extends SimpleParent {
  174. constructor() {
  175. // Implicitly "return this;"
  176. }
  177. };
  178. assert.throws(function() { new EmptyConstructor(); }, ReferenceError, "It's a ReferenceError to implicit return 'this' from class constructor without calling super", "Use before declaration");
  179. }
  180. },
  181. {
  182. name: "Derived class with default constructor returns initialized this argument",
  183. body: function () {
  184. class DefaultConstructor extends SimpleParent {
  185. // Implicitly
  186. // constructor(...args) { super(...args); }
  187. };
  188. let obj = new DefaultConstructor();
  189. assert.areEqual('SimpleParent', obj.foo, "Object from base constructor should have been returned.");
  190. assert.isTrue(obj instanceof SimpleParent, "Result object is instanceof base class");
  191. assert.isTrue(obj instanceof DefaultConstructor, "Result object is instanceof derived class");
  192. class DefaultConstructorReturningArgumentViaBaseClass extends UninitializedThisReturningArgumentConstructor {
  193. // Implicitly
  194. // constructor(...args) { super(...args); }
  195. };
  196. obj = new DefaultConstructorReturningArgumentViaBaseClass({ bar: 'DefaultConstructorReturningArgumentViaBaseClass' });
  197. assert.areEqual('DefaultConstructorReturningArgumentViaBaseClass', obj.bar, "Object from base constructor should have been returned.");
  198. assert.isFalse(obj instanceof UninitializedThisReturningArgumentConstructor, "Result object is not instanceof base class");
  199. assert.isFalse(obj instanceof DefaultConstructorReturningArgumentViaBaseClass, "Result object is not instanceof derived class");
  200. }
  201. },
  202. {
  203. name: "Derived class throws TypeError when returning non-object if this is initialized",
  204. body: function () {
  205. assert.throws(function() { new InitializedThisReturningArgumentConstructor(null); }, TypeError, "Returning null from derived constructor should throw TypeError", "Derived class constructor can return only object or undefined");
  206. assert.throws(function() { new InitializedThisReturningArgumentConstructor('string'); }, TypeError, "Returning 'string' from derived constructor should throw TypeError", "Derived class constructor can return only object or undefined");
  207. assert.throws(function() { new InitializedThisReturningArgumentConstructor(5); }, TypeError, "Returning 5 from derived constructor should throw TypeError", "Derived class constructor can return only object or undefined");
  208. }
  209. },
  210. {
  211. name: "Derived class throws TypeError when returning non-object if this is uninitialized",
  212. body: function () {
  213. assert.throws(function() { new UninitializedThisReturningArgumentConstructor(null); }, TypeError, "Returning null from derived constructor should throw TypeError", "Derived class constructor can return only object or undefined");
  214. assert.throws(function() { new UninitializedThisReturningArgumentConstructor('string'); }, TypeError, "Returning 'string' from derived constructor should throw TypeError", "Derived class constructor can return only object or undefined");
  215. assert.throws(function() { new UninitializedThisReturningArgumentConstructor(5); }, TypeError, "Returning 5 from derived constructor should throw TypeError", "Derived class constructor can return only object or undefined");
  216. }
  217. },
  218. {
  219. name: "TypeError is thrown when trying to derive from a function which is not a constructor",
  220. body: function () {
  221. assert.throws(function () { class A extends JSON { }; }, TypeError, "JSON object does not have an internal Construct slot", "Function is not a constructor");
  222. assert.throws(function () { class A extends Math { }; }, TypeError, "Math object does not have an internal Construct slot", "Function is not a constructor");
  223. function* gf(a) { yield 1 + a + this.a; }
  224. assert.throws(function () { class A extends gf { } }, TypeError, "Class deriving from a generator function throws TypeError", "Function is not a constructor");
  225. assert.throws(function () { class A extends (a) => a {} }, TypeError, "Class deriving from a lambda throws TypeError", "Function is not a constructor");
  226. }
  227. },
  228. {
  229. name: "Derived class returning undefined, returns 'this' instead",
  230. body: function () {
  231. class ImplicitReturnUndefinedNotInitializedThis extends SimpleParent {
  232. constructor() {
  233. return;
  234. }
  235. }
  236. assert.throws(function() { new ImplicitReturnUndefinedNotInitializedThis(); }, ReferenceError, "Derived class constructor implicitly returning undefined with no super call throws ReferenceError", "Use before declaration");
  237. class ExplicitReturnUndefinedNotInitializedThis extends SimpleParent {
  238. constructor() {
  239. return undefined;
  240. }
  241. }
  242. assert.throws(function() { new ExplicitReturnUndefinedNotInitializedThis(); }, ReferenceError, "Derived class constructor explicitly returning undefined with no super call throws ReferenceError", "Use before declaration");
  243. class ImplicitReturnUndefinedInitializedThis extends SimpleParent {
  244. constructor() {
  245. super();
  246. this.bar = 'ImplicitReturnUndefinedInitializedThis';
  247. return;
  248. }
  249. }
  250. let result = new ImplicitReturnUndefinedInitializedThis();
  251. assert.areEqual('SimpleParent', result.foo, "Object from base constructor should have been returned.");
  252. assert.areEqual('ImplicitReturnUndefinedInitializedThis', result.bar, "'this' modified in derived constructor.");
  253. assert.isTrue(result instanceof ImplicitReturnUndefinedInitializedThis, "Result object is instanceof derived class");
  254. assert.isTrue(result instanceof SimpleParent, "Result object is not instanceof base class");
  255. class ExplicitReturnUndefinedInitializedThis extends SimpleParent {
  256. constructor() {
  257. super();
  258. this.bar = 'ExplicitReturnUndefinedInitializedThis';
  259. return undefined;
  260. }
  261. }
  262. result = new ExplicitReturnUndefinedInitializedThis();
  263. assert.areEqual('SimpleParent', result.foo, "Object from base constructor should have been returned.");
  264. assert.areEqual('ExplicitReturnUndefinedInitializedThis', result.bar, "'this' modified in derived constructor.");
  265. assert.isTrue(result instanceof ExplicitReturnUndefinedInitializedThis, "Result object is instanceof derived class");
  266. assert.isTrue(result instanceof SimpleParent, "Result object is not instanceof base class");
  267. }
  268. },
  269. {
  270. name: "Derived class returns any object and avoids super call / this initialization",
  271. body: function () {
  272. let obj = new UninitializedThisReturningArgumentConstructor({ foo: 'value' });
  273. assert.areEqual('value', obj.foo, "We returned an object from the class constructor which didn't initialize 'this' so we should get that object back");
  274. }
  275. },
  276. {
  277. name: "Derived class throws ReferenceError with multiple calls to super ('this' is already initialized)",
  278. body: function () {
  279. // Reset call counter in parent
  280. calls_to_ConstructorCountingParent = 0;
  281. class IllegalSuperCallConstructor extends ConstructorCountingParent {
  282. constructor() {
  283. super();
  284. super();
  285. }
  286. };
  287. assert.throws(function() { new IllegalSuperCallConstructor(); }, ReferenceError, "It's a ReferenceError to call super multiple times in a derived class constructor", "Multiple calls to 'super' in a class constructor are not allowed");
  288. assert.areEqual(2, calls_to_ConstructorCountingParent, "We do call the super function body twice but we throw ReferenceError when trying to assign the 'this' value after the second super call");
  289. }
  290. },
  291. {
  292. name: "Derived class throws ReferenceError with multiple calls to super ('this' is already initialized) with one call in a lambda",
  293. body: function () {
  294. // Reset call counter in parent
  295. calls_to_ConstructorCountingParent = 0;
  296. class IllegalSuperCallConstructor extends ConstructorCountingParent {
  297. constructor() {
  298. let arrow = () => { super(); };
  299. super();
  300. arrow();
  301. }
  302. };
  303. assert.throws(function() { new IllegalSuperCallConstructor(); }, ReferenceError, "It's a ReferenceError to call super multiple times in a derived class constructor", "Multiple calls to 'super' in a class constructor are not allowed");
  304. assert.areEqual(2, calls_to_ConstructorCountingParent, "We do call the super function body twice but we throw ReferenceError when trying to assign the 'this' value after the second super call");
  305. }
  306. },
  307. {
  308. name: "Derived class throws ReferenceError with multiple calls to super ('this' is already initialized) with multiple calls in lambdas",
  309. body: function () {
  310. // Reset call counter in parent
  311. calls_to_ConstructorCountingParent = 0;
  312. class IllegalSuperCallConstructor extends ConstructorCountingParent {
  313. constructor() {
  314. let arrow = () => { super(); };
  315. let arrow2 = () => { super(); };
  316. arrow();
  317. arrow2();
  318. }
  319. };
  320. assert.throws(function() { new IllegalSuperCallConstructor(); }, ReferenceError, "It's a ReferenceError to call super multiple times in a derived class constructor", "Multiple calls to 'super' in a class constructor are not allowed");
  321. assert.areEqual(2, calls_to_ConstructorCountingParent, "We do call the super function body twice but we throw ReferenceError when trying to assign the 'this' value after the second super call");
  322. }
  323. },
  324. {
  325. name: "Derived class throws ReferenceError with multiple calls to super ('this' is already initialized) with multiple calls in the same lambda",
  326. body: function () {
  327. // Reset call counter in parent
  328. calls_to_ConstructorCountingParent = 0;
  329. class IllegalSuperCallConstructor extends ConstructorCountingParent {
  330. constructor() {
  331. let arrow = () => { super(); super(); };
  332. arrow();
  333. }
  334. };
  335. assert.throws(function() { new IllegalSuperCallConstructor(); }, ReferenceError, "It's a ReferenceError to call super multiple times in a derived class constructor", "Multiple calls to 'super' in a class constructor are not allowed");
  336. assert.areEqual(2, calls_to_ConstructorCountingParent, "We do call the super function body twice but we throw ReferenceError when trying to assign the 'this' value after the second super call");
  337. }
  338. },
  339. {
  340. name: "Derived class constructor captures this and super in a lambda but doesn't call the lambda",
  341. body: function () {
  342. class DerivedClassCapturingThisAndSuper extends SimpleParent {
  343. constructor() {
  344. let arrow = () => { this.bar = 'lambda'; super(); };
  345. super();
  346. this.bar = "DerivedClassCapturingThisAndSuper";
  347. }
  348. };
  349. let result = new DerivedClassCapturingThisAndSuper();
  350. assert.areEqual("DerivedClassCapturingThisAndSuper", result.bar, "This is initialized with the return value from super()");
  351. assert.areEqual("SimpleParent", result.foo, "Parent class returned the object from super to derived constructor");
  352. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  353. assert.isTrue(result instanceof DerivedClassCapturingThisAndSuper, "Result object is instanceof derived class");
  354. }
  355. },
  356. {
  357. name: "Creating an instance of base class doesn't block new.target calculation for super chain",
  358. body: function () {
  359. let parent = new SimpleParent();
  360. class SimpleDerivedClass extends SimpleParent {
  361. constructor() {
  362. super();
  363. this.bar = "SimpleDerivedClass";
  364. }
  365. };
  366. let result = new SimpleDerivedClass();
  367. assert.isFalse(Object.hasOwnProperty(parent, 'bar'), "Parent object doesn't have derived class values");
  368. assert.areEqual("SimpleParent", parent.foo, "Parent class initialized the object");
  369. assert.isTrue(parent instanceof SimpleParent, "Parent object is instanceof base class");
  370. assert.isFalse(parent instanceof SimpleDerivedClass, "Parent object isn't instanceof derived class");
  371. assert.areEqual("SimpleDerivedClass", result.bar, "This is initialized with the return value from super()");
  372. assert.areEqual("SimpleParent", result.foo, "Parent class returned the object from super to derived constructor");
  373. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  374. assert.isTrue(result instanceof SimpleDerivedClass, "Result object is instanceof derived class");
  375. }
  376. },
  377. {
  378. name: "Chain of multiple derived classes",
  379. body: function () {
  380. class MiddleDerivedClass extends SimpleParent {
  381. constructor() {
  382. super();
  383. this.bar = "MiddleDerivedClass";
  384. }
  385. };
  386. class BottomDerivedClass extends MiddleDerivedClass {
  387. constructor() {
  388. super();
  389. this.baz = "BottomDerivedClass";
  390. }
  391. };
  392. let result = new BottomDerivedClass();
  393. assert.areEqual("BottomDerivedClass", result.baz, "This is initialized with the return value from super()");
  394. assert.areEqual("MiddleDerivedClass", result.bar, "This is initialized with the return value from super()");
  395. assert.areEqual("SimpleParent", result.foo, "Parent class returned the object from super to derived constructor");
  396. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  397. assert.isTrue(result instanceof MiddleDerivedClass, "Result object is instanceof derived class");
  398. assert.isTrue(result instanceof BottomDerivedClass, "Result object is instanceof derived class");
  399. }
  400. },
  401. {
  402. name: "Derived class constructor leaks lambda which performs super",
  403. body: function() {
  404. class A {
  405. constructor() {
  406. this.a = 'A';
  407. }
  408. }
  409. class B extends A {
  410. constructor() {
  411. super();
  412. this.b = 'B';
  413. }
  414. }
  415. class C extends B {
  416. constructor() {
  417. var s = () => { super(); this.c = 'C'; return this; };
  418. return s;
  419. }
  420. }
  421. var maker = new C();
  422. var result = maker();
  423. assert.areEqual("C", result.c, "This is initialized with the return value from super()");
  424. assert.areEqual("B", result.b, "This is initialized with the return value from super()");
  425. assert.areEqual("A", result.a, "Parent class returned the object from super to derived constructor");
  426. assert.isTrue(result instanceof A, "Result object is instanceof base class");
  427. assert.isTrue(result instanceof B, "Result object is instanceof derived class");
  428. assert.isTrue(result instanceof C, "Result object is instanceof derived class");
  429. assert.throws(function() { var result2 = maker(); }, ReferenceError, "Calling the escaped lambda again will throw since 'this' of the parent is already initialized", "Multiple calls to 'super' in a class constructor are not allowed");
  430. // creating a new lambda should let us construct one more object
  431. maker = new C();
  432. result = maker();
  433. assert.areEqual("C", result.c, "This is initialized with the return value from super()");
  434. assert.areEqual("B", result.b, "This is initialized with the return value from super()");
  435. assert.areEqual("A", result.a, "Parent class returned the object from super to derived constructor");
  436. assert.isTrue(result instanceof A, "Result object is instanceof base class");
  437. assert.isTrue(result instanceof B, "Result object is instanceof derived class");
  438. assert.isTrue(result instanceof C, "Result object is instanceof derived class");
  439. assert.throws(function() { var result2 = maker(); }, ReferenceError, "Calling the escaped lambda again will throw since 'this' of the parent is already initialized", "Multiple calls to 'super' in a class constructor are not allowed");
  440. }
  441. },
  442. {
  443. name: "Derived class constructor leaks lambda which performs super (lamdba comes from middle derived class)",
  444. body: function() {
  445. class A {
  446. constructor() {
  447. this.a = 'A';
  448. }
  449. }
  450. class B extends A {
  451. constructor() {
  452. var s = () => { super(); this.b = 'B'; return this; };
  453. return s;
  454. }
  455. }
  456. class C extends B {
  457. constructor() {
  458. super();
  459. }
  460. }
  461. var maker = new C();
  462. var result = maker();
  463. assert.areEqual("B", result.b, "This is initialized with the return value from super()");
  464. assert.areEqual("A", result.a, "Parent class returned the object from super to derived constructor");
  465. assert.isTrue(result instanceof A, "Result object is instanceof base class");
  466. assert.isTrue(result instanceof B, "Result object is instanceof derived class");
  467. assert.isTrue(result instanceof C, "Result object is instanceof derived class");
  468. assert.throws(function() { var result2 = maker(); }, ReferenceError, "Calling the escaped lambda again will throw since 'this' of the parent is already initialized", "Multiple calls to 'super' in a class constructor are not allowed");
  469. // creating a new lambda should let us construct one more object
  470. maker = new C();
  471. result = maker();
  472. assert.areEqual("B", result.b, "This is initialized with the return value from super()");
  473. assert.areEqual("A", result.a, "Parent class returned the object from super to derived constructor");
  474. assert.isTrue(result instanceof A, "Result object is instanceof base class");
  475. assert.isTrue(result instanceof B, "Result object is instanceof derived class");
  476. assert.isTrue(result instanceof C, "Result object is instanceof derived class");
  477. assert.throws(function() { var result2 = maker(); }, ReferenceError, "Calling the escaped lambda again will throw since 'this' of the parent is already initialized", "Multiple calls to 'super' in a class constructor are not allowed");
  478. }
  479. },
  480. {
  481. name: "Derived class constructor leaks lambda which references 'this' in TDZ",
  482. body: function() {
  483. class A {
  484. constructor() {
  485. this.a = 'A';
  486. }
  487. }
  488. class B extends A {
  489. constructor() {
  490. super();
  491. this.b = 'B';
  492. }
  493. }
  494. class C extends B {
  495. constructor() {
  496. var s = () => { this.c = 'C'; super(); return this; };
  497. return s;
  498. }
  499. }
  500. var maker = new C();
  501. assert.throws(function() { var result = maker(); }, ReferenceError, "Calling the escaped lambda throws since 'this' is accessed before super call", "Use before declaration");
  502. }
  503. },
  504. {
  505. name: "Derived class constructor leaks lambda which references 'this' in TDZ (lamdba comes from middle derived class)",
  506. body: function() {
  507. class A {
  508. constructor() {
  509. this.a = 'A';
  510. }
  511. }
  512. class B extends A {
  513. constructor() {
  514. var s = () => { this.b = 'B'; super(); return this; };
  515. return s;
  516. }
  517. }
  518. class C extends B {
  519. constructor() {
  520. super();
  521. }
  522. }
  523. var maker = new C();
  524. assert.throws(function() { var result = maker(); }, ReferenceError, "Calling the escaped lambda throws since 'this' is accessed before super call", "Use before declaration");
  525. }
  526. },
  527. {
  528. name: "Derived class with null extends expression cannot be new'd",
  529. body: function () {
  530. class NullExtendsExpression extends null {
  531. };
  532. assert.throws(function() { new NullExtendsExpression(); }, TypeError, "Class that extends null throws when we attempt to call super as [[construct]]", "Function 'super' is not a constructor");
  533. }
  534. },
  535. {
  536. name: "Derived class with null extends expression can be new'd if constructor returns object",
  537. body: function () {
  538. class NullExtendsExpressionWithConstructor extends null {
  539. constructor(arg) {
  540. return arg;
  541. }
  542. };
  543. var result = new NullExtendsExpressionWithConstructor({foo:'value'});
  544. assert.areEqual('value', result.foo, "Class derived from null expression can return an object safely");
  545. assert.isFalse(result instanceof NullExtendsExpressionWithConstructor, "Result object is not instanceof class");
  546. }
  547. },
  548. {
  549. name: "Derived constructor with a super call that isn't executed",
  550. body: function () {
  551. let returnFalse = () => { return false; };
  552. class DerivedClassAccessThisImplicitReturn extends SimpleParent {
  553. constructor() {
  554. if (returnFalse()) {
  555. super();
  556. }
  557. this.bar = '';
  558. }
  559. };
  560. assert.throws(function() { new DerivedClassAccessThisImplicitReturn(); }, ReferenceError, "When super isn't called, this is undecl", "Use before declaration");
  561. class DerivedClassImplicitReturn extends SimpleParent {
  562. constructor() {
  563. if (returnFalse()) {
  564. super();
  565. }
  566. }
  567. };
  568. assert.throws(function() { new DerivedClassImplicitReturn(); }, ReferenceError, "When super isn't called, this is undecl", "Use before declaration");
  569. class DerivedClassAccessThisExplicitReturn extends SimpleParent {
  570. constructor() {
  571. if (returnFalse()) {
  572. super();
  573. }
  574. this.bar = '';
  575. return this;
  576. }
  577. };
  578. assert.throws(function() { new DerivedClassAccessThisExplicitReturn(); }, ReferenceError, "When super isn't called, this is undecl", "Use before declaration");
  579. class DerivedClassExplicitReturn extends SimpleParent {
  580. constructor() {
  581. if (returnFalse()) {
  582. super();
  583. }
  584. return this;
  585. }
  586. };
  587. assert.throws(function() { new DerivedClassExplicitReturn(); }, ReferenceError, "When super isn't called, this is undecl", "Use before declaration");
  588. class DerivedClassAccessThisViaLambdaImplicitReturn extends SimpleParent {
  589. constructor() {
  590. let arrow = () => { this.foo = ''; }
  591. if (returnFalse()) {
  592. super();
  593. }
  594. arrow();
  595. }
  596. };
  597. assert.throws(function() { new DerivedClassAccessThisViaLambdaImplicitReturn(); }, ReferenceError, "When super isn't called, this is undecl", "Use before declaration");
  598. class DerivedClassAccessThisViaLambdaExplicitReturn extends SimpleParent {
  599. constructor() {
  600. let arrow = () => { this.foo = ''; }
  601. if (returnFalse()) {
  602. super();
  603. }
  604. arrow();
  605. return this;
  606. }
  607. };
  608. assert.throws(function() { new DerivedClassAccessThisViaLambdaExplicitReturn(); }, ReferenceError, "When super isn't called, this is undecl", "Use before declaration");
  609. class DerivedClassWithThisScopeCaptureNoAccessImplicitReturn extends SimpleParent {
  610. constructor() {
  611. let arrow = () => { this.foo = ''; }
  612. if (returnFalse()) {
  613. super();
  614. }
  615. }
  616. };
  617. assert.throws(function() { new DerivedClassWithThisScopeCaptureNoAccessImplicitReturn(); }, ReferenceError, "When super isn't called, this is undecl", "Use before declaration");
  618. class DerivedClassWithThisScopeCaptureNoAccessExplicitReturn extends SimpleParent {
  619. constructor() {
  620. let arrow = () => { this.foo = ''; }
  621. if (returnFalse()) {
  622. super();
  623. }
  624. return this;
  625. }
  626. };
  627. assert.throws(function() { new DerivedClassWithThisScopeCaptureNoAccessExplicitReturn(); }, ReferenceError, "When super isn't called, this is undecl", "Use before declaration");
  628. }
  629. },
  630. {
  631. name: "Derived class with super call inside an eval",
  632. body: function () {
  633. class SuperCallInEvalClass extends SimpleParent {
  634. constructor() {
  635. eval('super();');
  636. }
  637. };
  638. var result = new SuperCallInEvalClass();
  639. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  640. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  641. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  642. }
  643. },
  644. {
  645. name: "Derived class with super call inside an eval which accesses this",
  646. body: function () {
  647. class SuperCallInEvalClass extends SimpleParent {
  648. constructor() {
  649. eval('super(); this.bar = "SuperCallInEvalClass";');
  650. }
  651. };
  652. var result = new SuperCallInEvalClass();
  653. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  654. assert.areEqual('SuperCallInEvalClass', result.bar, "Class derived from SimpleParent can return an object safely");
  655. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  656. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  657. }
  658. },
  659. {
  660. name: "Derived class with super call inside an eval which accesses new.target",
  661. body: function () {
  662. class SuperCallInEvalClass extends SimpleParent {
  663. constructor() {
  664. eval('assert.areEqual(new.target, SuperCallInEvalClass, "new.target === SuperCallInEvalClass"); super();');
  665. }
  666. };
  667. var result = new SuperCallInEvalClass();
  668. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  669. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  670. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  671. }
  672. },
  673. {
  674. name: "Derived class with super call inside an eval which accesses new.target and this",
  675. body: function () {
  676. class SuperCallInEvalClass extends SimpleParent {
  677. constructor() {
  678. eval('assert.areEqual(new.target, SuperCallInEvalClass); super(); this.bar = "SuperCallInEvalClass";');
  679. }
  680. };
  681. var result = new SuperCallInEvalClass();
  682. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  683. assert.areEqual('SuperCallInEvalClass', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass'");
  684. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  685. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  686. }
  687. },
  688. {
  689. name: "Derived class with super call inside an eval access to this inside eval and constructor",
  690. body: function () {
  691. class SuperCallInEvalClass extends SimpleParent {
  692. constructor() {
  693. eval('super(); this.bar = "SuperCallInEvalClass";');
  694. this.baz = "SuperCallInEvalClass_ctor";
  695. }
  696. };
  697. var result = new SuperCallInEvalClass();
  698. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  699. assert.areEqual('SuperCallInEvalClass', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass'");
  700. assert.areEqual('SuperCallInEvalClass_ctor', result.baz, "Result object has derived field baz set to 'SuperCallInEvalClass_ctor'");
  701. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  702. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  703. }
  704. },
  705. {
  706. name: "Derived class 'this' access inside an eval",
  707. body: function () {
  708. class SuperCallInEvalClass extends SimpleParent {
  709. constructor() {
  710. super();
  711. eval('this.bar = "SuperCallInEvalClass";');
  712. }
  713. };
  714. var result = new SuperCallInEvalClass();
  715. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  716. assert.areEqual('SuperCallInEvalClass', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass'");
  717. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  718. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  719. }
  720. },
  721. {
  722. name: "Derived class 'this' access inside and outside an eval",
  723. body: function () {
  724. class SuperCallInEvalClass extends SimpleParent {
  725. constructor() {
  726. super();
  727. eval('this.bar = "SuperCallInEvalClass";');
  728. this.baz = "SuperCallInEvalClass_ctor";
  729. }
  730. };
  731. var result = new SuperCallInEvalClass();
  732. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  733. assert.areEqual('SuperCallInEvalClass', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass'");
  734. assert.areEqual('SuperCallInEvalClass_ctor', result.baz, "Result object has derived field baz set to 'SuperCallInEvalClass_ctor'");
  735. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  736. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  737. }
  738. },
  739. {
  740. name: "Derived class super call inside an eval with an unused arrow super call",
  741. body: function () {
  742. class SuperCallInEvalClass extends SimpleParent {
  743. constructor() {
  744. let arrow = () => { super(); }
  745. eval('super(); this.bar = "SuperCallInEvalClass";');
  746. }
  747. };
  748. var result = new SuperCallInEvalClass();
  749. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  750. assert.areEqual('SuperCallInEvalClass', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass'");
  751. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  752. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  753. }
  754. },
  755. {
  756. name: "Derived class super call inside an arrow with this access in an eval",
  757. body: function () {
  758. class SuperCallInEvalClass extends SimpleParent {
  759. constructor() {
  760. let arrow = () => { super(); }
  761. arrow();
  762. eval('this.bar = "SuperCallInEvalClass";');
  763. }
  764. };
  765. var result = new SuperCallInEvalClass();
  766. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  767. assert.areEqual('SuperCallInEvalClass', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass'");
  768. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  769. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  770. }
  771. },
  772. {
  773. name: "Derived class super call inside an arrow accessing new.target with this access in an eval",
  774. body: function () {
  775. class SuperCallInEvalClass extends SimpleParent {
  776. constructor() {
  777. let arrow = () => {
  778. assert.areEqual(SuperCallInEvalClass, new.target, "SuperCallInEvalClass === new.target");
  779. super();
  780. }
  781. arrow();
  782. eval('this.bar = "SuperCallInEvalClass";');
  783. }
  784. };
  785. var result = new SuperCallInEvalClass();
  786. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  787. assert.areEqual('SuperCallInEvalClass', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass'");
  788. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  789. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  790. }
  791. },
  792. {
  793. name: "Derived class with arrow and eval accessing new.target, this - super in the arrow",
  794. body: function () {
  795. class SuperCallInEvalClass extends SimpleParent {
  796. constructor() {
  797. let arrow = () => {
  798. assert.areEqual(SuperCallInEvalClass, new.target, "SuperCallInEvalClass === new.target");
  799. super();
  800. this.baz = "SuperCallInEvalClass_arrow";
  801. }
  802. arrow();
  803. eval('assert.areEqual(SuperCallInEvalClass, new.target, "SuperCallInEvalClass === new.target"); this.bar = "SuperCallInEvalClass";');
  804. }
  805. };
  806. var result = new SuperCallInEvalClass();
  807. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  808. assert.areEqual('SuperCallInEvalClass', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass'");
  809. assert.areEqual('SuperCallInEvalClass_arrow', result.baz, "Result object has derived field baz set to 'SuperCallInEvalClass_arrow'");
  810. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  811. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  812. }
  813. },
  814. {
  815. name: "Derived class with arrow and eval accessing new.target, this - super in the eval",
  816. body: function () {
  817. class SuperCallInEvalClass extends SimpleParent {
  818. constructor() {
  819. let arrow = () => {
  820. assert.areEqual(SuperCallInEvalClass, new.target, "SuperCallInEvalClass === new.target");
  821. this.baz = "SuperCallInEvalClass_arrow";
  822. }
  823. eval('assert.areEqual(SuperCallInEvalClass, new.target, "SuperCallInEvalClass === new.target"); super(); this.bar = "SuperCallInEvalClass";');
  824. arrow();
  825. }
  826. };
  827. var result = new SuperCallInEvalClass();
  828. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  829. assert.areEqual('SuperCallInEvalClass', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass'");
  830. assert.areEqual('SuperCallInEvalClass_arrow', result.baz, "Result object has derived field baz set to 'SuperCallInEvalClass_arrow'");
  831. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  832. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  833. }
  834. },
  835. {
  836. name: "Derived class with arrow and multiple evals accessing new.target, this - super in the eval",
  837. body: function () {
  838. class SuperCallInEvalClass extends SimpleParent {
  839. constructor() {
  840. eval('assert.areEqual(SuperCallInEvalClass, new.target, "SuperCallInEvalClass === new.target");');
  841. let arrow = () => {
  842. assert.areEqual(SuperCallInEvalClass, new.target, "SuperCallInEvalClass === new.target");
  843. this.baz = "SuperCallInEvalClass_arrow";
  844. }
  845. eval('super();');
  846. arrow();
  847. eval('this.bar = "SuperCallInEvalClass";');
  848. }
  849. };
  850. var result = new SuperCallInEvalClass();
  851. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  852. assert.areEqual('SuperCallInEvalClass', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass'");
  853. assert.areEqual('SuperCallInEvalClass_arrow', result.baz, "Result object has derived field baz set to 'SuperCallInEvalClass_arrow'");
  854. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  855. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  856. }
  857. },
  858. {
  859. name: "Derived class testing all arrow/eval scenarios together",
  860. body: function () {
  861. class SuperCallInEvalClass extends SimpleParent {
  862. constructor(callSuperInCtor, callSuperInLambda, callSuperInEval) {
  863. assert.areEqual(SuperCallInEvalClass, new.target, "SuperCallInEvalClass === new.target");
  864. eval('assert.areEqual(SuperCallInEvalClass, new.target, "SuperCallInEvalClass === new.target");');
  865. let arrow_pre = () => {
  866. assert.areEqual(SuperCallInEvalClass, new.target, "SuperCallInEvalClass === new.target");
  867. }
  868. let arrow_post = () => {
  869. this.baz = "SuperCallInEvalClass_arrow";
  870. }
  871. let arrow_super = () => {
  872. super();
  873. }
  874. arrow_pre();
  875. if (callSuperInCtor) {
  876. super();
  877. }
  878. if (callSuperInLambda) {
  879. arrow_super();
  880. }
  881. if (callSuperInEval) {
  882. eval('super();');
  883. }
  884. arrow_post();
  885. eval('this.bar = "SuperCallInEvalClass_eval";');
  886. this.bot = 'SuperCallInEvalClass_ctor';
  887. }
  888. };
  889. function verifyObj(result) {
  890. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  891. assert.areEqual('SuperCallInEvalClass_eval', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass_eval'");
  892. assert.areEqual('SuperCallInEvalClass_arrow', result.baz, "Result object has derived field baz set to 'SuperCallInEvalClass_arrow'");
  893. assert.areEqual('SuperCallInEvalClass_ctor', result.bot, "Result object has derived field bot set to 'SuperCallInEvalClass_ctor'");
  894. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  895. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  896. }
  897. verifyObj(new SuperCallInEvalClass(true,false,false));
  898. verifyObj(new SuperCallInEvalClass(false,true,false));
  899. verifyObj(new SuperCallInEvalClass(false,false,true));
  900. assert.throws(function() { new SuperCallInEvalClass(false,false,false); }, ReferenceError, "Not calling super at all will cause a ReferenceError", "Use before declaration");
  901. assert.throws(function() { new SuperCallInEvalClass(true,true,false); }, ReferenceError, "Calling super in the ctor and a lambda throws a ReferenceError", "Multiple calls to 'super' in a class constructor are not allowed");
  902. assert.throws(function() { new SuperCallInEvalClass(true,false,true); }, ReferenceError, "Calling super in the ctor and eval throws a ReferenceError", "Multiple calls to 'super' in a class constructor are not allowed");
  903. assert.throws(function() { new SuperCallInEvalClass(false,true,true); }, ReferenceError, "Calling super in a lambda and eval throws a ReferenceError", "Multiple calls to 'super' in a class constructor are not allowed");
  904. }
  905. },
  906. {
  907. name: "Chained derived constructors performing super calls",
  908. body: function () {
  909. class DerivedClassUsingSuperInEval extends SimpleParent {
  910. constructor() {
  911. eval('super();');
  912. }
  913. };
  914. class DerivedClassUsingSuperInArrow extends DerivedClassUsingSuperInEval {
  915. constructor() {
  916. let arrow = () => { super(); }
  917. arrow();
  918. }
  919. };
  920. class DerivedClassUsingDefaultConstructor extends DerivedClassUsingSuperInArrow {
  921. }
  922. class BottomLevelDerivedClass extends DerivedClassUsingDefaultConstructor {
  923. constructor() {
  924. super();
  925. }
  926. };
  927. var result = new BottomLevelDerivedClass();
  928. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  929. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  930. assert.isTrue(result instanceof DerivedClassUsingSuperInEval, "Result object is instanceof derived class");
  931. assert.isTrue(result instanceof DerivedClassUsingSuperInArrow, "Result object is instanceof derived class");
  932. assert.isTrue(result instanceof DerivedClassUsingDefaultConstructor, "Result object is instanceof derived class");
  933. assert.isTrue(result instanceof BottomLevelDerivedClass, "Result object is instanceof derived class");
  934. }
  935. },
  936. {
  937. name: "Derived class with nested evals performing super",
  938. body: function () {
  939. class SuperCallInEvalClass extends SimpleParent {
  940. constructor() {
  941. var inner = "SuperCallInEvalClass_inner_eval";
  942. var outer = "SuperCallInEvalClass_outer_eval";
  943. eval('eval("super(); this.bar = inner;"); this.baz = outer;');
  944. }
  945. };
  946. var result = new SuperCallInEvalClass();
  947. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  948. assert.areEqual('SuperCallInEvalClass_inner_eval', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass_inner_eval'");
  949. assert.areEqual('SuperCallInEvalClass_outer_eval', result.baz, "Result object has derived field baz set to 'SuperCallInEvalClass_outer_eval'");
  950. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  951. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  952. }
  953. },
  954. {
  955. name: "Derived class with eval in arrow performing super",
  956. body: function () {
  957. class SuperCallInEvalClass extends SimpleParent {
  958. constructor() {
  959. var inner = "SuperCallInEvalClass_inner_eval";
  960. var outer = "SuperCallInEvalClass_outer_eval";
  961. var arrow = () => {
  962. eval("super(); this.bar = inner;");
  963. this.baz = outer;
  964. };
  965. arrow();
  966. }
  967. };
  968. var result = new SuperCallInEvalClass();
  969. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  970. assert.areEqual('SuperCallInEvalClass_inner_eval', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass_inner_eval'");
  971. assert.areEqual('SuperCallInEvalClass_outer_eval', result.baz, "Result object has derived field baz set to 'SuperCallInEvalClass_outer_eval'");
  972. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  973. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  974. }
  975. },
  976. {
  977. name: "Derived class with nested eval in arrow performing super",
  978. body: function () {
  979. class SuperCallInEvalClass extends SimpleParent {
  980. constructor() {
  981. var inner = "SuperCallInEvalClass_inner_eval";
  982. var outer = "SuperCallInEvalClass_outer_eval";
  983. var arrow = () => {
  984. eval(`eval("super(); this.bar = inner;");`);
  985. this.baz = outer;
  986. };
  987. arrow();
  988. }
  989. };
  990. var result = new SuperCallInEvalClass();
  991. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  992. assert.areEqual('SuperCallInEvalClass_inner_eval', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass_inner_eval'");
  993. assert.areEqual('SuperCallInEvalClass_outer_eval', result.baz, "Result object has derived field baz set to 'SuperCallInEvalClass_outer_eval'");
  994. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  995. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  996. }
  997. },
  998. {
  999. name: "Derived class with nested eval nested in arrow performing super",
  1000. body: function () {
  1001. class SuperCallInEvalClass extends SimpleParent {
  1002. constructor() {
  1003. var inner = "SuperCallInEvalClass_inner_eval";
  1004. var outer = "SuperCallInEvalClass_outer_eval";
  1005. var arrow = () => {
  1006. eval(`eval("super(); this.bar = inner;");`);
  1007. this.baz = outer;
  1008. };
  1009. var exec_arrow = () => {
  1010. arrow();
  1011. };
  1012. exec_arrow();
  1013. }
  1014. };
  1015. var result = new SuperCallInEvalClass();
  1016. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  1017. assert.areEqual('SuperCallInEvalClass_inner_eval', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass_inner_eval'");
  1018. assert.areEqual('SuperCallInEvalClass_outer_eval', result.baz, "Result object has derived field baz set to 'SuperCallInEvalClass_outer_eval'");
  1019. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  1020. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  1021. }
  1022. },
  1023. {
  1024. name: "Derived class with nested constructor calls and arrow performing super inside eval",
  1025. body: function () {
  1026. class B { }
  1027. class A extends B
  1028. {
  1029. constructor()
  1030. {
  1031. eval("(()=>{ super(); this.value = 1; class X extends Object { constructor() { eval(\"super(); this.value = 2; \"); } } var x = new X(); this.x = x;})()");
  1032. }
  1033. }
  1034. var a=new A();
  1035. assert.isTrue(a instanceof A, "object a should be instanceof derived class");
  1036. assert.isTrue(a instanceof B, "object should be instanceof base class");
  1037. assert.areEqual(1, a.value, "assignment to 'this' inside constructor eval nested arrow function");
  1038. assert.areEqual(2, a.x.value, "assignment to 'this' inside nested constructor and eval");
  1039. }
  1040. },
  1041. {
  1042. name: "Derived class with arrow in eval performing super",
  1043. body: function () {
  1044. class SuperCallInEvalClass extends SimpleParent {
  1045. constructor() {
  1046. var inner = "SuperCallInEvalClass_inner_eval";
  1047. var outer = "SuperCallInEvalClass_outer_eval";
  1048. eval('(()=>{super(); this.bar = inner;})(); this.baz = outer;');
  1049. }
  1050. };
  1051. var result = new SuperCallInEvalClass();
  1052. assert.areEqual('SimpleParent', result.foo, "Class derived from SimpleParent has foo field set to 'SimpleParent'");
  1053. assert.areEqual('SuperCallInEvalClass_inner_eval', result.bar, "Result object has derived field bar set to 'SuperCallInEvalClass_inner_eval'");
  1054. assert.areEqual('SuperCallInEvalClass_outer_eval', result.baz, "Result object has derived field baz set to 'SuperCallInEvalClass_outer_eval'");
  1055. assert.isTrue(result instanceof SimpleParent, "Result object is instanceof base class");
  1056. assert.isTrue(result instanceof SuperCallInEvalClass, "Result object is instanceof derived class");
  1057. }
  1058. },
  1059. {
  1060. name: "Eval class hierarchy with super call",
  1061. body: function () {
  1062. let result = eval(`
  1063. class EvalSimpleParent {
  1064. constructor() {
  1065. this.foo = "EvalSimpleParent";
  1066. }
  1067. };
  1068. class EvalDerivedClass extends EvalSimpleParent {
  1069. constructor() {
  1070. super();
  1071. this.bar = "EvalDerivedClass";
  1072. }
  1073. };
  1074. var o = new EvalDerivedClass();
  1075. assert.isTrue(o instanceof EvalSimpleParent, "Result object is instanceof base class");
  1076. assert.isTrue(o instanceof EvalDerivedClass, "Result object is instanceof derived class");
  1077. o;
  1078. `);
  1079. assert.areEqual('EvalSimpleParent', result.foo, "Class derived from EvalSimpleParent has foo field set to 'EvalSimpleParent'");
  1080. assert.areEqual('EvalDerivedClass', result.bar, "Result object has derived field bar set to 'EvalDerivedClass'");
  1081. }
  1082. },
  1083. {
  1084. name: "Eval class hierarchy with super call inside arrow function",
  1085. body: function () {
  1086. let result = eval(`
  1087. class EvalSimpleParent {
  1088. constructor() {
  1089. this.foo = "EvalSimpleParent";
  1090. }
  1091. };
  1092. class EvalDerivedClass extends EvalSimpleParent {
  1093. constructor() {
  1094. var inner = "EvalDerivedClass_inner";
  1095. var outer = "EvalDerivedClass_outer";
  1096. var arrow = () => {
  1097. super();
  1098. this.baz = inner;
  1099. };
  1100. arrow();
  1101. this.bar = outer;
  1102. }
  1103. };
  1104. var o = new EvalDerivedClass();
  1105. assert.isTrue(o instanceof EvalSimpleParent, "Result object is instanceof base class");
  1106. assert.isTrue(o instanceof EvalDerivedClass, "Result object is instanceof derived class");
  1107. o;
  1108. `);
  1109. assert.areEqual('EvalSimpleParent', result.foo, "Class derived from EvalSimpleParent has foo field set to 'EvalSimpleParent'");
  1110. assert.areEqual('EvalDerivedClass_outer', result.bar, "Result object has derived field bar set to 'EvalDerivedClass_outer'");
  1111. assert.areEqual('EvalDerivedClass_inner', result.baz, "Result object has derived field baz set to 'EvalDerivedClass_inner'");
  1112. }
  1113. },
  1114. {
  1115. name: "Eval class hierarchy with super call inside nested eval",
  1116. body: function () {
  1117. let result = eval(`
  1118. class EvalSimpleParent {
  1119. constructor() {
  1120. this.foo = "EvalSimpleParent";
  1121. }
  1122. };
  1123. class EvalDerivedClass extends EvalSimpleParent {
  1124. constructor() {
  1125. var inner = "EvalDerivedClass_inner";
  1126. var outer = "EvalDerivedClass_outer";
  1127. eval('super(); this.bar = inner;');
  1128. this.baz = outer;
  1129. }
  1130. };
  1131. var o = new EvalDerivedClass();
  1132. assert.isTrue(o instanceof EvalSimpleParent, "Result object is instanceof base class");
  1133. assert.isTrue(o instanceof EvalDerivedClass, "Result object is instanceof derived class");
  1134. o;
  1135. `);
  1136. assert.areEqual('EvalSimpleParent', result.foo, "Class derived from EvalSimpleParent has foo field set to 'EvalSimpleParent'");
  1137. assert.areEqual('EvalDerivedClass_inner', result.bar, "Result object has derived field bar set to 'EvalDerivedClass_inner'");
  1138. assert.areEqual('EvalDerivedClass_outer', result.baz, "Result object has derived field baz set to 'EvalDerivedClass_outer'");
  1139. }
  1140. },
  1141. {
  1142. name: "Eval class hierarchy with super call inside eval inside arrow function",
  1143. body: function () {
  1144. let result = eval(`
  1145. class EvalSimpleParent {
  1146. constructor() {
  1147. this.foo = "EvalSimpleParent";
  1148. }
  1149. };
  1150. class EvalDerivedClass extends EvalSimpleParent {
  1151. constructor() {
  1152. var moreinner = "EvalDerivedClass_moreinner";
  1153. var inner = "EvalDerivedClass_inner";
  1154. var outer = "EvalDerivedClass_outer";
  1155. var arrow = () => {
  1156. eval('super(); this.bat = moreinner;');
  1157. this.baz = inner;
  1158. };
  1159. arrow();
  1160. this.bar = outer;
  1161. }
  1162. };
  1163. var o = new EvalDerivedClass();
  1164. assert.isTrue(o instanceof EvalSimpleParent, "Result object is instanceof base class");
  1165. assert.isTrue(o instanceof EvalDerivedClass, "Result object is instanceof derived class");
  1166. o;
  1167. `);
  1168. assert.areEqual('EvalSimpleParent', result.foo, "Class derived from EvalSimpleParent has foo field set to 'EvalSimpleParent'");
  1169. assert.areEqual('EvalDerivedClass_outer', result.bar, "Result object has derived field bar set to 'EvalDerivedClass_outer'");
  1170. assert.areEqual('EvalDerivedClass_inner', result.baz, "Result object has derived field baz set to 'EvalDerivedClass_inner'");
  1171. assert.areEqual('EvalDerivedClass_moreinner', result.bat, "Result object has derived field bat set to 'EvalDerivedClass_moreinner'");
  1172. }
  1173. },
  1174. {
  1175. name: "ES5-style class as a base class - explicit return of this from base",
  1176. body: function () {
  1177. function Base() {
  1178. assert.areEqual(new.target, Derived, "new.target === Derived");
  1179. this.foo = "Base";
  1180. return this;
  1181. }
  1182. Base.prototype = {
  1183. constructor: Base
  1184. }
  1185. class Derived extends Base {
  1186. constructor() {
  1187. super();
  1188. this.bar = "Derived";
  1189. }
  1190. }
  1191. let result = new Derived();
  1192. assert.isTrue(result instanceof Base, "Result object is instanceof base class");
  1193. assert.isTrue(result instanceof Derived, "Result object is instanceof derived class");
  1194. assert.areEqual('Base', result.foo, "Class derived from Base has foo field set to 'Base'");
  1195. assert.areEqual('Derived', result.bar, "Result object has derived field bar set to 'Derived'");
  1196. }
  1197. },
  1198. {
  1199. name: "ES5-style class as a base class with the super call inside an eval - explicit return of this from base",
  1200. body: function () {
  1201. function Base() {
  1202. assert.areEqual(new.target, Derived, "new.target === Derived");
  1203. this.foo = "Base";
  1204. return this;
  1205. }
  1206. Base.prototype = {
  1207. constructor: Base
  1208. }
  1209. class Derived extends Base {
  1210. constructor() {
  1211. eval('super();');
  1212. this.bar = "Derived";
  1213. }
  1214. }
  1215. let result = new Derived();
  1216. assert.isTrue(result instanceof Base, "Result object is instanceof base class");
  1217. assert.isTrue(result instanceof Derived, "Result object is instanceof derived class");
  1218. assert.areEqual('Base', result.foo, "Class derived from Base has foo field set to 'Base'");
  1219. assert.areEqual('Derived', result.bar, "Result object has derived field bar set to 'Derived'");
  1220. }
  1221. },
  1222. {
  1223. name: "ES5-style class as a base class with the super call inside an arrow - explicit return of this from base",
  1224. body: function () {
  1225. function Base() {
  1226. assert.areEqual(new.target, Derived, "new.target === Derived");
  1227. this.foo = "Base";
  1228. return this;
  1229. }
  1230. Base.prototype = {
  1231. constructor: Base
  1232. }
  1233. class Derived extends Base {
  1234. constructor() {
  1235. let arrow = () => { super(); };
  1236. arrow();
  1237. this.bar = "Derived";
  1238. }
  1239. }
  1240. let result = new Derived();
  1241. assert.isTrue(result instanceof Base, "Result object is instanceof base class");
  1242. assert.isTrue(result instanceof Derived, "Result object is instanceof derived class");
  1243. assert.areEqual('Base', result.foo, "Class derived from Base has foo field set to 'Base'");
  1244. assert.areEqual('Derived', result.bar, "Result object has derived field bar set to 'Derived'");
  1245. }
  1246. },
  1247. {
  1248. name: "ES5-style class as a base class - implicit return of this from base",
  1249. body: function () {
  1250. function Base() {
  1251. assert.areEqual(new.target, Derived, "new.target === Derived");
  1252. this.foo = "Base";
  1253. }
  1254. Base.prototype = {
  1255. constructor: Base
  1256. }
  1257. class Derived extends Base {
  1258. constructor() {
  1259. super();
  1260. this.bar = "Derived";
  1261. }
  1262. }
  1263. let result = new Derived();
  1264. assert.isTrue(result instanceof Base, "Result object is instanceof base class");
  1265. assert.isTrue(result instanceof Derived, "Result object is instanceof derived class");
  1266. assert.areEqual('Base', result.foo, "Class derived from Base has foo field set to 'Base'");
  1267. assert.areEqual('Derived', result.bar, "Result object has derived field bar set to 'Derived'");
  1268. }
  1269. },
  1270. {
  1271. name: "ES5-style class as a base class with the super call inside an eval - implicit return of this from base",
  1272. body: function () {
  1273. function Base() {
  1274. assert.areEqual(new.target, Derived, "new.target === Derived");
  1275. this.foo = "Base";
  1276. }
  1277. Base.prototype = {
  1278. constructor: Base
  1279. }
  1280. class Derived extends Base {
  1281. constructor() {
  1282. eval('super();');
  1283. this.bar = "Derived";
  1284. }
  1285. }
  1286. let result = new Derived();
  1287. assert.isTrue(result instanceof Base, "Result object is instanceof base class");
  1288. assert.isTrue(result instanceof Derived, "Result object is instanceof derived class");
  1289. assert.areEqual('Base', result.foo, "Class derived from Base has foo field set to 'Base'");
  1290. assert.areEqual('Derived', result.bar, "Result object has derived field bar set to 'Derived'");
  1291. }
  1292. },
  1293. {
  1294. name: "ES5-style class as a base class with the super call inside an arrow - implicit return of this from base",
  1295. body: function () {
  1296. function Base() {
  1297. assert.areEqual(new.target, Derived, "new.target === Derived");
  1298. this.foo = "Base";
  1299. }
  1300. Base.prototype = {
  1301. constructor: Base
  1302. }
  1303. class Derived extends Base {
  1304. constructor() {
  1305. let arrow = () => { super(); };
  1306. arrow();
  1307. this.bar = "Derived";
  1308. }
  1309. }
  1310. let result = new Derived();
  1311. assert.isTrue(result instanceof Base, "Result object is instanceof base class");
  1312. assert.isTrue(result instanceof Derived, "Result object is instanceof derived class");
  1313. assert.areEqual('Base', result.foo, "Class derived from Base has foo field set to 'Base'");
  1314. assert.areEqual('Derived', result.bar, "Result object has derived field bar set to 'Derived'");
  1315. }
  1316. },
  1317. {
  1318. name: "Function as a base class can't assign this to non-object via super call",
  1319. body: function () {
  1320. function BaseReturnsArgument(arg) {
  1321. assert.areEqual(new.target, DerivedFromBase, "new.target === DerivedFromBase");
  1322. this.foo = 'BaseReturnsArgument';
  1323. return arg;
  1324. }
  1325. BaseReturnsArgument.prototype = {
  1326. constructor: BaseReturnsArgument
  1327. }
  1328. class DerivedFromBase extends BaseReturnsArgument {
  1329. constructor(arg) {
  1330. super(arg);
  1331. assert.isFalse(this === arg, "If base function returns non-object, 'this' is returned instead");
  1332. this.bar = 'DerivedFromBase';
  1333. }
  1334. }
  1335. function testDerivedClass(arg) {
  1336. let result = new DerivedFromBase(arg);
  1337. assert.isTrue(result instanceof BaseReturnsArgument, "Result object is instanceof base class");
  1338. assert.isTrue(result instanceof DerivedFromBase, "Result object is instanceof derived class");
  1339. assert.areEqual('BaseReturnsArgument', result.foo, "Class derived from Base has foo field set to 'Base'");
  1340. assert.areEqual('DerivedFromBase', result.bar, "Result object has derived field bar set to 'Derived'");
  1341. }
  1342. testDerivedClass();
  1343. testDerivedClass(undefined);
  1344. testDerivedClass(null);
  1345. testDerivedClass('string');
  1346. testDerivedClass(5);
  1347. testDerivedClass(Symbol());
  1348. }
  1349. },
  1350. {
  1351. name: "Function as a base class can return an object to override this",
  1352. body: function () {
  1353. function Base() {
  1354. assert.areEqual(new.target, Derived, "new.target === Derived");
  1355. this.foo = 'bad';
  1356. return { foo: 'Base' };
  1357. }
  1358. Base.prototype = {
  1359. constructor: Base
  1360. }
  1361. class Derived extends Base {
  1362. constructor() {
  1363. super();
  1364. this.bar = 'Derived';
  1365. }
  1366. }
  1367. let result = new Derived();
  1368. assert.isFalse(result instanceof Base, "Result object is instanceof base class");
  1369. assert.isFalse(result instanceof Derived, "Result object is instanceof derived class");
  1370. assert.areEqual('Base', result.foo, "Class derived from Base has foo field set to 'Base'");
  1371. assert.areEqual('Derived', result.bar, "Result object has derived field bar set to 'Derived'");
  1372. }
  1373. },
  1374. {
  1375. name: "Super call with expressions in arguments",
  1376. body: function () {
  1377. function Base() {
  1378. assert.areEqual(new.target, Derived, "new.target === Derived");
  1379. assert.areEqual(3, arguments[0], 'arguments[0] === 3')
  1380. assert.areEqual('str', arguments[1], 'arguments[1] === "str"')
  1381. assert.areEqual(1, arguments[2], 'arguments[2] === 1')
  1382. assert.areEqual(2, arguments[3], 'arguments[3] === 2')
  1383. assert.areEqual(3, arguments[4], 'arguments[4] === 3')
  1384. assert.areEqual(5, arguments.length, 'arguments.length === 5');
  1385. this.foo = 'Base';
  1386. }
  1387. Base.prototype = {
  1388. constructor: Base
  1389. }
  1390. function foo() { return 'str'; }
  1391. class Derived extends Base {
  1392. constructor() {
  1393. super(1+2,foo(),...[1,2,3]);
  1394. this.bar = 'Derived';
  1395. }
  1396. }
  1397. let result = new Derived();
  1398. assert.isTrue(result instanceof Base, "Result object is instanceof base class");
  1399. assert.isTrue(result instanceof Derived, "Result object is instanceof derived class");
  1400. assert.areEqual('Base', result.foo, "Class derived from Base has foo field set to 'Base'");
  1401. assert.areEqual('Derived', result.bar, "Result object has derived field bar set to 'Derived'");
  1402. }
  1403. },
  1404. ];
  1405. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });