compiler_test.go 96 KB

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