vm.go 116 KB

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