compiler_test.go 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582
  1. package goja
  2. import (
  3. "io/ioutil"
  4. "sync"
  5. "testing"
  6. )
  7. const TESTLIB = `
  8. function $ERROR(message) {
  9. throw new Error(message);
  10. }
  11. function Test262Error() {
  12. }
  13. function assert(mustBeTrue, message) {
  14. if (mustBeTrue === true) {
  15. return;
  16. }
  17. if (message === undefined) {
  18. message = 'Expected true but got ' + String(mustBeTrue);
  19. }
  20. $ERROR(message);
  21. }
  22. assert._isSameValue = function (a, b) {
  23. if (a === b) {
  24. // Handle +/-0 vs. -/+0
  25. return a !== 0 || 1 / a === 1 / b;
  26. }
  27. // Handle NaN vs. NaN
  28. return a !== a && b !== b;
  29. };
  30. assert.sameValue = function (actual, expected, message) {
  31. if (assert._isSameValue(actual, expected)) {
  32. return;
  33. }
  34. if (message === undefined) {
  35. message = '';
  36. } else {
  37. message += ' ';
  38. }
  39. message += 'Expected SameValue(«' + String(actual) + '», «' + String(expected) + '») to be true';
  40. $ERROR(message);
  41. };
  42. assert.throws = function (expectedErrorConstructor, func, message) {
  43. if (typeof func !== "function") {
  44. $ERROR('assert.throws requires two arguments: the error constructor ' +
  45. 'and a function to run');
  46. return;
  47. }
  48. if (message === undefined) {
  49. message = '';
  50. } else {
  51. message += ' ';
  52. }
  53. try {
  54. func();
  55. } catch (thrown) {
  56. if (typeof thrown !== 'object' || thrown === null) {
  57. message += 'Thrown value was not an object!';
  58. $ERROR(message);
  59. } else if (thrown.constructor !== expectedErrorConstructor) {
  60. message += 'Expected a ' + expectedErrorConstructor.name + ' but got a ' + thrown.constructor.name;
  61. $ERROR(message);
  62. }
  63. return;
  64. }
  65. message += 'Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all';
  66. $ERROR(message);
  67. };
  68. function compareArray(a, b) {
  69. if (b.length !== a.length) {
  70. return false;
  71. }
  72. for (var i = 0; i < a.length; i++) {
  73. if (b[i] !== a[i]) {
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. `
  80. const TESTLIBX = `
  81. function looksNative(fn) {
  82. return /native code/.test(Function.prototype.toString.call(fn));
  83. }
  84. function deepEqual(a, b) {
  85. if (typeof a === "object") {
  86. if (typeof b === "object") {
  87. if (a === b) {
  88. return true;
  89. }
  90. if (Reflect.getPrototypeOf(a) !== Reflect.getPrototypeOf(b)) {
  91. return false;
  92. }
  93. var keysA = Object.keys(a);
  94. var keysB = Object.keys(b);
  95. if (keysA.length !== keysB.length) {
  96. return false;
  97. }
  98. if (!compareArray(keysA.sort(), keysB.sort())) {
  99. return false;
  100. }
  101. for (var i = 0; i < keysA.length; i++) {
  102. var key = keysA[i];
  103. if (!deepEqual(a[key], b[key])) {
  104. return false;
  105. }
  106. }
  107. return true;
  108. } else {
  109. return false;
  110. }
  111. }
  112. return assert._isSameValue(a, b);
  113. }
  114. `
  115. var (
  116. // The reason it's implemented this way rather than just as _testLib = MustCompile(...)
  117. // is because when you try to debug the compiler and set a breakpoint it gets triggered during the
  118. // initialisation which is annoying.
  119. _testLib, _testLibX *Program
  120. testLibOnce, testLibXOnce sync.Once
  121. )
  122. func testLib() *Program {
  123. testLibOnce.Do(func() {
  124. _testLib = MustCompile("testlib.js", TESTLIB, false)
  125. })
  126. return _testLib
  127. }
  128. func testLibX() *Program {
  129. testLibXOnce.Do(func() {
  130. _testLibX = MustCompile("testlibx.js", TESTLIBX, false)
  131. })
  132. return _testLibX
  133. }
  134. func (r *Runtime) testPrg(p *Program, expectedResult Value, t *testing.T) {
  135. vm := r.vm
  136. vm.prg = p
  137. vm.pc = 0
  138. vm.prg.dumpCode(t.Logf)
  139. vm.result = _undefined
  140. vm.run()
  141. v := vm.result
  142. t.Logf("stack size: %d", len(vm.stack))
  143. t.Logf("stashAllocs: %d", vm.stashAllocs)
  144. if v == nil && expectedResult != nil || !v.SameAs(expectedResult) {
  145. t.Fatalf("Result: %+v, expected: %+v", v, expectedResult)
  146. }
  147. if vm.sp != 0 {
  148. t.Fatalf("sp: %d", vm.sp)
  149. }
  150. if l := len(vm.iterStack); l > 0 {
  151. t.Fatalf("iter stack is not empty: %d", l)
  152. }
  153. }
  154. func (r *Runtime) testScriptWithTestLib(script string, expectedResult Value, t *testing.T) {
  155. _, err := r.RunProgram(testLib())
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. r.testScript(script, expectedResult, t)
  160. }
  161. func (r *Runtime) testScriptWithTestLibX(script string, expectedResult Value, t *testing.T) {
  162. _, err := r.RunProgram(testLib())
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. _, err = r.RunProgram(testLibX())
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. r.testScript(script, expectedResult, t)
  171. }
  172. func (r *Runtime) testScript(script string, expectedResult Value, t *testing.T) {
  173. r.testPrg(MustCompile("test.js", script, false), expectedResult, t)
  174. }
  175. func testScript(script string, expectedResult Value, t *testing.T) {
  176. New().testScript(script, expectedResult, t)
  177. }
  178. func testScriptWithTestLib(script string, expectedResult Value, t *testing.T) {
  179. New().testScriptWithTestLib(script, expectedResult, t)
  180. }
  181. func testScriptWithTestLibX(script string, expectedResult Value, t *testing.T) {
  182. New().testScriptWithTestLibX(script, expectedResult, t)
  183. }
  184. func TestEmptyProgram(t *testing.T) {
  185. const SCRIPT = `
  186. `
  187. testScript(SCRIPT, _undefined, t)
  188. }
  189. func TestResultEmptyBlock(t *testing.T) {
  190. const SCRIPT = `
  191. undefined;
  192. {}
  193. `
  194. testScript(SCRIPT, _undefined, t)
  195. }
  196. func TestResultVarDecl(t *testing.T) {
  197. const SCRIPT = `
  198. 7; var x = 1;
  199. `
  200. testScript(SCRIPT, valueInt(7), t)
  201. }
  202. func TestResultLexDecl(t *testing.T) {
  203. const SCRIPT = `
  204. 7; {let x = 1};
  205. `
  206. testScript(SCRIPT, valueInt(7), t)
  207. }
  208. func TestResultLexDeclBreak(t *testing.T) {
  209. const SCRIPT = `
  210. L:{ 7; {let x = 1; break L;}};
  211. `
  212. testScript(SCRIPT, valueInt(7), t)
  213. }
  214. func TestResultLexDeclNested(t *testing.T) {
  215. const SCRIPT = `
  216. 7; {let x = (function() { return eval("8; {let y = 9}")})()};
  217. `
  218. testScript(SCRIPT, valueInt(7), t)
  219. }
  220. func TestErrorProto(t *testing.T) {
  221. const SCRIPT = `
  222. var e = new TypeError();
  223. e.name;
  224. `
  225. testScript(SCRIPT, asciiString("TypeError"), t)
  226. }
  227. func TestThis1(t *testing.T) {
  228. const SCRIPT = `
  229. function independent() {
  230. return this.prop;
  231. }
  232. var o = {};
  233. o.b = {g: independent, prop: 42};
  234. o.b.g();
  235. `
  236. testScript(SCRIPT, intToValue(42), t)
  237. }
  238. func TestThis2(t *testing.T) {
  239. const SCRIPT = `
  240. var o = {
  241. prop: 37,
  242. f: function() {
  243. return this.prop;
  244. }
  245. };
  246. o.f();
  247. `
  248. testScript(SCRIPT, intToValue(37), t)
  249. }
  250. func TestThisStrict(t *testing.T) {
  251. const SCRIPT = `
  252. "use strict";
  253. Object.defineProperty(Object.prototype, "x", { get: function () { return this; } });
  254. (5).x === 5;
  255. `
  256. testScript(SCRIPT, valueTrue, t)
  257. }
  258. func TestThisNoStrict(t *testing.T) {
  259. const SCRIPT = `
  260. Object.defineProperty(Object.prototype, "x", { get: function () { return this; } });
  261. (5).x == 5;
  262. `
  263. testScript(SCRIPT, valueTrue, t)
  264. }
  265. func TestNestedFuncVarResolution(t *testing.T) {
  266. const SCRIPT = `
  267. (function outer() {
  268. var v = 42;
  269. function inner() {
  270. return v;
  271. }
  272. return inner();
  273. })();
  274. `
  275. testScript(SCRIPT, valueInt(42), t)
  276. }
  277. func TestNestedFuncVarResolution1(t *testing.T) {
  278. const SCRIPT = `
  279. function outer(argOuter) {
  280. var called = 0;
  281. var inner = function(argInner) {
  282. if (arguments.length !== 1) {
  283. throw new Error();
  284. }
  285. called++;
  286. if (argOuter !== 1) {
  287. throw new Error("argOuter");
  288. }
  289. if (argInner !== 2) {
  290. throw new Error("argInner");
  291. }
  292. };
  293. inner(2);
  294. }
  295. outer(1);
  296. `
  297. testScript(SCRIPT, _undefined, t)
  298. }
  299. func TestCallFewerArgs(t *testing.T) {
  300. const SCRIPT = `
  301. function A(a, b, c) {
  302. return String(a) + " " + String(b) + " " + String(c);
  303. }
  304. A(1, 2);
  305. `
  306. testScript(SCRIPT, asciiString("1 2 undefined"), t)
  307. }
  308. func TestCallFewerArgsClosureNoArgs(t *testing.T) {
  309. const SCRIPT = `
  310. var x;
  311. function A(a, b, c) {
  312. var y = a;
  313. x = function() { return " " + y };
  314. return String(a) + " " + String(b) + " " + String(c);
  315. }
  316. A(1, 2) + x();
  317. `
  318. testScript(SCRIPT, asciiString("1 2 undefined 1"), t)
  319. }
  320. func TestCallFewerArgsClosureArgs(t *testing.T) {
  321. const SCRIPT = `
  322. var x;
  323. function A(a, b, c) {
  324. var y = b;
  325. x = function() { return " " + a + " " + y };
  326. return String(a) + " " + String(b) + " " + String(c);
  327. }
  328. A(1, 2) + x();
  329. `
  330. testScript(SCRIPT, asciiString("1 2 undefined 1 2"), t)
  331. }
  332. func TestCallMoreArgs(t *testing.T) {
  333. const SCRIPT = `
  334. function A(a, b) {
  335. var c = 4;
  336. return a - b + c;
  337. }
  338. A(1, 2, 3);
  339. `
  340. testScript(SCRIPT, intToValue(3), t)
  341. }
  342. func TestCallMoreArgsDynamic(t *testing.T) {
  343. const SCRIPT = `
  344. function A(a, b) {
  345. var c = 4;
  346. if (false) {
  347. eval("");
  348. }
  349. return a - b + c;
  350. }
  351. A(1, 2, 3);
  352. `
  353. testScript(SCRIPT, intToValue(3), t)
  354. }
  355. func TestCallLessArgsDynamic(t *testing.T) {
  356. const SCRIPT = `
  357. function A(a, b, c) {
  358. // Make it stashful
  359. function B() {
  360. return a;
  361. }
  362. return String(a) + " " + String(b) + " " + String(c);
  363. }
  364. A(1, 2);
  365. `
  366. testScript(SCRIPT, asciiString("1 2 undefined"), t)
  367. }
  368. func TestCallLessArgsDynamicLocalVar(t *testing.T) {
  369. const SCRIPT = `
  370. function f(param) {
  371. var a = 42;
  372. if (false) {
  373. eval("");
  374. }
  375. return a;
  376. }
  377. f();
  378. `
  379. testScript(SCRIPT, intToValue(42), t)
  380. }
  381. /*
  382. func TestFib(t *testing.T) {
  383. testScript(TEST_FIB, valueInt(9227465), t)
  384. }
  385. */
  386. func TestNativeCall(t *testing.T) {
  387. const SCRIPT = `
  388. var o = Object(1);
  389. Object.defineProperty(o, "test", {value: 42});
  390. o.test;
  391. `
  392. testScript(SCRIPT, intToValue(42), t)
  393. }
  394. func TestJSCall(t *testing.T) {
  395. const SCRIPT = `
  396. function getter() {
  397. return this.x;
  398. }
  399. var o = Object(1);
  400. o.x = 42;
  401. Object.defineProperty(o, "test", {get: getter});
  402. o.test;
  403. `
  404. testScript(SCRIPT, intToValue(42), t)
  405. }
  406. func TestLoop1(t *testing.T) {
  407. const SCRIPT = `
  408. function A() {
  409. var x = 1;
  410. for (var i = 0; i < 1; i++) {
  411. var x = 2;
  412. }
  413. return x;
  414. }
  415. A();
  416. `
  417. testScript(SCRIPT, intToValue(2), t)
  418. }
  419. func TestLoopBreak(t *testing.T) {
  420. const SCRIPT = `
  421. function A() {
  422. var x = 1;
  423. for (var i = 0; i < 1; i++) {
  424. break;
  425. var x = 2;
  426. }
  427. return x;
  428. }
  429. A();
  430. `
  431. testScript(SCRIPT, intToValue(1), t)
  432. }
  433. func TestForLoopOptionalExpr(t *testing.T) {
  434. const SCRIPT = `
  435. function A() {
  436. var x = 1;
  437. for (;;) {
  438. break;
  439. var x = 2;
  440. }
  441. return x;
  442. }
  443. A();
  444. `
  445. testScript(SCRIPT, intToValue(1), t)
  446. }
  447. func TestBlockBreak(t *testing.T) {
  448. const SCRIPT = `
  449. var rv = 0;
  450. B1: {
  451. rv = 1;
  452. B2: {
  453. rv = 2;
  454. break B1;
  455. }
  456. rv = 3;
  457. }
  458. rv;
  459. `
  460. testScript(SCRIPT, intToValue(2), t)
  461. }
  462. func TestTry(t *testing.T) {
  463. const SCRIPT = `
  464. function A() {
  465. var x = 1;
  466. try {
  467. x = 2;
  468. } catch(e) {
  469. x = 3;
  470. } finally {
  471. x = 4;
  472. }
  473. return x;
  474. }
  475. A();
  476. `
  477. testScript(SCRIPT, intToValue(4), t)
  478. }
  479. func TestTryOptionalCatchBinding(t *testing.T) {
  480. const SCRIPT = `
  481. try {
  482. throw null;
  483. } catch {
  484. }
  485. `
  486. testScript(SCRIPT, _undefined, t)
  487. }
  488. func TestTryCatch(t *testing.T) {
  489. const SCRIPT = `
  490. function A() {
  491. var x;
  492. try {
  493. throw 4;
  494. } catch(e) {
  495. x = e;
  496. }
  497. return x;
  498. }
  499. A();
  500. `
  501. testScript(SCRIPT, intToValue(4), t)
  502. }
  503. func TestTryCatchDirectEval(t *testing.T) {
  504. const SCRIPT = `
  505. function A() {
  506. var x;
  507. try {
  508. throw 4;
  509. } catch(e) {
  510. eval("x = e");
  511. }
  512. return x;
  513. }
  514. A();
  515. `
  516. testScript(SCRIPT, intToValue(4), t)
  517. }
  518. func TestTryExceptionInCatch(t *testing.T) {
  519. const SCRIPT = `
  520. function A() {
  521. var x;
  522. try {
  523. throw 4;
  524. } catch(e) {
  525. throw 5;
  526. }
  527. return x;
  528. }
  529. var rv;
  530. try {
  531. A();
  532. } catch (e) {
  533. rv = e;
  534. }
  535. rv;
  536. `
  537. testScript(SCRIPT, intToValue(5), t)
  538. }
  539. func TestTryContinueInCatch(t *testing.T) {
  540. const SCRIPT = `
  541. var c3 = 0, fin3 = 0;
  542. while (c3 < 2) {
  543. try {
  544. throw "ex1";
  545. } catch(er1) {
  546. c3 += 1;
  547. continue;
  548. } finally {
  549. fin3 = 1;
  550. }
  551. fin3 = 0;
  552. }
  553. fin3;
  554. `
  555. testScript(SCRIPT, intToValue(1), t)
  556. }
  557. func TestContinueInWith(t *testing.T) {
  558. const SCRIPT = `
  559. var x;
  560. var o = {x: 0};
  561. for (var i = 0; i < 2; i++) {
  562. with(o) {
  563. x = i;
  564. if (i === 0) {
  565. continue;
  566. }
  567. }
  568. break;
  569. }
  570. x;
  571. `
  572. testScript(SCRIPT, _undefined, t)
  573. }
  574. func TestTryContinueInFinally(t *testing.T) {
  575. const SCRIPT = `
  576. var c3 = 0, fin3 = 0;
  577. while (c3 < 2) {
  578. try {
  579. throw "ex1";
  580. } catch(er1) {
  581. c3 += 1;
  582. } finally {
  583. fin3 = 1;
  584. continue;
  585. }
  586. fin3 = 0;
  587. }
  588. fin3;
  589. `
  590. testScript(SCRIPT, intToValue(1), t)
  591. }
  592. func TestTryBreakFinallyContinue(t *testing.T) {
  593. const SCRIPT = `
  594. for (var i = 0; i < 3; i++) {
  595. try {
  596. break;
  597. } finally {
  598. continue;
  599. }
  600. }
  601. `
  602. testScript(SCRIPT, _undefined, t)
  603. }
  604. func TestTryBreakFinallyContinueWithResult(t *testing.T) {
  605. const SCRIPT = `
  606. for (var i = 0; i < 3; i++) {
  607. try {
  608. true;
  609. break;
  610. } finally {
  611. continue;
  612. }
  613. }
  614. `
  615. testScript(SCRIPT, _undefined, t)
  616. }
  617. func TestTryBreakFinallyContinueWithResult1(t *testing.T) {
  618. const SCRIPT = `
  619. for (var i = 0; i < 3; i++) {
  620. try {
  621. true;
  622. break;
  623. } finally {
  624. var x = 1;
  625. continue;
  626. }
  627. }
  628. `
  629. testScript(SCRIPT, _undefined, t)
  630. }
  631. func TestTryBreakFinallyContinueWithResultNested(t *testing.T) {
  632. const SCRIPT = `
  633. LOOP:
  634. for (var i = 0; i < 3; i++) {
  635. try {
  636. if (true) {
  637. false; break;
  638. }
  639. } finally {
  640. if (true) {
  641. true; continue;
  642. }
  643. }
  644. }
  645. `
  646. testScript(SCRIPT, valueTrue, t)
  647. }
  648. func TestTryBreakOuterFinallyContinue(t *testing.T) {
  649. const SCRIPT = `
  650. let iCount = 0, jCount = 0;
  651. OUTER: for (let i = 0; i < 1; i++) {
  652. iCount++;
  653. for (let j = 0; j < 2; j++) {
  654. jCount++;
  655. try {
  656. break OUTER;
  657. } finally {
  658. continue;
  659. }
  660. }
  661. }
  662. ""+iCount+jCount;
  663. `
  664. testScript(SCRIPT, asciiString("12"), t)
  665. }
  666. func TestTryIllegalContinueWithFinallyOverride(t *testing.T) {
  667. const SCRIPT = `
  668. L: {
  669. while (Math.random() > 0.5) {
  670. try {
  671. continue L;
  672. } finally {
  673. break;
  674. }
  675. }
  676. }
  677. `
  678. _, err := Compile("", SCRIPT, false)
  679. if err == nil {
  680. t.Fatal("expected error")
  681. }
  682. }
  683. func TestTryIllegalContinueWithFinallyOverrideNoLabel(t *testing.T) {
  684. const SCRIPT = `
  685. L: {
  686. try {
  687. continue;
  688. } finally {
  689. break L;
  690. }
  691. }
  692. `
  693. _, err := Compile("", SCRIPT, false)
  694. if err == nil {
  695. t.Fatal("expected error")
  696. }
  697. }
  698. func TestTryIllegalContinueWithFinallyOverrideDummy(t *testing.T) {
  699. const SCRIPT = `
  700. L: {
  701. while (false) {
  702. try {
  703. continue L;
  704. } finally {
  705. break;
  706. }
  707. }
  708. }
  709. `
  710. _, err := Compile("", SCRIPT, false)
  711. if err == nil {
  712. t.Fatal("expected error")
  713. }
  714. }
  715. func TestTryNoResult(t *testing.T) {
  716. const SCRIPT = `
  717. true;
  718. L:
  719. try {
  720. break L;
  721. } finally {
  722. }
  723. `
  724. testScript(SCRIPT, _undefined, t)
  725. }
  726. func TestCatchLexicalEnv(t *testing.T) {
  727. const SCRIPT = `
  728. function F() {
  729. try {
  730. throw 1;
  731. } catch (e) {
  732. var x = e;
  733. }
  734. return x;
  735. }
  736. F();
  737. `
  738. testScript(SCRIPT, intToValue(1), t)
  739. }
  740. func TestThrowType(t *testing.T) {
  741. const SCRIPT = `
  742. function Exception(message) {
  743. this.message = message;
  744. }
  745. function A() {
  746. try {
  747. throw new Exception("boo!");
  748. } catch(e) {
  749. return e;
  750. }
  751. }
  752. var thrown = A();
  753. thrown !== null && typeof thrown === "object" && thrown.constructor === Exception;
  754. `
  755. testScript(SCRIPT, valueTrue, t)
  756. }
  757. func TestThrowConstructorName(t *testing.T) {
  758. const SCRIPT = `
  759. function Exception(message) {
  760. this.message = message;
  761. }
  762. function A() {
  763. try {
  764. throw new Exception("boo!");
  765. } catch(e) {
  766. return e;
  767. }
  768. }
  769. A().constructor.name;
  770. `
  771. testScript(SCRIPT, asciiString("Exception"), t)
  772. }
  773. func TestThrowNativeConstructorName(t *testing.T) {
  774. const SCRIPT = `
  775. function A() {
  776. try {
  777. throw new TypeError();
  778. } catch(e) {
  779. return e;
  780. }
  781. }
  782. A().constructor.name;
  783. `
  784. testScript(SCRIPT, asciiString("TypeError"), t)
  785. }
  786. func TestEmptyTryNoCatch(t *testing.T) {
  787. const SCRIPT = `
  788. var called = false;
  789. try {
  790. } finally {
  791. called = true;
  792. }
  793. called;
  794. `
  795. testScript(SCRIPT, valueTrue, t)
  796. }
  797. func TestTryReturnFromCatch(t *testing.T) {
  798. const SCRIPT = `
  799. function f(o) {
  800. var x = 42;
  801. function innerf(o) {
  802. try {
  803. throw o;
  804. } catch (e) {
  805. return x;
  806. }
  807. }
  808. return innerf(o);
  809. }
  810. f({});
  811. `
  812. testScript(SCRIPT, valueInt(42), t)
  813. }
  814. func TestTryCompletionResult(t *testing.T) {
  815. const SCRIPT = `
  816. 99; do { -99; try { 39 } catch (e) { -1 } finally { break; -2 }; } while (false);
  817. `
  818. testScript(SCRIPT, _undefined, t)
  819. }
  820. func TestIfElse(t *testing.T) {
  821. const SCRIPT = `
  822. var rv;
  823. if (rv === undefined) {
  824. rv = "passed";
  825. } else {
  826. rv = "failed";
  827. }
  828. rv;
  829. `
  830. testScript(SCRIPT, asciiString("passed"), t)
  831. }
  832. func TestIfElseRetVal(t *testing.T) {
  833. const SCRIPT = `
  834. var x;
  835. if (x === undefined) {
  836. "passed";
  837. } else {
  838. "failed";
  839. }
  840. `
  841. testScript(SCRIPT, asciiString("passed"), t)
  842. }
  843. func TestWhileReturnValue(t *testing.T) {
  844. const SCRIPT = `
  845. var x = 0;
  846. while(true) {
  847. x = 1;
  848. break;
  849. }
  850. `
  851. testScript(SCRIPT, intToValue(1), t)
  852. }
  853. func TestIfElseLabel(t *testing.T) {
  854. const SCRIPT = `
  855. var x = 0;
  856. abc: if (true) {
  857. x = 1;
  858. break abc;
  859. }
  860. `
  861. testScript(SCRIPT, intToValue(1), t)
  862. }
  863. func TestIfMultipleLabels(t *testing.T) {
  864. const SCRIPT = `
  865. var x = 0;
  866. xyz:abc: if (true) {
  867. break xyz;
  868. }
  869. `
  870. testScript(SCRIPT, _undefined, t)
  871. }
  872. func TestBreakOutOfTry(t *testing.T) {
  873. const SCRIPT = `
  874. function A() {
  875. var x = 1;
  876. B: {
  877. try {
  878. x = 2;
  879. } catch(e) {
  880. x = 3;
  881. } finally {
  882. break B;
  883. x = 4;
  884. }
  885. }
  886. return x;
  887. }
  888. A();
  889. `
  890. testScript(SCRIPT, intToValue(2), t)
  891. }
  892. func TestReturnOutOfTryNested(t *testing.T) {
  893. const SCRIPT = `
  894. function A() {
  895. function nested() {
  896. try {
  897. return 1;
  898. } catch(e) {
  899. return 2;
  900. }
  901. }
  902. return nested();
  903. }
  904. A();
  905. `
  906. testScript(SCRIPT, intToValue(1), t)
  907. }
  908. func TestContinueLoop(t *testing.T) {
  909. const SCRIPT = `
  910. function A() {
  911. var r = 0;
  912. for (var i = 0; i < 5; i++) {
  913. if (i > 1) {
  914. continue;
  915. }
  916. r++;
  917. }
  918. return r;
  919. }
  920. A();
  921. `
  922. testScript(SCRIPT, intToValue(2), t)
  923. }
  924. func TestContinueOutOfTry(t *testing.T) {
  925. const SCRIPT = `
  926. function A() {
  927. var r = 0;
  928. for (var i = 0; i < 5; i++) {
  929. try {
  930. if (i > 1) {
  931. continue;
  932. }
  933. } catch(e) {
  934. return 99;
  935. }
  936. r++;
  937. }
  938. return r;
  939. }
  940. A();
  941. `
  942. testScript(SCRIPT, intToValue(2), t)
  943. }
  944. func TestThisInCatch(t *testing.T) {
  945. const SCRIPT = `
  946. function O() {
  947. try {
  948. f();
  949. } catch (e) {
  950. this.value = e.toString();
  951. }
  952. }
  953. function f() {
  954. throw "ex";
  955. }
  956. var o = new O();
  957. o.value;
  958. `
  959. testScript(SCRIPT, asciiString("ex"), t)
  960. }
  961. func TestNestedTry(t *testing.T) {
  962. const SCRIPT = `
  963. var ex;
  964. try {
  965. throw "ex1";
  966. } catch (er1) {
  967. try {
  968. throw "ex2";
  969. } catch (er1) {
  970. ex = er1;
  971. }
  972. }
  973. ex;
  974. `
  975. testScript(SCRIPT, asciiString("ex2"), t)
  976. }
  977. func TestNestedTryInStashlessFunc(t *testing.T) {
  978. const SCRIPT = `
  979. function f() {
  980. var ex1, ex2;
  981. try {
  982. throw "ex1";
  983. } catch (er1) {
  984. try {
  985. throw "ex2";
  986. } catch (er1) {
  987. ex2 = er1;
  988. }
  989. ex1 = er1;
  990. }
  991. return ex1 == "ex1" && ex2 == "ex2";
  992. }
  993. f();
  994. `
  995. testScript(SCRIPT, valueTrue, t)
  996. }
  997. func TestEvalLexicalDecl(t *testing.T) {
  998. const SCRIPT = `
  999. eval("let x = true; x;");
  1000. `
  1001. testScript(SCRIPT, valueTrue, t)
  1002. }
  1003. func TestEvalInCatchInStashlessFunc(t *testing.T) {
  1004. const SCRIPT = `
  1005. function f() {
  1006. var ex;
  1007. try {
  1008. throw "ex1";
  1009. } catch (er1) {
  1010. eval("ex = er1");
  1011. }
  1012. return ex;
  1013. }
  1014. f();
  1015. `
  1016. testScript(SCRIPT, asciiString("ex1"), t)
  1017. }
  1018. func TestCatchClosureInStashlessFunc(t *testing.T) {
  1019. const SCRIPT = `
  1020. function f() {
  1021. var ex;
  1022. try {
  1023. throw "ex1";
  1024. } catch (er1) {
  1025. return function() {
  1026. return er1;
  1027. }
  1028. }
  1029. }
  1030. f()();
  1031. `
  1032. testScript(SCRIPT, asciiString("ex1"), t)
  1033. }
  1034. func TestCatchVarNotUsedInStashlessFunc(t *testing.T) {
  1035. const SCRIPT = `
  1036. function f() {
  1037. var ex;
  1038. try {
  1039. throw "ex1";
  1040. } catch (er1) {
  1041. ex = "ok";
  1042. }
  1043. return ex;
  1044. }
  1045. f();
  1046. `
  1047. testScript(SCRIPT, asciiString("ok"), t)
  1048. }
  1049. func TestNew(t *testing.T) {
  1050. const SCRIPT = `
  1051. function O() {
  1052. this.x = 42;
  1053. }
  1054. new O().x;
  1055. `
  1056. testScript(SCRIPT, intToValue(42), t)
  1057. }
  1058. func TestStringConstructor(t *testing.T) {
  1059. const SCRIPT = `
  1060. function F() {
  1061. return String(33) + " " + String("cows");
  1062. }
  1063. F();
  1064. `
  1065. testScript(SCRIPT, asciiString("33 cows"), t)
  1066. }
  1067. func TestError(t *testing.T) {
  1068. const SCRIPT = `
  1069. function F() {
  1070. return new Error("test");
  1071. }
  1072. var e = F();
  1073. e.message == "test" && e.name == "Error";
  1074. `
  1075. testScript(SCRIPT, valueTrue, t)
  1076. }
  1077. func TestTypeError(t *testing.T) {
  1078. const SCRIPT = `
  1079. function F() {
  1080. return new TypeError("test");
  1081. }
  1082. var e = F();
  1083. e.message == "test" && e.name == "TypeError";
  1084. `
  1085. testScript(SCRIPT, valueTrue, t)
  1086. }
  1087. func TestToString(t *testing.T) {
  1088. const SCRIPT = `
  1089. var o = {x: 42};
  1090. o.toString = function() {
  1091. return String(this.x);
  1092. }
  1093. var o1 = {};
  1094. o.toString() + " ### " + o1.toString();
  1095. `
  1096. testScript(SCRIPT, asciiString("42 ### [object Object]"), t)
  1097. }
  1098. func TestEvalOrder(t *testing.T) {
  1099. const SCRIPT = `
  1100. var o = {f: function() {return 42}, x: 0};
  1101. var trace = "";
  1102. function F1() {
  1103. trace += "First!";
  1104. return o;
  1105. }
  1106. function F2() {
  1107. trace += "Second!";
  1108. return "f";
  1109. }
  1110. function F3() {
  1111. trace += "Third!";
  1112. }
  1113. var rv = F1()[F2()](F3());
  1114. rv += trace;
  1115. rv;
  1116. `
  1117. testScript(SCRIPT, asciiString("42First!Second!Third!"), t)
  1118. }
  1119. func TestPostfixIncBracket(t *testing.T) {
  1120. const SCRIPT = `
  1121. var o = {x: 42};
  1122. var trace = "";
  1123. function F1() {
  1124. trace += "First!";
  1125. return o;
  1126. }
  1127. function F2() {
  1128. trace += "Second!";
  1129. return "x";
  1130. }
  1131. var rv = F1()[F2()]++;
  1132. rv + trace + o.x;
  1133. `
  1134. testScript(SCRIPT, asciiString("42First!Second!43"), t)
  1135. }
  1136. func TestPostfixIncDot(t *testing.T) {
  1137. const SCRIPT = `
  1138. var o = {x: 42};
  1139. var trace = "";
  1140. function F1() {
  1141. trace += "First!";
  1142. return o;
  1143. }
  1144. var rv = F1().x++;
  1145. rv + trace + o.x;
  1146. `
  1147. testScript(SCRIPT, asciiString("42First!43"), t)
  1148. }
  1149. func TestPrefixIncBracket(t *testing.T) {
  1150. const SCRIPT = `
  1151. var o = {x: 42};
  1152. var trace = "";
  1153. function F1() {
  1154. trace += "First!";
  1155. return o;
  1156. }
  1157. function F2() {
  1158. trace += "Second!";
  1159. return "x";
  1160. }
  1161. var rv = ++F1()[F2()];
  1162. rv + trace + o.x;
  1163. `
  1164. testScript(SCRIPT, asciiString("43First!Second!43"), t)
  1165. }
  1166. func TestPrefixIncDot(t *testing.T) {
  1167. const SCRIPT = `
  1168. var o = {x: 42};
  1169. var trace = "";
  1170. function F1() {
  1171. trace += "First!";
  1172. return o;
  1173. }
  1174. var rv = ++F1().x;
  1175. rv + trace + o.x;
  1176. `
  1177. testScript(SCRIPT, asciiString("43First!43"), t)
  1178. }
  1179. func TestPostDecObj(t *testing.T) {
  1180. const SCRIPT = `
  1181. var object = {valueOf: function() {return 1}};
  1182. var y = object--;
  1183. var ok = false;
  1184. if (y === 1) {
  1185. ok = true;
  1186. }
  1187. ok;
  1188. `
  1189. testScript(SCRIPT, valueTrue, t)
  1190. }
  1191. func TestPropAcc1(t *testing.T) {
  1192. const SCRIPT = `
  1193. 1..toString()
  1194. `
  1195. testScript(SCRIPT, asciiString("1"), t)
  1196. }
  1197. func TestEvalDirect(t *testing.T) {
  1198. const SCRIPT = `
  1199. var rv = false;
  1200. function foo(){ rv = true; }
  1201. var o = { };
  1202. function f() {
  1203. try {
  1204. eval("o.bar( foo() );");
  1205. } catch (e) {
  1206. }
  1207. }
  1208. f();
  1209. rv;
  1210. `
  1211. testScript(SCRIPT, valueTrue, t)
  1212. }
  1213. func TestEvalRet(t *testing.T) {
  1214. const SCRIPT = `
  1215. eval("for (var i = 0; i < 3; i++) {i}")
  1216. `
  1217. testScript(SCRIPT, valueInt(2), t)
  1218. }
  1219. func TestEvalFunctionDecl(t *testing.T) {
  1220. const SCRIPT = `
  1221. eval("function F() {}")
  1222. `
  1223. testScript(SCRIPT, _undefined, t)
  1224. }
  1225. func TestEvalFunctionExpr(t *testing.T) {
  1226. const SCRIPT = `
  1227. eval("(function F() {return 42;})")()
  1228. `
  1229. testScript(SCRIPT, intToValue(42), t)
  1230. }
  1231. func TestEvalDirectScope(t *testing.T) {
  1232. const SCRIPT = `
  1233. var __10_4_2_1_3 = "str";
  1234. function testcase() {
  1235. var __10_4_2_1_3 = "str1";
  1236. try {
  1237. throw "error";
  1238. } catch (e) {
  1239. var __10_4_2_1_3 = "str2";
  1240. return eval("__10_4_2_1_3");
  1241. }
  1242. }
  1243. testcase();
  1244. `
  1245. testScript(SCRIPT, asciiString("str2"), t)
  1246. }
  1247. func TestEvalDirectScope1(t *testing.T) {
  1248. const SCRIPT = `
  1249. 'use strict';
  1250. var __10_4_2_1_5 = "str";
  1251. function testcase() {
  1252. var __10_4_2_1_5 = "str1";
  1253. var r = eval("\
  1254. var __10_4_2_1_5 = \'str2\'; \
  1255. eval(\"\'str2\' === __10_4_2_1_5\")\
  1256. ");
  1257. return r;
  1258. }
  1259. testcase();
  1260. `
  1261. testScript(SCRIPT, valueTrue, t)
  1262. }
  1263. func TestEvalDirectCreateBinding(t *testing.T) {
  1264. const SCRIPT = `
  1265. function f() {
  1266. eval("var x = true");
  1267. return x;
  1268. }
  1269. var res = f();
  1270. var thrown = false;
  1271. try {
  1272. x;
  1273. } catch(e) {
  1274. if (e instanceof ReferenceError) {
  1275. thrown = true;
  1276. } else {
  1277. throw e;
  1278. }
  1279. }
  1280. res && thrown;
  1281. `
  1282. testScript(SCRIPT, valueTrue, t)
  1283. }
  1284. func TestEvalDirectCreateBinding1(t *testing.T) {
  1285. const SCRIPT = `
  1286. function f() {
  1287. eval("let x = 1; var y = 2; function f1() {return x};");
  1288. assert.throws(ReferenceError, function() { x });
  1289. return ""+y+f1();
  1290. }
  1291. f();
  1292. `
  1293. testScriptWithTestLib(SCRIPT, asciiString("21"), t)
  1294. }
  1295. func TestEvalDirectCreateBinding3(t *testing.T) {
  1296. const SCRIPT = `
  1297. function f() {
  1298. let x;
  1299. try {
  1300. eval("var y=1, x=2");
  1301. } catch(e) {}
  1302. return y;
  1303. }
  1304. assert.throws(ReferenceError, f);
  1305. `
  1306. testScriptWithTestLib(SCRIPT, _undefined, t)
  1307. }
  1308. func TestEvalGlobalStrict(t *testing.T) {
  1309. const SCRIPT = `
  1310. 'use strict';
  1311. var evalStr =
  1312. 'for (var x in this) {\n'+
  1313. ' if ( x === \'Math\' ) {\n'+
  1314. ' }\n'+
  1315. '}\n';
  1316. eval(evalStr);
  1317. `
  1318. testScript(SCRIPT, _undefined, t)
  1319. }
  1320. func TestEvalEmptyStrict(t *testing.T) {
  1321. const SCRIPT = `
  1322. 'use strict';
  1323. eval("");
  1324. `
  1325. testScript(SCRIPT, _undefined, t)
  1326. }
  1327. func TestEvalFuncDecl(t *testing.T) {
  1328. const SCRIPT = `
  1329. 'use strict';
  1330. var funcA = eval("function __funcA(__arg){return __arg;}; __funcA");
  1331. typeof funcA;
  1332. `
  1333. testScript(SCRIPT, asciiString("function"), t)
  1334. }
  1335. func TestGetAfterSet(t *testing.T) {
  1336. const SCRIPT = `
  1337. function f() {
  1338. var x = 1;
  1339. return x;
  1340. }
  1341. `
  1342. testScript(SCRIPT, _undefined, t)
  1343. }
  1344. func TestForLoopRet(t *testing.T) {
  1345. const SCRIPT = `
  1346. for (var i = 0; i < 20; i++) { if (i > 2) {break;} else { i }}
  1347. `
  1348. testScript(SCRIPT, _undefined, t)
  1349. }
  1350. func TestForLoopRet1(t *testing.T) {
  1351. const SCRIPT = `
  1352. for (var i = 0; i < 20; i++) { if (i > 2) {42;; {L:{break;}}} else { i }}
  1353. `
  1354. testScript(SCRIPT, intToValue(42), t)
  1355. }
  1356. func TestForInLoopRet(t *testing.T) {
  1357. const SCRIPT = `
  1358. var o = [1, 2, 3, 4];
  1359. for (var i in o) { if (i > 2) {break;} else { i }}
  1360. `
  1361. testScript(SCRIPT, _undefined, t)
  1362. }
  1363. func TestForInLoopRet1(t *testing.T) {
  1364. const SCRIPT = `
  1365. var o = {};
  1366. o.x = 1;
  1367. o.y = 2;
  1368. for (var i in o) {
  1369. true;
  1370. }
  1371. `
  1372. testScript(SCRIPT, valueTrue, t)
  1373. }
  1374. func TestDoWhileLoopRet(t *testing.T) {
  1375. const SCRIPT = `
  1376. var i = 0;
  1377. do {
  1378. if (i > 2) {
  1379. break;
  1380. } else {
  1381. i;
  1382. }
  1383. } while (i++ < 20);
  1384. `
  1385. testScript(SCRIPT, _undefined, t)
  1386. }
  1387. func TestDoWhileContinueRet(t *testing.T) {
  1388. const SCRIPT = `
  1389. var i = 0;
  1390. do {
  1391. if (i > 2) {
  1392. true;
  1393. continue;
  1394. } else {
  1395. i;
  1396. }
  1397. } while (i++ < 20);
  1398. `
  1399. testScript(SCRIPT, valueTrue, t)
  1400. }
  1401. func TestWhileLoopRet(t *testing.T) {
  1402. const SCRIPT = `
  1403. var i; while (i < 20) { if (i > 2) {break;} else { i++ }}
  1404. `
  1405. testScript(SCRIPT, _undefined, t)
  1406. }
  1407. func TestLoopRet1(t *testing.T) {
  1408. const SCRIPT = `
  1409. for (var i = 0; i < 20; i++) { }
  1410. `
  1411. testScript(SCRIPT, _undefined, t)
  1412. }
  1413. func TestInstanceof(t *testing.T) {
  1414. const SCRIPT = `
  1415. var rv;
  1416. try {
  1417. true();
  1418. } catch (e) {
  1419. rv = e instanceof TypeError;
  1420. }
  1421. rv;
  1422. `
  1423. testScript(SCRIPT, valueTrue, t)
  1424. }
  1425. func TestStrictAssign(t *testing.T) {
  1426. const SCRIPT = `
  1427. 'use strict';
  1428. var rv;
  1429. var called = false;
  1430. function F() {
  1431. called = true;
  1432. return 1;
  1433. }
  1434. try {
  1435. x = F();
  1436. } catch (e) {
  1437. rv = e instanceof ReferenceError;
  1438. }
  1439. rv + " " + called;
  1440. `
  1441. testScript(SCRIPT, asciiString("true true"), t)
  1442. }
  1443. func TestStrictScope(t *testing.T) {
  1444. const SCRIPT = `
  1445. var rv;
  1446. var called = false;
  1447. function F() {
  1448. 'use strict';
  1449. x = 1;
  1450. }
  1451. try {
  1452. F();
  1453. } catch (e) {
  1454. rv = e instanceof ReferenceError;
  1455. }
  1456. x = 1;
  1457. rv + " " + x;
  1458. `
  1459. testScript(SCRIPT, asciiString("true 1"), t)
  1460. }
  1461. func TestStringObj(t *testing.T) {
  1462. const SCRIPT = `
  1463. var s = new String("test");
  1464. s[0] + s[2] + s[1];
  1465. `
  1466. testScript(SCRIPT, asciiString("tse"), t)
  1467. }
  1468. func TestStringPrimitive(t *testing.T) {
  1469. const SCRIPT = `
  1470. var s = "test";
  1471. s[0] + s[2] + s[1];
  1472. `
  1473. testScript(SCRIPT, asciiString("tse"), t)
  1474. }
  1475. func TestCallGlobalObject(t *testing.T) {
  1476. const SCRIPT = `
  1477. var rv;
  1478. try {
  1479. this();
  1480. } catch (e) {
  1481. rv = e instanceof TypeError
  1482. }
  1483. rv;
  1484. `
  1485. testScript(SCRIPT, valueTrue, t)
  1486. }
  1487. func TestFuncLength(t *testing.T) {
  1488. const SCRIPT = `
  1489. function F(x, y) {
  1490. }
  1491. F.length
  1492. `
  1493. testScript(SCRIPT, intToValue(2), t)
  1494. }
  1495. func TestNativeFuncLength(t *testing.T) {
  1496. const SCRIPT = `
  1497. eval.length + Object.defineProperty.length + String.length
  1498. `
  1499. testScript(SCRIPT, intToValue(5), t)
  1500. }
  1501. func TestArguments(t *testing.T) {
  1502. const SCRIPT = `
  1503. function F() {
  1504. return arguments.length + " " + arguments[1];
  1505. }
  1506. F(1,2,3)
  1507. `
  1508. testScript(SCRIPT, asciiString("3 2"), t)
  1509. }
  1510. func TestArgumentsPut(t *testing.T) {
  1511. const SCRIPT = `
  1512. function F(x, y) {
  1513. arguments[0] -= arguments[1];
  1514. return x;
  1515. }
  1516. F(5, 2)
  1517. `
  1518. testScript(SCRIPT, intToValue(3), t)
  1519. }
  1520. func TestArgumentsPutStrict(t *testing.T) {
  1521. const SCRIPT = `
  1522. function F(x, y) {
  1523. 'use strict';
  1524. arguments[0] -= arguments[1];
  1525. return x;
  1526. }
  1527. F(5, 2)
  1528. `
  1529. testScript(SCRIPT, intToValue(5), t)
  1530. }
  1531. func TestArgumentsExtra(t *testing.T) {
  1532. const SCRIPT = `
  1533. function F(x, y) {
  1534. return arguments[2];
  1535. }
  1536. F(1, 2, 42)
  1537. `
  1538. testScript(SCRIPT, intToValue(42), t)
  1539. }
  1540. func TestArgumentsExist(t *testing.T) {
  1541. const SCRIPT = `
  1542. function F(x, arguments) {
  1543. return arguments;
  1544. }
  1545. F(1, 42)
  1546. `
  1547. testScript(SCRIPT, intToValue(42), t)
  1548. }
  1549. func TestArgumentsDelete(t *testing.T) {
  1550. const SCRIPT = `
  1551. function f(x) {
  1552. delete arguments[0];
  1553. arguments[0] = 42;
  1554. return x;
  1555. }
  1556. f(1)
  1557. `
  1558. testScript(SCRIPT, intToValue(1), t)
  1559. }
  1560. func TestArgumentsInEval(t *testing.T) {
  1561. const SCRIPT = `
  1562. function f() {
  1563. return eval("arguments");
  1564. }
  1565. f(1)[0];
  1566. `
  1567. testScript(SCRIPT, intToValue(1), t)
  1568. }
  1569. func TestArgumentsRedeclareInEval(t *testing.T) {
  1570. const SCRIPT = `
  1571. assert.sameValue("arguments" in this, false, "No global 'arguments' binding");
  1572. function f(p = eval("var arguments = 'param'"), arguments) {}
  1573. assert.throws(SyntaxError, f);
  1574. assert.sameValue("arguments" in this, false, "No global 'arguments' binding");
  1575. `
  1576. testScriptWithTestLib(SCRIPT, _undefined, t)
  1577. }
  1578. func TestArgumentsRedeclareArrow(t *testing.T) {
  1579. const SCRIPT = `
  1580. const oldArguments = globalThis.arguments;
  1581. let count = 0;
  1582. const f = (p = eval("var arguments = 'param'"), q = () => arguments) => {
  1583. var arguments = "local";
  1584. assert.sameValue(arguments, "local", "arguments");
  1585. assert.sameValue(q(), "param", "q");
  1586. count++;
  1587. }
  1588. f();
  1589. assert.sameValue(count, 1);
  1590. assert.sameValue(globalThis.arguments, oldArguments, "globalThis.arguments unchanged");
  1591. `
  1592. testScriptWithTestLib(SCRIPT, _undefined, t)
  1593. }
  1594. func TestEvalParamWithDef(t *testing.T) {
  1595. const SCRIPT = `
  1596. function f(param = 0) {
  1597. eval("var param = 1");
  1598. return param;
  1599. }
  1600. f();
  1601. `
  1602. testScript(SCRIPT, valueInt(1), t)
  1603. }
  1604. func TestArgumentsRedefinedAsLetDyn(t *testing.T) {
  1605. const SCRIPT = `
  1606. function f() {
  1607. let arguments;
  1608. eval(""); // force dynamic scope
  1609. return arguments;
  1610. }
  1611. f(1,2);
  1612. `
  1613. testScript(SCRIPT, _undefined, t)
  1614. }
  1615. func TestWith(t *testing.T) {
  1616. const SCRIPT = `
  1617. var b = 1;
  1618. var o = {a: 41};
  1619. with(o) {
  1620. a += b;
  1621. }
  1622. o.a;
  1623. `
  1624. testScript(SCRIPT, intToValue(42), t)
  1625. }
  1626. func TestWithInFunc(t *testing.T) {
  1627. const SCRIPT = `
  1628. function F() {
  1629. var b = 1;
  1630. var c = 0;
  1631. var o = {a: 40, c: 1};
  1632. with(o) {
  1633. a += b + c;
  1634. }
  1635. return o.a;
  1636. }
  1637. F();
  1638. `
  1639. testScript(SCRIPT, intToValue(42), t)
  1640. }
  1641. func TestAssignNonExtendable(t *testing.T) {
  1642. const SCRIPT = `
  1643. 'use strict';
  1644. function F() {
  1645. this.x = 1;
  1646. }
  1647. var o = new F();
  1648. Object.preventExtensions(o);
  1649. o.x = 42;
  1650. o.x;
  1651. `
  1652. testScript(SCRIPT, intToValue(42), t)
  1653. }
  1654. func TestAssignNonExtendable1(t *testing.T) {
  1655. const SCRIPT = `
  1656. 'use strict';
  1657. function F() {
  1658. }
  1659. var o = new F();
  1660. var rv;
  1661. Object.preventExtensions(o);
  1662. try {
  1663. o.x = 42;
  1664. } catch (e) {
  1665. rv = e.constructor === TypeError;
  1666. }
  1667. rv += " " + o.x;
  1668. rv;
  1669. `
  1670. testScript(SCRIPT, asciiString("true undefined"), t)
  1671. }
  1672. func TestAssignStrict(t *testing.T) {
  1673. const SCRIPT = `
  1674. 'use strict';
  1675. try {
  1676. eval("eval = 42");
  1677. } catch(e) {
  1678. var rv = e instanceof SyntaxError
  1679. }
  1680. rv;
  1681. `
  1682. testScript(SCRIPT, valueTrue, t)
  1683. }
  1684. func TestIllegalArgmentName(t *testing.T) {
  1685. const SCRIPT = `
  1686. 'use strict';
  1687. try {
  1688. eval("function F(eval) {}");
  1689. } catch (e) {
  1690. var rv = e instanceof SyntaxError
  1691. }
  1692. rv;
  1693. `
  1694. testScript(SCRIPT, valueTrue, t)
  1695. }
  1696. func TestFunction(t *testing.T) {
  1697. const SCRIPT = `
  1698. var f0 = Function("");
  1699. var f1 = Function("return ' one'");
  1700. var f2 = Function("arg", "return ' ' + arg");
  1701. f0() + f1() + f2("two");
  1702. `
  1703. testScript(SCRIPT, asciiString("undefined one two"), t)
  1704. }
  1705. func TestFunction1(t *testing.T) {
  1706. const SCRIPT = `
  1707. var f = function f1(count) {
  1708. if (count == 0) {
  1709. return true;
  1710. }
  1711. return f1(count-1);
  1712. }
  1713. f(1);
  1714. `
  1715. testScript(SCRIPT, valueTrue, t)
  1716. }
  1717. func TestFunction2(t *testing.T) {
  1718. const SCRIPT = `
  1719. var trace = "";
  1720. function f(count) {
  1721. trace += "f("+count+")";
  1722. if (count == 0) {
  1723. return;
  1724. }
  1725. return f(count-1);
  1726. }
  1727. function f1() {
  1728. trace += "f1";
  1729. }
  1730. var f2 = f;
  1731. f = f1;
  1732. f2(1);
  1733. trace;
  1734. `
  1735. testScript(SCRIPT, asciiString("f(1)f1"), t)
  1736. }
  1737. func TestFunctionToString(t *testing.T) {
  1738. const SCRIPT = `
  1739. Function("arg1", "arg2", "return 42").toString();
  1740. `
  1741. testScript(SCRIPT, asciiString("function anonymous(arg1,arg2\n) {\nreturn 42\n}"), t)
  1742. }
  1743. func TestObjectLiteral(t *testing.T) {
  1744. const SCRIPT = `
  1745. var getterCalled = false;
  1746. var setterCalled = false;
  1747. var o = {get x() {getterCalled = true}, set x(_) {setterCalled = true}};
  1748. o.x;
  1749. o.x = 42;
  1750. getterCalled && setterCalled;
  1751. `
  1752. testScript(SCRIPT, valueTrue, t)
  1753. }
  1754. func TestConst(t *testing.T) {
  1755. const SCRIPT = `
  1756. var v1 = true && true;
  1757. var v2 = 1/(-1 * 0);
  1758. var v3 = 1 == 2 || v1;
  1759. var v4 = true && false
  1760. v1 === true && v2 === -Infinity && v3 === v1 && v4 === false;
  1761. `
  1762. testScript(SCRIPT, valueTrue, t)
  1763. }
  1764. func TestConstWhile(t *testing.T) {
  1765. const SCRIPT = `
  1766. var c = 0;
  1767. while (2 + 2 === 4) {
  1768. if (++c > 9) {
  1769. break;
  1770. }
  1771. }
  1772. c === 10;
  1773. `
  1774. testScript(SCRIPT, valueTrue, t)
  1775. }
  1776. func TestConstWhileThrow(t *testing.T) {
  1777. const SCRIPT = `
  1778. var thrown = false;
  1779. try {
  1780. while ('s' in true) {
  1781. break;
  1782. }
  1783. } catch (e) {
  1784. thrown = e instanceof TypeError
  1785. }
  1786. thrown;
  1787. `
  1788. testScript(SCRIPT, valueTrue, t)
  1789. }
  1790. func TestDupParams(t *testing.T) {
  1791. const SCRIPT = `
  1792. function F(x, y, x) {
  1793. return x;
  1794. }
  1795. F(1, 2);
  1796. `
  1797. testScript(SCRIPT, _undefined, t)
  1798. }
  1799. func TestUseUnsuppliedParam(t *testing.T) {
  1800. const SCRIPT = `
  1801. function getMessage(message) {
  1802. if (message === undefined) {
  1803. message = '';
  1804. }
  1805. message += " 123 456";
  1806. return message;
  1807. }
  1808. getMessage();
  1809. `
  1810. testScript(SCRIPT, asciiString(" 123 456"), t)
  1811. }
  1812. func TestForInLetWithInitializer(t *testing.T) {
  1813. const SCRIPT = `for (let x = 3 in {}) { }`
  1814. _, err := Compile("", SCRIPT, false)
  1815. if err == nil {
  1816. t.Fatal("Expected error")
  1817. }
  1818. }
  1819. func TestForInLoop(t *testing.T) {
  1820. const SCRIPT = `
  1821. function Proto() {}
  1822. Proto.prototype.x = 42;
  1823. var o = new Proto();
  1824. o.y = 44;
  1825. o.x = 45;
  1826. var hasX = false;
  1827. var hasY = false;
  1828. for (var i in o) {
  1829. switch(i) {
  1830. case "x":
  1831. if (hasX) {
  1832. throw new Error("Already has X");
  1833. }
  1834. hasX = true;
  1835. break;
  1836. case "y":
  1837. if (hasY) {
  1838. throw new Error("Already has Y");
  1839. }
  1840. hasY = true;
  1841. break;
  1842. }
  1843. }
  1844. hasX && hasY;
  1845. `
  1846. testScript(SCRIPT, valueTrue, t)
  1847. }
  1848. func TestWhileLoopResult(t *testing.T) {
  1849. const SCRIPT = `
  1850. while(false);
  1851. `
  1852. testScript(SCRIPT, _undefined, t)
  1853. }
  1854. func TestEmptySwitch(t *testing.T) {
  1855. const SCRIPT = `
  1856. switch(1){}
  1857. `
  1858. testScript(SCRIPT, _undefined, t)
  1859. }
  1860. func TestEmptyDoWhile(t *testing.T) {
  1861. const SCRIPT = `
  1862. do {} while(false)
  1863. `
  1864. testScript(SCRIPT, _undefined, t)
  1865. }
  1866. func TestSwitch(t *testing.T) {
  1867. const SCRIPT = `
  1868. function F(x) {
  1869. var i = 0;
  1870. switch (x) {
  1871. case 0:
  1872. i++;
  1873. case 1:
  1874. i++;
  1875. default:
  1876. i++;
  1877. case 2:
  1878. i++;
  1879. break;
  1880. case 3:
  1881. i++;
  1882. }
  1883. return i;
  1884. }
  1885. F(0) + F(1) + F(2) + F(4);
  1886. `
  1887. testScript(SCRIPT, intToValue(10), t)
  1888. }
  1889. func TestSwitchDefFirst(t *testing.T) {
  1890. const SCRIPT = `
  1891. function F(x) {
  1892. var i = 0;
  1893. switch (x) {
  1894. default:
  1895. i++;
  1896. case 0:
  1897. i++;
  1898. case 1:
  1899. i++;
  1900. case 2:
  1901. i++;
  1902. break;
  1903. case 3:
  1904. i++;
  1905. }
  1906. return i;
  1907. }
  1908. F(0) + F(1) + F(2) + F(4);
  1909. `
  1910. testScript(SCRIPT, intToValue(10), t)
  1911. }
  1912. func TestSwitchResult(t *testing.T) {
  1913. const SCRIPT = `
  1914. var x = 2;
  1915. switch (x) {
  1916. case 0:
  1917. "zero";
  1918. case 1:
  1919. "one";
  1920. case 2:
  1921. "two";
  1922. break;
  1923. case 3:
  1924. "three";
  1925. default:
  1926. "default";
  1927. }
  1928. `
  1929. testScript(SCRIPT, asciiString("two"), t)
  1930. }
  1931. func TestSwitchResult1(t *testing.T) {
  1932. const SCRIPT = `
  1933. var x = 0;
  1934. switch (x) { case 0: "two"; case 1: break}
  1935. `
  1936. testScript(SCRIPT, asciiString("two"), t)
  1937. }
  1938. func TestSwitchResult2(t *testing.T) {
  1939. const SCRIPT = `
  1940. 6; switch ("a") { case "a": 7; case "b": }
  1941. `
  1942. testScript(SCRIPT, valueInt(7), t)
  1943. }
  1944. func TestSwitchResultJumpIntoEmptyEval(t *testing.T) {
  1945. const SCRIPT = `
  1946. function t(x) {
  1947. return eval("switch(x) { case 1: 2; break; case 2: let x = 1; case 3: x+2; break; case 4: default: 9}");
  1948. }
  1949. ""+t(2)+t();
  1950. `
  1951. testScript(SCRIPT, asciiString("39"), t)
  1952. }
  1953. func TestSwitchResultJumpIntoEmpty(t *testing.T) {
  1954. const SCRIPT = `
  1955. switch(2) { case 1: 2; break; case 2: let x = 1; case 3: x+2; case 4: {let y = 2}; break; default: 9};
  1956. `
  1957. testScript(SCRIPT, valueInt(3), t)
  1958. }
  1959. func TestSwitchLexical(t *testing.T) {
  1960. const SCRIPT = `
  1961. switch (true) { case true: let x = 1; }
  1962. `
  1963. testScript(SCRIPT, _undefined, t)
  1964. }
  1965. func TestSwitchBreakOuter(t *testing.T) {
  1966. const SCRIPT = `
  1967. LOOP:
  1968. for (let i = 0; i < 10; i++) {
  1969. switch (i) {
  1970. case 0:
  1971. continue;
  1972. case 1:
  1973. let x = 1;
  1974. continue;
  1975. case 2:
  1976. try {
  1977. x++;
  1978. } catch (e) {
  1979. if (e instanceof ReferenceError) {
  1980. break LOOP;
  1981. }
  1982. }
  1983. throw new Error("Exception was not thrown");
  1984. }
  1985. }
  1986. `
  1987. testScript(SCRIPT, _undefined, t)
  1988. }
  1989. func TestIfBreakResult(t *testing.T) {
  1990. const SCRIPT = `
  1991. L: {if (true) {42;} break L;}
  1992. `
  1993. testScript(SCRIPT, intToValue(42), t)
  1994. }
  1995. func TestSwitchNoMatch(t *testing.T) {
  1996. const SCRIPT = `
  1997. var result;
  1998. var x;
  1999. switch (x) {
  2000. case 0:
  2001. result = "2";
  2002. break;
  2003. }
  2004. result;
  2005. `
  2006. testScript(SCRIPT, _undefined, t)
  2007. }
  2008. func TestSwitchNoMatchNoDefault(t *testing.T) {
  2009. const SCRIPT = `
  2010. switch (1) {
  2011. case 0:
  2012. }
  2013. `
  2014. testScript(SCRIPT, _undefined, t)
  2015. }
  2016. func TestSwitchNoMatchNoDefaultNoResult(t *testing.T) {
  2017. const SCRIPT = `
  2018. switch (1) {
  2019. case 0:
  2020. }
  2021. 42;
  2022. `
  2023. testScript(SCRIPT, intToValue(42), t)
  2024. }
  2025. func TestSwitchNoMatchNoDefaultNoResultMatch(t *testing.T) {
  2026. const SCRIPT = `
  2027. switch (1) {
  2028. case 1:
  2029. }
  2030. 42;
  2031. `
  2032. testScript(SCRIPT, intToValue(42), t)
  2033. }
  2034. func TestEmptySwitchNoResult(t *testing.T) {
  2035. const SCRIPT = `
  2036. switch (1) {}
  2037. 42;
  2038. `
  2039. testScript(SCRIPT, intToValue(42), t)
  2040. }
  2041. func TestGetOwnPropertyNames(t *testing.T) {
  2042. const SCRIPT = `
  2043. var o = {
  2044. prop1: 42,
  2045. prop2: "test"
  2046. }
  2047. var hasProp1 = false;
  2048. var hasProp2 = false;
  2049. var names = Object.getOwnPropertyNames(o);
  2050. for (var i in names) {
  2051. var p = names[i];
  2052. switch(p) {
  2053. case "prop1":
  2054. hasProp1 = true;
  2055. break;
  2056. case "prop2":
  2057. hasProp2 = true;
  2058. break;
  2059. }
  2060. }
  2061. hasProp1 && hasProp2;
  2062. `
  2063. testScript(SCRIPT, valueTrue, t)
  2064. }
  2065. func TestArrayLiteral(t *testing.T) {
  2066. const SCRIPT = `
  2067. var f1Called = false;
  2068. var f2Called = false;
  2069. var f3Called = false;
  2070. var errorThrown = false;
  2071. function F1() {
  2072. f1Called = true;
  2073. }
  2074. function F2() {
  2075. f2Called = true;
  2076. }
  2077. function F3() {
  2078. f3Called = true;
  2079. }
  2080. try {
  2081. var a = [F1(), x(F3()), F2()];
  2082. } catch(e) {
  2083. if (e instanceof ReferenceError) {
  2084. errorThrown = true;
  2085. } else {
  2086. throw e;
  2087. }
  2088. }
  2089. f1Called && !f2Called && f3Called && errorThrown && a === undefined;
  2090. `
  2091. testScript(SCRIPT, valueTrue, t)
  2092. }
  2093. func TestJumpOutOfReturn(t *testing.T) {
  2094. const SCRIPT = `
  2095. function f() {
  2096. var a;
  2097. if (a == 0) {
  2098. return true;
  2099. }
  2100. }
  2101. f();
  2102. `
  2103. testScript(SCRIPT, _undefined, t)
  2104. }
  2105. func TestSwitchJumpOutOfReturn(t *testing.T) {
  2106. const SCRIPT = `
  2107. function f(x) {
  2108. switch(x) {
  2109. case 0:
  2110. break;
  2111. default:
  2112. return x;
  2113. }
  2114. }
  2115. f(0);
  2116. `
  2117. testScript(SCRIPT, _undefined, t)
  2118. }
  2119. func TestSetToReadOnlyPropertyStrictBracket(t *testing.T) {
  2120. const SCRIPT = `
  2121. 'use strict';
  2122. var o = {};
  2123. var thrown = false;
  2124. Object.defineProperty(o, "test", {value: 42, configurable: true});
  2125. try {
  2126. o["test"] = 43;
  2127. } catch (e) {
  2128. thrown = e instanceof TypeError;
  2129. }
  2130. thrown;
  2131. `
  2132. testScript(SCRIPT, valueTrue, t)
  2133. }
  2134. func TestSetToReadOnlyPropertyStrictDot(t *testing.T) {
  2135. const SCRIPT = `
  2136. 'use strict';
  2137. var o = {};
  2138. var thrown = false;
  2139. Object.defineProperty(o, "test", {value: 42, configurable: true});
  2140. try {
  2141. o.test = 43;
  2142. } catch (e) {
  2143. thrown = e instanceof TypeError;
  2144. }
  2145. thrown;
  2146. `
  2147. testScript(SCRIPT, valueTrue, t)
  2148. }
  2149. func TestDeleteNonConfigurablePropertyStrictBracket(t *testing.T) {
  2150. const SCRIPT = `
  2151. 'use strict';
  2152. var o = {};
  2153. var thrown = false;
  2154. Object.defineProperty(o, "test", {value: 42});
  2155. try {
  2156. delete o["test"];
  2157. } catch (e) {
  2158. thrown = e instanceof TypeError;
  2159. }
  2160. thrown;
  2161. `
  2162. testScript(SCRIPT, valueTrue, t)
  2163. }
  2164. func TestDeleteNonConfigurablePropertyStrictDot(t *testing.T) {
  2165. const SCRIPT = `
  2166. 'use strict';
  2167. var o = {};
  2168. var thrown = false;
  2169. Object.defineProperty(o, "test", {value: 42});
  2170. try {
  2171. delete o.test;
  2172. } catch (e) {
  2173. thrown = e instanceof TypeError;
  2174. }
  2175. thrown;
  2176. `
  2177. testScript(SCRIPT, valueTrue, t)
  2178. }
  2179. func TestCompound1(t *testing.T) {
  2180. const SCRIPT = `
  2181. var x = 0;
  2182. var scope = {x: 1};
  2183. var f;
  2184. with (scope) {
  2185. f = function() {
  2186. x *= (delete scope.x, 2);
  2187. }
  2188. }
  2189. f();
  2190. scope.x === 2 && x === 0;
  2191. `
  2192. testScript(SCRIPT, valueTrue, t)
  2193. }
  2194. func TestCompound2(t *testing.T) {
  2195. const SCRIPT = `
  2196. var x;
  2197. x = "x";
  2198. x ^= "1";
  2199. `
  2200. testScript(SCRIPT, intToValue(1), t)
  2201. }
  2202. func TestDeleteArguments(t *testing.T) {
  2203. defer func() {
  2204. if _, ok := recover().(*CompilerSyntaxError); !ok {
  2205. t.Fatal("Expected syntax error")
  2206. }
  2207. }()
  2208. const SCRIPT = `
  2209. 'use strict';
  2210. function f() {
  2211. delete arguments;
  2212. }
  2213. `
  2214. testScript(SCRIPT, _undefined, t)
  2215. }
  2216. func TestReturnUndefined(t *testing.T) {
  2217. const SCRIPT = `
  2218. function f() {
  2219. return x;
  2220. }
  2221. var thrown = false;
  2222. try {
  2223. f();
  2224. } catch (e) {
  2225. thrown = e instanceof ReferenceError;
  2226. }
  2227. thrown;
  2228. `
  2229. testScript(SCRIPT, valueTrue, t)
  2230. }
  2231. func TestForBreak(t *testing.T) {
  2232. const SCRIPT = `
  2233. var supreme, count;
  2234. supreme = 5;
  2235. var __evaluated = eval("for(count=0;;) {if (count===supreme)break;else count++; }");
  2236. if (__evaluated !== void 0) {
  2237. throw new Error('#1: __evaluated === 4. Actual: __evaluated ==='+ __evaluated );
  2238. }
  2239. `
  2240. testScript(SCRIPT, _undefined, t)
  2241. }
  2242. func TestLargeNumberLiteral(t *testing.T) {
  2243. const SCRIPT = `
  2244. var x = 0x800000000000000000000;
  2245. x.toString();
  2246. `
  2247. testScript(SCRIPT, asciiString("9.671406556917033e+24"), t)
  2248. }
  2249. func TestIncDelete(t *testing.T) {
  2250. const SCRIPT = `
  2251. var o = {x: 1};
  2252. o.x += (delete o.x, 1);
  2253. o.x;
  2254. `
  2255. testScript(SCRIPT, intToValue(2), t)
  2256. }
  2257. func TestCompoundAssignRefError(t *testing.T) {
  2258. const SCRIPT = `
  2259. var thrown = false;
  2260. try {
  2261. a *= 1;
  2262. } catch (e) {
  2263. if (e instanceof ReferenceError) {
  2264. thrown = true;
  2265. } else {
  2266. throw e;
  2267. }
  2268. }
  2269. thrown;
  2270. `
  2271. testScript(SCRIPT, valueTrue, t)
  2272. }
  2273. func TestObjectLiteral__Proto__(t *testing.T) {
  2274. const SCRIPT = `
  2275. var o = {
  2276. __proto__: null,
  2277. test: 42
  2278. }
  2279. Object.getPrototypeOf(o);
  2280. `
  2281. testScript(SCRIPT, _null, t)
  2282. }
  2283. func TestEmptyCodeError(t *testing.T) {
  2284. if _, err := New().RunString(`i`); err == nil {
  2285. t.Fatal("Expected an error")
  2286. } else {
  2287. if e := err.Error(); e != "ReferenceError: i is not defined at <eval>:1:1(0)" {
  2288. t.Fatalf("Unexpected error: '%s'", e)
  2289. }
  2290. }
  2291. }
  2292. func TestForOfArray(t *testing.T) {
  2293. const SCRIPT = `
  2294. var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
  2295. var i = 0;
  2296. for (var value of array) {
  2297. assert.sameValue(value, array[i], 'element at index ' + i);
  2298. i++;
  2299. }
  2300. assert.sameValue(i, 8, 'Visits all elements');
  2301. `
  2302. testScriptWithTestLib(SCRIPT, _undefined, t)
  2303. }
  2304. func TestForOfReturn(t *testing.T) {
  2305. const SCRIPT = `
  2306. var callCount = 0;
  2307. var iterationCount = 0;
  2308. var iterable = {};
  2309. var x = {
  2310. set attr(_) {
  2311. throw new Test262Error();
  2312. }
  2313. };
  2314. iterable[Symbol.iterator] = function() {
  2315. return {
  2316. next: function() {
  2317. return { done: false, value: 0 };
  2318. },
  2319. return: function() {
  2320. callCount += 1;
  2321. }
  2322. }
  2323. };
  2324. assert.throws(Test262Error, function() {
  2325. for (x.attr of iterable) {
  2326. iterationCount += 1;
  2327. }
  2328. });
  2329. assert.sameValue(iterationCount, 0, 'The loop body is not evaluated');
  2330. assert.sameValue(callCount, 1, 'Iterator is closed');
  2331. `
  2332. testScriptWithTestLib(SCRIPT, _undefined, t)
  2333. }
  2334. func TestForOfReturn1(t *testing.T) {
  2335. const SCRIPT = `
  2336. var iterable = {};
  2337. var iterationCount = 0;
  2338. iterable[Symbol.iterator] = function() {
  2339. return {
  2340. next: function() {
  2341. return { done: false, value: null };
  2342. },
  2343. get return() {
  2344. throw new Test262Error();
  2345. }
  2346. };
  2347. };
  2348. assert.throws(Test262Error, function() {
  2349. for (var x of iterable) {
  2350. iterationCount += 1;
  2351. break;
  2352. }
  2353. });
  2354. assert.sameValue(iterationCount, 1, 'The loop body is evaluated');
  2355. `
  2356. testScriptWithTestLib(SCRIPT, _undefined, t)
  2357. }
  2358. func TestForOfLet(t *testing.T) {
  2359. const SCRIPT = `
  2360. var iterCount = 0;
  2361. function f() {}
  2362. for (var let of [23]) {
  2363. f(let);
  2364. if (let != 23) {
  2365. throw new Error("");
  2366. }
  2367. iterCount += 1;
  2368. }
  2369. iterCount;
  2370. `
  2371. testScript(SCRIPT, valueInt(1), t)
  2372. }
  2373. func TestForOfLetLet(t *testing.T) {
  2374. const SCRIPT = `
  2375. for (let let of [23]) {
  2376. }
  2377. `
  2378. _, err := Compile("", SCRIPT, false)
  2379. if err == nil {
  2380. t.Fatal("Expected error")
  2381. }
  2382. }
  2383. func TestForHeadLet(t *testing.T) {
  2384. const SCRIPT = `
  2385. for (let = 0; let < 2; let++);
  2386. `
  2387. testScript(SCRIPT, _undefined, t)
  2388. }
  2389. func TestLhsLet(t *testing.T) {
  2390. const SCRIPT = `
  2391. let = 1;
  2392. let;
  2393. `
  2394. testScript(SCRIPT, valueInt(1), t)
  2395. }
  2396. func TestLetPostfixASI(t *testing.T) {
  2397. const SCRIPT = `
  2398. let
  2399. ++
  2400. `
  2401. _, err := Compile("", SCRIPT, false)
  2402. if err == nil {
  2403. t.Fatal("Expected error")
  2404. }
  2405. }
  2406. func TestIteratorReturnNormal(t *testing.T) {
  2407. const SCRIPT = `
  2408. var iterable = {};
  2409. var iterationCount = 0;
  2410. iterable[Symbol.iterator] = function() {
  2411. return {
  2412. next: function() {
  2413. return { done: ++iterationCount > 2, value: null };
  2414. },
  2415. get return() {
  2416. throw new Test262Error();
  2417. }
  2418. };
  2419. };
  2420. for (var x of iterable) {
  2421. }
  2422. `
  2423. testScriptWithTestLib(SCRIPT, _undefined, t)
  2424. }
  2425. func TestIteratorReturnErrorNested(t *testing.T) {
  2426. const SCRIPT = `
  2427. var returnCalled = {};
  2428. function iter(id) {
  2429. return function() {
  2430. var count = 0;
  2431. return {
  2432. next: function () {
  2433. return {
  2434. value: null,
  2435. done: ++count > 2
  2436. };
  2437. },
  2438. return: function () {
  2439. returnCalled[id] = true;
  2440. throw new Error(id);
  2441. }
  2442. };
  2443. }
  2444. }
  2445. var iterable1 = {};
  2446. iterable1[Symbol.iterator] = iter("1");
  2447. var iterable2 = {};
  2448. iterable2[Symbol.iterator] = iter("2");
  2449. try {
  2450. for (var i of iterable1) {
  2451. for (var j of iterable2) {
  2452. break;
  2453. }
  2454. }
  2455. throw new Error("no exception was thrown");
  2456. } catch (e) {
  2457. if (e.message !== "2") {
  2458. throw e;
  2459. }
  2460. }
  2461. if (!returnCalled["1"]) {
  2462. throw new Error("no return 1");
  2463. }
  2464. if (!returnCalled["2"]) {
  2465. throw new Error("no return 2");
  2466. }
  2467. `
  2468. testScript(SCRIPT, _undefined, t)
  2469. }
  2470. func TestReturnFromForInLoop(t *testing.T) {
  2471. const SCRIPT = `
  2472. (function f() {
  2473. for (var i in {a: 1}) {
  2474. return true;
  2475. }
  2476. })();
  2477. `
  2478. testScript(SCRIPT, valueTrue, t)
  2479. }
  2480. func TestReturnFromForOfLoop(t *testing.T) {
  2481. const SCRIPT = `
  2482. (function f() {
  2483. for (var i of [1]) {
  2484. return true;
  2485. }
  2486. })();
  2487. `
  2488. testScript(SCRIPT, valueTrue, t)
  2489. }
  2490. func TestIfStackLeaks(t *testing.T) {
  2491. const SCRIPT = `
  2492. var t = 0;
  2493. if (t === 0) {
  2494. t;
  2495. }
  2496. `
  2497. testScript(SCRIPT, _positiveZero, t)
  2498. }
  2499. func TestWithCallee(t *testing.T) {
  2500. const SCRIPT = `
  2501. function O() {
  2502. var that = this;
  2503. this.m = function() {
  2504. return this === that;
  2505. }
  2506. }
  2507. with(new O()) {
  2508. m();
  2509. }
  2510. `
  2511. testScript(SCRIPT, valueTrue, t)
  2512. }
  2513. func TestWithScope(t *testing.T) {
  2514. const SCRIPT = `
  2515. function f(o) {
  2516. var x = 42;
  2517. function innerf(o) {
  2518. with (o) {
  2519. return x;
  2520. }
  2521. }
  2522. return innerf(o);
  2523. }
  2524. f({});
  2525. `
  2526. testScript(SCRIPT, valueInt(42), t)
  2527. }
  2528. func TestEvalCallee(t *testing.T) {
  2529. const SCRIPT = `
  2530. (function () {
  2531. 'use strict';
  2532. var v = function() {
  2533. return this === undefined;
  2534. };
  2535. return eval('v()');
  2536. })();
  2537. `
  2538. testScript(SCRIPT, valueTrue, t)
  2539. }
  2540. func TestEvalBindingDeleteVar(t *testing.T) {
  2541. const SCRIPT = `
  2542. (function () {
  2543. eval("var x = 1");
  2544. return x === 1 && delete x;
  2545. })();
  2546. `
  2547. testScript(SCRIPT, valueTrue, t)
  2548. }
  2549. func TestEvalBindingDeleteFunc(t *testing.T) {
  2550. const SCRIPT = `
  2551. (function () {
  2552. eval("function x(){}");
  2553. return typeof x === "function" && delete x;
  2554. })();
  2555. `
  2556. testScript(SCRIPT, valueTrue, t)
  2557. }
  2558. func TestDeleteGlobalLexical(t *testing.T) {
  2559. const SCRIPT = `
  2560. let x;
  2561. delete x;
  2562. `
  2563. testScript(SCRIPT, valueFalse, t)
  2564. }
  2565. func TestDeleteGlobalEval(t *testing.T) {
  2566. const SCRIPT = `
  2567. eval("var x");
  2568. delete x;
  2569. `
  2570. testScript(SCRIPT, valueTrue, t)
  2571. }
  2572. func TestGlobalVarNames(t *testing.T) {
  2573. vm := New()
  2574. _, err := vm.RunString("(0,eval)('var x')")
  2575. if err != nil {
  2576. t.Fatal(err)
  2577. }
  2578. _, err = vm.RunString("let x")
  2579. if err == nil {
  2580. t.Fatal("Expected error")
  2581. }
  2582. }
  2583. func TestTryResultEmpty(t *testing.T) {
  2584. const SCRIPT = `
  2585. 1; try { } finally { }
  2586. `
  2587. testScript(SCRIPT, _undefined, t)
  2588. }
  2589. func TestTryResultEmptyCatch(t *testing.T) {
  2590. const SCRIPT = `
  2591. 1; try { throw null } catch(e) { }
  2592. `
  2593. testScript(SCRIPT, _undefined, t)
  2594. }
  2595. func TestTryResultEmptyContinueLoop(t *testing.T) {
  2596. const SCRIPT = `
  2597. for (var i = 0; i < 2; i++) { try {throw null;} catch(e) {continue;} 'bad'}
  2598. `
  2599. testScript(SCRIPT, _undefined, t)
  2600. }
  2601. func TestTryEmptyCatchStackLeak(t *testing.T) {
  2602. const SCRIPT = `
  2603. (function() {
  2604. var f;
  2605. // Make sure the outer function is not stashless.
  2606. (function() {
  2607. f++;
  2608. })();
  2609. try {
  2610. throw new Error();
  2611. } catch(e) {}
  2612. })();
  2613. `
  2614. testScript(SCRIPT, _undefined, t)
  2615. }
  2616. func TestTryThrowEmptyCatch(t *testing.T) {
  2617. const SCRIPT = `
  2618. try {
  2619. throw new Error();
  2620. }
  2621. catch (e) {}
  2622. `
  2623. testScript(SCRIPT, _undefined, t)
  2624. }
  2625. func TestFalsyLoopBreak(t *testing.T) {
  2626. const SCRIPT = `
  2627. while(false) {
  2628. break;
  2629. }
  2630. for(;false;) {
  2631. break;
  2632. }
  2633. undefined;
  2634. `
  2635. MustCompile("", SCRIPT, false)
  2636. }
  2637. func TestFalsyLoopBreakWithResult(t *testing.T) {
  2638. const SCRIPT = `
  2639. while(false) {
  2640. break;
  2641. }
  2642. `
  2643. testScript(SCRIPT, _undefined, t)
  2644. }
  2645. func TestDummyCompile(t *testing.T) {
  2646. const SCRIPT = `
  2647. 'use strict';
  2648. for (;false;) {
  2649. eval = 1;
  2650. }
  2651. `
  2652. _, err := Compile("", SCRIPT, false)
  2653. if err == nil {
  2654. t.Fatal("expected error")
  2655. }
  2656. }
  2657. func TestDummyCompileForUpdate(t *testing.T) {
  2658. const SCRIPT = `
  2659. 'use strict';
  2660. for (;false;eval=1) {
  2661. }
  2662. `
  2663. _, err := Compile("", SCRIPT, false)
  2664. if err == nil {
  2665. t.Fatal("expected error")
  2666. }
  2667. }
  2668. func TestObjectLiteralWithNumericKeys(t *testing.T) {
  2669. const SCRIPT = `
  2670. var o = {1e3: true};
  2671. var keys = Object.keys(o);
  2672. var o1 = {get 1e3() {return true;}};
  2673. var keys1 = Object.keys(o1);
  2674. var o2 = {1e21: true};
  2675. var keys2 = Object.keys(o2);
  2676. keys.length === 1 && keys[0] === "1000" &&
  2677. keys1.length === 1 && keys1[0] === "1000" && o1[1e3] === true &&
  2678. keys2.length === 1 && keys2[0] === "1e+21";
  2679. `
  2680. testScript(SCRIPT, valueTrue, t)
  2681. }
  2682. func TestEscapedObjectPropertyKeys(t *testing.T) {
  2683. const SCRIPT = `
  2684. var obj = {
  2685. w\u0069th: 42
  2686. };
  2687. var obj = {
  2688. with() {42}
  2689. };
  2690. `
  2691. _, err := Compile("", SCRIPT, false)
  2692. if err != nil {
  2693. t.Fatal(err)
  2694. }
  2695. }
  2696. func TestEscapedKeywords(t *testing.T) {
  2697. const SCRIPT = `r\u0065turn;`
  2698. _, err := Compile("", SCRIPT, false)
  2699. if err == nil {
  2700. t.Fatal("Expected error")
  2701. }
  2702. }
  2703. func TestEscapedLet(t *testing.T) {
  2704. const SCRIPT = `
  2705. this.let = 0;
  2706. l\u0065t // ASI
  2707. a;
  2708. // If the parser treated the previous escaped "let" as a lexical declaration,
  2709. // this variable declaration will result an early syntax error.
  2710. var a;
  2711. `
  2712. _, err := Compile("", SCRIPT, false)
  2713. if err != nil {
  2714. t.Fatal(err)
  2715. }
  2716. }
  2717. func TestObjectLiteralFuncProps(t *testing.T) {
  2718. const SCRIPT = `
  2719. (function() {
  2720. 'use strict';
  2721. var o = {
  2722. eval: function() {return 1;},
  2723. arguments() {return 2;},
  2724. test: function test1() {}
  2725. }
  2726. assert.sameValue(o.eval.name, "eval");
  2727. assert.sameValue(o.arguments.name, "arguments");
  2728. assert.sameValue(o.eval(), 1);
  2729. assert.sameValue(o.arguments(), 2);
  2730. assert.sameValue(o.test.name, "test1");
  2731. })();
  2732. `
  2733. testScriptWithTestLib(SCRIPT, _undefined, t)
  2734. }
  2735. func TestFuncName(t *testing.T) {
  2736. const SCRIPT = `
  2737. var method = 1;
  2738. var o = {
  2739. method: function() {
  2740. return method;
  2741. },
  2742. method1: function method() {
  2743. return method;
  2744. }
  2745. }
  2746. o.method() === 1 && o.method1() === o.method1;
  2747. `
  2748. testScript(SCRIPT, valueTrue, t)
  2749. }
  2750. func TestFuncNameAssign(t *testing.T) {
  2751. const SCRIPT = `
  2752. var f = function() {};
  2753. var f1;
  2754. f1 = function() {};
  2755. let f2 = function() {};
  2756. f.name === "f" && f1.name === "f1" && f2.name === "f2";
  2757. `
  2758. testScript(SCRIPT, valueTrue, t)
  2759. }
  2760. func TestLexicalDeclGlobal(t *testing.T) {
  2761. const SCRIPT = `
  2762. if (true) {
  2763. let it = "be";
  2764. if (it !== "be") {
  2765. throw new Error(it);
  2766. }
  2767. }
  2768. let thrown = false;
  2769. try {
  2770. it;
  2771. } catch(e) {
  2772. if (e instanceof ReferenceError) {
  2773. thrown = true;
  2774. }
  2775. }
  2776. thrown;
  2777. `
  2778. testScript(SCRIPT, valueTrue, t)
  2779. }
  2780. func TestLexicalDeclFunction(t *testing.T) {
  2781. const SCRIPT = `
  2782. function f() {
  2783. if (true) {
  2784. let it = "be";
  2785. if (it !== "be") {
  2786. throw new Error(it);
  2787. }
  2788. }
  2789. let thrown = false;
  2790. try {
  2791. it;
  2792. } catch(e) {
  2793. if (e instanceof ReferenceError) {
  2794. thrown = true;
  2795. }
  2796. }
  2797. return thrown;
  2798. }
  2799. f();
  2800. `
  2801. testScript(SCRIPT, valueTrue, t)
  2802. }
  2803. func TestLexicalDynamicScope(t *testing.T) {
  2804. const SCRIPT = `
  2805. const global = 1;
  2806. function f() {
  2807. const func = global + 1;
  2808. function inner() {
  2809. function assertThrows(fn) {
  2810. let thrown = false;
  2811. try {
  2812. fn();
  2813. } catch (e) {
  2814. if (e instanceof TypeError) {
  2815. thrown = true;
  2816. } else {
  2817. throw e;
  2818. }
  2819. }
  2820. if (!thrown) {
  2821. throw new Error("Did not throw");
  2822. }
  2823. }
  2824. assertThrows(function() {
  2825. func++;
  2826. });
  2827. assertThrows(function() {
  2828. global++;
  2829. });
  2830. assertThrows(function() {
  2831. eval("func++");
  2832. });
  2833. assertThrows(function() {
  2834. eval("global++");
  2835. });
  2836. return eval("func + 1");
  2837. }
  2838. return inner();
  2839. }
  2840. f();
  2841. `
  2842. testScript(SCRIPT, valueInt(3), t)
  2843. }
  2844. func TestLexicalDynamicScope1(t *testing.T) {
  2845. const SCRIPT = `
  2846. (function() {
  2847. const x = 1 * 4;
  2848. return (function() {
  2849. eval("");
  2850. return x;
  2851. })();
  2852. })();
  2853. `
  2854. testScript(SCRIPT, intToValue(4), t)
  2855. }
  2856. func TestLexicalDynamicScope2(t *testing.T) {
  2857. const SCRIPT = `
  2858. (function() {
  2859. const x = 1 + 3;
  2860. var y = 2 * 2;
  2861. eval("");
  2862. return x;
  2863. })();
  2864. `
  2865. testScript(SCRIPT, intToValue(4), t)
  2866. }
  2867. func TestNonStrictLet(t *testing.T) {
  2868. const SCRIPT = `
  2869. var let = 1;
  2870. `
  2871. testScript(SCRIPT, _undefined, t)
  2872. }
  2873. func TestStrictLet(t *testing.T) {
  2874. const SCRIPT = `
  2875. var let = 1;
  2876. `
  2877. _, err := Compile("", SCRIPT, true)
  2878. if err == nil {
  2879. t.Fatal("Expected an error")
  2880. }
  2881. }
  2882. func TestLetLet(t *testing.T) {
  2883. const SCRIPT = `
  2884. let let = 1;
  2885. `
  2886. _, err := Compile("", SCRIPT, false)
  2887. if err == nil {
  2888. t.Fatal("Expected an error")
  2889. }
  2890. }
  2891. func TestLetASI(t *testing.T) {
  2892. const SCRIPT = `
  2893. while (false) let // ASI
  2894. x = 1;
  2895. `
  2896. _, err := Compile("", SCRIPT, false)
  2897. if err != nil {
  2898. t.Fatal(err)
  2899. }
  2900. }
  2901. func TestLetASI1(t *testing.T) {
  2902. const SCRIPT = `
  2903. let
  2904. x = 1;
  2905. `
  2906. _, err := Compile("", SCRIPT, true)
  2907. if err != nil {
  2908. t.Fatal(err)
  2909. }
  2910. }
  2911. func TestLetNoASI(t *testing.T) {
  2912. const SCRIPT = `
  2913. function f() {}let
  2914. x = 1;
  2915. `
  2916. _, err := Compile("", SCRIPT, true)
  2917. if err != nil {
  2918. t.Fatal(err)
  2919. }
  2920. }
  2921. func TestLetNoASI1(t *testing.T) {
  2922. const SCRIPT = `
  2923. let
  2924. let = 1;
  2925. `
  2926. _, err := Compile("", SCRIPT, false)
  2927. if err == nil {
  2928. t.Fatal("Expected error")
  2929. }
  2930. }
  2931. func TestLetArrayWithNewline(t *testing.T) {
  2932. const SCRIPT = `
  2933. with ({}) let
  2934. [a] = 0;
  2935. `
  2936. _, err := Compile("", SCRIPT, false)
  2937. if err == nil {
  2938. t.Fatal("Expected error")
  2939. }
  2940. }
  2941. func TestDynamicUninitedVarAccess(t *testing.T) {
  2942. const SCRIPT = `
  2943. function f() {
  2944. var x;
  2945. return eval("x");
  2946. }
  2947. f();
  2948. `
  2949. testScript(SCRIPT, _undefined, t)
  2950. }
  2951. func TestLexicalForLoopNoClosure(t *testing.T) {
  2952. const SCRIPT = `
  2953. let sum = 0;
  2954. for (let i = 0; i < 3; i++) {
  2955. sum += i;
  2956. }
  2957. sum;
  2958. `
  2959. testScript(SCRIPT, valueInt(3), t)
  2960. }
  2961. func TestLexicalForLoopClosure(t *testing.T) {
  2962. const SCRIPT = `
  2963. var f = [];
  2964. for (let i = 0; i < 3; i++) {
  2965. f.push(function() {
  2966. return i;
  2967. });
  2968. }
  2969. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  2970. `
  2971. testScript(SCRIPT, valueTrue, t)
  2972. }
  2973. func TestLexicalForLoopClosureInNext(t *testing.T) {
  2974. const SCRIPT = `
  2975. const a = [];
  2976. for (let i = 0; i < 5; a.push(function () { return i; }), ++i) { }
  2977. let res = "";
  2978. for (let k = 0; k < 5; ++k) {
  2979. res += ""+a[k]();
  2980. }
  2981. res;
  2982. `
  2983. testScript(SCRIPT, asciiString("12345"), t)
  2984. }
  2985. func TestVarForLoop(t *testing.T) {
  2986. const SCRIPT = `
  2987. var f = [];
  2988. for (var i = 0, j = 0; i < 3; i++) {
  2989. f.push(function() {
  2990. return i;
  2991. });
  2992. }
  2993. f.length === 3 && f[0]() === 3 && f[1]() === 3 && f[2]() === 3;
  2994. `
  2995. testScript(SCRIPT, valueTrue, t)
  2996. }
  2997. func TestLexicalForOfLoop(t *testing.T) {
  2998. const SCRIPT = `
  2999. var f = [];
  3000. for (let i of [0, 1, 2]) {
  3001. f.push(function() {
  3002. return i;
  3003. });
  3004. }
  3005. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  3006. `
  3007. testScript(SCRIPT, valueTrue, t)
  3008. }
  3009. func TestLexicalForOfLoopContBreak(t *testing.T) {
  3010. const SCRIPT = `
  3011. const f = [];
  3012. for (let i of [0, 1, 2, 3, 4, 5]) {
  3013. if (i % 2) continue;
  3014. f.push(function() {
  3015. return i;
  3016. });
  3017. if (i > 2) break;
  3018. }
  3019. let res = "";
  3020. f.forEach(function(item) {res += item()});
  3021. f.length === 3 && res === "024";
  3022. `
  3023. testScript(SCRIPT, valueTrue, t)
  3024. }
  3025. func TestVarBlockConflict(t *testing.T) {
  3026. const SCRIPT = `
  3027. let x;
  3028. {
  3029. if (false) {
  3030. var x;
  3031. }
  3032. }
  3033. `
  3034. _, err := Compile("", SCRIPT, false)
  3035. if err == nil {
  3036. t.Fatal("Expected an error")
  3037. }
  3038. }
  3039. func TestVarBlockConflictEval(t *testing.T) {
  3040. const SCRIPT = `
  3041. assert.throws(SyntaxError, function() {
  3042. let x;
  3043. {
  3044. if (true) {
  3045. eval("var x");
  3046. }
  3047. }
  3048. });
  3049. `
  3050. testScriptWithTestLib(SCRIPT, _undefined, t)
  3051. }
  3052. func TestVarBlockNoConflict(t *testing.T) {
  3053. const SCRIPT = `
  3054. function f() {
  3055. let x;
  3056. function ff() {
  3057. {
  3058. var x = 3;
  3059. }
  3060. }
  3061. ff();
  3062. }
  3063. f();
  3064. `
  3065. testScript(SCRIPT, _undefined, t)
  3066. }
  3067. func TestVarBlockNoConflictEval(t *testing.T) {
  3068. const SCRIPT = `
  3069. function f() {
  3070. let x;
  3071. function ff() {
  3072. {
  3073. eval("var x = 3");
  3074. }
  3075. }
  3076. ff();
  3077. }
  3078. f();
  3079. `
  3080. testScript(SCRIPT, _undefined, t)
  3081. }
  3082. func TestVarDeclCorrectScope(t *testing.T) {
  3083. const SCRIPT = `
  3084. function f() {
  3085. {
  3086. let z;
  3087. eval("var x = 3");
  3088. }
  3089. return x;
  3090. }
  3091. f();
  3092. `
  3093. testScript(SCRIPT, valueInt(3), t)
  3094. }
  3095. func TestLexicalCatch(t *testing.T) {
  3096. const SCRIPT = `
  3097. try {
  3098. throw null;
  3099. } catch (e) {
  3100. let x = 1;
  3101. function f() {}
  3102. e;
  3103. }
  3104. `
  3105. testScript(SCRIPT, _null, t)
  3106. }
  3107. func TestArgumentsLexicalDecl(t *testing.T) {
  3108. const SCRIPT = `
  3109. function f1() {
  3110. let arguments;
  3111. return arguments;
  3112. }
  3113. f1(42);
  3114. `
  3115. testScript(SCRIPT, _undefined, t)
  3116. }
  3117. func TestArgumentsLexicalDeclAssign(t *testing.T) {
  3118. const SCRIPT = `
  3119. function f1() {
  3120. let arguments = arguments;
  3121. return a;
  3122. }
  3123. assert.throws(ReferenceError, function() {
  3124. f1(42);
  3125. });
  3126. `
  3127. testScriptWithTestLib(SCRIPT, _undefined, t)
  3128. }
  3129. func TestLexicalConstModifyFromEval(t *testing.T) {
  3130. const SCRIPT = `
  3131. const x = 1;
  3132. function f() {
  3133. eval("x = 2");
  3134. }
  3135. assert.throws(TypeError, function() {
  3136. f();
  3137. });
  3138. `
  3139. testScriptWithTestLib(SCRIPT, _undefined, t)
  3140. }
  3141. func TestLexicalStrictNames(t *testing.T) {
  3142. const SCRIPT = `let eval = 1;`
  3143. _, err := Compile("", SCRIPT, true)
  3144. if err == nil {
  3145. t.Fatal("Expected an error")
  3146. }
  3147. }
  3148. func TestAssignAfterStackExpand(t *testing.T) {
  3149. // make sure the reference to the variable x does not remain stale after the stack is copied
  3150. const SCRIPT = `
  3151. function f() {
  3152. let sum = 0;
  3153. for (let i = 0; i < arguments.length; i++) {
  3154. sum += arguments[i];
  3155. }
  3156. return sum;
  3157. }
  3158. function testAssignment() {
  3159. var x = 0;
  3160. var scope = {};
  3161. with (scope) {
  3162. x = (scope.x = f(0, 0, 0, 0, 0, 0, 1, 1), 1);
  3163. }
  3164. if (scope.x !== 2) {
  3165. throw new Error('#1: scope.x === 2. Actual: ' + (scope.x));
  3166. }
  3167. if (x !== 1) {
  3168. throw new Error('#2: x === 1. Actual: ' + (x));
  3169. }
  3170. }
  3171. testAssignment();
  3172. `
  3173. testScript(SCRIPT, _undefined, t)
  3174. }
  3175. func TestArgAccessFromDynamicStash(t *testing.T) {
  3176. const SCRIPT = `
  3177. function f(arg) {
  3178. function test() {
  3179. eval("");
  3180. return a;
  3181. }
  3182. return arg;
  3183. }
  3184. f(true);
  3185. `
  3186. testScript(SCRIPT, valueTrue, t)
  3187. }
  3188. func TestLoadMixedLex(t *testing.T) {
  3189. const SCRIPT = `
  3190. function f() {
  3191. let a = 1;
  3192. {
  3193. function inner() {
  3194. eval("var a = true");
  3195. return a;
  3196. }
  3197. return inner();
  3198. }
  3199. }
  3200. f();
  3201. `
  3202. testScript(SCRIPT, valueTrue, t)
  3203. }
  3204. func TestObjectLiteralSpread(t *testing.T) {
  3205. const SCRIPT = `
  3206. let src = {prop1: 1};
  3207. Object.defineProperty(src, "prop2", {value: 2, configurable: true});
  3208. Object.defineProperty(src, "prop3", {value: 3, enumerable: true, configurable: true});
  3209. let target = {prop4: 4, ...src};
  3210. assert(deepEqual(target, {prop1: 1, prop3: 3, prop4: 4}));
  3211. `
  3212. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3213. }
  3214. func TestArrayLiteralSpread(t *testing.T) {
  3215. const SCRIPT = `
  3216. let a1 = [1, 2];
  3217. let a2 = [3, 4];
  3218. let a = [...a1, 0, ...a2, 1];
  3219. assert(compareArray(a, [1, 2, 0, 3, 4, 1]));
  3220. `
  3221. testScriptWithTestLib(SCRIPT, _undefined, t)
  3222. }
  3223. func TestObjectAssignmentPattern(t *testing.T) {
  3224. const SCRIPT = `
  3225. let a, b, c;
  3226. ({a, b, c=3} = {a: 1, b: 2});
  3227. assert.sameValue(a, 1, "a");
  3228. assert.sameValue(b, 2, "b");
  3229. assert.sameValue(c, 3, "c");
  3230. `
  3231. testScriptWithTestLib(SCRIPT, _undefined, t)
  3232. }
  3233. func TestObjectAssignmentPatternNoDyn(t *testing.T) {
  3234. const SCRIPT = `
  3235. (function() {
  3236. let a, b, c;
  3237. ({a, b, c=3} = {a: 1, b: 2});
  3238. assert.sameValue(a, 1, "a");
  3239. assert.sameValue(b, 2, "b");
  3240. assert.sameValue(c, 3, "c");
  3241. })();
  3242. `
  3243. testScriptWithTestLib(SCRIPT, _undefined, t)
  3244. }
  3245. func TestObjectAssignmentPatternNested(t *testing.T) {
  3246. const SCRIPT = `
  3247. let a, b, c, d;
  3248. ({a, b, c: {d} = 3} = {a: 1, b: 2, c: {d: 4}});
  3249. assert.sameValue(a, 1, "a");
  3250. assert.sameValue(b, 2, "b");
  3251. assert.sameValue(c, undefined, "c");
  3252. assert.sameValue(d, 4, "d");
  3253. `
  3254. testScriptWithTestLib(SCRIPT, _undefined, t)
  3255. }
  3256. func TestObjectAssignmentPatternEvalOrder(t *testing.T) {
  3257. const SCRIPT = `
  3258. let trace = "";
  3259. let target_obj = {};
  3260. function src() {
  3261. trace += "src(),";
  3262. return {
  3263. get a() {
  3264. trace += "get a,";
  3265. return "a";
  3266. }
  3267. }
  3268. }
  3269. function prop1() {
  3270. trace += "prop1(),"
  3271. return {
  3272. toString: function() {
  3273. trace += "prop1-to-string(),";
  3274. return "a";
  3275. }
  3276. }
  3277. }
  3278. function prop2() {
  3279. trace += "prop2(),";
  3280. return {
  3281. toString: function() {
  3282. trace += "prop2-to-string(),";
  3283. return "b";
  3284. }
  3285. }
  3286. }
  3287. function target() {
  3288. trace += "target(),"
  3289. return target_obj;
  3290. }
  3291. let a, b;
  3292. ({[prop1()]: target().a, [prop2()]: b} = src());
  3293. if (target_obj.a !== "a") {
  3294. throw new Error("target_obj.a="+target_obj.a);
  3295. }
  3296. trace;
  3297. `
  3298. testScript(SCRIPT, asciiString("src(),prop1(),prop1-to-string(),target(),get a,prop2(),prop2-to-string(),"), t)
  3299. }
  3300. func TestArrayAssignmentPatternEvalOrder(t *testing.T) {
  3301. const SCRIPT = `
  3302. let trace = "";
  3303. let src_arr = {
  3304. [Symbol.iterator]: function() {
  3305. let done = false;
  3306. return {
  3307. next: function() {
  3308. trace += "next,";
  3309. if (!done) {
  3310. done = true;
  3311. return {value: 0};
  3312. }
  3313. return {done: true};
  3314. },
  3315. return: function() {
  3316. trace += "return,";
  3317. }
  3318. }
  3319. }
  3320. }
  3321. function src() {
  3322. trace += "src(),";
  3323. return src_arr;
  3324. }
  3325. let tgt = {
  3326. get a() {
  3327. trace += "get a,";
  3328. return "a";
  3329. },
  3330. get b() {
  3331. trace += "get b,";
  3332. return "b";
  3333. }
  3334. }
  3335. function target() {
  3336. trace += "target(),";
  3337. return tgt;
  3338. }
  3339. function default_a() {
  3340. trace += "default a,";
  3341. return "def_a";
  3342. }
  3343. function default_b() {
  3344. trace += "default b,";
  3345. return "def_b";
  3346. }
  3347. ([target().a = default_a(), target().b = default_b()] = src());
  3348. trace;
  3349. `
  3350. testScript(SCRIPT, asciiString("src(),target(),next,target(),next,default b,"), t)
  3351. }
  3352. func TestObjectAssignPatternRest(t *testing.T) {
  3353. const SCRIPT = `
  3354. let a, b, c, d;
  3355. ({a, b, c, ...d} = {a: 1, b: 2, d: 4});
  3356. assert.sameValue(a, 1, "a");
  3357. assert.sameValue(b, 2, "b");
  3358. assert.sameValue(c, undefined, "c");
  3359. assert(deepEqual(d, {d: 4}), "d");
  3360. `
  3361. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3362. }
  3363. func TestObjectBindPattern(t *testing.T) {
  3364. const SCRIPT = `
  3365. let {a, b, c, ...d} = {a: 1, b: 2, d: 4};
  3366. assert.sameValue(a, 1, "a");
  3367. assert.sameValue(b, 2, "b");
  3368. assert.sameValue(c, undefined, "c");
  3369. assert(deepEqual(d, {d: 4}), "d");
  3370. var { x: y, } = { x: 23 };
  3371. assert.sameValue(y, 23);
  3372. assert.throws(ReferenceError, function() {
  3373. x;
  3374. });
  3375. `
  3376. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3377. }
  3378. func TestObjLiteralShorthandWithInitializer(t *testing.T) {
  3379. const SCRIPT = `
  3380. o = {a=1};
  3381. `
  3382. _, err := Compile("", SCRIPT, false)
  3383. if err == nil {
  3384. t.Fatal("Expected an error")
  3385. }
  3386. }
  3387. func TestObjLiteralShorthandLetStringLit(t *testing.T) {
  3388. const SCRIPT = `
  3389. o = {"let"};
  3390. `
  3391. _, err := Compile("", SCRIPT, false)
  3392. if err == nil {
  3393. t.Fatal("Expected an error")
  3394. }
  3395. }
  3396. func TestObjLiteralComputedKeys(t *testing.T) {
  3397. const SCRIPT = `
  3398. let o = {
  3399. get [Symbol.toString]() {
  3400. }
  3401. }
  3402. `
  3403. testScript(SCRIPT, _undefined, t)
  3404. }
  3405. func TestObjLiteralComputedKeysEvalOrder(t *testing.T) {
  3406. const SCRIPT = `
  3407. let trace = [];
  3408. function key() {
  3409. trace.push("key");
  3410. return {
  3411. toString: function() {
  3412. trace.push("key-toString");
  3413. return "key";
  3414. }
  3415. }
  3416. }
  3417. function val() {
  3418. trace.push("val");
  3419. return "val";
  3420. }
  3421. const _ = {
  3422. [key()]: val(),
  3423. }
  3424. trace.join(",");
  3425. `
  3426. testScript(SCRIPT, asciiString("key,key-toString,val"), t)
  3427. }
  3428. func TestArrayAssignPattern(t *testing.T) {
  3429. const SCRIPT = `
  3430. let a, b;
  3431. ([a, b] = [1, 2]);
  3432. a === 1 && b === 2;
  3433. `
  3434. testScript(SCRIPT, valueTrue, t)
  3435. }
  3436. func TestArrayAssignPattern1(t *testing.T) {
  3437. const SCRIPT = `
  3438. let a, b;
  3439. ([a = 3, b = 2] = [1]);
  3440. a === 1 && b === 2;
  3441. `
  3442. testScript(SCRIPT, valueTrue, t)
  3443. }
  3444. func TestArrayAssignPatternLHS(t *testing.T) {
  3445. const SCRIPT = `
  3446. let a = {};
  3447. [ a.b, a['c'] = 2 ] = [1];
  3448. a.b === 1 && a.c === 2;
  3449. `
  3450. testScript(SCRIPT, valueTrue, t)
  3451. }
  3452. func TestArrayAssignPatternElision(t *testing.T) {
  3453. const SCRIPT = `
  3454. let a, b;
  3455. ([a,, b] = [1, 4, 2]);
  3456. a === 1 && b === 2;
  3457. `
  3458. testScript(SCRIPT, valueTrue, t)
  3459. }
  3460. func TestArrayAssignPatternRestPattern(t *testing.T) {
  3461. const SCRIPT = `
  3462. let a, b, z;
  3463. [ z, ...[a, b] ] = [0, 1, 2];
  3464. z === 0 && a === 1 && b === 2;
  3465. `
  3466. testScript(SCRIPT, valueTrue, t)
  3467. }
  3468. func TestArrayBindingPattern(t *testing.T) {
  3469. const SCRIPT = `
  3470. let [a, b] = [1, 2];
  3471. a === 1 && b === 2;
  3472. `
  3473. testScript(SCRIPT, valueTrue, t)
  3474. }
  3475. func TestObjectPatternShorthandInit(t *testing.T) {
  3476. const SCRIPT = `
  3477. [...{ x = 1 }] = [];
  3478. x;
  3479. `
  3480. testScript(SCRIPT, valueInt(1), t)
  3481. }
  3482. func TestArrayBindingPatternRestPattern(t *testing.T) {
  3483. const SCRIPT = `
  3484. const [a, b, ...[c, d]] = [1, 2, 3, 4];
  3485. a === 1 && b === 2 && c === 3 && d === 4;
  3486. `
  3487. testScript(SCRIPT, valueTrue, t)
  3488. }
  3489. func TestForVarPattern(t *testing.T) {
  3490. const SCRIPT = `
  3491. var o = {a: 1};
  3492. var trace = "";
  3493. for (var [key, value] of Object.entries(o)) {
  3494. trace += key+":"+value;
  3495. }
  3496. trace;
  3497. `
  3498. testScript(SCRIPT, asciiString("a:1"), t)
  3499. }
  3500. func TestForLexPattern(t *testing.T) {
  3501. const SCRIPT = `
  3502. var o = {a: 1};
  3503. var trace = "";
  3504. for (const [key, value] of Object.entries(o)) {
  3505. trace += key+":"+value;
  3506. }
  3507. trace;
  3508. `
  3509. testScript(SCRIPT, asciiString("a:1"), t)
  3510. }
  3511. func TestBindingPatternRestTrailingComma(t *testing.T) {
  3512. const SCRIPT = `
  3513. const [a, b, ...rest,] = [];
  3514. `
  3515. _, err := Compile("", SCRIPT, false)
  3516. if err == nil {
  3517. t.Fatal("Expected an error")
  3518. }
  3519. }
  3520. func TestAssignPatternRestTrailingComma(t *testing.T) {
  3521. const SCRIPT = `
  3522. ([a, b, ...rest,] = []);
  3523. `
  3524. _, err := Compile("", SCRIPT, false)
  3525. if err == nil {
  3526. t.Fatal("Expected an error")
  3527. }
  3528. }
  3529. func TestFuncParamInitializerSimple(t *testing.T) {
  3530. const SCRIPT = `
  3531. function f(a = 1) {
  3532. return a;
  3533. }
  3534. ""+f()+f(2);
  3535. `
  3536. testScript(SCRIPT, asciiString("12"), t)
  3537. }
  3538. func TestFuncParamObjectPatternSimple(t *testing.T) {
  3539. const SCRIPT = `
  3540. function f({a, b} = {a: 1, b: 2}) {
  3541. return "" + a + b;
  3542. }
  3543. ""+f()+" "+f({a: 3, b: 4});
  3544. `
  3545. testScript(SCRIPT, asciiString("12 34"), t)
  3546. }
  3547. func TestFuncParamRestStackSimple(t *testing.T) {
  3548. const SCRIPT = `
  3549. function f(arg1, ...rest) {
  3550. return rest;
  3551. }
  3552. let ar = f(1, 2, 3);
  3553. ar.join(",");
  3554. `
  3555. testScript(SCRIPT, asciiString("2,3"), t)
  3556. }
  3557. func TestFuncParamRestStashSimple(t *testing.T) {
  3558. const SCRIPT = `
  3559. function f(arg1, ...rest) {
  3560. eval("true");
  3561. return rest;
  3562. }
  3563. let ar = f(1, 2, 3);
  3564. ar.join(",");
  3565. `
  3566. testScript(SCRIPT, asciiString("2,3"), t)
  3567. }
  3568. func TestRestArgsNotInStash(t *testing.T) {
  3569. const SCRIPT = `
  3570. function f(...rest) {
  3571. () => rest;
  3572. return rest.length;
  3573. }
  3574. f(1,2);
  3575. `
  3576. testScript(SCRIPT, valueInt(2), t)
  3577. }
  3578. func TestRestArgsInStash(t *testing.T) {
  3579. const SCRIPT = `
  3580. function f(first, ...rest) {
  3581. () => first;
  3582. () => rest;
  3583. return rest.length;
  3584. }
  3585. f(1,2);
  3586. `
  3587. testScript(SCRIPT, valueInt(1), t)
  3588. }
  3589. func TestRestArgsInStashFwdRef(t *testing.T) {
  3590. const SCRIPT = `
  3591. function f(first = eval(), ...rest) {
  3592. () => first;
  3593. () => rest;
  3594. return rest.length === 1 && rest[0] === 2;
  3595. }
  3596. f(1,2);
  3597. `
  3598. testScript(SCRIPT, valueTrue, t)
  3599. }
  3600. func TestFuncParamRestPattern(t *testing.T) {
  3601. const SCRIPT = `
  3602. function f(arg1, ...{0: rest1, 1: rest2}) {
  3603. return ""+arg1+" "+rest1+" "+rest2;
  3604. }
  3605. f(1, 2, 3);
  3606. `
  3607. testScript(SCRIPT, asciiString("1 2 3"), t)
  3608. }
  3609. func TestFuncParamForwardRef(t *testing.T) {
  3610. const SCRIPT = `
  3611. function f(a = b + 1, b) {
  3612. return ""+a+" "+b;
  3613. }
  3614. f(1, 2);
  3615. `
  3616. testScript(SCRIPT, asciiString("1 2"), t)
  3617. }
  3618. func TestFuncParamForwardRefMissing(t *testing.T) {
  3619. const SCRIPT = `
  3620. function f(a = b + 1, b) {
  3621. return ""+a+" "+b;
  3622. }
  3623. assert.throws(ReferenceError, function() {
  3624. f();
  3625. });
  3626. `
  3627. testScriptWithTestLib(SCRIPT, _undefined, t)
  3628. }
  3629. func TestFuncParamInnerRef(t *testing.T) {
  3630. const SCRIPT = `
  3631. function f(a = inner) {
  3632. var inner = 42;
  3633. return a;
  3634. }
  3635. assert.throws(ReferenceError, function() {
  3636. f();
  3637. });
  3638. `
  3639. testScriptWithTestLib(SCRIPT, _undefined, t)
  3640. }
  3641. func TestFuncParamInnerRefEval(t *testing.T) {
  3642. const SCRIPT = `
  3643. function f(a = eval("inner")) {
  3644. var inner = 42;
  3645. return a;
  3646. }
  3647. assert.throws(ReferenceError, function() {
  3648. f();
  3649. });
  3650. `
  3651. testScriptWithTestLib(SCRIPT, _undefined, t)
  3652. }
  3653. func TestFuncParamCalleeName(t *testing.T) {
  3654. const SCRIPT = `
  3655. function f(a = f) {
  3656. var f;
  3657. return f;
  3658. }
  3659. typeof f();
  3660. `
  3661. testScript(SCRIPT, asciiString("undefined"), t)
  3662. }
  3663. func TestFuncParamVarCopy(t *testing.T) {
  3664. const SCRIPT = `
  3665. function f(a = f) {
  3666. var a;
  3667. return a;
  3668. }
  3669. typeof f();
  3670. `
  3671. testScript(SCRIPT, asciiString("function"), t)
  3672. }
  3673. func TestFuncParamScope(t *testing.T) {
  3674. const SCRIPT = `
  3675. var x = 'outside';
  3676. var probe1, probe2;
  3677. function f(
  3678. _ = probe1 = function() { return x; },
  3679. __ = (eval('var x = "inside";'), probe2 = function() { return x; })
  3680. ) {
  3681. }
  3682. f();
  3683. probe1()+" "+probe2();
  3684. `
  3685. testScript(SCRIPT, asciiString("inside inside"), t)
  3686. }
  3687. func TestDefParamsStackPtr(t *testing.T) {
  3688. const SCRIPT = `
  3689. function A() {};
  3690. A.B = function () {};
  3691. function D(message = '') {
  3692. var C = A.B;
  3693. C([1,2,3]);
  3694. };
  3695. D();
  3696. `
  3697. testScript(SCRIPT, _undefined, t)
  3698. }
  3699. func TestNestedVariadicCalls(t *testing.T) {
  3700. const SCRIPT = `
  3701. function f() {
  3702. return Array.prototype.join.call(arguments, ",");
  3703. }
  3704. f(...[1], "a", f(...[2]));
  3705. `
  3706. testScript(SCRIPT, asciiString("1,a,2"), t)
  3707. }
  3708. func TestVariadicNew(t *testing.T) {
  3709. const SCRIPT = `
  3710. function C() {
  3711. this.res = Array.prototype.join.call(arguments, ",");
  3712. }
  3713. var c = new C(...[1], "a", new C(...[2]).res);
  3714. c.res;
  3715. `
  3716. testScript(SCRIPT, asciiString("1,a,2"), t)
  3717. }
  3718. func TestVariadicUseStackVars(t *testing.T) {
  3719. const SCRIPT = `
  3720. function A(message) { return message; }
  3721. function B(...args){
  3722. return A(...args);
  3723. }
  3724. B("C");
  3725. `
  3726. testScript(SCRIPT, asciiString("C"), t)
  3727. }
  3728. func TestCatchParamPattern(t *testing.T) {
  3729. const SCRIPT = `
  3730. function f() {
  3731. let x = 3;
  3732. try {
  3733. throw {a: 1, b: 2};
  3734. } catch ({a, b, c = x}) {
  3735. let x = 99;
  3736. return ""+a+" "+b+" "+c;
  3737. }
  3738. }
  3739. f();
  3740. `
  3741. testScript(SCRIPT, asciiString("1 2 3"), t)
  3742. }
  3743. func TestArrowUseStrict(t *testing.T) {
  3744. // simple parameter list -- ok
  3745. _, err := Compile("", "(a) => {'use strict';}", false)
  3746. if err != nil {
  3747. t.Fatal(err)
  3748. }
  3749. // non-simple parameter list -- syntax error
  3750. _, err = Compile("", "(a=0) => {'use strict';}", false)
  3751. if err == nil {
  3752. t.Fatal("expected error")
  3753. }
  3754. }
  3755. func TestArrowBoxedThis(t *testing.T) {
  3756. const SCRIPT = `
  3757. var context;
  3758. fn = function() {
  3759. return (arg) => { var local; context = this; };
  3760. };
  3761. fn()();
  3762. context === this;
  3763. `
  3764. testScript(SCRIPT, valueTrue, t)
  3765. }
  3766. func TestParameterOverride(t *testing.T) {
  3767. const SCRIPT = `
  3768. function f(arg) {
  3769. var arg = arg || "default"
  3770. return arg
  3771. }
  3772. f()
  3773. `
  3774. testScript(SCRIPT, asciiString("default"), t)
  3775. }
  3776. func TestEvalInIterScope(t *testing.T) {
  3777. const SCRIPT = `
  3778. for (let a = 0; a < 1; a++) {
  3779. eval("a");
  3780. }
  3781. `
  3782. testScript(SCRIPT, valueInt(0), t)
  3783. }
  3784. func TestTemplateLiterals(t *testing.T) {
  3785. vm := New()
  3786. _, err := vm.RunString("const a = 1, b = 'b';")
  3787. if err != nil {
  3788. t.Fatal(err)
  3789. }
  3790. f := func(t *testing.T, template, expected string) {
  3791. res, err := vm.RunString(template)
  3792. if err != nil {
  3793. t.Fatal(err)
  3794. }
  3795. if actual := res.Export(); actual != expected {
  3796. t.Fatalf("Expected: %q, actual: %q", expected, actual)
  3797. }
  3798. }
  3799. t.Run("empty", func(t *testing.T) {
  3800. f(t, "``", "")
  3801. })
  3802. t.Run("noSub", func(t *testing.T) {
  3803. f(t, "`test`", "test")
  3804. })
  3805. t.Run("emptyTail", func(t *testing.T) {
  3806. f(t, "`a=${a},b=${b}`", "a=1,b=b")
  3807. })
  3808. t.Run("emptyHead", func(t *testing.T) {
  3809. f(t, "`${a},b=${b}$`", "1,b=b$")
  3810. })
  3811. t.Run("headAndTail", func(t *testing.T) {
  3812. f(t, "`a=${a},b=${b}$`", "a=1,b=b$")
  3813. })
  3814. }
  3815. func TestTaggedTemplate(t *testing.T) {
  3816. const SCRIPT = `
  3817. let res;
  3818. const o = {
  3819. tmpl() {
  3820. res = this;
  3821. return () => {};
  3822. }
  3823. }
  3824. ` +
  3825. "o.tmpl()`test`;" + `
  3826. res === o;
  3827. `
  3828. testScript(SCRIPT, valueTrue, t)
  3829. }
  3830. func TestDuplicateGlobalFunc(t *testing.T) {
  3831. const SCRIPT = `
  3832. function a(){}
  3833. function b(){ return "b" }
  3834. function c(){ return "c" }
  3835. function a(){}
  3836. b();
  3837. `
  3838. testScript(SCRIPT, asciiString("b"), t)
  3839. }
  3840. func TestDuplicateFunc(t *testing.T) {
  3841. const SCRIPT = `
  3842. function f() {
  3843. function a(){}
  3844. function b(){ return "b" }
  3845. function c(){ return "c" }
  3846. function a(){}
  3847. return b();
  3848. }
  3849. f();
  3850. `
  3851. testScript(SCRIPT, asciiString("b"), t)
  3852. }
  3853. func TestSrcLocations(t *testing.T) {
  3854. // Do not reformat, assertions depend on line and column numbers
  3855. const SCRIPT = `
  3856. let i = {
  3857. valueOf() {
  3858. throw new Error();
  3859. }
  3860. };
  3861. try {
  3862. i++;
  3863. } catch(e) {
  3864. assertStack(e, [["test.js", "valueOf", 4, 10],
  3865. ["test.js", "", 8, 3]
  3866. ]);
  3867. }
  3868. Object.defineProperty(globalThis, "x", {
  3869. get() {
  3870. throw new Error();
  3871. },
  3872. set() {
  3873. throw new Error();
  3874. }
  3875. });
  3876. try {
  3877. x;
  3878. } catch(e) {
  3879. assertStack(e, [["test.js", "get", 17, 10],
  3880. ["test.js", "", 25, 3]
  3881. ]);
  3882. }
  3883. try {
  3884. x++;
  3885. } catch(e) {
  3886. assertStack(e, [["test.js", "get", 17, 10],
  3887. ["test.js", "", 33, 3]
  3888. ]);
  3889. }
  3890. try {
  3891. x = 2;
  3892. } catch(e) {
  3893. assertStack(e, [["test.js", "set", 20, 10],
  3894. ["test.js", "", 41, 3]
  3895. ]);
  3896. }
  3897. try {
  3898. +i;
  3899. } catch(e) {
  3900. assertStack(e, [["test.js", "valueOf", 4, 10],
  3901. ["test.js", "", 49, 4]
  3902. ]);
  3903. }
  3904. function assertStack(e, expected) {
  3905. const lines = e.stack.split('\n');
  3906. let lnum = 1;
  3907. for (const [file, func, line, col] of expected) {
  3908. const expLine = func === "" ?
  3909. "\tat " + file + ":" + line + ":" + col + "(" :
  3910. "\tat " + func + " (" + file + ":" + line + ":" + col + "(";
  3911. assert.sameValue(lines[lnum].substring(0, expLine.length), expLine, "line " + lnum);
  3912. lnum++;
  3913. }
  3914. }
  3915. `
  3916. testScriptWithTestLib(SCRIPT, _undefined, t)
  3917. }
  3918. func TestSrcLocationThrowLiteral(t *testing.T) {
  3919. vm := New()
  3920. _, err := vm.RunString(`
  3921. const z = 1;
  3922. throw "";
  3923. `)
  3924. if ex, ok := err.(*Exception); ok {
  3925. pos := ex.stack[0].Position()
  3926. if pos.Line != 3 {
  3927. t.Fatal(pos)
  3928. }
  3929. } else {
  3930. t.Fatal(err)
  3931. }
  3932. }
  3933. func TestSrcLocation(t *testing.T) {
  3934. prg := MustCompile("test.js", `
  3935. f();
  3936. var x = 1;
  3937. let y = 1;
  3938. let [z1, z2] = [0, 0];
  3939. var [z3, z4] = [0, 0];
  3940. `, false)
  3941. const (
  3942. varLine = 3
  3943. letLine = 4
  3944. dstrLetLine = 5
  3945. dstrVarLine = 7
  3946. )
  3947. linesOfInterest := map[int]string{
  3948. varLine: "var",
  3949. letLine: "let",
  3950. dstrLetLine: "destruct let",
  3951. dstrVarLine: "destruct var",
  3952. }
  3953. for i := range prg.code {
  3954. loc := prg.src.Position(prg.sourceOffset(i))
  3955. delete(linesOfInterest, loc.Line)
  3956. if len(linesOfInterest) == 0 {
  3957. break
  3958. }
  3959. }
  3960. for _, v := range linesOfInterest {
  3961. t.Fatalf("no %s line", v)
  3962. }
  3963. }
  3964. func TestBadObjectKey(t *testing.T) {
  3965. _, err := Compile("", "({!:0})", false)
  3966. if err == nil {
  3967. t.Fatal("expected error")
  3968. }
  3969. }
  3970. func TestConstantFolding(t *testing.T) {
  3971. testValues := func(prg *Program, result Value, t *testing.T) {
  3972. if len(prg.values) != 1 || !prg.values[0].SameAs(result) {
  3973. prg.dumpCode(t.Logf)
  3974. t.Fatalf("values: %v", prg.values)
  3975. }
  3976. }
  3977. f := func(src string, result Value, t *testing.T) {
  3978. prg := MustCompile("test.js", src, false)
  3979. testValues(prg, result, t)
  3980. New().testPrg(prg, result, t)
  3981. }
  3982. ff := func(src string, result Value, t *testing.T) {
  3983. prg := MustCompile("test.js", src, false)
  3984. fl := prg.code[0].(*newFunc)
  3985. testValues(fl.prg, result, t)
  3986. New().testPrg(prg, result, t)
  3987. }
  3988. t.Run("lexical binding", func(t *testing.T) {
  3989. f("const x = 1 + 2; x", valueInt(3), t)
  3990. })
  3991. t.Run("var binding", func(t *testing.T) {
  3992. f("var x = 1 + 2; x", valueInt(3), t)
  3993. })
  3994. t.Run("assignment", func(t *testing.T) {
  3995. f("x = 1 + 2; x", valueInt(3), t)
  3996. })
  3997. t.Run("object pattern", func(t *testing.T) {
  3998. f("const {x = 1 + 2} = {}; x", valueInt(3), t)
  3999. })
  4000. t.Run("array pattern", func(t *testing.T) {
  4001. f("const [x = 1 + 2] = []; x", valueInt(3), t)
  4002. })
  4003. t.Run("object literal", func(t *testing.T) {
  4004. f("var o = {x: 1 + 2}; o.x", valueInt(3), t)
  4005. })
  4006. t.Run("array literal", func(t *testing.T) {
  4007. f("var a = [3, 3, 3, 1 + 2]; a[3]", valueInt(3), t)
  4008. })
  4009. t.Run("default function parameter", func(t *testing.T) {
  4010. ff("function f(arg = 1 + 2) {return arg}; f()", valueInt(3), t)
  4011. })
  4012. t.Run("return", func(t *testing.T) {
  4013. ff("function f() {return 1 + 2}; f()", valueInt(3), t)
  4014. })
  4015. }
  4016. func TestAssignBeforeInit(t *testing.T) {
  4017. const SCRIPT = `
  4018. assert.throws(ReferenceError, () => {
  4019. a = 1;
  4020. let a;
  4021. });
  4022. assert.throws(ReferenceError, () => {
  4023. ({a, b} = {a: 1, b: 2});
  4024. let a, b;
  4025. });
  4026. assert.throws(ReferenceError, () => {
  4027. (function() {
  4028. eval("");
  4029. ({a} = {a: 1});
  4030. })();
  4031. let a;
  4032. });
  4033. assert.throws(ReferenceError, () => {
  4034. const ctx = {x: 1};
  4035. function t() {
  4036. delete ctx.x;
  4037. return 42;
  4038. }
  4039. with(ctx) {
  4040. (function() {
  4041. 'use strict';
  4042. ({x} = {x: t()});
  4043. })();
  4044. }
  4045. return ctx.x;
  4046. });
  4047. assert.throws(ReferenceError, () => {
  4048. const ctx = {x: 1};
  4049. function t() {
  4050. delete ctx.x;
  4051. return 42;
  4052. }
  4053. with(ctx) {
  4054. (function() {
  4055. 'use strict';
  4056. const src = {};
  4057. Object.defineProperty(src, "x", {
  4058. get() {
  4059. return t();
  4060. }
  4061. });
  4062. ({x} = src);
  4063. })();
  4064. }
  4065. return ctx.x;
  4066. });
  4067. `
  4068. testScriptWithTestLib(SCRIPT, _undefined, t)
  4069. }
  4070. func TestOptChainCallee(t *testing.T) {
  4071. const SCRIPT = `
  4072. var a;
  4073. assert.sameValue(a?.(true), undefined);
  4074. a = null;
  4075. assert.sameValue(a?.(), undefined);
  4076. var o = {n: null};
  4077. assert.sameValue(o.m?.(true), undefined);
  4078. assert.sameValue(o.n?.(true), undefined);
  4079. `
  4080. testScriptWithTestLib(SCRIPT, _undefined, t)
  4081. }
  4082. func TestObjectLiteralSuper(t *testing.T) {
  4083. const SCRIPT = `
  4084. const proto = {
  4085. m() {
  4086. return 40;
  4087. }
  4088. }
  4089. const o = {
  4090. m() {
  4091. return super.m() + 2;
  4092. }
  4093. }
  4094. o.__proto__ = proto;
  4095. o.m();
  4096. `
  4097. testScript(SCRIPT, intToValue(42), t)
  4098. }
  4099. func TestClassCaptureThisInFieldInit(t *testing.T) {
  4100. const SCRIPT = `
  4101. let capture;
  4102. class C {
  4103. a = () => this
  4104. }
  4105. let c = new C();
  4106. c.a() === c;
  4107. `
  4108. testScript(SCRIPT, valueTrue, t)
  4109. }
  4110. func TestClassUseThisInFieldInit(t *testing.T) {
  4111. const SCRIPT = `
  4112. let capture;
  4113. class C {
  4114. a = this
  4115. }
  4116. let c = new C();
  4117. c.a === c;
  4118. `
  4119. testScript(SCRIPT, valueTrue, t)
  4120. }
  4121. func TestClassCaptureThisInStaticFieldInit(t *testing.T) {
  4122. const SCRIPT = `
  4123. let capture;
  4124. class C {
  4125. static a = (capture = () => this, 0)
  4126. }
  4127. let c = new C();
  4128. capture() === C;
  4129. `
  4130. testScript(SCRIPT, valueTrue, t)
  4131. }
  4132. func TestClassDynCaptureThisInStaticFieldInit(t *testing.T) {
  4133. const SCRIPT = `
  4134. class C {
  4135. static a = eval("this")
  4136. }
  4137. C.a === C;
  4138. `
  4139. testScript(SCRIPT, valueTrue, t)
  4140. }
  4141. func TestCompileClass(t *testing.T) {
  4142. const SCRIPT = `
  4143. class C extends Error {
  4144. a = true;
  4145. b = 1;
  4146. ["b".toUpperCase()] = 2
  4147. static A = Math.random() < 1
  4148. constructor(message = "My Error") {
  4149. super(message);
  4150. }
  4151. static M() {
  4152. }
  4153. static M1() {
  4154. }
  4155. m() {
  4156. //return C.a;
  4157. }
  4158. m1() {
  4159. return true;
  4160. }
  4161. static {
  4162. this.supername = super.name;
  4163. }
  4164. }
  4165. let c = new C();
  4166. c.a === true && c.b === 1 && c.B === 2 && c.m1() && C.A && C.supername === "Error";
  4167. `
  4168. testScript(SCRIPT, valueTrue, t)
  4169. }
  4170. func TestSuperInEval(t *testing.T) {
  4171. const SCRIPT = `
  4172. class C extends Error {
  4173. constructor() {
  4174. eval("super()");
  4175. }
  4176. m() {
  4177. return eval("super.name");
  4178. }
  4179. }
  4180. let c = new C();
  4181. c.m() === "Error";
  4182. `
  4183. testScript(SCRIPT, valueTrue, t)
  4184. }
  4185. func TestSuperRefDot(t *testing.T) {
  4186. const SCRIPT = `
  4187. let thisGet, thisSet;
  4188. class P {
  4189. _p = 0
  4190. get p() {
  4191. thisGet = this;
  4192. return this._p;
  4193. }
  4194. set p(v) {
  4195. thisSet = this;
  4196. this._p = v;
  4197. }
  4198. }
  4199. class C extends P {
  4200. g() {
  4201. return super.p;
  4202. }
  4203. s(v) {
  4204. super.p = v;
  4205. }
  4206. inc() {
  4207. super.p++;
  4208. }
  4209. incR() {
  4210. return super.p++;
  4211. }
  4212. inc1() {
  4213. ++super.p;
  4214. }
  4215. inc1R() {
  4216. return ++super.p;
  4217. }
  4218. unary() {
  4219. return +super.p;
  4220. }
  4221. pattern() {
  4222. [super.p] = [9];
  4223. }
  4224. }
  4225. let o = new C();
  4226. assert.sameValue(o.g(), 0, "get value");
  4227. assert.sameValue(thisGet, o, "get this");
  4228. o.s(1);
  4229. assert.sameValue(o._p, 1, "set value");
  4230. assert.sameValue(thisSet, o, "set this");
  4231. thisGet = undefined;
  4232. thisSet = undefined;
  4233. o.inc();
  4234. assert.sameValue(o._p, 2, "inc value");
  4235. assert.sameValue(thisGet, o, "inc thisGet");
  4236. assert.sameValue(thisSet, o, "inc thisSet");
  4237. thisGet = undefined;
  4238. thisSet = undefined;
  4239. assert.sameValue(o.incR(), 2, "incR result");
  4240. assert.sameValue(o._p, 3, "incR value");
  4241. assert.sameValue(thisGet, o, "incR thisGet");
  4242. assert.sameValue(thisSet, o, "incR thisSet");
  4243. thisGet = undefined;
  4244. thisSet = undefined;
  4245. o.inc1();
  4246. assert.sameValue(o._p, 4, "inc1 value");
  4247. assert.sameValue(thisGet, o, "inc1 thisGet");
  4248. assert.sameValue(thisSet, o, "inc1 thisSet");
  4249. thisGet = undefined;
  4250. thisSet = undefined;
  4251. assert.sameValue(o.inc1R(), 5, "inc1R result");
  4252. assert.sameValue(o._p, 5, "inc1R value");
  4253. assert.sameValue(thisGet, o, "inc1R thisGet");
  4254. assert.sameValue(thisSet, o, "inc1R thisSet");
  4255. assert.sameValue(o.unary(), 5, "unary");
  4256. o.pattern();
  4257. assert.sameValue(o._p, 9, "pattern");
  4258. `
  4259. testScriptWithTestLib(SCRIPT, _undefined, t)
  4260. }
  4261. func TestPrivateRefDot(t *testing.T) {
  4262. const SCRIPT = `
  4263. class C {
  4264. #p = 0;
  4265. g() {
  4266. return this.#p;
  4267. }
  4268. s(v) {
  4269. this.#p = v;
  4270. }
  4271. inc() {
  4272. this.#p++;
  4273. }
  4274. incR() {
  4275. return this.#p++;
  4276. }
  4277. inc1() {
  4278. ++this.#p;
  4279. }
  4280. inc1R() {
  4281. return ++this.#p;
  4282. }
  4283. pattern() {
  4284. [this.#p] = [9];
  4285. }
  4286. }
  4287. let o = new C();
  4288. assert.sameValue(o.g(), 0, "get value");
  4289. o.s(1);
  4290. assert.sameValue(o.g(), 1, "set value");
  4291. o.inc();
  4292. assert.sameValue(o.g(), 2, "inc value");
  4293. assert.sameValue(o.incR(), 2, "incR result");
  4294. assert.sameValue(o.g(), 3, "incR value");
  4295. o.inc1();
  4296. assert.sameValue(o.g(), 4, "inc1 value");
  4297. assert.sameValue(o.inc1R(), 5, "inc1R result");
  4298. assert.sameValue(o.g(), 5, "inc1R value");
  4299. o.pattern();
  4300. assert.sameValue(o.g(), 9, "pattern");
  4301. `
  4302. testScriptWithTestLib(SCRIPT, _undefined, t)
  4303. }
  4304. func TestPrivateRefDotEval(t *testing.T) {
  4305. const SCRIPT = `
  4306. class C {
  4307. #p = 0;
  4308. g() {
  4309. return eval("this.#p");
  4310. }
  4311. s(v) {
  4312. eval("this.#p = v");
  4313. }
  4314. incR() {
  4315. return eval("this.#p++");
  4316. }
  4317. inc1R() {
  4318. return eval("++this.#p");
  4319. }
  4320. pattern() {
  4321. eval("[this.#p] = [9]");
  4322. }
  4323. }
  4324. let o = new C();
  4325. assert.sameValue(o.g(), 0, "get value");
  4326. o.s(1);
  4327. assert.sameValue(o.g(), 1, "set value");
  4328. assert.sameValue(o.incR(), 1, "incR result");
  4329. assert.sameValue(o.g(), 2, "incR value");
  4330. assert.sameValue(o.inc1R(), 3, "inc1R result");
  4331. assert.sameValue(o.g(), 3, "inc1R value");
  4332. o.pattern();
  4333. assert.sameValue(o.g(), 9, "pattern");
  4334. `
  4335. testScriptWithTestLib(SCRIPT, _undefined, t)
  4336. }
  4337. func TestSuperRefDotCallee(t *testing.T) {
  4338. const SCRIPT = `
  4339. class P {
  4340. get p() {
  4341. return function() {
  4342. return this;
  4343. };
  4344. }
  4345. }
  4346. class C extends P {
  4347. m() {
  4348. return super.p();
  4349. }
  4350. }
  4351. let o = new C();
  4352. o.m() === o;
  4353. `
  4354. testScript(SCRIPT, valueTrue, t)
  4355. }
  4356. func TestSuperRefBracket(t *testing.T) {
  4357. const SCRIPT = `
  4358. let PROP = "p";
  4359. let thisGet, thisSet;
  4360. class P {
  4361. _p = 0
  4362. get p() {
  4363. thisGet = this;
  4364. return this._p;
  4365. }
  4366. set p(v) {
  4367. thisSet = this;
  4368. this._p = v;
  4369. }
  4370. }
  4371. class C extends P {
  4372. g() {
  4373. return super[PROP];
  4374. }
  4375. s(v) {
  4376. super[PROP] = v;
  4377. }
  4378. inc() {
  4379. super[PROP]++;
  4380. }
  4381. incR() {
  4382. return super[PROP]++;
  4383. }
  4384. inc1() {
  4385. ++super[PROP];
  4386. }
  4387. inc1R() {
  4388. return ++super[PROP];
  4389. }
  4390. pattern() {
  4391. [super[PROP]] = [9];
  4392. }
  4393. }
  4394. let o = new C();
  4395. assert.sameValue(o.g(), 0, "get value");
  4396. assert.sameValue(thisGet, o, "get this");
  4397. o.s(1);
  4398. assert.sameValue(o._p, 1, "set value");
  4399. assert.sameValue(thisSet, o, "set this");
  4400. thisGet = undefined;
  4401. thisSet = undefined;
  4402. o.inc();
  4403. assert.sameValue(o._p, 2, "inc value");
  4404. assert.sameValue(thisGet, o, "inc thisGet");
  4405. assert.sameValue(thisSet, o, "inc thisSet");
  4406. thisGet = undefined;
  4407. thisSet = undefined;
  4408. assert.sameValue(o.incR(), 2, "incR result");
  4409. assert.sameValue(o._p, 3, "incR value");
  4410. assert.sameValue(thisGet, o, "incR thisGet");
  4411. assert.sameValue(thisSet, o, "incR thisSet");
  4412. thisGet = undefined;
  4413. thisSet = undefined;
  4414. o.inc1();
  4415. assert.sameValue(o._p, 4, "inc1 value");
  4416. assert.sameValue(thisGet, o, "inc1 thisGet");
  4417. assert.sameValue(thisSet, o, "inc1 thisSet");
  4418. thisGet = undefined;
  4419. thisSet = undefined;
  4420. assert.sameValue(o.inc1R(), 5, "inc1R result");
  4421. assert.sameValue(o._p, 5, "inc1R value");
  4422. assert.sameValue(thisGet, o, "inc1R thisGet");
  4423. assert.sameValue(thisSet, o, "inc1R thisSet");
  4424. o.pattern();
  4425. assert.sameValue(o._p, 9, "pattern");
  4426. `
  4427. testScriptWithTestLib(SCRIPT, _undefined, t)
  4428. }
  4429. func TestSuperRefBracketEvalOrder(t *testing.T) {
  4430. const SCRIPT = `
  4431. let keyCallCount = 0;
  4432. function key() {
  4433. keyCallCount++;
  4434. C.prototype.__proto__ = null;
  4435. return "k";
  4436. }
  4437. class C {
  4438. constructor() {
  4439. super[key()]++;
  4440. }
  4441. }
  4442. assert.throws(TypeError, () => new C());
  4443. assert.sameValue(keyCallCount, 1);
  4444. `
  4445. testScriptWithTestLib(SCRIPT, _undefined, t)
  4446. }
  4447. func TestSuperRefBracketCallee(t *testing.T) {
  4448. const SCRIPT = `
  4449. let PROP = "p";
  4450. class P {
  4451. get p() {
  4452. return function() {
  4453. return this;
  4454. };
  4455. }
  4456. }
  4457. class C extends P {
  4458. m() {
  4459. return super[PROP]();
  4460. }
  4461. }
  4462. let o = new C();
  4463. o.m() === o;
  4464. `
  4465. testScript(SCRIPT, valueTrue, t)
  4466. }
  4467. func TestSuperBaseInCtor(t *testing.T) {
  4468. const SCRIPT = `
  4469. let result;
  4470. class Derived extends Object {
  4471. constructor() {
  4472. super();
  4473. result = super.constructor === Object;
  4474. }
  4475. }
  4476. new Derived();
  4477. result;
  4478. `
  4479. testScript(SCRIPT, valueTrue, t)
  4480. }
  4481. func TestClassNamedEval(t *testing.T) {
  4482. const SCRIPT = `
  4483. const C = class {
  4484. }
  4485. C.name === "C";
  4486. `
  4487. testScript(SCRIPT, valueTrue, t)
  4488. }
  4489. func TestClassNonDerived(t *testing.T) {
  4490. const SCRIPT = `
  4491. function initF() {
  4492. }
  4493. class C {
  4494. f = initF()
  4495. }
  4496. let c = new C();
  4497. `
  4498. testScript(SCRIPT, _undefined, t)
  4499. }
  4500. func TestClassExpr(t *testing.T) {
  4501. const SCRIPT = `
  4502. typeof Object.getOwnPropertyDescriptor(class {get f() {}}.prototype, "f").get === "function";
  4503. `
  4504. testScript(SCRIPT, valueTrue, t)
  4505. }
  4506. func TestClassSuperInHeritage(t *testing.T) {
  4507. const SCRIPT = `
  4508. class P {
  4509. a() {
  4510. return Error;
  4511. }
  4512. }
  4513. class C extends P {
  4514. m() {
  4515. class Inner extends super.a() {
  4516. }
  4517. return new Inner();
  4518. }
  4519. }
  4520. new C().m() instanceof Error;
  4521. `
  4522. testScript(SCRIPT, valueTrue, t)
  4523. }
  4524. func TestClassSuperInHeritageExpr(t *testing.T) {
  4525. const SCRIPT = `
  4526. class P {
  4527. a() {
  4528. return Error;
  4529. }
  4530. }
  4531. class C extends P {
  4532. m() {
  4533. function f(cls) {
  4534. return new cls();
  4535. }
  4536. return f(class Inner extends super.a() {
  4537. })
  4538. }
  4539. }
  4540. new C().m() instanceof Error;
  4541. `
  4542. testScript(SCRIPT, valueTrue, t)
  4543. }
  4544. func TestClassReferToBinding(t *testing.T) {
  4545. const SCRIPT = `
  4546. const CC = class C {
  4547. static T = 40
  4548. f = C.T + 2
  4549. }
  4550. let c = new CC();
  4551. c.f;
  4552. `
  4553. testScript(SCRIPT, intToValue(42), t)
  4554. }
  4555. func TestClassReferToBindingInStaticDecl(t *testing.T) {
  4556. const SCRIPT = `
  4557. class C {
  4558. static T = C.name
  4559. }
  4560. C.T;
  4561. `
  4562. testScript(SCRIPT, asciiString("C"), t)
  4563. }
  4564. func TestClassReferToBindingInStaticEval(t *testing.T) {
  4565. const SCRIPT = `
  4566. const CC = class C {
  4567. static T = eval("C.name")
  4568. }
  4569. CC.T;
  4570. `
  4571. testScript(SCRIPT, asciiString("C"), t)
  4572. }
  4573. func TestClassReferToBindingFromHeritage(t *testing.T) {
  4574. const SCRIPT = `
  4575. assert.throws(ReferenceError, () => {
  4576. class C extends C {
  4577. }
  4578. });
  4579. `
  4580. testScriptWithTestLib(SCRIPT, _undefined, t)
  4581. }
  4582. func TestClassCaptureSuperCallInArrowFunc(t *testing.T) {
  4583. const SCRIPT = `
  4584. let f;
  4585. class C extends class {} {
  4586. constructor() {
  4587. f = () => super();
  4588. f();
  4589. }
  4590. }
  4591. let c = new C();
  4592. `
  4593. testScript(SCRIPT, _undefined, t)
  4594. }
  4595. func TestClassCaptureSuperCallInNestedArrowFunc(t *testing.T) {
  4596. const SCRIPT = `
  4597. let f;
  4598. class P {
  4599. }
  4600. class C extends P {
  4601. constructor() {
  4602. f = () => () => super();
  4603. f()();
  4604. }
  4605. }
  4606. new C() instanceof P;
  4607. `
  4608. testScript(SCRIPT, valueTrue, t)
  4609. }
  4610. func TestThisInEval(t *testing.T) {
  4611. const SCRIPT = `
  4612. assert.sameValue(eval("this"), this, "global");
  4613. let o = {
  4614. f() {
  4615. return eval("this");
  4616. }
  4617. }
  4618. assert.sameValue(o.f(), o, "obj literal");
  4619. `
  4620. testScriptWithTestLib(SCRIPT, _undefined, t)
  4621. }
  4622. func TestStaticAsBindingTarget(t *testing.T) {
  4623. const SCRIPT = `
  4624. let [static] = [];
  4625. `
  4626. testScript(SCRIPT, _undefined, t)
  4627. }
  4628. func TestEvalInStaticFieldInit(t *testing.T) {
  4629. const SCRIPT = `
  4630. var C = class {
  4631. static f = 'test';
  4632. static g = this.f + '262';
  4633. static h = eval('this.g') + 'test';
  4634. }
  4635. C.f === "test" && C.g === "test262" && C.h === "test262test";
  4636. `
  4637. testScript(SCRIPT, valueTrue, t)
  4638. }
  4639. func TestClassPrivateElemInEval(t *testing.T) {
  4640. const SCRIPT = `
  4641. let f1, f2;
  4642. class C extends (f1 = o => eval("o.#a"), Object) {
  4643. static #a = 42;
  4644. static {
  4645. f2 = o => eval("o.#a");
  4646. assert.sameValue(C.#a, 42);
  4647. assert.sameValue((() => C.#a)(), 42);
  4648. }
  4649. }
  4650. assert.throws(SyntaxError, () => f1(C));
  4651. assert.sameValue(f2(C), 42);
  4652. `
  4653. testScriptWithTestLib(SCRIPT, _undefined, t)
  4654. }
  4655. func TestClassPrivateElemInIndirectEval(t *testing.T) {
  4656. const SCRIPT = `
  4657. let f1, f2;
  4658. class C extends (f1 = o => (0, eval)("o.#a"), Object) {
  4659. static #a = 42;
  4660. static {
  4661. f2 = o => (0, eval)("o.#a");
  4662. assert.throws(SyntaxError, () => (0, eval)("C.#a"));
  4663. }
  4664. }
  4665. assert.throws(SyntaxError, () => f1(C));
  4666. assert.throws(SyntaxError, () => f2(C));
  4667. `
  4668. testScriptWithTestLib(SCRIPT, _undefined, t)
  4669. }
  4670. func TestClassPrivateElemInFunction(t *testing.T) {
  4671. const SCRIPT = `
  4672. assert.throws(SyntaxError, () => {
  4673. class C {
  4674. static #a = 42;
  4675. static {
  4676. Function("o", "return o.#a");
  4677. }
  4678. }
  4679. });
  4680. `
  4681. testScriptWithTestLib(SCRIPT, _undefined, t)
  4682. }
  4683. func TestClassPrivateElementsDecl(t *testing.T) {
  4684. const SCRIPT = `
  4685. class C {
  4686. #a = 42;
  4687. get #b() {}
  4688. set #b(_) {}
  4689. get c() {
  4690. return this.#a;
  4691. }
  4692. #m() {
  4693. return this.#a;
  4694. }
  4695. static getter(inst) {
  4696. return inst.#m();
  4697. }
  4698. }
  4699. let c = new C();
  4700. c.c + C.getter(c);
  4701. `
  4702. testScript(SCRIPT, intToValue(84), t)
  4703. }
  4704. func TestPrivateIn(t *testing.T) {
  4705. const SCRIPT = `
  4706. class C {
  4707. #a = 42;
  4708. static check(inst) {
  4709. return #a in inst;
  4710. }
  4711. }
  4712. let c = new C();
  4713. C.check(c);
  4714. `
  4715. testScript(SCRIPT, valueTrue, t)
  4716. }
  4717. func TestDeletePropOfNonObject(t *testing.T) {
  4718. const SCRIPT = `
  4719. delete 'Test262'[100] && delete 'Test262'.a && delete 'Test262'['@'];
  4720. `
  4721. testScript(SCRIPT, valueTrue, t)
  4722. }
  4723. func TestKeywordsAsLabels(t *testing.T) {
  4724. const SCRIPT = `
  4725. let: for (let i = 0; i < 2; i++) {
  4726. if (i === 0) continue let;
  4727. break let;
  4728. }
  4729. \u006Cet: for (let i = 0; i < 2; i++) {
  4730. if (i === 0) continue \u006Cet;
  4731. break \u006Cet;
  4732. }
  4733. yield: for (let i = 0; i < 2; i++) {
  4734. if (i === 0) continue yield;
  4735. break yield;
  4736. }
  4737. yi\u0065ld: for (let i = 0; i < 2; i++) {
  4738. if (i === 0) continue yi\u0065ld;
  4739. break yi\u0065ld;
  4740. }
  4741. `
  4742. testScript(SCRIPT, _undefined, t)
  4743. }
  4744. func TestThisResolutionWithArg(t *testing.T) {
  4745. const SCRIPT = `
  4746. let capture;
  4747. function f(arg) {
  4748. capture = () => this; // move 'this' to stash
  4749. return [this, arg];
  4750. }
  4751. const _this = {};
  4752. const arg = {};
  4753. const [_this1, arg1] = f.call(_this, arg);
  4754. _this1 === _this && arg1 === arg;
  4755. `
  4756. testScript(SCRIPT, valueTrue, t)
  4757. }
  4758. func TestThisResolutionArgInStash(t *testing.T) {
  4759. const SCRIPT = `
  4760. let capture;
  4761. function f(arg) {
  4762. capture = () => this + arg; // move 'this' and arguments to stash
  4763. return [this, arg];
  4764. }
  4765. const _this = {};
  4766. const arg = {};
  4767. const [_this1, arg1] = f.call(_this, arg);
  4768. _this1 === _this && arg1 === arg;
  4769. `
  4770. testScript(SCRIPT, valueTrue, t)
  4771. }
  4772. func TestThisResolutionWithStackVar(t *testing.T) {
  4773. const SCRIPT = `
  4774. let capture;
  4775. function f(arg) {
  4776. const _ = 1; // a stack variable
  4777. capture = () => this + arg; // move 'this' and arguments to stash
  4778. return [this, arg];
  4779. }
  4780. const _this = {};
  4781. const arg = {};
  4782. const [_this1, arg1] = f.call(_this, arg);
  4783. _this1 === _this && arg1 === arg;
  4784. `
  4785. testScript(SCRIPT, valueTrue, t)
  4786. }
  4787. /*
  4788. func TestBabel(t *testing.T) {
  4789. src, err := ioutil.ReadFile("babel7.js")
  4790. if err != nil {
  4791. t.Fatal(err)
  4792. }
  4793. vm := New()
  4794. _, err = vm.RunString(string(src))
  4795. if err != nil {
  4796. t.Fatal(err)
  4797. }
  4798. _, err = vm.RunString(`var result = Babel.transform("", {presets: ["es2015"]});`)
  4799. if err != nil {
  4800. t.Fatal(err)
  4801. }
  4802. }*/
  4803. func BenchmarkCompile(b *testing.B) {
  4804. data, err := ioutil.ReadFile("testdata/S15.10.2.12_A1_T1.js")
  4805. if err != nil {
  4806. b.Fatal(err)
  4807. }
  4808. src := string(data)
  4809. for i := 0; i < b.N; i++ {
  4810. _, err := Compile("test.js", src, false)
  4811. if err != nil {
  4812. b.Fatal(err)
  4813. }
  4814. }
  4815. }