vm.go 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613
  1. package goja
  2. import (
  3. "fmt"
  4. "math"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. "sync/atomic"
  9. "time"
  10. "github.com/dop251/goja/unistring"
  11. )
  12. const (
  13. maxInt = 1 << 53
  14. tryPanicMarker = -2
  15. )
  16. type valueStack []Value
  17. type stash struct {
  18. values []Value
  19. extraArgs []Value
  20. names map[unistring.String]uint32
  21. obj *Object
  22. outer *stash
  23. // If this is a top-level function stash, sets the type of the function. If set, dynamic var declarations
  24. // created by direct eval go here.
  25. funcType funcType
  26. }
  27. type context struct {
  28. prg *Program
  29. stash *stash
  30. privEnv *privateEnv
  31. newTarget Value
  32. result Value
  33. pc, sb int
  34. args int
  35. }
  36. type tryFrame struct {
  37. // holds an uncaught exception for the 'finally' block
  38. exception *Exception
  39. callStackLen, iterLen, refLen uint32
  40. sp int32
  41. stash *stash
  42. privEnv *privateEnv
  43. catchPos, finallyPos, finallyRet int32
  44. }
  45. type execCtx struct {
  46. context
  47. stack []Value
  48. tryStack []tryFrame
  49. iterStack []iterStackItem
  50. refStack []ref
  51. }
  52. func (vm *vm) suspend(ectx *execCtx, tryStackLen, iterStackLen, refStackLen uint32) {
  53. vm.saveCtx(&ectx.context)
  54. ectx.stack = append(ectx.stack[:0], vm.stack[vm.sb-1:vm.sp]...)
  55. if len(vm.tryStack) > int(tryStackLen) {
  56. ectx.tryStack = append(ectx.tryStack[:0], vm.tryStack[tryStackLen:]...)
  57. vm.tryStack = vm.tryStack[:tryStackLen]
  58. sp := int32(vm.sb - 1)
  59. for i := range ectx.tryStack {
  60. tf := &ectx.tryStack[i]
  61. tf.iterLen -= iterStackLen
  62. tf.refLen -= refStackLen
  63. tf.sp -= sp
  64. }
  65. }
  66. if len(vm.iterStack) > int(iterStackLen) {
  67. ectx.iterStack = append(ectx.iterStack[:0], vm.iterStack[iterStackLen:]...)
  68. vm.iterStack = vm.iterStack[:iterStackLen]
  69. }
  70. if len(vm.refStack) > int(refStackLen) {
  71. ectx.refStack = append(ectx.refStack[:0], vm.refStack[refStackLen:]...)
  72. vm.refStack = vm.refStack[:refStackLen]
  73. }
  74. }
  75. func (vm *vm) resume(ctx *execCtx) {
  76. vm.restoreCtx(&ctx.context)
  77. sp := vm.sp
  78. vm.sb = sp + 1
  79. vm.stack.expand(sp + len(ctx.stack))
  80. copy(vm.stack[sp:], ctx.stack)
  81. vm.sp += len(ctx.stack)
  82. for i := range ctx.tryStack {
  83. tf := &ctx.tryStack[i]
  84. tf.callStackLen = uint32(len(vm.callStack))
  85. tf.iterLen += uint32(len(vm.iterStack))
  86. tf.refLen += uint32(len(vm.refStack))
  87. tf.sp += int32(sp)
  88. }
  89. vm.tryStack = append(vm.tryStack, ctx.tryStack...)
  90. vm.iterStack = append(vm.iterStack, ctx.iterStack...)
  91. vm.refStack = append(vm.refStack, ctx.refStack...)
  92. }
  93. type iterStackItem struct {
  94. val Value
  95. f iterNextFunc
  96. iter *iteratorRecord
  97. }
  98. type ref interface {
  99. get() Value
  100. set(Value)
  101. init(Value)
  102. refname() unistring.String
  103. }
  104. type stashRef struct {
  105. n unistring.String
  106. v *[]Value
  107. idx int
  108. }
  109. func (r *stashRef) get() Value {
  110. return nilSafe((*r.v)[r.idx])
  111. }
  112. func (r *stashRef) set(v Value) {
  113. (*r.v)[r.idx] = v
  114. }
  115. func (r *stashRef) init(v Value) {
  116. r.set(v)
  117. }
  118. func (r *stashRef) refname() unistring.String {
  119. return r.n
  120. }
  121. type thisRef struct {
  122. v *[]Value
  123. idx int
  124. }
  125. func (r *thisRef) get() Value {
  126. v := (*r.v)[r.idx]
  127. if v == nil {
  128. panic(referenceError("Must call super constructor in derived class before accessing 'this'"))
  129. }
  130. return v
  131. }
  132. func (r *thisRef) set(v Value) {
  133. ptr := &(*r.v)[r.idx]
  134. if *ptr != nil {
  135. panic(referenceError("Super constructor may only be called once"))
  136. }
  137. *ptr = v
  138. }
  139. func (r *thisRef) init(v Value) {
  140. r.set(v)
  141. }
  142. func (r *thisRef) refname() unistring.String {
  143. return thisBindingName
  144. }
  145. type stashRefLex struct {
  146. stashRef
  147. }
  148. func (r *stashRefLex) get() Value {
  149. v := (*r.v)[r.idx]
  150. if v == nil {
  151. panic(errAccessBeforeInit)
  152. }
  153. return v
  154. }
  155. func (r *stashRefLex) set(v Value) {
  156. p := &(*r.v)[r.idx]
  157. if *p == nil {
  158. panic(errAccessBeforeInit)
  159. }
  160. *p = v
  161. }
  162. func (r *stashRefLex) init(v Value) {
  163. (*r.v)[r.idx] = v
  164. }
  165. type stashRefConst struct {
  166. stashRefLex
  167. strictConst bool
  168. }
  169. func (r *stashRefConst) set(v Value) {
  170. if r.strictConst {
  171. panic(errAssignToConst)
  172. }
  173. }
  174. type objRef struct {
  175. base *Object
  176. name unistring.String
  177. this Value
  178. strict bool
  179. binding bool
  180. }
  181. func (r *objRef) get() Value {
  182. return r.base.self.getStr(r.name, r.this)
  183. }
  184. func (r *objRef) set(v Value) {
  185. if r.strict && r.binding && !r.base.self.hasOwnPropertyStr(r.name) {
  186. panic(referenceError(fmt.Sprintf("%s is not defined", r.name)))
  187. }
  188. if r.this != nil {
  189. r.base.setStr(r.name, v, r.this, r.strict)
  190. } else {
  191. r.base.self.setOwnStr(r.name, v, r.strict)
  192. }
  193. }
  194. func (r *objRef) init(v Value) {
  195. if r.this != nil {
  196. r.base.setStr(r.name, v, r.this, r.strict)
  197. } else {
  198. r.base.self.setOwnStr(r.name, v, r.strict)
  199. }
  200. }
  201. func (r *objRef) refname() unistring.String {
  202. return r.name
  203. }
  204. type privateRefRes struct {
  205. base *Object
  206. name *resolvedPrivateName
  207. }
  208. func (p *privateRefRes) get() Value {
  209. return (*getPrivatePropRes)(p.name)._get(p.base, p.base.runtime.vm)
  210. }
  211. func (p *privateRefRes) set(value Value) {
  212. (*setPrivatePropRes)(p.name)._set(p.base, value, p.base.runtime.vm)
  213. }
  214. func (p *privateRefRes) init(value Value) {
  215. panic("not supported")
  216. }
  217. func (p *privateRefRes) refname() unistring.String {
  218. return p.name.string()
  219. }
  220. type privateRefId struct {
  221. base *Object
  222. id *privateId
  223. }
  224. func (p *privateRefId) get() Value {
  225. return p.base.runtime.vm.getPrivateProp(p.base, p.id.name, p.id.typ, p.id.idx, p.id.isMethod)
  226. }
  227. func (p *privateRefId) set(value Value) {
  228. p.base.runtime.vm.setPrivateProp(p.base, p.id.name, p.id.typ, p.id.idx, p.id.isMethod, value)
  229. }
  230. func (p *privateRefId) init(value Value) {
  231. panic("not supported")
  232. }
  233. func (p *privateRefId) refname() unistring.String {
  234. return p.id.string()
  235. }
  236. type unresolvedRef struct {
  237. runtime *Runtime
  238. name unistring.String
  239. }
  240. func (r *unresolvedRef) get() Value {
  241. r.runtime.throwReferenceError(r.name)
  242. panic("Unreachable")
  243. }
  244. func (r *unresolvedRef) set(Value) {
  245. r.get()
  246. }
  247. func (r *unresolvedRef) init(Value) {
  248. r.get()
  249. }
  250. func (r *unresolvedRef) refname() unistring.String {
  251. return r.name
  252. }
  253. type vm struct {
  254. r *Runtime
  255. prg *Program
  256. pc int
  257. stack valueStack
  258. sp, sb, args int
  259. stash *stash
  260. privEnv *privateEnv
  261. callStack []context
  262. iterStack []iterStackItem
  263. refStack []ref
  264. tryStack []tryFrame
  265. newTarget Value
  266. result Value
  267. maxCallStackSize int
  268. stashAllocs int
  269. interrupted uint32
  270. interruptVal interface{}
  271. interruptLock sync.Mutex
  272. curAsyncRunner *asyncRunner
  273. profTracker *profTracker
  274. }
  275. type instruction interface {
  276. exec(*vm)
  277. }
  278. func intToValue(i int64) Value {
  279. if idx := 256 + i; idx >= 0 && idx < 256 {
  280. return intCache[idx]
  281. }
  282. if i >= -maxInt && i <= maxInt {
  283. return valueInt(i)
  284. }
  285. return valueFloat(i)
  286. }
  287. func floatToInt(f float64) (result int64, ok bool) {
  288. if (f != 0 || !math.Signbit(f)) && !math.IsInf(f, 0) && f == math.Trunc(f) && f >= -maxInt && f <= maxInt {
  289. return int64(f), true
  290. }
  291. return 0, false
  292. }
  293. func floatToValue(f float64) (result Value) {
  294. if i, ok := floatToInt(f); ok {
  295. return intToValue(i)
  296. }
  297. switch {
  298. case f == 0:
  299. return _negativeZero
  300. case math.IsNaN(f):
  301. return _NaN
  302. case math.IsInf(f, 1):
  303. return _positiveInf
  304. case math.IsInf(f, -1):
  305. return _negativeInf
  306. }
  307. return valueFloat(f)
  308. }
  309. func assertInt64(v Value) (int64, bool) {
  310. num := v.ToNumber()
  311. if i, ok := num.(valueInt); ok {
  312. return int64(i), true
  313. }
  314. if f, ok := num.(valueFloat); ok {
  315. if i, ok := floatToInt(float64(f)); ok {
  316. return i, true
  317. }
  318. }
  319. return 0, false
  320. }
  321. func (s *valueStack) expand(idx int) {
  322. if idx < len(*s) {
  323. return
  324. }
  325. idx++
  326. if idx < cap(*s) {
  327. *s = (*s)[:idx]
  328. } else {
  329. var newCap int
  330. if idx < 1024 {
  331. newCap = idx * 2
  332. } else {
  333. newCap = (idx + 1025) &^ 1023
  334. }
  335. n := make([]Value, idx, newCap)
  336. copy(n, *s)
  337. *s = n
  338. }
  339. }
  340. func stashObjHas(obj *Object, name unistring.String) bool {
  341. if obj.self.hasPropertyStr(name) {
  342. if unscopables, ok := obj.self.getSym(SymUnscopables, nil).(*Object); ok {
  343. if b := unscopables.self.getStr(name, nil); b != nil {
  344. return !b.ToBoolean()
  345. }
  346. }
  347. return true
  348. }
  349. return false
  350. }
  351. func (s *stash) isVariable() bool {
  352. return s.funcType != funcNone
  353. }
  354. func (s *stash) initByIdx(idx uint32, v Value) {
  355. if s.obj != nil {
  356. panic("Attempt to init by idx into an object scope")
  357. }
  358. s.values[idx] = v
  359. }
  360. func (s *stash) initByName(name unistring.String, v Value) {
  361. if idx, exists := s.names[name]; exists {
  362. s.values[idx&^maskTyp] = v
  363. } else {
  364. panic(referenceError(fmt.Sprintf("%s is not defined", name)))
  365. }
  366. }
  367. func (s *stash) getByIdx(idx uint32) Value {
  368. return s.values[idx]
  369. }
  370. func (s *stash) getByName(name unistring.String) (v Value, exists bool) {
  371. if s.obj != nil {
  372. if stashObjHas(s.obj, name) {
  373. return nilSafe(s.obj.self.getStr(name, nil)), true
  374. }
  375. return nil, false
  376. }
  377. if idx, exists := s.names[name]; exists {
  378. v := s.values[idx&^maskTyp]
  379. if v == nil {
  380. if idx&maskVar == 0 {
  381. panic(errAccessBeforeInit)
  382. } else {
  383. v = _undefined
  384. }
  385. }
  386. return v, true
  387. }
  388. return nil, false
  389. }
  390. func (s *stash) getRefByName(name unistring.String, strict bool) ref {
  391. if obj := s.obj; obj != nil {
  392. if stashObjHas(obj, name) {
  393. return &objRef{
  394. base: obj,
  395. name: name,
  396. strict: strict,
  397. binding: true,
  398. }
  399. }
  400. } else {
  401. if idx, exists := s.names[name]; exists {
  402. if idx&maskVar == 0 {
  403. if idx&maskConst == 0 {
  404. return &stashRefLex{
  405. stashRef: stashRef{
  406. n: name,
  407. v: &s.values,
  408. idx: int(idx &^ maskTyp),
  409. },
  410. }
  411. } else {
  412. return &stashRefConst{
  413. stashRefLex: stashRefLex{
  414. stashRef: stashRef{
  415. n: name,
  416. v: &s.values,
  417. idx: int(idx &^ maskTyp),
  418. },
  419. },
  420. strictConst: strict || (idx&maskStrict != 0),
  421. }
  422. }
  423. } else {
  424. return &stashRef{
  425. n: name,
  426. v: &s.values,
  427. idx: int(idx &^ maskTyp),
  428. }
  429. }
  430. }
  431. }
  432. return nil
  433. }
  434. func (s *stash) createBinding(name unistring.String, deletable bool) {
  435. if s.names == nil {
  436. s.names = make(map[unistring.String]uint32)
  437. }
  438. if _, exists := s.names[name]; !exists {
  439. idx := uint32(len(s.names)) | maskVar
  440. if deletable {
  441. idx |= maskDeletable
  442. }
  443. s.names[name] = idx
  444. s.values = append(s.values, _undefined)
  445. }
  446. }
  447. func (s *stash) createLexBinding(name unistring.String, isConst bool) {
  448. if s.names == nil {
  449. s.names = make(map[unistring.String]uint32)
  450. }
  451. if _, exists := s.names[name]; !exists {
  452. idx := uint32(len(s.names))
  453. if isConst {
  454. idx |= maskConst | maskStrict
  455. }
  456. s.names[name] = idx
  457. s.values = append(s.values, nil)
  458. }
  459. }
  460. func (s *stash) deleteBinding(name unistring.String) {
  461. delete(s.names, name)
  462. }
  463. func (vm *vm) newStash() {
  464. vm.stash = &stash{
  465. outer: vm.stash,
  466. }
  467. vm.stashAllocs++
  468. }
  469. func (vm *vm) init() {
  470. vm.sb = -1
  471. vm.stash = &vm.r.global.stash
  472. vm.maxCallStackSize = math.MaxInt32
  473. }
  474. func (vm *vm) halted() bool {
  475. pc := vm.pc
  476. return pc < 0 || pc >= len(vm.prg.code)
  477. }
  478. func (vm *vm) run() {
  479. if vm.profTracker != nil && !vm.runWithProfiler() {
  480. return
  481. }
  482. count := 0
  483. interrupted := false
  484. for {
  485. if count == 0 {
  486. if atomic.LoadInt32(&globalProfiler.enabled) == 1 && !vm.runWithProfiler() {
  487. return
  488. }
  489. count = 100
  490. } else {
  491. count--
  492. }
  493. if interrupted = atomic.LoadUint32(&vm.interrupted) != 0; interrupted {
  494. break
  495. }
  496. pc := vm.pc
  497. if pc < 0 || pc >= len(vm.prg.code) {
  498. break
  499. }
  500. vm.prg.code[pc].exec(vm)
  501. }
  502. if interrupted {
  503. vm.interruptLock.Lock()
  504. v := &InterruptedError{
  505. iface: vm.interruptVal,
  506. }
  507. v.stack = vm.captureStack(nil, 0)
  508. vm.interruptLock.Unlock()
  509. panic(v)
  510. }
  511. }
  512. func (vm *vm) runWithProfiler() bool {
  513. pt := vm.profTracker
  514. if pt == nil {
  515. pt = globalProfiler.p.registerVm()
  516. vm.profTracker = pt
  517. defer func() {
  518. atomic.StoreInt32(&vm.profTracker.finished, 1)
  519. vm.profTracker = nil
  520. }()
  521. }
  522. interrupted := false
  523. for {
  524. if interrupted = atomic.LoadUint32(&vm.interrupted) != 0; interrupted {
  525. return true
  526. }
  527. pc := vm.pc
  528. if pc < 0 || pc >= len(vm.prg.code) {
  529. break
  530. }
  531. vm.prg.code[pc].exec(vm)
  532. req := atomic.LoadInt32(&pt.req)
  533. if req == profReqStop {
  534. return true
  535. }
  536. if req == profReqDoSample {
  537. pt.stop = time.Now()
  538. pt.numFrames = len(vm.r.CaptureCallStack(len(pt.frames), pt.frames[:0]))
  539. pt.frames[0].pc = pc
  540. atomic.StoreInt32(&pt.req, profReqSampleReady)
  541. }
  542. }
  543. return false
  544. }
  545. func (vm *vm) Interrupt(v interface{}) {
  546. vm.interruptLock.Lock()
  547. vm.interruptVal = v
  548. atomic.StoreUint32(&vm.interrupted, 1)
  549. vm.interruptLock.Unlock()
  550. }
  551. func (vm *vm) ClearInterrupt() {
  552. atomic.StoreUint32(&vm.interrupted, 0)
  553. }
  554. func getFuncName(stack []Value, sb int) unistring.String {
  555. if sb > 0 {
  556. if f, ok := stack[sb-1].(*Object); ok {
  557. if _, isProxy := f.self.(*proxyObject); isProxy {
  558. return "proxy"
  559. }
  560. return nilSafe(f.self.getStr("name", nil)).string()
  561. }
  562. }
  563. return ""
  564. }
  565. func (vm *vm) captureStack(stack []StackFrame, ctxOffset int) []StackFrame {
  566. // Unroll the context stack
  567. if vm.prg != nil || vm.sb > 0 {
  568. var funcName unistring.String
  569. if vm.prg != nil {
  570. funcName = vm.prg.funcName
  571. } else {
  572. funcName = getFuncName(vm.stack, vm.sb)
  573. }
  574. stack = append(stack, StackFrame{prg: vm.prg, pc: vm.pc, funcName: funcName})
  575. }
  576. for i := len(vm.callStack) - 1; i > ctxOffset-1; i-- {
  577. frame := &vm.callStack[i]
  578. if frame.prg != nil || frame.sb > 0 {
  579. var funcName unistring.String
  580. if prg := frame.prg; prg != nil {
  581. funcName = prg.funcName
  582. } else {
  583. funcName = getFuncName(vm.stack, frame.sb)
  584. }
  585. stack = append(stack, StackFrame{prg: vm.callStack[i].prg, pc: frame.pc, funcName: funcName})
  586. }
  587. }
  588. if ctxOffset == 0 && vm.curAsyncRunner != nil {
  589. stack = vm.captureAsyncStack(stack, vm.curAsyncRunner)
  590. }
  591. return stack
  592. }
  593. func (vm *vm) captureAsyncStack(stack []StackFrame, runner *asyncRunner) []StackFrame {
  594. if promise, _ := runner.promiseCap.promise.self.(*Promise); promise != nil {
  595. if len(promise.fulfillReactions) == 1 {
  596. if r := promise.fulfillReactions[0].asyncRunner; r != nil {
  597. ctx := &r.gen.ctx
  598. if ctx.prg != nil || ctx.sb > 0 {
  599. var funcName unistring.String
  600. if prg := ctx.prg; prg != nil {
  601. funcName = prg.funcName
  602. } else {
  603. funcName = getFuncName(ctx.stack, 1)
  604. }
  605. stack = append(stack, StackFrame{prg: ctx.prg, pc: ctx.pc, funcName: funcName})
  606. }
  607. stack = vm.captureAsyncStack(stack, r)
  608. }
  609. }
  610. }
  611. return stack
  612. }
  613. func (vm *vm) pushTryFrame(catchPos, finallyPos int32) {
  614. vm.tryStack = append(vm.tryStack, tryFrame{
  615. callStackLen: uint32(len(vm.callStack)),
  616. iterLen: uint32(len(vm.iterStack)),
  617. refLen: uint32(len(vm.refStack)),
  618. sp: int32(vm.sp),
  619. stash: vm.stash,
  620. privEnv: vm.privEnv,
  621. catchPos: catchPos,
  622. finallyPos: finallyPos,
  623. finallyRet: -1,
  624. })
  625. }
  626. func (vm *vm) popTryFrame() {
  627. vm.tryStack = vm.tryStack[:len(vm.tryStack)-1]
  628. }
  629. func (vm *vm) restoreStacks(iterLen, refLen uint32) (ex *Exception) {
  630. // Restore other stacks
  631. iterTail := vm.iterStack[iterLen:]
  632. for i := len(iterTail) - 1; i >= 0; i-- {
  633. if iter := iterTail[i].iter; iter != nil {
  634. ex1 := vm.try(func() {
  635. iter.returnIter()
  636. })
  637. if ex1 != nil && ex == nil {
  638. ex = ex1
  639. }
  640. }
  641. iterTail[i] = iterStackItem{}
  642. }
  643. vm.iterStack = vm.iterStack[:iterLen]
  644. refTail := vm.refStack[refLen:]
  645. for i := range refTail {
  646. refTail[i] = nil
  647. }
  648. vm.refStack = vm.refStack[:refLen]
  649. return
  650. }
  651. func (vm *vm) handleThrow(arg interface{}) *Exception {
  652. ex := vm.exceptionFromValue(arg)
  653. for len(vm.tryStack) > 0 {
  654. tf := &vm.tryStack[len(vm.tryStack)-1]
  655. if tf.catchPos == -1 && tf.finallyPos == -1 || ex == nil && tf.catchPos != tryPanicMarker {
  656. tf.exception = nil
  657. vm.popTryFrame()
  658. continue
  659. }
  660. if int(tf.callStackLen) < len(vm.callStack) {
  661. ctx := &vm.callStack[tf.callStackLen]
  662. vm.prg, vm.newTarget, vm.result, vm.pc, vm.sb, vm.args =
  663. ctx.prg, ctx.newTarget, ctx.result, ctx.pc, ctx.sb, ctx.args
  664. vm.callStack = vm.callStack[:tf.callStackLen]
  665. }
  666. vm.sp = int(tf.sp)
  667. vm.stash = tf.stash
  668. vm.privEnv = tf.privEnv
  669. _ = vm.restoreStacks(tf.iterLen, tf.refLen)
  670. if tf.catchPos == tryPanicMarker {
  671. break
  672. }
  673. if tf.catchPos >= 0 {
  674. // exception is caught
  675. vm.push(ex.val)
  676. vm.pc = int(tf.catchPos)
  677. tf.catchPos = -1
  678. return nil
  679. }
  680. if tf.finallyPos >= 0 {
  681. // no 'catch' block, but there is a 'finally' block
  682. tf.exception = ex
  683. vm.pc = int(tf.finallyPos)
  684. tf.finallyPos = -1
  685. tf.finallyRet = -1
  686. return nil
  687. }
  688. }
  689. if ex == nil {
  690. panic(arg)
  691. }
  692. return ex
  693. }
  694. // Calls to this method must be made from the run() loop and must be the last statement before 'return'.
  695. // In all other cases exceptions must be thrown using panic().
  696. func (vm *vm) throw(v interface{}) {
  697. if ex := vm.handleThrow(v); ex != nil {
  698. panic(ex)
  699. }
  700. }
  701. func (vm *vm) try(f func()) (ex *Exception) {
  702. vm.pushTryFrame(tryPanicMarker, -1)
  703. defer vm.popTryFrame()
  704. defer func() {
  705. if x := recover(); x != nil {
  706. ex = vm.handleThrow(x)
  707. }
  708. }()
  709. f()
  710. return
  711. }
  712. func (vm *vm) runTry() (ex *Exception) {
  713. vm.pushTryFrame(tryPanicMarker, -1)
  714. defer vm.popTryFrame()
  715. for {
  716. ex = vm.runTryInner()
  717. if ex != nil || vm.halted() {
  718. return
  719. }
  720. }
  721. }
  722. func (vm *vm) runTryInner() (ex *Exception) {
  723. defer func() {
  724. if x := recover(); x != nil {
  725. ex = vm.handleThrow(x)
  726. }
  727. }()
  728. vm.run()
  729. return
  730. }
  731. func (vm *vm) push(v Value) {
  732. vm.stack.expand(vm.sp)
  733. vm.stack[vm.sp] = v
  734. vm.sp++
  735. }
  736. func (vm *vm) pop() Value {
  737. vm.sp--
  738. return vm.stack[vm.sp]
  739. }
  740. func (vm *vm) peek() Value {
  741. return vm.stack[vm.sp-1]
  742. }
  743. func (vm *vm) saveCtx(ctx *context) {
  744. ctx.prg, ctx.stash, ctx.privEnv, ctx.newTarget, ctx.result, ctx.pc, ctx.sb, ctx.args =
  745. vm.prg, vm.stash, vm.privEnv, vm.newTarget, vm.result, vm.pc, vm.sb, vm.args
  746. }
  747. func (vm *vm) pushCtx() {
  748. if len(vm.callStack) > vm.maxCallStackSize {
  749. ex := &StackOverflowError{}
  750. ex.stack = vm.captureStack(nil, 0)
  751. panic(ex)
  752. }
  753. vm.callStack = append(vm.callStack, context{})
  754. ctx := &vm.callStack[len(vm.callStack)-1]
  755. vm.saveCtx(ctx)
  756. }
  757. func (vm *vm) restoreCtx(ctx *context) {
  758. vm.prg, vm.stash, vm.privEnv, vm.newTarget, vm.result, vm.pc, vm.sb, vm.args =
  759. ctx.prg, ctx.stash, ctx.privEnv, ctx.newTarget, ctx.result, ctx.pc, ctx.sb, ctx.args
  760. }
  761. func (vm *vm) popCtx() {
  762. l := len(vm.callStack) - 1
  763. ctx := &vm.callStack[l]
  764. vm.restoreCtx(ctx)
  765. if ctx.prg != nil {
  766. *ctx = context{}
  767. }
  768. vm.callStack = vm.callStack[:l]
  769. }
  770. func (vm *vm) toCallee(v Value) *Object {
  771. if obj, ok := v.(*Object); ok {
  772. return obj
  773. }
  774. switch unresolved := v.(type) {
  775. case valueUnresolved:
  776. unresolved.throw()
  777. panic("Unreachable")
  778. case memberUnresolved:
  779. panic(vm.r.NewTypeError("Object has no member '%s'", unresolved.ref))
  780. }
  781. panic(vm.r.NewTypeError("Value is not an object: %s", v.toString()))
  782. }
  783. type loadVal uint32
  784. func (l loadVal) exec(vm *vm) {
  785. vm.push(vm.prg.values[l])
  786. vm.pc++
  787. }
  788. type _loadUndef struct{}
  789. var loadUndef _loadUndef
  790. func (_loadUndef) exec(vm *vm) {
  791. vm.push(_undefined)
  792. vm.pc++
  793. }
  794. type _loadNil struct{}
  795. var loadNil _loadNil
  796. func (_loadNil) exec(vm *vm) {
  797. vm.push(nil)
  798. vm.pc++
  799. }
  800. type _saveResult struct{}
  801. var saveResult _saveResult
  802. func (_saveResult) exec(vm *vm) {
  803. vm.sp--
  804. vm.result = vm.stack[vm.sp]
  805. vm.pc++
  806. }
  807. type _clearResult struct{}
  808. var clearResult _clearResult
  809. func (_clearResult) exec(vm *vm) {
  810. vm.result = _undefined
  811. vm.pc++
  812. }
  813. type _loadGlobalObject struct{}
  814. var loadGlobalObject _loadGlobalObject
  815. func (_loadGlobalObject) exec(vm *vm) {
  816. vm.push(vm.r.globalObject)
  817. vm.pc++
  818. }
  819. type loadStack int
  820. func (l loadStack) exec(vm *vm) {
  821. // l > 0 -- var<l-1>
  822. // l == 0 -- this
  823. if l > 0 {
  824. vm.push(nilSafe(vm.stack[vm.sb+vm.args+int(l)]))
  825. } else {
  826. vm.push(vm.stack[vm.sb])
  827. }
  828. vm.pc++
  829. }
  830. type loadStack1 int
  831. func (l loadStack1) exec(vm *vm) {
  832. // args are in stash
  833. // l > 0 -- var<l-1>
  834. // l == 0 -- this
  835. if l > 0 {
  836. vm.push(nilSafe(vm.stack[vm.sb+int(l)]))
  837. } else {
  838. vm.push(vm.stack[vm.sb])
  839. }
  840. vm.pc++
  841. }
  842. type loadStackLex int
  843. func (l loadStackLex) exec(vm *vm) {
  844. // l < 0 -- arg<-l-1>
  845. // l > 0 -- var<l-1>
  846. // l == 0 -- this
  847. var p *Value
  848. if l <= 0 {
  849. arg := int(-l)
  850. if arg > vm.args {
  851. vm.push(_undefined)
  852. vm.pc++
  853. return
  854. } else {
  855. p = &vm.stack[vm.sb+arg]
  856. }
  857. } else {
  858. p = &vm.stack[vm.sb+vm.args+int(l)]
  859. }
  860. if *p == nil {
  861. vm.throw(errAccessBeforeInit)
  862. return
  863. }
  864. vm.push(*p)
  865. vm.pc++
  866. }
  867. type loadStack1Lex int
  868. func (l loadStack1Lex) exec(vm *vm) {
  869. p := &vm.stack[vm.sb+int(l)]
  870. if *p == nil {
  871. vm.throw(errAccessBeforeInit)
  872. return
  873. }
  874. vm.push(*p)
  875. vm.pc++
  876. }
  877. type _loadCallee struct{}
  878. var loadCallee _loadCallee
  879. func (_loadCallee) exec(vm *vm) {
  880. vm.push(vm.stack[vm.sb-1])
  881. vm.pc++
  882. }
  883. func (vm *vm) storeStack(s int) {
  884. // l > 0 -- var<l-1>
  885. if s > 0 {
  886. vm.stack[vm.sb+vm.args+s] = vm.stack[vm.sp-1]
  887. } else {
  888. panic("Illegal stack var index")
  889. }
  890. vm.pc++
  891. }
  892. func (vm *vm) storeStack1(s int) {
  893. // args are in stash
  894. // l > 0 -- var<l-1>
  895. if s > 0 {
  896. vm.stack[vm.sb+s] = vm.stack[vm.sp-1]
  897. } else {
  898. panic("Illegal stack var index")
  899. }
  900. vm.pc++
  901. }
  902. func (vm *vm) storeStackLex(s int) {
  903. // l < 0 -- arg<-l-1>
  904. // l > 0 -- var<l-1>
  905. var p *Value
  906. if s < 0 {
  907. p = &vm.stack[vm.sb-s]
  908. } else {
  909. p = &vm.stack[vm.sb+vm.args+s]
  910. }
  911. if *p != nil {
  912. *p = vm.stack[vm.sp-1]
  913. } else {
  914. panic(errAccessBeforeInit)
  915. }
  916. vm.pc++
  917. }
  918. func (vm *vm) storeStack1Lex(s int) {
  919. // args are in stash
  920. // s > 0 -- var<l-1>
  921. if s <= 0 {
  922. panic("Illegal stack var index")
  923. }
  924. p := &vm.stack[vm.sb+s]
  925. if *p != nil {
  926. *p = vm.stack[vm.sp-1]
  927. } else {
  928. panic(errAccessBeforeInit)
  929. }
  930. vm.pc++
  931. }
  932. func (vm *vm) initStack(s int) {
  933. if s <= 0 {
  934. vm.stack[vm.sb-s] = vm.stack[vm.sp-1]
  935. } else {
  936. vm.stack[vm.sb+vm.args+s] = vm.stack[vm.sp-1]
  937. }
  938. vm.pc++
  939. }
  940. func (vm *vm) initStack1(s int) {
  941. if s <= 0 {
  942. panic("Illegal stack var index")
  943. }
  944. vm.stack[vm.sb+s] = vm.stack[vm.sp-1]
  945. vm.pc++
  946. }
  947. type storeStack int
  948. func (s storeStack) exec(vm *vm) {
  949. vm.storeStack(int(s))
  950. }
  951. type storeStack1 int
  952. func (s storeStack1) exec(vm *vm) {
  953. vm.storeStack1(int(s))
  954. }
  955. type storeStackLex int
  956. func (s storeStackLex) exec(vm *vm) {
  957. vm.storeStackLex(int(s))
  958. }
  959. type storeStack1Lex int
  960. func (s storeStack1Lex) exec(vm *vm) {
  961. vm.storeStack1Lex(int(s))
  962. }
  963. type initStack int
  964. func (s initStack) exec(vm *vm) {
  965. vm.initStack(int(s))
  966. }
  967. type initStackP int
  968. func (s initStackP) exec(vm *vm) {
  969. vm.initStack(int(s))
  970. vm.sp--
  971. }
  972. type initStack1 int
  973. func (s initStack1) exec(vm *vm) {
  974. vm.initStack1(int(s))
  975. }
  976. type initStack1P int
  977. func (s initStack1P) exec(vm *vm) {
  978. vm.initStack1(int(s))
  979. vm.sp--
  980. }
  981. type storeStackP int
  982. func (s storeStackP) exec(vm *vm) {
  983. vm.storeStack(int(s))
  984. vm.sp--
  985. }
  986. type storeStack1P int
  987. func (s storeStack1P) exec(vm *vm) {
  988. vm.storeStack1(int(s))
  989. vm.sp--
  990. }
  991. type storeStackLexP int
  992. func (s storeStackLexP) exec(vm *vm) {
  993. vm.storeStackLex(int(s))
  994. vm.sp--
  995. }
  996. type storeStack1LexP int
  997. func (s storeStack1LexP) exec(vm *vm) {
  998. vm.storeStack1Lex(int(s))
  999. vm.sp--
  1000. }
  1001. type _toNumber struct{}
  1002. var toNumber _toNumber
  1003. func (_toNumber) exec(vm *vm) {
  1004. vm.stack[vm.sp-1] = vm.stack[vm.sp-1].ToNumber()
  1005. vm.pc++
  1006. }
  1007. type _add struct{}
  1008. var add _add
  1009. func (_add) exec(vm *vm) {
  1010. right := vm.stack[vm.sp-1]
  1011. left := vm.stack[vm.sp-2]
  1012. if o, ok := left.(*Object); ok {
  1013. left = o.toPrimitive()
  1014. }
  1015. if o, ok := right.(*Object); ok {
  1016. right = o.toPrimitive()
  1017. }
  1018. var ret Value
  1019. leftString, isLeftString := left.(valueString)
  1020. rightString, isRightString := right.(valueString)
  1021. if isLeftString || isRightString {
  1022. if !isLeftString {
  1023. leftString = left.toString()
  1024. }
  1025. if !isRightString {
  1026. rightString = right.toString()
  1027. }
  1028. ret = leftString.concat(rightString)
  1029. } else {
  1030. if leftInt, ok := left.(valueInt); ok {
  1031. if rightInt, ok := right.(valueInt); ok {
  1032. ret = intToValue(int64(leftInt) + int64(rightInt))
  1033. } else {
  1034. ret = floatToValue(float64(leftInt) + right.ToFloat())
  1035. }
  1036. } else {
  1037. ret = floatToValue(left.ToFloat() + right.ToFloat())
  1038. }
  1039. }
  1040. vm.stack[vm.sp-2] = ret
  1041. vm.sp--
  1042. vm.pc++
  1043. }
  1044. type _sub struct{}
  1045. var sub _sub
  1046. func (_sub) exec(vm *vm) {
  1047. right := vm.stack[vm.sp-1]
  1048. left := vm.stack[vm.sp-2]
  1049. var result Value
  1050. if left, ok := left.(valueInt); ok {
  1051. if right, ok := right.(valueInt); ok {
  1052. result = intToValue(int64(left) - int64(right))
  1053. goto end
  1054. }
  1055. }
  1056. result = floatToValue(left.ToFloat() - right.ToFloat())
  1057. end:
  1058. vm.sp--
  1059. vm.stack[vm.sp-1] = result
  1060. vm.pc++
  1061. }
  1062. type _mul struct{}
  1063. var mul _mul
  1064. func (_mul) exec(vm *vm) {
  1065. left := vm.stack[vm.sp-2]
  1066. right := vm.stack[vm.sp-1]
  1067. var result Value
  1068. if left, ok := assertInt64(left); ok {
  1069. if right, ok := assertInt64(right); ok {
  1070. if left == 0 && right == -1 || left == -1 && right == 0 {
  1071. result = _negativeZero
  1072. goto end
  1073. }
  1074. res := left * right
  1075. // check for overflow
  1076. if left == 0 || right == 0 || res/left == right {
  1077. result = intToValue(res)
  1078. goto end
  1079. }
  1080. }
  1081. }
  1082. result = floatToValue(left.ToFloat() * right.ToFloat())
  1083. end:
  1084. vm.sp--
  1085. vm.stack[vm.sp-1] = result
  1086. vm.pc++
  1087. }
  1088. type _exp struct{}
  1089. var exp _exp
  1090. func (_exp) exec(vm *vm) {
  1091. vm.sp--
  1092. vm.stack[vm.sp-1] = pow(vm.stack[vm.sp-1], vm.stack[vm.sp])
  1093. vm.pc++
  1094. }
  1095. type _div struct{}
  1096. var div _div
  1097. func (_div) exec(vm *vm) {
  1098. left := vm.stack[vm.sp-2].ToFloat()
  1099. right := vm.stack[vm.sp-1].ToFloat()
  1100. var result Value
  1101. if math.IsNaN(left) || math.IsNaN(right) {
  1102. result = _NaN
  1103. goto end
  1104. }
  1105. if math.IsInf(left, 0) && math.IsInf(right, 0) {
  1106. result = _NaN
  1107. goto end
  1108. }
  1109. if left == 0 && right == 0 {
  1110. result = _NaN
  1111. goto end
  1112. }
  1113. if math.IsInf(left, 0) {
  1114. if math.Signbit(left) == math.Signbit(right) {
  1115. result = _positiveInf
  1116. goto end
  1117. } else {
  1118. result = _negativeInf
  1119. goto end
  1120. }
  1121. }
  1122. if math.IsInf(right, 0) {
  1123. if math.Signbit(left) == math.Signbit(right) {
  1124. result = _positiveZero
  1125. goto end
  1126. } else {
  1127. result = _negativeZero
  1128. goto end
  1129. }
  1130. }
  1131. if right == 0 {
  1132. if math.Signbit(left) == math.Signbit(right) {
  1133. result = _positiveInf
  1134. goto end
  1135. } else {
  1136. result = _negativeInf
  1137. goto end
  1138. }
  1139. }
  1140. result = floatToValue(left / right)
  1141. end:
  1142. vm.sp--
  1143. vm.stack[vm.sp-1] = result
  1144. vm.pc++
  1145. }
  1146. type _mod struct{}
  1147. var mod _mod
  1148. func (_mod) exec(vm *vm) {
  1149. left := vm.stack[vm.sp-2]
  1150. right := vm.stack[vm.sp-1]
  1151. var result Value
  1152. if leftInt, ok := assertInt64(left); ok {
  1153. if rightInt, ok := assertInt64(right); ok {
  1154. if rightInt == 0 {
  1155. result = _NaN
  1156. goto end
  1157. }
  1158. r := leftInt % rightInt
  1159. if r == 0 && leftInt < 0 {
  1160. result = _negativeZero
  1161. } else {
  1162. result = intToValue(leftInt % rightInt)
  1163. }
  1164. goto end
  1165. }
  1166. }
  1167. result = floatToValue(math.Mod(left.ToFloat(), right.ToFloat()))
  1168. end:
  1169. vm.sp--
  1170. vm.stack[vm.sp-1] = result
  1171. vm.pc++
  1172. }
  1173. type _neg struct{}
  1174. var neg _neg
  1175. func (_neg) exec(vm *vm) {
  1176. operand := vm.stack[vm.sp-1]
  1177. var result Value
  1178. if i, ok := assertInt64(operand); ok {
  1179. if i == 0 {
  1180. result = _negativeZero
  1181. } else {
  1182. result = valueInt(-i)
  1183. }
  1184. } else {
  1185. f := operand.ToFloat()
  1186. if !math.IsNaN(f) {
  1187. f = -f
  1188. }
  1189. result = valueFloat(f)
  1190. }
  1191. vm.stack[vm.sp-1] = result
  1192. vm.pc++
  1193. }
  1194. type _plus struct{}
  1195. var plus _plus
  1196. func (_plus) exec(vm *vm) {
  1197. vm.stack[vm.sp-1] = vm.stack[vm.sp-1].ToNumber()
  1198. vm.pc++
  1199. }
  1200. type _inc struct{}
  1201. var inc _inc
  1202. func (_inc) exec(vm *vm) {
  1203. v := vm.stack[vm.sp-1]
  1204. if i, ok := assertInt64(v); ok {
  1205. v = intToValue(i + 1)
  1206. goto end
  1207. }
  1208. v = valueFloat(v.ToFloat() + 1)
  1209. end:
  1210. vm.stack[vm.sp-1] = v
  1211. vm.pc++
  1212. }
  1213. type _dec struct{}
  1214. var dec _dec
  1215. func (_dec) exec(vm *vm) {
  1216. v := vm.stack[vm.sp-1]
  1217. if i, ok := assertInt64(v); ok {
  1218. v = intToValue(i - 1)
  1219. goto end
  1220. }
  1221. v = valueFloat(v.ToFloat() - 1)
  1222. end:
  1223. vm.stack[vm.sp-1] = v
  1224. vm.pc++
  1225. }
  1226. type _and struct{}
  1227. var and _and
  1228. func (_and) exec(vm *vm) {
  1229. left := toInt32(vm.stack[vm.sp-2])
  1230. right := toInt32(vm.stack[vm.sp-1])
  1231. vm.stack[vm.sp-2] = intToValue(int64(left & right))
  1232. vm.sp--
  1233. vm.pc++
  1234. }
  1235. type _or struct{}
  1236. var or _or
  1237. func (_or) exec(vm *vm) {
  1238. left := toInt32(vm.stack[vm.sp-2])
  1239. right := toInt32(vm.stack[vm.sp-1])
  1240. vm.stack[vm.sp-2] = intToValue(int64(left | right))
  1241. vm.sp--
  1242. vm.pc++
  1243. }
  1244. type _xor struct{}
  1245. var xor _xor
  1246. func (_xor) exec(vm *vm) {
  1247. left := toInt32(vm.stack[vm.sp-2])
  1248. right := toInt32(vm.stack[vm.sp-1])
  1249. vm.stack[vm.sp-2] = intToValue(int64(left ^ right))
  1250. vm.sp--
  1251. vm.pc++
  1252. }
  1253. type _bnot struct{}
  1254. var bnot _bnot
  1255. func (_bnot) exec(vm *vm) {
  1256. op := toInt32(vm.stack[vm.sp-1])
  1257. vm.stack[vm.sp-1] = intToValue(int64(^op))
  1258. vm.pc++
  1259. }
  1260. type _sal struct{}
  1261. var sal _sal
  1262. func (_sal) exec(vm *vm) {
  1263. left := toInt32(vm.stack[vm.sp-2])
  1264. right := toUint32(vm.stack[vm.sp-1])
  1265. vm.stack[vm.sp-2] = intToValue(int64(left << (right & 0x1F)))
  1266. vm.sp--
  1267. vm.pc++
  1268. }
  1269. type _sar struct{}
  1270. var sar _sar
  1271. func (_sar) exec(vm *vm) {
  1272. left := toInt32(vm.stack[vm.sp-2])
  1273. right := toUint32(vm.stack[vm.sp-1])
  1274. vm.stack[vm.sp-2] = intToValue(int64(left >> (right & 0x1F)))
  1275. vm.sp--
  1276. vm.pc++
  1277. }
  1278. type _shr struct{}
  1279. var shr _shr
  1280. func (_shr) exec(vm *vm) {
  1281. left := toUint32(vm.stack[vm.sp-2])
  1282. right := toUint32(vm.stack[vm.sp-1])
  1283. vm.stack[vm.sp-2] = intToValue(int64(left >> (right & 0x1F)))
  1284. vm.sp--
  1285. vm.pc++
  1286. }
  1287. type jump int32
  1288. func (j jump) exec(vm *vm) {
  1289. vm.pc += int(j)
  1290. }
  1291. type _toPropertyKey struct{}
  1292. func (_toPropertyKey) exec(vm *vm) {
  1293. p := vm.sp - 1
  1294. vm.stack[p] = toPropertyKey(vm.stack[p])
  1295. vm.pc++
  1296. }
  1297. type _toString struct{}
  1298. func (_toString) exec(vm *vm) {
  1299. p := vm.sp - 1
  1300. vm.stack[p] = vm.stack[p].toString()
  1301. vm.pc++
  1302. }
  1303. type _getElemRef struct{}
  1304. var getElemRef _getElemRef
  1305. func (_getElemRef) exec(vm *vm) {
  1306. obj := vm.stack[vm.sp-2].ToObject(vm.r)
  1307. propName := toPropertyKey(vm.stack[vm.sp-1])
  1308. vm.refStack = append(vm.refStack, &objRef{
  1309. base: obj,
  1310. name: propName.string(),
  1311. })
  1312. vm.sp -= 2
  1313. vm.pc++
  1314. }
  1315. type _getElemRefRecv struct{}
  1316. var getElemRefRecv _getElemRefRecv
  1317. func (_getElemRefRecv) exec(vm *vm) {
  1318. obj := vm.stack[vm.sp-1].ToObject(vm.r)
  1319. propName := toPropertyKey(vm.stack[vm.sp-2])
  1320. vm.refStack = append(vm.refStack, &objRef{
  1321. base: obj,
  1322. name: propName.string(),
  1323. this: vm.stack[vm.sp-3],
  1324. })
  1325. vm.sp -= 3
  1326. vm.pc++
  1327. }
  1328. type _getElemRefStrict struct{}
  1329. var getElemRefStrict _getElemRefStrict
  1330. func (_getElemRefStrict) exec(vm *vm) {
  1331. obj := vm.stack[vm.sp-2].ToObject(vm.r)
  1332. propName := toPropertyKey(vm.stack[vm.sp-1])
  1333. vm.refStack = append(vm.refStack, &objRef{
  1334. base: obj,
  1335. name: propName.string(),
  1336. strict: true,
  1337. })
  1338. vm.sp -= 2
  1339. vm.pc++
  1340. }
  1341. type _getElemRefRecvStrict struct{}
  1342. var getElemRefRecvStrict _getElemRefRecvStrict
  1343. func (_getElemRefRecvStrict) exec(vm *vm) {
  1344. obj := vm.stack[vm.sp-1].ToObject(vm.r)
  1345. propName := toPropertyKey(vm.stack[vm.sp-2])
  1346. vm.refStack = append(vm.refStack, &objRef{
  1347. base: obj,
  1348. name: propName.string(),
  1349. this: vm.stack[vm.sp-3],
  1350. strict: true,
  1351. })
  1352. vm.sp -= 3
  1353. vm.pc++
  1354. }
  1355. type _setElem struct{}
  1356. var setElem _setElem
  1357. func (_setElem) exec(vm *vm) {
  1358. obj := vm.stack[vm.sp-3].ToObject(vm.r)
  1359. propName := toPropertyKey(vm.stack[vm.sp-2])
  1360. val := vm.stack[vm.sp-1]
  1361. obj.setOwn(propName, val, false)
  1362. vm.sp -= 2
  1363. vm.stack[vm.sp-1] = val
  1364. vm.pc++
  1365. }
  1366. type _setElem1 struct{}
  1367. var setElem1 _setElem1
  1368. func (_setElem1) exec(vm *vm) {
  1369. obj := vm.stack[vm.sp-3].ToObject(vm.r)
  1370. propName := vm.stack[vm.sp-2]
  1371. val := vm.stack[vm.sp-1]
  1372. obj.setOwn(propName, val, true)
  1373. vm.sp -= 2
  1374. vm.pc++
  1375. }
  1376. type _setElem1Named struct{}
  1377. var setElem1Named _setElem1Named
  1378. func (_setElem1Named) exec(vm *vm) {
  1379. receiver := vm.stack[vm.sp-3]
  1380. base := receiver.ToObject(vm.r)
  1381. propName := vm.stack[vm.sp-2]
  1382. val := vm.stack[vm.sp-1]
  1383. vm.r.toObject(val).self.defineOwnPropertyStr("name", PropertyDescriptor{
  1384. Value: funcName("", propName),
  1385. Configurable: FLAG_TRUE,
  1386. }, true)
  1387. base.set(propName, val, receiver, true)
  1388. vm.sp -= 2
  1389. vm.pc++
  1390. }
  1391. type defineMethod struct {
  1392. enumerable bool
  1393. }
  1394. func (d *defineMethod) exec(vm *vm) {
  1395. obj := vm.r.toObject(vm.stack[vm.sp-3])
  1396. propName := vm.stack[vm.sp-2]
  1397. method := vm.r.toObject(vm.stack[vm.sp-1])
  1398. method.self.defineOwnPropertyStr("name", PropertyDescriptor{
  1399. Value: funcName("", propName),
  1400. Configurable: FLAG_TRUE,
  1401. }, true)
  1402. obj.defineOwnProperty(propName, PropertyDescriptor{
  1403. Value: method,
  1404. Writable: FLAG_TRUE,
  1405. Configurable: FLAG_TRUE,
  1406. Enumerable: ToFlag(d.enumerable),
  1407. }, true)
  1408. vm.sp -= 2
  1409. vm.pc++
  1410. }
  1411. type _setElemP struct{}
  1412. var setElemP _setElemP
  1413. func (_setElemP) exec(vm *vm) {
  1414. obj := vm.stack[vm.sp-3].ToObject(vm.r)
  1415. propName := toPropertyKey(vm.stack[vm.sp-2])
  1416. val := vm.stack[vm.sp-1]
  1417. obj.setOwn(propName, val, false)
  1418. vm.sp -= 3
  1419. vm.pc++
  1420. }
  1421. type _setElemStrict struct{}
  1422. var setElemStrict _setElemStrict
  1423. func (_setElemStrict) exec(vm *vm) {
  1424. propName := toPropertyKey(vm.stack[vm.sp-2])
  1425. receiver := vm.stack[vm.sp-3]
  1426. val := vm.stack[vm.sp-1]
  1427. if receiverObj, ok := receiver.(*Object); ok {
  1428. receiverObj.setOwn(propName, val, true)
  1429. } else {
  1430. base := receiver.ToObject(vm.r)
  1431. base.set(propName, val, receiver, true)
  1432. }
  1433. vm.sp -= 2
  1434. vm.stack[vm.sp-1] = val
  1435. vm.pc++
  1436. }
  1437. type _setElemRecv struct{}
  1438. var setElemRecv _setElemRecv
  1439. func (_setElemRecv) exec(vm *vm) {
  1440. receiver := vm.stack[vm.sp-4]
  1441. propName := toPropertyKey(vm.stack[vm.sp-3])
  1442. o := vm.stack[vm.sp-2]
  1443. val := vm.stack[vm.sp-1]
  1444. if obj, ok := o.(*Object); ok {
  1445. obj.set(propName, val, receiver, false)
  1446. } else {
  1447. base := o.ToObject(vm.r)
  1448. base.set(propName, val, receiver, false)
  1449. }
  1450. vm.sp -= 3
  1451. vm.stack[vm.sp-1] = val
  1452. vm.pc++
  1453. }
  1454. type _setElemRecvStrict struct{}
  1455. var setElemRecvStrict _setElemRecvStrict
  1456. func (_setElemRecvStrict) exec(vm *vm) {
  1457. receiver := vm.stack[vm.sp-4]
  1458. propName := toPropertyKey(vm.stack[vm.sp-3])
  1459. o := vm.stack[vm.sp-2]
  1460. val := vm.stack[vm.sp-1]
  1461. if obj, ok := o.(*Object); ok {
  1462. obj.set(propName, val, receiver, true)
  1463. } else {
  1464. base := o.ToObject(vm.r)
  1465. base.set(propName, val, receiver, true)
  1466. }
  1467. vm.sp -= 3
  1468. vm.stack[vm.sp-1] = val
  1469. vm.pc++
  1470. }
  1471. type _setElemStrictP struct{}
  1472. var setElemStrictP _setElemStrictP
  1473. func (_setElemStrictP) exec(vm *vm) {
  1474. propName := toPropertyKey(vm.stack[vm.sp-2])
  1475. receiver := vm.stack[vm.sp-3]
  1476. val := vm.stack[vm.sp-1]
  1477. if receiverObj, ok := receiver.(*Object); ok {
  1478. receiverObj.setOwn(propName, val, true)
  1479. } else {
  1480. base := receiver.ToObject(vm.r)
  1481. base.set(propName, val, receiver, true)
  1482. }
  1483. vm.sp -= 3
  1484. vm.pc++
  1485. }
  1486. type _setElemRecvP struct{}
  1487. var setElemRecvP _setElemRecvP
  1488. func (_setElemRecvP) exec(vm *vm) {
  1489. receiver := vm.stack[vm.sp-4]
  1490. propName := toPropertyKey(vm.stack[vm.sp-3])
  1491. o := vm.stack[vm.sp-2]
  1492. val := vm.stack[vm.sp-1]
  1493. if obj, ok := o.(*Object); ok {
  1494. obj.set(propName, val, receiver, false)
  1495. } else {
  1496. base := o.ToObject(vm.r)
  1497. base.set(propName, val, receiver, false)
  1498. }
  1499. vm.sp -= 4
  1500. vm.pc++
  1501. }
  1502. type _setElemRecvStrictP struct{}
  1503. var setElemRecvStrictP _setElemRecvStrictP
  1504. func (_setElemRecvStrictP) exec(vm *vm) {
  1505. receiver := vm.stack[vm.sp-4]
  1506. propName := toPropertyKey(vm.stack[vm.sp-3])
  1507. o := vm.stack[vm.sp-2]
  1508. val := vm.stack[vm.sp-1]
  1509. if obj, ok := o.(*Object); ok {
  1510. obj.set(propName, val, receiver, true)
  1511. } else {
  1512. base := o.ToObject(vm.r)
  1513. base.set(propName, val, receiver, true)
  1514. }
  1515. vm.sp -= 4
  1516. vm.pc++
  1517. }
  1518. type _deleteElem struct{}
  1519. var deleteElem _deleteElem
  1520. func (_deleteElem) exec(vm *vm) {
  1521. obj := vm.stack[vm.sp-2].ToObject(vm.r)
  1522. propName := toPropertyKey(vm.stack[vm.sp-1])
  1523. if obj.delete(propName, false) {
  1524. vm.stack[vm.sp-2] = valueTrue
  1525. } else {
  1526. vm.stack[vm.sp-2] = valueFalse
  1527. }
  1528. vm.sp--
  1529. vm.pc++
  1530. }
  1531. type _deleteElemStrict struct{}
  1532. var deleteElemStrict _deleteElemStrict
  1533. func (_deleteElemStrict) exec(vm *vm) {
  1534. obj := vm.stack[vm.sp-2].ToObject(vm.r)
  1535. propName := toPropertyKey(vm.stack[vm.sp-1])
  1536. obj.delete(propName, true)
  1537. vm.stack[vm.sp-2] = valueTrue
  1538. vm.sp--
  1539. vm.pc++
  1540. }
  1541. type deleteProp unistring.String
  1542. func (d deleteProp) exec(vm *vm) {
  1543. obj := vm.stack[vm.sp-1].ToObject(vm.r)
  1544. if obj.self.deleteStr(unistring.String(d), false) {
  1545. vm.stack[vm.sp-1] = valueTrue
  1546. } else {
  1547. vm.stack[vm.sp-1] = valueFalse
  1548. }
  1549. vm.pc++
  1550. }
  1551. type deletePropStrict unistring.String
  1552. func (d deletePropStrict) exec(vm *vm) {
  1553. obj := vm.stack[vm.sp-1].ToObject(vm.r)
  1554. obj.self.deleteStr(unistring.String(d), true)
  1555. vm.stack[vm.sp-1] = valueTrue
  1556. vm.pc++
  1557. }
  1558. type getPropRef unistring.String
  1559. func (p getPropRef) exec(vm *vm) {
  1560. vm.refStack = append(vm.refStack, &objRef{
  1561. base: vm.stack[vm.sp-1].ToObject(vm.r),
  1562. name: unistring.String(p),
  1563. })
  1564. vm.sp--
  1565. vm.pc++
  1566. }
  1567. type getPropRefRecv unistring.String
  1568. func (p getPropRefRecv) exec(vm *vm) {
  1569. vm.refStack = append(vm.refStack, &objRef{
  1570. this: vm.stack[vm.sp-2],
  1571. base: vm.stack[vm.sp-1].ToObject(vm.r),
  1572. name: unistring.String(p),
  1573. })
  1574. vm.sp -= 2
  1575. vm.pc++
  1576. }
  1577. type getPropRefStrict unistring.String
  1578. func (p getPropRefStrict) exec(vm *vm) {
  1579. vm.refStack = append(vm.refStack, &objRef{
  1580. base: vm.stack[vm.sp-1].ToObject(vm.r),
  1581. name: unistring.String(p),
  1582. strict: true,
  1583. })
  1584. vm.sp--
  1585. vm.pc++
  1586. }
  1587. type getPropRefRecvStrict unistring.String
  1588. func (p getPropRefRecvStrict) exec(vm *vm) {
  1589. vm.refStack = append(vm.refStack, &objRef{
  1590. this: vm.stack[vm.sp-2],
  1591. base: vm.stack[vm.sp-1].ToObject(vm.r),
  1592. name: unistring.String(p),
  1593. strict: true,
  1594. })
  1595. vm.sp -= 2
  1596. vm.pc++
  1597. }
  1598. type setProp unistring.String
  1599. func (p setProp) exec(vm *vm) {
  1600. val := vm.stack[vm.sp-1]
  1601. vm.stack[vm.sp-2].ToObject(vm.r).self.setOwnStr(unistring.String(p), val, false)
  1602. vm.stack[vm.sp-2] = val
  1603. vm.sp--
  1604. vm.pc++
  1605. }
  1606. type setPropP unistring.String
  1607. func (p setPropP) exec(vm *vm) {
  1608. val := vm.stack[vm.sp-1]
  1609. vm.stack[vm.sp-2].ToObject(vm.r).self.setOwnStr(unistring.String(p), val, false)
  1610. vm.sp -= 2
  1611. vm.pc++
  1612. }
  1613. type setPropStrict unistring.String
  1614. func (p setPropStrict) exec(vm *vm) {
  1615. receiver := vm.stack[vm.sp-2]
  1616. val := vm.stack[vm.sp-1]
  1617. propName := unistring.String(p)
  1618. if receiverObj, ok := receiver.(*Object); ok {
  1619. receiverObj.self.setOwnStr(propName, val, true)
  1620. } else {
  1621. base := receiver.ToObject(vm.r)
  1622. base.setStr(propName, val, receiver, true)
  1623. }
  1624. vm.stack[vm.sp-2] = val
  1625. vm.sp--
  1626. vm.pc++
  1627. }
  1628. type setPropRecv unistring.String
  1629. func (p setPropRecv) exec(vm *vm) {
  1630. receiver := vm.stack[vm.sp-3]
  1631. o := vm.stack[vm.sp-2]
  1632. val := vm.stack[vm.sp-1]
  1633. propName := unistring.String(p)
  1634. if obj, ok := o.(*Object); ok {
  1635. obj.setStr(propName, val, receiver, false)
  1636. } else {
  1637. base := o.ToObject(vm.r)
  1638. base.setStr(propName, val, receiver, false)
  1639. }
  1640. vm.stack[vm.sp-3] = val
  1641. vm.sp -= 2
  1642. vm.pc++
  1643. }
  1644. type setPropRecvStrict unistring.String
  1645. func (p setPropRecvStrict) exec(vm *vm) {
  1646. receiver := vm.stack[vm.sp-3]
  1647. o := vm.stack[vm.sp-2]
  1648. val := vm.stack[vm.sp-1]
  1649. propName := unistring.String(p)
  1650. if obj, ok := o.(*Object); ok {
  1651. obj.setStr(propName, val, receiver, true)
  1652. } else {
  1653. base := o.ToObject(vm.r)
  1654. base.setStr(propName, val, receiver, true)
  1655. }
  1656. vm.stack[vm.sp-3] = val
  1657. vm.sp -= 2
  1658. vm.pc++
  1659. }
  1660. type setPropRecvP unistring.String
  1661. func (p setPropRecvP) exec(vm *vm) {
  1662. receiver := vm.stack[vm.sp-3]
  1663. o := vm.stack[vm.sp-2]
  1664. val := vm.stack[vm.sp-1]
  1665. propName := unistring.String(p)
  1666. if obj, ok := o.(*Object); ok {
  1667. obj.setStr(propName, val, receiver, false)
  1668. } else {
  1669. base := o.ToObject(vm.r)
  1670. base.setStr(propName, val, receiver, false)
  1671. }
  1672. vm.sp -= 3
  1673. vm.pc++
  1674. }
  1675. type setPropRecvStrictP unistring.String
  1676. func (p setPropRecvStrictP) exec(vm *vm) {
  1677. receiver := vm.stack[vm.sp-3]
  1678. o := vm.stack[vm.sp-2]
  1679. val := vm.stack[vm.sp-1]
  1680. propName := unistring.String(p)
  1681. if obj, ok := o.(*Object); ok {
  1682. obj.setStr(propName, val, receiver, true)
  1683. } else {
  1684. base := o.ToObject(vm.r)
  1685. base.setStr(propName, val, receiver, true)
  1686. }
  1687. vm.sp -= 3
  1688. vm.pc++
  1689. }
  1690. type setPropStrictP unistring.String
  1691. func (p setPropStrictP) exec(vm *vm) {
  1692. receiver := vm.stack[vm.sp-2]
  1693. val := vm.stack[vm.sp-1]
  1694. propName := unistring.String(p)
  1695. if receiverObj, ok := receiver.(*Object); ok {
  1696. receiverObj.self.setOwnStr(propName, val, true)
  1697. } else {
  1698. base := receiver.ToObject(vm.r)
  1699. base.setStr(propName, val, receiver, true)
  1700. }
  1701. vm.sp -= 2
  1702. vm.pc++
  1703. }
  1704. type putProp unistring.String
  1705. func (p putProp) exec(vm *vm) {
  1706. vm.r.toObject(vm.stack[vm.sp-2]).self._putProp(unistring.String(p), vm.stack[vm.sp-1], true, true, true)
  1707. vm.sp--
  1708. vm.pc++
  1709. }
  1710. // used in class declarations instead of putProp because DefineProperty must be observable by Proxy
  1711. type definePropKeyed unistring.String
  1712. func (p definePropKeyed) exec(vm *vm) {
  1713. vm.r.toObject(vm.stack[vm.sp-2]).self.defineOwnPropertyStr(unistring.String(p), PropertyDescriptor{
  1714. Value: vm.stack[vm.sp-1],
  1715. Writable: FLAG_TRUE,
  1716. Configurable: FLAG_TRUE,
  1717. Enumerable: FLAG_TRUE,
  1718. }, true)
  1719. vm.sp--
  1720. vm.pc++
  1721. }
  1722. type defineProp struct{}
  1723. func (defineProp) exec(vm *vm) {
  1724. vm.r.toObject(vm.stack[vm.sp-3]).defineOwnProperty(vm.stack[vm.sp-2], PropertyDescriptor{
  1725. Value: vm.stack[vm.sp-1],
  1726. Writable: FLAG_TRUE,
  1727. Configurable: FLAG_TRUE,
  1728. Enumerable: FLAG_TRUE,
  1729. }, true)
  1730. vm.sp -= 2
  1731. vm.pc++
  1732. }
  1733. type defineMethodKeyed struct {
  1734. key unistring.String
  1735. enumerable bool
  1736. }
  1737. func (d *defineMethodKeyed) exec(vm *vm) {
  1738. obj := vm.r.toObject(vm.stack[vm.sp-2])
  1739. method := vm.r.toObject(vm.stack[vm.sp-1])
  1740. obj.self.defineOwnPropertyStr(d.key, PropertyDescriptor{
  1741. Value: method,
  1742. Writable: FLAG_TRUE,
  1743. Configurable: FLAG_TRUE,
  1744. Enumerable: ToFlag(d.enumerable),
  1745. }, true)
  1746. vm.sp--
  1747. vm.pc++
  1748. }
  1749. type _setProto struct{}
  1750. var setProto _setProto
  1751. func (_setProto) exec(vm *vm) {
  1752. vm.r.setObjectProto(vm.stack[vm.sp-2], vm.stack[vm.sp-1])
  1753. vm.sp--
  1754. vm.pc++
  1755. }
  1756. type defineGetterKeyed struct {
  1757. key unistring.String
  1758. enumerable bool
  1759. }
  1760. func (s *defineGetterKeyed) exec(vm *vm) {
  1761. obj := vm.r.toObject(vm.stack[vm.sp-2])
  1762. val := vm.stack[vm.sp-1]
  1763. method := vm.r.toObject(val)
  1764. method.self.defineOwnPropertyStr("name", PropertyDescriptor{
  1765. Value: asciiString("get ").concat(stringValueFromRaw(s.key)),
  1766. Configurable: FLAG_TRUE,
  1767. }, true)
  1768. descr := PropertyDescriptor{
  1769. Getter: val,
  1770. Configurable: FLAG_TRUE,
  1771. Enumerable: ToFlag(s.enumerable),
  1772. }
  1773. obj.self.defineOwnPropertyStr(s.key, descr, true)
  1774. vm.sp--
  1775. vm.pc++
  1776. }
  1777. type defineSetterKeyed struct {
  1778. key unistring.String
  1779. enumerable bool
  1780. }
  1781. func (s *defineSetterKeyed) exec(vm *vm) {
  1782. obj := vm.r.toObject(vm.stack[vm.sp-2])
  1783. val := vm.stack[vm.sp-1]
  1784. method := vm.r.toObject(val)
  1785. method.self.defineOwnPropertyStr("name", PropertyDescriptor{
  1786. Value: asciiString("set ").concat(stringValueFromRaw(s.key)),
  1787. Configurable: FLAG_TRUE,
  1788. }, true)
  1789. descr := PropertyDescriptor{
  1790. Setter: val,
  1791. Configurable: FLAG_TRUE,
  1792. Enumerable: ToFlag(s.enumerable),
  1793. }
  1794. obj.self.defineOwnPropertyStr(s.key, descr, true)
  1795. vm.sp--
  1796. vm.pc++
  1797. }
  1798. type defineGetter struct {
  1799. enumerable bool
  1800. }
  1801. func (s *defineGetter) exec(vm *vm) {
  1802. obj := vm.r.toObject(vm.stack[vm.sp-3])
  1803. propName := vm.stack[vm.sp-2]
  1804. val := vm.stack[vm.sp-1]
  1805. method := vm.r.toObject(val)
  1806. method.self.defineOwnPropertyStr("name", PropertyDescriptor{
  1807. Value: funcName("get ", propName),
  1808. Configurable: FLAG_TRUE,
  1809. }, true)
  1810. descr := PropertyDescriptor{
  1811. Getter: val,
  1812. Configurable: FLAG_TRUE,
  1813. Enumerable: ToFlag(s.enumerable),
  1814. }
  1815. obj.defineOwnProperty(propName, descr, true)
  1816. vm.sp -= 2
  1817. vm.pc++
  1818. }
  1819. type defineSetter struct {
  1820. enumerable bool
  1821. }
  1822. func (s *defineSetter) exec(vm *vm) {
  1823. obj := vm.r.toObject(vm.stack[vm.sp-3])
  1824. propName := vm.stack[vm.sp-2]
  1825. val := vm.stack[vm.sp-1]
  1826. method := vm.r.toObject(val)
  1827. method.self.defineOwnPropertyStr("name", PropertyDescriptor{
  1828. Value: funcName("set ", propName),
  1829. Configurable: FLAG_TRUE,
  1830. }, true)
  1831. descr := PropertyDescriptor{
  1832. Setter: val,
  1833. Configurable: FLAG_TRUE,
  1834. Enumerable: FLAG_TRUE,
  1835. }
  1836. obj.defineOwnProperty(propName, descr, true)
  1837. vm.sp -= 2
  1838. vm.pc++
  1839. }
  1840. type getProp unistring.String
  1841. func (g getProp) exec(vm *vm) {
  1842. v := vm.stack[vm.sp-1]
  1843. obj := v.baseObject(vm.r)
  1844. if obj == nil {
  1845. vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", g))
  1846. return
  1847. }
  1848. vm.stack[vm.sp-1] = nilSafe(obj.self.getStr(unistring.String(g), v))
  1849. vm.pc++
  1850. }
  1851. type getPropRecv unistring.String
  1852. func (g getPropRecv) exec(vm *vm) {
  1853. recv := vm.stack[vm.sp-2]
  1854. v := vm.stack[vm.sp-1]
  1855. obj := v.baseObject(vm.r)
  1856. if obj == nil {
  1857. vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", g))
  1858. return
  1859. }
  1860. vm.stack[vm.sp-2] = nilSafe(obj.self.getStr(unistring.String(g), recv))
  1861. vm.sp--
  1862. vm.pc++
  1863. }
  1864. type getPropRecvCallee unistring.String
  1865. func (g getPropRecvCallee) exec(vm *vm) {
  1866. recv := vm.stack[vm.sp-2]
  1867. v := vm.stack[vm.sp-1]
  1868. obj := v.baseObject(vm.r)
  1869. if obj == nil {
  1870. vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", g))
  1871. return
  1872. }
  1873. n := unistring.String(g)
  1874. prop := obj.self.getStr(n, recv)
  1875. if prop == nil {
  1876. prop = memberUnresolved{valueUnresolved{r: vm.r, ref: n}}
  1877. }
  1878. vm.stack[vm.sp-1] = prop
  1879. vm.pc++
  1880. }
  1881. type getPropCallee unistring.String
  1882. func (g getPropCallee) exec(vm *vm) {
  1883. v := vm.stack[vm.sp-1]
  1884. obj := v.baseObject(vm.r)
  1885. n := unistring.String(g)
  1886. if obj == nil {
  1887. vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined or null", n))
  1888. return
  1889. }
  1890. prop := obj.self.getStr(n, v)
  1891. if prop == nil {
  1892. prop = memberUnresolved{valueUnresolved{r: vm.r, ref: n}}
  1893. }
  1894. vm.push(prop)
  1895. vm.pc++
  1896. }
  1897. type _getElem struct{}
  1898. var getElem _getElem
  1899. func (_getElem) exec(vm *vm) {
  1900. v := vm.stack[vm.sp-2]
  1901. obj := v.baseObject(vm.r)
  1902. propName := toPropertyKey(vm.stack[vm.sp-1])
  1903. if obj == nil {
  1904. vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", propName.String()))
  1905. return
  1906. }
  1907. vm.stack[vm.sp-2] = nilSafe(obj.get(propName, v))
  1908. vm.sp--
  1909. vm.pc++
  1910. }
  1911. type _getElemRecv struct{}
  1912. var getElemRecv _getElemRecv
  1913. func (_getElemRecv) exec(vm *vm) {
  1914. recv := vm.stack[vm.sp-3]
  1915. propName := toPropertyKey(vm.stack[vm.sp-2])
  1916. v := vm.stack[vm.sp-1]
  1917. obj := v.baseObject(vm.r)
  1918. if obj == nil {
  1919. vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", propName.String()))
  1920. return
  1921. }
  1922. vm.stack[vm.sp-3] = nilSafe(obj.get(propName, recv))
  1923. vm.sp -= 2
  1924. vm.pc++
  1925. }
  1926. type _getKey struct{}
  1927. var getKey _getKey
  1928. func (_getKey) exec(vm *vm) {
  1929. v := vm.stack[vm.sp-2]
  1930. obj := v.baseObject(vm.r)
  1931. propName := vm.stack[vm.sp-1]
  1932. if obj == nil {
  1933. vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", propName.String()))
  1934. return
  1935. }
  1936. vm.stack[vm.sp-2] = nilSafe(obj.get(propName, v))
  1937. vm.sp--
  1938. vm.pc++
  1939. }
  1940. type _getElemCallee struct{}
  1941. var getElemCallee _getElemCallee
  1942. func (_getElemCallee) exec(vm *vm) {
  1943. v := vm.stack[vm.sp-2]
  1944. obj := v.baseObject(vm.r)
  1945. propName := toPropertyKey(vm.stack[vm.sp-1])
  1946. if obj == nil {
  1947. vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", propName.String()))
  1948. return
  1949. }
  1950. prop := obj.get(propName, v)
  1951. if prop == nil {
  1952. prop = memberUnresolved{valueUnresolved{r: vm.r, ref: propName.string()}}
  1953. }
  1954. vm.stack[vm.sp-1] = prop
  1955. vm.pc++
  1956. }
  1957. type _getElemRecvCallee struct{}
  1958. var getElemRecvCallee _getElemRecvCallee
  1959. func (_getElemRecvCallee) exec(vm *vm) {
  1960. recv := vm.stack[vm.sp-3]
  1961. v := vm.stack[vm.sp-2]
  1962. obj := v.baseObject(vm.r)
  1963. propName := toPropertyKey(vm.stack[vm.sp-1])
  1964. if obj == nil {
  1965. vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", propName.String()))
  1966. return
  1967. }
  1968. prop := obj.get(propName, recv)
  1969. if prop == nil {
  1970. prop = memberUnresolved{valueUnresolved{r: vm.r, ref: propName.string()}}
  1971. }
  1972. vm.stack[vm.sp-2] = prop
  1973. vm.sp--
  1974. vm.pc++
  1975. }
  1976. type _dup struct{}
  1977. var dup _dup
  1978. func (_dup) exec(vm *vm) {
  1979. vm.push(vm.stack[vm.sp-1])
  1980. vm.pc++
  1981. }
  1982. type dupN uint32
  1983. func (d dupN) exec(vm *vm) {
  1984. vm.push(vm.stack[vm.sp-1-int(d)])
  1985. vm.pc++
  1986. }
  1987. type rdupN uint32
  1988. func (d rdupN) exec(vm *vm) {
  1989. vm.stack[vm.sp-1-int(d)] = vm.stack[vm.sp-1]
  1990. vm.pc++
  1991. }
  1992. type dupLast uint32
  1993. func (d dupLast) exec(vm *vm) {
  1994. e := vm.sp + int(d)
  1995. vm.stack.expand(e)
  1996. copy(vm.stack[vm.sp:e], vm.stack[vm.sp-int(d):])
  1997. vm.sp = e
  1998. vm.pc++
  1999. }
  2000. type _newObject struct{}
  2001. var newObject _newObject
  2002. func (_newObject) exec(vm *vm) {
  2003. vm.push(vm.r.NewObject())
  2004. vm.pc++
  2005. }
  2006. type newArray uint32
  2007. func (l newArray) exec(vm *vm) {
  2008. values := make([]Value, 0, l)
  2009. vm.push(vm.r.newArrayValues(values))
  2010. vm.pc++
  2011. }
  2012. type _pushArrayItem struct{}
  2013. var pushArrayItem _pushArrayItem
  2014. func (_pushArrayItem) exec(vm *vm) {
  2015. arr := vm.stack[vm.sp-2].(*Object).self.(*arrayObject)
  2016. if arr.length < math.MaxUint32 {
  2017. arr.length++
  2018. } else {
  2019. vm.throw(vm.r.newError(vm.r.global.RangeError, "Invalid array length"))
  2020. return
  2021. }
  2022. val := vm.stack[vm.sp-1]
  2023. arr.values = append(arr.values, val)
  2024. if val != nil {
  2025. arr.objCount++
  2026. }
  2027. vm.sp--
  2028. vm.pc++
  2029. }
  2030. type _pushArraySpread struct{}
  2031. var pushArraySpread _pushArraySpread
  2032. func (_pushArraySpread) exec(vm *vm) {
  2033. arr := vm.stack[vm.sp-2].(*Object).self.(*arrayObject)
  2034. vm.r.getIterator(vm.stack[vm.sp-1], nil).iterate(func(val Value) {
  2035. if arr.length < math.MaxUint32 {
  2036. arr.length++
  2037. } else {
  2038. vm.throw(vm.r.newError(vm.r.global.RangeError, "Invalid array length"))
  2039. return
  2040. }
  2041. arr.values = append(arr.values, val)
  2042. arr.objCount++
  2043. })
  2044. vm.sp--
  2045. vm.pc++
  2046. }
  2047. type _pushSpread struct{}
  2048. var pushSpread _pushSpread
  2049. func (_pushSpread) exec(vm *vm) {
  2050. vm.sp--
  2051. obj := vm.stack[vm.sp]
  2052. vm.r.getIterator(obj, nil).iterate(func(val Value) {
  2053. vm.push(val)
  2054. })
  2055. vm.pc++
  2056. }
  2057. type _newArrayFromIter struct{}
  2058. var newArrayFromIter _newArrayFromIter
  2059. func (_newArrayFromIter) exec(vm *vm) {
  2060. var values []Value
  2061. l := len(vm.iterStack) - 1
  2062. iter := vm.iterStack[l].iter
  2063. vm.iterStack[l] = iterStackItem{}
  2064. vm.iterStack = vm.iterStack[:l]
  2065. if iter.iterator != nil {
  2066. iter.iterate(func(val Value) {
  2067. values = append(values, val)
  2068. })
  2069. }
  2070. vm.push(vm.r.newArrayValues(values))
  2071. vm.pc++
  2072. }
  2073. type newRegexp struct {
  2074. pattern *regexpPattern
  2075. src valueString
  2076. }
  2077. func (n *newRegexp) exec(vm *vm) {
  2078. vm.push(vm.r.newRegExpp(n.pattern.clone(), n.src, vm.r.global.RegExpPrototype).val)
  2079. vm.pc++
  2080. }
  2081. func (vm *vm) setLocalLex(s int) {
  2082. v := vm.stack[vm.sp-1]
  2083. level := s >> 24
  2084. idx := uint32(s & 0x00FFFFFF)
  2085. stash := vm.stash
  2086. for i := 0; i < level; i++ {
  2087. stash = stash.outer
  2088. }
  2089. p := &stash.values[idx]
  2090. if *p == nil {
  2091. panic(errAccessBeforeInit)
  2092. }
  2093. *p = v
  2094. vm.pc++
  2095. }
  2096. func (vm *vm) initLocal(s int) {
  2097. v := vm.stack[vm.sp-1]
  2098. level := s >> 24
  2099. idx := uint32(s & 0x00FFFFFF)
  2100. stash := vm.stash
  2101. for i := 0; i < level; i++ {
  2102. stash = stash.outer
  2103. }
  2104. stash.initByIdx(idx, v)
  2105. vm.pc++
  2106. }
  2107. type storeStash uint32
  2108. func (s storeStash) exec(vm *vm) {
  2109. vm.initLocal(int(s))
  2110. }
  2111. type storeStashP uint32
  2112. func (s storeStashP) exec(vm *vm) {
  2113. vm.initLocal(int(s))
  2114. vm.sp--
  2115. }
  2116. type storeStashLex uint32
  2117. func (s storeStashLex) exec(vm *vm) {
  2118. vm.setLocalLex(int(s))
  2119. }
  2120. type storeStashLexP uint32
  2121. func (s storeStashLexP) exec(vm *vm) {
  2122. vm.setLocalLex(int(s))
  2123. vm.sp--
  2124. }
  2125. type initStash uint32
  2126. func (s initStash) exec(vm *vm) {
  2127. vm.initLocal(int(s))
  2128. }
  2129. type initStashP uint32
  2130. func (s initStashP) exec(vm *vm) {
  2131. vm.initLocal(int(s))
  2132. vm.sp--
  2133. }
  2134. type initGlobalP unistring.String
  2135. func (s initGlobalP) exec(vm *vm) {
  2136. vm.sp--
  2137. vm.r.global.stash.initByName(unistring.String(s), vm.stack[vm.sp])
  2138. vm.pc++
  2139. }
  2140. type initGlobal unistring.String
  2141. func (s initGlobal) exec(vm *vm) {
  2142. vm.r.global.stash.initByName(unistring.String(s), vm.stack[vm.sp])
  2143. vm.pc++
  2144. }
  2145. type resolveVar1 unistring.String
  2146. func (s resolveVar1) exec(vm *vm) {
  2147. name := unistring.String(s)
  2148. var ref ref
  2149. for stash := vm.stash; stash != nil; stash = stash.outer {
  2150. ref = stash.getRefByName(name, false)
  2151. if ref != nil {
  2152. goto end
  2153. }
  2154. }
  2155. ref = &objRef{
  2156. base: vm.r.globalObject,
  2157. name: name,
  2158. binding: true,
  2159. }
  2160. end:
  2161. vm.refStack = append(vm.refStack, ref)
  2162. vm.pc++
  2163. }
  2164. type deleteVar unistring.String
  2165. func (d deleteVar) exec(vm *vm) {
  2166. name := unistring.String(d)
  2167. ret := true
  2168. for stash := vm.stash; stash != nil; stash = stash.outer {
  2169. if stash.obj != nil {
  2170. if stashObjHas(stash.obj, name) {
  2171. ret = stash.obj.self.deleteStr(name, false)
  2172. goto end
  2173. }
  2174. } else {
  2175. if idx, exists := stash.names[name]; exists {
  2176. if idx&(maskVar|maskDeletable) == maskVar|maskDeletable {
  2177. stash.deleteBinding(name)
  2178. } else {
  2179. ret = false
  2180. }
  2181. goto end
  2182. }
  2183. }
  2184. }
  2185. if vm.r.globalObject.self.hasPropertyStr(name) {
  2186. ret = vm.r.globalObject.self.deleteStr(name, false)
  2187. }
  2188. end:
  2189. if ret {
  2190. vm.push(valueTrue)
  2191. } else {
  2192. vm.push(valueFalse)
  2193. }
  2194. vm.pc++
  2195. }
  2196. type deleteGlobal unistring.String
  2197. func (d deleteGlobal) exec(vm *vm) {
  2198. name := unistring.String(d)
  2199. var ret bool
  2200. if vm.r.globalObject.self.hasPropertyStr(name) {
  2201. ret = vm.r.globalObject.self.deleteStr(name, false)
  2202. if ret {
  2203. delete(vm.r.global.varNames, name)
  2204. }
  2205. } else {
  2206. ret = true
  2207. }
  2208. if ret {
  2209. vm.push(valueTrue)
  2210. } else {
  2211. vm.push(valueFalse)
  2212. }
  2213. vm.pc++
  2214. }
  2215. type resolveVar1Strict unistring.String
  2216. func (s resolveVar1Strict) exec(vm *vm) {
  2217. name := unistring.String(s)
  2218. var ref ref
  2219. for stash := vm.stash; stash != nil; stash = stash.outer {
  2220. ref = stash.getRefByName(name, true)
  2221. if ref != nil {
  2222. goto end
  2223. }
  2224. }
  2225. if vm.r.globalObject.self.hasPropertyStr(name) {
  2226. ref = &objRef{
  2227. base: vm.r.globalObject,
  2228. name: name,
  2229. binding: true,
  2230. strict: true,
  2231. }
  2232. goto end
  2233. }
  2234. ref = &unresolvedRef{
  2235. runtime: vm.r,
  2236. name: name,
  2237. }
  2238. end:
  2239. vm.refStack = append(vm.refStack, ref)
  2240. vm.pc++
  2241. }
  2242. type setGlobal unistring.String
  2243. func (s setGlobal) exec(vm *vm) {
  2244. vm.r.setGlobal(unistring.String(s), vm.peek(), false)
  2245. vm.pc++
  2246. }
  2247. type setGlobalStrict unistring.String
  2248. func (s setGlobalStrict) exec(vm *vm) {
  2249. vm.r.setGlobal(unistring.String(s), vm.peek(), true)
  2250. vm.pc++
  2251. }
  2252. // Load a var from stash
  2253. type loadStash uint32
  2254. func (g loadStash) exec(vm *vm) {
  2255. level := int(g >> 24)
  2256. idx := uint32(g & 0x00FFFFFF)
  2257. stash := vm.stash
  2258. for i := 0; i < level; i++ {
  2259. stash = stash.outer
  2260. }
  2261. vm.push(nilSafe(stash.getByIdx(idx)))
  2262. vm.pc++
  2263. }
  2264. // Load a lexical binding from stash
  2265. type loadStashLex uint32
  2266. func (g loadStashLex) exec(vm *vm) {
  2267. level := int(g >> 24)
  2268. idx := uint32(g & 0x00FFFFFF)
  2269. stash := vm.stash
  2270. for i := 0; i < level; i++ {
  2271. stash = stash.outer
  2272. }
  2273. v := stash.getByIdx(idx)
  2274. if v == nil {
  2275. vm.throw(errAccessBeforeInit)
  2276. return
  2277. }
  2278. vm.push(v)
  2279. vm.pc++
  2280. }
  2281. // scan dynamic stashes up to the given level (encoded as 8 most significant bits of idx), if not found
  2282. // return the indexed var binding value from stash
  2283. type loadMixed struct {
  2284. name unistring.String
  2285. idx uint32
  2286. callee bool
  2287. }
  2288. func (g *loadMixed) exec(vm *vm) {
  2289. level := int(g.idx >> 24)
  2290. idx := g.idx & 0x00FFFFFF
  2291. stash := vm.stash
  2292. name := g.name
  2293. for i := 0; i < level; i++ {
  2294. if v, found := stash.getByName(name); found {
  2295. if g.callee {
  2296. if stash.obj != nil {
  2297. vm.push(stash.obj)
  2298. } else {
  2299. vm.push(_undefined)
  2300. }
  2301. }
  2302. vm.push(v)
  2303. goto end
  2304. }
  2305. stash = stash.outer
  2306. }
  2307. if g.callee {
  2308. vm.push(_undefined)
  2309. }
  2310. if stash != nil {
  2311. vm.push(nilSafe(stash.getByIdx(idx)))
  2312. }
  2313. end:
  2314. vm.pc++
  2315. }
  2316. // scan dynamic stashes up to the given level (encoded as 8 most significant bits of idx), if not found
  2317. // return the indexed lexical binding value from stash
  2318. type loadMixedLex loadMixed
  2319. func (g *loadMixedLex) exec(vm *vm) {
  2320. level := int(g.idx >> 24)
  2321. idx := g.idx & 0x00FFFFFF
  2322. stash := vm.stash
  2323. name := g.name
  2324. for i := 0; i < level; i++ {
  2325. if v, found := stash.getByName(name); found {
  2326. if g.callee {
  2327. if stash.obj != nil {
  2328. vm.push(stash.obj)
  2329. } else {
  2330. vm.push(_undefined)
  2331. }
  2332. }
  2333. vm.push(v)
  2334. goto end
  2335. }
  2336. stash = stash.outer
  2337. }
  2338. if g.callee {
  2339. vm.push(_undefined)
  2340. }
  2341. if stash != nil {
  2342. v := stash.getByIdx(idx)
  2343. if v == nil {
  2344. vm.throw(errAccessBeforeInit)
  2345. return
  2346. }
  2347. vm.push(v)
  2348. }
  2349. end:
  2350. vm.pc++
  2351. }
  2352. // scan dynamic stashes up to the given level (encoded as 8 most significant bits of idx), if not found
  2353. // return the indexed var binding value from stack
  2354. type loadMixedStack struct {
  2355. name unistring.String
  2356. idx int
  2357. level uint8
  2358. callee bool
  2359. }
  2360. // same as loadMixedStack, but the args have been moved to stash (therefore stack layout is different)
  2361. type loadMixedStack1 loadMixedStack
  2362. func (g *loadMixedStack) exec(vm *vm) {
  2363. stash := vm.stash
  2364. name := g.name
  2365. level := int(g.level)
  2366. for i := 0; i < level; i++ {
  2367. if v, found := stash.getByName(name); found {
  2368. if g.callee {
  2369. if stash.obj != nil {
  2370. vm.push(stash.obj)
  2371. } else {
  2372. vm.push(_undefined)
  2373. }
  2374. }
  2375. vm.push(v)
  2376. goto end
  2377. }
  2378. stash = stash.outer
  2379. }
  2380. if g.callee {
  2381. vm.push(_undefined)
  2382. }
  2383. loadStack(g.idx).exec(vm)
  2384. return
  2385. end:
  2386. vm.pc++
  2387. }
  2388. func (g *loadMixedStack1) exec(vm *vm) {
  2389. stash := vm.stash
  2390. name := g.name
  2391. level := int(g.level)
  2392. for i := 0; i < level; i++ {
  2393. if v, found := stash.getByName(name); found {
  2394. if g.callee {
  2395. if stash.obj != nil {
  2396. vm.push(stash.obj)
  2397. } else {
  2398. vm.push(_undefined)
  2399. }
  2400. }
  2401. vm.push(v)
  2402. goto end
  2403. }
  2404. stash = stash.outer
  2405. }
  2406. if g.callee {
  2407. vm.push(_undefined)
  2408. }
  2409. loadStack1(g.idx).exec(vm)
  2410. return
  2411. end:
  2412. vm.pc++
  2413. }
  2414. type loadMixedStackLex loadMixedStack
  2415. // same as loadMixedStackLex but when the arguments have been moved into stash
  2416. type loadMixedStack1Lex loadMixedStack
  2417. func (g *loadMixedStackLex) exec(vm *vm) {
  2418. stash := vm.stash
  2419. name := g.name
  2420. level := int(g.level)
  2421. for i := 0; i < level; i++ {
  2422. if v, found := stash.getByName(name); found {
  2423. if g.callee {
  2424. if stash.obj != nil {
  2425. vm.push(stash.obj)
  2426. } else {
  2427. vm.push(_undefined)
  2428. }
  2429. }
  2430. vm.push(v)
  2431. goto end
  2432. }
  2433. stash = stash.outer
  2434. }
  2435. if g.callee {
  2436. vm.push(_undefined)
  2437. }
  2438. loadStackLex(g.idx).exec(vm)
  2439. return
  2440. end:
  2441. vm.pc++
  2442. }
  2443. func (g *loadMixedStack1Lex) exec(vm *vm) {
  2444. stash := vm.stash
  2445. name := g.name
  2446. level := int(g.level)
  2447. for i := 0; i < level; i++ {
  2448. if v, found := stash.getByName(name); found {
  2449. if g.callee {
  2450. if stash.obj != nil {
  2451. vm.push(stash.obj)
  2452. } else {
  2453. vm.push(_undefined)
  2454. }
  2455. }
  2456. vm.push(v)
  2457. goto end
  2458. }
  2459. stash = stash.outer
  2460. }
  2461. if g.callee {
  2462. vm.push(_undefined)
  2463. }
  2464. loadStack1Lex(g.idx).exec(vm)
  2465. return
  2466. end:
  2467. vm.pc++
  2468. }
  2469. type resolveMixed struct {
  2470. name unistring.String
  2471. idx uint32
  2472. typ varType
  2473. strict bool
  2474. }
  2475. func newStashRef(typ varType, name unistring.String, v *[]Value, idx int) ref {
  2476. switch typ {
  2477. case varTypeVar:
  2478. return &stashRef{
  2479. n: name,
  2480. v: v,
  2481. idx: idx,
  2482. }
  2483. case varTypeLet:
  2484. return &stashRefLex{
  2485. stashRef: stashRef{
  2486. n: name,
  2487. v: v,
  2488. idx: idx,
  2489. },
  2490. }
  2491. case varTypeConst, varTypeStrictConst:
  2492. return &stashRefConst{
  2493. stashRefLex: stashRefLex{
  2494. stashRef: stashRef{
  2495. n: name,
  2496. v: v,
  2497. idx: idx,
  2498. },
  2499. },
  2500. strictConst: typ == varTypeStrictConst,
  2501. }
  2502. }
  2503. panic("unsupported var type")
  2504. }
  2505. func (r *resolveMixed) exec(vm *vm) {
  2506. level := int(r.idx >> 24)
  2507. idx := r.idx & 0x00FFFFFF
  2508. stash := vm.stash
  2509. var ref ref
  2510. for i := 0; i < level; i++ {
  2511. ref = stash.getRefByName(r.name, r.strict)
  2512. if ref != nil {
  2513. goto end
  2514. }
  2515. stash = stash.outer
  2516. }
  2517. if stash != nil {
  2518. ref = newStashRef(r.typ, r.name, &stash.values, int(idx))
  2519. goto end
  2520. }
  2521. ref = &unresolvedRef{
  2522. runtime: vm.r,
  2523. name: r.name,
  2524. }
  2525. end:
  2526. vm.refStack = append(vm.refStack, ref)
  2527. vm.pc++
  2528. }
  2529. type resolveMixedStack struct {
  2530. name unistring.String
  2531. idx int
  2532. typ varType
  2533. level uint8
  2534. strict bool
  2535. }
  2536. type resolveMixedStack1 resolveMixedStack
  2537. func (r *resolveMixedStack) exec(vm *vm) {
  2538. level := int(r.level)
  2539. stash := vm.stash
  2540. var ref ref
  2541. var idx int
  2542. for i := 0; i < level; i++ {
  2543. ref = stash.getRefByName(r.name, r.strict)
  2544. if ref != nil {
  2545. goto end
  2546. }
  2547. stash = stash.outer
  2548. }
  2549. if r.idx > 0 {
  2550. idx = vm.sb + vm.args + r.idx
  2551. } else {
  2552. idx = vm.sb - r.idx
  2553. }
  2554. ref = newStashRef(r.typ, r.name, (*[]Value)(&vm.stack), idx)
  2555. end:
  2556. vm.refStack = append(vm.refStack, ref)
  2557. vm.pc++
  2558. }
  2559. func (r *resolveMixedStack1) exec(vm *vm) {
  2560. level := int(r.level)
  2561. stash := vm.stash
  2562. var ref ref
  2563. for i := 0; i < level; i++ {
  2564. ref = stash.getRefByName(r.name, r.strict)
  2565. if ref != nil {
  2566. goto end
  2567. }
  2568. stash = stash.outer
  2569. }
  2570. ref = newStashRef(r.typ, r.name, (*[]Value)(&vm.stack), vm.sb+r.idx)
  2571. end:
  2572. vm.refStack = append(vm.refStack, ref)
  2573. vm.pc++
  2574. }
  2575. type _getValue struct{}
  2576. var getValue _getValue
  2577. func (_getValue) exec(vm *vm) {
  2578. ref := vm.refStack[len(vm.refStack)-1]
  2579. if v := ref.get(); v != nil {
  2580. vm.push(v)
  2581. } else {
  2582. vm.throw(vm.r.newReferenceError(ref.refname()))
  2583. return
  2584. }
  2585. vm.pc++
  2586. }
  2587. type _putValue struct{}
  2588. var putValue _putValue
  2589. func (_putValue) exec(vm *vm) {
  2590. l := len(vm.refStack) - 1
  2591. ref := vm.refStack[l]
  2592. vm.refStack[l] = nil
  2593. vm.refStack = vm.refStack[:l]
  2594. ref.set(vm.stack[vm.sp-1])
  2595. vm.pc++
  2596. }
  2597. type _putValueP struct{}
  2598. var putValueP _putValueP
  2599. func (_putValueP) exec(vm *vm) {
  2600. l := len(vm.refStack) - 1
  2601. ref := vm.refStack[l]
  2602. vm.refStack[l] = nil
  2603. vm.refStack = vm.refStack[:l]
  2604. ref.set(vm.stack[vm.sp-1])
  2605. vm.sp--
  2606. vm.pc++
  2607. }
  2608. type _initValueP struct{}
  2609. var initValueP _initValueP
  2610. func (_initValueP) exec(vm *vm) {
  2611. l := len(vm.refStack) - 1
  2612. ref := vm.refStack[l]
  2613. vm.refStack[l] = nil
  2614. vm.refStack = vm.refStack[:l]
  2615. ref.init(vm.stack[vm.sp-1])
  2616. vm.sp--
  2617. vm.pc++
  2618. }
  2619. type loadDynamic unistring.String
  2620. func (n loadDynamic) exec(vm *vm) {
  2621. name := unistring.String(n)
  2622. var val Value
  2623. for stash := vm.stash; stash != nil; stash = stash.outer {
  2624. if v, exists := stash.getByName(name); exists {
  2625. val = v
  2626. break
  2627. }
  2628. }
  2629. if val == nil {
  2630. val = vm.r.globalObject.self.getStr(name, nil)
  2631. if val == nil {
  2632. vm.throw(vm.r.newReferenceError(name))
  2633. return
  2634. }
  2635. }
  2636. vm.push(val)
  2637. vm.pc++
  2638. }
  2639. type loadDynamicRef unistring.String
  2640. func (n loadDynamicRef) exec(vm *vm) {
  2641. name := unistring.String(n)
  2642. var val Value
  2643. for stash := vm.stash; stash != nil; stash = stash.outer {
  2644. if v, exists := stash.getByName(name); exists {
  2645. val = v
  2646. break
  2647. }
  2648. }
  2649. if val == nil {
  2650. val = vm.r.globalObject.self.getStr(name, nil)
  2651. if val == nil {
  2652. val = valueUnresolved{r: vm.r, ref: name}
  2653. }
  2654. }
  2655. vm.push(val)
  2656. vm.pc++
  2657. }
  2658. type loadDynamicCallee unistring.String
  2659. func (n loadDynamicCallee) exec(vm *vm) {
  2660. name := unistring.String(n)
  2661. var val Value
  2662. var callee *Object
  2663. for stash := vm.stash; stash != nil; stash = stash.outer {
  2664. if v, exists := stash.getByName(name); exists {
  2665. callee = stash.obj
  2666. val = v
  2667. break
  2668. }
  2669. }
  2670. if val == nil {
  2671. val = vm.r.globalObject.self.getStr(name, nil)
  2672. if val == nil {
  2673. val = valueUnresolved{r: vm.r, ref: name}
  2674. }
  2675. }
  2676. if callee != nil {
  2677. vm.push(callee)
  2678. } else {
  2679. vm.push(_undefined)
  2680. }
  2681. vm.push(val)
  2682. vm.pc++
  2683. }
  2684. type _pop struct{}
  2685. var pop _pop
  2686. func (_pop) exec(vm *vm) {
  2687. vm.sp--
  2688. vm.pc++
  2689. }
  2690. func (vm *vm) callEval(n int, strict bool) {
  2691. if vm.r.toObject(vm.stack[vm.sp-n-1]) == vm.r.global.Eval {
  2692. if n > 0 {
  2693. srcVal := vm.stack[vm.sp-n]
  2694. if src, ok := srcVal.(valueString); ok {
  2695. ret := vm.r.eval(src, true, strict)
  2696. vm.stack[vm.sp-n-2] = ret
  2697. } else {
  2698. vm.stack[vm.sp-n-2] = srcVal
  2699. }
  2700. } else {
  2701. vm.stack[vm.sp-n-2] = _undefined
  2702. }
  2703. vm.sp -= n + 1
  2704. vm.pc++
  2705. } else {
  2706. call(n).exec(vm)
  2707. }
  2708. }
  2709. type callEval uint32
  2710. func (numargs callEval) exec(vm *vm) {
  2711. vm.callEval(int(numargs), false)
  2712. }
  2713. type callEvalStrict uint32
  2714. func (numargs callEvalStrict) exec(vm *vm) {
  2715. vm.callEval(int(numargs), true)
  2716. }
  2717. type _callEvalVariadic struct{}
  2718. var callEvalVariadic _callEvalVariadic
  2719. func (_callEvalVariadic) exec(vm *vm) {
  2720. vm.callEval(vm.countVariadicArgs()-2, false)
  2721. }
  2722. type _callEvalVariadicStrict struct{}
  2723. var callEvalVariadicStrict _callEvalVariadicStrict
  2724. func (_callEvalVariadicStrict) exec(vm *vm) {
  2725. vm.callEval(vm.countVariadicArgs()-2, true)
  2726. }
  2727. type _boxThis struct{}
  2728. var boxThis _boxThis
  2729. func (_boxThis) exec(vm *vm) {
  2730. v := vm.stack[vm.sb]
  2731. if v == _undefined || v == _null {
  2732. vm.stack[vm.sb] = vm.r.globalObject
  2733. } else {
  2734. vm.stack[vm.sb] = v.ToObject(vm.r)
  2735. }
  2736. vm.pc++
  2737. }
  2738. var variadicMarker Value = newSymbol(asciiString("[variadic marker]"))
  2739. type _startVariadic struct{}
  2740. var startVariadic _startVariadic
  2741. func (_startVariadic) exec(vm *vm) {
  2742. vm.push(variadicMarker)
  2743. vm.pc++
  2744. }
  2745. type _callVariadic struct{}
  2746. var callVariadic _callVariadic
  2747. func (vm *vm) countVariadicArgs() int {
  2748. count := 0
  2749. for i := vm.sp - 1; i >= 0; i-- {
  2750. if vm.stack[i] == variadicMarker {
  2751. return count
  2752. }
  2753. count++
  2754. }
  2755. panic("Variadic marker was not found. Compiler bug.")
  2756. }
  2757. func (_callVariadic) exec(vm *vm) {
  2758. call(vm.countVariadicArgs() - 2).exec(vm)
  2759. }
  2760. type _endVariadic struct{}
  2761. var endVariadic _endVariadic
  2762. func (_endVariadic) exec(vm *vm) {
  2763. vm.sp--
  2764. vm.stack[vm.sp-1] = vm.stack[vm.sp]
  2765. vm.pc++
  2766. }
  2767. type call uint32
  2768. func (numargs call) exec(vm *vm) {
  2769. // this
  2770. // callee
  2771. // arg0
  2772. // ...
  2773. // arg<numargs-1>
  2774. n := int(numargs)
  2775. v := vm.stack[vm.sp-n-1] // callee
  2776. obj := vm.toCallee(v)
  2777. obj.self.vmCall(vm, n)
  2778. }
  2779. func (vm *vm) clearStack() {
  2780. sp := vm.sp
  2781. stackTail := vm.stack[sp:]
  2782. for i := range stackTail {
  2783. stackTail[i] = nil
  2784. }
  2785. vm.stack = vm.stack[:sp]
  2786. }
  2787. type enterBlock struct {
  2788. names map[unistring.String]uint32
  2789. stashSize uint32
  2790. stackSize uint32
  2791. }
  2792. func (e *enterBlock) exec(vm *vm) {
  2793. if e.stashSize > 0 {
  2794. vm.newStash()
  2795. vm.stash.values = make([]Value, e.stashSize)
  2796. if len(e.names) > 0 {
  2797. vm.stash.names = e.names
  2798. }
  2799. }
  2800. ss := int(e.stackSize)
  2801. vm.stack.expand(vm.sp + ss - 1)
  2802. vv := vm.stack[vm.sp : vm.sp+ss]
  2803. for i := range vv {
  2804. vv[i] = nil
  2805. }
  2806. vm.sp += ss
  2807. vm.pc++
  2808. }
  2809. type enterCatchBlock struct {
  2810. names map[unistring.String]uint32
  2811. stashSize uint32
  2812. stackSize uint32
  2813. }
  2814. func (e *enterCatchBlock) exec(vm *vm) {
  2815. vm.newStash()
  2816. vm.stash.values = make([]Value, e.stashSize)
  2817. if len(e.names) > 0 {
  2818. vm.stash.names = e.names
  2819. }
  2820. vm.sp--
  2821. vm.stash.values[0] = vm.stack[vm.sp]
  2822. ss := int(e.stackSize)
  2823. vm.stack.expand(vm.sp + ss - 1)
  2824. vv := vm.stack[vm.sp : vm.sp+ss]
  2825. for i := range vv {
  2826. vv[i] = nil
  2827. }
  2828. vm.sp += ss
  2829. vm.pc++
  2830. }
  2831. type leaveBlock struct {
  2832. stackSize uint32
  2833. popStash bool
  2834. }
  2835. func (l *leaveBlock) exec(vm *vm) {
  2836. if l.popStash {
  2837. vm.stash = vm.stash.outer
  2838. }
  2839. if ss := l.stackSize; ss > 0 {
  2840. vm.sp -= int(ss)
  2841. }
  2842. vm.pc++
  2843. }
  2844. type enterFunc struct {
  2845. names map[unistring.String]uint32
  2846. stashSize uint32
  2847. stackSize uint32
  2848. numArgs uint32
  2849. funcType funcType
  2850. argsToStash bool
  2851. extensible bool
  2852. }
  2853. func (e *enterFunc) exec(vm *vm) {
  2854. // Input stack:
  2855. //
  2856. // callee
  2857. // this
  2858. // arg0
  2859. // ...
  2860. // argN
  2861. // <- sp
  2862. // Output stack:
  2863. //
  2864. // this <- sb
  2865. // <local stack vars...>
  2866. // <- sp
  2867. sp := vm.sp
  2868. vm.sb = sp - vm.args - 1
  2869. vm.newStash()
  2870. stash := vm.stash
  2871. stash.funcType = e.funcType
  2872. stash.values = make([]Value, e.stashSize)
  2873. if len(e.names) > 0 {
  2874. if e.extensible {
  2875. m := make(map[unistring.String]uint32, len(e.names))
  2876. for name, idx := range e.names {
  2877. m[name] = idx
  2878. }
  2879. stash.names = m
  2880. } else {
  2881. stash.names = e.names
  2882. }
  2883. }
  2884. ss := int(e.stackSize)
  2885. ea := 0
  2886. if e.argsToStash {
  2887. offset := vm.args - int(e.numArgs)
  2888. copy(stash.values, vm.stack[sp-vm.args:sp])
  2889. if offset > 0 {
  2890. vm.stash.extraArgs = make([]Value, offset)
  2891. copy(stash.extraArgs, vm.stack[sp-offset:])
  2892. } else {
  2893. vv := stash.values[vm.args:e.numArgs]
  2894. for i := range vv {
  2895. vv[i] = _undefined
  2896. }
  2897. }
  2898. sp -= vm.args
  2899. } else {
  2900. d := int(e.numArgs) - vm.args
  2901. if d > 0 {
  2902. ss += d
  2903. ea = d
  2904. vm.args = int(e.numArgs)
  2905. }
  2906. }
  2907. vm.stack.expand(sp + ss - 1)
  2908. if ea > 0 {
  2909. vv := vm.stack[sp : vm.sp+ea]
  2910. for i := range vv {
  2911. vv[i] = _undefined
  2912. }
  2913. }
  2914. vv := vm.stack[sp+ea : sp+ss]
  2915. for i := range vv {
  2916. vv[i] = nil
  2917. }
  2918. vm.sp = sp + ss
  2919. vm.pc++
  2920. }
  2921. // Similar to enterFunc, but for when arguments may be accessed before they are initialised,
  2922. // e.g. by an eval() code or from a closure, or from an earlier initialiser code.
  2923. // In this case the arguments remain on stack, first argsToCopy of them are copied to the stash.
  2924. type enterFunc1 struct {
  2925. names map[unistring.String]uint32
  2926. stashSize uint32
  2927. numArgs uint32
  2928. argsToCopy uint32
  2929. funcType funcType
  2930. extensible bool
  2931. }
  2932. func (e *enterFunc1) exec(vm *vm) {
  2933. sp := vm.sp
  2934. vm.sb = sp - vm.args - 1
  2935. vm.newStash()
  2936. stash := vm.stash
  2937. stash.funcType = e.funcType
  2938. stash.values = make([]Value, e.stashSize)
  2939. if len(e.names) > 0 {
  2940. if e.extensible {
  2941. m := make(map[unistring.String]uint32, len(e.names))
  2942. for name, idx := range e.names {
  2943. m[name] = idx
  2944. }
  2945. stash.names = m
  2946. } else {
  2947. stash.names = e.names
  2948. }
  2949. }
  2950. offset := vm.args - int(e.argsToCopy)
  2951. if offset > 0 {
  2952. copy(stash.values, vm.stack[sp-vm.args:sp-offset])
  2953. if offset := vm.args - int(e.numArgs); offset > 0 {
  2954. vm.stash.extraArgs = make([]Value, offset)
  2955. copy(stash.extraArgs, vm.stack[sp-offset:])
  2956. }
  2957. } else {
  2958. copy(stash.values, vm.stack[sp-vm.args:sp])
  2959. if int(e.argsToCopy) > vm.args {
  2960. vv := stash.values[vm.args:e.argsToCopy]
  2961. for i := range vv {
  2962. vv[i] = _undefined
  2963. }
  2964. }
  2965. }
  2966. vm.pc++
  2967. }
  2968. // Finalises the initialisers section and starts the function body which has its own
  2969. // scope. When used in conjunction with enterFunc1 adjustStack is set to true which
  2970. // causes the arguments to be removed from the stack.
  2971. type enterFuncBody struct {
  2972. enterBlock
  2973. funcType funcType
  2974. extensible bool
  2975. adjustStack bool
  2976. }
  2977. func (e *enterFuncBody) exec(vm *vm) {
  2978. if e.stashSize > 0 || e.extensible {
  2979. vm.newStash()
  2980. stash := vm.stash
  2981. stash.funcType = e.funcType
  2982. stash.values = make([]Value, e.stashSize)
  2983. if len(e.names) > 0 {
  2984. if e.extensible {
  2985. m := make(map[unistring.String]uint32, len(e.names))
  2986. for name, idx := range e.names {
  2987. m[name] = idx
  2988. }
  2989. stash.names = m
  2990. } else {
  2991. stash.names = e.names
  2992. }
  2993. }
  2994. }
  2995. sp := vm.sp
  2996. if e.adjustStack {
  2997. sp -= vm.args
  2998. }
  2999. nsp := sp + int(e.stackSize)
  3000. if e.stackSize > 0 {
  3001. vm.stack.expand(nsp - 1)
  3002. vv := vm.stack[sp:nsp]
  3003. for i := range vv {
  3004. vv[i] = nil
  3005. }
  3006. }
  3007. vm.sp = nsp
  3008. vm.pc++
  3009. }
  3010. type _ret struct{}
  3011. var ret _ret
  3012. func (_ret) exec(vm *vm) {
  3013. // callee -3
  3014. // this -2 <- sb
  3015. // retval -1
  3016. vm.stack[vm.sb-1] = vm.stack[vm.sp-1]
  3017. vm.sp = vm.sb
  3018. vm.popCtx()
  3019. vm.pc++
  3020. }
  3021. type cret uint32
  3022. func (c cret) exec(vm *vm) {
  3023. vm.stack[vm.sb] = *vm.getStashPtr(uint32(c))
  3024. ret.exec(vm)
  3025. }
  3026. type enterFuncStashless struct {
  3027. stackSize uint32
  3028. args uint32
  3029. }
  3030. func (e *enterFuncStashless) exec(vm *vm) {
  3031. sp := vm.sp
  3032. vm.sb = sp - vm.args - 1
  3033. d := int(e.args) - vm.args
  3034. if d > 0 {
  3035. ss := sp + int(e.stackSize) + d
  3036. vm.stack.expand(ss)
  3037. vv := vm.stack[sp : sp+d]
  3038. for i := range vv {
  3039. vv[i] = _undefined
  3040. }
  3041. vv = vm.stack[sp+d : ss]
  3042. for i := range vv {
  3043. vv[i] = nil
  3044. }
  3045. vm.args = int(e.args)
  3046. vm.sp = ss
  3047. } else {
  3048. if e.stackSize > 0 {
  3049. ss := sp + int(e.stackSize)
  3050. vm.stack.expand(ss)
  3051. vv := vm.stack[sp:ss]
  3052. for i := range vv {
  3053. vv[i] = nil
  3054. }
  3055. vm.sp = ss
  3056. }
  3057. }
  3058. vm.pc++
  3059. }
  3060. type newFuncInstruction interface {
  3061. getPrg() *Program
  3062. }
  3063. type newFunc struct {
  3064. prg *Program
  3065. name unistring.String
  3066. source string
  3067. length int
  3068. strict bool
  3069. }
  3070. func (n *newFunc) exec(vm *vm) {
  3071. obj := vm.r.newFunc(n.name, n.length, n.strict)
  3072. obj.prg = n.prg
  3073. obj.stash = vm.stash
  3074. obj.privEnv = vm.privEnv
  3075. obj.src = n.source
  3076. vm.push(obj.val)
  3077. vm.pc++
  3078. }
  3079. func (n *newFunc) getPrg() *Program {
  3080. return n.prg
  3081. }
  3082. type newAsyncFunc struct {
  3083. newFunc
  3084. }
  3085. func (n *newAsyncFunc) exec(vm *vm) {
  3086. obj := vm.r.newAsyncFunc(n.name, n.length, n.strict)
  3087. obj.prg = n.prg
  3088. obj.stash = vm.stash
  3089. obj.privEnv = vm.privEnv
  3090. obj.src = n.source
  3091. vm.push(obj.val)
  3092. vm.pc++
  3093. }
  3094. type newGeneratorFunc struct {
  3095. newFunc
  3096. }
  3097. func (n *newGeneratorFunc) exec(vm *vm) {
  3098. obj := vm.r.newGeneratorFunc(n.name, n.length, n.strict)
  3099. obj.prg = n.prg
  3100. obj.stash = vm.stash
  3101. obj.privEnv = vm.privEnv
  3102. obj.src = n.source
  3103. vm.push(obj.val)
  3104. vm.pc++
  3105. }
  3106. type newMethod struct {
  3107. newFunc
  3108. homeObjOffset uint32
  3109. }
  3110. func (n *newMethod) _exec(vm *vm, obj *methodFuncObject) {
  3111. obj.prg = n.prg
  3112. obj.stash = vm.stash
  3113. obj.privEnv = vm.privEnv
  3114. obj.src = n.source
  3115. if n.homeObjOffset > 0 {
  3116. obj.homeObject = vm.r.toObject(vm.stack[vm.sp-int(n.homeObjOffset)])
  3117. }
  3118. vm.push(obj.val)
  3119. vm.pc++
  3120. }
  3121. func (n *newMethod) exec(vm *vm) {
  3122. n._exec(vm, vm.r.newMethod(n.name, n.length, n.strict))
  3123. }
  3124. type newAsyncMethod struct {
  3125. newMethod
  3126. }
  3127. func (n *newAsyncMethod) exec(vm *vm) {
  3128. obj := vm.r.newAsyncMethod(n.name, n.length, n.strict)
  3129. n._exec(vm, &obj.methodFuncObject)
  3130. }
  3131. type newGeneratorMethod struct {
  3132. newMethod
  3133. }
  3134. func (n *newGeneratorMethod) exec(vm *vm) {
  3135. obj := vm.r.newGeneratorMethod(n.name, n.length, n.strict)
  3136. n._exec(vm, &obj.methodFuncObject)
  3137. }
  3138. type newArrowFunc struct {
  3139. newFunc
  3140. }
  3141. type newAsyncArrowFunc struct {
  3142. newArrowFunc
  3143. }
  3144. func getFuncObject(v Value) *Object {
  3145. if o, ok := v.(*Object); ok {
  3146. if fn, ok := o.self.(*arrowFuncObject); ok {
  3147. return fn.funcObj
  3148. }
  3149. return o
  3150. }
  3151. if v == _undefined {
  3152. return nil
  3153. }
  3154. panic(typeError("Value is not an Object"))
  3155. }
  3156. func getHomeObject(v Value) *Object {
  3157. if o, ok := v.(*Object); ok {
  3158. switch fn := o.self.(type) {
  3159. case *methodFuncObject:
  3160. return fn.homeObject
  3161. case *generatorMethodFuncObject:
  3162. return fn.homeObject
  3163. case *asyncMethodFuncObject:
  3164. return fn.homeObject
  3165. case *classFuncObject:
  3166. return o.runtime.toObject(fn.getStr("prototype", nil))
  3167. case *arrowFuncObject:
  3168. return getHomeObject(fn.funcObj)
  3169. case *asyncArrowFuncObject:
  3170. return getHomeObject(fn.funcObj)
  3171. }
  3172. }
  3173. panic(newTypeError("Compiler bug: getHomeObject() on the wrong value: %T", v))
  3174. }
  3175. func (n *newArrowFunc) _exec(vm *vm, obj *arrowFuncObject) {
  3176. obj.prg = n.prg
  3177. obj.stash = vm.stash
  3178. obj.privEnv = vm.privEnv
  3179. obj.src = n.source
  3180. if vm.sb > 0 {
  3181. obj.funcObj = getFuncObject(vm.stack[vm.sb-1])
  3182. }
  3183. vm.push(obj.val)
  3184. vm.pc++
  3185. }
  3186. func (n *newArrowFunc) exec(vm *vm) {
  3187. n._exec(vm, vm.r.newArrowFunc(n.name, n.length, n.strict))
  3188. }
  3189. func (n *newAsyncArrowFunc) exec(vm *vm) {
  3190. obj := vm.r.newAsyncArrowFunc(n.name, n.length, n.strict)
  3191. n._exec(vm, &obj.arrowFuncObject)
  3192. }
  3193. func (vm *vm) alreadyDeclared(name unistring.String) Value {
  3194. return vm.r.newError(vm.r.global.SyntaxError, "Identifier '%s' has already been declared", name)
  3195. }
  3196. func (vm *vm) checkBindVarsGlobal(names []unistring.String) {
  3197. o := vm.r.globalObject.self
  3198. sn := vm.r.global.stash.names
  3199. if bo, ok := o.(*baseObject); ok {
  3200. // shortcut
  3201. if bo.extensible {
  3202. for _, name := range names {
  3203. if _, exists := sn[name]; exists {
  3204. panic(vm.alreadyDeclared(name))
  3205. }
  3206. }
  3207. } else {
  3208. for _, name := range names {
  3209. if !bo.hasOwnPropertyStr(name) {
  3210. panic(vm.r.NewTypeError("Cannot define global variable '%s', global object is not extensible", name))
  3211. }
  3212. if _, exists := sn[name]; exists {
  3213. panic(vm.alreadyDeclared(name))
  3214. }
  3215. }
  3216. }
  3217. } else {
  3218. for _, name := range names {
  3219. if !o.hasOwnPropertyStr(name) && !o.isExtensible() {
  3220. panic(vm.r.NewTypeError("Cannot define global variable '%s', global object is not extensible", name))
  3221. }
  3222. if _, exists := sn[name]; exists {
  3223. panic(vm.alreadyDeclared(name))
  3224. }
  3225. }
  3226. }
  3227. }
  3228. func (vm *vm) createGlobalVarBindings(names []unistring.String, d bool) {
  3229. globalVarNames := vm.r.global.varNames
  3230. if globalVarNames == nil {
  3231. globalVarNames = make(map[unistring.String]struct{})
  3232. vm.r.global.varNames = globalVarNames
  3233. }
  3234. o := vm.r.globalObject.self
  3235. if bo, ok := o.(*baseObject); ok {
  3236. for _, name := range names {
  3237. if !bo.hasOwnPropertyStr(name) && bo.extensible {
  3238. bo._putProp(name, _undefined, true, true, d)
  3239. }
  3240. globalVarNames[name] = struct{}{}
  3241. }
  3242. } else {
  3243. var cf Flag
  3244. if d {
  3245. cf = FLAG_TRUE
  3246. } else {
  3247. cf = FLAG_FALSE
  3248. }
  3249. for _, name := range names {
  3250. if !o.hasOwnPropertyStr(name) && o.isExtensible() {
  3251. o.defineOwnPropertyStr(name, PropertyDescriptor{
  3252. Value: _undefined,
  3253. Writable: FLAG_TRUE,
  3254. Enumerable: FLAG_TRUE,
  3255. Configurable: cf,
  3256. }, true)
  3257. o.setOwnStr(name, _undefined, false)
  3258. }
  3259. globalVarNames[name] = struct{}{}
  3260. }
  3261. }
  3262. }
  3263. func (vm *vm) createGlobalFuncBindings(names []unistring.String, d bool) {
  3264. globalVarNames := vm.r.global.varNames
  3265. if globalVarNames == nil {
  3266. globalVarNames = make(map[unistring.String]struct{})
  3267. vm.r.global.varNames = globalVarNames
  3268. }
  3269. o := vm.r.globalObject.self
  3270. b := vm.sp - len(names)
  3271. var shortcutObj *baseObject
  3272. if o, ok := o.(*baseObject); ok {
  3273. shortcutObj = o
  3274. }
  3275. for i, name := range names {
  3276. var desc PropertyDescriptor
  3277. prop := o.getOwnPropStr(name)
  3278. desc.Value = vm.stack[b+i]
  3279. if shortcutObj != nil && prop == nil && shortcutObj.extensible {
  3280. shortcutObj._putProp(name, desc.Value, true, true, d)
  3281. } else {
  3282. if prop, ok := prop.(*valueProperty); ok && !prop.configurable {
  3283. // no-op
  3284. } else {
  3285. desc.Writable = FLAG_TRUE
  3286. desc.Enumerable = FLAG_TRUE
  3287. if d {
  3288. desc.Configurable = FLAG_TRUE
  3289. } else {
  3290. desc.Configurable = FLAG_FALSE
  3291. }
  3292. }
  3293. if shortcutObj != nil {
  3294. shortcutObj.defineOwnPropertyStr(name, desc, true)
  3295. } else {
  3296. o.defineOwnPropertyStr(name, desc, true)
  3297. o.setOwnStr(name, desc.Value, false) // not a bug, see https://262.ecma-international.org/#sec-createglobalfunctionbinding
  3298. }
  3299. }
  3300. globalVarNames[name] = struct{}{}
  3301. }
  3302. vm.sp = b
  3303. }
  3304. func (vm *vm) checkBindFuncsGlobal(names []unistring.String) {
  3305. o := vm.r.globalObject.self
  3306. sn := vm.r.global.stash.names
  3307. for _, name := range names {
  3308. if _, exists := sn[name]; exists {
  3309. panic(vm.alreadyDeclared(name))
  3310. }
  3311. prop := o.getOwnPropStr(name)
  3312. allowed := true
  3313. switch prop := prop.(type) {
  3314. case nil:
  3315. allowed = o.isExtensible()
  3316. case *valueProperty:
  3317. allowed = prop.configurable || prop.getterFunc == nil && prop.setterFunc == nil && prop.writable && prop.enumerable
  3318. }
  3319. if !allowed {
  3320. panic(vm.r.NewTypeError("Cannot redefine global function '%s'", name))
  3321. }
  3322. }
  3323. }
  3324. func (vm *vm) checkBindLexGlobal(names []unistring.String) {
  3325. o := vm.r.globalObject.self
  3326. s := &vm.r.global.stash
  3327. for _, name := range names {
  3328. if _, exists := vm.r.global.varNames[name]; exists {
  3329. goto fail
  3330. }
  3331. if _, exists := s.names[name]; exists {
  3332. goto fail
  3333. }
  3334. if prop, ok := o.getOwnPropStr(name).(*valueProperty); ok && !prop.configurable {
  3335. goto fail
  3336. }
  3337. continue
  3338. fail:
  3339. panic(vm.alreadyDeclared(name))
  3340. }
  3341. }
  3342. type bindVars struct {
  3343. names []unistring.String
  3344. deletable bool
  3345. }
  3346. func (d *bindVars) exec(vm *vm) {
  3347. var target *stash
  3348. for _, name := range d.names {
  3349. for s := vm.stash; s != nil; s = s.outer {
  3350. if idx, exists := s.names[name]; exists && idx&maskVar == 0 {
  3351. vm.throw(vm.alreadyDeclared(name))
  3352. return
  3353. }
  3354. if s.isVariable() {
  3355. target = s
  3356. break
  3357. }
  3358. }
  3359. }
  3360. if target == nil {
  3361. target = vm.stash
  3362. }
  3363. deletable := d.deletable
  3364. for _, name := range d.names {
  3365. target.createBinding(name, deletable)
  3366. }
  3367. vm.pc++
  3368. }
  3369. type bindGlobal struct {
  3370. vars, funcs, lets, consts []unistring.String
  3371. deletable bool
  3372. }
  3373. func (b *bindGlobal) exec(vm *vm) {
  3374. vm.checkBindFuncsGlobal(b.funcs)
  3375. vm.checkBindLexGlobal(b.lets)
  3376. vm.checkBindLexGlobal(b.consts)
  3377. vm.checkBindVarsGlobal(b.vars)
  3378. s := &vm.r.global.stash
  3379. for _, name := range b.lets {
  3380. s.createLexBinding(name, false)
  3381. }
  3382. for _, name := range b.consts {
  3383. s.createLexBinding(name, true)
  3384. }
  3385. vm.createGlobalFuncBindings(b.funcs, b.deletable)
  3386. vm.createGlobalVarBindings(b.vars, b.deletable)
  3387. vm.pc++
  3388. }
  3389. type jne int32
  3390. func (j jne) exec(vm *vm) {
  3391. vm.sp--
  3392. if !vm.stack[vm.sp].ToBoolean() {
  3393. vm.pc += int(j)
  3394. } else {
  3395. vm.pc++
  3396. }
  3397. }
  3398. type jeq int32
  3399. func (j jeq) exec(vm *vm) {
  3400. vm.sp--
  3401. if vm.stack[vm.sp].ToBoolean() {
  3402. vm.pc += int(j)
  3403. } else {
  3404. vm.pc++
  3405. }
  3406. }
  3407. type jeq1 int32
  3408. func (j jeq1) exec(vm *vm) {
  3409. if vm.stack[vm.sp-1].ToBoolean() {
  3410. vm.pc += int(j)
  3411. } else {
  3412. vm.sp--
  3413. vm.pc++
  3414. }
  3415. }
  3416. type jneq1 int32
  3417. func (j jneq1) exec(vm *vm) {
  3418. if !vm.stack[vm.sp-1].ToBoolean() {
  3419. vm.pc += int(j)
  3420. } else {
  3421. vm.sp--
  3422. vm.pc++
  3423. }
  3424. }
  3425. type jdef int32
  3426. func (j jdef) exec(vm *vm) {
  3427. if vm.stack[vm.sp-1] != _undefined {
  3428. vm.pc += int(j)
  3429. } else {
  3430. vm.sp--
  3431. vm.pc++
  3432. }
  3433. }
  3434. type jdefP int32
  3435. func (j jdefP) exec(vm *vm) {
  3436. if vm.stack[vm.sp-1] != _undefined {
  3437. vm.pc += int(j)
  3438. } else {
  3439. vm.pc++
  3440. }
  3441. vm.sp--
  3442. }
  3443. type jopt int32
  3444. func (j jopt) exec(vm *vm) {
  3445. switch vm.stack[vm.sp-1] {
  3446. case _null:
  3447. vm.stack[vm.sp-1] = _undefined
  3448. fallthrough
  3449. case _undefined:
  3450. vm.pc += int(j)
  3451. default:
  3452. vm.pc++
  3453. }
  3454. }
  3455. type joptc int32
  3456. func (j joptc) exec(vm *vm) {
  3457. switch vm.stack[vm.sp-1].(type) {
  3458. case valueNull, valueUndefined, memberUnresolved:
  3459. vm.sp--
  3460. vm.stack[vm.sp-1] = _undefined
  3461. vm.pc += int(j)
  3462. default:
  3463. vm.pc++
  3464. }
  3465. }
  3466. type jcoalesc int32
  3467. func (j jcoalesc) exec(vm *vm) {
  3468. switch vm.stack[vm.sp-1] {
  3469. case _undefined, _null:
  3470. vm.sp--
  3471. vm.pc++
  3472. default:
  3473. vm.pc += int(j)
  3474. }
  3475. }
  3476. type _not struct{}
  3477. var not _not
  3478. func (_not) exec(vm *vm) {
  3479. if vm.stack[vm.sp-1].ToBoolean() {
  3480. vm.stack[vm.sp-1] = valueFalse
  3481. } else {
  3482. vm.stack[vm.sp-1] = valueTrue
  3483. }
  3484. vm.pc++
  3485. }
  3486. func toPrimitiveNumber(v Value) Value {
  3487. if o, ok := v.(*Object); ok {
  3488. return o.toPrimitiveNumber()
  3489. }
  3490. return v
  3491. }
  3492. func toPrimitive(v Value) Value {
  3493. if o, ok := v.(*Object); ok {
  3494. return o.toPrimitive()
  3495. }
  3496. return v
  3497. }
  3498. func cmp(px, py Value) Value {
  3499. var ret bool
  3500. var nx, ny float64
  3501. if xs, ok := px.(valueString); ok {
  3502. if ys, ok := py.(valueString); ok {
  3503. ret = xs.compareTo(ys) < 0
  3504. goto end
  3505. }
  3506. }
  3507. if xi, ok := px.(valueInt); ok {
  3508. if yi, ok := py.(valueInt); ok {
  3509. ret = xi < yi
  3510. goto end
  3511. }
  3512. }
  3513. nx = px.ToFloat()
  3514. ny = py.ToFloat()
  3515. if math.IsNaN(nx) || math.IsNaN(ny) {
  3516. return _undefined
  3517. }
  3518. ret = nx < ny
  3519. end:
  3520. if ret {
  3521. return valueTrue
  3522. }
  3523. return valueFalse
  3524. }
  3525. type _op_lt struct{}
  3526. var op_lt _op_lt
  3527. func (_op_lt) exec(vm *vm) {
  3528. left := toPrimitiveNumber(vm.stack[vm.sp-2])
  3529. right := toPrimitiveNumber(vm.stack[vm.sp-1])
  3530. r := cmp(left, right)
  3531. if r == _undefined {
  3532. vm.stack[vm.sp-2] = valueFalse
  3533. } else {
  3534. vm.stack[vm.sp-2] = r
  3535. }
  3536. vm.sp--
  3537. vm.pc++
  3538. }
  3539. type _op_lte struct{}
  3540. var op_lte _op_lte
  3541. func (_op_lte) exec(vm *vm) {
  3542. left := toPrimitiveNumber(vm.stack[vm.sp-2])
  3543. right := toPrimitiveNumber(vm.stack[vm.sp-1])
  3544. r := cmp(right, left)
  3545. if r == _undefined || r == valueTrue {
  3546. vm.stack[vm.sp-2] = valueFalse
  3547. } else {
  3548. vm.stack[vm.sp-2] = valueTrue
  3549. }
  3550. vm.sp--
  3551. vm.pc++
  3552. }
  3553. type _op_gt struct{}
  3554. var op_gt _op_gt
  3555. func (_op_gt) exec(vm *vm) {
  3556. left := toPrimitiveNumber(vm.stack[vm.sp-2])
  3557. right := toPrimitiveNumber(vm.stack[vm.sp-1])
  3558. r := cmp(right, left)
  3559. if r == _undefined {
  3560. vm.stack[vm.sp-2] = valueFalse
  3561. } else {
  3562. vm.stack[vm.sp-2] = r
  3563. }
  3564. vm.sp--
  3565. vm.pc++
  3566. }
  3567. type _op_gte struct{}
  3568. var op_gte _op_gte
  3569. func (_op_gte) exec(vm *vm) {
  3570. left := toPrimitiveNumber(vm.stack[vm.sp-2])
  3571. right := toPrimitiveNumber(vm.stack[vm.sp-1])
  3572. r := cmp(left, right)
  3573. if r == _undefined || r == valueTrue {
  3574. vm.stack[vm.sp-2] = valueFalse
  3575. } else {
  3576. vm.stack[vm.sp-2] = valueTrue
  3577. }
  3578. vm.sp--
  3579. vm.pc++
  3580. }
  3581. type _op_eq struct{}
  3582. var op_eq _op_eq
  3583. func (_op_eq) exec(vm *vm) {
  3584. if vm.stack[vm.sp-2].Equals(vm.stack[vm.sp-1]) {
  3585. vm.stack[vm.sp-2] = valueTrue
  3586. } else {
  3587. vm.stack[vm.sp-2] = valueFalse
  3588. }
  3589. vm.sp--
  3590. vm.pc++
  3591. }
  3592. type _op_neq struct{}
  3593. var op_neq _op_neq
  3594. func (_op_neq) exec(vm *vm) {
  3595. if vm.stack[vm.sp-2].Equals(vm.stack[vm.sp-1]) {
  3596. vm.stack[vm.sp-2] = valueFalse
  3597. } else {
  3598. vm.stack[vm.sp-2] = valueTrue
  3599. }
  3600. vm.sp--
  3601. vm.pc++
  3602. }
  3603. type _op_strict_eq struct{}
  3604. var op_strict_eq _op_strict_eq
  3605. func (_op_strict_eq) exec(vm *vm) {
  3606. if vm.stack[vm.sp-2].StrictEquals(vm.stack[vm.sp-1]) {
  3607. vm.stack[vm.sp-2] = valueTrue
  3608. } else {
  3609. vm.stack[vm.sp-2] = valueFalse
  3610. }
  3611. vm.sp--
  3612. vm.pc++
  3613. }
  3614. type _op_strict_neq struct{}
  3615. var op_strict_neq _op_strict_neq
  3616. func (_op_strict_neq) exec(vm *vm) {
  3617. if vm.stack[vm.sp-2].StrictEquals(vm.stack[vm.sp-1]) {
  3618. vm.stack[vm.sp-2] = valueFalse
  3619. } else {
  3620. vm.stack[vm.sp-2] = valueTrue
  3621. }
  3622. vm.sp--
  3623. vm.pc++
  3624. }
  3625. type _op_instanceof struct{}
  3626. var op_instanceof _op_instanceof
  3627. func (_op_instanceof) exec(vm *vm) {
  3628. left := vm.stack[vm.sp-2]
  3629. right := vm.r.toObject(vm.stack[vm.sp-1])
  3630. if instanceOfOperator(left, right) {
  3631. vm.stack[vm.sp-2] = valueTrue
  3632. } else {
  3633. vm.stack[vm.sp-2] = valueFalse
  3634. }
  3635. vm.sp--
  3636. vm.pc++
  3637. }
  3638. type _op_in struct{}
  3639. var op_in _op_in
  3640. func (_op_in) exec(vm *vm) {
  3641. left := vm.stack[vm.sp-2]
  3642. right := vm.r.toObject(vm.stack[vm.sp-1])
  3643. if right.hasProperty(left) {
  3644. vm.stack[vm.sp-2] = valueTrue
  3645. } else {
  3646. vm.stack[vm.sp-2] = valueFalse
  3647. }
  3648. vm.sp--
  3649. vm.pc++
  3650. }
  3651. type try struct {
  3652. catchOffset int32
  3653. finallyOffset int32
  3654. }
  3655. func (t try) exec(vm *vm) {
  3656. var catchPos, finallyPos int32
  3657. if t.catchOffset > 0 {
  3658. catchPos = int32(vm.pc) + t.catchOffset
  3659. } else {
  3660. catchPos = -1
  3661. }
  3662. if t.finallyOffset > 0 {
  3663. finallyPos = int32(vm.pc) + t.finallyOffset
  3664. } else {
  3665. finallyPos = -1
  3666. }
  3667. vm.pushTryFrame(catchPos, finallyPos)
  3668. vm.pc++
  3669. }
  3670. type leaveTry struct{}
  3671. func (leaveTry) exec(vm *vm) {
  3672. tf := &vm.tryStack[len(vm.tryStack)-1]
  3673. if tf.finallyPos >= 0 {
  3674. tf.finallyRet = int32(vm.pc + 1)
  3675. vm.pc = int(tf.finallyPos)
  3676. tf.finallyPos = -1
  3677. tf.catchPos = -1
  3678. } else {
  3679. vm.popTryFrame()
  3680. vm.pc++
  3681. }
  3682. }
  3683. type enterFinally struct{}
  3684. func (enterFinally) exec(vm *vm) {
  3685. tf := &vm.tryStack[len(vm.tryStack)-1]
  3686. tf.finallyPos = -1
  3687. vm.pc++
  3688. }
  3689. type leaveFinally struct{}
  3690. func (leaveFinally) exec(vm *vm) {
  3691. tf := &vm.tryStack[len(vm.tryStack)-1]
  3692. ex, ret := tf.exception, tf.finallyRet
  3693. tf.exception = nil
  3694. vm.popTryFrame()
  3695. if ex != nil {
  3696. vm.throw(ex)
  3697. return
  3698. } else {
  3699. if ret != -1 {
  3700. vm.pc = int(ret)
  3701. } else {
  3702. vm.pc++
  3703. }
  3704. }
  3705. }
  3706. type _throw struct{}
  3707. var throw _throw
  3708. func (_throw) exec(vm *vm) {
  3709. v := vm.stack[vm.sp-1]
  3710. var ex *Exception
  3711. if o, ok := v.(*Object); ok {
  3712. if e, ok := o.self.(*errorObject); ok {
  3713. if len(e.stack) > 0 {
  3714. frame0 := e.stack[0]
  3715. // If the Error was created immediately before throwing it (i.e. 'throw new Error(....)')
  3716. // avoid capturing the stack again by the reusing the stack from the Error.
  3717. // These stacks would be almost identical and the difference doesn't matter for debugging.
  3718. if frame0.prg == vm.prg && vm.pc-frame0.pc == 1 {
  3719. ex = &Exception{
  3720. val: v,
  3721. stack: e.stack,
  3722. }
  3723. }
  3724. }
  3725. }
  3726. }
  3727. if ex == nil {
  3728. ex = &Exception{
  3729. val: v,
  3730. stack: vm.captureStack(make([]StackFrame, 0, len(vm.callStack)+1), 0),
  3731. }
  3732. }
  3733. if ex = vm.handleThrow(ex); ex != nil {
  3734. panic(ex)
  3735. }
  3736. }
  3737. type _newVariadic struct{}
  3738. var newVariadic _newVariadic
  3739. func (_newVariadic) exec(vm *vm) {
  3740. _new(vm.countVariadicArgs() - 1).exec(vm)
  3741. }
  3742. type _new uint32
  3743. func (n _new) exec(vm *vm) {
  3744. sp := vm.sp - int(n)
  3745. obj := vm.stack[sp-1]
  3746. ctor := vm.r.toConstructor(obj)
  3747. vm.stack[sp-1] = ctor(vm.stack[sp:vm.sp], nil)
  3748. vm.sp = sp
  3749. vm.pc++
  3750. }
  3751. type superCall uint32
  3752. func (s superCall) exec(vm *vm) {
  3753. l := len(vm.refStack) - 1
  3754. thisRef := vm.refStack[l]
  3755. vm.refStack[l] = nil
  3756. vm.refStack = vm.refStack[:l]
  3757. obj := vm.r.toObject(vm.stack[vm.sb-1])
  3758. var cls *classFuncObject
  3759. switch fn := obj.self.(type) {
  3760. case *classFuncObject:
  3761. cls = fn
  3762. case *arrowFuncObject:
  3763. cls, _ = fn.funcObj.self.(*classFuncObject)
  3764. }
  3765. if cls == nil {
  3766. vm.throw(vm.r.NewTypeError("wrong callee type for super()"))
  3767. return
  3768. }
  3769. sp := vm.sp - int(s)
  3770. newTarget := vm.r.toObject(vm.newTarget)
  3771. v := cls.createInstance(vm.stack[sp:vm.sp], newTarget)
  3772. thisRef.set(v)
  3773. vm.sp = sp
  3774. cls._initFields(v)
  3775. vm.push(v)
  3776. vm.pc++
  3777. }
  3778. type _superCallVariadic struct{}
  3779. var superCallVariadic _superCallVariadic
  3780. func (_superCallVariadic) exec(vm *vm) {
  3781. superCall(vm.countVariadicArgs()).exec(vm)
  3782. }
  3783. type _loadNewTarget struct{}
  3784. var loadNewTarget _loadNewTarget
  3785. func (_loadNewTarget) exec(vm *vm) {
  3786. if t := vm.newTarget; t != nil {
  3787. vm.push(t)
  3788. } else {
  3789. vm.push(_undefined)
  3790. }
  3791. vm.pc++
  3792. }
  3793. type _typeof struct{}
  3794. var typeof _typeof
  3795. func (_typeof) exec(vm *vm) {
  3796. var r Value
  3797. switch v := vm.stack[vm.sp-1].(type) {
  3798. case valueUndefined, valueUnresolved:
  3799. r = stringUndefined
  3800. case valueNull:
  3801. r = stringObjectC
  3802. case *Object:
  3803. r = v.self.typeOf()
  3804. case valueBool:
  3805. r = stringBoolean
  3806. case valueString:
  3807. r = stringString
  3808. case valueInt, valueFloat:
  3809. r = stringNumber
  3810. case *Symbol:
  3811. r = stringSymbol
  3812. default:
  3813. panic(newTypeError("Compiler bug: unknown type: %T", v))
  3814. }
  3815. vm.stack[vm.sp-1] = r
  3816. vm.pc++
  3817. }
  3818. type createArgsMapped uint32
  3819. func (formalArgs createArgsMapped) exec(vm *vm) {
  3820. v := &Object{runtime: vm.r}
  3821. args := &argumentsObject{}
  3822. args.extensible = true
  3823. args.prototype = vm.r.global.ObjectPrototype
  3824. args.class = "Arguments"
  3825. v.self = args
  3826. args.val = v
  3827. args.length = vm.args
  3828. args.init()
  3829. i := 0
  3830. c := int(formalArgs)
  3831. if vm.args < c {
  3832. c = vm.args
  3833. }
  3834. for ; i < c; i++ {
  3835. args._put(unistring.String(strconv.Itoa(i)), &mappedProperty{
  3836. valueProperty: valueProperty{
  3837. writable: true,
  3838. configurable: true,
  3839. enumerable: true,
  3840. },
  3841. v: &vm.stash.values[i],
  3842. })
  3843. }
  3844. for _, v := range vm.stash.extraArgs {
  3845. args._put(unistring.String(strconv.Itoa(i)), v)
  3846. i++
  3847. }
  3848. args._putProp("callee", vm.stack[vm.sb-1], true, false, true)
  3849. args._putSym(SymIterator, valueProp(vm.r.global.arrayValues, true, false, true))
  3850. vm.push(v)
  3851. vm.pc++
  3852. }
  3853. type createArgsUnmapped uint32
  3854. func (formalArgs createArgsUnmapped) exec(vm *vm) {
  3855. args := vm.r.newBaseObject(vm.r.global.ObjectPrototype, "Arguments")
  3856. i := 0
  3857. c := int(formalArgs)
  3858. if vm.args < c {
  3859. c = vm.args
  3860. }
  3861. for _, v := range vm.stash.values[:c] {
  3862. args._put(unistring.String(strconv.Itoa(i)), v)
  3863. i++
  3864. }
  3865. for _, v := range vm.stash.extraArgs {
  3866. args._put(unistring.String(strconv.Itoa(i)), v)
  3867. i++
  3868. }
  3869. args._putProp("length", intToValue(int64(vm.args)), true, false, true)
  3870. args._put("callee", vm.r.global.throwerProperty)
  3871. args._putSym(SymIterator, valueProp(vm.r.global.arrayValues, true, false, true))
  3872. vm.push(args.val)
  3873. vm.pc++
  3874. }
  3875. type _enterWith struct{}
  3876. var enterWith _enterWith
  3877. func (_enterWith) exec(vm *vm) {
  3878. vm.newStash()
  3879. vm.stash.obj = vm.stack[vm.sp-1].ToObject(vm.r)
  3880. vm.sp--
  3881. vm.pc++
  3882. }
  3883. type _leaveWith struct{}
  3884. var leaveWith _leaveWith
  3885. func (_leaveWith) exec(vm *vm) {
  3886. vm.stash = vm.stash.outer
  3887. vm.pc++
  3888. }
  3889. func emptyIter() (propIterItem, iterNextFunc) {
  3890. return propIterItem{}, nil
  3891. }
  3892. type _enumerate struct{}
  3893. var enumerate _enumerate
  3894. func (_enumerate) exec(vm *vm) {
  3895. v := vm.stack[vm.sp-1]
  3896. if v == _undefined || v == _null {
  3897. vm.iterStack = append(vm.iterStack, iterStackItem{f: emptyIter})
  3898. } else {
  3899. vm.iterStack = append(vm.iterStack, iterStackItem{f: enumerateRecursive(v.ToObject(vm.r))})
  3900. }
  3901. vm.sp--
  3902. vm.pc++
  3903. }
  3904. type enumNext int32
  3905. func (jmp enumNext) exec(vm *vm) {
  3906. l := len(vm.iterStack) - 1
  3907. item, n := vm.iterStack[l].f()
  3908. if n != nil {
  3909. vm.iterStack[l].val = item.name
  3910. vm.iterStack[l].f = n
  3911. vm.pc++
  3912. } else {
  3913. vm.pc += int(jmp)
  3914. }
  3915. }
  3916. type _enumGet struct{}
  3917. var enumGet _enumGet
  3918. func (_enumGet) exec(vm *vm) {
  3919. l := len(vm.iterStack) - 1
  3920. vm.push(vm.iterStack[l].val)
  3921. vm.pc++
  3922. }
  3923. type _enumPop struct{}
  3924. var enumPop _enumPop
  3925. func (_enumPop) exec(vm *vm) {
  3926. l := len(vm.iterStack) - 1
  3927. vm.iterStack[l] = iterStackItem{}
  3928. vm.iterStack = vm.iterStack[:l]
  3929. vm.pc++
  3930. }
  3931. type _enumPopClose struct{}
  3932. var enumPopClose _enumPopClose
  3933. func (_enumPopClose) exec(vm *vm) {
  3934. l := len(vm.iterStack) - 1
  3935. item := vm.iterStack[l]
  3936. vm.iterStack[l] = iterStackItem{}
  3937. vm.iterStack = vm.iterStack[:l]
  3938. if iter := item.iter; iter != nil {
  3939. iter.returnIter()
  3940. }
  3941. vm.pc++
  3942. }
  3943. type _iterateP struct{}
  3944. var iterateP _iterateP
  3945. func (_iterateP) exec(vm *vm) {
  3946. iter := vm.r.getIterator(vm.stack[vm.sp-1], nil)
  3947. vm.iterStack = append(vm.iterStack, iterStackItem{iter: iter})
  3948. vm.sp--
  3949. vm.pc++
  3950. }
  3951. type _iterate struct{}
  3952. var iterate _iterate
  3953. func (_iterate) exec(vm *vm) {
  3954. iter := vm.r.getIterator(vm.stack[vm.sp-1], nil)
  3955. vm.iterStack = append(vm.iterStack, iterStackItem{iter: iter})
  3956. vm.pc++
  3957. }
  3958. type iterNext int32
  3959. func (jmp iterNext) exec(vm *vm) {
  3960. l := len(vm.iterStack) - 1
  3961. iter := vm.iterStack[l].iter
  3962. value, ex := iter.step()
  3963. if ex == nil {
  3964. if value == nil {
  3965. vm.pc += int(jmp)
  3966. } else {
  3967. vm.iterStack[l].val = value
  3968. vm.pc++
  3969. }
  3970. } else {
  3971. l := len(vm.iterStack) - 1
  3972. vm.iterStack[l] = iterStackItem{}
  3973. vm.iterStack = vm.iterStack[:l]
  3974. vm.throw(ex.val)
  3975. return
  3976. }
  3977. }
  3978. type iterGetNextOrUndef struct{}
  3979. func (iterGetNextOrUndef) exec(vm *vm) {
  3980. l := len(vm.iterStack) - 1
  3981. iter := vm.iterStack[l].iter
  3982. var value Value
  3983. if iter.iterator != nil {
  3984. var ex *Exception
  3985. value, ex = iter.step()
  3986. if ex != nil {
  3987. l := len(vm.iterStack) - 1
  3988. vm.iterStack[l] = iterStackItem{}
  3989. vm.iterStack = vm.iterStack[:l]
  3990. vm.throw(ex.val)
  3991. return
  3992. }
  3993. }
  3994. vm.push(nilSafe(value))
  3995. vm.pc++
  3996. }
  3997. type copyStash struct{}
  3998. func (copyStash) exec(vm *vm) {
  3999. oldStash := vm.stash
  4000. newStash := &stash{
  4001. outer: oldStash.outer,
  4002. }
  4003. vm.stashAllocs++
  4004. newStash.values = append([]Value(nil), oldStash.values...)
  4005. newStash.names = oldStash.names
  4006. vm.stash = newStash
  4007. vm.pc++
  4008. }
  4009. type _throwAssignToConst struct{}
  4010. var throwAssignToConst _throwAssignToConst
  4011. func (_throwAssignToConst) exec(vm *vm) {
  4012. vm.throw(errAssignToConst)
  4013. }
  4014. func (r *Runtime) copyDataProperties(target, source Value) {
  4015. targetObj := r.toObject(target)
  4016. if source == _null || source == _undefined {
  4017. return
  4018. }
  4019. sourceObj := source.ToObject(r)
  4020. for item, next := iterateEnumerableProperties(sourceObj)(); next != nil; item, next = next() {
  4021. createDataPropertyOrThrow(targetObj, item.name, item.value)
  4022. }
  4023. }
  4024. type _copySpread struct{}
  4025. var copySpread _copySpread
  4026. func (_copySpread) exec(vm *vm) {
  4027. vm.r.copyDataProperties(vm.stack[vm.sp-2], vm.stack[vm.sp-1])
  4028. vm.sp--
  4029. vm.pc++
  4030. }
  4031. type _copyRest struct{}
  4032. var copyRest _copyRest
  4033. func (_copyRest) exec(vm *vm) {
  4034. vm.push(vm.r.NewObject())
  4035. vm.r.copyDataProperties(vm.stack[vm.sp-1], vm.stack[vm.sp-2])
  4036. vm.pc++
  4037. }
  4038. type _createDestructSrc struct{}
  4039. var createDestructSrc _createDestructSrc
  4040. func (_createDestructSrc) exec(vm *vm) {
  4041. v := vm.stack[vm.sp-1]
  4042. vm.r.checkObjectCoercible(v)
  4043. vm.push(vm.r.newDestructKeyedSource(v))
  4044. vm.pc++
  4045. }
  4046. type _checkObjectCoercible struct{}
  4047. var checkObjectCoercible _checkObjectCoercible
  4048. func (_checkObjectCoercible) exec(vm *vm) {
  4049. vm.r.checkObjectCoercible(vm.stack[vm.sp-1])
  4050. vm.pc++
  4051. }
  4052. type createArgsRestStack int
  4053. func (n createArgsRestStack) exec(vm *vm) {
  4054. var values []Value
  4055. delta := vm.args - int(n)
  4056. if delta > 0 {
  4057. values = make([]Value, delta)
  4058. copy(values, vm.stack[vm.sb+int(n)+1:])
  4059. }
  4060. vm.push(vm.r.newArrayValues(values))
  4061. vm.pc++
  4062. }
  4063. type _createArgsRestStash struct{}
  4064. var createArgsRestStash _createArgsRestStash
  4065. func (_createArgsRestStash) exec(vm *vm) {
  4066. vm.push(vm.r.newArrayValues(vm.stash.extraArgs))
  4067. vm.stash.extraArgs = nil
  4068. vm.pc++
  4069. }
  4070. type concatStrings int
  4071. func (n concatStrings) exec(vm *vm) {
  4072. strs := vm.stack[vm.sp-int(n) : vm.sp]
  4073. length := 0
  4074. allAscii := true
  4075. for i, s := range strs {
  4076. switch s := s.(type) {
  4077. case asciiString:
  4078. length += s.length()
  4079. case unicodeString:
  4080. length += s.length()
  4081. allAscii = false
  4082. case *importedString:
  4083. s.ensureScanned()
  4084. if s.u != nil {
  4085. strs[i] = s.u
  4086. length += s.u.length()
  4087. allAscii = false
  4088. } else {
  4089. strs[i] = asciiString(s.s)
  4090. length += len(s.s)
  4091. }
  4092. default:
  4093. panic(unknownStringTypeErr(s))
  4094. }
  4095. }
  4096. vm.sp -= int(n) - 1
  4097. if allAscii {
  4098. var buf strings.Builder
  4099. buf.Grow(length)
  4100. for _, s := range strs {
  4101. buf.WriteString(string(s.(asciiString)))
  4102. }
  4103. vm.stack[vm.sp-1] = asciiString(buf.String())
  4104. } else {
  4105. var buf unicodeStringBuilder
  4106. buf.Grow(length)
  4107. for _, s := range strs {
  4108. buf.WriteString(s.(valueString))
  4109. }
  4110. vm.stack[vm.sp-1] = buf.String()
  4111. }
  4112. vm.pc++
  4113. }
  4114. type getTaggedTmplObject struct {
  4115. raw, cooked []Value
  4116. }
  4117. // As tagged template objects are not cached (because it's hard to ensure the cache is cleaned without using
  4118. // finalizers) this wrapper is needed to override the equality method so that two objects for the same template
  4119. // literal appeared to be equal from the code's point of view.
  4120. type taggedTemplateArray struct {
  4121. *arrayObject
  4122. idPtr *[]Value
  4123. }
  4124. func (a *taggedTemplateArray) equal(other objectImpl) bool {
  4125. if o, ok := other.(*taggedTemplateArray); ok {
  4126. return a.idPtr == o.idPtr
  4127. }
  4128. return false
  4129. }
  4130. func (c *getTaggedTmplObject) exec(vm *vm) {
  4131. cooked := vm.r.newArrayObject()
  4132. setArrayValues(cooked, c.cooked)
  4133. raw := vm.r.newArrayObject()
  4134. setArrayValues(raw, c.raw)
  4135. cooked.propValueCount = len(c.cooked)
  4136. cooked.lengthProp.writable = false
  4137. raw.propValueCount = len(c.raw)
  4138. raw.lengthProp.writable = false
  4139. raw.preventExtensions(true)
  4140. raw.val.self = &taggedTemplateArray{
  4141. arrayObject: raw,
  4142. idPtr: &c.raw,
  4143. }
  4144. cooked._putProp("raw", raw.val, false, false, false)
  4145. cooked.preventExtensions(true)
  4146. cooked.val.self = &taggedTemplateArray{
  4147. arrayObject: cooked,
  4148. idPtr: &c.cooked,
  4149. }
  4150. vm.push(cooked.val)
  4151. vm.pc++
  4152. }
  4153. type _loadSuper struct{}
  4154. var loadSuper _loadSuper
  4155. func (_loadSuper) exec(vm *vm) {
  4156. homeObject := getHomeObject(vm.stack[vm.sb-1])
  4157. if proto := homeObject.Prototype(); proto != nil {
  4158. vm.push(proto)
  4159. } else {
  4160. vm.push(_undefined)
  4161. }
  4162. vm.pc++
  4163. }
  4164. type newClass struct {
  4165. ctor *Program
  4166. name unistring.String
  4167. source string
  4168. initFields *Program
  4169. privateFields, privateMethods []unistring.String // only set when dynamic resolution is needed
  4170. numPrivateFields, numPrivateMethods uint32
  4171. length int
  4172. hasPrivateEnv bool
  4173. }
  4174. type newDerivedClass struct {
  4175. newClass
  4176. }
  4177. func (vm *vm) createPrivateType(f *classFuncObject, numFields, numMethods uint32) {
  4178. typ := &privateEnvType{}
  4179. typ.numFields = numFields
  4180. typ.numMethods = numMethods
  4181. f.privateEnvType = typ
  4182. f.privateMethods = make([]Value, numMethods)
  4183. }
  4184. func (vm *vm) fillPrivateNamesMap(typ *privateEnvType, privateFields, privateMethods []unistring.String) {
  4185. if len(privateFields) > 0 || len(privateMethods) > 0 {
  4186. penv := vm.privEnv.names
  4187. if penv == nil {
  4188. penv = make(privateNames)
  4189. vm.privEnv.names = penv
  4190. }
  4191. for idx, field := range privateFields {
  4192. penv[field] = &privateId{
  4193. typ: typ,
  4194. idx: uint32(idx),
  4195. }
  4196. }
  4197. for idx, method := range privateMethods {
  4198. penv[method] = &privateId{
  4199. typ: typ,
  4200. idx: uint32(idx),
  4201. isMethod: true,
  4202. }
  4203. }
  4204. }
  4205. }
  4206. func (c *newClass) create(protoParent, ctorParent *Object, vm *vm, derived bool) (prototype, cls *Object) {
  4207. proto := vm.r.newBaseObject(protoParent, classObject)
  4208. f := vm.r.newClassFunc(c.name, c.length, ctorParent, derived)
  4209. f._putProp("prototype", proto.val, false, false, false)
  4210. proto._putProp("constructor", f.val, true, false, true)
  4211. f.prg = c.ctor
  4212. f.stash = vm.stash
  4213. f.src = c.source
  4214. f.initFields = c.initFields
  4215. if c.hasPrivateEnv {
  4216. vm.privEnv = &privateEnv{
  4217. outer: vm.privEnv,
  4218. }
  4219. vm.createPrivateType(f, c.numPrivateFields, c.numPrivateMethods)
  4220. vm.fillPrivateNamesMap(f.privateEnvType, c.privateFields, c.privateMethods)
  4221. vm.privEnv.instanceType = f.privateEnvType
  4222. }
  4223. f.privEnv = vm.privEnv
  4224. return proto.val, f.val
  4225. }
  4226. func (c *newClass) exec(vm *vm) {
  4227. proto, cls := c.create(vm.r.global.ObjectPrototype, vm.r.global.FunctionPrototype, vm, false)
  4228. sp := vm.sp
  4229. vm.stack.expand(sp + 1)
  4230. vm.stack[sp] = proto
  4231. vm.stack[sp+1] = cls
  4232. vm.sp = sp + 2
  4233. vm.pc++
  4234. }
  4235. func (c *newDerivedClass) exec(vm *vm) {
  4236. var protoParent *Object
  4237. var superClass *Object
  4238. if o := vm.stack[vm.sp-1]; o != _null {
  4239. if sc, ok := o.(*Object); !ok || sc.self.assertConstructor() == nil {
  4240. vm.throw(vm.r.NewTypeError("Class extends value is not a constructor or null"))
  4241. return
  4242. } else {
  4243. v := sc.self.getStr("prototype", nil)
  4244. if v != _null {
  4245. if o, ok := v.(*Object); ok {
  4246. protoParent = o
  4247. } else {
  4248. vm.throw(vm.r.NewTypeError("Class extends value does not have valid prototype property"))
  4249. return
  4250. }
  4251. }
  4252. superClass = sc
  4253. }
  4254. } else {
  4255. superClass = vm.r.global.FunctionPrototype
  4256. }
  4257. proto, cls := c.create(protoParent, superClass, vm, true)
  4258. vm.stack[vm.sp-1] = proto
  4259. vm.push(cls)
  4260. vm.pc++
  4261. }
  4262. // Creates a special instance of *classFuncObject which is only used during evaluation of a class declaration
  4263. // to initialise static fields and instance private methods of another class.
  4264. type newStaticFieldInit struct {
  4265. initFields *Program
  4266. numPrivateFields, numPrivateMethods uint32
  4267. }
  4268. func (c *newStaticFieldInit) exec(vm *vm) {
  4269. f := vm.r.newClassFunc("", 0, vm.r.global.FunctionPrototype, false)
  4270. if c.numPrivateFields > 0 || c.numPrivateMethods > 0 {
  4271. vm.createPrivateType(f, c.numPrivateFields, c.numPrivateMethods)
  4272. }
  4273. f.initFields = c.initFields
  4274. f.stash = vm.stash
  4275. vm.push(f.val)
  4276. vm.pc++
  4277. }
  4278. func (vm *vm) loadThis(v Value) {
  4279. if v != nil {
  4280. vm.push(v)
  4281. } else {
  4282. vm.throw(vm.r.newError(vm.r.global.ReferenceError, "Must call super constructor in derived class before accessing 'this'"))
  4283. return
  4284. }
  4285. vm.pc++
  4286. }
  4287. type loadThisStash uint32
  4288. func (l loadThisStash) exec(vm *vm) {
  4289. vm.loadThis(*vm.getStashPtr(uint32(l)))
  4290. }
  4291. type loadThisStack struct{}
  4292. func (loadThisStack) exec(vm *vm) {
  4293. vm.loadThis(vm.stack[vm.sb])
  4294. }
  4295. func (vm *vm) getStashPtr(s uint32) *Value {
  4296. level := int(s) >> 24
  4297. idx := s & 0x00FFFFFF
  4298. stash := vm.stash
  4299. for i := 0; i < level; i++ {
  4300. stash = stash.outer
  4301. }
  4302. return &stash.values[idx]
  4303. }
  4304. type getThisDynamic struct{}
  4305. func (getThisDynamic) exec(vm *vm) {
  4306. for stash := vm.stash; stash != nil; stash = stash.outer {
  4307. if stash.obj == nil {
  4308. if v, exists := stash.getByName(thisBindingName); exists {
  4309. vm.push(v)
  4310. vm.pc++
  4311. return
  4312. }
  4313. }
  4314. }
  4315. vm.push(vm.r.globalObject)
  4316. vm.pc++
  4317. }
  4318. type throwConst struct {
  4319. v interface{}
  4320. }
  4321. func (t throwConst) exec(vm *vm) {
  4322. vm.throw(t.v)
  4323. }
  4324. type resolveThisStack struct{}
  4325. func (r resolveThisStack) exec(vm *vm) {
  4326. vm.refStack = append(vm.refStack, &thisRef{v: (*[]Value)(&vm.stack), idx: vm.sb})
  4327. vm.pc++
  4328. }
  4329. type resolveThisStash uint32
  4330. func (r resolveThisStash) exec(vm *vm) {
  4331. level := int(r) >> 24
  4332. idx := r & 0x00FFFFFF
  4333. stash := vm.stash
  4334. for i := 0; i < level; i++ {
  4335. stash = stash.outer
  4336. }
  4337. vm.refStack = append(vm.refStack, &thisRef{v: &stash.values, idx: int(idx)})
  4338. vm.pc++
  4339. }
  4340. type resolveThisDynamic struct{}
  4341. func (resolveThisDynamic) exec(vm *vm) {
  4342. for stash := vm.stash; stash != nil; stash = stash.outer {
  4343. if stash.obj == nil {
  4344. if idx, exists := stash.names[thisBindingName]; exists {
  4345. vm.refStack = append(vm.refStack, &thisRef{v: &stash.values, idx: int(idx &^ maskTyp)})
  4346. vm.pc++
  4347. return
  4348. }
  4349. }
  4350. }
  4351. panic(vm.r.newError(vm.r.global.ReferenceError, "Compiler bug: 'this' reference is not found in resolveThisDynamic"))
  4352. }
  4353. type defineComputedKey int
  4354. func (offset defineComputedKey) exec(vm *vm) {
  4355. obj := vm.r.toObject(vm.stack[vm.sp-int(offset)])
  4356. if h, ok := obj.self.(*classFuncObject); ok {
  4357. key := toPropertyKey(vm.stack[vm.sp-1])
  4358. h.computedKeys = append(h.computedKeys, key)
  4359. vm.sp--
  4360. vm.pc++
  4361. return
  4362. }
  4363. panic(vm.r.NewTypeError("Compiler bug: unexpected target for defineComputedKey: %v", obj))
  4364. }
  4365. type loadComputedKey int
  4366. func (idx loadComputedKey) exec(vm *vm) {
  4367. obj := vm.r.toObject(vm.stack[vm.sb-1])
  4368. if h, ok := obj.self.(*classFuncObject); ok {
  4369. vm.push(h.computedKeys[idx])
  4370. vm.pc++
  4371. return
  4372. }
  4373. panic(vm.r.NewTypeError("Compiler bug: unexpected target for loadComputedKey: %v", obj))
  4374. }
  4375. type initStaticElements struct {
  4376. privateFields, privateMethods []unistring.String
  4377. }
  4378. func (i *initStaticElements) exec(vm *vm) {
  4379. cls := vm.stack[vm.sp-1]
  4380. staticInit := vm.r.toObject(vm.stack[vm.sp-3])
  4381. vm.sp -= 2
  4382. if h, ok := staticInit.self.(*classFuncObject); ok {
  4383. h._putProp("prototype", cls, true, true, true) // so that 'super' resolution work
  4384. h.privEnv = vm.privEnv
  4385. if h.privateEnvType != nil {
  4386. vm.privEnv.staticType = h.privateEnvType
  4387. vm.fillPrivateNamesMap(h.privateEnvType, i.privateFields, i.privateMethods)
  4388. }
  4389. h._initFields(vm.r.toObject(cls))
  4390. vm.stack[vm.sp-1] = cls
  4391. vm.pc++
  4392. return
  4393. }
  4394. panic(vm.r.NewTypeError("Compiler bug: unexpected target for initStaticElements: %v", staticInit))
  4395. }
  4396. type definePrivateMethod struct {
  4397. idx int
  4398. targetOffset int
  4399. }
  4400. func (d *definePrivateMethod) getPrivateMethods(vm *vm) []Value {
  4401. obj := vm.r.toObject(vm.stack[vm.sp-d.targetOffset])
  4402. if cls, ok := obj.self.(*classFuncObject); ok {
  4403. return cls.privateMethods
  4404. } else {
  4405. panic(vm.r.NewTypeError("Compiler bug: wrong target type for definePrivateMethod: %T", obj.self))
  4406. }
  4407. }
  4408. func (d *definePrivateMethod) exec(vm *vm) {
  4409. methods := d.getPrivateMethods(vm)
  4410. methods[d.idx] = vm.stack[vm.sp-1]
  4411. vm.sp--
  4412. vm.pc++
  4413. }
  4414. type definePrivateGetter struct {
  4415. definePrivateMethod
  4416. }
  4417. func (d *definePrivateGetter) exec(vm *vm) {
  4418. methods := d.getPrivateMethods(vm)
  4419. val := vm.stack[vm.sp-1]
  4420. method := vm.r.toObject(val)
  4421. p, _ := methods[d.idx].(*valueProperty)
  4422. if p == nil {
  4423. p = &valueProperty{
  4424. accessor: true,
  4425. }
  4426. methods[d.idx] = p
  4427. }
  4428. if p.getterFunc != nil {
  4429. vm.throw(vm.r.NewTypeError("Private getter has already been declared"))
  4430. return
  4431. }
  4432. p.getterFunc = method
  4433. vm.sp--
  4434. vm.pc++
  4435. }
  4436. type definePrivateSetter struct {
  4437. definePrivateMethod
  4438. }
  4439. func (d *definePrivateSetter) exec(vm *vm) {
  4440. methods := d.getPrivateMethods(vm)
  4441. val := vm.stack[vm.sp-1]
  4442. method := vm.r.toObject(val)
  4443. p, _ := methods[d.idx].(*valueProperty)
  4444. if p == nil {
  4445. p = &valueProperty{
  4446. accessor: true,
  4447. }
  4448. methods[d.idx] = p
  4449. }
  4450. if p.setterFunc != nil {
  4451. vm.throw(vm.r.NewTypeError("Private setter has already been declared"))
  4452. return
  4453. }
  4454. p.setterFunc = method
  4455. vm.sp--
  4456. vm.pc++
  4457. }
  4458. type definePrivateProp struct {
  4459. idx int
  4460. }
  4461. func (d *definePrivateProp) exec(vm *vm) {
  4462. f := vm.r.toObject(vm.stack[vm.sb-1]).self.(*classFuncObject)
  4463. obj := vm.r.toObject(vm.stack[vm.sp-2])
  4464. penv := obj.self.getPrivateEnv(f.privateEnvType, false)
  4465. penv.fields[d.idx] = vm.stack[vm.sp-1]
  4466. vm.sp--
  4467. vm.pc++
  4468. }
  4469. type getPrivatePropRes resolvedPrivateName
  4470. func (vm *vm) getPrivateType(level uint8, isStatic bool) *privateEnvType {
  4471. e := vm.privEnv
  4472. for i := uint8(0); i < level; i++ {
  4473. e = e.outer
  4474. }
  4475. if isStatic {
  4476. return e.staticType
  4477. }
  4478. return e.instanceType
  4479. }
  4480. func (g *getPrivatePropRes) _get(base Value, vm *vm) Value {
  4481. return vm.getPrivateProp(base, g.name, vm.getPrivateType(g.level, g.isStatic), g.idx, g.isMethod)
  4482. }
  4483. func (g *getPrivatePropRes) exec(vm *vm) {
  4484. vm.stack[vm.sp-1] = g._get(vm.stack[vm.sp-1], vm)
  4485. vm.pc++
  4486. }
  4487. type getPrivatePropId privateId
  4488. func (g *getPrivatePropId) exec(vm *vm) {
  4489. vm.stack[vm.sp-1] = vm.getPrivateProp(vm.stack[vm.sp-1], g.name, g.typ, g.idx, g.isMethod)
  4490. vm.pc++
  4491. }
  4492. type getPrivatePropIdCallee privateId
  4493. func (g *getPrivatePropIdCallee) exec(vm *vm) {
  4494. prop := vm.getPrivateProp(vm.stack[vm.sp-1], g.name, g.typ, g.idx, g.isMethod)
  4495. if prop == nil {
  4496. prop = memberUnresolved{valueUnresolved{r: vm.r, ref: (*privateId)(g).string()}}
  4497. }
  4498. vm.push(prop)
  4499. vm.pc++
  4500. }
  4501. func (vm *vm) getPrivateProp(base Value, name unistring.String, typ *privateEnvType, idx uint32, isMethod bool) Value {
  4502. obj := vm.r.toObject(base)
  4503. penv := obj.self.getPrivateEnv(typ, false)
  4504. var v Value
  4505. if penv != nil {
  4506. if isMethod {
  4507. v = penv.methods[idx]
  4508. } else {
  4509. v = penv.fields[idx]
  4510. if v == nil {
  4511. panic(vm.r.NewTypeError("Private member #%s is accessed before it is initialized", name))
  4512. }
  4513. }
  4514. } else {
  4515. panic(vm.r.NewTypeError("Cannot read private member #%s from an object whose class did not declare it", name))
  4516. }
  4517. if prop, ok := v.(*valueProperty); ok {
  4518. if prop.getterFunc == nil {
  4519. panic(vm.r.NewTypeError("'#%s' was defined without a getter", name))
  4520. }
  4521. v = prop.get(obj)
  4522. }
  4523. return v
  4524. }
  4525. type getPrivatePropResCallee getPrivatePropRes
  4526. func (g *getPrivatePropResCallee) exec(vm *vm) {
  4527. prop := (*getPrivatePropRes)(g)._get(vm.stack[vm.sp-1], vm)
  4528. if prop == nil {
  4529. prop = memberUnresolved{valueUnresolved{r: vm.r, ref: (*resolvedPrivateName)(g).string()}}
  4530. }
  4531. vm.push(prop)
  4532. vm.pc++
  4533. }
  4534. func (vm *vm) setPrivateProp(base Value, name unistring.String, typ *privateEnvType, idx uint32, isMethod bool, val Value) {
  4535. obj := vm.r.toObject(base)
  4536. penv := obj.self.getPrivateEnv(typ, false)
  4537. if penv != nil {
  4538. if isMethod {
  4539. v := penv.methods[idx]
  4540. if prop, ok := v.(*valueProperty); ok {
  4541. if prop.setterFunc != nil {
  4542. prop.set(base, val)
  4543. } else {
  4544. panic(vm.r.NewTypeError("Cannot assign to read only property '#%s'", name))
  4545. }
  4546. } else {
  4547. panic(vm.r.NewTypeError("Private method '#%s' is not writable", name))
  4548. }
  4549. } else {
  4550. ptr := &penv.fields[idx]
  4551. if *ptr == nil {
  4552. panic(vm.r.NewTypeError("Private member #%s is accessed before it is initialized", name))
  4553. }
  4554. *ptr = val
  4555. }
  4556. } else {
  4557. panic(vm.r.NewTypeError("Cannot write private member #%s from an object whose class did not declare it", name))
  4558. }
  4559. }
  4560. func (vm *vm) exceptionFromValue(x interface{}) *Exception {
  4561. var ex *Exception
  4562. switch x1 := x.(type) {
  4563. case *Object:
  4564. ex = &Exception{
  4565. val: x1,
  4566. }
  4567. if er, ok := x1.self.(*errorObject); ok {
  4568. ex.stack = er.stack
  4569. }
  4570. case Value:
  4571. ex = &Exception{
  4572. val: x1,
  4573. }
  4574. case *Exception:
  4575. ex = x1
  4576. case typeError:
  4577. ex = &Exception{
  4578. val: vm.r.NewTypeError(string(x1)),
  4579. }
  4580. case referenceError:
  4581. ex = &Exception{
  4582. val: vm.r.newError(vm.r.global.ReferenceError, string(x1)),
  4583. }
  4584. case rangeError:
  4585. ex = &Exception{
  4586. val: vm.r.newError(vm.r.global.RangeError, string(x1)),
  4587. }
  4588. case syntaxError:
  4589. ex = &Exception{
  4590. val: vm.r.newError(vm.r.global.SyntaxError, string(x1)),
  4591. }
  4592. default:
  4593. /*
  4594. if vm.prg != nil {
  4595. vm.prg.dumpCode(log.Printf)
  4596. }
  4597. log.Print("Stack: ", string(debug.Stack()))
  4598. panic(fmt.Errorf("Panic at %d: %v", vm.pc, x))
  4599. */
  4600. return nil
  4601. }
  4602. if ex.stack == nil {
  4603. ex.stack = vm.captureStack(make([]StackFrame, 0, len(vm.callStack)+1), 0)
  4604. }
  4605. return ex
  4606. }
  4607. type setPrivatePropRes resolvedPrivateName
  4608. func (p *setPrivatePropRes) _set(base Value, val Value, vm *vm) {
  4609. vm.setPrivateProp(base, p.name, vm.getPrivateType(p.level, p.isStatic), p.idx, p.isMethod, val)
  4610. }
  4611. func (p *setPrivatePropRes) exec(vm *vm) {
  4612. v := vm.stack[vm.sp-1]
  4613. p._set(vm.stack[vm.sp-2], v, vm)
  4614. vm.stack[vm.sp-2] = v
  4615. vm.sp--
  4616. vm.pc++
  4617. }
  4618. type setPrivatePropResP setPrivatePropRes
  4619. func (p *setPrivatePropResP) exec(vm *vm) {
  4620. v := vm.stack[vm.sp-1]
  4621. (*setPrivatePropRes)(p)._set(vm.stack[vm.sp-2], v, vm)
  4622. vm.sp -= 2
  4623. vm.pc++
  4624. }
  4625. type setPrivatePropId privateId
  4626. func (p *setPrivatePropId) exec(vm *vm) {
  4627. v := vm.stack[vm.sp-1]
  4628. vm.setPrivateProp(vm.stack[vm.sp-2], p.name, p.typ, p.idx, p.isMethod, v)
  4629. vm.stack[vm.sp-2] = v
  4630. vm.sp--
  4631. vm.pc++
  4632. }
  4633. type setPrivatePropIdP privateId
  4634. func (p *setPrivatePropIdP) exec(vm *vm) {
  4635. v := vm.stack[vm.sp-1]
  4636. vm.setPrivateProp(vm.stack[vm.sp-2], p.name, p.typ, p.idx, p.isMethod, v)
  4637. vm.sp -= 2
  4638. vm.pc++
  4639. }
  4640. type popPrivateEnv struct{}
  4641. func (popPrivateEnv) exec(vm *vm) {
  4642. vm.privEnv = vm.privEnv.outer
  4643. vm.pc++
  4644. }
  4645. type privateInRes resolvedPrivateName
  4646. func (i *privateInRes) exec(vm *vm) {
  4647. obj := vm.r.toObject(vm.stack[vm.sp-1])
  4648. pe := obj.self.getPrivateEnv(vm.getPrivateType(i.level, i.isStatic), false)
  4649. if pe != nil && (i.isMethod && pe.methods[i.idx] != nil || !i.isMethod && pe.fields[i.idx] != nil) {
  4650. vm.stack[vm.sp-1] = valueTrue
  4651. } else {
  4652. vm.stack[vm.sp-1] = valueFalse
  4653. }
  4654. vm.pc++
  4655. }
  4656. type privateInId privateId
  4657. func (i *privateInId) exec(vm *vm) {
  4658. obj := vm.r.toObject(vm.stack[vm.sp-1])
  4659. pe := obj.self.getPrivateEnv(i.typ, false)
  4660. if pe != nil && (i.isMethod && pe.methods[i.idx] != nil || !i.isMethod && pe.fields[i.idx] != nil) {
  4661. vm.stack[vm.sp-1] = valueTrue
  4662. } else {
  4663. vm.stack[vm.sp-1] = valueFalse
  4664. }
  4665. vm.pc++
  4666. }
  4667. type getPrivateRefRes resolvedPrivateName
  4668. func (r *getPrivateRefRes) exec(vm *vm) {
  4669. vm.refStack = append(vm.refStack, &privateRefRes{
  4670. base: vm.stack[vm.sp-1].ToObject(vm.r),
  4671. name: (*resolvedPrivateName)(r),
  4672. })
  4673. vm.sp--
  4674. vm.pc++
  4675. }
  4676. type getPrivateRefId privateId
  4677. func (r *getPrivateRefId) exec(vm *vm) {
  4678. vm.refStack = append(vm.refStack, &privateRefId{
  4679. base: vm.stack[vm.sp-1].ToObject(vm.r),
  4680. id: (*privateId)(r),
  4681. })
  4682. vm.sp--
  4683. vm.pc++
  4684. }
  4685. func (y *yieldMarker) exec(vm *vm) {
  4686. vm.pc = -vm.pc // this will terminate the run loop
  4687. vm.push(y) // marker so the caller knows it's a yield, not a return
  4688. }
  4689. func (y *yieldMarker) String() string {
  4690. if y == yieldEmpty {
  4691. return "empty"
  4692. }
  4693. switch y.resultType {
  4694. case resultYield:
  4695. return "yield"
  4696. case resultYieldRes:
  4697. return "yieldRes"
  4698. case resultYieldDelegate:
  4699. return "yield*"
  4700. case resultYieldDelegateRes:
  4701. return "yield*Res"
  4702. case resultAwait:
  4703. return "await"
  4704. default:
  4705. return "unknown"
  4706. }
  4707. }