compiler_test.go 98 KB

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