compiler_test.go 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265
  1. package goja
  2. import (
  3. "github.com/dop251/goja/parser"
  4. "io/ioutil"
  5. "testing"
  6. )
  7. func testScript(script string, expectedResult Value, t *testing.T) {
  8. prg, err := parser.ParseFile(nil, "test.js", script, 0)
  9. if err != nil {
  10. t.Fatal(err)
  11. }
  12. c := newCompiler()
  13. c.compile(prg, false, false, true)
  14. r := &Runtime{}
  15. r.init()
  16. vm := r.vm
  17. vm.prg = c.p
  18. vm.prg.dumpCode(t.Logf)
  19. vm.run()
  20. t.Logf("stack size: %d", len(vm.stack))
  21. t.Logf("stashAllocs: %d", vm.stashAllocs)
  22. v := vm.r.globalObject.self.getStr("rv", nil)
  23. if v == nil {
  24. v = _undefined
  25. }
  26. if !v.SameAs(expectedResult) {
  27. t.Fatalf("Result: %+v, expected: %+v", v, expectedResult)
  28. }
  29. if vm.sp != 0 {
  30. t.Fatalf("sp: %d", vm.sp)
  31. }
  32. }
  33. func testScript1(script string, expectedResult Value, t *testing.T) {
  34. prg, err := parser.ParseFile(nil, "test.js", script, 0)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. c := newCompiler()
  39. c.compile(prg, false, false, true)
  40. r := &Runtime{}
  41. r.init()
  42. vm := r.vm
  43. vm.prg = c.p
  44. vm.prg.dumpCode(t.Logf)
  45. vm.result = _undefined
  46. vm.run()
  47. v := vm.result
  48. t.Logf("stack size: %d", len(vm.stack))
  49. t.Logf("stashAllocs: %d", vm.stashAllocs)
  50. if v == nil && expectedResult != nil || !v.SameAs(expectedResult) {
  51. t.Fatalf("Result: %+v, expected: %+v", v, expectedResult)
  52. }
  53. if vm.sp != 0 {
  54. t.Fatalf("sp: %d", vm.sp)
  55. }
  56. if l := len(vm.iterStack); l > 0 {
  57. t.Fatalf("iter stack is not empty: %d", l)
  58. }
  59. }
  60. func TestEmptyProgram(t *testing.T) {
  61. const SCRIPT = `
  62. `
  63. testScript1(SCRIPT, _undefined, t)
  64. }
  65. func TestResultEmptyBlock(t *testing.T) {
  66. const SCRIPT = `
  67. undefined;
  68. {}
  69. `
  70. testScript1(SCRIPT, _undefined, t)
  71. }
  72. func TestResultVarDecl(t *testing.T) {
  73. const SCRIPT = `
  74. 7; var x = 1;
  75. `
  76. testScript1(SCRIPT, valueInt(7), t)
  77. }
  78. func TestResultLexDecl(t *testing.T) {
  79. const SCRIPT = `
  80. 7; {let x = 1};
  81. `
  82. testScript1(SCRIPT, valueInt(7), t)
  83. }
  84. func TestResultLexDeclBreak(t *testing.T) {
  85. const SCRIPT = `
  86. L:{ 7; {let x = 1; break L;}};
  87. `
  88. testScript1(SCRIPT, valueInt(7), t)
  89. }
  90. func TestResultLexDeclNested(t *testing.T) {
  91. const SCRIPT = `
  92. 7; {let x = (function() { return eval("8; {let y = 9}")})()};
  93. `
  94. testScript1(SCRIPT, valueInt(7), t)
  95. }
  96. func TestErrorProto(t *testing.T) {
  97. const SCRIPT = `
  98. var e = new TypeError();
  99. e.name;
  100. `
  101. testScript1(SCRIPT, asciiString("TypeError"), t)
  102. }
  103. func TestThis1(t *testing.T) {
  104. const SCRIPT = `
  105. function independent() {
  106. return this.prop;
  107. }
  108. var o = {};
  109. o.b = {g: independent, prop: 42};
  110. var rv = o.b.g();
  111. `
  112. testScript(SCRIPT, intToValue(42), t)
  113. }
  114. func TestThis2(t *testing.T) {
  115. const SCRIPT = `
  116. var o = {
  117. prop: 37,
  118. f: function() {
  119. return this.prop;
  120. }
  121. };
  122. var rv = o.f();
  123. `
  124. testScript(SCRIPT, intToValue(37), t)
  125. }
  126. func TestThisStrict(t *testing.T) {
  127. const SCRIPT = `
  128. "use strict";
  129. Object.defineProperty(Object.prototype, "x", { get: function () { return this; } });
  130. (5).x === 5;
  131. `
  132. testScript1(SCRIPT, valueTrue, t)
  133. }
  134. func TestThisNoStrict(t *testing.T) {
  135. const SCRIPT = `
  136. Object.defineProperty(Object.prototype, "x", { get: function () { return this; } });
  137. (5).x == 5;
  138. `
  139. testScript1(SCRIPT, valueTrue, t)
  140. }
  141. func TestNestedFuncVarResolution(t *testing.T) {
  142. const SCRIPT = `
  143. (function outer() {
  144. var v = 42;
  145. function inner() {
  146. return v;
  147. }
  148. return inner();
  149. })();
  150. `
  151. testScript1(SCRIPT, valueInt(42), t)
  152. }
  153. func TestNestedFuncVarResolution1(t *testing.T) {
  154. const SCRIPT = `
  155. function outer(argOuter) {
  156. var called = 0;
  157. var inner = function(argInner) {
  158. if (arguments.length !== 1) {
  159. throw new Error();
  160. }
  161. called++;
  162. if (argOuter !== 1) {
  163. throw new Error("argOuter");
  164. }
  165. if (argInner !== 2) {
  166. throw new Error("argInner");
  167. }
  168. };
  169. inner(2);
  170. }
  171. outer(1);
  172. `
  173. testScript1(SCRIPT, _undefined, t)
  174. }
  175. func TestCallFewerArgs(t *testing.T) {
  176. const SCRIPT = `
  177. function A(a, b, c) {
  178. return String(a) + " " + String(b) + " " + String(c);
  179. }
  180. var rv = A(1, 2);
  181. `
  182. testScript(SCRIPT, asciiString("1 2 undefined"), t)
  183. }
  184. func TestCallFewerArgsClosureNoArgs(t *testing.T) {
  185. const SCRIPT = `
  186. var x;
  187. function A(a, b, c) {
  188. var y = a;
  189. x = function() { return " " + y };
  190. return String(a) + " " + String(b) + " " + String(c);
  191. }
  192. var rv = A(1, 2) + x();
  193. `
  194. testScript(SCRIPT, asciiString("1 2 undefined 1"), t)
  195. }
  196. func TestCallFewerArgsClosureArgs(t *testing.T) {
  197. const SCRIPT = `
  198. var x;
  199. function A(a, b, c) {
  200. var y = b;
  201. x = function() { return " " + a + " " + y };
  202. return String(a) + " " + String(b) + " " + String(c);
  203. }
  204. var rv = A(1, 2) + x();
  205. `
  206. testScript(SCRIPT, asciiString("1 2 undefined 1 2"), t)
  207. }
  208. func TestCallMoreArgs(t *testing.T) {
  209. const SCRIPT = `
  210. function A(a, b) {
  211. var c = 4;
  212. return a - b + c;
  213. }
  214. var rv = A(1, 2, 3);
  215. `
  216. testScript(SCRIPT, intToValue(3), t)
  217. }
  218. func TestCallMoreArgsDynamic(t *testing.T) {
  219. const SCRIPT = `
  220. function A(a, b) {
  221. var c = 4;
  222. if (false) {
  223. eval("");
  224. }
  225. return a - b + c;
  226. }
  227. var rv = A(1, 2, 3);
  228. `
  229. testScript(SCRIPT, intToValue(3), t)
  230. }
  231. func TestCallLessArgsDynamic(t *testing.T) {
  232. const SCRIPT = `
  233. function A(a, b, c) {
  234. // Make it stashful
  235. function B() {
  236. return a;
  237. }
  238. return String(a) + " " + String(b) + " " + String(c);
  239. }
  240. var rv = A(1, 2);
  241. `
  242. testScript(SCRIPT, asciiString("1 2 undefined"), t)
  243. }
  244. func TestCallLessArgsDynamicLocalVar(t *testing.T) {
  245. const SCRIPT = `
  246. function f(param) {
  247. var a = 42;
  248. if (false) {
  249. eval("");
  250. }
  251. return a;
  252. }
  253. f();
  254. `
  255. testScript1(SCRIPT, intToValue(42), t)
  256. }
  257. /*
  258. func TestFib(t *testing.T) {
  259. testScript(TEST_FIB, valueInt(9227465), t)
  260. }
  261. */
  262. func TestNativeCall(t *testing.T) {
  263. const SCRIPT = `
  264. var o = Object(1);
  265. Object.defineProperty(o, "test", {value: 42});
  266. var rv = o.test;
  267. `
  268. testScript(SCRIPT, intToValue(42), t)
  269. }
  270. func TestJSCall(t *testing.T) {
  271. const SCRIPT = `
  272. function getter() {
  273. return this.x;
  274. }
  275. var o = Object(1);
  276. o.x = 42;
  277. Object.defineProperty(o, "test", {get: getter});
  278. var rv = o.test;
  279. `
  280. testScript(SCRIPT, intToValue(42), t)
  281. }
  282. func TestLoop1(t *testing.T) {
  283. const SCRIPT = `
  284. function A() {
  285. var x = 1;
  286. for (var i = 0; i < 1; i++) {
  287. var x = 2;
  288. }
  289. return x;
  290. }
  291. var rv = A();
  292. `
  293. testScript(SCRIPT, intToValue(2), t)
  294. }
  295. func TestLoopBreak(t *testing.T) {
  296. const SCRIPT = `
  297. function A() {
  298. var x = 1;
  299. for (var i = 0; i < 1; i++) {
  300. break;
  301. var x = 2;
  302. }
  303. return x;
  304. }
  305. var rv = A();
  306. `
  307. testScript(SCRIPT, intToValue(1), t)
  308. }
  309. func TestForLoopOptionalExpr(t *testing.T) {
  310. const SCRIPT = `
  311. function A() {
  312. var x = 1;
  313. for (;;) {
  314. break;
  315. var x = 2;
  316. }
  317. return x;
  318. }
  319. var rv = A();
  320. `
  321. testScript(SCRIPT, intToValue(1), t)
  322. }
  323. func TestBlockBreak(t *testing.T) {
  324. const SCRIPT = `
  325. var rv = 0;
  326. B1: {
  327. rv = 1;
  328. B2: {
  329. rv = 2;
  330. break B1;
  331. }
  332. rv = 3;
  333. }
  334. `
  335. testScript(SCRIPT, intToValue(2), t)
  336. }
  337. func TestTry(t *testing.T) {
  338. const SCRIPT = `
  339. function A() {
  340. var x = 1;
  341. try {
  342. x = 2;
  343. } catch(e) {
  344. x = 3;
  345. } finally {
  346. x = 4;
  347. }
  348. return x;
  349. }
  350. var rv = A();
  351. `
  352. testScript(SCRIPT, intToValue(4), t)
  353. }
  354. func TestTryOptionalCatchBinding(t *testing.T) {
  355. const SCRIPT = `
  356. try {
  357. throw null;
  358. } catch {
  359. }
  360. `
  361. testScript1(SCRIPT, _undefined, t)
  362. }
  363. func TestTryCatch(t *testing.T) {
  364. const SCRIPT = `
  365. function A() {
  366. var x;
  367. try {
  368. throw 4;
  369. } catch(e) {
  370. x = e;
  371. }
  372. return x;
  373. }
  374. var rv = A();
  375. `
  376. testScript(SCRIPT, intToValue(4), t)
  377. }
  378. func TestTryCatchDirectEval(t *testing.T) {
  379. const SCRIPT = `
  380. function A() {
  381. var x;
  382. try {
  383. throw 4;
  384. } catch(e) {
  385. eval("x = e");
  386. }
  387. return x;
  388. }
  389. var rv = A();
  390. `
  391. testScript(SCRIPT, intToValue(4), t)
  392. }
  393. func TestTryExceptionInCatch(t *testing.T) {
  394. const SCRIPT = `
  395. function A() {
  396. var x;
  397. try {
  398. throw 4;
  399. } catch(e) {
  400. throw 5;
  401. }
  402. return x;
  403. }
  404. var rv;
  405. try {
  406. A();
  407. } catch (e) {
  408. rv = e;
  409. }
  410. `
  411. testScript(SCRIPT, intToValue(5), t)
  412. }
  413. func TestTryContinueInCatch(t *testing.T) {
  414. const SCRIPT = `
  415. var c3 = 0, fin3 = 0;
  416. while (c3 < 2) {
  417. try {
  418. throw "ex1";
  419. } catch(er1) {
  420. c3 += 1;
  421. continue;
  422. } finally {
  423. fin3 = 1;
  424. }
  425. fin3 = 0;
  426. }
  427. fin3;
  428. `
  429. testScript1(SCRIPT, intToValue(1), t)
  430. }
  431. func TestContinueInWith(t *testing.T) {
  432. const SCRIPT = `
  433. var x;
  434. var o = {x: 0};
  435. for (var i = 0; i < 2; i++) {
  436. with(o) {
  437. x = i;
  438. if (i === 0) {
  439. continue;
  440. }
  441. }
  442. break;
  443. }
  444. x;
  445. `
  446. testScript1(SCRIPT, _undefined, t)
  447. }
  448. func TestTryContinueInFinally(t *testing.T) {
  449. const SCRIPT = `
  450. var c3 = 0, fin3 = 0;
  451. while (c3 < 2) {
  452. try {
  453. throw "ex1";
  454. } catch(er1) {
  455. c3 += 1;
  456. } finally {
  457. fin3 = 1;
  458. continue;
  459. }
  460. fin3 = 0;
  461. }
  462. fin3;
  463. `
  464. testScript1(SCRIPT, intToValue(1), t)
  465. }
  466. func TestTryBreakFinallyContinue(t *testing.T) {
  467. const SCRIPT = `
  468. for (var i = 0; i < 3; i++) {
  469. try {
  470. break;
  471. } finally {
  472. continue;
  473. }
  474. }
  475. `
  476. testScript1(SCRIPT, _undefined, t)
  477. }
  478. func TestTryBreakFinallyContinueWithResult(t *testing.T) {
  479. const SCRIPT = `
  480. for (var i = 0; i < 3; i++) {
  481. try {
  482. true;
  483. break;
  484. } finally {
  485. continue;
  486. }
  487. }
  488. `
  489. testScript1(SCRIPT, valueTrue, t)
  490. }
  491. func TestTryBreakFinallyContinueWithResult1(t *testing.T) {
  492. const SCRIPT = `
  493. for (var i = 0; i < 3; i++) {
  494. try {
  495. true;
  496. break;
  497. } finally {
  498. var x = 1;
  499. continue;
  500. }
  501. }
  502. `
  503. testScript1(SCRIPT, valueTrue, t)
  504. }
  505. func TestTryBreakFinallyContinueWithResultNested(t *testing.T) {
  506. const SCRIPT = `
  507. LOOP:
  508. for (var i = 0; i < 3; i++) {
  509. try {
  510. if (true) {
  511. false; break;
  512. }
  513. } finally {
  514. if (true) {
  515. true; continue;
  516. }
  517. }
  518. }
  519. `
  520. testScript1(SCRIPT, valueTrue, t)
  521. }
  522. func TestTryBreakOuterFinallyContinue(t *testing.T) {
  523. const SCRIPT = `
  524. let iCount = 0, jCount = 0;
  525. OUTER: for (let i = 0; i < 1; i++) {
  526. iCount++;
  527. for (let j = 0; j < 2; j++) {
  528. jCount++;
  529. try {
  530. break OUTER;
  531. } finally {
  532. continue;
  533. }
  534. }
  535. }
  536. ""+iCount+jCount;
  537. `
  538. testScript1(SCRIPT, asciiString("12"), t)
  539. }
  540. func TestTryIllegalContinueWithFinallyOverride(t *testing.T) {
  541. const SCRIPT = `
  542. L: {
  543. while (Math.random() > 0.5) {
  544. try {
  545. continue L;
  546. } finally {
  547. break;
  548. }
  549. }
  550. }
  551. `
  552. _, err := Compile("", SCRIPT, false)
  553. if err == nil {
  554. t.Fatal("expected error")
  555. }
  556. }
  557. func TestTryIllegalContinueWithFinallyOverrideNoLabel(t *testing.T) {
  558. const SCRIPT = `
  559. L: {
  560. try {
  561. continue;
  562. } finally {
  563. break L;
  564. }
  565. }
  566. `
  567. _, err := Compile("", SCRIPT, false)
  568. if err == nil {
  569. t.Fatal("expected error")
  570. }
  571. }
  572. func TestTryIllegalContinueWithFinallyOverrideDummy(t *testing.T) {
  573. const SCRIPT = `
  574. L: {
  575. while (false) {
  576. try {
  577. continue L;
  578. } finally {
  579. break;
  580. }
  581. }
  582. }
  583. `
  584. _, err := Compile("", SCRIPT, false)
  585. if err == nil {
  586. t.Fatal("expected error")
  587. }
  588. }
  589. func TestTryNoResult(t *testing.T) {
  590. const SCRIPT = `
  591. true;
  592. L:
  593. try {
  594. break L;
  595. } finally {
  596. }
  597. `
  598. testScript1(SCRIPT, _undefined, t)
  599. }
  600. func TestCatchLexicalEnv(t *testing.T) {
  601. const SCRIPT = `
  602. function F() {
  603. try {
  604. throw 1;
  605. } catch (e) {
  606. var x = e;
  607. }
  608. return x;
  609. }
  610. F();
  611. `
  612. testScript1(SCRIPT, intToValue(1), t)
  613. }
  614. func TestThrowType(t *testing.T) {
  615. const SCRIPT = `
  616. function Exception(message) {
  617. this.message = message;
  618. }
  619. function A() {
  620. try {
  621. throw new Exception("boo!");
  622. } catch(e) {
  623. return e;
  624. }
  625. }
  626. var thrown = A();
  627. var rv = thrown !== null && typeof thrown === "object" && thrown.constructor === Exception;
  628. `
  629. testScript(SCRIPT, valueTrue, t)
  630. }
  631. func TestThrowConstructorName(t *testing.T) {
  632. const SCRIPT = `
  633. function Exception(message) {
  634. this.message = message;
  635. }
  636. function A() {
  637. try {
  638. throw new Exception("boo!");
  639. } catch(e) {
  640. return e;
  641. }
  642. }
  643. A().constructor.name;
  644. `
  645. testScript1(SCRIPT, asciiString("Exception"), t)
  646. }
  647. func TestThrowNativeConstructorName(t *testing.T) {
  648. const SCRIPT = `
  649. function A() {
  650. try {
  651. throw new TypeError();
  652. } catch(e) {
  653. return e;
  654. }
  655. }
  656. A().constructor.name;
  657. `
  658. testScript1(SCRIPT, asciiString("TypeError"), t)
  659. }
  660. func TestEmptyTryNoCatch(t *testing.T) {
  661. const SCRIPT = `
  662. var called = false;
  663. try {
  664. } finally {
  665. called = true;
  666. }
  667. called;
  668. `
  669. testScript1(SCRIPT, valueTrue, t)
  670. }
  671. func TestTryReturnFromCatch(t *testing.T) {
  672. const SCRIPT = `
  673. function f(o) {
  674. var x = 42;
  675. function innerf(o) {
  676. try {
  677. throw o;
  678. } catch (e) {
  679. return x;
  680. }
  681. }
  682. return innerf(o);
  683. }
  684. f({});
  685. `
  686. testScript1(SCRIPT, valueInt(42), t)
  687. }
  688. func TestIfElse(t *testing.T) {
  689. const SCRIPT = `
  690. var rv;
  691. if (rv === undefined) {
  692. rv = "passed";
  693. } else {
  694. rv = "failed";
  695. }
  696. `
  697. testScript(SCRIPT, asciiString("passed"), t)
  698. }
  699. func TestIfElseRetVal(t *testing.T) {
  700. const SCRIPT = `
  701. var x;
  702. if (x === undefined) {
  703. "passed";
  704. } else {
  705. "failed";
  706. }
  707. `
  708. testScript1(SCRIPT, asciiString("passed"), t)
  709. }
  710. func TestWhileReturnValue(t *testing.T) {
  711. const SCRIPT = `
  712. var x = 0;
  713. while(true) {
  714. x = 1;
  715. break;
  716. }
  717. `
  718. testScript1(SCRIPT, intToValue(1), t)
  719. }
  720. func TestIfElseLabel(t *testing.T) {
  721. const SCRIPT = `
  722. var x = 0;
  723. abc: if (true) {
  724. x = 1;
  725. break abc;
  726. }
  727. `
  728. testScript1(SCRIPT, intToValue(1), t)
  729. }
  730. func TestIfMultipleLabels(t *testing.T) {
  731. const SCRIPT = `
  732. var x = 0;
  733. xyz:abc: if (true) {
  734. break xyz;
  735. }
  736. `
  737. testScript1(SCRIPT, _undefined, t)
  738. }
  739. func TestBreakOutOfTry(t *testing.T) {
  740. const SCRIPT = `
  741. function A() {
  742. var x = 1;
  743. B: {
  744. try {
  745. x = 2;
  746. } catch(e) {
  747. x = 3;
  748. } finally {
  749. break B;
  750. x = 4;
  751. }
  752. }
  753. return x;
  754. }
  755. A();
  756. `
  757. testScript1(SCRIPT, intToValue(2), t)
  758. }
  759. func TestReturnOutOfTryNested(t *testing.T) {
  760. const SCRIPT = `
  761. function A() {
  762. function nested() {
  763. try {
  764. return 1;
  765. } catch(e) {
  766. return 2;
  767. }
  768. }
  769. return nested();
  770. }
  771. A();
  772. `
  773. testScript1(SCRIPT, intToValue(1), t)
  774. }
  775. func TestContinueLoop(t *testing.T) {
  776. const SCRIPT = `
  777. function A() {
  778. var r = 0;
  779. for (var i = 0; i < 5; i++) {
  780. if (i > 1) {
  781. continue;
  782. }
  783. r++;
  784. }
  785. return r;
  786. }
  787. A();
  788. `
  789. testScript1(SCRIPT, intToValue(2), t)
  790. }
  791. func TestContinueOutOfTry(t *testing.T) {
  792. const SCRIPT = `
  793. function A() {
  794. var r = 0;
  795. for (var i = 0; i < 5; i++) {
  796. try {
  797. if (i > 1) {
  798. continue;
  799. }
  800. } catch(e) {
  801. return 99;
  802. }
  803. r++;
  804. }
  805. return r;
  806. }
  807. A();
  808. `
  809. testScript1(SCRIPT, intToValue(2), t)
  810. }
  811. func TestThisInCatch(t *testing.T) {
  812. const SCRIPT = `
  813. function O() {
  814. try {
  815. f();
  816. } catch (e) {
  817. this.value = e.toString();
  818. }
  819. }
  820. function f() {
  821. throw "ex";
  822. }
  823. var o = new O();
  824. o.value;
  825. `
  826. testScript1(SCRIPT, asciiString("ex"), t)
  827. }
  828. func TestNestedTry(t *testing.T) {
  829. const SCRIPT = `
  830. var ex;
  831. try {
  832. throw "ex1";
  833. } catch (er1) {
  834. try {
  835. throw "ex2";
  836. } catch (er1) {
  837. ex = er1;
  838. }
  839. }
  840. ex;
  841. `
  842. testScript1(SCRIPT, asciiString("ex2"), t)
  843. }
  844. func TestNestedTryInStashlessFunc(t *testing.T) {
  845. const SCRIPT = `
  846. function f() {
  847. var ex1, ex2;
  848. try {
  849. throw "ex1";
  850. } catch (er1) {
  851. try {
  852. throw "ex2";
  853. } catch (er1) {
  854. ex2 = er1;
  855. }
  856. ex1 = er1;
  857. }
  858. return ex1 == "ex1" && ex2 == "ex2";
  859. }
  860. f();
  861. `
  862. testScript1(SCRIPT, valueTrue, t)
  863. }
  864. func TestEvalLexicalDecl(t *testing.T) {
  865. const SCRIPT = `
  866. eval("let x = true; x;");
  867. `
  868. testScript1(SCRIPT, valueTrue, t)
  869. }
  870. func TestEvalInCatchInStashlessFunc(t *testing.T) {
  871. const SCRIPT = `
  872. function f() {
  873. var ex;
  874. try {
  875. throw "ex1";
  876. } catch (er1) {
  877. eval("ex = er1");
  878. }
  879. return ex;
  880. }
  881. f();
  882. `
  883. testScript1(SCRIPT, asciiString("ex1"), t)
  884. }
  885. func TestCatchClosureInStashlessFunc(t *testing.T) {
  886. const SCRIPT = `
  887. function f() {
  888. var ex;
  889. try {
  890. throw "ex1";
  891. } catch (er1) {
  892. return function() {
  893. return er1;
  894. }
  895. }
  896. }
  897. f()();
  898. `
  899. testScript1(SCRIPT, asciiString("ex1"), t)
  900. }
  901. func TestCatchVarNotUsedInStashlessFunc(t *testing.T) {
  902. const SCRIPT = `
  903. function f() {
  904. var ex;
  905. try {
  906. throw "ex1";
  907. } catch (er1) {
  908. ex = "ok";
  909. }
  910. return ex;
  911. }
  912. f();
  913. `
  914. testScript1(SCRIPT, asciiString("ok"), t)
  915. }
  916. func TestNew(t *testing.T) {
  917. const SCRIPT = `
  918. function O() {
  919. this.x = 42;
  920. }
  921. new O().x;
  922. `
  923. testScript1(SCRIPT, intToValue(42), t)
  924. }
  925. func TestStringConstructor(t *testing.T) {
  926. const SCRIPT = `
  927. function F() {
  928. return String(33) + " " + String("cows");
  929. }
  930. F();
  931. `
  932. testScript1(SCRIPT, asciiString("33 cows"), t)
  933. }
  934. func TestError(t *testing.T) {
  935. const SCRIPT = `
  936. function F() {
  937. return new Error("test");
  938. }
  939. var e = F();
  940. var rv = e.message == "test" && e.name == "Error";
  941. `
  942. testScript(SCRIPT, valueTrue, t)
  943. }
  944. func TestTypeError(t *testing.T) {
  945. const SCRIPT = `
  946. function F() {
  947. return new TypeError("test");
  948. }
  949. var e = F();
  950. e.message == "test" && e.name == "TypeError";
  951. `
  952. testScript1(SCRIPT, valueTrue, t)
  953. }
  954. func TestToString(t *testing.T) {
  955. const SCRIPT = `
  956. var o = {x: 42};
  957. o.toString = function() {
  958. return String(this.x);
  959. }
  960. var o1 = {};
  961. o.toString() + " ### " + o1.toString();
  962. `
  963. testScript1(SCRIPT, asciiString("42 ### [object Object]"), t)
  964. }
  965. func TestEvalOrder(t *testing.T) {
  966. const SCRIPT = `
  967. var o = {f: function() {return 42}, x: 0};
  968. var trace = "";
  969. function F1() {
  970. trace += "First!";
  971. return o;
  972. }
  973. function F2() {
  974. trace += "Second!";
  975. return "f";
  976. }
  977. function F3() {
  978. trace += "Third!";
  979. }
  980. var rv = F1()[F2()](F3());
  981. rv += trace;
  982. `
  983. testScript(SCRIPT, asciiString("42First!Second!Third!"), t)
  984. }
  985. func TestPostfixIncBracket(t *testing.T) {
  986. const SCRIPT = `
  987. var o = {x: 42};
  988. var trace = "";
  989. function F1() {
  990. trace += "First!";
  991. return o;
  992. }
  993. function F2() {
  994. trace += "Second!";
  995. return "x";
  996. }
  997. var rv = F1()[F2()]++;
  998. rv += trace + o.x;
  999. `
  1000. testScript(SCRIPT, asciiString("42First!Second!43"), t)
  1001. }
  1002. func TestPostfixIncDot(t *testing.T) {
  1003. const SCRIPT = `
  1004. var o = {x: 42};
  1005. var trace = "";
  1006. function F1() {
  1007. trace += "First!";
  1008. return o;
  1009. }
  1010. var rv = F1().x++;
  1011. rv += trace + o.x;
  1012. `
  1013. testScript(SCRIPT, asciiString("42First!43"), t)
  1014. }
  1015. func TestPrefixIncBracket(t *testing.T) {
  1016. const SCRIPT = `
  1017. var o = {x: 42};
  1018. var trace = "";
  1019. function F1() {
  1020. trace += "First!";
  1021. return o;
  1022. }
  1023. function F2() {
  1024. trace += "Second!";
  1025. return "x";
  1026. }
  1027. var rv = ++F1()[F2()];
  1028. rv += trace + o.x;
  1029. `
  1030. testScript(SCRIPT, asciiString("43First!Second!43"), t)
  1031. }
  1032. func TestPrefixIncDot(t *testing.T) {
  1033. const SCRIPT = `
  1034. var o = {x: 42};
  1035. var trace = "";
  1036. function F1() {
  1037. trace += "First!";
  1038. return o;
  1039. }
  1040. var rv = ++F1().x;
  1041. rv += trace + o.x;
  1042. `
  1043. testScript(SCRIPT, asciiString("43First!43"), t)
  1044. }
  1045. func TestPostDecObj(t *testing.T) {
  1046. const SCRIPT = `
  1047. var object = {valueOf: function() {return 1}};
  1048. var y = object--;
  1049. var ok = false;
  1050. if (y === 1) {
  1051. ok = true;
  1052. }
  1053. ok;
  1054. `
  1055. testScript1(SCRIPT, valueTrue, t)
  1056. }
  1057. func TestPropAcc1(t *testing.T) {
  1058. const SCRIPT = `
  1059. 1..toString()
  1060. `
  1061. testScript1(SCRIPT, asciiString("1"), t)
  1062. }
  1063. func TestEvalDirect(t *testing.T) {
  1064. const SCRIPT = `
  1065. var rv = false;
  1066. function foo(){ rv = true; }
  1067. var o = { };
  1068. function f() {
  1069. try {
  1070. eval("o.bar( foo() );");
  1071. } catch (e) {
  1072. }
  1073. }
  1074. f();
  1075. `
  1076. testScript(SCRIPT, valueTrue, t)
  1077. }
  1078. func TestEvalRet(t *testing.T) {
  1079. const SCRIPT = `
  1080. eval("for (var i = 0; i < 3; i++) {i}")
  1081. `
  1082. testScript1(SCRIPT, valueInt(2), t)
  1083. }
  1084. func TestEvalFunctionDecl(t *testing.T) {
  1085. const SCRIPT = `
  1086. eval("function F() {}")
  1087. `
  1088. testScript1(SCRIPT, _undefined, t)
  1089. }
  1090. func TestEvalFunctionExpr(t *testing.T) {
  1091. const SCRIPT = `
  1092. eval("(function F() {return 42;})")()
  1093. `
  1094. testScript1(SCRIPT, intToValue(42), t)
  1095. }
  1096. func TestEvalDirectScope(t *testing.T) {
  1097. const SCRIPT = `
  1098. var __10_4_2_1_3 = "str";
  1099. function testcase() {
  1100. var __10_4_2_1_3 = "str1";
  1101. try {
  1102. throw "error";
  1103. } catch (e) {
  1104. var __10_4_2_1_3 = "str2";
  1105. return eval("__10_4_2_1_3");
  1106. }
  1107. }
  1108. testcase();
  1109. `
  1110. testScript1(SCRIPT, asciiString("str2"), t)
  1111. }
  1112. func TestEvalDirectScope1(t *testing.T) {
  1113. const SCRIPT = `
  1114. 'use strict';
  1115. var __10_4_2_1_5 = "str";
  1116. function testcase() {
  1117. var __10_4_2_1_5 = "str1";
  1118. var r = eval("\
  1119. var __10_4_2_1_5 = \'str2\'; \
  1120. eval(\"\'str2\' === __10_4_2_1_5\")\
  1121. ");
  1122. return r;
  1123. }
  1124. testcase();
  1125. `
  1126. testScript1(SCRIPT, valueTrue, t)
  1127. }
  1128. func TestEvalDirectCreateBinding(t *testing.T) {
  1129. const SCRIPT = `
  1130. function f() {
  1131. eval("var x = true");
  1132. return x;
  1133. }
  1134. var res = f();
  1135. var thrown = false;
  1136. try {
  1137. x;
  1138. } catch(e) {
  1139. if (e instanceof ReferenceError) {
  1140. thrown = true;
  1141. } else {
  1142. throw e;
  1143. }
  1144. }
  1145. res && thrown;
  1146. `
  1147. testScript1(SCRIPT, valueTrue, t)
  1148. }
  1149. func TestEvalDirectCreateBinding1(t *testing.T) {
  1150. const SCRIPT = `
  1151. function f() {
  1152. eval("let x = 1; var y = 2; function f1() {return x};");
  1153. assert.throws(ReferenceError, function() { x });
  1154. return ""+y+f1();
  1155. }
  1156. f();
  1157. `
  1158. testScript1(TESTLIB+SCRIPT, asciiString("21"), t)
  1159. }
  1160. func TestEvalDirectCreateBinding3(t *testing.T) {
  1161. const SCRIPT = `
  1162. function f() {
  1163. let x;
  1164. try {
  1165. eval("var y=1, x=2");
  1166. } catch(e) {}
  1167. return y;
  1168. }
  1169. assert.throws(ReferenceError, f);
  1170. `
  1171. testScript1(TESTLIB+SCRIPT, _undefined, t)
  1172. }
  1173. func TestEvalGlobalStrict(t *testing.T) {
  1174. const SCRIPT = `
  1175. 'use strict';
  1176. var evalStr =
  1177. 'for (var x in this) {\n'+
  1178. ' if ( x === \'Math\' ) {\n'+
  1179. ' }\n'+
  1180. '}\n';
  1181. eval(evalStr);
  1182. `
  1183. testScript1(SCRIPT, _undefined, t)
  1184. }
  1185. func TestEvalEmptyStrict(t *testing.T) {
  1186. const SCRIPT = `
  1187. 'use strict';
  1188. eval("");
  1189. `
  1190. testScript1(SCRIPT, _undefined, t)
  1191. }
  1192. func TestEvalFuncDecl(t *testing.T) {
  1193. const SCRIPT = `
  1194. 'use strict';
  1195. var funcA = eval("function __funcA(__arg){return __arg;}; __funcA");
  1196. typeof funcA;
  1197. `
  1198. testScript1(SCRIPT, asciiString("function"), t)
  1199. }
  1200. func TestGetAfterSet(t *testing.T) {
  1201. const SCRIPT = `
  1202. function f() {
  1203. var x = 1;
  1204. return x;
  1205. }
  1206. `
  1207. testScript1(SCRIPT, _undefined, t)
  1208. }
  1209. func TestForLoopRet(t *testing.T) {
  1210. const SCRIPT = `
  1211. for (var i = 0; i < 20; i++) { if (i > 2) {break;} else { i }}
  1212. `
  1213. testScript1(SCRIPT, _undefined, t)
  1214. }
  1215. func TestForLoopRet1(t *testing.T) {
  1216. const SCRIPT = `
  1217. for (var i = 0; i < 20; i++) { if (i > 2) {42;; {L:{break;}}} else { i }}
  1218. `
  1219. testScript1(SCRIPT, intToValue(42), t)
  1220. }
  1221. func TestForInLoopRet(t *testing.T) {
  1222. const SCRIPT = `
  1223. var o = [1, 2, 3, 4];
  1224. for (var i in o) { if (i > 2) {break;} else { i }}
  1225. `
  1226. testScript1(SCRIPT, _undefined, t)
  1227. }
  1228. func TestForInLoopRet1(t *testing.T) {
  1229. const SCRIPT = `
  1230. var o = {};
  1231. o.x = 1;
  1232. o.y = 2;
  1233. for (var i in o) {
  1234. true;
  1235. }
  1236. `
  1237. testScript1(SCRIPT, valueTrue, t)
  1238. }
  1239. func TestDoWhileLoopRet(t *testing.T) {
  1240. const SCRIPT = `
  1241. var i = 0;
  1242. do {
  1243. if (i > 2) {
  1244. break;
  1245. } else {
  1246. i;
  1247. }
  1248. } while (i++ < 20);
  1249. `
  1250. testScript1(SCRIPT, _undefined, t)
  1251. }
  1252. func TestDoWhileContinueRet(t *testing.T) {
  1253. const SCRIPT = `
  1254. var i = 0;
  1255. do {
  1256. if (i > 2) {
  1257. true;
  1258. continue;
  1259. } else {
  1260. i;
  1261. }
  1262. } while (i++ < 20);
  1263. `
  1264. testScript1(SCRIPT, valueTrue, t)
  1265. }
  1266. func TestWhileLoopRet(t *testing.T) {
  1267. const SCRIPT = `
  1268. var i; while (i < 20) { if (i > 2) {break;} else { i++ }}
  1269. `
  1270. testScript1(SCRIPT, _undefined, t)
  1271. }
  1272. func TestLoopRet1(t *testing.T) {
  1273. const SCRIPT = `
  1274. for (var i = 0; i < 20; i++) { }
  1275. `
  1276. testScript1(SCRIPT, _undefined, t)
  1277. }
  1278. func TestInstanceof(t *testing.T) {
  1279. const SCRIPT = `
  1280. var rv;
  1281. try {
  1282. true();
  1283. } catch (e) {
  1284. rv = e instanceof TypeError;
  1285. }
  1286. `
  1287. testScript(SCRIPT, valueTrue, t)
  1288. }
  1289. func TestStrictAssign(t *testing.T) {
  1290. const SCRIPT = `
  1291. 'use strict';
  1292. var rv;
  1293. var called = false;
  1294. function F() {
  1295. called = true;
  1296. return 1;
  1297. }
  1298. try {
  1299. x = F();
  1300. } catch (e) {
  1301. rv = e instanceof ReferenceError;
  1302. }
  1303. rv += " " + called;
  1304. `
  1305. testScript(SCRIPT, asciiString("true true"), t)
  1306. }
  1307. func TestStrictScope(t *testing.T) {
  1308. const SCRIPT = `
  1309. var rv;
  1310. var called = false;
  1311. function F() {
  1312. 'use strict';
  1313. x = 1;
  1314. }
  1315. try {
  1316. F();
  1317. } catch (e) {
  1318. rv = e instanceof ReferenceError;
  1319. }
  1320. x = 1;
  1321. rv += " " + x;
  1322. `
  1323. testScript(SCRIPT, asciiString("true 1"), t)
  1324. }
  1325. func TestStringObj(t *testing.T) {
  1326. const SCRIPT = `
  1327. var s = new String("test");
  1328. s[0] + s[2] + s[1];
  1329. `
  1330. testScript1(SCRIPT, asciiString("tse"), t)
  1331. }
  1332. func TestStringPrimitive(t *testing.T) {
  1333. const SCRIPT = `
  1334. var s = "test";
  1335. s[0] + s[2] + s[1];
  1336. `
  1337. testScript1(SCRIPT, asciiString("tse"), t)
  1338. }
  1339. func TestCallGlobalObject(t *testing.T) {
  1340. const SCRIPT = `
  1341. var rv;
  1342. try {
  1343. this();
  1344. } catch (e) {
  1345. rv = e instanceof TypeError
  1346. }
  1347. `
  1348. testScript(SCRIPT, valueTrue, t)
  1349. }
  1350. func TestFuncLength(t *testing.T) {
  1351. const SCRIPT = `
  1352. function F(x, y) {
  1353. }
  1354. F.length
  1355. `
  1356. testScript1(SCRIPT, intToValue(2), t)
  1357. }
  1358. func TestNativeFuncLength(t *testing.T) {
  1359. const SCRIPT = `
  1360. eval.length + Object.defineProperty.length + String.length
  1361. `
  1362. testScript1(SCRIPT, intToValue(5), t)
  1363. }
  1364. func TestArguments(t *testing.T) {
  1365. const SCRIPT = `
  1366. function F() {
  1367. return arguments.length + " " + arguments[1];
  1368. }
  1369. F(1,2,3)
  1370. `
  1371. testScript1(SCRIPT, asciiString("3 2"), t)
  1372. }
  1373. func TestArgumentsPut(t *testing.T) {
  1374. const SCRIPT = `
  1375. function F(x, y) {
  1376. arguments[0] -= arguments[1];
  1377. return x;
  1378. }
  1379. F(5, 2)
  1380. `
  1381. testScript1(SCRIPT, intToValue(3), t)
  1382. }
  1383. func TestArgumentsPutStrict(t *testing.T) {
  1384. const SCRIPT = `
  1385. function F(x, y) {
  1386. 'use strict';
  1387. arguments[0] -= arguments[1];
  1388. return x;
  1389. }
  1390. F(5, 2)
  1391. `
  1392. testScript1(SCRIPT, intToValue(5), t)
  1393. }
  1394. func TestArgumentsExtra(t *testing.T) {
  1395. const SCRIPT = `
  1396. function F(x, y) {
  1397. return arguments[2];
  1398. }
  1399. F(1, 2, 42)
  1400. `
  1401. testScript1(SCRIPT, intToValue(42), t)
  1402. }
  1403. func TestArgumentsExist(t *testing.T) {
  1404. const SCRIPT = `
  1405. function F(x, arguments) {
  1406. return arguments;
  1407. }
  1408. F(1, 42)
  1409. `
  1410. testScript1(SCRIPT, intToValue(42), t)
  1411. }
  1412. func TestArgumentsDelete(t *testing.T) {
  1413. const SCRIPT = `
  1414. function f(x) {
  1415. delete arguments[0];
  1416. arguments[0] = 42;
  1417. return x;
  1418. }
  1419. f(1)
  1420. `
  1421. testScript1(SCRIPT, intToValue(1), t)
  1422. }
  1423. func TestArgumentsInEval(t *testing.T) {
  1424. const SCRIPT = `
  1425. function f() {
  1426. return eval("arguments");
  1427. }
  1428. f(1)[0];
  1429. `
  1430. testScript1(SCRIPT, intToValue(1), t)
  1431. }
  1432. func TestArgumentsRedeclareInEval(t *testing.T) {
  1433. const SCRIPT = `
  1434. assert.sameValue("arguments" in this, false, "No global 'arguments' binding");
  1435. function f(p = eval("var arguments = 'param'"), arguments) {}
  1436. assert.throws(SyntaxError, f);
  1437. assert.sameValue("arguments" in this, false, "No global 'arguments' binding");
  1438. `
  1439. testScript1(TESTLIB+SCRIPT, _undefined, t)
  1440. }
  1441. func TestArgumentsRedeclareArrow(t *testing.T) {
  1442. const SCRIPT = `
  1443. const oldArguments = globalThis.arguments;
  1444. let count = 0;
  1445. const f = (p = eval("var arguments = 'param'"), q = () => arguments) => {
  1446. var arguments = "local";
  1447. assert.sameValue(arguments, "local", "arguments");
  1448. assert.sameValue(q(), "param", "q");
  1449. count++;
  1450. }
  1451. f();
  1452. assert.sameValue(count, 1);
  1453. assert.sameValue(globalThis.arguments, oldArguments, "globalThis.arguments unchanged");
  1454. `
  1455. testScript1(TESTLIB+SCRIPT, _undefined, t)
  1456. }
  1457. func TestEvalParamWithDef(t *testing.T) {
  1458. const SCRIPT = `
  1459. function f(param = 0) {
  1460. eval("var param = 1");
  1461. return param;
  1462. }
  1463. f();
  1464. `
  1465. testScript1(SCRIPT, valueInt(1), t)
  1466. }
  1467. func TestArgumentsRedefinedAsLetDyn(t *testing.T) {
  1468. const SCRIPT = `
  1469. function f() {
  1470. let arguments;
  1471. eval(""); // force dynamic scope
  1472. return arguments;
  1473. }
  1474. f(1,2);
  1475. `
  1476. testScript1(SCRIPT, _undefined, t)
  1477. }
  1478. func TestWith(t *testing.T) {
  1479. const SCRIPT = `
  1480. var b = 1;
  1481. var o = {a: 41};
  1482. with(o) {
  1483. a += b;
  1484. }
  1485. o.a;
  1486. `
  1487. testScript1(SCRIPT, intToValue(42), t)
  1488. }
  1489. func TestWithInFunc(t *testing.T) {
  1490. const SCRIPT = `
  1491. function F() {
  1492. var b = 1;
  1493. var c = 0;
  1494. var o = {a: 40, c: 1};
  1495. with(o) {
  1496. a += b + c;
  1497. }
  1498. return o.a;
  1499. }
  1500. F();
  1501. `
  1502. testScript1(SCRIPT, intToValue(42), t)
  1503. }
  1504. func TestAssignNonExtendable(t *testing.T) {
  1505. const SCRIPT = `
  1506. 'use strict';
  1507. function F() {
  1508. this.x = 1;
  1509. }
  1510. var o = new F();
  1511. Object.preventExtensions(o);
  1512. o.x = 42;
  1513. o.x;
  1514. `
  1515. testScript1(SCRIPT, intToValue(42), t)
  1516. }
  1517. func TestAssignNonExtendable1(t *testing.T) {
  1518. const SCRIPT = `
  1519. 'use strict';
  1520. function F() {
  1521. }
  1522. var o = new F();
  1523. var rv;
  1524. Object.preventExtensions(o);
  1525. try {
  1526. o.x = 42;
  1527. } catch (e) {
  1528. rv = e.constructor === TypeError;
  1529. }
  1530. rv += " " + o.x;
  1531. `
  1532. testScript(SCRIPT, asciiString("true undefined"), t)
  1533. }
  1534. func TestAssignStrict(t *testing.T) {
  1535. const SCRIPT = `
  1536. 'use strict';
  1537. try {
  1538. eval("eval = 42");
  1539. } catch(e) {
  1540. var rv = e instanceof SyntaxError
  1541. }
  1542. `
  1543. testScript(SCRIPT, valueTrue, t)
  1544. }
  1545. func TestIllegalArgmentName(t *testing.T) {
  1546. const SCRIPT = `
  1547. 'use strict';
  1548. try {
  1549. eval("function F(eval) {}");
  1550. } catch (e) {
  1551. var rv = e instanceof SyntaxError
  1552. }
  1553. `
  1554. testScript(SCRIPT, valueTrue, t)
  1555. }
  1556. func TestFunction(t *testing.T) {
  1557. const SCRIPT = `
  1558. var f0 = Function("");
  1559. var f1 = Function("return ' one'");
  1560. var f2 = Function("arg", "return ' ' + arg");
  1561. f0() + f1() + f2("two");
  1562. `
  1563. testScript1(SCRIPT, asciiString("undefined one two"), t)
  1564. }
  1565. func TestFunction1(t *testing.T) {
  1566. const SCRIPT = `
  1567. var f = function f1(count) {
  1568. if (count == 0) {
  1569. return true;
  1570. }
  1571. return f1(count-1);
  1572. }
  1573. f(1);
  1574. `
  1575. testScript1(SCRIPT, valueTrue, t)
  1576. }
  1577. func TestFunction2(t *testing.T) {
  1578. const SCRIPT = `
  1579. var trace = "";
  1580. function f(count) {
  1581. trace += "f("+count+")";
  1582. if (count == 0) {
  1583. return;
  1584. }
  1585. return f(count-1);
  1586. }
  1587. function f1() {
  1588. trace += "f1";
  1589. }
  1590. var f2 = f;
  1591. f = f1;
  1592. f2(1);
  1593. trace;
  1594. `
  1595. testScript1(SCRIPT, asciiString("f(1)f1"), t)
  1596. }
  1597. func TestFunctionToString(t *testing.T) {
  1598. const SCRIPT = `
  1599. Function("arg1", "arg2", "return 42").toString();
  1600. `
  1601. testScript1(SCRIPT, asciiString("function anonymous(arg1,arg2){return 42}"), t)
  1602. }
  1603. func TestObjectLiteral(t *testing.T) {
  1604. const SCRIPT = `
  1605. var getterCalled = false;
  1606. var setterCalled = false;
  1607. var o = {get x() {getterCalled = true}, set x() {setterCalled = true}};
  1608. o.x;
  1609. o.x = 42;
  1610. getterCalled && setterCalled;
  1611. `
  1612. testScript1(SCRIPT, valueTrue, t)
  1613. }
  1614. func TestConst(t *testing.T) {
  1615. const SCRIPT = `
  1616. var v1 = true && true;
  1617. var v2 = 1/(-1 * 0);
  1618. var v3 = 1 == 2 || v1;
  1619. var v4 = true && false
  1620. v1 === true && v2 === -Infinity && v3 === v1 && v4 === false;
  1621. `
  1622. testScript1(SCRIPT, valueTrue, t)
  1623. }
  1624. func TestConstWhile(t *testing.T) {
  1625. const SCRIPT = `
  1626. var c = 0;
  1627. while (2 + 2 === 4) {
  1628. if (++c > 9) {
  1629. break;
  1630. }
  1631. }
  1632. c === 10;
  1633. `
  1634. testScript1(SCRIPT, valueTrue, t)
  1635. }
  1636. func TestConstWhileThrow(t *testing.T) {
  1637. const SCRIPT = `
  1638. var thrown = false;
  1639. try {
  1640. while ('s' in true) {
  1641. break;
  1642. }
  1643. } catch (e) {
  1644. thrown = e instanceof TypeError
  1645. }
  1646. thrown;
  1647. `
  1648. testScript1(SCRIPT, valueTrue, t)
  1649. }
  1650. func TestDupParams(t *testing.T) {
  1651. const SCRIPT = `
  1652. function F(x, y, x) {
  1653. return x;
  1654. }
  1655. F(1, 2);
  1656. `
  1657. testScript1(SCRIPT, _undefined, t)
  1658. }
  1659. func TestUseUnsuppliedParam(t *testing.T) {
  1660. const SCRIPT = `
  1661. function getMessage(message) {
  1662. if (message === undefined) {
  1663. message = '';
  1664. }
  1665. message += " 123 456";
  1666. return message;
  1667. }
  1668. getMessage();
  1669. `
  1670. testScript1(SCRIPT, asciiString(" 123 456"), t)
  1671. }
  1672. func TestForInLetWithInitializer(t *testing.T) {
  1673. const SCRIPT = `for (let x = 3 in {}) { }`
  1674. _, err := Compile("", SCRIPT, false)
  1675. if err == nil {
  1676. t.Fatal("Expected error")
  1677. }
  1678. }
  1679. func TestForInLoop(t *testing.T) {
  1680. const SCRIPT = `
  1681. function Proto() {}
  1682. Proto.prototype.x = 42;
  1683. var o = new Proto();
  1684. o.y = 44;
  1685. o.x = 45;
  1686. var hasX = false;
  1687. var hasY = false;
  1688. for (var i in o) {
  1689. switch(i) {
  1690. case "x":
  1691. if (hasX) {
  1692. throw new Error("Already has X");
  1693. }
  1694. hasX = true;
  1695. break;
  1696. case "y":
  1697. if (hasY) {
  1698. throw new Error("Already has Y");
  1699. }
  1700. hasY = true;
  1701. break;
  1702. }
  1703. }
  1704. hasX && hasY;
  1705. `
  1706. testScript1(SCRIPT, valueTrue, t)
  1707. }
  1708. func TestWhileLoopResult(t *testing.T) {
  1709. const SCRIPT = `
  1710. while(false);
  1711. `
  1712. testScript1(SCRIPT, _undefined, t)
  1713. }
  1714. func TestEmptySwitch(t *testing.T) {
  1715. const SCRIPT = `
  1716. switch(1){}
  1717. `
  1718. testScript1(SCRIPT, _undefined, t)
  1719. }
  1720. func TestEmptyDoWhile(t *testing.T) {
  1721. const SCRIPT = `
  1722. do {} while(false)
  1723. `
  1724. testScript1(SCRIPT, _undefined, t)
  1725. }
  1726. func TestSwitch(t *testing.T) {
  1727. const SCRIPT = `
  1728. function F(x) {
  1729. var i = 0;
  1730. switch (x) {
  1731. case 0:
  1732. i++;
  1733. case 1:
  1734. i++;
  1735. default:
  1736. i++;
  1737. case 2:
  1738. i++;
  1739. break;
  1740. case 3:
  1741. i++;
  1742. }
  1743. return i;
  1744. }
  1745. F(0) + F(1) + F(2) + F(4);
  1746. `
  1747. testScript1(SCRIPT, intToValue(10), t)
  1748. }
  1749. func TestSwitchDefFirst(t *testing.T) {
  1750. const SCRIPT = `
  1751. function F(x) {
  1752. var i = 0;
  1753. switch (x) {
  1754. default:
  1755. i++;
  1756. case 0:
  1757. i++;
  1758. case 1:
  1759. i++;
  1760. case 2:
  1761. i++;
  1762. break;
  1763. case 3:
  1764. i++;
  1765. }
  1766. return i;
  1767. }
  1768. F(0) + F(1) + F(2) + F(4);
  1769. `
  1770. testScript1(SCRIPT, intToValue(10), t)
  1771. }
  1772. func TestSwitchResult(t *testing.T) {
  1773. const SCRIPT = `
  1774. var x = 2;
  1775. switch (x) {
  1776. case 0:
  1777. "zero";
  1778. case 1:
  1779. "one";
  1780. case 2:
  1781. "two";
  1782. break;
  1783. case 3:
  1784. "three";
  1785. default:
  1786. "default";
  1787. }
  1788. `
  1789. testScript1(SCRIPT, asciiString("two"), t)
  1790. }
  1791. func TestSwitchResult1(t *testing.T) {
  1792. const SCRIPT = `
  1793. var x = 0;
  1794. switch (x) { case 0: "two"; case 1: break}
  1795. `
  1796. testScript1(SCRIPT, asciiString("two"), t)
  1797. }
  1798. func TestSwitchResult2(t *testing.T) {
  1799. const SCRIPT = `
  1800. 6; switch ("a") { case "a": 7; case "b": }
  1801. `
  1802. testScript1(SCRIPT, valueInt(7), t)
  1803. }
  1804. func TestSwitchResultJumpIntoEmptyEval(t *testing.T) {
  1805. const SCRIPT = `
  1806. function t(x) {
  1807. return eval("switch(x) { case 1: 2; break; case 2: let x = 1; case 3: x+2; break; case 4: default: 9}");
  1808. }
  1809. ""+t(2)+t();
  1810. `
  1811. testScript1(SCRIPT, asciiString("39"), t)
  1812. }
  1813. func TestSwitchResultJumpIntoEmpty(t *testing.T) {
  1814. const SCRIPT = `
  1815. switch(2) { case 1: 2; break; case 2: let x = 1; case 3: x+2; case 4: {let y = 2}; break; default: 9};
  1816. `
  1817. testScript1(SCRIPT, valueInt(3), t)
  1818. }
  1819. func TestSwitchLexical(t *testing.T) {
  1820. const SCRIPT = `
  1821. switch (true) { case true: let x = 1; }
  1822. `
  1823. testScript1(SCRIPT, _undefined, t)
  1824. }
  1825. func TestSwitchBreakOuter(t *testing.T) {
  1826. const SCRIPT = `
  1827. LOOP:
  1828. for (let i = 0; i < 10; i++) {
  1829. switch (i) {
  1830. case 0:
  1831. continue;
  1832. case 1:
  1833. let x = 1;
  1834. continue;
  1835. case 2:
  1836. try {
  1837. x++;
  1838. } catch (e) {
  1839. if (e instanceof ReferenceError) {
  1840. break LOOP;
  1841. }
  1842. }
  1843. throw new Error("Exception was not thrown");
  1844. }
  1845. }
  1846. `
  1847. testScript1(SCRIPT, _undefined, t)
  1848. }
  1849. func TestIfBreakResult(t *testing.T) {
  1850. const SCRIPT = `
  1851. L: {if (true) {42;} break L;}
  1852. `
  1853. testScript1(SCRIPT, intToValue(42), t)
  1854. }
  1855. func TestSwitchNoMatch(t *testing.T) {
  1856. const SCRIPT = `
  1857. var result;
  1858. var x;
  1859. switch (x) {
  1860. case 0:
  1861. result = "2";
  1862. break;
  1863. }
  1864. result;
  1865. `
  1866. testScript1(SCRIPT, _undefined, t)
  1867. }
  1868. func TestSwitchNoMatchNoDefault(t *testing.T) {
  1869. const SCRIPT = `
  1870. switch (1) {
  1871. case 0:
  1872. }
  1873. `
  1874. testScript1(SCRIPT, _undefined, t)
  1875. }
  1876. func TestSwitchNoMatchNoDefaultNoResult(t *testing.T) {
  1877. const SCRIPT = `
  1878. switch (1) {
  1879. case 0:
  1880. }
  1881. 42;
  1882. `
  1883. testScript1(SCRIPT, intToValue(42), t)
  1884. }
  1885. func TestSwitchNoMatchNoDefaultNoResultMatch(t *testing.T) {
  1886. const SCRIPT = `
  1887. switch (1) {
  1888. case 1:
  1889. }
  1890. 42;
  1891. `
  1892. testScript1(SCRIPT, intToValue(42), t)
  1893. }
  1894. func TestEmptySwitchNoResult(t *testing.T) {
  1895. const SCRIPT = `
  1896. switch (1) {}
  1897. 42;
  1898. `
  1899. testScript1(SCRIPT, intToValue(42), t)
  1900. }
  1901. func TestGetOwnPropertyNames(t *testing.T) {
  1902. const SCRIPT = `
  1903. var o = {
  1904. prop1: 42,
  1905. prop2: "test"
  1906. }
  1907. var hasProp1 = false;
  1908. var hasProp2 = false;
  1909. var names = Object.getOwnPropertyNames(o);
  1910. for (var i in names) {
  1911. var p = names[i];
  1912. switch(p) {
  1913. case "prop1":
  1914. hasProp1 = true;
  1915. break;
  1916. case "prop2":
  1917. hasProp2 = true;
  1918. break;
  1919. }
  1920. }
  1921. hasProp1 && hasProp2;
  1922. `
  1923. testScript1(SCRIPT, valueTrue, t)
  1924. }
  1925. func TestArrayLiteral(t *testing.T) {
  1926. const SCRIPT = `
  1927. var f1Called = false;
  1928. var f2Called = false;
  1929. var f3Called = false;
  1930. var errorThrown = false;
  1931. function F1() {
  1932. f1Called = true;
  1933. }
  1934. function F2() {
  1935. f2Called = true;
  1936. }
  1937. function F3() {
  1938. f3Called = true;
  1939. }
  1940. try {
  1941. var a = [F1(), x(F3()), F2()];
  1942. } catch(e) {
  1943. if (e instanceof ReferenceError) {
  1944. errorThrown = true;
  1945. } else {
  1946. throw e;
  1947. }
  1948. }
  1949. f1Called && !f2Called && f3Called && errorThrown && a === undefined;
  1950. `
  1951. testScript1(SCRIPT, valueTrue, t)
  1952. }
  1953. func TestJumpOutOfReturn(t *testing.T) {
  1954. const SCRIPT = `
  1955. function f() {
  1956. var a;
  1957. if (a == 0) {
  1958. return true;
  1959. }
  1960. }
  1961. f();
  1962. `
  1963. testScript1(SCRIPT, _undefined, t)
  1964. }
  1965. func TestSwitchJumpOutOfReturn(t *testing.T) {
  1966. const SCRIPT = `
  1967. function f(x) {
  1968. switch(x) {
  1969. case 0:
  1970. break;
  1971. default:
  1972. return x;
  1973. }
  1974. }
  1975. f(0);
  1976. `
  1977. testScript1(SCRIPT, _undefined, t)
  1978. }
  1979. func TestSetToReadOnlyPropertyStrictBracket(t *testing.T) {
  1980. const SCRIPT = `
  1981. 'use strict';
  1982. var o = {};
  1983. var thrown = false;
  1984. Object.defineProperty(o, "test", {value: 42, configurable: true});
  1985. try {
  1986. o["test"] = 43;
  1987. } catch (e) {
  1988. thrown = e instanceof TypeError;
  1989. }
  1990. thrown;
  1991. `
  1992. testScript1(SCRIPT, valueTrue, t)
  1993. }
  1994. func TestSetToReadOnlyPropertyStrictDot(t *testing.T) {
  1995. const SCRIPT = `
  1996. 'use strict';
  1997. var o = {};
  1998. var thrown = false;
  1999. Object.defineProperty(o, "test", {value: 42, configurable: true});
  2000. try {
  2001. o.test = 43;
  2002. } catch (e) {
  2003. thrown = e instanceof TypeError;
  2004. }
  2005. thrown;
  2006. `
  2007. testScript1(SCRIPT, valueTrue, t)
  2008. }
  2009. func TestDeleteNonConfigurablePropertyStrictBracket(t *testing.T) {
  2010. const SCRIPT = `
  2011. 'use strict';
  2012. var o = {};
  2013. var thrown = false;
  2014. Object.defineProperty(o, "test", {value: 42});
  2015. try {
  2016. delete o["test"];
  2017. } catch (e) {
  2018. thrown = e instanceof TypeError;
  2019. }
  2020. thrown;
  2021. `
  2022. testScript1(SCRIPT, valueTrue, t)
  2023. }
  2024. func TestDeleteNonConfigurablePropertyStrictDot(t *testing.T) {
  2025. const SCRIPT = `
  2026. 'use strict';
  2027. var o = {};
  2028. var thrown = false;
  2029. Object.defineProperty(o, "test", {value: 42});
  2030. try {
  2031. delete o.test;
  2032. } catch (e) {
  2033. thrown = e instanceof TypeError;
  2034. }
  2035. thrown;
  2036. `
  2037. testScript1(SCRIPT, valueTrue, t)
  2038. }
  2039. func TestCompound1(t *testing.T) {
  2040. const SCRIPT = `
  2041. var x = 0;
  2042. var scope = {x: 1};
  2043. var f;
  2044. with (scope) {
  2045. f = function() {
  2046. x *= (delete scope.x, 2);
  2047. }
  2048. }
  2049. f();
  2050. scope.x === 2 && x === 0;
  2051. `
  2052. testScript1(SCRIPT, valueTrue, t)
  2053. }
  2054. func TestCompound2(t *testing.T) {
  2055. const SCRIPT = `
  2056. var x;
  2057. x = "x";
  2058. x ^= "1";
  2059. `
  2060. testScript1(SCRIPT, intToValue(1), t)
  2061. }
  2062. func TestDeleteArguments(t *testing.T) {
  2063. defer func() {
  2064. if _, ok := recover().(*CompilerSyntaxError); !ok {
  2065. t.Fatal("Expected syntax error")
  2066. }
  2067. }()
  2068. const SCRIPT = `
  2069. 'use strict';
  2070. function f() {
  2071. delete arguments;
  2072. }
  2073. `
  2074. testScript1(SCRIPT, _undefined, t)
  2075. }
  2076. func TestReturnUndefined(t *testing.T) {
  2077. const SCRIPT = `
  2078. function f() {
  2079. return x;
  2080. }
  2081. var thrown = false;
  2082. try {
  2083. f();
  2084. } catch (e) {
  2085. thrown = e instanceof ReferenceError;
  2086. }
  2087. thrown;
  2088. `
  2089. testScript1(SCRIPT, valueTrue, t)
  2090. }
  2091. func TestForBreak(t *testing.T) {
  2092. const SCRIPT = `
  2093. var supreme, count;
  2094. supreme = 5;
  2095. var __evaluated = eval("for(count=0;;) {if (count===supreme)break;else count++; }");
  2096. if (__evaluated !== void 0) {
  2097. throw new Error('#1: __evaluated === 4. Actual: __evaluated ==='+ __evaluated );
  2098. }
  2099. `
  2100. testScript1(SCRIPT, _undefined, t)
  2101. }
  2102. func TestLargeNumberLiteral(t *testing.T) {
  2103. const SCRIPT = `
  2104. var x = 0x800000000000000000000;
  2105. x.toString();
  2106. `
  2107. testScript1(SCRIPT, asciiString("9.671406556917033e+24"), t)
  2108. }
  2109. func TestIncDelete(t *testing.T) {
  2110. const SCRIPT = `
  2111. var o = {x: 1};
  2112. o.x += (delete o.x, 1);
  2113. o.x;
  2114. `
  2115. testScript1(SCRIPT, intToValue(2), t)
  2116. }
  2117. func TestCompoundAssignRefError(t *testing.T) {
  2118. const SCRIPT = `
  2119. var thrown = false;
  2120. try {
  2121. a *= 1;
  2122. } catch (e) {
  2123. if (e instanceof ReferenceError) {
  2124. thrown = true;
  2125. } else {
  2126. throw e;
  2127. }
  2128. }
  2129. thrown;
  2130. `
  2131. testScript1(SCRIPT, valueTrue, t)
  2132. }
  2133. func TestObjectLiteral__Proto__(t *testing.T) {
  2134. const SCRIPT = `
  2135. var o = {
  2136. __proto__: null,
  2137. test: 42
  2138. }
  2139. Object.getPrototypeOf(o);
  2140. `
  2141. testScript1(SCRIPT, _null, t)
  2142. }
  2143. func TestEmptyCodeError(t *testing.T) {
  2144. if _, err := New().RunString(`i`); err == nil {
  2145. t.Fatal("Expected an error")
  2146. } else {
  2147. if e := err.Error(); e != "ReferenceError: i is not defined at <eval>:1:1(0)" {
  2148. t.Fatalf("Unexpected error: '%s'", e)
  2149. }
  2150. }
  2151. }
  2152. func TestForOfArray(t *testing.T) {
  2153. const SCRIPT = `
  2154. var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
  2155. var i = 0;
  2156. for (var value of array) {
  2157. assert.sameValue(value, array[i], 'element at index ' + i);
  2158. i++;
  2159. }
  2160. assert.sameValue(i, 8, 'Visits all elements');
  2161. `
  2162. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2163. }
  2164. func TestForOfReturn(t *testing.T) {
  2165. const SCRIPT = `
  2166. var callCount = 0;
  2167. var iterationCount = 0;
  2168. var iterable = {};
  2169. var x = {
  2170. set attr(_) {
  2171. throw new Test262Error();
  2172. }
  2173. };
  2174. iterable[Symbol.iterator] = function() {
  2175. return {
  2176. next: function() {
  2177. return { done: false, value: 0 };
  2178. },
  2179. return: function() {
  2180. callCount += 1;
  2181. }
  2182. }
  2183. };
  2184. assert.throws(Test262Error, function() {
  2185. for (x.attr of iterable) {
  2186. iterationCount += 1;
  2187. }
  2188. });
  2189. assert.sameValue(iterationCount, 0, 'The loop body is not evaluated');
  2190. assert.sameValue(callCount, 1, 'Iterator is closed');
  2191. `
  2192. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2193. }
  2194. func TestForOfReturn1(t *testing.T) {
  2195. const SCRIPT = `
  2196. var iterable = {};
  2197. var iterationCount = 0;
  2198. iterable[Symbol.iterator] = function() {
  2199. return {
  2200. next: function() {
  2201. return { done: false, value: null };
  2202. },
  2203. get return() {
  2204. throw new Test262Error();
  2205. }
  2206. };
  2207. };
  2208. assert.throws(Test262Error, function() {
  2209. for (var x of iterable) {
  2210. iterationCount += 1;
  2211. break;
  2212. }
  2213. });
  2214. assert.sameValue(iterationCount, 1, 'The loop body is evaluated');
  2215. `
  2216. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2217. }
  2218. func TestForOfLet(t *testing.T) {
  2219. const SCRIPT = `
  2220. var iterCount = 0;
  2221. function f() {}
  2222. for (var let of [23]) {
  2223. f(let);
  2224. if (let != 23) {
  2225. throw new Error("");
  2226. }
  2227. iterCount += 1;
  2228. }
  2229. iterCount;
  2230. `
  2231. testScript1(SCRIPT, valueInt(1), t)
  2232. }
  2233. func TestForOfLetLet(t *testing.T) {
  2234. const SCRIPT = `
  2235. for (let let of [23]) {
  2236. }
  2237. `
  2238. _, err := Compile("", SCRIPT, false)
  2239. if err == nil {
  2240. t.Fatal("Expected error")
  2241. }
  2242. }
  2243. func TestForHeadLet(t *testing.T) {
  2244. const SCRIPT = `
  2245. for (let = 0; let < 2; let++);
  2246. `
  2247. testScript1(SCRIPT, _undefined, t)
  2248. }
  2249. func TestLhsLet(t *testing.T) {
  2250. const SCRIPT = `
  2251. let = 1;
  2252. let;
  2253. `
  2254. testScript1(SCRIPT, valueInt(1), t)
  2255. }
  2256. func TestLetPostfixASI(t *testing.T) {
  2257. const SCRIPT = `
  2258. let
  2259. ++
  2260. `
  2261. _, err := Compile("", SCRIPT, false)
  2262. if err == nil {
  2263. t.Fatal("Expected error")
  2264. }
  2265. }
  2266. func TestIteratorReturnNormal(t *testing.T) {
  2267. const SCRIPT = `
  2268. var iterable = {};
  2269. var iterationCount = 0;
  2270. iterable[Symbol.iterator] = function() {
  2271. return {
  2272. next: function() {
  2273. return { done: ++iterationCount > 2, value: null };
  2274. },
  2275. get return() {
  2276. throw new Test262Error();
  2277. }
  2278. };
  2279. };
  2280. for (var x of iterable) {
  2281. }
  2282. `
  2283. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2284. }
  2285. func TestIteratorReturnErrorNested(t *testing.T) {
  2286. const SCRIPT = `
  2287. var returnCalled = {};
  2288. function iter(id) {
  2289. return function() {
  2290. var count = 0;
  2291. return {
  2292. next: function () {
  2293. return {
  2294. value: null,
  2295. done: ++count > 2
  2296. };
  2297. },
  2298. return: function () {
  2299. returnCalled[id] = true;
  2300. throw new Error(id);
  2301. }
  2302. };
  2303. }
  2304. }
  2305. var iterable1 = {};
  2306. iterable1[Symbol.iterator] = iter("1");
  2307. var iterable2 = {};
  2308. iterable2[Symbol.iterator] = iter("2");
  2309. try {
  2310. for (var i of iterable1) {
  2311. for (var j of iterable2) {
  2312. break;
  2313. }
  2314. }
  2315. throw new Error("no exception was thrown");
  2316. } catch (e) {
  2317. if (e.message !== "2") {
  2318. throw e;
  2319. }
  2320. }
  2321. if (!returnCalled["1"]) {
  2322. throw new Error("no return 1");
  2323. }
  2324. if (!returnCalled["2"]) {
  2325. throw new Error("no return 2");
  2326. }
  2327. `
  2328. testScript1(SCRIPT, _undefined, t)
  2329. }
  2330. func TestReturnFromForInLoop(t *testing.T) {
  2331. const SCRIPT = `
  2332. (function f() {
  2333. for (var i in {a: 1}) {
  2334. return true;
  2335. }
  2336. })();
  2337. `
  2338. testScript1(SCRIPT, valueTrue, t)
  2339. }
  2340. func TestReturnFromForOfLoop(t *testing.T) {
  2341. const SCRIPT = `
  2342. (function f() {
  2343. for (var i of [1]) {
  2344. return true;
  2345. }
  2346. })();
  2347. `
  2348. testScript1(SCRIPT, valueTrue, t)
  2349. }
  2350. func TestIfStackLeaks(t *testing.T) {
  2351. const SCRIPT = `
  2352. var t = 0;
  2353. if (t === 0) {
  2354. t;
  2355. }
  2356. `
  2357. testScript1(SCRIPT, _positiveZero, t)
  2358. }
  2359. func TestWithCallee(t *testing.T) {
  2360. const SCRIPT = `
  2361. function O() {
  2362. var that = this;
  2363. this.m = function() {
  2364. return this === that;
  2365. }
  2366. }
  2367. with(new O()) {
  2368. m();
  2369. }
  2370. `
  2371. testScript1(SCRIPT, valueTrue, t)
  2372. }
  2373. func TestWithScope(t *testing.T) {
  2374. const SCRIPT = `
  2375. function f(o) {
  2376. var x = 42;
  2377. function innerf(o) {
  2378. with (o) {
  2379. return x;
  2380. }
  2381. }
  2382. return innerf(o);
  2383. }
  2384. f({});
  2385. `
  2386. testScript1(SCRIPT, valueInt(42), t)
  2387. }
  2388. func TestEvalCallee(t *testing.T) {
  2389. const SCRIPT = `
  2390. (function () {
  2391. 'use strict';
  2392. var v = function() {
  2393. return this === undefined;
  2394. };
  2395. return eval('v()');
  2396. })();
  2397. `
  2398. testScript1(SCRIPT, valueTrue, t)
  2399. }
  2400. func TestEvalBindingDeleteVar(t *testing.T) {
  2401. const SCRIPT = `
  2402. (function () {
  2403. eval("var x = 1");
  2404. return x === 1 && delete x;
  2405. })();
  2406. `
  2407. testScript1(SCRIPT, valueTrue, t)
  2408. }
  2409. func TestEvalBindingDeleteFunc(t *testing.T) {
  2410. const SCRIPT = `
  2411. (function () {
  2412. eval("function x(){}");
  2413. return typeof x === "function" && delete x;
  2414. })();
  2415. `
  2416. testScript1(SCRIPT, valueTrue, t)
  2417. }
  2418. func TestDeleteGlobalLexical(t *testing.T) {
  2419. const SCRIPT = `
  2420. let x;
  2421. delete x;
  2422. `
  2423. testScript1(SCRIPT, valueFalse, t)
  2424. }
  2425. func TestDeleteGlobalEval(t *testing.T) {
  2426. const SCRIPT = `
  2427. eval("var x");
  2428. delete x;
  2429. `
  2430. testScript1(SCRIPT, valueTrue, t)
  2431. }
  2432. func TestGlobalVarNames(t *testing.T) {
  2433. vm := New()
  2434. _, err := vm.RunString("(0,eval)('var x')")
  2435. if err != nil {
  2436. t.Fatal(err)
  2437. }
  2438. _, err = vm.RunString("let x")
  2439. if err == nil {
  2440. t.Fatal("Expected error")
  2441. }
  2442. }
  2443. func TestTryResultEmpty(t *testing.T) {
  2444. const SCRIPT = `
  2445. 1; try { } finally { }
  2446. `
  2447. testScript1(SCRIPT, _undefined, t)
  2448. }
  2449. func TestTryResultEmptyCatch(t *testing.T) {
  2450. const SCRIPT = `
  2451. 1; try { throw null } catch(e) { }
  2452. `
  2453. testScript1(SCRIPT, _undefined, t)
  2454. }
  2455. func TestTryResultEmptyContinueLoop(t *testing.T) {
  2456. const SCRIPT = `
  2457. for (var i = 0; i < 2; i++) { try {throw null;} catch(e) {continue;} 'bad'}
  2458. `
  2459. testScript1(SCRIPT, _undefined, t)
  2460. }
  2461. func TestTryEmptyCatchStackLeak(t *testing.T) {
  2462. const SCRIPT = `
  2463. (function() {
  2464. var f;
  2465. // Make sure the outer function is not stashless.
  2466. (function() {
  2467. f++;
  2468. })();
  2469. try {
  2470. throw new Error();
  2471. } catch(e) {}
  2472. })();
  2473. `
  2474. testScript1(SCRIPT, _undefined, t)
  2475. }
  2476. func TestTryThrowEmptyCatch(t *testing.T) {
  2477. const SCRIPT = `
  2478. try {
  2479. throw new Error();
  2480. }
  2481. catch (e) {}
  2482. `
  2483. testScript1(SCRIPT, _undefined, t)
  2484. }
  2485. func TestFalsyLoopBreak(t *testing.T) {
  2486. const SCRIPT = `
  2487. while(false) {
  2488. break;
  2489. }
  2490. for(;false;) {
  2491. break;
  2492. }
  2493. undefined;
  2494. `
  2495. MustCompile("", SCRIPT, false)
  2496. }
  2497. func TestFalsyLoopBreakWithResult(t *testing.T) {
  2498. const SCRIPT = `
  2499. while(false) {
  2500. break;
  2501. }
  2502. `
  2503. testScript1(SCRIPT, _undefined, t)
  2504. }
  2505. func TestDummyCompile(t *testing.T) {
  2506. const SCRIPT = `
  2507. 'use strict';
  2508. for (;false;) {
  2509. eval = 1;
  2510. }
  2511. `
  2512. _, err := Compile("", SCRIPT, false)
  2513. if err == nil {
  2514. t.Fatal("expected error")
  2515. }
  2516. }
  2517. func TestDummyCompileForUpdate(t *testing.T) {
  2518. const SCRIPT = `
  2519. 'use strict';
  2520. for (;false;eval=1) {
  2521. }
  2522. `
  2523. _, err := Compile("", SCRIPT, false)
  2524. if err == nil {
  2525. t.Fatal("expected error")
  2526. }
  2527. }
  2528. func TestObjectLiteralWithNumericKeys(t *testing.T) {
  2529. const SCRIPT = `
  2530. var o = {1e3: true};
  2531. var keys = Object.keys(o);
  2532. var o1 = {get 1e3() {return true;}};
  2533. var keys1 = Object.keys(o1);
  2534. var o2 = {1e21: true};
  2535. var keys2 = Object.keys(o2);
  2536. keys.length === 1 && keys[0] === "1000" &&
  2537. keys1.length === 1 && keys1[0] === "1000" && o1[1e3] === true &&
  2538. keys2.length === 1 && keys2[0] === "1e+21";
  2539. `
  2540. testScript1(SCRIPT, valueTrue, t)
  2541. }
  2542. func TestEscapedObjectPropertyKeys(t *testing.T) {
  2543. const SCRIPT = `
  2544. var obj = {
  2545. w\u0069th: 42
  2546. };
  2547. var obj = {
  2548. with() {42}
  2549. };
  2550. `
  2551. _, err := Compile("", SCRIPT, false)
  2552. if err != nil {
  2553. t.Fatal(err)
  2554. }
  2555. }
  2556. func TestObjectLiteralFuncProps(t *testing.T) {
  2557. const SCRIPT = `
  2558. (function() {
  2559. 'use strict';
  2560. var o = {
  2561. eval: function() {return 1;},
  2562. arguments() {return 2;},
  2563. test: function test1() {}
  2564. }
  2565. assert.sameValue(o.eval.name, "eval");
  2566. assert.sameValue(o.arguments.name, "arguments");
  2567. assert.sameValue(o.eval(), 1);
  2568. assert.sameValue(o.arguments(), 2);
  2569. assert.sameValue(o.test.name, "test1");
  2570. })();
  2571. `
  2572. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2573. }
  2574. func TestFuncName(t *testing.T) {
  2575. const SCRIPT = `
  2576. var method = 1;
  2577. var o = {
  2578. method: function() {
  2579. return method;
  2580. },
  2581. method1: function method() {
  2582. return method;
  2583. }
  2584. }
  2585. o.method() === 1 && o.method1() === o.method1;
  2586. `
  2587. testScript1(SCRIPT, valueTrue, t)
  2588. }
  2589. func TestFuncNameAssign(t *testing.T) {
  2590. const SCRIPT = `
  2591. var f = function() {};
  2592. var f1;
  2593. f1 = function() {};
  2594. let f2 = function() {};
  2595. f.name === "f" && f1.name === "f1" && f2.name === "f2";
  2596. `
  2597. testScript1(SCRIPT, valueTrue, t)
  2598. }
  2599. func TestLexicalDeclGlobal(t *testing.T) {
  2600. const SCRIPT = `
  2601. if (true) {
  2602. let it = "be";
  2603. if (it !== "be") {
  2604. throw new Error(it);
  2605. }
  2606. }
  2607. let thrown = false;
  2608. try {
  2609. it;
  2610. } catch(e) {
  2611. if (e instanceof ReferenceError) {
  2612. thrown = true;
  2613. }
  2614. }
  2615. thrown;
  2616. `
  2617. testScript1(SCRIPT, valueTrue, t)
  2618. }
  2619. func TestLexicalDeclFunction(t *testing.T) {
  2620. const SCRIPT = `
  2621. function f() {
  2622. if (true) {
  2623. let it = "be";
  2624. if (it !== "be") {
  2625. throw new Error(it);
  2626. }
  2627. }
  2628. let thrown = false;
  2629. try {
  2630. it;
  2631. } catch(e) {
  2632. if (e instanceof ReferenceError) {
  2633. thrown = true;
  2634. }
  2635. }
  2636. return thrown;
  2637. }
  2638. f();
  2639. `
  2640. testScript1(SCRIPT, valueTrue, t)
  2641. }
  2642. func TestLexicalDynamicScope(t *testing.T) {
  2643. const SCRIPT = `
  2644. const global = 1;
  2645. function f() {
  2646. const func = global + 1;
  2647. function inner() {
  2648. function assertThrows(fn) {
  2649. let thrown = false;
  2650. try {
  2651. fn();
  2652. } catch (e) {
  2653. if (e instanceof TypeError) {
  2654. thrown = true;
  2655. } else {
  2656. throw e;
  2657. }
  2658. }
  2659. if (!thrown) {
  2660. throw new Error("Did not throw");
  2661. }
  2662. }
  2663. assertThrows(function() {
  2664. func++;
  2665. });
  2666. assertThrows(function() {
  2667. global++;
  2668. });
  2669. assertThrows(function() {
  2670. eval("func++");
  2671. });
  2672. assertThrows(function() {
  2673. eval("global++");
  2674. });
  2675. return eval("func + 1");
  2676. }
  2677. return inner();
  2678. }
  2679. f();
  2680. `
  2681. testScript1(SCRIPT, valueInt(3), t)
  2682. }
  2683. func TestNonStrictLet(t *testing.T) {
  2684. const SCRIPT = `
  2685. var let = 1;
  2686. `
  2687. testScript1(SCRIPT, _undefined, t)
  2688. }
  2689. func TestStrictLet(t *testing.T) {
  2690. const SCRIPT = `
  2691. var let = 1;
  2692. `
  2693. _, err := Compile("", SCRIPT, true)
  2694. if err == nil {
  2695. t.Fatal("Expected an error")
  2696. }
  2697. }
  2698. func TestLetLet(t *testing.T) {
  2699. const SCRIPT = `
  2700. let let = 1;
  2701. `
  2702. _, err := Compile("", SCRIPT, false)
  2703. if err == nil {
  2704. t.Fatal("Expected an error")
  2705. }
  2706. }
  2707. func TestLetASI(t *testing.T) {
  2708. const SCRIPT = `
  2709. while (false) let // ASI
  2710. x = 1;
  2711. `
  2712. _, err := Compile("", SCRIPT, false)
  2713. if err != nil {
  2714. t.Fatal(err)
  2715. }
  2716. }
  2717. func TestLetASI1(t *testing.T) {
  2718. const SCRIPT = `
  2719. let
  2720. x = 1;
  2721. `
  2722. _, err := Compile("", SCRIPT, true)
  2723. if err != nil {
  2724. t.Fatal(err)
  2725. }
  2726. }
  2727. func TestLetNoASI(t *testing.T) {
  2728. const SCRIPT = `
  2729. function f() {}let
  2730. x = 1;
  2731. `
  2732. _, err := Compile("", SCRIPT, true)
  2733. if err != nil {
  2734. t.Fatal(err)
  2735. }
  2736. }
  2737. func TestLetNoASI1(t *testing.T) {
  2738. const SCRIPT = `
  2739. let
  2740. let = 1;
  2741. `
  2742. _, err := Compile("", SCRIPT, false)
  2743. if err == nil {
  2744. t.Fatal("Expected error")
  2745. }
  2746. }
  2747. func TestLetArrayWithNewline(t *testing.T) {
  2748. const SCRIPT = `
  2749. with ({}) let
  2750. [a] = 0;
  2751. `
  2752. _, err := Compile("", SCRIPT, false)
  2753. if err == nil {
  2754. t.Fatal("Expected error")
  2755. }
  2756. }
  2757. func TestDynamicUninitedVarAccess(t *testing.T) {
  2758. const SCRIPT = `
  2759. function f() {
  2760. var x;
  2761. return eval("x");
  2762. }
  2763. f();
  2764. `
  2765. testScript1(SCRIPT, _undefined, t)
  2766. }
  2767. func TestLexicalForLoopNoClosure(t *testing.T) {
  2768. const SCRIPT = `
  2769. let sum = 0;
  2770. for (let i = 0; i < 3; i++) {
  2771. sum += i;
  2772. }
  2773. sum;
  2774. `
  2775. testScript1(SCRIPT, valueInt(3), t)
  2776. }
  2777. func TestLexicalForLoopClosure(t *testing.T) {
  2778. const SCRIPT = `
  2779. var f = [];
  2780. for (let i = 0; i < 3; i++) {
  2781. f.push(function() {
  2782. return i;
  2783. });
  2784. }
  2785. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  2786. `
  2787. testScript1(SCRIPT, valueTrue, t)
  2788. }
  2789. func TestLexicalForLoopClosureInNext(t *testing.T) {
  2790. const SCRIPT = `
  2791. const a = [];
  2792. for (let i = 0; i < 5; a.push(function () { return i; }), ++i) { }
  2793. let res = "";
  2794. for (let k = 0; k < 5; ++k) {
  2795. res += ""+a[k]();
  2796. }
  2797. res;
  2798. `
  2799. testScript1(SCRIPT, asciiString("12345"), t)
  2800. }
  2801. func TestVarForLoop(t *testing.T) {
  2802. const SCRIPT = `
  2803. var f = [];
  2804. for (var i = 0, j = 0; i < 3; i++) {
  2805. f.push(function() {
  2806. return i;
  2807. });
  2808. }
  2809. f.length === 3 && f[0]() === 3 && f[1]() === 3 && f[2]() === 3;
  2810. `
  2811. testScript1(SCRIPT, valueTrue, t)
  2812. }
  2813. func TestLexicalForOfLoop(t *testing.T) {
  2814. const SCRIPT = `
  2815. var f = [];
  2816. for (let i of [0, 1, 2]) {
  2817. f.push(function() {
  2818. return i;
  2819. });
  2820. }
  2821. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  2822. `
  2823. testScript1(SCRIPT, valueTrue, t)
  2824. }
  2825. func TestLexicalForOfLoopContBreak(t *testing.T) {
  2826. const SCRIPT = `
  2827. const f = [];
  2828. for (let i of [0, 1, 2, 3, 4, 5]) {
  2829. if (i % 2) continue;
  2830. f.push(function() {
  2831. return i;
  2832. });
  2833. if (i > 2) break;
  2834. }
  2835. let res = "";
  2836. f.forEach(function(item) {res += item()});
  2837. f.length === 3 && res === "024";
  2838. `
  2839. testScript1(SCRIPT, valueTrue, t)
  2840. }
  2841. func TestVarBlockConflict(t *testing.T) {
  2842. const SCRIPT = `
  2843. let x;
  2844. {
  2845. if (false) {
  2846. var x;
  2847. }
  2848. }
  2849. `
  2850. _, err := Compile("", SCRIPT, false)
  2851. if err == nil {
  2852. t.Fatal("Expected an error")
  2853. }
  2854. }
  2855. func TestVarBlockConflictEval(t *testing.T) {
  2856. const SCRIPT = `
  2857. assert.throws(SyntaxError, function() {
  2858. let x;
  2859. {
  2860. if (true) {
  2861. eval("var x");
  2862. }
  2863. }
  2864. });
  2865. `
  2866. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2867. }
  2868. func TestVarBlockNoConflict(t *testing.T) {
  2869. const SCRIPT = `
  2870. function f() {
  2871. let x;
  2872. function ff() {
  2873. {
  2874. var x = 3;
  2875. }
  2876. }
  2877. ff();
  2878. }
  2879. f();
  2880. `
  2881. testScript1(SCRIPT, _undefined, t)
  2882. }
  2883. func TestVarBlockNoConflictEval(t *testing.T) {
  2884. const SCRIPT = `
  2885. function f() {
  2886. let x;
  2887. function ff() {
  2888. {
  2889. eval("var x = 3");
  2890. }
  2891. }
  2892. ff();
  2893. }
  2894. f();
  2895. `
  2896. testScript1(SCRIPT, _undefined, t)
  2897. }
  2898. func TestVarDeclCorrectScope(t *testing.T) {
  2899. const SCRIPT = `
  2900. function f() {
  2901. {
  2902. let z;
  2903. eval("var x = 3");
  2904. }
  2905. return x;
  2906. }
  2907. f();
  2908. `
  2909. testScript1(SCRIPT, valueInt(3), t)
  2910. }
  2911. func TestLexicalCatch(t *testing.T) {
  2912. const SCRIPT = `
  2913. try {
  2914. throw null;
  2915. } catch (e) {
  2916. let x = 1;
  2917. function f() {}
  2918. e;
  2919. }
  2920. `
  2921. testScript1(SCRIPT, _null, t)
  2922. }
  2923. func TestArgumentsLexicalDecl(t *testing.T) {
  2924. const SCRIPT = `
  2925. function f1() {
  2926. let arguments;
  2927. return arguments;
  2928. }
  2929. f1(42);
  2930. `
  2931. testScript1(SCRIPT, _undefined, t)
  2932. }
  2933. func TestArgumentsLexicalDeclAssign(t *testing.T) {
  2934. const SCRIPT = `
  2935. function f1() {
  2936. let arguments = arguments;
  2937. return a;
  2938. }
  2939. assert.throws(ReferenceError, function() {
  2940. f1(42);
  2941. });
  2942. `
  2943. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2944. }
  2945. func TestLexicalConstModifyFromEval(t *testing.T) {
  2946. const SCRIPT = `
  2947. const x = 1;
  2948. function f() {
  2949. eval("x = 2");
  2950. }
  2951. assert.throws(TypeError, function() {
  2952. f();
  2953. });
  2954. `
  2955. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2956. }
  2957. func TestLexicalStrictNames(t *testing.T) {
  2958. const SCRIPT = `let eval = 1;`
  2959. _, err := Compile("", SCRIPT, true)
  2960. if err == nil {
  2961. t.Fatal("Expected an error")
  2962. }
  2963. }
  2964. func TestAssignAfterStackExpand(t *testing.T) {
  2965. // make sure the reference to the variable x does not remain stale after the stack is copied
  2966. const SCRIPT = `
  2967. function f() {
  2968. let sum = 0;
  2969. for (let i = 0; i < arguments.length; i++) {
  2970. sum += arguments[i];
  2971. }
  2972. return sum;
  2973. }
  2974. function testAssignment() {
  2975. var x = 0;
  2976. var scope = {};
  2977. with (scope) {
  2978. x = (scope.x = f(0, 0, 0, 0, 0, 0, 1, 1), 1);
  2979. }
  2980. if (scope.x !== 2) {
  2981. throw new Error('#1: scope.x === 2. Actual: ' + (scope.x));
  2982. }
  2983. if (x !== 1) {
  2984. throw new Error('#2: x === 1. Actual: ' + (x));
  2985. }
  2986. }
  2987. testAssignment();
  2988. `
  2989. testScript1(SCRIPT, _undefined, t)
  2990. }
  2991. func TestArgAccessFromDynamicStash(t *testing.T) {
  2992. const SCRIPT = `
  2993. function f(arg) {
  2994. function test() {
  2995. eval("");
  2996. return a;
  2997. }
  2998. return arg;
  2999. }
  3000. f(true);
  3001. `
  3002. testScript1(SCRIPT, valueTrue, t)
  3003. }
  3004. func TestLoadMixedLex(t *testing.T) {
  3005. const SCRIPT = `
  3006. function f() {
  3007. let a = 1;
  3008. {
  3009. function inner() {
  3010. eval("var a = true");
  3011. return a;
  3012. }
  3013. return inner();
  3014. }
  3015. }
  3016. f();
  3017. `
  3018. testScript1(SCRIPT, valueTrue, t)
  3019. }
  3020. func TestObjectLiteralSpread(t *testing.T) {
  3021. const SCRIPT = `
  3022. let src = {prop1: 1};
  3023. Object.defineProperty(src, "prop2", {value: 2, configurable: true});
  3024. Object.defineProperty(src, "prop3", {value: 3, enumerable: true, configurable: true});
  3025. let target = {prop4: 4, ...src};
  3026. assert(deepEqual(target, {prop1: 1, prop3: 3, prop4: 4}));
  3027. `
  3028. testScript1(TESTLIBX+SCRIPT, _undefined, t)
  3029. }
  3030. func TestArrayLiteralSpread(t *testing.T) {
  3031. const SCRIPT = `
  3032. let a1 = [1, 2];
  3033. let a2 = [3, 4];
  3034. let a = [...a1, 0, ...a2, 1];
  3035. assert(compareArray(a, [1, 2, 0, 3, 4, 1]));
  3036. `
  3037. testScript1(TESTLIB+SCRIPT, _undefined, t)
  3038. }
  3039. func TestObjectAssignmentPattern(t *testing.T) {
  3040. const SCRIPT = `
  3041. let a, b, c;
  3042. ({a, b, c=3} = {a: 1, b: 2});
  3043. assert.sameValue(a, 1, "a");
  3044. assert.sameValue(b, 2, "b");
  3045. assert.sameValue(c, 3, "c");
  3046. `
  3047. testScript1(TESTLIB+SCRIPT, _undefined, t)
  3048. }
  3049. func TestObjectAssignmentPatternNested(t *testing.T) {
  3050. const SCRIPT = `
  3051. let a, b, c, d;
  3052. ({a, b, c: {d} = 3} = {a: 1, b: 2, c: {d: 4}});
  3053. assert.sameValue(a, 1, "a");
  3054. assert.sameValue(b, 2, "b");
  3055. assert.sameValue(c, undefined, "c");
  3056. assert.sameValue(d, 4, "d");
  3057. `
  3058. testScript1(TESTLIB+SCRIPT, _undefined, t)
  3059. }
  3060. func TestObjectAssignmentPatternEvalOrder(t *testing.T) {
  3061. const SCRIPT = `
  3062. let trace = "";
  3063. let target_obj = {};
  3064. function src() {
  3065. trace += "src(),";
  3066. return {
  3067. get a() {
  3068. trace += "get a,";
  3069. return "a";
  3070. }
  3071. }
  3072. }
  3073. function prop1() {
  3074. trace += "prop1(),"
  3075. return {
  3076. toString: function() {
  3077. trace += "prop1-to-string(),";
  3078. return "a";
  3079. }
  3080. }
  3081. }
  3082. function prop2() {
  3083. trace += "prop2(),";
  3084. return {
  3085. toString: function() {
  3086. trace += "prop2-to-string(),";
  3087. return "b";
  3088. }
  3089. }
  3090. }
  3091. function target() {
  3092. trace += "target(),"
  3093. return target_obj;
  3094. }
  3095. let a, b;
  3096. ({[prop1()]: target().a, [prop2()]: b} = src());
  3097. if (target_obj.a !== "a") {
  3098. throw new Error("target_obj.a="+target_obj.a);
  3099. }
  3100. trace;
  3101. `
  3102. testScript1(SCRIPT, asciiString("src(),prop1(),prop1-to-string(),target(),get a,prop2(),prop2-to-string(),"), t)
  3103. }
  3104. func TestArrayAssignmentPatternEvalOrder(t *testing.T) {
  3105. const SCRIPT = `
  3106. let trace = "";
  3107. let src_arr = {
  3108. [Symbol.iterator]: function() {
  3109. let done = false;
  3110. return {
  3111. next: function() {
  3112. trace += "next,";
  3113. if (!done) {
  3114. done = true;
  3115. return {value: 0};
  3116. }
  3117. return {done: true};
  3118. },
  3119. return: function() {
  3120. trace += "return,";
  3121. }
  3122. }
  3123. }
  3124. }
  3125. function src() {
  3126. trace += "src(),";
  3127. return src_arr;
  3128. }
  3129. let tgt = {
  3130. get a() {
  3131. trace += "get a,";
  3132. return "a";
  3133. },
  3134. get b() {
  3135. trace += "get b,";
  3136. return "b";
  3137. }
  3138. }
  3139. function target() {
  3140. trace += "target(),";
  3141. return tgt;
  3142. }
  3143. function default_a() {
  3144. trace += "default a,";
  3145. return "def_a";
  3146. }
  3147. function default_b() {
  3148. trace += "default b,";
  3149. return "def_b";
  3150. }
  3151. ([target().a = default_a(), target().b = default_b()] = src());
  3152. trace;
  3153. `
  3154. testScript1(SCRIPT, asciiString("src(),target(),next,target(),next,default b,"), t)
  3155. }
  3156. func TestObjectAssignPatternRest(t *testing.T) {
  3157. const SCRIPT = `
  3158. let a, b, c, d;
  3159. ({a, b, c, ...d} = {a: 1, b: 2, d: 4});
  3160. assert.sameValue(a, 1, "a");
  3161. assert.sameValue(b, 2, "b");
  3162. assert.sameValue(c, undefined, "c");
  3163. assert(deepEqual(d, {d: 4}), "d");
  3164. `
  3165. testScript1(TESTLIBX+SCRIPT, _undefined, t)
  3166. }
  3167. func TestObjectBindPattern(t *testing.T) {
  3168. const SCRIPT = `
  3169. let {a, b, c, ...d} = {a: 1, b: 2, d: 4};
  3170. assert.sameValue(a, 1, "a");
  3171. assert.sameValue(b, 2, "b");
  3172. assert.sameValue(c, undefined, "c");
  3173. assert(deepEqual(d, {d: 4}), "d");
  3174. var { x: y, } = { x: 23 };
  3175. assert.sameValue(y, 23);
  3176. assert.throws(ReferenceError, function() {
  3177. x;
  3178. });
  3179. `
  3180. testScript1(TESTLIBX+SCRIPT, _undefined, t)
  3181. }
  3182. func TestObjLiteralShorthandWithInitializer(t *testing.T) {
  3183. const SCRIPT = `
  3184. o = {a=1};
  3185. `
  3186. _, err := Compile("", SCRIPT, false)
  3187. if err == nil {
  3188. t.Fatal("Expected an error")
  3189. }
  3190. }
  3191. func TestObjLiteralShorthandLetStringLit(t *testing.T) {
  3192. const SCRIPT = `
  3193. o = {"let"};
  3194. `
  3195. _, err := Compile("", SCRIPT, false)
  3196. if err == nil {
  3197. t.Fatal("Expected an error")
  3198. }
  3199. }
  3200. func TestObjLiteralComputedKeys(t *testing.T) {
  3201. const SCRIPT = `
  3202. let o = {
  3203. get [Symbol.toString]() {
  3204. }
  3205. }
  3206. `
  3207. testScript1(SCRIPT, _undefined, t)
  3208. }
  3209. func TestObjLiteralComputedKeysEvalOrder(t *testing.T) {
  3210. const SCRIPT = `
  3211. let trace = [];
  3212. function key() {
  3213. trace.push("key");
  3214. return {
  3215. toString: function() {
  3216. trace.push("key-toString");
  3217. return "key";
  3218. }
  3219. }
  3220. }
  3221. function val() {
  3222. trace.push("val");
  3223. return "val";
  3224. }
  3225. const _ = {
  3226. [key()]: val(),
  3227. }
  3228. trace.join(",");
  3229. `
  3230. testScript1(SCRIPT, asciiString("key,key-toString,val"), t)
  3231. }
  3232. func TestArrayAssignPattern(t *testing.T) {
  3233. const SCRIPT = `
  3234. let a, b;
  3235. ([a, b] = [1, 2]);
  3236. a === 1 && b === 2;
  3237. `
  3238. testScript1(SCRIPT, valueTrue, t)
  3239. }
  3240. func TestArrayAssignPattern1(t *testing.T) {
  3241. const SCRIPT = `
  3242. let a, b;
  3243. ([a = 3, b = 2] = [1]);
  3244. a === 1 && b === 2;
  3245. `
  3246. testScript1(SCRIPT, valueTrue, t)
  3247. }
  3248. func TestArrayAssignPatternLHS(t *testing.T) {
  3249. const SCRIPT = `
  3250. let a = {};
  3251. [ a.b, a['c'] = 2 ] = [1];
  3252. a.b === 1 && a.c === 2;
  3253. `
  3254. testScript1(SCRIPT, valueTrue, t)
  3255. }
  3256. func TestArrayAssignPatternElision(t *testing.T) {
  3257. const SCRIPT = `
  3258. let a, b;
  3259. ([a,, b] = [1, 4, 2]);
  3260. a === 1 && b === 2;
  3261. `
  3262. testScript1(SCRIPT, valueTrue, t)
  3263. }
  3264. func TestArrayAssignPatternRestPattern(t *testing.T) {
  3265. const SCRIPT = `
  3266. let a, b, z;
  3267. [ z, ...[a, b] ] = [0, 1, 2];
  3268. z === 0 && a === 1 && b === 2;
  3269. `
  3270. testScript1(SCRIPT, valueTrue, t)
  3271. }
  3272. func TestArrayBindingPattern(t *testing.T) {
  3273. const SCRIPT = `
  3274. let [a, b] = [1, 2];
  3275. a === 1 && b === 2;
  3276. `
  3277. testScript1(SCRIPT, valueTrue, t)
  3278. }
  3279. func TestObjectPatternShorthandInit(t *testing.T) {
  3280. const SCRIPT = `
  3281. [...{ x = 1 }] = [];
  3282. x;
  3283. `
  3284. testScript1(SCRIPT, valueInt(1), t)
  3285. }
  3286. func TestArrayBindingPatternRestPattern(t *testing.T) {
  3287. const SCRIPT = `
  3288. const [a, b, ...[c, d]] = [1, 2, 3, 4];
  3289. a === 1 && b === 2 && c === 3 && d === 4;
  3290. `
  3291. testScript1(SCRIPT, valueTrue, t)
  3292. }
  3293. func TestForVarPattern(t *testing.T) {
  3294. const SCRIPT = `
  3295. var o = {a: 1};
  3296. var trace = "";
  3297. for (var [key, value] of Object.entries(o)) {
  3298. trace += key+":"+value;
  3299. }
  3300. trace;
  3301. `
  3302. testScript1(SCRIPT, asciiString("a:1"), t)
  3303. }
  3304. func TestForLexPattern(t *testing.T) {
  3305. const SCRIPT = `
  3306. var o = {a: 1};
  3307. var trace = "";
  3308. for (const [key, value] of Object.entries(o)) {
  3309. trace += key+":"+value;
  3310. }
  3311. trace;
  3312. `
  3313. testScript1(SCRIPT, asciiString("a:1"), t)
  3314. }
  3315. func TestBindingPatternRestTrailingComma(t *testing.T) {
  3316. const SCRIPT = `
  3317. const [a, b, ...rest,] = [];
  3318. `
  3319. _, err := Compile("", SCRIPT, false)
  3320. if err == nil {
  3321. t.Fatal("Expected an error")
  3322. }
  3323. }
  3324. func TestAssignPatternRestTrailingComma(t *testing.T) {
  3325. const SCRIPT = `
  3326. ([a, b, ...rest,] = []);
  3327. `
  3328. _, err := Compile("", SCRIPT, false)
  3329. if err == nil {
  3330. t.Fatal("Expected an error")
  3331. }
  3332. }
  3333. func TestFuncParamInitializerSimple(t *testing.T) {
  3334. const SCRIPT = `
  3335. function f(a = 1) {
  3336. return a;
  3337. }
  3338. ""+f()+f(2);
  3339. `
  3340. testScript1(SCRIPT, asciiString("12"), t)
  3341. }
  3342. func TestFuncParamObjectPatternSimple(t *testing.T) {
  3343. const SCRIPT = `
  3344. function f({a, b} = {a: 1, b: 2}) {
  3345. return "" + a + b;
  3346. }
  3347. ""+f()+" "+f({a: 3, b: 4});
  3348. `
  3349. testScript1(SCRIPT, asciiString("12 34"), t)
  3350. }
  3351. func TestFuncParamRestStackSimple(t *testing.T) {
  3352. const SCRIPT = `
  3353. function f(arg1, ...rest) {
  3354. return rest;
  3355. }
  3356. let ar = f(1, 2, 3);
  3357. ar.join(",");
  3358. `
  3359. testScript1(SCRIPT, asciiString("2,3"), t)
  3360. }
  3361. func TestFuncParamRestStashSimple(t *testing.T) {
  3362. const SCRIPT = `
  3363. function f(arg1, ...rest) {
  3364. eval("true");
  3365. return rest;
  3366. }
  3367. let ar = f(1, 2, 3);
  3368. ar.join(",");
  3369. `
  3370. testScript1(SCRIPT, asciiString("2,3"), t)
  3371. }
  3372. func TestRestArgsNotInStash(t *testing.T) {
  3373. const SCRIPT = `
  3374. function f(...rest) {
  3375. () => rest;
  3376. return rest.length;
  3377. }
  3378. f(1,2);
  3379. `
  3380. testScript1(SCRIPT, valueInt(2), t)
  3381. }
  3382. func TestRestArgsInStash(t *testing.T) {
  3383. const SCRIPT = `
  3384. function f(first, ...rest) {
  3385. () => first;
  3386. () => rest;
  3387. return rest.length;
  3388. }
  3389. f(1,2);
  3390. `
  3391. testScript1(SCRIPT, valueInt(1), t)
  3392. }
  3393. func TestRestArgsInStashFwdRef(t *testing.T) {
  3394. const SCRIPT = `
  3395. function f(first = eval(), ...rest) {
  3396. () => first;
  3397. () => rest;
  3398. return rest.length === 1 && rest[0] === 2;
  3399. }
  3400. f(1,2);
  3401. `
  3402. testScript1(SCRIPT, valueTrue, t)
  3403. }
  3404. func TestFuncParamRestPattern(t *testing.T) {
  3405. const SCRIPT = `
  3406. function f(arg1, ...{0: rest1, 1: rest2}) {
  3407. return ""+arg1+" "+rest1+" "+rest2;
  3408. }
  3409. f(1, 2, 3);
  3410. `
  3411. testScript1(SCRIPT, asciiString("1 2 3"), t)
  3412. }
  3413. func TestFuncParamForwardRef(t *testing.T) {
  3414. const SCRIPT = `
  3415. function f(a = b + 1, b) {
  3416. return ""+a+" "+b;
  3417. }
  3418. f(1, 2);
  3419. `
  3420. testScript1(SCRIPT, asciiString("1 2"), t)
  3421. }
  3422. func TestFuncParamForwardRefMissing(t *testing.T) {
  3423. const SCRIPT = `
  3424. function f(a = b + 1, b) {
  3425. return ""+a+" "+b;
  3426. }
  3427. assert.throws(ReferenceError, function() {
  3428. f();
  3429. });
  3430. `
  3431. testScript1(TESTLIB+SCRIPT, _undefined, t)
  3432. }
  3433. func TestFuncParamInnerRef(t *testing.T) {
  3434. const SCRIPT = `
  3435. function f(a = inner) {
  3436. var inner = 42;
  3437. return a;
  3438. }
  3439. assert.throws(ReferenceError, function() {
  3440. f();
  3441. });
  3442. `
  3443. testScript1(TESTLIB+SCRIPT, _undefined, t)
  3444. }
  3445. func TestFuncParamInnerRefEval(t *testing.T) {
  3446. const SCRIPT = `
  3447. function f(a = eval("inner")) {
  3448. var inner = 42;
  3449. return a;
  3450. }
  3451. assert.throws(ReferenceError, function() {
  3452. f();
  3453. });
  3454. `
  3455. testScript1(TESTLIB+SCRIPT, _undefined, t)
  3456. }
  3457. func TestFuncParamCalleeName(t *testing.T) {
  3458. const SCRIPT = `
  3459. function f(a = f) {
  3460. var f;
  3461. return f;
  3462. }
  3463. typeof f();
  3464. `
  3465. testScript1(SCRIPT, asciiString("undefined"), t)
  3466. }
  3467. func TestFuncParamVarCopy(t *testing.T) {
  3468. const SCRIPT = `
  3469. function f(a = f) {
  3470. var a;
  3471. return a;
  3472. }
  3473. typeof f();
  3474. `
  3475. testScript1(SCRIPT, asciiString("function"), t)
  3476. }
  3477. func TestFuncParamScope(t *testing.T) {
  3478. const SCRIPT = `
  3479. var x = 'outside';
  3480. var probe1, probe2;
  3481. function f(
  3482. _ = probe1 = function() { return x; },
  3483. __ = (eval('var x = "inside";'), probe2 = function() { return x; })
  3484. ) {
  3485. }
  3486. f();
  3487. probe1()+" "+probe2();
  3488. `
  3489. testScript1(SCRIPT, asciiString("inside inside"), t)
  3490. }
  3491. func TestDefParamsStackPtr(t *testing.T) {
  3492. const SCRIPT = `
  3493. function A() {};
  3494. A.B = function () {};
  3495. function D(message = '') {
  3496. var C = A.B;
  3497. C([1,2,3]);
  3498. };
  3499. D();
  3500. `
  3501. testScript1(SCRIPT, _undefined, t)
  3502. }
  3503. func TestNestedVariadicCalls(t *testing.T) {
  3504. const SCRIPT = `
  3505. function f() {
  3506. return Array.prototype.join.call(arguments, ",");
  3507. }
  3508. f(...[1], "a", f(...[2]));
  3509. `
  3510. testScript1(SCRIPT, asciiString("1,a,2"), t)
  3511. }
  3512. func TestVariadicNew(t *testing.T) {
  3513. const SCRIPT = `
  3514. function C() {
  3515. this.res = Array.prototype.join.call(arguments, ",");
  3516. }
  3517. var c = new C(...[1], "a", new C(...[2]).res);
  3518. c.res;
  3519. `
  3520. testScript1(SCRIPT, asciiString("1,a,2"), t)
  3521. }
  3522. func TestVariadicUseStackVars(t *testing.T) {
  3523. const SCRIPT = `
  3524. function A(message) { return message; }
  3525. function B(...args){
  3526. return A(...args);
  3527. }
  3528. B("C");
  3529. `
  3530. testScript1(SCRIPT, asciiString("C"), t)
  3531. }
  3532. func TestCatchParamPattern(t *testing.T) {
  3533. const SCRIPT = `
  3534. function f() {
  3535. let x = 3;
  3536. try {
  3537. throw {a: 1, b: 2};
  3538. } catch ({a, b, c = x}) {
  3539. let x = 99;
  3540. return ""+a+" "+b+" "+c;
  3541. }
  3542. }
  3543. f();
  3544. `
  3545. testScript1(SCRIPT, asciiString("1 2 3"), t)
  3546. }
  3547. func TestArrowUseStrict(t *testing.T) {
  3548. // simple parameter list -- ok
  3549. _, err := Compile("", "(a) => {'use strict';}", false)
  3550. if err != nil {
  3551. t.Fatal(err)
  3552. }
  3553. // non-simple parameter list -- syntax error
  3554. _, err = Compile("", "(a=0) => {'use strict';}", false)
  3555. if err == nil {
  3556. t.Fatal("expected error")
  3557. }
  3558. }
  3559. func TestArrowBoxedThis(t *testing.T) {
  3560. const SCRIPT = `
  3561. var context;
  3562. fn = function() {
  3563. return (arg) => { var local; context = this; };
  3564. };
  3565. fn()();
  3566. context === this;
  3567. `
  3568. testScript1(SCRIPT, valueTrue, t)
  3569. }
  3570. func TestParameterOverride(t *testing.T) {
  3571. const SCRIPT = `
  3572. function f(arg) {
  3573. var arg = arg || "default"
  3574. return arg
  3575. }
  3576. f()
  3577. `
  3578. testScript1(SCRIPT, asciiString("default"), t)
  3579. }
  3580. func TestEvalInIterScope(t *testing.T) {
  3581. const SCRIPT = `
  3582. for (let a = 0; a < 1; a++) {
  3583. eval("a");
  3584. }
  3585. `
  3586. testScript1(SCRIPT, valueInt(0), t)
  3587. }
  3588. func TestTemplateLiterals(t *testing.T) {
  3589. vm := New()
  3590. _, err := vm.RunString("const a = 1, b = 'b';")
  3591. if err != nil {
  3592. t.Fatal(err)
  3593. }
  3594. f := func(t *testing.T, template, expected string) {
  3595. res, err := vm.RunString(template)
  3596. if err != nil {
  3597. t.Fatal(err)
  3598. }
  3599. if actual := res.Export(); actual != expected {
  3600. t.Fatalf("Expected: %q, actual: %q", expected, actual)
  3601. }
  3602. }
  3603. t.Run("empty", func(t *testing.T) {
  3604. f(t, "``", "")
  3605. })
  3606. t.Run("noSub", func(t *testing.T) {
  3607. f(t, "`test`", "test")
  3608. })
  3609. t.Run("emptyTail", func(t *testing.T) {
  3610. f(t, "`a=${a},b=${b}`", "a=1,b=b")
  3611. })
  3612. t.Run("emptyHead", func(t *testing.T) {
  3613. f(t, "`${a},b=${b}$`", "1,b=b$")
  3614. })
  3615. t.Run("headAndTail", func(t *testing.T) {
  3616. f(t, "`a=${a},b=${b}$`", "a=1,b=b$")
  3617. })
  3618. }
  3619. func TestTaggedTemplate(t *testing.T) {
  3620. const SCRIPT = `
  3621. let res;
  3622. const o = {
  3623. tmpl() {
  3624. res = this;
  3625. return () => {};
  3626. }
  3627. }
  3628. ` +
  3629. "o.tmpl()`test`;" + `
  3630. res === o;
  3631. `
  3632. testScript1(SCRIPT, valueTrue, t)
  3633. }
  3634. /*
  3635. func TestBabel(t *testing.T) {
  3636. src, err := ioutil.ReadFile("babel7.js")
  3637. if err != nil {
  3638. t.Fatal(err)
  3639. }
  3640. vm := New()
  3641. _, err = vm.RunString(string(src))
  3642. if err != nil {
  3643. t.Fatal(err)
  3644. }
  3645. _, err = vm.RunString(`var result = Babel.transform("", {presets: ["es2015"]});`)
  3646. if err != nil {
  3647. t.Fatal(err)
  3648. }
  3649. }*/
  3650. func BenchmarkCompile(b *testing.B) {
  3651. data, err := ioutil.ReadFile("testdata/S15.10.2.12_A1_T1.js")
  3652. if err != nil {
  3653. b.Fatal(err)
  3654. }
  3655. src := string(data)
  3656. for i := 0; i < b.N; i++ {
  3657. _, err := Compile("test.js", src, false)
  3658. if err != nil {
  3659. b.Fatal(err)
  3660. }
  3661. }
  3662. }