default-splitscope.js 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584
  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 v15 = 1;
  7. var tests = [
  8. {
  9. name: "Split parameter scope in function definition",
  10. body: function () {
  11. function f1(a = 10, b = function () { return a; }) {
  12. assert.areEqual(10, a, "Initial value of parameter in the body scope should be the same as the one in param scope");
  13. var a = 20;
  14. assert.areEqual(20, a, "New assignment in the body scope updates the variable's value in body scope");
  15. return b;
  16. }
  17. assert.areEqual(10, f1()(), "Function defined in the param scope captures the formals from the param scope not body scope");
  18. function f2(a = 10, b = function () { return a; }, c = b() + a) {
  19. assert.areEqual(10, a, "Initial value of parameter in the body scope should be the same as the one in param scope");
  20. assert.areEqual(20, c, "Initial value of the third parameter in the body scope should be twice the value of the first parameter");
  21. var a = 20;
  22. assert.areEqual(20, a, "New assignment in the body scope updates the variable's value in body scope");
  23. return b;
  24. }
  25. assert.areEqual(10, f2()(), "Function defined in the param scope captures the formals from the param scope not body scope");
  26. function f3(a = 10, b = function () { return a; }) {
  27. assert.areEqual(1, a, "Initial value of parameter in the body scope should be the same as the one passed in");
  28. var a = 20;
  29. assert.areEqual(20, a, "Assignment in the body scope updates the variable's value in body scope");
  30. return b;
  31. }
  32. assert.areEqual(f3(1)(), 1, "Function defined in the param scope captures the formals from the param scope even when the default expression is not applied for that param");
  33. (function (a = 10, b = a += 10, c = function () { return a + b; }) {
  34. assert.areEqual(20, a, "Initial value of parameter in the body scope should be same as the corresponding symbol's final value in the param scope");
  35. var a2 = 40;
  36. (function () { assert.areEqual(40, a2, "Symbols defined in the body scope should be unaffected by the duplicate formal symbols"); })();
  37. assert.areEqual(40, c(), "Function defined in param scope uses the formals from param scope even when executed inside the body");
  38. })();
  39. (function (a = 10, b = function () { assert.areEqual(10, a, "Function defined in the param scope captures the formals from the param scope when executed from the param scope"); }, c = b()) {
  40. })();
  41. function f4(a = 10, b = function () { return function () { return a; }; }) {
  42. var a = 20;
  43. return b;
  44. }
  45. assert.areEqual(10, f4()()(), "Parameter scope works fine with nested functions");
  46. var a1 = 10;
  47. function f5(a, b = function () { a; return a1; }) {
  48. assert.areEqual(undefined, a1, "Inside the function body the assignment hasn't happened yet");
  49. var a1 = 20;
  50. assert.areEqual(20, a1, "Assignment to the symbol inside the function changes the value");
  51. return b;
  52. }
  53. assert.areEqual(10, f5()(), "Function in the param scope correctly binds to the outer variable");
  54. function f6(a = 10, b = { iFnc () { return a; } }) {
  55. var a = 20;
  56. return b;
  57. }
  58. assert.areEqual(10, f6().iFnc(), "Function definition inside the object literal should capture the formal from the param scope");
  59. function f7(a = 10, b = function () { return a; }) {
  60. assert.areEqual(10, a, "Initial value of parameter in the body scope should be the same as the one in param scope");
  61. a = 100;
  62. assert.areEqual(100, a, "New assignment in the body scope updates the value of the formal scope");
  63. return b;
  64. }
  65. assert.areEqual(100, f7()(), "Chnage in the value of the formal is reflected in the param scoped function's capture");
  66. var f8 = function (a, b = ((function() { assert.areEqual('string1', a, "First argument receives the right value"); })(), 1), c) {
  67. var d = 'string3';
  68. (function () { assert.areEqual('string3', d, "Var declaration in the body is initialized properly"); })();
  69. return c;
  70. };
  71. assert.areEqual('string2', f8('string1', undefined, 'string2'), "Function returns the third argument properly");
  72. function f9() {
  73. var f10 = function (a = function () { c; }, b, c) {
  74. assert.areEqual(1, c, "Third argument is properly populated");
  75. arguments;
  76. function f11() {};
  77. };
  78. f10(undefined, undefined, 1);
  79. }
  80. f9();
  81. f9();
  82. function f10() {
  83. var result = ((a = (w = a => a * a) => w) => a)()()(10);
  84. assert.areEqual(100, result, "The inner lambda function properly maps to the right symbol for a");
  85. };
  86. f10();
  87. function f11(a = 10, b = 20, c = () => [a, b]) {
  88. assert.areEqual(10, c()[0], "Before the assignment in the body the value of the symbol is the body is same as the formal formal");
  89. assert.areEqual(20, c()[1], "Before the assignment in the body updates the value of the formal is retained");
  90. var a = 20;
  91. b = 200;
  92. assert.areEqual(20, a, "New assignment in the body scope updates the variable's value in body scope");
  93. return c;
  94. }
  95. assert.areEqual(10, f11()()[0], "Assignment in the body does not affect the value of the formal");
  96. assert.areEqual(200, f11()()[1], "Assignment in the body updates the value of the formal");
  97. }
  98. },
  99. {
  100. name: "Split parameter scope and function expressions with name",
  101. body: function () {
  102. function f1(a = 10, b = function c() { return a; }) {
  103. assert.areEqual(10, a, "Initial value of parameter in the body scope of the method should be the same as the one in param scope");
  104. var a = 20;
  105. assert.areEqual(20, a, "New assignment in the body scope of the method updates the variable's value in body scope");
  106. return b;
  107. }
  108. assert.areEqual(10, f1()(), "Function expression defined in the param scope captures the formals from the param scope not body scope");
  109. function f2(a = 10, b = function c(recurse = true) { return recurse ? c(false) : a; }) {
  110. return b;
  111. }
  112. assert.areEqual(10, f2()(), "Recursive function expression defined in the param scope captures the formals from the param scope not body scope");
  113. var f3 = function f4 (a = function ( ) { b; return f4(20); }, b) {
  114. if (a == 20) {
  115. return 10;
  116. }
  117. return a;
  118. }
  119. assert.areEqual(10, f3()(), "Recursive call to the function from the param scope returns the right value");
  120. var f5 = function f6 (a = function ( ) { b; return f6; }, b) {
  121. if (a == 20) {
  122. return 10;
  123. }
  124. return a;
  125. }
  126. assert.areEqual(10, f5()()(20), "Recursive call to the function from the param scope returns the right value");
  127. var f7 = function f8 (a = function ( ) { b; }, b) {
  128. if (a == 20) {
  129. return 10;
  130. }
  131. var a = function () { return f8(20); };
  132. return a;
  133. }
  134. assert.areEqual(10, f7()(), "Recursive call to the function from the body scope returns the right value");
  135. var f9 = function f10 (a = function ( ) { b; return f10(20); }, b) {
  136. eval("");
  137. if (a == 20) {
  138. return 10;
  139. }
  140. return a;
  141. }
  142. assert.areEqual(10, f9()(), "Recursive call to the function from the param scope returns the right value when eval is there in the body");
  143. var f11 = function f12 (a = function ( ) { b; }, b) {
  144. eval("");
  145. if (a == 20) {
  146. return 10;
  147. }
  148. var a = function () { return f12(20); };
  149. return a;
  150. }
  151. assert.areEqual(10, f11()(), "Recursive call to the function from the body scope returns the right value when eval is there in the body");
  152. // [aneeshd] Commenting the test case to unblock the build
  153. // function f13() {
  154. // var a = function jnvgfg(sfgnmj = function ccunlk() { jnvgfg(undefined, 1); }, b) {
  155. // if (b) {
  156. // assert.areEqual(undefined, jnvgfg, "This refers to the instance in the body and the value of the function expression is not copied over");
  157. // }
  158. // var jnvgfg = 10;
  159. // if (!b) {
  160. // sfgnmj();
  161. // return 100;
  162. // }
  163. // };
  164. // assert.areEqual(100, a(), "After the recursion the right value is returned by the split scoped function");
  165. // };
  166. // f13();
  167. var f14 = function f15(a = (function() {
  168. return f15(1);
  169. })()) {
  170. with({}) {
  171. };
  172. return a === 1 ? 10 : a;
  173. };
  174. assert.areEqual(10, f14(), "Function expresison is captured in the param scope when no other formals are captured");
  175. }
  176. },
  177. {
  178. name: "Split parameter scope in member functions",
  179. body: function () {
  180. var o1 = {
  181. f(a = 10, b = function () { return a; }) {
  182. assert.areEqual(10, a, "Initial value of parameter in the body scope of the method should be the same as the one in param scope");
  183. var a = 20;
  184. assert.areEqual(20, a, "New assignment in the body scope of the method updates the variable's value in body scope");
  185. return b;
  186. }
  187. }
  188. assert.areEqual(o1.f()(), 10, "Function defined in the param scope of the object method captures the formals from the param scope not body scope");
  189. var o2 = {
  190. f1(a = 10, b = function () { return { f2 () { return a; } } }) {
  191. var a = 20;
  192. c = function () { return { f2 () { return a; } } };
  193. return [b, c];
  194. }
  195. }
  196. var result = o2.f1();
  197. assert.areEqual(10, result[0]().f2(), "Short hand method defined in the param scope of the object method captures the formals from the param scope not body scope");
  198. assert.areEqual(20, result[1]().f2(), "Short hand method defined in the param scope of the object method captures the formals from the param scope not body scope");
  199. var o3 = {
  200. f(a = 10, b = function () { return a; }) {
  201. assert.areEqual(10, a, "Initial value of the formal in the method should be the same as the one in param scope");
  202. a = 20;
  203. assert.areEqual(20, a, "New assignment in the body of the method updates the formal's value in body scope");
  204. return b;
  205. }
  206. }
  207. assert.areEqual(o3.f()(), 20, "Update to the value of the formal in the method's body is reflected in the capture");
  208. }
  209. },
  210. {
  211. name: "Arrow functions in split param scope",
  212. body: function () {
  213. function f1(a = 10, b = () => { return a; }) {
  214. assert.areEqual(10, a, "Initial value of parameter in the body scope should be the same as the one in param scope");
  215. var a = 20;
  216. assert.areEqual(20, a, "New assignment in the body scope updates the variable's value in body scope");
  217. return b;
  218. }
  219. assert.areEqual(10, f1()(), "Arrow functions defined in the param scope captures the formals from the param scope not body scope");
  220. function f2(a = 10, b = () => { return a; }) {
  221. assert.areEqual(1, a, "Initial value of parameter in the body scope should be the same as the one in param scope");
  222. var a = 20;
  223. assert.areEqual(20, a, "New assignment in the body scope updates the variable's value in body scope");
  224. return b;
  225. }
  226. assert.areEqual(1, f2(1)(), "Arrow functions defined in the param scope captures the formals from the param scope not body scope even when value is passed");
  227. function f3(a = 10, b = () => a) {
  228. assert.areEqual(10, a, "Initial value of parameter in the body scope should be the same as the one in param scope");
  229. var a = 20;
  230. assert.areEqual(20, a, "New assignment in the body scope updates the variable's value in body scope");
  231. return b;
  232. }
  233. assert.areEqual(10, f3()(), "Arrow functions with concise body defined in the param scope captures the formals from the param scope not body scope");
  234. ((a = 10, b = a += 10, c = () => { assert.areEqual(20, a, "Value of the first formal inside the lambda should be same as the default value"); return a + b; }, d = c() * 10) => {
  235. assert.areEqual(d, 400, "Initial value of the formal parameter inside the body should be the same as final value from the param scope");
  236. })();
  237. function f4(a = 10, b = () => { return () => a; }) {
  238. var a = 20;
  239. return b;
  240. }
  241. assert.areEqual(10, f4()()(), "Nested lambda should capture the formal param value from the param scope");
  242. assert.throws(function f5(a = () => x) { var x = 1; a(); }, ReferenceError, "Lambdas in param scope shouldn't be able to access the variables from body", "'x' is not defined");
  243. assert.throws(function f6() { (function (a = () => x) { var x = 1; return a; })()(); }, ReferenceError, "Lambdas in param scope shouldn't be able to access the variables from body", "'x' is not defined");
  244. assert.throws((a = () => 10, b = a() + c, c = 10) => {}, ReferenceError, "Formals defined to the right shouldn't be usable in lambdas", "Use before declaration");
  245. function f7(a = 10, b = () => { return a; }) {
  246. assert.areEqual(10, a, "Initial value of parameter in the body scope should be the same as the one in param scope");
  247. a = 20;
  248. assert.areEqual(20, a, "New assignment in the body scope updates the formal's value in body scope");
  249. return b;
  250. }
  251. assert.areEqual(20, f7()(), "Assignment in the function body is reflected in the arrow function's capture");
  252. }
  253. },
  254. {
  255. name: "Split parameter scope with Rest",
  256. body: function () {
  257. var arr = [2, 3, 4];
  258. function f1(a = 10, b = function () { return a; }, ...c) {
  259. assert.areEqual(arr.length, c.length, "Rest parameter should contain the same number of elements as the spread arg");
  260. for (i = 0; i < arr.length; i++) {
  261. assert.areEqual(arr[i], c[i], "Elements in the rest and the spread should be in the same order");
  262. }
  263. return b;
  264. }
  265. assert.areEqual(f1(undefined, undefined, ...arr)(), 10, "Presence of rest parameter shouldn't affect the binding");
  266. ((a = 10, b = () => a, ...c) => {
  267. assert.areEqual(arr.length, c.length, "Rest parameter should contain the same number of elements as the spread arg");
  268. for (i = 0; i < arr.length; i++) {
  269. assert.areEqual(arr[i], c[i], "Elements in the rest and the spread should be in the same order");
  270. }
  271. return b;
  272. })(undefined, undefined, ...arr);
  273. }
  274. },
  275. {
  276. name: "Split parameter scope with this",
  277. body: function () {
  278. function f1(a = this.x, b = function() { assert.areEqual(100, this.x, "this object for the function in param scope is passed from the final call site"); return a; }) {
  279. assert.areEqual(10, this.x, "this objects property retains the value from param scope");
  280. var a = 20;
  281. return b;
  282. }
  283. assert.areEqual(10, f1.call({x : 10}).call({x : 100}), "Arrow functions defined in the param scope captures the formals from the param scope not body scope");
  284. (function (a = this.x, b = function() {this.x = 20; return a;}) {
  285. assert.areEqual(10, this.x, "this objects property retains the value in param scope before the inner function call");
  286. b.call(this);
  287. assert.areEqual(20, this.x, "Update to a this's property from the param scope is reflected in the body scope");
  288. }).call({x : 10});
  289. this.x = 10;
  290. ((a = this.x, b = function() { a; this.x = 20; }) => {
  291. assert.areEqual(10, this.x, "this objects property retains the value in param scope before the inner function call in lambda");
  292. b.call(this);
  293. assert.areEqual(20, this.x, "Update to a this's property from the param scope of lambda function is reflected in the body scope");
  294. })();
  295. function f2(a = function() { return this.x; }, b = this.y, c = a.call({x : 20}) + b) {
  296. assert.areEqual(undefined, this.x, "this object remains unaffected");
  297. return c;
  298. }
  299. assert.areEqual(30, f2.call({y : 10}), "Properties are accessed from the right this object");
  300. var thisObj = {x : 1, y : 20 };
  301. function f3(a, b = () => { a; this.x = 10; return this.y; }) {
  302. assert.areEqual(1, this.x, "Assignment from the param scope has not happened yet");
  303. assert.areEqual(20, this.y, "y property of the this object is not affected");
  304. return b;
  305. }
  306. assert.areEqual(20, f3.call(thisObj)(), "Lambda defined in the param scope returns the right property value from thisObj");
  307. assert.areEqual(10, thisObj.x, "Assignment from the param scope method updates thisObj's property");
  308. function f4(a, b = () => { a; return this; }) {
  309. return b;
  310. }
  311. assert.areEqual(thisObj, f4.call(thisObj)(), "Lambda defined in the param scope returns the right this object");
  312. var thisObj = { x : 1 };
  313. function f5() {
  314. return (a = this, b = function() { return a; }) => b;
  315. }
  316. assert.areEqual(thisObj, f5.call(thisObj)()(), "This object is returned properly from the inner lambda method's child function");
  317. function f6(a, b = function () { return a; }) {
  318. return (a = this, b = function() { return a; }) => b;
  319. }
  320. assert.areEqual(thisObj, f6.call(thisObj)()(), "This object is returned properly from the inner lambda defnied inside a split scoped function");
  321. function f7(a, b = function () { return a; }) {
  322. function f8() {
  323. return (a = this, b = function() { return a; }) => b;
  324. }
  325. return f8.call(this);
  326. }
  327. assert.areEqual(thisObj, f7.call(thisObj)()(), "This object is returned properly from the inner lambda defnied inside a nested split scoped function");
  328. function f9(a, b = function () { return a; }) {
  329. function f10(c, d = function () { c; }) {
  330. return (a = this, b = function() { return a; }) => b;
  331. }
  332. return f10.call(this);
  333. }
  334. assert.areEqual(thisObj, f9.call(thisObj)()(), "This object is returned properly from the inner lambda defnied inside a double nested split scoped function");
  335. function f11(a = this.x * 10, b = () => { a; return this; }) {
  336. assert.areEqual(10, a, "this should be accessible in the parameter scope");
  337. assert.areEqual(thisObj, this, "Body scope should get the right value for this object");
  338. assert.isTrue(eval("thisObj == this"), "Eval should be able to access the this object properly");
  339. return b;
  340. }
  341. assert.areEqual(thisObj, f11.call(thisObj)(), "Lambda defined in the param scope returns the right this object");
  342. function f12(a = this.x * 10, b = () => { a; return this; }) {
  343. var c = 100;
  344. assert.areEqual(10, a, "this should be accessible in the parameter scope");
  345. assert.areEqual(thisObj, this, "Body scope should get the right value for this object");
  346. assert.isTrue(eval("thisObj == this"), "Eval should be able to access the this object properly");
  347. assert.areEqual(thisObj, (() => this)(), "Lambda should capture the this object from body properly");
  348. assert.areEqual(100, c, "Body variable should be unaffected by the slot allocation of this object");
  349. return b;
  350. }
  351. assert.areEqual(thisObj, f12.call(thisObj)(), "Lambda defined in the param scope returns the right this object");
  352. function f13(a = 10, b = () => { a; return this; }) {
  353. var c = 100;
  354. assert.areEqual(thisObj, this, "Body scope should get the right value for this object");
  355. var d = () => this;
  356. this.x = 5;
  357. assert.isTrue(eval("this.x == 5"), "Eval should be able to access the this object properly after the field is updated");
  358. assert.isTrue(eval("d().x == 5"), "Lambda should capture the this symbol from the body properly");
  359. assert.isTrue(eval("a == 10"), "Eval should be able to access the first parameter properly");
  360. assert.isTrue(eval("b().x == 5"), "Lambda from the param scope should capture the this symbol properly");
  361. assert.isTrue(eval("d().x == 5"), "Lambda should capture the this symbol from the body properly");
  362. return b;
  363. }
  364. assert.areEqual(5, f13.call(thisObj)().x, "Lambda defined in the param scope returns the same this object as the one in body");
  365. function f14(a = this.x, b = function() { assert.areEqual(100, this.x, "this object for the function in param scope is passed from the final call site"); return a; }) {
  366. assert.areEqual(10, this.x, "this objects property retains the value from param scope when body does not have any duplicate formals");
  367. a = 20;
  368. return b;
  369. }
  370. assert.areEqual(20, f14.call({x : 10}).call({x : 100}), "Update to the value of the formal in the body scope is reflected in the param scope function's capture");
  371. }
  372. },
  373. {
  374. name: "Split parameter scope and class",
  375. body: function () {
  376. class c1 {
  377. f(a = 10, d, b = function () { return a; }, c) {
  378. assert.areEqual(10, a, "Initial value of parameter in the body scope in class method should be the same as the one in param scope");
  379. var a = 20;
  380. assert.areEqual(20, a, "Assignment in the class method body updates the value of the variable");
  381. return b;
  382. }
  383. }
  384. assert.areEqual(10, (new c1()).f()(), "Method defined in the param scope of the class should capture the formal from the param scope itself");
  385. function f1(a = 10, d, b = class { method1() { return a; } }, c) {
  386. var a = 20;
  387. assert.areEqual(10, (new b()).method1(), "Class method defined within the param scope should capture the formal from the param scope");
  388. return b;
  389. }
  390. var result = f1();
  391. assert.areEqual(10, (new result()).method1(), "Methods defined in a class defined in param scope should capture the formals form that param scope itself");
  392. class c2 {
  393. f1(a = 10, d, b = function () { a = this.f2(); return a; }, c) {
  394. assert.areEqual(30, this.f2(), "this object in the body points to the right this object");
  395. return b;
  396. };
  397. f2() {
  398. return 30;
  399. }
  400. }
  401. var f2Obj = new c2();
  402. assert.areEqual(100, f2Obj.f1().call({f2() { return 100; }}), "Method defined in the param uses its own this object while updating the formal");
  403. function f2(a = 10, d, b = class { method1() { return class { method2() { return a; }} } }, c) {
  404. var a = 20;
  405. return b;
  406. }
  407. var obj1 = f2();
  408. var obj2 = (new obj1()).method1();
  409. assert.areEqual(10, (new obj2()).method2(), "Nested class definition in the param scope should capture the formals from the param scope");
  410. var actualArray = [2, 3, 4];
  411. class c3 {
  412. f(a = 10, b = () => { return c; }, ...c) {
  413. assert.areEqual(actualArray.length, c.length, "Rest param and the actual array should have the same length");
  414. for (var i = 0; i < c.length; i++) {
  415. assert.areEqual(actualArray[i], c[i], "Rest parameter should have the same value as the actual array");
  416. }
  417. var c = [];
  418. return b;
  419. }
  420. }
  421. result = (new c3()).f(undefined, undefined, ...[2, 3, 4])();
  422. assert.areEqual(actualArray.length, result.length, "The result and the actual array should have the same length");
  423. for (var i = 0; i < result.length; i++) {
  424. assert.areEqual(actualArray[i], result[i], "The result array should have the same value as the actual array");
  425. }
  426. class c4 {
  427. f({x:x = 10, y:y = () => { return x; }}) {
  428. assert.areEqual(10, x, "Initial value of destructure parameter in the body scope in class method should be the same as the one in param scope");
  429. var x = 20;
  430. assert.areEqual(20, x, "Assignment in the class method body updates the value of the variable");
  431. return y;
  432. }
  433. }
  434. assert.areEqual(10, (new c4()).f({})(), "The method defined as the default destructured value of the parameter should capture the formal from the param scope");
  435. function f3(a = 10, d, b = (function () { return a; }, class { method1() { return a; } }), c) {
  436. var a = 20;
  437. assert.areEqual(10, (new b()).method1(), "Class method defined within the param scope should capture the formal from the param scope");
  438. return b;
  439. }
  440. result = f3();
  441. assert.areEqual(10, (new result()).method1(), "Methods defined in a class defined, after another function definition, in the param scope should capture the formals form that param scope itself");
  442. function f4(a = 10, d, b = (function () { return a; }, class {}, class { method1() { return a; } }), c) {
  443. var a = 20;
  444. return b;
  445. }
  446. result = f4();
  447. assert.areEqual(10, (new result()).method1(), "Methods defined in a class defined, after another class definition, in the param scope should capture the formals form that param scope itself");
  448. function f5(a = 10, d, b = (function () { return a; }, class {}, function () {}, class { method1() { return a; } }), c) {
  449. var a = 20;
  450. return b;
  451. }
  452. result = f5();
  453. assert.areEqual(10, (new result()).method1(), "Methods defined in a class defined, after a function and class, in the param scope should capture the formals form that param scope itself");
  454. function f6(a = 10, d, b = (function () { return a; }, class {}, function (a, b = () => a) {}, class { method1() { return a; } }), c) {
  455. var a = 20;
  456. return b;
  457. }
  458. result = f6();
  459. assert.areEqual(10, (new result()).method1(), "Methods defined in a class defined, after a split scope function, in the param scope should capture the formals form that param scope itself");
  460. function f7(a = 10, d, b = (function () { return a; }, class c1 { method1() { return a; } }), c) {
  461. var a = 20;
  462. assert.areEqual(10, (new b()).method1(), "Class method defined within the param scope should capture the formal from the param scope");
  463. return b;
  464. }
  465. result = f7();
  466. assert.areEqual(10, (new result()).method1(), "Methods defined in a class with name defined, after another function definition, in the param scope should capture the formals form that param scope itself");
  467. function f8(a = 10, d, b = class c1 { method1() { return a; } }, c = (function () { return a; }, class c2 extends b { method2() { return a * a; } })) {
  468. var a = 20;
  469. assert.areEqual(10, (new b()).method1(), "Class method defined within the param scope should capture the formal from the param scope");
  470. return c;
  471. }
  472. result = f8();
  473. assert.areEqual(10, (new result()).method1(), "Methods defined in a class extending another class defined, after another function definition, in the param scope should capture the formals form that param scope itself");
  474. assert.areEqual(100, (new result()).method2(), "Method in the derived class returns the right value");
  475. class c5 {
  476. f(a = 10, d, b = function () { return a; }, c) {
  477. assert.areEqual(10, a, "Initial value of parameter in the body scope in class method should be the same as the one in param scope");
  478. a = 20;
  479. assert.areEqual(20, a, "Assignment in the class method body updates the value of the variable");
  480. return b;
  481. }
  482. }
  483. assert.areEqual(20, (new c5()).f()(), "Update to the value of the formal in the body is reflected in the param function's capture");
  484. class c6 {
  485. f(a = 10, b = () => { return c; }, ...c) {
  486. assert.areEqual(actualArray.length, c.length, "Rest param and the actual array should have the same length");
  487. for (var i = 0; i < c.length; i++) {
  488. assert.areEqual(actualArray[i], c[i], "Rest parameter should have the same value as the actual array");
  489. }
  490. c = [];
  491. return b;
  492. }
  493. }
  494. result = (new c6()).f(undefined, undefined, ...[2, 3, 4])();
  495. assert.areEqual(0, result.length, "Assignment to the rest formal in the body is reflected in the param scoped function's capture");
  496. }
  497. },
  498. {
  499. name: "Split parameter scope in generator methods",
  500. body: function () {
  501. function *f1(a = 10, d, b = function () { return a; }, c) {
  502. yield a;
  503. var a = 20;
  504. yield a;
  505. yield b;
  506. }
  507. var f1Obj = f1();
  508. assert.areEqual(10, f1Obj.next().value, "Initial value of the parameter in the body scope should be the same as the final value of the parameter in param scope");
  509. assert.areEqual(20, f1Obj.next().value, "Assignment in the body scope updates the variable's value");
  510. assert.areEqual(10, f1Obj.next().value(), "Function defined in the param scope captures the formal from the param scope itself");
  511. function *f2(a = 10, d, b = function () { return a; }, c) {
  512. yield a;
  513. a = 20;
  514. yield a;
  515. yield b;
  516. }
  517. var f2Obj = f2();
  518. assert.areEqual(10, f2Obj.next().value, "Initial value of the parameter in the body scope should be the same as the final value of the parameter in param scope");
  519. assert.areEqual(20, f2Obj.next().value, "Assignment in the body scope updates the variable's value");
  520. assert.areEqual(20, f2Obj.next().value(), "Function defined in the param scope captures the formal from the param scope which gets updated by the assignment in the body");
  521. function *f3(a = 10, d, b = function *() { yield a + c; }, c = 100) {
  522. var a = 20;
  523. yield a;
  524. yield b;
  525. }
  526. var f3Obj = f3();
  527. assert.areEqual(20, f3Obj.next().value, "Assignment in the body scope updates the variable's value");
  528. assert.areEqual(110, f3Obj.next().value().next().value, "Function defined in the param scope captures the formals from the param scope");
  529. function *f4(a = 10, d, b = function *() { yield a; }, c) {
  530. var a = 20;
  531. yield function *() { yield a; };
  532. yield b;
  533. }
  534. var f4Obj = f4();
  535. assert.areEqual(20, f4Obj.next().value().next().value, "Generator defined inside the body captures the symbol from the body scope");
  536. assert.areEqual(10, f4Obj.next().value().next().value, "Function defined in the param scope captures the formal from param scope even if it is captured in the body scope");
  537. }
  538. },
  539. {
  540. name: "Split parameter scope with destructuring",
  541. body: function () {
  542. function f1( {a:a1, b:b1}, c = function() { return a1 + b1; } ) {
  543. assert.areEqual(10, a1, "Initial value of the first destructuring parameter in the body scope should be the same as the one in param scope");
  544. assert.areEqual(20, b1, "Initial value of the second destructuring parameter in the body scope should be the same as the one in param scope");
  545. var a1 = 1;
  546. var b1 = 2;
  547. assert.areEqual(1, a1, "New assignment in the body scope updates the first formal's value in body scope");
  548. assert.areEqual(2, b1, "New assignment in the body scope updates the second formal's value in body scope");
  549. assert.areEqual(30, c(), "The param scope method should return the sum of the destructured formals from the param scope");
  550. return c;
  551. }
  552. assert.areEqual(30, f1({ a : 10, b : 20 })(), "Returned method should return the sum of the destructured formals from the param scope");
  553. function f2({x:x = 10, y:y = function () { return x; }}) {
  554. assert.areEqual(10, x, "Initial value of the first destructuring parameter in the body scope should be the same as the one in param scope");
  555. var x = 20;
  556. assert.areEqual(20, x, "Assignment in the body updates the formal's value");
  557. return y;
  558. }
  559. assert.areEqual(10, f2({ })(), "Returned method should return the value of the destructured formal from the param scope");
  560. function f3({y:y = function () { return x; }, x:x = 10}) {
  561. assert.areEqual(10, x, "Initial value of the first destructuring parameter in the body scope should be the same as the one in param scope");
  562. var x = 20;
  563. assert.areEqual(20, x, "Assignment in the body updates the formal's value");
  564. return y;
  565. }
  566. assert.areEqual(10, f3({ })(), "Returned method should return the value of the destructured formal from the param scope even if declared after");
  567. (({x:x = 10, y:y = function () { return x; }}) => {
  568. assert.areEqual(10, x, "Initial value of the first destructuring parameter in the body scope should be the same as the one in param scope");
  569. var x = 20;
  570. assert.areEqual(10, y(), "Assignment in the body does not affect the formal captured from the param scope");
  571. })({});
  572. function f4( {a:a1, b:b1}, c = function() { return a1 + b1; } ) {
  573. assert.areEqual(10, a1, "Initial value of the first destructuring parameter in the body scope should be the same as the one in param scope");
  574. assert.areEqual(20, b1, "Initial value of the second destructuring parameter in the body scope should be the same as the one in param scope");
  575. a1 = 1;
  576. b1 = 2;
  577. assert.areEqual(1, a1, "New assignment in the body scope updates the first formal's value in body scope");
  578. assert.areEqual(2, b1, "New assignment in the body scope updates the second formal's value in body scope");
  579. assert.areEqual(3, c(), "The param scope method should return the sum of the destructured formals from the param scope and reflect the value update in the body");
  580. return c;
  581. }
  582. assert.areEqual(3, f4({ a : 10, b : 20 })(), "Returned method should return the sum of the destructured formals which are updated in the body");
  583. }
  584. },
  585. {
  586. name: "Nested split scopes",
  587. body: function () {
  588. function f1(a = 10, b = function () { return a; }, c) {
  589. function iFnc(d = 100, e = 200, pf1 = function () { return d + e; }) {
  590. var d = 1000;
  591. var e = 2000;
  592. pf2 = function () { return d + e; };
  593. return [pf1, pf2];
  594. }
  595. return [b].concat(iFnc());
  596. }
  597. var result = f1();
  598. assert.areEqual(10, result[0](), "Function defined in the param scope of the outer function should capture the symbols from its own param scope");
  599. assert.areEqual(300, result[1](), "Function defined in the param scope of the inner function should capture the symbols from its own param scope");
  600. assert.areEqual(3000, result[2](), "Function defined in the body scope of the inner function should capture the symbols from its body scope");
  601. function f2(a = 10, b = function () { return a; }, c) {
  602. var a = 1000;
  603. var c = 2000;
  604. function iFnc(a = 100, b = function () { return a + c; }, c = 200) {
  605. var a = 1000;
  606. var c = 2000;
  607. return b;
  608. }
  609. return [b, iFnc()];
  610. }
  611. result = f2();
  612. assert.areEqual(10, result[0](), "Function defined in the param scope of the outer function should capture the symbols from its own param scope even if formals are with the same name in inner function");
  613. assert.areEqual(300, result[1](), "Function defined in the param scope of the inner function should capture the symbols from its own param scope if formals are with the same name in the outer function");
  614. function f3(a = 10, b = function () { return a; }, c) {
  615. a = 1000;
  616. c = 2000;
  617. function iFnc(a = 100, b = function () { return a + c; }, c = 200) {
  618. a = 1000;
  619. c = 2000;
  620. return b;
  621. }
  622. return [b, iFnc];
  623. }
  624. result = f3();
  625. assert.areEqual(1000, result[0](), "Assignments in the body of the outer function updates the formal's value");
  626. assert.areEqual(3000, result[1]()(), "Assignments in the body of the inner function updates the formal values");
  627. function f4(a = 10, b = function () { return a; }, c) {
  628. var a = 1000;
  629. function iFnc(a = 100, b = function () { return a + c; }, c = 200) {
  630. var a = 1000;
  631. var c = 2000;
  632. return b;
  633. }
  634. return [b, iFnc(undefined, b, c)];
  635. }
  636. result = f4(1, undefined, 3);
  637. assert.areEqual(1, result[0](), "Function defined in the param scope of the outer function correctly captures the passed in value for the formal");
  638. assert.areEqual(1, result[1](), "Function defined in the param scope of the inner function is replaced by the function definition from the param scope of the outer function");
  639. function f5(a = 10, b = function () { return a; }, c) {
  640. function iFnc(a = 100, b = function () { return a + c; }, c = 200) {
  641. var a = 1000;
  642. var c = 2000;
  643. return b;
  644. }
  645. return [b, iFnc(a, undefined, c)];
  646. }
  647. result = f5(1, undefined, 3);
  648. assert.areEqual(1, result[0](), "Function defined in the param scope of the outer function correctly captures the passed in value for the formal");
  649. assert.areEqual(4, result[1](), "Function defined in the param scope of the inner function captures the passed values for the formals");
  650. function f6(a , b, c) {
  651. function iFnc(a = 1, b = function () { return a + c; }, c = 2) {
  652. var a = 10;
  653. var c = 20;
  654. return b;
  655. }
  656. return iFnc;
  657. }
  658. assert.areEqual(3, f6()()(), "Function defined in the param scope captures the formals when defined inside another method without split scope");
  659. function f7(a = 10 , b = 20, c = function () { return a + b; }) {
  660. return (function () {
  661. function iFnc(a = 100, b = function () { return a + c; }, c = 200) {
  662. var a = 1000;
  663. var c = 2000;
  664. return b;
  665. }
  666. return [c, iFnc];
  667. })();
  668. }
  669. result = f7();
  670. assert.areEqual(30, result[0](), "Function defined in the param scope of the outer function should capture the symbols from its own param scope even in nested case");
  671. assert.areEqual(300, result[1]()(), "Function defined in the param scope of the inner function should capture the symbols from its own param scope even when nested inside a normal method and a split scope");
  672. function f8(a = 1, b = function (d = 10, e = function () { return a + d; }) { assert.areEqual(d, 10, "Split scope function defined in param scope should capture the right formal value"); d = 20; return e; }, c) {
  673. var a = 2;
  674. return b;
  675. }
  676. assert.areEqual(21, f8()()(), "Split scope function defined within the param scope should capture the formals from the corresponding param scopes");
  677. function f9(a = 1, b = function () { return function (d = 10, e = function () { return a + d; }) { d = 20; return e; } }, c) {
  678. var a = 2;
  679. return b;
  680. }
  681. assert.areEqual(21, f9()()()(), "Split scope function defined within the param scope should capture the formals from the corresponding param scope in nested scope");
  682. function f10(a = 10, b = () => eval("a"), c) {
  683. function iFnc(d = 100, e = 200, pf1 = function () { return d + e; }) {
  684. d = 1000;
  685. e = 2000;
  686. pf2 = function () { return d + e; };
  687. return [pf1, pf2];
  688. }
  689. return [b].concat(iFnc());
  690. }
  691. result = f10();
  692. assert.areEqual(10, result[0](), "Function with eval defined in the param scope of the outer function should capture the symbols from its own param scope");
  693. assert.areEqual(3000, result[1](), "Function defined in the param scope of the inner function should capture the symbols from its own param scope");
  694. assert.areEqual(3000, result[2](), "Function defined in the body scope of the inner function should capture the symbols from its body scope");
  695. function f11(a = 10, b = () => eval("a"), c) {
  696. function iFnc(d = 100, e = 200, pf1 = eval("d + e")) {
  697. var d = 1000;
  698. var e = 2000;
  699. pf2 = function () { return d + e; };
  700. return [pf1, pf2];
  701. }
  702. return [b].concat(iFnc());
  703. }
  704. result = f11();
  705. assert.areEqual(10, result[0](), "Function with eval defined in the param scope of the outer function should capture the symbols from its own param scope");
  706. assert.areEqual(300, result[1], "Second formal of the inner function should be able capture the symbols from its own param scope");
  707. assert.areEqual(3000, result[2](), "Function defined in the body scope of the inner function should capture the symbols from its body scope");
  708. function f12(a = 10, b = function () { return a; }, c) {
  709. function iFnc(d = 100, e = 200, pf1 = () => { return eval("d + e"); }) {
  710. var d = 1000;
  711. var e = 2000;
  712. pf2 = function () { return d + e; };
  713. return [pf1, pf2];
  714. }
  715. return [b].concat(iFnc());
  716. }
  717. result = f12();
  718. assert.areEqual(10, result[0](), "Function with eval defined in the param scope of the outer function should capture the symbols from its own param scope");
  719. assert.areEqual(300, result[1](), "Function with eval in the param defined in the param scope of the inner function should capture the symbols from its own param scope");
  720. assert.areEqual(3000, result[2](), "Function with eval in the param defined in the body scope of the inner function should capture the symbols from its body scope");
  721. function f13(a = 10, b = function () { return a; }, c) {
  722. function iFnc(d = 100, e = 200, pf1 = () => { return eval("d + e"); }) {
  723. d = 1000;
  724. var e = 2000;
  725. pf2 = function () { return d + e; };
  726. a = 100;
  727. return [pf1, pf2];
  728. }
  729. return [b].concat(iFnc());
  730. }
  731. result = f13();
  732. assert.areEqual(100, result[0](), "Function with eval defined in the param scope of the outer function should capture the symbols from its own param scope");
  733. assert.areEqual(1200, result[1](), "The first formal is updated in the function body but the second formal is duplicated");
  734. assert.areEqual(3000, result[2](), "Function with eval in the param defined in the body scope of the inner function should capture the symbols from its body scope and the param scope");
  735. }
  736. },
  737. {
  738. name: "Split scope with symbol shadowing",
  739. body: function () {
  740. function f1(a = 10, b = function () { return a; }) {
  741. assert.areEqual(100, a(), "Function definition inside the body is hoisted");
  742. function a () {
  743. return 100;
  744. }
  745. return b;
  746. }
  747. assert.areEqual(10, f1()(), "Function definition in the param scope captures the symbol from the param scope");
  748. function f2(a = 10, b = function () { return a; }, c = b) {
  749. var a = 20;
  750. assert.areEqual(20, b(), "Function definition in the body scope captures the body symbol");
  751. function b() {
  752. return a;
  753. }
  754. return [c, b];
  755. }
  756. var result = f2();
  757. assert.areEqual(10, result[0](), "Function definition in the param scope captures the param scope symbol");
  758. assert.areEqual(20, result[1](), "Function definition in the body captures the body symbol");
  759. var g = 1;
  760. function f3(a = 10, b = function () { a; return g;}) {
  761. assert.areEqual(10, g(), "Function definition inside the body is unaffected by the outer variable");
  762. function g() {
  763. return 10;
  764. }
  765. return b;
  766. }
  767. assert.areEqual(1, f3()(), "Function definition in the param scope captures the outer scoped var");
  768. function f4(a = x1, b = function g() {
  769. a;
  770. return function h() {
  771. assert.areEqual(10, x1, "x1 is captured from the outer scope");
  772. };
  773. }) {
  774. var x1 = 100;
  775. b()();
  776. };
  777. var x1 = 10;
  778. f4();
  779. var x2 = 1;
  780. function f5(a = x2, b = function() { a; return x2; }) {
  781. {
  782. function x2() {
  783. }
  784. }
  785. var x2 = 2;
  786. return b;
  787. }
  788. assert.areEqual(1, f5()(), "Symbol capture at the param scope is unaffected by the inner definitions");
  789. var x3 = 1;
  790. function f6(a = x3, b = function(_x) { a; return x3; }) {
  791. var x3 = 2;
  792. return b;
  793. }
  794. assert.areEqual(1, f6()(), "Symbol capture at the param scope is unaffected by other references in the body and param");
  795. function f7(a = 10, b = function () { return a; }, c) {
  796. assert.areEqual(100, a(), "Function definition inside the body is hoisted");
  797. function a () {
  798. return c;
  799. }
  800. return [b, c];
  801. }
  802. result = f7(undefined, undefined, 100);
  803. assert.areEqual(10, result[0](), "Function definition in the param scope captures the symbol from the param scope");
  804. assert.areEqual(100, result[1], "Function defined in the body overriding the formal should be able to capture another formal");
  805. function f8(a = 10, b = function () { return a; }, c) {
  806. var a = function a () {
  807. return c;
  808. }
  809. assert.areEqual(100, a(), "Function definition inside the body is hoisted");
  810. return [b, c];
  811. }
  812. result = f8(undefined, undefined, 100);
  813. assert.areEqual(10, result[0](), "Function definition in the param scope captures the symbol from the param scope");
  814. assert.areEqual(100, result[1], "Function expression with name defined in the body overriding the formal should be able to capture another formal");
  815. }
  816. },
  817. {
  818. name : "Split scope and arguments symbol in the body",
  819. body : function () {
  820. function f1(a, b = () => a) {
  821. eval("");
  822. b = () => { return arguments; };
  823. assert.areEqual(1, arguments[0], "Arguments object receives the first parameter properly");
  824. assert.areEqual(1, b()[0], "First argument receives the right value passed in");
  825. assert.areEqual(undefined, b()[1], "Second argument receives the right value passed in");
  826. assert.areEqual(2, arguments.length, "Arguments should have only two elements in it");
  827. }
  828. f1(1, undefined);
  829. function f2(a, b = () => { return a; }) {
  830. a = 10;
  831. assert.areEqual(1, arguments[0], "First argument is properly received");
  832. assert.areEqual(2, arguments[2], "Third argument is properly received");
  833. assert.areEqual(3, arguments.length, "Only three arguments are passed in");
  834. (() => { arguments = [3, 4]; a; })();
  835. assert.areEqual(3, arguments[0], "Arguments symbol is updated with the new value when the lambda is executed");
  836. assert.areEqual(4, arguments[1], "New array is properly assigned to arguments symbol");
  837. assert.areEqual(2, arguments.length, "New array has only elements");
  838. return b;
  839. }
  840. assert.areEqual(10, f2(1, undefined, 2)(), "Param scope method properly captures the first parameter");
  841. function f3(a, b = () => { return a; }) {
  842. eval("");
  843. var a = 10;
  844. assert.areEqual(1, arguments[0], "First argument is properly received");
  845. assert.areEqual(2, arguments[2], "Third argument is properly received");
  846. assert.areEqual(3, arguments.length, "Only three arguments are passed in");
  847. (() => { arguments = [3, 4]; a; })();
  848. assert.areEqual(3, arguments[0], "Arguments symbol is updated with the new value when the lambda is executed");
  849. assert.areEqual(4, arguments[1], "New array is properly assigned to arguments symbol");
  850. assert.areEqual(2, arguments.length, "New array has only elements");
  851. return b;
  852. }
  853. assert.areEqual(1, f3(1, undefined, 2)(), "Param scope method properly captures the first parameter, with eval in the body");
  854. function f4(a, b = function () { a; } ) {
  855. var c = 10;
  856. assert.areEqual(1, arguments[0], "Arguments symbol properly receives the passed in values");
  857. eval("");
  858. }
  859. f4(1);
  860. function f5(a, b = function () { a; } ) {
  861. var c = 10;
  862. assert.areEqual(1, arguments[0], "Arguments symbol properly receives the passed in values");
  863. arguments = 100;
  864. assert.areEqual(100, arguments, "Arguments is updated after the assignment");
  865. eval("");
  866. }
  867. f5(1);
  868. function f6(a, b = function () { a; } ) {
  869. assert.areEqual(1, arguments[0], "Arguments symbol properly receives the passed in values");
  870. arguments = 100;
  871. assert.areEqual(100, arguments, "Arguments is updated after the assignment");
  872. }
  873. f6(1);
  874. function f7(a, b = function () { a; } ) {
  875. assert.areEqual(5, arguments(), "Function definition is hoisted");
  876. function arguments() { return 5; }
  877. }
  878. f7(1);
  879. function f8(a, b = function () { a; } ) {
  880. assert.areEqual(5, arguments(), "Function definition is hoisted");
  881. function arguments() { return 5; }
  882. eval("");
  883. }
  884. f8(1);
  885. function f9(a, b = function () { a; } ) {
  886. assert.areEqual(1, eval("a"), "Eval should be able to access the first argument properly");
  887. assert.areEqual(1, eval("arguments[0]"), "Eval should be able to access the first argument properly from arguments object");
  888. assert.areEqual(1, arguments[0], "Arguments symbol properly receives the passed in values");
  889. arguments = 100;
  890. assert.areEqual(100, arguments, "Arguments is updated after the assignment");
  891. assert.areEqual(100, eval("arguments"), "Updated value of arguments is visible in eval");
  892. assert.areEqual(1, eval("a"), "First argument remains unchanged after the arguments are updated");
  893. }
  894. f9(1);
  895. function f10(a, b = function () { a; } ) {
  896. assert.areEqual(1, arguments[0], "Arguments symbol properly receives the passed in values");
  897. var arguments = 100;
  898. assert.areEqual(100, arguments, "Arguments is updated after the assignment");
  899. }
  900. f10(1);
  901. function f11(a, b = function () { a; } ) {
  902. assert.areEqual(1, arguments[0], "Arguments symbol properly receives the passed in values");
  903. var arguments = 100;
  904. assert.areEqual(100, arguments, "Arguments is updated after the assignment");
  905. eval("");
  906. }
  907. f11(1);
  908. function f12(a, b = function () { a; } ) {
  909. assert.areEqual(1, arguments[0], "Arguments symbol properly receives the passed in values");
  910. b = () => arguments;
  911. assert.areEqual(1, b()[0], "Lambda captures the right arguments symbol");
  912. var arguments = 100;
  913. assert.areEqual(100, arguments, "Arguments is updated after the assignment");
  914. assert.areEqual(100, b(), "Lambda now gives the updated value");
  915. eval("");
  916. }
  917. f12(1);
  918. function f13(a, b = () => { return a; }) {
  919. var a = 10;
  920. assert.areEqual(1, arguments[0], "First argument is properly received");
  921. assert.areEqual(2, arguments[2], "Third argument is properly received");
  922. assert.areEqual(3, arguments.length, "Only three arguments are passed in");
  923. ((c = arguments = [3, 4]) => { a; })();
  924. assert.areEqual(3, arguments[0], "Arguments symbol is updated with the new value when the lambda is executed");
  925. assert.areEqual(4, arguments[1], "New array is properly assigned to arguments symbol");
  926. assert.areEqual(2, arguments.length, "New array has only elements");
  927. return b;
  928. }
  929. assert.areEqual(1, f13(1, undefined, 2)(), "Param scope method properly captures the first parameter");
  930. function f14(a, b = () => { return a; }) {
  931. eval("");
  932. var a = 10;
  933. assert.areEqual(1, arguments[0], "First argument is properly received");
  934. assert.areEqual(2, arguments[2], "Third argument is properly received");
  935. assert.areEqual(3, arguments.length, "Only three arguments are passed in");
  936. ((c = arguments = [3, 4]) => { a; })();
  937. assert.areEqual(3, arguments[0], "Arguments symbol is updated with the new value when the lambda is executed");
  938. assert.areEqual(4, arguments[1], "New array is properly assigned to arguments symbol");
  939. assert.areEqual(2, arguments.length, "New array has only elements");
  940. return b;
  941. }
  942. assert.areEqual(1, f14(1, undefined, 2)(), "Param scope method properly captures the first parameter, with eval in the body");
  943. function f15(a, b = function () { a; }, ...c) {
  944. assert.areEqual(1, arguments[0], "Checking first argument");
  945. assert.areEqual(undefined, arguments[1], "Checking second argument");
  946. assert.areEqual(2, arguments[2], "Checking third argument");
  947. assert.areEqual(3, arguments[3], "Checking fourth argument");
  948. assert.areEqual([2, 3], c, "Rest argument should get the trailing parameters properly");
  949. var arguments = 100;
  950. assert.areEqual(100, arguments, "Arguments is updated after the assignment");
  951. assert.areEqual([2, 3], c, "Rest should remain unaffected when arguments is updated");
  952. eval("");
  953. }
  954. f15(1, undefined, 2, 3);
  955. var f16 = function f17(a, b = function () { a; }, ...c) {
  956. if (a === 1) {
  957. assert.areEqual(1, arguments[0], "Checking first argument");
  958. assert.areEqual(undefined, arguments[1], "Checking second argument");
  959. assert.areEqual(2, arguments[2], "Checking third argument");
  960. assert.areEqual(3, arguments[3], "Checking fourth argument");
  961. assert.areEqual([2, 3], c, "Rest argument should get the trailing parameters properly");
  962. return f17(undefined, undefined, ...c);
  963. } else {
  964. assert.areEqual(undefined, arguments[0], "Checking first argument on the recursive call");
  965. assert.areEqual(undefined, arguments[1], "Checking second argument on the recursive call");
  966. assert.areEqual(2, arguments[2], "Checking third argument on the recursive call");
  967. assert.areEqual(3, arguments[3], "Checking fourth argument on the recursive call");
  968. assert.areEqual([2, 3], c, "Rest argument should get the trailing parameters properly");
  969. var arguments = 100;
  970. assert.areEqual(100, arguments, "Arguments is updated after the assignment");
  971. assert.areEqual([2, 3], c, "Rest should remain unaffected when arguments is updated");
  972. return eval("c");
  973. }
  974. }
  975. assert.areEqual([2, 3], f16(1, undefined, 2, 3), "Rest should remain unaffected when arguments is updated");
  976. function f18(a, b = () => { return a; }) {
  977. assert.areEqual(1, arguments[0], "First argument is properly received");
  978. assert.areEqual(2, arguments[2], "Third argument is properly received");
  979. assert.areEqual(3, arguments.length, "Only three arguments are passed in");
  980. assert.areEqual(1, ((c = arguments = [3, 4]) => { return a; })(), "Function defined in the body is able to capture a formal from the param scope");
  981. assert.areEqual(3, arguments[0], "Arguments symbol is updated with the new value when the lambda is executed");
  982. assert.areEqual(4, arguments[1], "New array is properly assigned to arguments symbol");
  983. assert.areEqual(2, arguments.length, "New array has only elements");
  984. return b;
  985. }
  986. assert.areEqual(1, f18(1, undefined, 2)(), "Param scope method properly captures the first parameter");
  987. function f19(a, b = () => { return a; }) {
  988. eval("");
  989. a = 10;
  990. assert.areEqual(1, arguments[0], "First argument is properly received");
  991. assert.areEqual(2, arguments[2], "Third argument is properly received");
  992. assert.areEqual(3, arguments.length, "Only three arguments are passed in");
  993. assert.areEqual(10, ((c = arguments = [3, 4]) => { return a; })(), "Function defined in the body is able to capture a formal from the param scope");
  994. assert.areEqual(3, arguments[0], "Arguments symbol is updated with the new value when the lambda is executed");
  995. assert.areEqual(4, arguments[1], "New array is properly assigned to arguments symbol");
  996. assert.areEqual(2, arguments.length, "New array has only elements");
  997. return b;
  998. }
  999. assert.areEqual(10, f19(1, undefined, 2)(), "Param scope method properly captures the first parameter, with eval in the body, and reflects the value update from the body");
  1000. function f20(a, b = () => { return a; }) {
  1001. eval("");
  1002. var a = 10;
  1003. assert.areEqual(1, arguments[0], "First argument is properly received");
  1004. assert.areEqual(2, arguments[2], "Third argument is properly received");
  1005. assert.areEqual(3, arguments.length, "Only three arguments are passed in");
  1006. assert.areEqual(10, ((c = arguments = [3, 4]) => { return a; })(), "Function defined in the body is able to capture a formal from the param scope");
  1007. assert.areEqual(3, arguments[0], "Arguments symbol is updated with the new value when the lambda is executed");
  1008. assert.areEqual(4, arguments[1], "New array is properly assigned to arguments symbol");
  1009. assert.areEqual(2, arguments.length, "New array has only elements");
  1010. return b;
  1011. }
  1012. assert.areEqual(1, f20(1, undefined, 2)(), "Param scope method properly captures the first parameter, with eval in the body");
  1013. function f21(a, b = function arguments(c) {
  1014. if (!c) {
  1015. return arguments.callee(a, 10, 20);
  1016. }
  1017. return arguments;
  1018. }) {
  1019. assert.areEqual(10, b()[1], "Function defined in the param scope can be called recursively");
  1020. assert.areEqual(1, arguments[0], "Arguments symbol is unaffected by the function expression");
  1021. }
  1022. f21(1);
  1023. function f22(a, b = arguments) {
  1024. var c = function arguments(c) {
  1025. if (!arguments.length) {
  1026. return arguments.callee(a, 10, 20, 30);
  1027. }
  1028. return arguments;
  1029. }
  1030. assert.areEqual(30, c()[3], "In the function body the arguments function expression with name is not visible");
  1031. assert.areEqual(1, b[0], "In the param scope arguments symbol referes to the passed in values");
  1032. }
  1033. f22(1, undefined, 2, 3, 4);
  1034. function f23(a, b = function arguments(c) {
  1035. if (!c) {
  1036. return arguments.callee(a, 10, 20);
  1037. }
  1038. return eval("arguments");
  1039. }) {
  1040. assert.areEqual(1, b()[0], "Function defined in the param scope can be called recursively when eval occurs in its body");
  1041. assert.areEqual(1, arguments[0], "Arguments symbol is unaffected by the function expression");
  1042. }
  1043. f23(1);
  1044. function f24(a, b = arguments) {
  1045. var c = function arguments(c) {
  1046. if (!arguments.length) {
  1047. return arguments.callee(a, 10, 20, 30);
  1048. }
  1049. return arguments;
  1050. }
  1051. assert.areEqual(30, c()[3], "In the function body the arguments function expression with name is not visible when eval is there in the body");
  1052. assert.areEqual(3, eval("b[3]"), "In the param scope arguments symbol referes to the passed in values");
  1053. }
  1054. f24(1, undefined, 2, 3, 4);
  1055. function f25(a, b = () => a) {
  1056. assert.areEqual(1, arguments[0], "Function in block causes a var declaration to be hoisted and the initial value should be same as the arguments symbol");
  1057. {
  1058. {
  1059. function arguments() {
  1060. return 10;
  1061. }
  1062. }
  1063. }
  1064. assert.areEqual(1, b(), "Function defined in the param scope should be able to capture the formal even when arguments in overwritten the body");
  1065. assert.areEqual(10, arguments(), "Hoisted var binding is updated after the block is exected");
  1066. }
  1067. f25(1);
  1068. function f26(a, b = () => a) {
  1069. function f27() {
  1070. eval("");
  1071. this.arguments = 1;
  1072. }
  1073. var a = 10;
  1074. var obj = new f27();
  1075. function arguments() {
  1076. return 10;
  1077. }
  1078. assert.areEqual(1, obj.arguments, "Inner function with eval should add the property named arguments when duplicate arguments definition occurs in the parent body");
  1079. assert.areEqual(1, b(), "Formal captured from the param scope should be constrained to the param scope");
  1080. };
  1081. f26(1);
  1082. function f27(a, b = () => a) {
  1083. let arguments = 10;
  1084. assert.areEqual(10, arguments, "Body should use the let declaration");
  1085. assert.areEqual(1, b(), "Function in the param scope should capture the formal");
  1086. }
  1087. f27(1);
  1088. function f28(a, b = () => a) {
  1089. let arguments = 10;
  1090. assert.areEqual(10, eval("arguments"), "Body should use the let declaration when eval is present");
  1091. assert.areEqual(1, b(), "Function in the param scope should capture the formal");
  1092. }
  1093. f28(1);
  1094. function f29(a, b = () => arguments) {
  1095. let arguments = 10;
  1096. assert.areEqual(10, arguments, "Body should use the let declaration");
  1097. assert.areEqual(1, b()[0], "Function in the param scope should capture the arguments symbol from the param scope");
  1098. }
  1099. f29(1);
  1100. function f30(a, b = () => arguments) {
  1101. let arguments = 10;
  1102. assert.areEqual(10, eval("arguments"), "Body should use the let declaration when eval is present");
  1103. assert.areEqual(1, b()[0], "Function in the param scope should capture the arguments symbol from the param scope when eval is present");
  1104. }
  1105. f30(1);
  1106. }
  1107. },
  1108. {
  1109. name: "Split scope and arguments symbol in the param scope",
  1110. body: function () {
  1111. function f1(a = arguments[1], b, c = () => a) {
  1112. assert.areEqual(1, c(), "Arguments symbol is accessible in the param scope");
  1113. }
  1114. f1(undefined, 1);
  1115. function f2(a, b = () => a + arguments[0]) {
  1116. assert.areEqual(2, b(), "An arrow function in the param scope can capture the Arguments symbol");
  1117. }
  1118. f2(1);
  1119. function f3(a, b = () => () => a + arguments[0]) {
  1120. assert.areEqual(2, b()(), "A nested arrow function in the param scope can capture the Arguments symbol");
  1121. }
  1122. f3(1);
  1123. function f4() {
  1124. (function (a = arguments[1], b, c = () => a) {
  1125. assert.areEqual(1, c(), "Arguments symbol is accessible in the param scope of a nested function");
  1126. })(undefined, 1);
  1127. }
  1128. f4();
  1129. function f5(a = arguments[1], b, c = () => a) {
  1130. assert.areEqual(1, c(), "Arguments symbol is accessible in the param scope");
  1131. assert.areEqual(1, arguments[1], "Arguments access in the body works fine when arguments is accessed in the param scope also");
  1132. }
  1133. f5(undefined, 1);
  1134. function f6(a = arguments[2], b = eval("arguments")) {
  1135. assert.areEqual(1, a, "Arguments should be accessible in the param scope when eval is present in the param scope")
  1136. assert.areEqual(1, b[2], "Eval in param scope is able to access arguments");
  1137. }
  1138. f6(undefined, undefined, 1);
  1139. function f7(a = arguments[2], b = () => a) {
  1140. assert.areEqual(1, a, "Arguments should be accessible in the param scope when eval is present in the param scope")
  1141. assert.areEqual(1, b(), "Function in the param scope should be able to access the formal properly");
  1142. assert.areEqual(1, eval("arguments[2]"), "Eval in body should be able to access arguments properly");
  1143. }
  1144. f7(undefined, undefined, 1);
  1145. function f8(a, b = function() { return eval("arguments"); }) {
  1146. assert.areEqual(1, b(a)[0], "Eval inside a function in the param scope should be able to access the formal properly");
  1147. }
  1148. f8(1);
  1149. function f9(x, a = arguments, b = arguments = [10, 20], c = () => b) {
  1150. assert.areEqual(1, a[0], "First formal initializer accesses the arguments before assignment");
  1151. assert.areEqual(10, c()[0], "Formal captured correctly reflects the updated arguments value");
  1152. assert.areEqual(10, arguments[0], "Arguments value is updated when the second formal is evaluated");
  1153. }
  1154. f9(1);
  1155. function f10(x, a = arguments = [10, 20], b = () => { assert.areEqual(10, a[0], "Formals is assigned the new array"); return arguments; }) {
  1156. assert.areEqual(10, b()[0], "Arguments captured in the param scope reflects the assignment");
  1157. assert.areEqual(20, b()[1], "Arguments captured in the param scope reflects the assignment");
  1158. assert.areEqual(10, arguments[0], "Arguments value is updated when the formal is evaluated");
  1159. assert.areEqual(20, arguments[1], "Arguments value is updated when the formal is evaluated");
  1160. }
  1161. f10(1);
  1162. function f11(a, b = 10, c = () => {
  1163. assert.areEqual(10, b, "Second formal has the value from initializer");
  1164. assert.areEqual(1, arguments[0], "Arguments is not updated yet");
  1165. arguments = [100, 200, 300, 400, 500];
  1166. }) {
  1167. assert.areEqual(1, arguments[0], "In the body arguments initial value is the same as the one from param scope");
  1168. c();
  1169. assert.areEqual(500, arguments[4], "Arguments value is updated in the param scope and that reflects in the body scope");
  1170. }
  1171. f11(1);
  1172. function f12(a, b = 10, c = () => {
  1173. assert.areEqual(1, eval("arguments[0]"), "Arguments is not updated yet");
  1174. arguments = [100, 200, 300, 400, 500];
  1175. }) {
  1176. assert.areEqual(1, arguments[0], "In the body arguments initial value is the same as the one from param scope");
  1177. c();
  1178. assert.areEqual(500, arguments[4], "With eval in the param scope arguments value is updated in the param scope and that reflects in the body scope");
  1179. }
  1180. f12(1);
  1181. function f13(a, b = 10, c = () => {
  1182. assert.areEqual(10, b, "Second formal has the value from initializer");
  1183. return arguments;
  1184. }) {
  1185. assert.areEqual(1, arguments[0], "In the body arguments initial value is the same as the one from param scope");
  1186. assert.areEqual(1, c()[0], "Param scope captures the arguments symbol properly");
  1187. arguments = 100;
  1188. assert.areEqual(100, c(), "Update to the arguments symbol's value is reflected by the param scope function too");
  1189. assert.areEqual(100, arguments, "Update to the arguments symbol's value is reflected in the body too");
  1190. }
  1191. f13(1);
  1192. function f14(a, b = 10, c = () => {
  1193. assert.areEqual(10, eval("b"), "Second formal has the value from initializer");
  1194. return arguments;
  1195. }) {
  1196. assert.areEqual(1, arguments[0], "In the body arguments initial value is the same as the one from param scope");
  1197. assert.areEqual(1, c()[0], "Param scope captures the arguments symbol properly");
  1198. arguments = 100;
  1199. assert.areEqual(100, c(), "With eval in the param scope update to the arguments symbol's value is reflected by the param scope function too");
  1200. assert.areEqual(100, arguments, "With eval in the param scope update to the arguments symbol's value is reflected in the body too");
  1201. }
  1202. f14(1);
  1203. function f15(a, b = 10, c = () => {
  1204. assert.areEqual(10, b, "Second formal has the value from initializer");
  1205. return arguments;
  1206. }) {
  1207. assert.areEqual(1, arguments[0], "In the body arguments initial value is the same as the one from param scope");
  1208. assert.areEqual(1, c()[0], "Param scope captures the arguments symbol properly");
  1209. arguments = 100;
  1210. assert.areEqual(100, eval("c()"), "With eval in the body scope update to the arguments symbol's value is reflected by the param scope function too");
  1211. assert.areEqual(100, arguments, "With eval in the body scope update to the arguments symbol's value is reflected in the body too");
  1212. }
  1213. f15(1);
  1214. function f16(a, b = 10, c = () => {
  1215. assert.areEqual(10, b, "Second formal has the value from initializer");
  1216. return arguments;
  1217. }) {
  1218. d = () => arguments;
  1219. assert.areEqual(1, d()[0], "In the body arguments initial value is the same as the one from param scope");
  1220. assert.areEqual(1, c()[0], "Param scope captures the arguments symbol properly");
  1221. arguments = 100;
  1222. assert.areEqual(100, c(), "Update to the arguments symbol's value is reflected by the param scope function too");
  1223. assert.areEqual(100, d(), "Update to the arguments symbol's value is reflected in functions defined in the body too");
  1224. }
  1225. f16(1);
  1226. function f17(a, b = () => a, c = (x = arguments = [10, 20]) => x) {
  1227. d = () => arguments;
  1228. assert.areEqual(1, d()[0], "Arguments initial value is properly reflected in the body");
  1229. assert.areEqual(1, b(), "Formal captured by lambda should have the right value");
  1230. assert.areEqual(10, c()[0], "Lambda captures the first argument of the lambda");
  1231. assert.areEqual(20, d()[1], "Arguments value is updated when the lambda from param scope is executed");
  1232. assert.areEqual(1, b(), "Value of the formal is not affected");
  1233. }
  1234. f17(1);
  1235. function f18(a, b = () => a, c = (x = 10, y = () => x + (arguments = [10, 20])[1]) => y) {
  1236. d = (x = arguments, y = () => x) => y;
  1237. assert.areEqual(1, d()()[0], "Arguments initial value is properly reflected in the body");
  1238. assert.areEqual(1, b(), "Formal captured by lambda should have the right value");
  1239. assert.areEqual(30, c()(), "Split scoped lambda captures and updates the arguments symbol");
  1240. assert.areEqual(20, d()()[1], "Arguments value is updated when the lambda from param scope is executed");
  1241. assert.areEqual(1, b(), "Value of the formal is not affected");
  1242. }
  1243. f18(1);
  1244. function f19({a = arguments[1]}, b, c = () => a) {
  1245. assert.areEqual(1, c(), "Arguments symbol is accessible in a destructured pattern in the param scope");
  1246. }
  1247. f19({}, 1);
  1248. function f20({a = arguments[1], b = () => a + arguments[2]}, c, ...d) {
  1249. assert.areEqual(3, b(), "Arguments symbol is accessible in a destructured pattern in the param scope");
  1250. assert.areEqual("2,3,4", d.toString(), "Rest parameter gets the right value when arguments is captured");
  1251. }
  1252. f20({}, 1, 2, 3, 4);
  1253. function f21(a, b = () => arguments) {
  1254. function arguments() {
  1255. return 100;
  1256. }
  1257. assert.areEqual(10, b()[0], "Param scope function captures the arguments symbol");
  1258. assert.areEqual(100, arguments(), "In the body arguments in the newly defined function");
  1259. }
  1260. f21(10);
  1261. }
  1262. },
  1263. {
  1264. name: "Splits scope with formals named arguments",
  1265. body: function () {
  1266. function f1(a = 10, arguments = () => a) {
  1267. assert.areEqual(10, arguments(), "Arguments is a function defined in the param scope");
  1268. }
  1269. f1(undefined, undefined, 1, 2, 3);
  1270. function f2(arguments = 10, a = () => arguments) {
  1271. assert.areEqual(10, a(), "Function defined in the param scope captures the formal named arguments");
  1272. }
  1273. f2(undefined, undefined, 1, 2, 3);
  1274. function f3(arguments = 10, a = () => arguments) {
  1275. assert.areEqual(10, eval("a()"), "Function defined in the param scope captures the formal named arguments when eval is present in the body");
  1276. }
  1277. f3(undefined, undefined, 1, 2, 3);
  1278. function f4(arguments = 10, a = () => eval("arguments")) {
  1279. assert.areEqual(10, a(), "Function defined in the param scope captures the formal named arguments when eval is present in its body");
  1280. }
  1281. f4(undefined, undefined, 1, 2, 3);
  1282. function f5(arguments = 10, a = () => arguments) {
  1283. function arguments() {
  1284. return 100;
  1285. }
  1286. assert.areEqual(10, a(), "Param scope function properly captures the formal");
  1287. assert.areEqual(100, arguments(), "In the body arguments in the newly defined function");
  1288. }
  1289. f5();
  1290. function f6(arguments = 10, a = arguments, b = () => arguments + eval("a")) {
  1291. assert.areEqual(100, arguments(), "In the body arguments in the newly defined function");
  1292. function arguments() {
  1293. return 100;
  1294. }
  1295. assert.areEqual(10, a, "Second formals accesses the arguments symbol from param scope");
  1296. assert.areEqual(20, b(), "Function defined in the param scope can access arguments and second formal");
  1297. }
  1298. f6();
  1299. function f7(arguments = 10, a = () => arguments) {
  1300. function arguments() {
  1301. return 100;
  1302. }
  1303. assert.areEqual(10, a(), "Param scope function properly captures the formal");
  1304. assert.areEqual(100, eval("arguments()"), "In the body arguments in the newly defined function");
  1305. }
  1306. f7();
  1307. function f8(arguments = 10, a = () => arguments) {
  1308. assert.areEqual(10, arguments, "In the body arguments has the initial value from the param scope");
  1309. var arguments= 100;
  1310. assert.areEqual(10, a(), "Param scope function properly captures the formal");
  1311. assert.areEqual(100, arguments, "In the body arguments in the newly defined var");
  1312. }
  1313. f8();
  1314. function f9(arguments = [10, 20], a = () => arguments, b = arguments[1], c = () => a) {
  1315. function arguments() {
  1316. return 100;
  1317. }
  1318. assert.areEqual(100, arguments(), "In the body arguments in the newly defined function");
  1319. assert.areEqual(20, b, "Other formals can access the arguments formal");
  1320. assert.areEqual("10,20", c()().toString(), "Formal capturing works fine with arguments defined as a formal");
  1321. }
  1322. f9();
  1323. function f10({arguments = [10, 20], a = () => arguments}, b = () => a) {
  1324. function arguments() {
  1325. return 100;
  1326. }
  1327. assert.areEqual(100, arguments(), "In the body arguments in the newly defined function");
  1328. assert.areEqual("10,20", b()().toString(), "Formal capturing works fine with arguments defined as a formal");
  1329. }
  1330. f10({}, undefined, 1, );
  1331. }
  1332. },
  1333. {
  1334. name: "Split scope and super call",
  1335. body: function () {
  1336. class c1 {
  1337. constructor() {
  1338. return { x : 1 };
  1339. }
  1340. };
  1341. class c2 extends c1 {
  1342. constructor(a = 1, b = () => { assert.areEqual(1, super().x, "Super is accessible in the param scope"); return a; }) {
  1343. var c = 10;
  1344. var a = 20;
  1345. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  1346. assert.areEqual(1, b(), "Function defined in the param scope should capture the formal");
  1347. return {};
  1348. }
  1349. }
  1350. new c2();
  1351. class c3 extends c1 {
  1352. constructor(a = 1, b = () => { return a; }) {
  1353. (() => assert.areEqual(1, super().x, "Lambda should be able to access the super method properly in the body"))();
  1354. var a = 10;
  1355. assert.areEqual(1, b(), "Function defined in the param scope should capture the formal");
  1356. }
  1357. }
  1358. new c3();
  1359. class c4 extends c1 {
  1360. constructor(a = 1, b = () => { return a; }) {
  1361. var c = 10;
  1362. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  1363. assert.areEqual(1, b(), "Function defined in the param scope should capture the formal");
  1364. assert.areEqual(1, eval("super().x"), "Eval should be able to access the super property properly");
  1365. }
  1366. }
  1367. new c4();
  1368. class c5 extends c1 {
  1369. constructor(a = super().x, b = () => { return a; }) {
  1370. assert.areEqual(1, a, "First formal calls the super from the param scope");
  1371. var c = 10;
  1372. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  1373. assert.areEqual(1, b(), "Function defined in the param scope should capture the formal");
  1374. }
  1375. }
  1376. new c5();
  1377. class c6 extends c1 {
  1378. constructor(a = 1, b = () => { assert.areEqual(1, super().x, "Super is accessible in the param scope"); return a; }) {
  1379. var c = 10;
  1380. a = 20;
  1381. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  1382. assert.areEqual(20, b(), "Assignment in the body updates the value of the formal");
  1383. return {};
  1384. }
  1385. }
  1386. new c6();
  1387. class c7 extends c1 {
  1388. constructor(a = () => super(), b = () => a) {
  1389. assert.areEqual(1, a().x, "Super is captured in a lambda in param scope and executed in the body scope to make sure the same super slot is used in both body and param scope");
  1390. }
  1391. }
  1392. new c7();
  1393. }
  1394. },
  1395. {
  1396. name: "Split scope and super property",
  1397. body: function () {
  1398. class c1 {
  1399. foo () {
  1400. return 1;
  1401. }
  1402. };
  1403. class c2 extends c1 {
  1404. foo(a = 1, b = () => { assert.areEqual(1, super.foo(), "Super property access works fine from a lambda defined in the param scope"); return a; }) {
  1405. var a = 20;
  1406. var c = 10;
  1407. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  1408. assert.areEqual(1, b(), "Function defined in the param scope should capture the formal");
  1409. }
  1410. }
  1411. (new c2()).foo();
  1412. class c3 extends c1 {
  1413. foo(a = 1, b = () => { return a; }) {
  1414. var c = 10;
  1415. var a = 20;
  1416. (() => assert.areEqual(1, super.foo(), "Super property access works fine from a lambda defined in the body scope"))();
  1417. assert.areEqual(1, b(), "Function defined in the param scope should capture the formal");
  1418. }
  1419. }
  1420. (new c3()).foo();
  1421. class c4 extends c1 {
  1422. foo(a = 1, b = () => { return a; }) {
  1423. var c = 10;
  1424. var a = 20;
  1425. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  1426. assert.areEqual(1, b(), "Function defined in the param scope should capture the formal");
  1427. assert.areEqual(1, eval("super.foo()"), "Eval should be able to access the super property properly from the body scope");
  1428. }
  1429. }
  1430. (new c4()).foo();
  1431. class c5 extends c1 {
  1432. foo(a = super.foo(), b = () => { return a; }) {
  1433. assert.areEqual(1, a, "First formal uses the super property from the param scope");
  1434. var c = 10;
  1435. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  1436. var a = 20;
  1437. assert.areEqual(1, b(), "Function defined in the param scope should capture the formal");
  1438. }
  1439. }
  1440. (new c5()).foo();
  1441. class c6 extends c1 {
  1442. foo(a = 1, b = () => { assert.areEqual(1, super.foo(), "Super property access works fine from a lambda defined in the param scope"); return a; }) {
  1443. a = 20;
  1444. var c = 10;
  1445. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  1446. assert.areEqual(20, b(), "Assignment in the body updates the value of the formal");
  1447. }
  1448. }
  1449. (new c6()).foo();
  1450. }
  1451. },
  1452. {
  1453. name: "Split scope and new.target",
  1454. body: function () {
  1455. class c1 {
  1456. constructor(newTarget) {
  1457. assert.isTrue(newTarget == new.target, "Base class should receive the right value for new.target");
  1458. }
  1459. };
  1460. class c2 extends c1 {
  1461. constructor(a = 1, b = () => { assert.isTrue(new.target == c2, "new.target should have the derived class value in the param scope"); return a; }) {
  1462. super(c2);
  1463. var c = 10;
  1464. var a = 20;
  1465. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  1466. assert.areEqual(1, b(), "Function defined in the param scope should capture the formal");
  1467. }
  1468. }
  1469. new c2();
  1470. class c3 extends c1 {
  1471. constructor(a = 1, b = () => { return a; }) {
  1472. super(c3);
  1473. var c = 10;
  1474. (() => assert.isTrue(new.target == c3, "new.target should be the derived class in the body scope when captured by lambda"))();
  1475. assert.isTrue(new.target == c3, "new.target should be the derived class in the body scope");
  1476. }
  1477. }
  1478. new c3();
  1479. class c4 extends c1 {
  1480. constructor(a = 1, b = () => { return a; }) {
  1481. super(c4);
  1482. assert.isTrue(eval("new.target == c4"), "new.target should be the derived class inside eval");
  1483. assert.isTrue(new.target == c4, "new.target should be the derived class in the body scope");
  1484. }
  1485. }
  1486. new c4();
  1487. class c5 extends c1 {
  1488. constructor(a = new.target, b = () => { return a; }) {
  1489. super(c5);
  1490. assert.isTrue(a == c5, "new.target accessed from the param scope should work fine");
  1491. }
  1492. }
  1493. new c5();
  1494. class c6 extends c1 {
  1495. constructor(a = 1, b = () => { assert.isTrue(new.target == c6, "new.target should have the derived class value in the param scope"); return a; }) {
  1496. super(c6);
  1497. var c = 10;
  1498. a = 20;
  1499. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  1500. assert.areEqual(20, b(), "Assignment in the body updates the value of the captured formal");
  1501. }
  1502. }
  1503. new c6();
  1504. }
  1505. },
  1506. {
  1507. name: "Split parameter scope and eval",
  1508. body: function () {
  1509. function g() {
  1510. return 3 * 3;
  1511. }
  1512. function f1(h = () => eval("g()")) {
  1513. assert.areEqual(6, g(), "Right method is called in the body scope");
  1514. function g() {
  1515. return 2 * 3;
  1516. }
  1517. return h();
  1518. }
  1519. assert.areEqual(9, f1(), "Paramater scope remains split");
  1520. function f2(h = () => eval("g()")) {
  1521. assert.areEqual(6, eval("g()"), "Right method is called in the body scope");
  1522. function g() {
  1523. return 2 * 3;
  1524. }
  1525. return h();
  1526. }
  1527. assert.areEqual(9, f2(), "Paramater scope remains split");
  1528. }
  1529. },
  1530. {
  1531. name: "Split parameter scope with eval in body",
  1532. body: function () {
  1533. function f1(a = 10, b = function () { return a; }) {
  1534. assert.areEqual(10, a, "Initial value of parameter in the body scope should be the same as the one in param scope");
  1535. assert.areEqual(10, eval('a'), "Initial value of parameter in the body scope in eval should be the same as the one in param scope");
  1536. var a = 20;
  1537. assert.areEqual(20, a, "New assignment in the body scope updates the variable's value in body scope");
  1538. assert.areEqual(20, eval('a'), "New assignment in the body scope updates the variable's value when evaluated through eval in body scope");
  1539. return b;
  1540. }
  1541. assert.areEqual(10, f1()(), "Function defined in the param scope captures the formals from the param scope not body scope with eval");
  1542. function f2(a = 10, b = function () { return a; }) {
  1543. assert.areEqual(10, eval('b()'), "Eval of the function from param scope should return the right value for the formal");
  1544. var a = 20;
  1545. assert.areEqual(10, eval('b()'), "Eval of the function from param scope should return the right value for the formal even after assignment to the corresponding body symbol");
  1546. return b;
  1547. }
  1548. assert.areEqual(10, f2()(), "Function defined in the param scope captures the formals from the param scope not body scope with eval");
  1549. function f3(a = 10, b = function () { return a; }) {
  1550. assert.areEqual(100, eval('b()'), "Eval of the function from body scope should return the right value for the formal");
  1551. var a = 20;
  1552. function b () { return a * a; }
  1553. assert.areEqual(400, eval('b()'), "Eval of the function from body scope should return the right value after assignment to the corresponding body symbol");
  1554. return b;
  1555. }
  1556. assert.areEqual(400, f3()(), "Function defined in the body scope captures the symbol from the body scope with eval");
  1557. function f4 (a, b, c = function () { b; }, d = 1) {
  1558. var e = 10;
  1559. assert.areEqual(2, arguments[0], "Unmapped arguments value has the expected value in the body");
  1560. (function () {
  1561. eval('');
  1562. }());
  1563. };
  1564. f4.call(1, 2);
  1565. function f5(a = 10, b = 20, c = () => [a, b]) {
  1566. assert.areEqual(10, c()[0], "Before the assignment in the body the value of the symbol is the body is same as the formal formal with eval in the body");
  1567. assert.areEqual(20, eval("c()[1]"), "With eval in the body, the value of the formal is retained before the assignment");
  1568. var a = 20;
  1569. b = 200;
  1570. assert.areEqual(20, a, "New assignment in the body scope updates the variable's value in body scope with eval in the body");
  1571. return c;
  1572. }
  1573. assert.areEqual(10, f5()()[0], "Assignment in the body does not affect the value of the formal with eval in the body");
  1574. assert.areEqual(200, f5()()[1], "Assignment in the body updates the value of the formal with eval in the body");
  1575. function f6(a = 10, b = function () { return a; }) {
  1576. assert.areEqual(10, a, "Initial value of parameter in the body scope should be the same as the one in param scope");
  1577. assert.areEqual(10, eval('a'), "Initial value of parameter in the body scope in eval should be the same as the one in param scope");
  1578. a = 20;
  1579. assert.areEqual(20, a, "New assignment in the body scope updates the variable's value in body scope");
  1580. assert.areEqual(20, eval('a'), "New assignment in the body scope updates the variable's value when evaluated through eval in body scope");
  1581. return b;
  1582. }
  1583. assert.areEqual(20, f6()(), "Function defined in the param scope captures the formals from the param scope which is updated in the body");
  1584. function f7(a, b = ()=> a) {
  1585. eval("var a = 100");
  1586. assert.areEqual(100, a, "Body gets the variable declaration leaked from eval");
  1587. assert.areEqual(10, b(), "Param scope function still points to the formal symbol");
  1588. }
  1589. f7(10);
  1590. var x = 1;
  1591. var f8 = (a = 10, b = () => a) => {
  1592. eval("var a = 100");
  1593. return function f9()
  1594. {
  1595. function abc() { return 1000; }
  1596. function c(foo) {
  1597. return abc();
  1598. }
  1599. return c() + x + b() + a;
  1600. };
  1601. }
  1602. assert.areEqual(1111, f8()(), "The new definition of variable in the body, when there are no scope slots intiially allocated for the body, should be captured by the inner function");
  1603. }
  1604. },
  1605. {
  1606. name: "Split scope and with",
  1607. body: function () {
  1608. function f1(a, b, c = function () { a; }) {
  1609. with ({}) {
  1610. var d = function () {
  1611. return 10;
  1612. };
  1613. assert.areEqual(10, d(), "With inside a split scope function should work fine");
  1614. }
  1615. }
  1616. f1();
  1617. function f2(a, b, c = function () { a; }) {
  1618. var d = function () {
  1619. return 10;
  1620. };
  1621. with ({}) {
  1622. assert.areEqual(10, d(), "With inside a split scope function should be able to access the function definition from the body");
  1623. }
  1624. }
  1625. f2();
  1626. function f3(a, b = function () { return 10; }, c = function () { a; }) {
  1627. with ({}) {
  1628. assert.areEqual(10, b(), "With inside a split scope function should be able to access the function definition from the param scope");
  1629. }
  1630. }
  1631. f3();
  1632. function f4(a, b = function () { return 10; }, c = function () { a; }) {
  1633. var d = {
  1634. e : function () { return 10; }
  1635. };
  1636. e = function () { return 100; };
  1637. with (d) {
  1638. assert.areEqual(10, e(), "With should use the function definition inside the object not the one from body");
  1639. }
  1640. }
  1641. f4();
  1642. function f5(a, b = { d : function () { return 10; } }, c = function () { a; }) {
  1643. var d = { };
  1644. with (b) {
  1645. assert.areEqual(10, d(), "With should use the function definition inside the object from the param scope not the one from body");
  1646. }
  1647. }
  1648. f5();
  1649. var v6 = 100
  1650. function f6(a, b, c = function () { a; }, e = function () { with({}) { assert.areEqual(100, v6, "With inside param scope should be able to access var from outside"); } }, f = e()) {
  1651. var v6 = { };
  1652. }
  1653. f6();
  1654. function f7(a, b, c = function () { a; }) {
  1655. with ({}) {
  1656. assert.areEqual(100, v6, "With inside body scope should be able to access var from outside");
  1657. }
  1658. }
  1659. f7();
  1660. function f8() {
  1661. function f9() {
  1662. return 1;
  1663. }
  1664. var v1 = 10;
  1665. function f10(a = 10, b = function f11() {
  1666. a;
  1667. assert.areEqual(10, v1, "Function in the param scope should be able to access the outside variable");
  1668. with ({}) {
  1669. assert.areEqual(1, f9(), "With construct inside a param scoped function should be able to execute functions from outside");
  1670. }
  1671. }) {
  1672. b();
  1673. };
  1674. f10();
  1675. }
  1676. f8();
  1677. f8();
  1678. function f12() {
  1679. function f13() {
  1680. return 1;
  1681. }
  1682. var v2 = 100;
  1683. function f14(a = 10, b = function () {
  1684. assert.areEqual(10, a, "Function in the param scope should be able to access the formal from parent");
  1685. return function () {
  1686. assert.areEqual(10, a, "Function nested in the param scope should be able to access the formal from the split scoped function");
  1687. assert.areEqual(100, v2, "Function in the param scope should be able to access the outside variable");
  1688. with ({}) {
  1689. assert.areEqual(1, f13(), "With construct inside a param scoped function should be able to execute functions from outside");
  1690. }
  1691. };
  1692. }) {
  1693. b()();
  1694. };
  1695. f14();
  1696. }
  1697. f12();
  1698. f12();
  1699. function f13() {
  1700. var protoObj1 = { x : 1 };
  1701. var f14 = function () {
  1702. return 100;
  1703. };
  1704. var f15 = function (a = 10, b = function () {
  1705. with (protoObj1) {
  1706. assert.areEqual(1, x, "With inside a param scope function should be able to retrieve the object properties");
  1707. }
  1708. assert.areEqual(10, a, "Param scope function containing with construct should be able to access the first parameter");
  1709. }) {
  1710. function func5() {
  1711. return f14;
  1712. }
  1713. assert.areEqual(100, func5()(), "Function in the body scope which has 0 scope slots accesses an outside function");
  1714. b();
  1715. };
  1716. f15();
  1717. }
  1718. f13();
  1719. }
  1720. },
  1721. {
  1722. name: "Basic eval in parameter scope",
  1723. body: function () {
  1724. assert.areEqual(1,
  1725. function (a = eval("1")) { return a; }(),
  1726. "Eval with static constant works in parameter scope");
  1727. {
  1728. let b = 2;
  1729. assert.areEqual(2,
  1730. function (a = eval("b")) { return a; }(),
  1731. "Eval with parent var reference works in parameter scope");
  1732. }
  1733. assert.areEqual(1,
  1734. function (a, b = eval("arguments[0]")) { return b; }(1),
  1735. "Eval with arguments reference works in parameter scope");
  1736. function testSelf(a = eval("testSelf(1)")) {
  1737. return a;
  1738. }
  1739. assert.areEqual(1, testSelf(1), "Eval with reference to the current function works in parameter scope");
  1740. var testSelfExpr = function (a = eval("testSelfExpr(1)")) {
  1741. return a;
  1742. }
  1743. assert.areEqual(1, testSelfExpr(), "Eval with reference to the current function expression works in parameter scope");
  1744. {
  1745. let a = 1, b = 2, c = 3;
  1746. function testEvalRef(a = eval("a"), b = eval("b"), c = eval("c")) {
  1747. return [a, b, c];
  1748. }
  1749. assert.throws(function () { testEvalRef(); },
  1750. ReferenceError,
  1751. "Eval with reference to the current formal throws",
  1752. "Use before declaration");
  1753. function testEvalRef2(x = eval("a"), y = eval("b"), z = eval("c")) {
  1754. return [x, y, z];
  1755. }
  1756. assert.areEqual([1, 2, 3], testEvalRef2(), "Eval with references works in parameter scope");
  1757. }
  1758. function f1(a = 10, b = () => eval("a")) {
  1759. assert.areEqual(10, eval("a"), "In the body initial value of the symbol should be same as the final value from param scope");
  1760. var a = 20;
  1761. assert.areEqual(20, eval("a"), "In the body after assignment the symbol value is updated");
  1762. assert.areEqual(10, b(), "Eval in the param scope captures the symbol from the param scope");
  1763. }
  1764. f1();
  1765. function f2(a = 10, b = () => eval("a")) {
  1766. var a = 20;
  1767. assert.areEqual(10, b(), "Eval in the param scope captures the symbol from the param scope even when there is no eval in the body");
  1768. }
  1769. f2();
  1770. function f3(a = 10, b = function () { return eval("a"); }) {
  1771. var a = 20;
  1772. assert.areEqual(10, b(), "Eval in the child function of a param scope captures the symbol from the param scope even when there is no eval in the body");
  1773. }
  1774. f3();
  1775. function f4(a = 10, b = () => eval("a"), c = a = 30) {
  1776. assert.areEqual(30, eval("a"), "In the body initial value of the symbol should be same as the final value from param scope");
  1777. var a = 20;
  1778. assert.areEqual(20, eval("a"), "In the body after assignment the symbol value is updated");
  1779. assert.areEqual(30, b(), "Eval in the param scope captures the symbol from the param scope");
  1780. }
  1781. f4();
  1782. function f5(a = 10, b = () => eval("a")) {
  1783. assert.areEqual(30, eval("a"), "In the body initial value of the symbol should be same as the final value from param scope");
  1784. var a = 20;
  1785. assert.areEqual(20, eval("a"), "In the body after assignment the symbol value is updated");
  1786. assert.areEqual(30, b(), "Eval in the param scope captures the symbol from the param scope");
  1787. }
  1788. f5(30);
  1789. var f6 = function f7(a = 10, b = () => eval("a")) {
  1790. var a = 20;
  1791. assert.areEqual(10, b(), "Eval in the param scope captures the symbol from the param scope for a function expression with name");
  1792. }
  1793. f6();
  1794. var f8 = function f9(a = 10, b = () => eval("a"), c = b) {
  1795. var a = 20;
  1796. function b() {
  1797. return a;
  1798. }
  1799. assert.areEqual(20, eval("b()"), "Eval in the body uses the function definition from body");
  1800. assert.areEqual(10, c(), "Eval in the param scope captures the symbol from the param scope for a function expression with name even with eval in the body");
  1801. }
  1802. f8();
  1803. function f9(a = 10, b = () => () => eval("a")) {
  1804. var a = 20;
  1805. assert.areEqual(10, b()(), "Eval in a nested function in the param scope captures the symbol from the param scope even when there is no eval in the body");
  1806. }
  1807. f9();
  1808. function f10(a = 10, b = () => () => eval("a")) {
  1809. var a = 20;
  1810. assert.areEqual(10, eval("b()()"), "Eval in a nested function in the param scope captures the symbol from the param scope even when there is no eval in the body");
  1811. }
  1812. f10();
  1813. var obj = {
  1814. mf1(a = 10, b = () => () => eval("a")) {
  1815. var a = 20;
  1816. assert.areEqual(10, eval("b()()"), "Eval in a nested function in the param scope of a method definition captures the symbol from the param scope");
  1817. }
  1818. }
  1819. obj.mf1();
  1820. var arr = [2, 3, 4];
  1821. function f11(a = 10, b = function () { return eval("a"); }, ...c) {
  1822. assert.areEqual(arr.length, c.length, "Rest parameter should contain the same number of elements as the spread arg");
  1823. for (i = 0; i < arr.length; i++) {
  1824. assert.areEqual(arr[i], c[i], "Elements in the rest and the spread should be in the same order");
  1825. }
  1826. return b;
  1827. }
  1828. assert.areEqual(f11(undefined, undefined, ...arr)(), 10, "Presence of rest parameter shouldn't affect the binding");
  1829. function f12(a = 10, b = eval("a"), ...c) {
  1830. assert.areEqual(arr.length, c.length, "Rest parameter should contain the same number of elements as the spread arg");
  1831. for (i = 0; i < arr.length; i++) {
  1832. assert.areEqual(arr[i], c[i], "Elements in the rest and the spread should be in the same order");
  1833. }
  1834. return b;
  1835. }
  1836. assert.areEqual(f12(undefined, undefined, ...arr), 10, "Presence of rest parameter shouldn't affect the binding");
  1837. function f13(x = 1) {
  1838. (function () {
  1839. function f14 (a = () => eval("x"), b = a) {
  1840. var a = 20;
  1841. assert.areEqual(1, b(), "Eval in param scope should be able to access formals from an outer function");
  1842. };
  1843. f14();
  1844. })()
  1845. }
  1846. f13();
  1847. function f15(a = eval) {
  1848. assert.areEqual(1, a("v15"), "Indirect eval should be able to access the global variables properly");
  1849. }
  1850. f15();
  1851. function f16(a = 10, b = () => eval("a")) {
  1852. assert.areEqual(10, eval("a"), "In the body initial value of the symbol should be same as the final value from param scope");
  1853. a = 20;
  1854. assert.areEqual(20, eval("a"), "In the body after assignment the symbol value is updated");
  1855. assert.areEqual(20, b(), "Eval in the param scope captures the symbol from the param scope which is updated in the body");
  1856. }
  1857. f16();
  1858. function f17(a = 10, b = () => eval("a")) {
  1859. a = 20;
  1860. assert.areEqual(20, b(), "Eval in the param scope captures the symbol from the param scope even when there is no eval in the body");
  1861. }
  1862. f17();
  1863. function f18(a = 10, b = function () { return eval("a"); }) {
  1864. a = 20;
  1865. assert.areEqual(20, b(), "Eval in the param scope captures the symbol from the param scope even when there is no eval in the body");
  1866. }
  1867. f18();
  1868. var x = 10;
  1869. var f19 = (d = eval("10")) => {
  1870. function f20()
  1871. {
  1872. function abc() { return 1; }
  1873. function c() {
  1874. return abc();
  1875. }
  1876. return c() + x + d;
  1877. };
  1878. return f20();
  1879. }
  1880. assert.areEqual(21, f19(), "Method inside a split scoped function is able to access the outside variable");
  1881. var x = 1;
  1882. var f20 = (d = eval("10")) => {
  1883. return function f21()
  1884. {
  1885. function abc() { return 100; }
  1886. function c(foo) {
  1887. return abc();
  1888. }
  1889. return c() + x + d;
  1890. };
  1891. }
  1892. assert.areEqual(111, f20()(), "When there are no scope slots allocated in the body we should still be creating the inner closure for the body.");
  1893. }
  1894. },
  1895. {
  1896. name: "Split scoped functions inside eval",
  1897. body: function () {
  1898. var c = 10;
  1899. var result = eval(`(function (a = 1, b = () => a + c) {
  1900. var a = 2 + c;
  1901. return [a, b()];
  1902. })()`);
  1903. assert.areEqual([12, 11].toString(), result.toString(), "Split scope function defined inside an eval should work fine");
  1904. c = 10;
  1905. result = eval(`(function (a = 1, b = () => a + c) {
  1906. a = 2 + c;
  1907. return [a, b()];
  1908. })()`);
  1909. assert.areEqual([12, 22].toString(), result.toString(), "Split scope function defined inside an eval captures the formal whose value is updated in the body");
  1910. c = 10;
  1911. result = eval(`(function (a = 1, b = () => eval('a + c')) {
  1912. var a = 2 + c;
  1913. return [a, b()];
  1914. })()`);
  1915. assert.areEqual([12, 11].toString(), result.toString(), "Split scope function with eval defined inside an eval should work fine");
  1916. c = 10;
  1917. result = (function (b = eval(`((a = 1, b = () => a + c) => {
  1918. a = 2 + c;
  1919. return [a, b()];
  1920. })()`)) {
  1921. a = c;
  1922. result = eval(`((a1 = 1, b1 = () => a1 + a + c) => {
  1923. a = 2 + a1 + c;
  1924. return [a, b1()];
  1925. })()`);
  1926. return [a, result, b];
  1927. })();
  1928. assert.areEqual([13, 13, 24, 12, 22].toString(), result.toString(), "Split scope functions defined in both body and the param scope should work fine with eval");
  1929. }
  1930. },
  1931. {
  1932. name: "Eval declarations in parameter scope",
  1933. body: function() {
  1934. // Redeclarations of formals - var
  1935. assert.throws(function () { return function (a = eval("var a = 2"), b = a) { return [a, b]; }() },
  1936. ReferenceError,
  1937. "Redeclaring the current formal using var inside an eval throws",
  1938. "Let/Const redeclaration");
  1939. assert.doesNotThrow(function () { "use strict"; return function (a = eval("var a = 2"), b = a) { return [a, b]; }() },
  1940. "Redeclaring the current formal using var inside a strict mode eval does not throw");
  1941. assert.doesNotThrow(function () { "use strict"; return function (a = eval("var a = 2"), b = a) { return [a, b]; }() },
  1942. "Redeclaring the current formal using var inside a strict mode function eval does not throw");
  1943. assert.throws(function () { function foo(a = eval("var b"), b, c = b) { return [a, b, c]; } foo(); },
  1944. ReferenceError,
  1945. "Redeclaring a future formal using var inside an eval throws",
  1946. "Let/Const redeclaration");
  1947. assert.throws(function () { function foo(a, b = eval("var a"), c = a) { return [a, b, c]; } foo(); },
  1948. ReferenceError,
  1949. "Redeclaring a previous formal using var inside an eval throws",
  1950. "Let/Const redeclaration");
  1951. // Let and const do not leak outside of an eval, so the test cases below should never throw.
  1952. // Redeclarations of formals - let
  1953. assert.doesNotThrow(function (a = eval("let a")) { return a; },
  1954. "Attempting to redeclare the current formal using let inside an eval does not leak");
  1955. assert.doesNotThrow(function (a = eval("let b"), b) { return [a, b]; },
  1956. "Attempting to redeclare a future formal using let inside an eval does not leak");
  1957. assert.doesNotThrow(function (a, b = eval("let a")) { return [a, b]; },
  1958. "Attempting to redeclare a previous formal using let inside an eval does not leak");
  1959. // Redeclarations of formals - const
  1960. assert.doesNotThrow(function (a = eval("const a = 1")) { return a; },
  1961. "Attempting to redeclare the current formal using const inside an eval does not leak");
  1962. assert.doesNotThrow(function (a = eval("const b = 1"), b) { return [a, b]; },
  1963. "Attempting to redeclare a future formal using const inside an eval does not leak");
  1964. assert.doesNotThrow(function (a, b = eval("const a = 1")) { return [a, b]; },
  1965. "Attempting to redeclare a previous formal using const inside an eval does not leak");
  1966. // Conditional declarations
  1967. function test(x = eval("var a1 = 1; let b1 = 2; const c1 = 3;")) {
  1968. // none should be visible
  1969. assert.throws(function () { a1 }, ReferenceError, "Ignoring the default value does not result in an eval declaration leaking", "'a1' is not defined");
  1970. assert.throws(function () { b1 }, ReferenceError, "Let declarations do not leak out of eval to parameter scope", "'b1' is not defined");
  1971. assert.throws(function () { c1 }, ReferenceError, "Const declarations do not leak out of eval to parameter scope when x is ", "'c1' is not defined");
  1972. }
  1973. test();
  1974. // Redefining locals
  1975. function foo(a = eval("var x = 1; assert.areEqual(1, x, 'Variable declared inside eval is accessible within eval');")) {
  1976. assert.areEqual(undefined, x, "Var declaration from eval is not visible in the body");
  1977. var x = 10;
  1978. assert.areEqual(10, x, "Var declaration from eval uses its new value in the body declaration");
  1979. }
  1980. assert.doesNotThrow(function() { foo(); }, "Redefining a local var with an eval var does not throw");
  1981. // Function bodies defined in eval
  1982. function funcArrow(a = eval("() => 1"), b = a) { function a() { return 10; }; return [a(), b()]; }
  1983. assert.areEqual([10,1], funcArrow(), "Defining an arrow function body inside an eval works at default parameter scope");
  1984. function f1(a = eval("(function f11() { return 1; })"), b = a()) { return [a(), b]; }
  1985. assert.areEqual([1, 1], f1(), "Defining a function inside an eval works at default parameter scope");
  1986. function f2(a = eval("function f21() { return 1; }; f21"), b = a()) { return [a(), b]; }
  1987. assert.areEqual([1, 1], f2(), "Defining a function inside an eval works at default parameter scope");
  1988. function f3(a = eval("(f31 = function () { return 1; }, f31())")) { return a; }
  1989. assert.areEqual(1, f3(), "Defining a function inside an eval works at default parameter scope");
  1990. function f4(a = eval("(function *f41() { yield 1; return 2; })"), b = a(), c = b.next()) { return [c, b.next()]; }
  1991. assert.areEqual([{value : 1, done : false}, {value : 2, done : true}], f4(), "Declaring a generator function inside an eval works at default parameter scope");
  1992. function f5(a = eval("f = function f51() { return 1; }"), b = f()) { return [a(), b, f()]; }
  1993. assert.areEqual([1, 1, 1], f5(), "Declaring a function inside an eval works at default parameter scope");
  1994. assert.throws(function () { eval("function f6(a = eval('b'), b) {}; f6();"); }, ReferenceError, "Future default references using eval are not allowed", "Use before declaration");
  1995. }
  1996. },
  1997. {
  1998. name: "Eval and with",
  1999. body: function () {
  2000. function f1(a = { x : 10 }, b = () => eval("a")) {
  2001. var a = 20
  2002. with (b()) {
  2003. return b().x + a;
  2004. }
  2005. }
  2006. assert.areEqual(30, f1(), "With in the body of a eval split scoped function should not affect the usage of eval in the param scope");
  2007. function f2(a = { x : 10 }, b = function () { return eval("a.x"); }) {
  2008. var a = 20;
  2009. with ({}) {
  2010. return b() + a;
  2011. }
  2012. }
  2013. assert.areEqual(30, f2(), "With in the param scope of a eval split scoped function should not affect the parameter capture");
  2014. function f3(a = { x : 10 }, b = function () { return eval("a.x"); }, c = b) {
  2015. var a = 20;
  2016. with ({}) {
  2017. return c() + b();
  2018. }
  2019. function b() {
  2020. return a;
  2021. }
  2022. }
  2023. assert.areEqual(30, f3(), "With in the param scope of a eval split scoped function should work fine even with shadowing in body");
  2024. function f4(a = { x : 10 }, b = () => eval("a")) {
  2025. a = 20
  2026. with (b()) {
  2027. return b() + a;
  2028. }
  2029. }
  2030. assert.areEqual(40, f4(), "With eval in the param scope capturing should reflect the update to the formal's value from the body");
  2031. }
  2032. },
  2033. {
  2034. name: "Eval and this",
  2035. body: function () {
  2036. function f1(a, b = () => eval("this.x")) {
  2037. return b();
  2038. }
  2039. assert.areEqual(10, f1.call({x : 10}), "Eval in the param scope should be able to access this");
  2040. function f2(a, b = () => () => eval("this.x()")) {
  2041. return b();
  2042. }
  2043. assert.areEqual(10, f2.call({x () { return 10; }})(), "Eval in a nested function in param should be able to access this");
  2044. function f3(b = () => eval("this"), c = () => b) {
  2045. function b() {
  2046. return 20;
  2047. }
  2048. return b() + c()().x();
  2049. }
  2050. assert.areEqual(30, f3.call({x () { return 10; }}), "Eval in param scope can access this with function definition in body");
  2051. function f4(a, b = () => eval("this")) {
  2052. return eval("b().x() + a");
  2053. }
  2054. assert.areEqual(30, f4.call({x () { return 10; }}, 20), "Eval in param scope can access this when eval is present in the body");
  2055. }
  2056. },
  2057. {
  2058. name: "Class definitions in eval",
  2059. body: function () {
  2060. function f1(a = eval("(class c1 { mf1 () { return b; } })"), b = 10) {
  2061. var b = new a();
  2062. assert.areEqual(10, b.mf1(), "Class defined in an eval in a param scope should work fine");
  2063. }
  2064. f1();
  2065. var f2 = function f3(a = eval("(class c1 { mf1 () { return b; } })"), b = 10) {
  2066. var b = new a();
  2067. assert.areEqual(10, b.mf1(), "Class defined in an eval in a param scope of a function expression with name should work fine");
  2068. }
  2069. f2();
  2070. function f4(a = eval("(class c1 { mf1 () { return b; } })"), b = 10) {
  2071. assert.areEqual(10, eval("(new a()).mf1()"), "Class defined in an eval in a param scope should work fine with eval in the body");
  2072. }
  2073. f4();
  2074. }
  2075. },
  2076. {
  2077. name: "Eval and new.target",
  2078. body: function () {
  2079. class c1 {
  2080. constructor(newTarget) {
  2081. assert.isTrue(newTarget == new.target, "Base class should receive the right value for new.target");
  2082. }
  2083. };
  2084. class c2 extends c1 {
  2085. constructor(a = 1, b = () => { assert.isTrue(eval("new.target") == c2, "new.target should have the derived class value in the param scope"); return a; }) {
  2086. super(c2);
  2087. var c = 10;
  2088. var a = 20;
  2089. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  2090. assert.areEqual(1, b(), "Function with eval defined in the param scope should be abel to access new.target properly");
  2091. }
  2092. }
  2093. new c2();
  2094. class c3 extends c1 {
  2095. constructor(a = 1, b = () => { assert.isTrue(eval("new.target") == c3, "new.target should have the derived class value in the param scope"); return a; }) {
  2096. super(c3);
  2097. var c = 10;
  2098. var a = 20;
  2099. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  2100. assert.areEqual(1, eval("b()"), "Function defined in the param scope should capture the formal");
  2101. }
  2102. }
  2103. new c3();
  2104. class c4 extends c1 {
  2105. constructor(a = new.target, b = () => eval("a")) {
  2106. super(c4);
  2107. assert.isTrue(b() == c4, "new.target accessed from the param scope should work fine");
  2108. }
  2109. }
  2110. new c4();
  2111. class c5 extends c1 {
  2112. constructor(a, b = eval("a = new.target"), c = () => a) {
  2113. super(c5);
  2114. assert.isTrue(c() == c5, "new.target accessed from the param scope should work fine");
  2115. }
  2116. }
  2117. new c5();
  2118. class c6 extends c1 {
  2119. constructor(a = 1, b = () => { assert.isTrue(eval("new.target") == c6, "new.target should have the derived class value in the param scope"); return a; }) {
  2120. super(c6);
  2121. var c = 10;
  2122. a = 20;
  2123. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  2124. assert.areEqual(20, b(), "The value of the formal is updated by the assignment in the body");
  2125. }
  2126. }
  2127. new c6();
  2128. }
  2129. },
  2130. {
  2131. name: "Eval and super call",
  2132. body: function () {
  2133. class c1 {
  2134. constructor() {
  2135. return { x : 1 };
  2136. }
  2137. };
  2138. class c2 extends c1 {
  2139. constructor(a = 1, b = () => { assert.areEqual(1, eval("super().x"), "Super is accessible in eval in the param scope"); return a; }) {
  2140. var c = 10;
  2141. var a = 20;
  2142. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables with eval in the param scope"))();
  2143. assert.areEqual(1, b(), "Function with eval defined in the param scope should capture the formal");
  2144. return {};
  2145. }
  2146. }
  2147. new c2();
  2148. class c3 extends c1 {
  2149. constructor(a = 1, b = eval("a")) {
  2150. assert.areEqual(1, b, "Eval should be able to capture the formal even when there is no eval in the body");
  2151. assert.areEqual(1, super().x, "Super call should work fine in the body with eval in the param scope");
  2152. }
  2153. }
  2154. new c3();
  2155. class c4 extends c1 {
  2156. constructor(a = 1, b = () => eval("a")) {
  2157. var c = 10;
  2158. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  2159. assert.areEqual(1, b(), "Function with eval defined in the param scope should capture the formal the right way");
  2160. assert.areEqual(1, eval("super().x"), "Eval should be able to access the super property properly in the body");
  2161. }
  2162. }
  2163. new c4();
  2164. class c5 extends c1 {
  2165. constructor(a = super().x, b = eval("a")) {
  2166. assert.areEqual(1, a, "First formal calls the super from the param scope should work fine with eval in the param scope");
  2167. assert.areEqual(1, b, "Eval should work fine with super in the param");
  2168. }
  2169. }
  2170. new c5();
  2171. class c6 extends c1 {
  2172. constructor(a = 1, b = () => { assert.areEqual(1, eval("super().x"), "Super is accessible in eval in the param scope"); return a; }) {
  2173. var c = 10;
  2174. a = 20;
  2175. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables with eval in the param scope"))();
  2176. assert.areEqual(20, b(), "Function with eval defined in the param scope should capture the formal which is updated in the body");
  2177. return {};
  2178. }
  2179. }
  2180. new c6();
  2181. class c7 extends c1 {
  2182. constructor(a = 1, b = eval("a")) {
  2183. assert.areEqual(1, a, "Default value is assigned to the first formal");
  2184. assert.areEqual(1, b, "Eval of a formal works fine in param scope when body has super call");
  2185. assert.areEqual(1, super().x, "Super call is executed in the body when param scope has eval");
  2186. }
  2187. }
  2188. new c7();
  2189. class c8 extends c1 {
  2190. constructor(a = super().x, b = eval("a")) {
  2191. assert.areEqual(1, b, "First formal is assigned the value returned from super constructor");
  2192. }
  2193. }
  2194. assert.areEqual(1, (new c8()).x, "Super call from the formal with eval in the param returns the correct value");
  2195. class c9 extends c1 {
  2196. constructor(a = eval("super().x")) {
  2197. assert.areEqual(1, a, "First formal is assigned the value returned from super constructor through eval");
  2198. }
  2199. }
  2200. assert.areEqual(1, (new c9()).x, "Super call from the formal with eval in the param returns the correct value");
  2201. }
  2202. },
  2203. {
  2204. name: "Eval and super property",
  2205. body: function () {
  2206. class c1 {
  2207. foo () {
  2208. return 1;
  2209. }
  2210. };
  2211. class c2 extends c1 {
  2212. foo(a = 1, b = () => { assert.areEqual(1, eval(super.foo()), "Super property access works fine inside eval in a lambda defined in the param scope"); return a; }) {
  2213. var a = 20;
  2214. var c = 10;
  2215. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  2216. assert.areEqual(1, b(), "Function with eval defined in the param scope should capture the formal");
  2217. }
  2218. }
  2219. (new c2()).foo();
  2220. class c3 extends c1 {
  2221. foo(a = 1, b = eval("a")) {
  2222. assert.areEqual(1, b, "Eval should be able to capture the formal even when there is no eval in the body");
  2223. assert.areEqual(1, super.foo(), "Super property access should work fine in the body with eval in the param scope");
  2224. }
  2225. }
  2226. (new c3()).foo();
  2227. class c4 extends c1 {
  2228. foo(a = 1, b = () => eval("a")) {
  2229. var c = 10;
  2230. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  2231. assert.areEqual(1, b(), "Function with eval defined in the param scope should capture the formal the right way");
  2232. assert.areEqual(1, eval("super.foo()"), "Eval should be able to access the super property properly in the body with eval");
  2233. }
  2234. }
  2235. (new c4()).foo();
  2236. class c5 extends c1 {
  2237. foo(a = super.foo(), b = eval("a")) {
  2238. assert.areEqual(1, a, "First formal's super property access from the param scope should work fine with eval in the param scope");
  2239. assert.areEqual(1, b, "Eval should work fine with super in the param");
  2240. }
  2241. }
  2242. (new c5()).foo();
  2243. class c6 extends c1 {
  2244. foo(a = 1, b = () => { assert.areEqual(1, eval(super.foo()), "Super property access works fine inside eval in a lambda defined in the param scope"); return a; }) {
  2245. a = 20;
  2246. var c = 10;
  2247. (() => assert.areEqual(10, c, "Allocation of scope slot for super property shouldn't affect the body variables"))();
  2248. assert.areEqual(20, b(), "Function with eval defined in the param scope should capture the formal and reflect the assignment from the body");
  2249. }
  2250. }
  2251. (new c6()).foo();
  2252. }
  2253. },
  2254. {
  2255. name: "Eval and destructuring",
  2256. body: function () {
  2257. function f1({a:a1, b:b1}, c = eval("a1 + b1")) {
  2258. assert.areEqual(10, a1, "Initial value of the first destructuring parameter in the body scope should be the same as the one in param scope");
  2259. assert.areEqual(20, b1, "Initial value of the second destructuring parameter in the body scope should be the same as the one in param scope");
  2260. var a1 = 1;
  2261. var b1 = 2;
  2262. assert.areEqual(1, a1, "New assignment in the body scope updates the first formal's value in body scope");
  2263. assert.areEqual(2, b1, "New assignment in the body scope updates the second formal's value in body scope");
  2264. assert.areEqual(30, c, "The param scope method should return the sum of the destructured formals from the param scope");
  2265. return c;
  2266. }
  2267. assert.areEqual(30, f1({ a : 10, b : 20 }), "Method should return the sum of the destructured formals from the param scope");
  2268. function f2( {a:a1, b:b1}, c = function () { return eval("a1 + b1"); }) {
  2269. var a1 = 1;
  2270. var b1 = 2;
  2271. eval("");
  2272. return c;
  2273. }
  2274. assert.areEqual(30, f2({ a : 10, b : 20 })(), "Returned method should return the sum of the destructured formals from the param scope even with eval in the body");
  2275. function f3({x:x = 10, y:y = eval("x")}) {
  2276. assert.areEqual(10, x, "Initial value of the first destructuring parameter in the body scope should be the same as the one in param scope");
  2277. var x = 20;
  2278. assert.areEqual(20, x, "Assignment in the body updates the formal's value");
  2279. return y;
  2280. }
  2281. assert.areEqual(10, f3({ }), "Returned method should return the value of the destructured formal from the param scope");
  2282. (({x:x = 10, y:y = () => { return eval("x"); }}) => {
  2283. assert.areEqual(10, x, "Initial value of the first destructuring parameter in the body scope should be the same as the one in param scope");
  2284. var x = 20;
  2285. assert.areEqual(10, y(), "Assignment in the body does not affect the formal captured from the param scope");
  2286. })({});
  2287. function f4({a:a1, b:b1}, c = () => eval("a1 + b1")) {
  2288. assert.areEqual(10, a1, "Initial value of the first destructuring parameter in the body scope should be the same as the one in param scope");
  2289. assert.areEqual(20, b1, "Initial value of the second destructuring parameter in the body scope should be the same as the one in param scope");
  2290. a1 = 1;
  2291. b1 = 2;
  2292. assert.areEqual(1, a1, "New assignment in the body scope updates the first formal's value in body scope");
  2293. assert.areEqual(2, b1, "New assignment in the body scope updates the second formal's value in body scope");
  2294. assert.areEqual(3, c(), "The param scope method should return the sum of the destructured formals from the param scope");
  2295. return c;
  2296. }
  2297. assert.areEqual(3, f4({ a : 10, b : 20 })(), "Method should return the sum of the destructured formals which are updated in the body");
  2298. }
  2299. },
  2300. {
  2301. name: "Eval and arguments in param scope",
  2302. body: function () {
  2303. function f1(a, b = eval("arguments[0]")) {
  2304. assert.areEqual(1, b, "arguments should be accessible from an eval inside the param scope");
  2305. assert.areEqual(1, arguments[0], "arguments should work fine in the body when eval present in the param scope");
  2306. }
  2307. f1(1);
  2308. function f2(a, b = eval("arguments[0] = 100")) {
  2309. assert.areEqual(100, b, "Assignment to arguments inside the eval should be reflected in the body");
  2310. assert.areEqual(100, arguments[0], "arguments is updated after the assignment");
  2311. }
  2312. f2(1);
  2313. function f3(a, b = function () { return eval("arguments[0]"); }) {
  2314. assert.areEqual(1, b(1), "Nested function in the param scope should be able to use its own arguments object properly");
  2315. }
  2316. f3();
  2317. }
  2318. }
  2319. ];
  2320. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });