compiler_test.go 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891
  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 TestTryResultEmpty(t *testing.T) {
  2651. const SCRIPT = `
  2652. 1; try { } finally { }
  2653. `
  2654. testScript(SCRIPT, _undefined, t)
  2655. }
  2656. func TestTryResultEmptyCatch(t *testing.T) {
  2657. const SCRIPT = `
  2658. 1; try { throw null } catch(e) { }
  2659. `
  2660. testScript(SCRIPT, _undefined, t)
  2661. }
  2662. func TestTryResultEmptyContinueLoop(t *testing.T) {
  2663. const SCRIPT = `
  2664. for (var i = 0; i < 2; i++) { try {throw null;} catch(e) {continue;} 'bad'}
  2665. `
  2666. testScript(SCRIPT, _undefined, t)
  2667. }
  2668. func TestTryEmptyCatchStackLeak(t *testing.T) {
  2669. const SCRIPT = `
  2670. (function() {
  2671. var f;
  2672. // Make sure the outer function is not stashless.
  2673. (function() {
  2674. f++;
  2675. })();
  2676. try {
  2677. throw new Error();
  2678. } catch(e) {}
  2679. })();
  2680. `
  2681. testScript(SCRIPT, _undefined, t)
  2682. }
  2683. func TestTryThrowEmptyCatch(t *testing.T) {
  2684. const SCRIPT = `
  2685. try {
  2686. throw new Error();
  2687. }
  2688. catch (e) {}
  2689. `
  2690. testScript(SCRIPT, _undefined, t)
  2691. }
  2692. func TestFalsyLoopBreak(t *testing.T) {
  2693. const SCRIPT = `
  2694. while(false) {
  2695. break;
  2696. }
  2697. for(;false;) {
  2698. break;
  2699. }
  2700. undefined;
  2701. `
  2702. MustCompile("", SCRIPT, false)
  2703. }
  2704. func TestFalsyLoopBreakWithResult(t *testing.T) {
  2705. const SCRIPT = `
  2706. while(false) {
  2707. break;
  2708. }
  2709. `
  2710. testScript(SCRIPT, _undefined, t)
  2711. }
  2712. func TestDummyCompile(t *testing.T) {
  2713. const SCRIPT = `
  2714. 'use strict';
  2715. for (;false;) {
  2716. eval = 1;
  2717. }
  2718. `
  2719. _, err := Compile("", SCRIPT, false)
  2720. if err == nil {
  2721. t.Fatal("expected error")
  2722. }
  2723. }
  2724. func TestDummyCompileForUpdate(t *testing.T) {
  2725. const SCRIPT = `
  2726. 'use strict';
  2727. for (;false;eval=1) {
  2728. }
  2729. `
  2730. _, err := Compile("", SCRIPT, false)
  2731. if err == nil {
  2732. t.Fatal("expected error")
  2733. }
  2734. }
  2735. func TestObjectLiteralWithNumericKeys(t *testing.T) {
  2736. const SCRIPT = `
  2737. var o = {1e3: true};
  2738. var keys = Object.keys(o);
  2739. var o1 = {get 1e3() {return true;}};
  2740. var keys1 = Object.keys(o1);
  2741. var o2 = {1e21: true};
  2742. var keys2 = Object.keys(o2);
  2743. let o3 = {0(){return true}};
  2744. keys.length === 1 && keys[0] === "1000" &&
  2745. keys1.length === 1 && keys1[0] === "1000" && o1[1e3] === true &&
  2746. keys2.length === 1 && keys2[0] === "1e+21" && o3[0]();
  2747. `
  2748. testScript(SCRIPT, valueTrue, t)
  2749. }
  2750. func TestEscapedObjectPropertyKeys(t *testing.T) {
  2751. const SCRIPT = `
  2752. var obj = {
  2753. w\u0069th: 42
  2754. };
  2755. var obj = {
  2756. with() {42}
  2757. };
  2758. `
  2759. _, err := Compile("", SCRIPT, false)
  2760. if err != nil {
  2761. t.Fatal(err)
  2762. }
  2763. }
  2764. func TestEscapedKeywords(t *testing.T) {
  2765. const SCRIPT = `r\u0065turn;`
  2766. _, err := Compile("", SCRIPT, false)
  2767. if err == nil {
  2768. t.Fatal("Expected error")
  2769. }
  2770. }
  2771. func TestEscapedLet(t *testing.T) {
  2772. const SCRIPT = `
  2773. this.let = 0;
  2774. l\u0065t // ASI
  2775. a;
  2776. // If the parser treated the previous escaped "let" as a lexical declaration,
  2777. // this variable declaration will result an early syntax error.
  2778. var a;
  2779. `
  2780. _, err := Compile("", SCRIPT, false)
  2781. if err != nil {
  2782. t.Fatal(err)
  2783. }
  2784. }
  2785. func TestObjectLiteralFuncProps(t *testing.T) {
  2786. const SCRIPT = `
  2787. (function() {
  2788. 'use strict';
  2789. var o = {
  2790. eval: function() {return 1;},
  2791. arguments() {return 2;},
  2792. test: function test1() {}
  2793. }
  2794. assert.sameValue(o.eval.name, "eval");
  2795. assert.sameValue(o.arguments.name, "arguments");
  2796. assert.sameValue(o.eval(), 1);
  2797. assert.sameValue(o.arguments(), 2);
  2798. assert.sameValue(o.test.name, "test1");
  2799. })();
  2800. `
  2801. testScriptWithTestLib(SCRIPT, _undefined, t)
  2802. }
  2803. func TestFuncName(t *testing.T) {
  2804. const SCRIPT = `
  2805. var method = 1;
  2806. var o = {
  2807. method: function() {
  2808. return method;
  2809. },
  2810. method1: function method() {
  2811. return method;
  2812. }
  2813. }
  2814. o.method() === 1 && o.method1() === o.method1;
  2815. `
  2816. testScript(SCRIPT, valueTrue, t)
  2817. }
  2818. func TestFuncNameAssign(t *testing.T) {
  2819. const SCRIPT = `
  2820. var f = function() {};
  2821. var f1;
  2822. f1 = function() {};
  2823. let f2 = function() {};
  2824. f.name === "f" && f1.name === "f1" && f2.name === "f2";
  2825. `
  2826. testScript(SCRIPT, valueTrue, t)
  2827. }
  2828. func TestLexicalDeclGlobal(t *testing.T) {
  2829. const SCRIPT = `
  2830. if (true) {
  2831. let it = "be";
  2832. if (it !== "be") {
  2833. throw new Error(it);
  2834. }
  2835. }
  2836. let thrown = false;
  2837. try {
  2838. it;
  2839. } catch(e) {
  2840. if (e instanceof ReferenceError) {
  2841. thrown = true;
  2842. }
  2843. }
  2844. thrown;
  2845. `
  2846. testScript(SCRIPT, valueTrue, t)
  2847. }
  2848. func TestLexicalDeclFunction(t *testing.T) {
  2849. const SCRIPT = `
  2850. function f() {
  2851. if (true) {
  2852. let it = "be";
  2853. if (it !== "be") {
  2854. throw new Error(it);
  2855. }
  2856. }
  2857. let thrown = false;
  2858. try {
  2859. it;
  2860. } catch(e) {
  2861. if (e instanceof ReferenceError) {
  2862. thrown = true;
  2863. }
  2864. }
  2865. return thrown;
  2866. }
  2867. f();
  2868. `
  2869. testScript(SCRIPT, valueTrue, t)
  2870. }
  2871. func TestLexicalDynamicScope(t *testing.T) {
  2872. const SCRIPT = `
  2873. const global = 1;
  2874. function f() {
  2875. const func = global + 1;
  2876. function inner() {
  2877. function assertThrows(fn) {
  2878. let thrown = false;
  2879. try {
  2880. fn();
  2881. } catch (e) {
  2882. if (e instanceof TypeError) {
  2883. thrown = true;
  2884. } else {
  2885. throw e;
  2886. }
  2887. }
  2888. if (!thrown) {
  2889. throw new Error("Did not throw");
  2890. }
  2891. }
  2892. assertThrows(function() {
  2893. func++;
  2894. });
  2895. assertThrows(function() {
  2896. global++;
  2897. });
  2898. assertThrows(function() {
  2899. eval("func++");
  2900. });
  2901. assertThrows(function() {
  2902. eval("global++");
  2903. });
  2904. return eval("func + 1");
  2905. }
  2906. return inner();
  2907. }
  2908. f();
  2909. `
  2910. testScript(SCRIPT, valueInt(3), t)
  2911. }
  2912. func TestLexicalDynamicScope1(t *testing.T) {
  2913. const SCRIPT = `
  2914. (function() {
  2915. const x = 1 * 4;
  2916. return (function() {
  2917. eval("");
  2918. return x;
  2919. })();
  2920. })();
  2921. `
  2922. testScript(SCRIPT, intToValue(4), t)
  2923. }
  2924. func TestLexicalDynamicScope2(t *testing.T) {
  2925. const SCRIPT = `
  2926. (function() {
  2927. const x = 1 + 3;
  2928. var y = 2 * 2;
  2929. eval("");
  2930. return x;
  2931. })();
  2932. `
  2933. testScript(SCRIPT, intToValue(4), t)
  2934. }
  2935. func TestNonStrictLet(t *testing.T) {
  2936. const SCRIPT = `
  2937. var let = 1;
  2938. `
  2939. testScript(SCRIPT, _undefined, t)
  2940. }
  2941. func TestStrictLet(t *testing.T) {
  2942. const SCRIPT = `
  2943. var let = 1;
  2944. `
  2945. _, err := Compile("", SCRIPT, true)
  2946. if err == nil {
  2947. t.Fatal("Expected an error")
  2948. }
  2949. }
  2950. func TestLetLet(t *testing.T) {
  2951. const SCRIPT = `
  2952. let let = 1;
  2953. `
  2954. _, err := Compile("", SCRIPT, false)
  2955. if err == nil {
  2956. t.Fatal("Expected an error")
  2957. }
  2958. }
  2959. func TestLetASI(t *testing.T) {
  2960. const SCRIPT = `
  2961. while (false) let // ASI
  2962. x = 1;
  2963. `
  2964. _, err := Compile("", SCRIPT, false)
  2965. if err != nil {
  2966. t.Fatal(err)
  2967. }
  2968. }
  2969. func TestLetASI1(t *testing.T) {
  2970. const SCRIPT = `
  2971. let
  2972. x = 1;
  2973. `
  2974. _, err := Compile("", SCRIPT, true)
  2975. if err != nil {
  2976. t.Fatal(err)
  2977. }
  2978. }
  2979. func TestLetNoASI(t *testing.T) {
  2980. const SCRIPT = `
  2981. function f() {}let
  2982. x = 1;
  2983. `
  2984. _, err := Compile("", SCRIPT, true)
  2985. if err != nil {
  2986. t.Fatal(err)
  2987. }
  2988. }
  2989. func TestLetNoASI1(t *testing.T) {
  2990. const SCRIPT = `
  2991. let
  2992. let = 1;
  2993. `
  2994. _, err := Compile("", SCRIPT, false)
  2995. if err == nil {
  2996. t.Fatal("Expected error")
  2997. }
  2998. }
  2999. func TestLetArrayWithNewline(t *testing.T) {
  3000. const SCRIPT = `
  3001. with ({}) let
  3002. [a] = 0;
  3003. `
  3004. _, err := Compile("", SCRIPT, false)
  3005. if err == nil {
  3006. t.Fatal("Expected error")
  3007. }
  3008. }
  3009. func TestDynamicUninitedVarAccess(t *testing.T) {
  3010. const SCRIPT = `
  3011. function f() {
  3012. var x;
  3013. return eval("x");
  3014. }
  3015. f();
  3016. `
  3017. testScript(SCRIPT, _undefined, t)
  3018. }
  3019. func TestLexicalForLoopNoClosure(t *testing.T) {
  3020. const SCRIPT = `
  3021. let sum = 0;
  3022. for (let i = 0; i < 3; i++) {
  3023. sum += i;
  3024. }
  3025. sum;
  3026. `
  3027. testScript(SCRIPT, valueInt(3), t)
  3028. }
  3029. func TestLexicalForLoopClosure(t *testing.T) {
  3030. const SCRIPT = `
  3031. var f = [];
  3032. for (let i = 0; i < 3; i++) {
  3033. f.push(function() {
  3034. return i;
  3035. });
  3036. }
  3037. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  3038. `
  3039. testScript(SCRIPT, valueTrue, t)
  3040. }
  3041. func TestLexicalForLoopClosureInNext(t *testing.T) {
  3042. const SCRIPT = `
  3043. const a = [];
  3044. for (let i = 0; i < 5; a.push(function () { return i; }), ++i) { }
  3045. let res = "";
  3046. for (let k = 0; k < 5; ++k) {
  3047. res += ""+a[k]();
  3048. }
  3049. res;
  3050. `
  3051. testScript(SCRIPT, asciiString("12345"), t)
  3052. }
  3053. func TestVarForLoop(t *testing.T) {
  3054. const SCRIPT = `
  3055. var f = [];
  3056. for (var i = 0, j = 0; i < 3; i++) {
  3057. f.push(function() {
  3058. return i;
  3059. });
  3060. }
  3061. f.length === 3 && f[0]() === 3 && f[1]() === 3 && f[2]() === 3;
  3062. `
  3063. testScript(SCRIPT, valueTrue, t)
  3064. }
  3065. func TestLexicalForOfLoop(t *testing.T) {
  3066. const SCRIPT = `
  3067. var f = [];
  3068. for (let i of [0, 1, 2]) {
  3069. f.push(function() {
  3070. return i;
  3071. });
  3072. }
  3073. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  3074. `
  3075. testScript(SCRIPT, valueTrue, t)
  3076. }
  3077. func TestLexicalForOfLoopContBreak(t *testing.T) {
  3078. const SCRIPT = `
  3079. const f = [];
  3080. for (let i of [0, 1, 2, 3, 4, 5]) {
  3081. if (i % 2) continue;
  3082. f.push(function() {
  3083. return i;
  3084. });
  3085. if (i > 2) break;
  3086. }
  3087. let res = "";
  3088. f.forEach(function(item) {res += item()});
  3089. f.length === 3 && res === "024";
  3090. `
  3091. testScript(SCRIPT, valueTrue, t)
  3092. }
  3093. func TestVarBlockConflict(t *testing.T) {
  3094. const SCRIPT = `
  3095. let x;
  3096. {
  3097. if (false) {
  3098. var x;
  3099. }
  3100. }
  3101. `
  3102. _, err := Compile("", SCRIPT, false)
  3103. if err == nil {
  3104. t.Fatal("Expected an error")
  3105. }
  3106. }
  3107. func TestVarBlockConflictEval(t *testing.T) {
  3108. const SCRIPT = `
  3109. assert.throws(SyntaxError, function() {
  3110. let x;
  3111. {
  3112. if (true) {
  3113. eval("var x");
  3114. }
  3115. }
  3116. });
  3117. `
  3118. testScriptWithTestLib(SCRIPT, _undefined, t)
  3119. }
  3120. func TestVarBlockNoConflict(t *testing.T) {
  3121. const SCRIPT = `
  3122. function f() {
  3123. let x;
  3124. function ff() {
  3125. {
  3126. var x = 3;
  3127. }
  3128. }
  3129. ff();
  3130. }
  3131. f();
  3132. `
  3133. testScript(SCRIPT, _undefined, t)
  3134. }
  3135. func TestVarBlockNoConflictEval(t *testing.T) {
  3136. const SCRIPT = `
  3137. function f() {
  3138. let x;
  3139. function ff() {
  3140. {
  3141. eval("var x = 3");
  3142. }
  3143. }
  3144. ff();
  3145. }
  3146. f();
  3147. `
  3148. testScript(SCRIPT, _undefined, t)
  3149. }
  3150. func TestVarDeclCorrectScope(t *testing.T) {
  3151. const SCRIPT = `
  3152. function f() {
  3153. {
  3154. let z;
  3155. eval("var x = 3");
  3156. }
  3157. return x;
  3158. }
  3159. f();
  3160. `
  3161. testScript(SCRIPT, valueInt(3), t)
  3162. }
  3163. func TestLexicalCatch(t *testing.T) {
  3164. const SCRIPT = `
  3165. try {
  3166. throw null;
  3167. } catch (e) {
  3168. let x = 1;
  3169. function f() {}
  3170. e;
  3171. }
  3172. `
  3173. testScript(SCRIPT, _null, t)
  3174. }
  3175. func TestArgumentsLexicalDecl(t *testing.T) {
  3176. const SCRIPT = `
  3177. function f1() {
  3178. let arguments;
  3179. return arguments;
  3180. }
  3181. f1(42);
  3182. `
  3183. testScript(SCRIPT, _undefined, t)
  3184. }
  3185. func TestArgumentsLexicalDeclAssign(t *testing.T) {
  3186. const SCRIPT = `
  3187. function f1() {
  3188. let arguments = arguments;
  3189. return a;
  3190. }
  3191. assert.throws(ReferenceError, function() {
  3192. f1(42);
  3193. });
  3194. `
  3195. testScriptWithTestLib(SCRIPT, _undefined, t)
  3196. }
  3197. func TestLexicalConstModifyFromEval(t *testing.T) {
  3198. const SCRIPT = `
  3199. const x = 1;
  3200. function f() {
  3201. eval("x = 2");
  3202. }
  3203. assert.throws(TypeError, function() {
  3204. f();
  3205. });
  3206. `
  3207. testScriptWithTestLib(SCRIPT, _undefined, t)
  3208. }
  3209. func TestLexicalStrictNames(t *testing.T) {
  3210. const SCRIPT = `let eval = 1;`
  3211. _, err := Compile("", SCRIPT, true)
  3212. if err == nil {
  3213. t.Fatal("Expected an error")
  3214. }
  3215. }
  3216. func TestAssignAfterStackExpand(t *testing.T) {
  3217. // make sure the reference to the variable x does not remain stale after the stack is copied
  3218. const SCRIPT = `
  3219. function f() {
  3220. let sum = 0;
  3221. for (let i = 0; i < arguments.length; i++) {
  3222. sum += arguments[i];
  3223. }
  3224. return sum;
  3225. }
  3226. function testAssignment() {
  3227. var x = 0;
  3228. var scope = {};
  3229. with (scope) {
  3230. x = (scope.x = f(0, 0, 0, 0, 0, 0, 1, 1), 1);
  3231. }
  3232. if (scope.x !== 2) {
  3233. throw new Error('#1: scope.x === 2. Actual: ' + (scope.x));
  3234. }
  3235. if (x !== 1) {
  3236. throw new Error('#2: x === 1. Actual: ' + (x));
  3237. }
  3238. }
  3239. testAssignment();
  3240. `
  3241. testScript(SCRIPT, _undefined, t)
  3242. }
  3243. func TestArgAccessFromDynamicStash(t *testing.T) {
  3244. const SCRIPT = `
  3245. function f(arg) {
  3246. function test() {
  3247. eval("");
  3248. return a;
  3249. }
  3250. return arg;
  3251. }
  3252. f(true);
  3253. `
  3254. testScript(SCRIPT, valueTrue, t)
  3255. }
  3256. func TestLoadMixedLex(t *testing.T) {
  3257. const SCRIPT = `
  3258. function f() {
  3259. let a = 1;
  3260. {
  3261. function inner() {
  3262. eval("var a = true");
  3263. return a;
  3264. }
  3265. return inner();
  3266. }
  3267. }
  3268. f();
  3269. `
  3270. testScript(SCRIPT, valueTrue, t)
  3271. }
  3272. func TestObjectLiteralSpread(t *testing.T) {
  3273. const SCRIPT = `
  3274. let src = {prop1: 1};
  3275. Object.defineProperty(src, "prop2", {value: 2, configurable: true});
  3276. Object.defineProperty(src, "prop3", {value: 3, enumerable: true, configurable: true});
  3277. let target = {prop4: 4, ...src};
  3278. assert(deepEqual(target, {prop1: 1, prop3: 3, prop4: 4}));
  3279. `
  3280. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3281. }
  3282. func TestArrayLiteralSpread(t *testing.T) {
  3283. const SCRIPT = `
  3284. let a1 = [1, 2];
  3285. let a2 = [3, 4];
  3286. let a = [...a1, 0, ...a2, 1];
  3287. assert(compareArray(a, [1, 2, 0, 3, 4, 1]));
  3288. `
  3289. testScriptWithTestLib(SCRIPT, _undefined, t)
  3290. }
  3291. func TestObjectAssignmentPattern(t *testing.T) {
  3292. const SCRIPT = `
  3293. let a, b, c;
  3294. ({a, b, c=3} = {a: 1, b: 2});
  3295. assert.sameValue(a, 1, "a");
  3296. assert.sameValue(b, 2, "b");
  3297. assert.sameValue(c, 3, "c");
  3298. `
  3299. testScriptWithTestLib(SCRIPT, _undefined, t)
  3300. }
  3301. func TestObjectAssignmentPatternNoDyn(t *testing.T) {
  3302. const SCRIPT = `
  3303. (function() {
  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. `
  3311. testScriptWithTestLib(SCRIPT, _undefined, t)
  3312. }
  3313. func TestObjectAssignmentPatternNested(t *testing.T) {
  3314. const SCRIPT = `
  3315. let a, b, c, d;
  3316. ({a, b, c: {d} = 3} = {a: 1, b: 2, c: {d: 4}});
  3317. assert.sameValue(a, 1, "a");
  3318. assert.sameValue(b, 2, "b");
  3319. assert.sameValue(c, undefined, "c");
  3320. assert.sameValue(d, 4, "d");
  3321. `
  3322. testScriptWithTestLib(SCRIPT, _undefined, t)
  3323. }
  3324. func TestObjectAssignmentPatternEvalOrder(t *testing.T) {
  3325. const SCRIPT = `
  3326. let trace = "";
  3327. let target_obj = {};
  3328. function src() {
  3329. trace += "src(),";
  3330. return {
  3331. get a() {
  3332. trace += "get a,";
  3333. return "a";
  3334. }
  3335. }
  3336. }
  3337. function prop1() {
  3338. trace += "prop1(),"
  3339. return {
  3340. toString: function() {
  3341. trace += "prop1-to-string(),";
  3342. return "a";
  3343. }
  3344. }
  3345. }
  3346. function prop2() {
  3347. trace += "prop2(),";
  3348. return {
  3349. toString: function() {
  3350. trace += "prop2-to-string(),";
  3351. return "b";
  3352. }
  3353. }
  3354. }
  3355. function target() {
  3356. trace += "target(),"
  3357. return target_obj;
  3358. }
  3359. let a, b;
  3360. ({[prop1()]: target().a, [prop2()]: b} = src());
  3361. if (target_obj.a !== "a") {
  3362. throw new Error("target_obj.a="+target_obj.a);
  3363. }
  3364. trace;
  3365. `
  3366. testScript(SCRIPT, asciiString("src(),prop1(),prop1-to-string(),target(),get a,prop2(),prop2-to-string(),"), t)
  3367. }
  3368. func TestArrayAssignmentPatternEvalOrder(t *testing.T) {
  3369. const SCRIPT = `
  3370. let trace = "";
  3371. let src_arr = {
  3372. [Symbol.iterator]: function() {
  3373. let done = false;
  3374. return {
  3375. next: function() {
  3376. trace += "next,";
  3377. if (!done) {
  3378. done = true;
  3379. return {value: 0};
  3380. }
  3381. return {done: true};
  3382. },
  3383. return: function() {
  3384. trace += "return,";
  3385. }
  3386. }
  3387. }
  3388. }
  3389. function src() {
  3390. trace += "src(),";
  3391. return src_arr;
  3392. }
  3393. let tgt = {
  3394. get a() {
  3395. trace += "get a,";
  3396. return "a";
  3397. },
  3398. get b() {
  3399. trace += "get b,";
  3400. return "b";
  3401. }
  3402. }
  3403. function target() {
  3404. trace += "target(),";
  3405. return tgt;
  3406. }
  3407. function default_a() {
  3408. trace += "default a,";
  3409. return "def_a";
  3410. }
  3411. function default_b() {
  3412. trace += "default b,";
  3413. return "def_b";
  3414. }
  3415. ([target().a = default_a(), target().b = default_b()] = src());
  3416. trace;
  3417. `
  3418. testScript(SCRIPT, asciiString("src(),target(),next,target(),next,default b,"), t)
  3419. }
  3420. func TestObjectAssignPatternRest(t *testing.T) {
  3421. const SCRIPT = `
  3422. let a, b, c, d;
  3423. ({a, b, c, ...d} = {a: 1, b: 2, d: 4});
  3424. assert.sameValue(a, 1, "a");
  3425. assert.sameValue(b, 2, "b");
  3426. assert.sameValue(c, undefined, "c");
  3427. assert(deepEqual(d, {d: 4}), "d");
  3428. `
  3429. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3430. }
  3431. func TestObjectBindPattern(t *testing.T) {
  3432. const SCRIPT = `
  3433. let {a, b, c, ...d} = {a: 1, b: 2, d: 4};
  3434. assert.sameValue(a, 1, "a");
  3435. assert.sameValue(b, 2, "b");
  3436. assert.sameValue(c, undefined, "c");
  3437. assert(deepEqual(d, {d: 4}), "d");
  3438. var { x: y, } = { x: 23 };
  3439. assert.sameValue(y, 23);
  3440. assert.throws(ReferenceError, function() {
  3441. x;
  3442. });
  3443. `
  3444. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3445. }
  3446. func TestObjLiteralShorthandWithInitializer(t *testing.T) {
  3447. const SCRIPT = `
  3448. o = {a=1};
  3449. `
  3450. _, err := Compile("", SCRIPT, false)
  3451. if err == nil {
  3452. t.Fatal("Expected an error")
  3453. }
  3454. }
  3455. func TestObjLiteralShorthandLetStringLit(t *testing.T) {
  3456. const SCRIPT = `
  3457. o = {"let"};
  3458. `
  3459. _, err := Compile("", SCRIPT, false)
  3460. if err == nil {
  3461. t.Fatal("Expected an error")
  3462. }
  3463. }
  3464. func TestObjLiteralComputedKeys(t *testing.T) {
  3465. const SCRIPT = `
  3466. let o = {
  3467. get [Symbol.toString]() {
  3468. }
  3469. }
  3470. `
  3471. testScript(SCRIPT, _undefined, t)
  3472. }
  3473. func TestObjLiteralComputedKeysEvalOrder(t *testing.T) {
  3474. const SCRIPT = `
  3475. let trace = [];
  3476. function key() {
  3477. trace.push("key");
  3478. return {
  3479. toString: function() {
  3480. trace.push("key-toString");
  3481. return "key";
  3482. }
  3483. }
  3484. }
  3485. function val() {
  3486. trace.push("val");
  3487. return "val";
  3488. }
  3489. const _ = {
  3490. [key()]: val(),
  3491. }
  3492. trace.join(",");
  3493. `
  3494. testScript(SCRIPT, asciiString("key,key-toString,val"), t)
  3495. }
  3496. func TestArrayAssignPattern(t *testing.T) {
  3497. const SCRIPT = `
  3498. let a, b;
  3499. ([a, b] = [1, 2]);
  3500. a === 1 && b === 2;
  3501. `
  3502. testScript(SCRIPT, valueTrue, t)
  3503. }
  3504. func TestArrayAssignPattern1(t *testing.T) {
  3505. const SCRIPT = `
  3506. let a, b;
  3507. ([a = 3, b = 2] = [1]);
  3508. a === 1 && b === 2;
  3509. `
  3510. testScript(SCRIPT, valueTrue, t)
  3511. }
  3512. func TestArrayAssignPatternLHS(t *testing.T) {
  3513. const SCRIPT = `
  3514. let a = {};
  3515. [ a.b, a['c'] = 2 ] = [1];
  3516. a.b === 1 && a.c === 2;
  3517. `
  3518. testScript(SCRIPT, valueTrue, t)
  3519. }
  3520. func TestArrayAssignPatternElision(t *testing.T) {
  3521. const SCRIPT = `
  3522. let a, b;
  3523. ([a,, b] = [1, 4, 2]);
  3524. a === 1 && b === 2;
  3525. `
  3526. testScript(SCRIPT, valueTrue, t)
  3527. }
  3528. func TestArrayAssignPatternRestPattern(t *testing.T) {
  3529. const SCRIPT = `
  3530. let a, b, z;
  3531. [ z, ...[a, b] ] = [0, 1, 2];
  3532. z === 0 && a === 1 && b === 2;
  3533. `
  3534. testScript(SCRIPT, valueTrue, t)
  3535. }
  3536. func TestArrayBindingPattern(t *testing.T) {
  3537. const SCRIPT = `
  3538. let [a, b] = [1, 2];
  3539. a === 1 && b === 2;
  3540. `
  3541. testScript(SCRIPT, valueTrue, t)
  3542. }
  3543. func TestObjectPatternShorthandInit(t *testing.T) {
  3544. const SCRIPT = `
  3545. [...{ x = 1 }] = [];
  3546. x;
  3547. `
  3548. testScript(SCRIPT, valueInt(1), t)
  3549. }
  3550. func TestArrayBindingPatternRestPattern(t *testing.T) {
  3551. const SCRIPT = `
  3552. const [a, b, ...[c, d]] = [1, 2, 3, 4];
  3553. a === 1 && b === 2 && c === 3 && d === 4;
  3554. `
  3555. testScript(SCRIPT, valueTrue, t)
  3556. }
  3557. func TestForVarPattern(t *testing.T) {
  3558. const SCRIPT = `
  3559. var o = {a: 1};
  3560. var trace = "";
  3561. for (var [key, value] of Object.entries(o)) {
  3562. trace += key+":"+value;
  3563. }
  3564. trace;
  3565. `
  3566. testScript(SCRIPT, asciiString("a:1"), t)
  3567. }
  3568. func TestForLexPattern(t *testing.T) {
  3569. const SCRIPT = `
  3570. var o = {a: 1};
  3571. var trace = "";
  3572. for (const [key, value] of Object.entries(o)) {
  3573. trace += key+":"+value;
  3574. }
  3575. trace;
  3576. `
  3577. testScript(SCRIPT, asciiString("a:1"), t)
  3578. }
  3579. func TestBindingPatternRestTrailingComma(t *testing.T) {
  3580. const SCRIPT = `
  3581. const [a, b, ...rest,] = [];
  3582. `
  3583. _, err := Compile("", SCRIPT, false)
  3584. if err == nil {
  3585. t.Fatal("Expected an error")
  3586. }
  3587. }
  3588. func TestAssignPatternRestTrailingComma(t *testing.T) {
  3589. const SCRIPT = `
  3590. ([a, b, ...rest,] = []);
  3591. `
  3592. _, err := Compile("", SCRIPT, false)
  3593. if err == nil {
  3594. t.Fatal("Expected an error")
  3595. }
  3596. }
  3597. func TestFuncParamInitializerSimple(t *testing.T) {
  3598. const SCRIPT = `
  3599. function f(a = 1) {
  3600. return a;
  3601. }
  3602. ""+f()+f(2);
  3603. `
  3604. testScript(SCRIPT, asciiString("12"), t)
  3605. }
  3606. func TestFuncParamObjectPatternSimple(t *testing.T) {
  3607. const SCRIPT = `
  3608. function f({a, b} = {a: 1, b: 2}) {
  3609. return "" + a + b;
  3610. }
  3611. ""+f()+" "+f({a: 3, b: 4});
  3612. `
  3613. testScript(SCRIPT, asciiString("12 34"), t)
  3614. }
  3615. func TestFuncParamRestStackSimple(t *testing.T) {
  3616. const SCRIPT = `
  3617. function f(arg1, ...rest) {
  3618. return rest;
  3619. }
  3620. let ar = f(1, 2, 3);
  3621. ar.join(",");
  3622. `
  3623. testScript(SCRIPT, asciiString("2,3"), t)
  3624. }
  3625. func TestFuncParamRestStashSimple(t *testing.T) {
  3626. const SCRIPT = `
  3627. function f(arg1, ...rest) {
  3628. eval("true");
  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 TestRestArgsNotInStash(t *testing.T) {
  3637. const SCRIPT = `
  3638. function f(...rest) {
  3639. () => rest;
  3640. return rest.length;
  3641. }
  3642. f(1,2);
  3643. `
  3644. testScript(SCRIPT, valueInt(2), t)
  3645. }
  3646. func TestRestArgsInStash(t *testing.T) {
  3647. const SCRIPT = `
  3648. function f(first, ...rest) {
  3649. () => first;
  3650. () => rest;
  3651. return rest.length;
  3652. }
  3653. f(1,2);
  3654. `
  3655. testScript(SCRIPT, valueInt(1), t)
  3656. }
  3657. func TestRestArgsInStashFwdRef(t *testing.T) {
  3658. const SCRIPT = `
  3659. function f(first = eval(), ...rest) {
  3660. () => first;
  3661. () => rest;
  3662. return rest.length === 1 && rest[0] === 2;
  3663. }
  3664. f(1,2);
  3665. `
  3666. testScript(SCRIPT, valueTrue, t)
  3667. }
  3668. func TestFuncParamRestPattern(t *testing.T) {
  3669. const SCRIPT = `
  3670. function f(arg1, ...{0: rest1, 1: rest2}) {
  3671. return ""+arg1+" "+rest1+" "+rest2;
  3672. }
  3673. f(1, 2, 3);
  3674. `
  3675. testScript(SCRIPT, asciiString("1 2 3"), t)
  3676. }
  3677. func TestFuncParamForwardRef(t *testing.T) {
  3678. const SCRIPT = `
  3679. function f(a = b + 1, b) {
  3680. return ""+a+" "+b;
  3681. }
  3682. f(1, 2);
  3683. `
  3684. testScript(SCRIPT, asciiString("1 2"), t)
  3685. }
  3686. func TestFuncParamForwardRefMissing(t *testing.T) {
  3687. const SCRIPT = `
  3688. function f(a = b + 1, b) {
  3689. return ""+a+" "+b;
  3690. }
  3691. assert.throws(ReferenceError, function() {
  3692. f();
  3693. });
  3694. `
  3695. testScriptWithTestLib(SCRIPT, _undefined, t)
  3696. }
  3697. func TestFuncParamInnerRef(t *testing.T) {
  3698. const SCRIPT = `
  3699. function f(a = inner) {
  3700. var inner = 42;
  3701. return a;
  3702. }
  3703. assert.throws(ReferenceError, function() {
  3704. f();
  3705. });
  3706. `
  3707. testScriptWithTestLib(SCRIPT, _undefined, t)
  3708. }
  3709. func TestFuncParamInnerRefEval(t *testing.T) {
  3710. const SCRIPT = `
  3711. function f(a = eval("inner")) {
  3712. var inner = 42;
  3713. return a;
  3714. }
  3715. assert.throws(ReferenceError, function() {
  3716. f();
  3717. });
  3718. `
  3719. testScriptWithTestLib(SCRIPT, _undefined, t)
  3720. }
  3721. func TestFuncParamCalleeName(t *testing.T) {
  3722. const SCRIPT = `
  3723. function f(a = f) {
  3724. var f;
  3725. return f;
  3726. }
  3727. typeof f();
  3728. `
  3729. testScript(SCRIPT, asciiString("undefined"), t)
  3730. }
  3731. func TestFuncParamVarCopy(t *testing.T) {
  3732. const SCRIPT = `
  3733. function f(a = f) {
  3734. var a;
  3735. return a;
  3736. }
  3737. typeof f();
  3738. `
  3739. testScript(SCRIPT, asciiString("function"), t)
  3740. }
  3741. func TestFuncParamScope(t *testing.T) {
  3742. const SCRIPT = `
  3743. var x = 'outside';
  3744. var probe1, probe2;
  3745. function f(
  3746. _ = probe1 = function() { return x; },
  3747. __ = (eval('var x = "inside";'), probe2 = function() { return x; })
  3748. ) {
  3749. }
  3750. f();
  3751. probe1()+" "+probe2();
  3752. `
  3753. testScript(SCRIPT, asciiString("inside inside"), t)
  3754. }
  3755. func TestDefParamsStackPtr(t *testing.T) {
  3756. const SCRIPT = `
  3757. function A() {};
  3758. A.B = function () {};
  3759. function D(message = '') {
  3760. var C = A.B;
  3761. C([1,2,3]);
  3762. };
  3763. D();
  3764. `
  3765. testScript(SCRIPT, _undefined, t)
  3766. }
  3767. func TestNestedVariadicCalls(t *testing.T) {
  3768. const SCRIPT = `
  3769. function f() {
  3770. return Array.prototype.join.call(arguments, ",");
  3771. }
  3772. f(...[1], "a", f(...[2]));
  3773. `
  3774. testScript(SCRIPT, asciiString("1,a,2"), t)
  3775. }
  3776. func TestVariadicNew(t *testing.T) {
  3777. const SCRIPT = `
  3778. function C() {
  3779. this.res = Array.prototype.join.call(arguments, ",");
  3780. }
  3781. var c = new C(...[1], "a", new C(...[2]).res);
  3782. c.res;
  3783. `
  3784. testScript(SCRIPT, asciiString("1,a,2"), t)
  3785. }
  3786. func TestVariadicUseStackVars(t *testing.T) {
  3787. const SCRIPT = `
  3788. function A(message) { return message; }
  3789. function B(...args){
  3790. return A(...args);
  3791. }
  3792. B("C");
  3793. `
  3794. testScript(SCRIPT, asciiString("C"), t)
  3795. }
  3796. func TestCatchParamPattern(t *testing.T) {
  3797. const SCRIPT = `
  3798. function f() {
  3799. let x = 3;
  3800. try {
  3801. throw {a: 1, b: 2};
  3802. } catch ({a, b, c = x}) {
  3803. let x = 99;
  3804. return ""+a+" "+b+" "+c;
  3805. }
  3806. }
  3807. f();
  3808. `
  3809. testScript(SCRIPT, asciiString("1 2 3"), t)
  3810. }
  3811. func TestArrowUseStrict(t *testing.T) {
  3812. // simple parameter list -- ok
  3813. _, err := Compile("", "(a) => {'use strict';}", false)
  3814. if err != nil {
  3815. t.Fatal(err)
  3816. }
  3817. // non-simple parameter list -- syntax error
  3818. _, err = Compile("", "(a=0) => {'use strict';}", false)
  3819. if err == nil {
  3820. t.Fatal("expected error")
  3821. }
  3822. }
  3823. func TestArrowBoxedThis(t *testing.T) {
  3824. const SCRIPT = `
  3825. var context;
  3826. fn = function() {
  3827. return (arg) => { var local; context = this; };
  3828. };
  3829. fn()();
  3830. context === this;
  3831. `
  3832. testScript(SCRIPT, valueTrue, t)
  3833. }
  3834. func TestParameterOverride(t *testing.T) {
  3835. const SCRIPT = `
  3836. function f(arg) {
  3837. var arg = arg || "default"
  3838. return arg
  3839. }
  3840. f()
  3841. `
  3842. testScript(SCRIPT, asciiString("default"), t)
  3843. }
  3844. func TestEvalInIterScope(t *testing.T) {
  3845. const SCRIPT = `
  3846. for (let a = 0; a < 1; a++) {
  3847. eval("a");
  3848. }
  3849. `
  3850. testScript(SCRIPT, valueInt(0), t)
  3851. }
  3852. func TestTemplateLiterals(t *testing.T) {
  3853. vm := New()
  3854. _, err := vm.RunString("const a = 1, b = 'b';")
  3855. if err != nil {
  3856. t.Fatal(err)
  3857. }
  3858. f := func(t *testing.T, template, expected string) {
  3859. res, err := vm.RunString(template)
  3860. if err != nil {
  3861. t.Fatal(err)
  3862. }
  3863. if actual := res.Export(); actual != expected {
  3864. t.Fatalf("Expected: %q, actual: %q", expected, actual)
  3865. }
  3866. }
  3867. t.Run("empty", func(t *testing.T) {
  3868. f(t, "``", "")
  3869. })
  3870. t.Run("noSub", func(t *testing.T) {
  3871. f(t, "`test`", "test")
  3872. })
  3873. t.Run("emptyTail", func(t *testing.T) {
  3874. f(t, "`a=${a},b=${b}`", "a=1,b=b")
  3875. })
  3876. t.Run("emptyHead", func(t *testing.T) {
  3877. f(t, "`${a},b=${b}$`", "1,b=b$")
  3878. })
  3879. t.Run("headAndTail", func(t *testing.T) {
  3880. f(t, "`a=${a},b=${b}$`", "a=1,b=b$")
  3881. })
  3882. }
  3883. func TestTaggedTemplate(t *testing.T) {
  3884. const SCRIPT = `
  3885. let res;
  3886. const o = {
  3887. tmpl() {
  3888. res = this;
  3889. return () => {};
  3890. }
  3891. }
  3892. ` +
  3893. "o.tmpl()`test`;" + `
  3894. res === o;
  3895. `
  3896. testScript(SCRIPT, valueTrue, t)
  3897. }
  3898. func TestDuplicateGlobalFunc(t *testing.T) {
  3899. const SCRIPT = `
  3900. function a(){}
  3901. function b(){ return "b" }
  3902. function c(){ return "c" }
  3903. function a(){}
  3904. b();
  3905. `
  3906. testScript(SCRIPT, asciiString("b"), t)
  3907. }
  3908. func TestDuplicateFunc(t *testing.T) {
  3909. const SCRIPT = `
  3910. function f() {
  3911. function a(){}
  3912. function b(){ return "b" }
  3913. function c(){ return "c" }
  3914. function a(){}
  3915. return b();
  3916. }
  3917. f();
  3918. `
  3919. testScript(SCRIPT, asciiString("b"), t)
  3920. }
  3921. func TestSrcLocations(t *testing.T) {
  3922. // Do not reformat, assertions depend on the line and column numbers
  3923. const SCRIPT = `
  3924. let i = {
  3925. valueOf() {
  3926. throw new Error();
  3927. }
  3928. };
  3929. try {
  3930. i++;
  3931. } catch(e) {
  3932. assertStack(e, [["test.js", "valueOf", 4, 10],
  3933. ["test.js", "", 8, 3]
  3934. ]);
  3935. }
  3936. Object.defineProperty(globalThis, "x", {
  3937. get() {
  3938. throw new Error();
  3939. },
  3940. set() {
  3941. throw new Error();
  3942. }
  3943. });
  3944. try {
  3945. x;
  3946. } catch(e) {
  3947. assertStack(e, [["test.js", "get", 17, 10],
  3948. ["test.js", "", 25, 3]
  3949. ]);
  3950. }
  3951. try {
  3952. x++;
  3953. } catch(e) {
  3954. assertStack(e, [["test.js", "get", 17, 10],
  3955. ["test.js", "", 33, 3]
  3956. ]);
  3957. }
  3958. try {
  3959. x = 2;
  3960. } catch(e) {
  3961. assertStack(e, [["test.js", "set", 20, 10],
  3962. ["test.js", "", 41, 3]
  3963. ]);
  3964. }
  3965. try {
  3966. +i;
  3967. } catch(e) {
  3968. assertStack(e, [["test.js", "valueOf", 4, 10],
  3969. ["test.js", "", 49, 4]
  3970. ]);
  3971. }
  3972. try {
  3973. let n;
  3974. n.field = {
  3975. "key1": "test",
  3976. "key2": {},
  3977. }
  3978. } catch(e) {
  3979. assertStack(e, [["test.js", "", 58, 5]
  3980. ]);
  3981. }
  3982. `
  3983. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3984. }
  3985. func TestSrcLocationThrowLiteral(t *testing.T) {
  3986. vm := New()
  3987. _, err := vm.RunString(`
  3988. const z = 1;
  3989. throw "";
  3990. `)
  3991. if ex, ok := err.(*Exception); ok {
  3992. pos := ex.stack[0].Position()
  3993. if pos.Line != 3 {
  3994. t.Fatal(pos)
  3995. }
  3996. } else {
  3997. t.Fatal(err)
  3998. }
  3999. }
  4000. func TestSrcLocation(t *testing.T) {
  4001. prg := MustCompile("test.js", `
  4002. f();
  4003. var x = 1;
  4004. let y = 1;
  4005. let [z1, z2] = [0, 0];
  4006. var [z3, z4] = [0, 0];
  4007. `, false)
  4008. const (
  4009. varLine = 3
  4010. letLine = 4
  4011. dstrLetLine = 5
  4012. dstrVarLine = 7
  4013. )
  4014. linesOfInterest := map[int]string{
  4015. varLine: "var",
  4016. letLine: "let",
  4017. dstrLetLine: "destruct let",
  4018. dstrVarLine: "destruct var",
  4019. }
  4020. for i := range prg.code {
  4021. loc := prg.src.Position(prg.sourceOffset(i))
  4022. delete(linesOfInterest, loc.Line)
  4023. if len(linesOfInterest) == 0 {
  4024. break
  4025. }
  4026. }
  4027. for _, v := range linesOfInterest {
  4028. t.Fatalf("no %s line", v)
  4029. }
  4030. }
  4031. func TestBadObjectKey(t *testing.T) {
  4032. _, err := Compile("", "({!:0})", false)
  4033. if err == nil {
  4034. t.Fatal("expected error")
  4035. }
  4036. }
  4037. func TestConstantFolding(t *testing.T) {
  4038. testValues := func(prg *Program, result Value, t *testing.T) {
  4039. values := make(map[unistring.String]struct{})
  4040. for _, ins := range prg.code {
  4041. if lv, ok := ins.(loadVal); ok {
  4042. values[lv.v.string()] = struct{}{}
  4043. }
  4044. }
  4045. if len(values) != 1 {
  4046. prg.dumpCode(t.Logf)
  4047. t.Fatalf("values: %v", values)
  4048. }
  4049. }
  4050. f := func(src string, result Value, t *testing.T) {
  4051. prg := MustCompile("test.js", src, false)
  4052. testValues(prg, result, t)
  4053. New().testPrg(prg, result, t)
  4054. }
  4055. ff := func(src string, result Value, t *testing.T) {
  4056. prg := MustCompile("test.js", src, false)
  4057. fl := prg.code[0].(*newFunc)
  4058. testValues(fl.prg, result, t)
  4059. New().testPrg(prg, result, t)
  4060. }
  4061. t.Run("lexical binding", func(t *testing.T) {
  4062. f("const x = 1 + 2; x", valueInt(3), t)
  4063. })
  4064. t.Run("var binding", func(t *testing.T) {
  4065. f("var x = 1 + 2; x", valueInt(3), t)
  4066. })
  4067. t.Run("assignment", func(t *testing.T) {
  4068. f("x = 1 + 2; x", valueInt(3), t)
  4069. })
  4070. t.Run("object pattern", func(t *testing.T) {
  4071. f("const {x = 1 + 2} = {}; x", valueInt(3), t)
  4072. })
  4073. t.Run("array pattern", func(t *testing.T) {
  4074. f("const [x = 1 + 2] = []; x", valueInt(3), t)
  4075. })
  4076. t.Run("object literal", func(t *testing.T) {
  4077. f("var o = {x: 1 + 2}; o.x", valueInt(3), t)
  4078. })
  4079. t.Run("array literal", func(t *testing.T) {
  4080. f("var a = [3, 3, 3, 1 + 2]; a[3]", valueInt(3), t)
  4081. })
  4082. t.Run("default function parameter", func(t *testing.T) {
  4083. ff("function f(arg = 1 + 2) {return arg}; f()", valueInt(3), t)
  4084. })
  4085. t.Run("return", func(t *testing.T) {
  4086. ff("function f() {return 1 + 2}; f()", valueInt(3), t)
  4087. })
  4088. }
  4089. func TestStringInterning(t *testing.T) {
  4090. const SCRIPT = `
  4091. const str1 = "Test";
  4092. function f() {
  4093. return "Test";
  4094. }
  4095. [str1, f()];
  4096. `
  4097. vm := New()
  4098. res, err := vm.RunString(SCRIPT)
  4099. if err != nil {
  4100. t.Fatal(err)
  4101. }
  4102. str1 := res.(*Object).Get("0").String()
  4103. str2 := res.(*Object).Get("1").String()
  4104. if unsafe.StringData(str1) != unsafe.StringData(str2) {
  4105. t.Fatal("not interned")
  4106. }
  4107. }
  4108. func TestAssignBeforeInit(t *testing.T) {
  4109. const SCRIPT = `
  4110. assert.throws(ReferenceError, () => {
  4111. a = 1;
  4112. let a;
  4113. });
  4114. assert.throws(ReferenceError, () => {
  4115. ({a, b} = {a: 1, b: 2});
  4116. let a, b;
  4117. });
  4118. assert.throws(ReferenceError, () => {
  4119. (function() {
  4120. eval("");
  4121. ({a} = {a: 1});
  4122. })();
  4123. let a;
  4124. });
  4125. assert.throws(ReferenceError, () => {
  4126. const ctx = {x: 1};
  4127. function t() {
  4128. delete ctx.x;
  4129. return 42;
  4130. }
  4131. with(ctx) {
  4132. (function() {
  4133. 'use strict';
  4134. ({x} = {x: t()});
  4135. })();
  4136. }
  4137. return ctx.x;
  4138. });
  4139. assert.throws(ReferenceError, () => {
  4140. const ctx = {x: 1};
  4141. function t() {
  4142. delete ctx.x;
  4143. return 42;
  4144. }
  4145. with(ctx) {
  4146. (function() {
  4147. 'use strict';
  4148. const src = {};
  4149. Object.defineProperty(src, "x", {
  4150. get() {
  4151. return t();
  4152. }
  4153. });
  4154. ({x} = src);
  4155. })();
  4156. }
  4157. return ctx.x;
  4158. });
  4159. `
  4160. testScriptWithTestLib(SCRIPT, _undefined, t)
  4161. }
  4162. func TestOptChainCallee(t *testing.T) {
  4163. const SCRIPT = `
  4164. var a;
  4165. assert.sameValue(a?.(true), undefined);
  4166. a = null;
  4167. assert.sameValue(a?.(), undefined);
  4168. var o = {n: null};
  4169. assert.sameValue(o.m?.(true), undefined);
  4170. assert.sameValue(o.n?.(true), undefined);
  4171. `
  4172. testScriptWithTestLib(SCRIPT, _undefined, t)
  4173. }
  4174. func TestObjectLiteralSuper(t *testing.T) {
  4175. const SCRIPT = `
  4176. const proto = {
  4177. m() {
  4178. return 40;
  4179. }
  4180. }
  4181. const o = {
  4182. m() {
  4183. return super.m() + 2;
  4184. }
  4185. }
  4186. o.__proto__ = proto;
  4187. o.m();
  4188. `
  4189. testScript(SCRIPT, intToValue(42), t)
  4190. }
  4191. func TestClassCaptureThisInFieldInit(t *testing.T) {
  4192. const SCRIPT = `
  4193. let capture;
  4194. class C {
  4195. a = () => this
  4196. }
  4197. let c = new C();
  4198. c.a() === c;
  4199. `
  4200. testScript(SCRIPT, valueTrue, t)
  4201. }
  4202. func TestClassUseThisInFieldInit(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 TestClassCaptureThisInStaticFieldInit(t *testing.T) {
  4214. const SCRIPT = `
  4215. let capture;
  4216. class C {
  4217. static a = (capture = () => this, 0)
  4218. }
  4219. let c = new C();
  4220. capture() === C;
  4221. `
  4222. testScript(SCRIPT, valueTrue, t)
  4223. }
  4224. func TestClassDynCaptureThisInStaticFieldInit(t *testing.T) {
  4225. const SCRIPT = `
  4226. class C {
  4227. static a = eval("this")
  4228. }
  4229. C.a === C;
  4230. `
  4231. testScript(SCRIPT, valueTrue, t)
  4232. }
  4233. func TestCompileClass(t *testing.T) {
  4234. const SCRIPT = `
  4235. class C extends Error {
  4236. a = true;
  4237. b = 1;
  4238. ["b".toUpperCase()] = 2
  4239. static A = Math.random() < 1
  4240. constructor(message = "My Error") {
  4241. super(message);
  4242. }
  4243. static M() {
  4244. }
  4245. static M1() {
  4246. }
  4247. m() {
  4248. //return C.a;
  4249. }
  4250. m1() {
  4251. return true;
  4252. }
  4253. static {
  4254. this.supername = super.name;
  4255. }
  4256. }
  4257. let c = new C();
  4258. c.a === true && c.b === 1 && c.B === 2 && c.m1() && C.A && C.supername === "Error";
  4259. `
  4260. testScript(SCRIPT, valueTrue, t)
  4261. }
  4262. func TestSuperInEval(t *testing.T) {
  4263. const SCRIPT = `
  4264. class C extends Error {
  4265. constructor() {
  4266. eval("super()");
  4267. }
  4268. m() {
  4269. return eval("super.name");
  4270. }
  4271. }
  4272. let c = new C();
  4273. c.m() === "Error";
  4274. `
  4275. testScript(SCRIPT, valueTrue, t)
  4276. }
  4277. func TestSuperRefDot(t *testing.T) {
  4278. const SCRIPT = `
  4279. let thisGet, thisSet;
  4280. class P {
  4281. _p = 0
  4282. get p() {
  4283. thisGet = this;
  4284. return this._p;
  4285. }
  4286. set p(v) {
  4287. thisSet = this;
  4288. this._p = v;
  4289. }
  4290. }
  4291. class C extends P {
  4292. g() {
  4293. return super.p;
  4294. }
  4295. s(v) {
  4296. super.p = v;
  4297. }
  4298. inc() {
  4299. super.p++;
  4300. }
  4301. incR() {
  4302. return super.p++;
  4303. }
  4304. inc1() {
  4305. ++super.p;
  4306. }
  4307. inc1R() {
  4308. return ++super.p;
  4309. }
  4310. unary() {
  4311. return +super.p;
  4312. }
  4313. pattern() {
  4314. [super.p] = [9];
  4315. }
  4316. }
  4317. let o = new C();
  4318. assert.sameValue(o.g(), 0, "get value");
  4319. assert.sameValue(thisGet, o, "get this");
  4320. o.s(1);
  4321. assert.sameValue(o._p, 1, "set value");
  4322. assert.sameValue(thisSet, o, "set this");
  4323. thisGet = undefined;
  4324. thisSet = undefined;
  4325. o.inc();
  4326. assert.sameValue(o._p, 2, "inc value");
  4327. assert.sameValue(thisGet, o, "inc thisGet");
  4328. assert.sameValue(thisSet, o, "inc thisSet");
  4329. thisGet = undefined;
  4330. thisSet = undefined;
  4331. assert.sameValue(o.incR(), 2, "incR result");
  4332. assert.sameValue(o._p, 3, "incR value");
  4333. assert.sameValue(thisGet, o, "incR thisGet");
  4334. assert.sameValue(thisSet, o, "incR thisSet");
  4335. thisGet = undefined;
  4336. thisSet = undefined;
  4337. o.inc1();
  4338. assert.sameValue(o._p, 4, "inc1 value");
  4339. assert.sameValue(thisGet, o, "inc1 thisGet");
  4340. assert.sameValue(thisSet, o, "inc1 thisSet");
  4341. thisGet = undefined;
  4342. thisSet = undefined;
  4343. assert.sameValue(o.inc1R(), 5, "inc1R result");
  4344. assert.sameValue(o._p, 5, "inc1R value");
  4345. assert.sameValue(thisGet, o, "inc1R thisGet");
  4346. assert.sameValue(thisSet, o, "inc1R thisSet");
  4347. assert.sameValue(o.unary(), 5, "unary");
  4348. o.pattern();
  4349. assert.sameValue(o._p, 9, "pattern");
  4350. `
  4351. testScriptWithTestLib(SCRIPT, _undefined, t)
  4352. }
  4353. func TestPrivateRefDot(t *testing.T) {
  4354. const SCRIPT = `
  4355. class C {
  4356. #p = 0;
  4357. g() {
  4358. return this.#p;
  4359. }
  4360. s(v) {
  4361. this.#p = v;
  4362. }
  4363. inc() {
  4364. this.#p++;
  4365. }
  4366. incR() {
  4367. return this.#p++;
  4368. }
  4369. inc1() {
  4370. ++this.#p;
  4371. }
  4372. inc1R() {
  4373. return ++this.#p;
  4374. }
  4375. pattern() {
  4376. [this.#p] = [9];
  4377. }
  4378. }
  4379. let o = new C();
  4380. assert.sameValue(o.g(), 0, "get value");
  4381. o.s(1);
  4382. assert.sameValue(o.g(), 1, "set value");
  4383. o.inc();
  4384. assert.sameValue(o.g(), 2, "inc value");
  4385. assert.sameValue(o.incR(), 2, "incR result");
  4386. assert.sameValue(o.g(), 3, "incR value");
  4387. o.inc1();
  4388. assert.sameValue(o.g(), 4, "inc1 value");
  4389. assert.sameValue(o.inc1R(), 5, "inc1R result");
  4390. assert.sameValue(o.g(), 5, "inc1R value");
  4391. o.pattern();
  4392. assert.sameValue(o.g(), 9, "pattern");
  4393. `
  4394. testScriptWithTestLib(SCRIPT, _undefined, t)
  4395. }
  4396. func TestPrivateRefDotEval(t *testing.T) {
  4397. const SCRIPT = `
  4398. class C {
  4399. #p = 0;
  4400. g() {
  4401. return eval("this.#p");
  4402. }
  4403. s(v) {
  4404. eval("this.#p = v");
  4405. }
  4406. incR() {
  4407. return eval("this.#p++");
  4408. }
  4409. inc1R() {
  4410. return eval("++this.#p");
  4411. }
  4412. pattern() {
  4413. eval("[this.#p] = [9]");
  4414. }
  4415. }
  4416. let o = new C();
  4417. assert.sameValue(o.g(), 0, "get value");
  4418. o.s(1);
  4419. assert.sameValue(o.g(), 1, "set value");
  4420. assert.sameValue(o.incR(), 1, "incR result");
  4421. assert.sameValue(o.g(), 2, "incR value");
  4422. assert.sameValue(o.inc1R(), 3, "inc1R result");
  4423. assert.sameValue(o.g(), 3, "inc1R value");
  4424. o.pattern();
  4425. assert.sameValue(o.g(), 9, "pattern");
  4426. `
  4427. testScriptWithTestLib(SCRIPT, _undefined, t)
  4428. }
  4429. func TestSuperRefDotCallee(t *testing.T) {
  4430. const SCRIPT = `
  4431. class P {
  4432. get p() {
  4433. return function() {
  4434. return this;
  4435. };
  4436. }
  4437. }
  4438. class C extends P {
  4439. m() {
  4440. return super.p();
  4441. }
  4442. }
  4443. let o = new C();
  4444. o.m() === o;
  4445. `
  4446. testScript(SCRIPT, valueTrue, t)
  4447. }
  4448. func TestSuperRefBracket(t *testing.T) {
  4449. const SCRIPT = `
  4450. let PROP = "p";
  4451. let thisGet, thisSet;
  4452. class P {
  4453. _p = 0
  4454. get p() {
  4455. thisGet = this;
  4456. return this._p;
  4457. }
  4458. set p(v) {
  4459. thisSet = this;
  4460. this._p = v;
  4461. }
  4462. }
  4463. class C extends P {
  4464. g() {
  4465. return super[PROP];
  4466. }
  4467. s(v) {
  4468. super[PROP] = v;
  4469. }
  4470. inc() {
  4471. super[PROP]++;
  4472. }
  4473. incR() {
  4474. return super[PROP]++;
  4475. }
  4476. inc1() {
  4477. ++super[PROP];
  4478. }
  4479. inc1R() {
  4480. return ++super[PROP];
  4481. }
  4482. pattern() {
  4483. [super[PROP]] = [9];
  4484. }
  4485. }
  4486. let o = new C();
  4487. assert.sameValue(o.g(), 0, "get value");
  4488. assert.sameValue(thisGet, o, "get this");
  4489. o.s(1);
  4490. assert.sameValue(o._p, 1, "set value");
  4491. assert.sameValue(thisSet, o, "set this");
  4492. thisGet = undefined;
  4493. thisSet = undefined;
  4494. o.inc();
  4495. assert.sameValue(o._p, 2, "inc value");
  4496. assert.sameValue(thisGet, o, "inc thisGet");
  4497. assert.sameValue(thisSet, o, "inc thisSet");
  4498. thisGet = undefined;
  4499. thisSet = undefined;
  4500. assert.sameValue(o.incR(), 2, "incR result");
  4501. assert.sameValue(o._p, 3, "incR value");
  4502. assert.sameValue(thisGet, o, "incR thisGet");
  4503. assert.sameValue(thisSet, o, "incR thisSet");
  4504. thisGet = undefined;
  4505. thisSet = undefined;
  4506. o.inc1();
  4507. assert.sameValue(o._p, 4, "inc1 value");
  4508. assert.sameValue(thisGet, o, "inc1 thisGet");
  4509. assert.sameValue(thisSet, o, "inc1 thisSet");
  4510. thisGet = undefined;
  4511. thisSet = undefined;
  4512. assert.sameValue(o.inc1R(), 5, "inc1R result");
  4513. assert.sameValue(o._p, 5, "inc1R value");
  4514. assert.sameValue(thisGet, o, "inc1R thisGet");
  4515. assert.sameValue(thisSet, o, "inc1R thisSet");
  4516. o.pattern();
  4517. assert.sameValue(o._p, 9, "pattern");
  4518. `
  4519. testScriptWithTestLib(SCRIPT, _undefined, t)
  4520. }
  4521. func TestSuperRefBracketEvalOrder(t *testing.T) {
  4522. const SCRIPT = `
  4523. let keyCallCount = 0;
  4524. function key() {
  4525. keyCallCount++;
  4526. C.prototype.__proto__ = null;
  4527. return "k";
  4528. }
  4529. class C {
  4530. constructor() {
  4531. super[key()]++;
  4532. }
  4533. }
  4534. assert.throws(TypeError, () => new C());
  4535. assert.sameValue(keyCallCount, 1);
  4536. `
  4537. testScriptWithTestLib(SCRIPT, _undefined, t)
  4538. }
  4539. func TestSuperRefBracketCallee(t *testing.T) {
  4540. const SCRIPT = `
  4541. let PROP = "p";
  4542. class P {
  4543. get p() {
  4544. return function() {
  4545. return this;
  4546. };
  4547. }
  4548. }
  4549. class C extends P {
  4550. m() {
  4551. return super[PROP]();
  4552. }
  4553. }
  4554. let o = new C();
  4555. o.m() === o;
  4556. `
  4557. testScript(SCRIPT, valueTrue, t)
  4558. }
  4559. func TestSuperBaseInCtor(t *testing.T) {
  4560. const SCRIPT = `
  4561. let result;
  4562. class Derived extends Object {
  4563. constructor() {
  4564. super();
  4565. result = super.constructor === Object;
  4566. }
  4567. }
  4568. new Derived();
  4569. result;
  4570. `
  4571. testScript(SCRIPT, valueTrue, t)
  4572. }
  4573. func TestClassNamedEval(t *testing.T) {
  4574. const SCRIPT = `
  4575. const C = class {
  4576. }
  4577. C.name === "C";
  4578. `
  4579. testScript(SCRIPT, valueTrue, t)
  4580. }
  4581. func TestClassNonDerived(t *testing.T) {
  4582. const SCRIPT = `
  4583. function initF() {
  4584. }
  4585. class C {
  4586. f = initF()
  4587. }
  4588. let c = new C();
  4589. `
  4590. testScript(SCRIPT, _undefined, t)
  4591. }
  4592. func TestClassExpr(t *testing.T) {
  4593. const SCRIPT = `
  4594. typeof Object.getOwnPropertyDescriptor(class {get f() {}}.prototype, "f").get === "function";
  4595. `
  4596. testScript(SCRIPT, valueTrue, t)
  4597. }
  4598. func TestClassSuperInHeritage(t *testing.T) {
  4599. const SCRIPT = `
  4600. class P {
  4601. a() {
  4602. return Error;
  4603. }
  4604. }
  4605. class C extends P {
  4606. m() {
  4607. class Inner extends super.a() {
  4608. }
  4609. return new Inner();
  4610. }
  4611. }
  4612. new C().m() instanceof Error;
  4613. `
  4614. testScript(SCRIPT, valueTrue, t)
  4615. }
  4616. func TestClassSuperInHeritageExpr(t *testing.T) {
  4617. const SCRIPT = `
  4618. class P {
  4619. a() {
  4620. return Error;
  4621. }
  4622. }
  4623. class C extends P {
  4624. m() {
  4625. function f(cls) {
  4626. return new cls();
  4627. }
  4628. return f(class Inner extends super.a() {
  4629. })
  4630. }
  4631. }
  4632. new C().m() instanceof Error;
  4633. `
  4634. testScript(SCRIPT, valueTrue, t)
  4635. }
  4636. func TestClassReferToBinding(t *testing.T) {
  4637. const SCRIPT = `
  4638. const CC = class C {
  4639. static T = 40
  4640. f = C.T + 2
  4641. }
  4642. let c = new CC();
  4643. c.f;
  4644. `
  4645. testScript(SCRIPT, intToValue(42), t)
  4646. }
  4647. func TestClassReferToBindingInStaticDecl(t *testing.T) {
  4648. const SCRIPT = `
  4649. class C {
  4650. static T = C.name
  4651. }
  4652. C.T;
  4653. `
  4654. testScript(SCRIPT, asciiString("C"), t)
  4655. }
  4656. func TestClassReferToBindingInStaticEval(t *testing.T) {
  4657. const SCRIPT = `
  4658. const CC = class C {
  4659. static T = eval("C.name")
  4660. }
  4661. CC.T;
  4662. `
  4663. testScript(SCRIPT, asciiString("C"), t)
  4664. }
  4665. func TestClassReferToBindingFromHeritage(t *testing.T) {
  4666. const SCRIPT = `
  4667. assert.throws(ReferenceError, () => {
  4668. class C extends C {
  4669. }
  4670. });
  4671. `
  4672. testScriptWithTestLib(SCRIPT, _undefined, t)
  4673. }
  4674. func TestClassCaptureSuperCallInArrowFunc(t *testing.T) {
  4675. const SCRIPT = `
  4676. let f;
  4677. class C extends class {} {
  4678. constructor() {
  4679. f = () => super();
  4680. f();
  4681. }
  4682. }
  4683. let c = new C();
  4684. `
  4685. testScript(SCRIPT, _undefined, t)
  4686. }
  4687. func TestClassCaptureSuperCallInNestedArrowFunc(t *testing.T) {
  4688. const SCRIPT = `
  4689. let f;
  4690. class P {
  4691. }
  4692. class C extends P {
  4693. constructor() {
  4694. f = () => () => super();
  4695. f()();
  4696. }
  4697. }
  4698. new C() instanceof P;
  4699. `
  4700. testScript(SCRIPT, valueTrue, t)
  4701. }
  4702. func TestThisInEval(t *testing.T) {
  4703. const SCRIPT = `
  4704. assert.sameValue(eval("this"), this, "global");
  4705. let o = {
  4706. f() {
  4707. return eval("this");
  4708. }
  4709. }
  4710. assert.sameValue(o.f(), o, "obj literal");
  4711. `
  4712. testScriptWithTestLib(SCRIPT, _undefined, t)
  4713. }
  4714. func TestStaticAsBindingTarget(t *testing.T) {
  4715. const SCRIPT = `
  4716. let [static] = [];
  4717. `
  4718. testScript(SCRIPT, _undefined, t)
  4719. }
  4720. func TestEvalInStaticFieldInit(t *testing.T) {
  4721. const SCRIPT = `
  4722. var C = class {
  4723. static f = 'test';
  4724. static g = this.f + '262';
  4725. static h = eval('this.g') + 'test';
  4726. }
  4727. C.f === "test" && C.g === "test262" && C.h === "test262test";
  4728. `
  4729. testScript(SCRIPT, valueTrue, t)
  4730. }
  4731. func TestClassPrivateElemInEval(t *testing.T) {
  4732. const SCRIPT = `
  4733. let f1, f2;
  4734. class C extends (f1 = o => eval("o.#a"), Object) {
  4735. static #a = 42;
  4736. static {
  4737. f2 = o => eval("o.#a");
  4738. assert.sameValue(C.#a, 42);
  4739. assert.sameValue((() => C.#a)(), 42);
  4740. }
  4741. }
  4742. assert.throws(SyntaxError, () => f1(C));
  4743. assert.sameValue(f2(C), 42);
  4744. `
  4745. testScriptWithTestLib(SCRIPT, _undefined, t)
  4746. }
  4747. func TestClassPrivateElemInIndirectEval(t *testing.T) {
  4748. const SCRIPT = `
  4749. let f1, f2;
  4750. class C extends (f1 = o => (0, eval)("o.#a"), Object) {
  4751. static #a = 42;
  4752. static {
  4753. f2 = o => (0, eval)("o.#a");
  4754. assert.throws(SyntaxError, () => (0, eval)("C.#a"));
  4755. }
  4756. }
  4757. assert.throws(SyntaxError, () => f1(C));
  4758. assert.throws(SyntaxError, () => f2(C));
  4759. `
  4760. testScriptWithTestLib(SCRIPT, _undefined, t)
  4761. }
  4762. func TestClassPrivateElemInFunction(t *testing.T) {
  4763. const SCRIPT = `
  4764. assert.throws(SyntaxError, () => {
  4765. class C {
  4766. static #a = 42;
  4767. static {
  4768. Function("o", "return o.#a");
  4769. }
  4770. }
  4771. });
  4772. `
  4773. testScriptWithTestLib(SCRIPT, _undefined, t)
  4774. }
  4775. func TestClassPrivateElementsDecl(t *testing.T) {
  4776. const SCRIPT = `
  4777. class C {
  4778. #a = 42;
  4779. get #b() {}
  4780. set #b(_) {}
  4781. get c() {
  4782. return this.#a;
  4783. }
  4784. #m() {
  4785. return this.#a;
  4786. }
  4787. static getter(inst) {
  4788. return inst.#m();
  4789. }
  4790. }
  4791. let c = new C();
  4792. c.c + C.getter(c);
  4793. `
  4794. testScript(SCRIPT, intToValue(84), t)
  4795. }
  4796. func TestPrivateIn(t *testing.T) {
  4797. const SCRIPT = `
  4798. class C {
  4799. #a = 42;
  4800. static check(inst) {
  4801. return #a in inst;
  4802. }
  4803. }
  4804. let c = new C();
  4805. C.check(c);
  4806. `
  4807. testScript(SCRIPT, valueTrue, t)
  4808. }
  4809. func TestDeletePropOfNonObject(t *testing.T) {
  4810. const SCRIPT = `
  4811. delete 'Test262'[100] && delete 'Test262'.a && delete 'Test262'['@'];
  4812. `
  4813. testScript(SCRIPT, valueTrue, t)
  4814. }
  4815. func TestKeywordsAsLabels(t *testing.T) {
  4816. const SCRIPT = `
  4817. let: for (let i = 0; i < 2; i++) {
  4818. if (i === 0) continue let;
  4819. break let;
  4820. }
  4821. \u006Cet: for (let i = 0; i < 2; i++) {
  4822. if (i === 0) continue \u006Cet;
  4823. break \u006Cet;
  4824. }
  4825. yield: for (let i = 0; i < 2; i++) {
  4826. if (i === 0) continue yield;
  4827. break yield;
  4828. }
  4829. yi\u0065ld: for (let i = 0; i < 2; i++) {
  4830. if (i === 0) continue yi\u0065ld;
  4831. break yi\u0065ld;
  4832. }
  4833. `
  4834. testScript(SCRIPT, _undefined, t)
  4835. }
  4836. func TestThisResolutionWithArg(t *testing.T) {
  4837. const SCRIPT = `
  4838. let capture;
  4839. function f(arg) {
  4840. capture = () => this; // move 'this' to stash
  4841. return [this, arg];
  4842. }
  4843. const _this = {};
  4844. const arg = {};
  4845. const [_this1, arg1] = f.call(_this, arg);
  4846. _this1 === _this && arg1 === arg;
  4847. `
  4848. testScript(SCRIPT, valueTrue, t)
  4849. }
  4850. func TestThisResolutionArgInStash(t *testing.T) {
  4851. const SCRIPT = `
  4852. let capture;
  4853. function f(arg) {
  4854. capture = () => this + arg; // move 'this' and arguments to stash
  4855. return [this, arg];
  4856. }
  4857. const _this = {};
  4858. const arg = {};
  4859. const [_this1, arg1] = f.call(_this, arg);
  4860. _this1 === _this && arg1 === arg;
  4861. `
  4862. testScript(SCRIPT, valueTrue, t)
  4863. }
  4864. func TestThisResolutionWithStackVar(t *testing.T) {
  4865. const SCRIPT = `
  4866. let capture;
  4867. function f(arg) {
  4868. const _ = 1; // a stack variable
  4869. capture = () => this + arg; // move 'this' and arguments to stash
  4870. return [this, arg];
  4871. }
  4872. const _this = {};
  4873. const arg = {};
  4874. const [_this1, arg1] = f.call(_this, arg);
  4875. _this1 === _this && arg1 === arg;
  4876. `
  4877. testScript(SCRIPT, valueTrue, t)
  4878. }
  4879. func TestForInLoopContinue(t *testing.T) {
  4880. const SCRIPT = `
  4881. var globalSink;
  4882. (function() {
  4883. const data = [{disabled: true}, {}];
  4884. function dummy() {}
  4885. function f1() {}
  4886. function f() {
  4887. dummy(); // move dummy to stash (so that f1 is at index 1)
  4888. for (const d of data) {
  4889. if (d.disabled) continue;
  4890. globalSink = () => d; // move d to stash
  4891. f1();
  4892. }
  4893. }
  4894. f();
  4895. })();
  4896. `
  4897. testScript(SCRIPT, _undefined, t)
  4898. }
  4899. func TestForInLoopContinueOuter(t *testing.T) {
  4900. const SCRIPT = `
  4901. var globalSink;
  4902. (function() {
  4903. const data = [{disabled: true}, {}];
  4904. function dummy1() {}
  4905. function f1() {}
  4906. function f() {
  4907. dummy1();
  4908. let counter = 0;
  4909. OUTER: for (let i = 0; i < 1; i++) {
  4910. for (const d of data) {
  4911. if (d.disabled) continue OUTER;
  4912. globalSink = () => d;
  4913. }
  4914. counter++;
  4915. }
  4916. f1();
  4917. if (counter !== 0) {
  4918. throw new Error(counter);
  4919. }
  4920. }
  4921. f();
  4922. })();
  4923. `
  4924. testScript(SCRIPT, _undefined, t)
  4925. }
  4926. func TestLexicalDeclInSwitch(t *testing.T) {
  4927. const SCRIPT = `
  4928. switch(0) {
  4929. case 1:
  4930. if (false) b = 3;
  4931. case 2:
  4932. const c = 1;
  4933. }
  4934. `
  4935. testScript(SCRIPT, _undefined, t)
  4936. }
  4937. func TestClassFieldSpecial(t *testing.T) {
  4938. const SCRIPT = `
  4939. class C {
  4940. get;
  4941. set;
  4942. async;
  4943. static;
  4944. }
  4945. `
  4946. testScript(SCRIPT, _undefined, t)
  4947. }
  4948. func TestClassMethodSpecial(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 TestClassMethodNumLiteral(t *testing.T) {
  4960. const SCRIPT = `
  4961. class C {
  4962. 0() {
  4963. return true;
  4964. }
  4965. }
  4966. new C()[0]();
  4967. `
  4968. testScript(SCRIPT, valueTrue, t)
  4969. }
  4970. func TestAsyncFunc(t *testing.T) {
  4971. const SCRIPT = `
  4972. async (x = true, y) => {};
  4973. async x => {};
  4974. let passed = false;
  4975. async function f() {
  4976. return true;
  4977. }
  4978. async function f1(arg = true) {
  4979. passed = await f();
  4980. }
  4981. await f1();
  4982. return passed;
  4983. `
  4984. testAsyncFunc(SCRIPT, valueTrue, t)
  4985. }
  4986. func TestObjectLiteralComputedMethodKeys(t *testing.T) {
  4987. _, err := Compile("", `
  4988. ({
  4989. ["__proto__"]() {},
  4990. ["__proto__"]() {}
  4991. })
  4992. `, false)
  4993. if err != nil {
  4994. t.Fatal(err)
  4995. }
  4996. _, err = Compile("", `
  4997. ({
  4998. get ["__proto__"]() {},
  4999. get ["__proto__"]() {}
  5000. })
  5001. `, false)
  5002. if err != nil {
  5003. t.Fatal(err)
  5004. }
  5005. }
  5006. func TestGeneratorFunc(t *testing.T) {
  5007. const SCRIPT = `
  5008. let trace = "";
  5009. function defParam() {
  5010. trace += "1";
  5011. return "def";
  5012. }
  5013. function* g(param = defParam()) {
  5014. const THREE = 3;
  5015. trace += "2";
  5016. assert.sameValue(Math.floor(yield 1), THREE);
  5017. return 42;
  5018. }
  5019. let iter = g();
  5020. assert.sameValue(trace, "1");
  5021. let next = iter.next();
  5022. assert.sameValue(next.value, 1);
  5023. assert.sameValue(next.done, false);
  5024. next = iter.next(Math.PI);
  5025. assert.sameValue(next.value, 42);
  5026. assert.sameValue(next.done, true);
  5027. assert.sameValue(trace, "12");
  5028. `
  5029. testScriptWithTestLib(SCRIPT, _undefined, t)
  5030. }
  5031. func TestGeneratorMethods(t *testing.T) {
  5032. const SCRIPT = `
  5033. class C {
  5034. *g(param) {
  5035. yield 1;
  5036. yield 2;
  5037. }
  5038. }
  5039. let c = new C();
  5040. let iter = c.g();
  5041. let res = iter.next();
  5042. assert.sameValue(res.value, 1);
  5043. assert.sameValue(res.done, false);
  5044. res = iter.next();
  5045. assert.sameValue(res.value, 2);
  5046. assert.sameValue(res.done, false);
  5047. res = iter.next();
  5048. assert.sameValue(res.value, undefined);
  5049. assert.sameValue(res.done, true);
  5050. `
  5051. testScriptWithTestLib(SCRIPT, _undefined, t)
  5052. }
  5053. func TestFunctionBodyClassDecl(t *testing.T) {
  5054. const SCRIPT = `
  5055. function as(requiredArgument = {}) {
  5056. class something { }
  5057. };
  5058. `
  5059. _, err := Compile("", SCRIPT, false)
  5060. if err != nil {
  5061. t.Fatal(err)
  5062. }
  5063. }
  5064. /*
  5065. func TestBabel(t *testing.T) {
  5066. src, err := os.ReadFile("babel7.js")
  5067. if err != nil {
  5068. t.Fatal(err)
  5069. }
  5070. vm := New()
  5071. _, err = vm.RunString(string(src))
  5072. if err != nil {
  5073. t.Fatal(err)
  5074. }
  5075. _, err = vm.RunString(`var result = Babel.transform("", {presets: ["es2015"]});`)
  5076. if err != nil {
  5077. t.Fatal(err)
  5078. }
  5079. }*/
  5080. func BenchmarkCompile(b *testing.B) {
  5081. data, err := os.ReadFile("testdata/S15.10.2.12_A1_T1.js")
  5082. if err != nil {
  5083. b.Fatal(err)
  5084. }
  5085. src := string(data)
  5086. for i := 0; i < b.N; i++ {
  5087. _, err := Compile("test.js", src, false)
  5088. if err != nil {
  5089. b.Fatal(err)
  5090. }
  5091. }
  5092. }