12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942 |
- package goja
- import (
- "fmt"
- "math"
- "math/big"
- "strconv"
- "strings"
- "sync"
- "sync/atomic"
- "time"
- "github.com/dop251/goja/unistring"
- )
- const (
- maxInt = 1 << 53
- tryPanicMarker = -2
- )
- type valueStack []Value
- type stash struct {
- values []Value
- extraArgs []Value
- names map[unistring.String]uint32
- obj *Object
- outer *stash
- // If this is a top-level function stash, sets the type of the function. If set, dynamic var declarations
- // created by direct eval go here.
- funcType funcType
- }
- type context struct {
- prg *Program
- stash *stash
- privEnv *privateEnv
- newTarget Value
- result Value
- pc, sb int
- args int
- }
- type tryFrame struct {
- // holds an uncaught exception for the 'finally' block
- exception *Exception
- callStackLen, iterLen, refLen uint32
- sp int32
- stash *stash
- privEnv *privateEnv
- catchPos, finallyPos, finallyRet int32
- }
- type execCtx struct {
- context
- stack []Value
- tryStack []tryFrame
- iterStack []iterStackItem
- refStack []ref
- }
- func (vm *vm) suspend(ectx *execCtx, tryStackLen, iterStackLen, refStackLen uint32) {
- vm.saveCtx(&ectx.context)
- ectx.stack = append(ectx.stack[:0], vm.stack[vm.sb-1:vm.sp]...)
- if len(vm.tryStack) > int(tryStackLen) {
- ectx.tryStack = append(ectx.tryStack[:0], vm.tryStack[tryStackLen:]...)
- vm.tryStack = vm.tryStack[:tryStackLen]
- sp := int32(vm.sb - 1)
- for i := range ectx.tryStack {
- tf := &ectx.tryStack[i]
- tf.iterLen -= iterStackLen
- tf.refLen -= refStackLen
- tf.sp -= sp
- }
- }
- if len(vm.iterStack) > int(iterStackLen) {
- ectx.iterStack = append(ectx.iterStack[:0], vm.iterStack[iterStackLen:]...)
- vm.iterStack = vm.iterStack[:iterStackLen]
- }
- if len(vm.refStack) > int(refStackLen) {
- ectx.refStack = append(ectx.refStack[:0], vm.refStack[refStackLen:]...)
- vm.refStack = vm.refStack[:refStackLen]
- }
- }
- func (vm *vm) resume(ctx *execCtx) {
- vm.restoreCtx(&ctx.context)
- sp := vm.sp
- vm.sb = sp + 1
- vm.stack.expand(sp + len(ctx.stack))
- copy(vm.stack[sp:], ctx.stack)
- vm.sp += len(ctx.stack)
- for i := range ctx.tryStack {
- tf := &ctx.tryStack[i]
- tf.callStackLen = uint32(len(vm.callStack))
- tf.iterLen += uint32(len(vm.iterStack))
- tf.refLen += uint32(len(vm.refStack))
- tf.sp += int32(sp)
- }
- vm.tryStack = append(vm.tryStack, ctx.tryStack...)
- vm.iterStack = append(vm.iterStack, ctx.iterStack...)
- vm.refStack = append(vm.refStack, ctx.refStack...)
- }
- type iterStackItem struct {
- val Value
- f iterNextFunc
- iter *iteratorRecord
- }
- type ref interface {
- get() Value
- set(Value)
- init(Value)
- refname() unistring.String
- }
- type stashRef struct {
- n unistring.String
- v *[]Value
- idx int
- }
- func (r *stashRef) get() Value {
- return nilSafe((*r.v)[r.idx])
- }
- func (r *stashRef) set(v Value) {
- (*r.v)[r.idx] = v
- }
- func (r *stashRef) init(v Value) {
- r.set(v)
- }
- func (r *stashRef) refname() unistring.String {
- return r.n
- }
- type thisRef struct {
- v *[]Value
- idx int
- }
- func (r *thisRef) get() Value {
- v := (*r.v)[r.idx]
- if v == nil {
- panic(referenceError("Must call super constructor in derived class before accessing 'this'"))
- }
- return v
- }
- func (r *thisRef) set(v Value) {
- ptr := &(*r.v)[r.idx]
- if *ptr != nil {
- panic(referenceError("Super constructor may only be called once"))
- }
- *ptr = v
- }
- func (r *thisRef) init(v Value) {
- r.set(v)
- }
- func (r *thisRef) refname() unistring.String {
- return thisBindingName
- }
- type stashRefLex struct {
- stashRef
- }
- func (r *stashRefLex) get() Value {
- v := (*r.v)[r.idx]
- if v == nil {
- panic(errAccessBeforeInit)
- }
- return v
- }
- func (r *stashRefLex) set(v Value) {
- p := &(*r.v)[r.idx]
- if *p == nil {
- panic(errAccessBeforeInit)
- }
- *p = v
- }
- func (r *stashRefLex) init(v Value) {
- (*r.v)[r.idx] = v
- }
- type stashRefConst struct {
- stashRefLex
- strictConst bool
- }
- func (r *stashRefConst) set(v Value) {
- if r.strictConst {
- panic(errAssignToConst)
- }
- }
- type objRef struct {
- base *Object
- name Value
- this Value
- strict bool
- nameConverted bool
- }
- func (r *objRef) getKey() Value {
- if !r.nameConverted {
- r.name = toPropertyKey(r.name)
- r.nameConverted = true
- }
- return r.name
- }
- func (r *objRef) get() Value {
- return r.base.get(r.getKey(), r.this)
- }
- func (r *objRef) set(v Value) {
- key := r.getKey()
- if r.this != nil {
- r.base.set(key, v, r.this, r.strict)
- } else {
- r.base.setOwn(key, v, r.strict)
- }
- }
- func (r *objRef) init(v Value) {
- if r.this != nil {
- r.base.set(r.getKey(), v, r.this, r.strict)
- } else {
- r.base.setOwn(r.getKey(), v, r.strict)
- }
- }
- func (r *objRef) refname() unistring.String {
- return r.getKey().string()
- }
- type objStrRef struct {
- base *Object
- name unistring.String
- this Value
- strict bool
- binding bool
- }
- func (r *objStrRef) get() Value {
- if v := r.base.self.getStr(r.name, r.this); v != nil {
- return v
- }
- if r.binding {
- rt := r.base.runtime
- panic(rt.newReferenceError(r.name))
- }
- return _undefined
- }
- func (r *objStrRef) set(v Value) {
- if r.strict && r.binding && !r.base.self.hasOwnPropertyStr(r.name) {
- panic(referenceError(fmt.Sprintf("%s is not defined", r.name)))
- }
- if r.this != nil {
- r.base.setStr(r.name, v, r.this, r.strict)
- } else {
- r.base.self.setOwnStr(r.name, v, r.strict)
- }
- }
- func (r *objStrRef) init(v Value) {
- if r.this != nil {
- r.base.setStr(r.name, v, r.this, r.strict)
- } else {
- r.base.self.setOwnStr(r.name, v, r.strict)
- }
- }
- func (r *objStrRef) refname() unistring.String {
- return r.name
- }
- type privateRefRes struct {
- base *Object
- name *resolvedPrivateName
- }
- func (p *privateRefRes) get() Value {
- return (*getPrivatePropRes)(p.name)._get(p.base, p.base.runtime.vm)
- }
- func (p *privateRefRes) set(value Value) {
- (*setPrivatePropRes)(p.name)._set(p.base, value, p.base.runtime.vm)
- }
- func (p *privateRefRes) init(value Value) {
- panic("not supported")
- }
- func (p *privateRefRes) refname() unistring.String {
- return p.name.string()
- }
- type privateRefId struct {
- base *Object
- id *privateId
- }
- func (p *privateRefId) get() Value {
- return p.base.runtime.vm.getPrivateProp(p.base, p.id.name, p.id.typ, p.id.idx, p.id.isMethod)
- }
- func (p *privateRefId) set(value Value) {
- p.base.runtime.vm.setPrivateProp(p.base, p.id.name, p.id.typ, p.id.idx, p.id.isMethod, value)
- }
- func (p *privateRefId) init(value Value) {
- panic("not supported")
- }
- func (p *privateRefId) refname() unistring.String {
- return p.id.string()
- }
- type unresolvedRef struct {
- runtime *Runtime
- name unistring.String
- }
- func (r *unresolvedRef) get() Value {
- r.runtime.throwReferenceError(r.name)
- panic("Unreachable")
- }
- func (r *unresolvedRef) set(Value) {
- r.get()
- }
- func (r *unresolvedRef) init(Value) {
- r.get()
- }
- func (r *unresolvedRef) refname() unistring.String {
- return r.name
- }
- type vm struct {
- r *Runtime
- prg *Program
- pc int
- stack valueStack
- sp, sb, args int
- stash *stash
- privEnv *privateEnv
- callStack []context
- iterStack []iterStackItem
- refStack []ref
- tryStack []tryFrame
- newTarget Value
- result Value
- maxCallStackSize int
- stashAllocs int
- interrupted uint32
- interruptVal interface{}
- interruptLock sync.Mutex
- curAsyncRunner *asyncRunner
- profTracker *profTracker
- }
- type instruction interface {
- exec(*vm)
- }
- func intToValue(i int64) Value {
- if idx := 256 + i; idx >= 0 && idx < 256 {
- return intCache[idx]
- }
- if i >= -maxInt && i <= maxInt {
- return valueInt(i)
- }
- return valueFloat(i)
- }
- func floatToInt(f float64) (result int64, ok bool) {
- if (f != 0 || !math.Signbit(f)) && !math.IsInf(f, 0) && f == math.Trunc(f) && f >= -maxInt && f <= maxInt {
- return int64(f), true
- }
- return 0, false
- }
- func floatToValue(f float64) (result Value) {
- if i, ok := floatToInt(f); ok {
- return intToValue(i)
- }
- switch {
- case f == 0:
- return _negativeZero
- case math.IsNaN(f):
- return _NaN
- case math.IsInf(f, 1):
- return _positiveInf
- case math.IsInf(f, -1):
- return _negativeInf
- }
- return valueFloat(f)
- }
- func toNumeric(value Value) Value {
- switch v := value.(type) {
- case valueInt, *valueBigInt:
- return v
- case valueFloat:
- return floatToValue(float64(v))
- case *Object:
- primValue := v.toPrimitiveNumber()
- if bigint, ok := primValue.(*valueBigInt); ok {
- return bigint
- }
- return primValue.ToNumber()
- }
- return value.ToNumber()
- }
- func (s *valueStack) expand(idx int) {
- if idx < len(*s) {
- return
- }
- idx++
- if idx < cap(*s) {
- *s = (*s)[:idx]
- } else {
- var newCap int
- if idx < 1024 {
- newCap = idx * 2
- } else {
- newCap = (idx + 1025) &^ 1023
- }
- n := make([]Value, idx, newCap)
- copy(n, *s)
- *s = n
- }
- }
- func stashObjHas(obj *Object, name unistring.String) bool {
- if obj.self.hasPropertyStr(name) {
- if unscopables, ok := obj.self.getSym(SymUnscopables, nil).(*Object); ok {
- if b := unscopables.self.getStr(name, nil); b != nil {
- return !b.ToBoolean()
- }
- }
- return true
- }
- return false
- }
- func (s *stash) isVariable() bool {
- return s.funcType != funcNone
- }
- func (s *stash) initByIdx(idx uint32, v Value) {
- if s.obj != nil {
- panic("Attempt to init by idx into an object scope")
- }
- s.values[idx] = v
- }
- func (s *stash) initByName(name unistring.String, v Value) {
- if idx, exists := s.names[name]; exists {
- s.values[idx&^maskTyp] = v
- } else {
- panic(referenceError(fmt.Sprintf("%s is not defined", name)))
- }
- }
- func (s *stash) getByIdx(idx uint32) Value {
- return s.values[idx]
- }
- func (s *stash) getByName(name unistring.String) (v Value, exists bool) {
- if s.obj != nil {
- if stashObjHas(s.obj, name) {
- return nilSafe(s.obj.self.getStr(name, nil)), true
- }
- return nil, false
- }
- if idx, exists := s.names[name]; exists {
- v := s.values[idx&^maskTyp]
- if v == nil {
- if idx&maskVar == 0 {
- panic(errAccessBeforeInit)
- } else {
- v = _undefined
- }
- }
- return v, true
- }
- return nil, false
- }
- func (s *stash) getRefByName(name unistring.String, strict bool) ref {
- if obj := s.obj; obj != nil {
- if stashObjHas(obj, name) {
- return &objStrRef{
- base: obj,
- name: name,
- strict: strict,
- binding: true,
- }
- }
- } else {
- if idx, exists := s.names[name]; exists {
- if idx&maskVar == 0 {
- if idx&maskConst == 0 {
- return &stashRefLex{
- stashRef: stashRef{
- n: name,
- v: &s.values,
- idx: int(idx &^ maskTyp),
- },
- }
- } else {
- return &stashRefConst{
- stashRefLex: stashRefLex{
- stashRef: stashRef{
- n: name,
- v: &s.values,
- idx: int(idx &^ maskTyp),
- },
- },
- strictConst: strict || (idx&maskStrict != 0),
- }
- }
- } else {
- return &stashRef{
- n: name,
- v: &s.values,
- idx: int(idx &^ maskTyp),
- }
- }
- }
- }
- return nil
- }
- func (s *stash) createBinding(name unistring.String, deletable bool) {
- if s.names == nil {
- s.names = make(map[unistring.String]uint32)
- }
- if _, exists := s.names[name]; !exists {
- idx := uint32(len(s.names)) | maskVar
- if deletable {
- idx |= maskDeletable
- }
- s.names[name] = idx
- s.values = append(s.values, _undefined)
- }
- }
- func (s *stash) createLexBinding(name unistring.String, isConst bool) {
- if s.names == nil {
- s.names = make(map[unistring.String]uint32)
- }
- if _, exists := s.names[name]; !exists {
- idx := uint32(len(s.names))
- if isConst {
- idx |= maskConst | maskStrict
- }
- s.names[name] = idx
- s.values = append(s.values, nil)
- }
- }
- func (s *stash) deleteBinding(name unistring.String) {
- delete(s.names, name)
- }
- func (vm *vm) newStash() {
- vm.stash = &stash{
- outer: vm.stash,
- }
- vm.stashAllocs++
- }
- func (vm *vm) init() {
- vm.sb = -1
- vm.stash = &vm.r.global.stash
- vm.maxCallStackSize = math.MaxInt32
- }
- func (vm *vm) halted() bool {
- pc := vm.pc
- return pc < 0 || pc >= len(vm.prg.code)
- }
- func (vm *vm) run() {
- if vm.profTracker != nil && !vm.runWithProfiler() {
- return
- }
- count := 0
- interrupted := false
- for {
- if count == 0 {
- if atomic.LoadInt32(&globalProfiler.enabled) == 1 && !vm.runWithProfiler() {
- return
- }
- count = 100
- } else {
- count--
- }
- if interrupted = atomic.LoadUint32(&vm.interrupted) != 0; interrupted {
- break
- }
- pc := vm.pc
- if pc < 0 || pc >= len(vm.prg.code) {
- break
- }
- vm.prg.code[pc].exec(vm)
- }
- if interrupted {
- vm.interruptLock.Lock()
- v := &InterruptedError{
- iface: vm.interruptVal,
- }
- v.stack = vm.captureStack(nil, 0)
- vm.interruptLock.Unlock()
- panic(v)
- }
- }
- func (vm *vm) runWithProfiler() bool {
- pt := vm.profTracker
- if pt == nil {
- pt = globalProfiler.p.registerVm()
- vm.profTracker = pt
- defer func() {
- atomic.StoreInt32(&vm.profTracker.finished, 1)
- vm.profTracker = nil
- }()
- }
- interrupted := false
- for {
- if interrupted = atomic.LoadUint32(&vm.interrupted) != 0; interrupted {
- return true
- }
- pc := vm.pc
- if pc < 0 || pc >= len(vm.prg.code) {
- break
- }
- vm.prg.code[pc].exec(vm)
- req := atomic.LoadInt32(&pt.req)
- if req == profReqStop {
- return true
- }
- if req == profReqDoSample {
- pt.stop = time.Now()
- pt.numFrames = len(vm.r.CaptureCallStack(len(pt.frames), pt.frames[:0]))
- pt.frames[0].pc = pc
- atomic.StoreInt32(&pt.req, profReqSampleReady)
- }
- }
- return false
- }
- func (vm *vm) Interrupt(v interface{}) {
- vm.interruptLock.Lock()
- vm.interruptVal = v
- atomic.StoreUint32(&vm.interrupted, 1)
- vm.interruptLock.Unlock()
- }
- func (vm *vm) ClearInterrupt() {
- atomic.StoreUint32(&vm.interrupted, 0)
- }
- func getFuncName(stack []Value, sb int) unistring.String {
- if sb > 0 {
- if f, ok := stack[sb-1].(*Object); ok {
- if _, isProxy := f.self.(*proxyObject); isProxy {
- return "proxy"
- }
- return nilSafe(f.self.getStr("name", nil)).string()
- }
- }
- return ""
- }
- func (vm *vm) captureStack(stack []StackFrame, ctxOffset int) []StackFrame {
- // Unroll the context stack
- if vm.prg != nil || vm.sb > 0 {
- var funcName unistring.String
- if vm.prg != nil {
- funcName = vm.prg.funcName
- } else {
- funcName = getFuncName(vm.stack, vm.sb)
- }
- stack = append(stack, StackFrame{prg: vm.prg, pc: vm.pc, funcName: funcName})
- }
- for i := len(vm.callStack) - 1; i > ctxOffset-1; i-- {
- frame := &vm.callStack[i]
- if frame.prg != nil || frame.sb > 0 {
- var funcName unistring.String
- if prg := frame.prg; prg != nil {
- funcName = prg.funcName
- } else {
- funcName = getFuncName(vm.stack, frame.sb)
- }
- stack = append(stack, StackFrame{prg: vm.callStack[i].prg, pc: frame.pc, funcName: funcName})
- }
- }
- if ctxOffset == 0 && vm.curAsyncRunner != nil {
- stack = vm.captureAsyncStack(stack, vm.curAsyncRunner)
- }
- return stack
- }
- func (vm *vm) captureAsyncStack(stack []StackFrame, runner *asyncRunner) []StackFrame {
- if promise, _ := runner.promiseCap.promise.self.(*Promise); promise != nil {
- if len(promise.fulfillReactions) == 1 {
- if r := promise.fulfillReactions[0].asyncRunner; r != nil {
- ctx := &r.gen.ctx
- if ctx.prg != nil || ctx.sb > 0 {
- var funcName unistring.String
- if prg := ctx.prg; prg != nil {
- funcName = prg.funcName
- } else {
- funcName = getFuncName(ctx.stack, 1)
- }
- stack = append(stack, StackFrame{prg: ctx.prg, pc: ctx.pc, funcName: funcName})
- }
- stack = vm.captureAsyncStack(stack, r)
- }
- }
- }
- return stack
- }
- func (vm *vm) pushTryFrame(catchPos, finallyPos int32) {
- vm.tryStack = append(vm.tryStack, tryFrame{
- callStackLen: uint32(len(vm.callStack)),
- iterLen: uint32(len(vm.iterStack)),
- refLen: uint32(len(vm.refStack)),
- sp: int32(vm.sp),
- stash: vm.stash,
- privEnv: vm.privEnv,
- catchPos: catchPos,
- finallyPos: finallyPos,
- finallyRet: -1,
- })
- }
- func (vm *vm) popTryFrame() {
- vm.tryStack = vm.tryStack[:len(vm.tryStack)-1]
- }
- func (vm *vm) restoreStacks(iterLen, refLen uint32) (ex *Exception) {
- // Restore other stacks
- iterTail := vm.iterStack[iterLen:]
- for i := len(iterTail) - 1; i >= 0; i-- {
- if iter := iterTail[i].iter; iter != nil {
- ex1 := vm.try(func() {
- iter.returnIter()
- })
- if ex1 != nil && ex == nil {
- ex = ex1
- }
- }
- iterTail[i] = iterStackItem{}
- }
- vm.iterStack = vm.iterStack[:iterLen]
- refTail := vm.refStack[refLen:]
- for i := range refTail {
- refTail[i] = nil
- }
- vm.refStack = vm.refStack[:refLen]
- return
- }
- func (vm *vm) handleThrow(arg interface{}) *Exception {
- ex := vm.exceptionFromValue(arg)
- for len(vm.tryStack) > 0 {
- tf := &vm.tryStack[len(vm.tryStack)-1]
- if tf.catchPos == -1 && tf.finallyPos == -1 || ex == nil && tf.catchPos != tryPanicMarker {
- tf.exception = nil
- vm.popTryFrame()
- continue
- }
- if int(tf.callStackLen) < len(vm.callStack) {
- ctx := &vm.callStack[tf.callStackLen]
- vm.prg, vm.newTarget, vm.result, vm.pc, vm.sb, vm.args =
- ctx.prg, ctx.newTarget, ctx.result, ctx.pc, ctx.sb, ctx.args
- vm.callStack = vm.callStack[:tf.callStackLen]
- }
- vm.sp = int(tf.sp)
- vm.stash = tf.stash
- vm.privEnv = tf.privEnv
- _ = vm.restoreStacks(tf.iterLen, tf.refLen)
- if tf.catchPos == tryPanicMarker {
- break
- }
- if tf.catchPos >= 0 {
- // exception is caught
- vm.push(ex.val)
- vm.pc = int(tf.catchPos)
- tf.catchPos = -1
- return nil
- }
- if tf.finallyPos >= 0 {
- // no 'catch' block, but there is a 'finally' block
- tf.exception = ex
- vm.pc = int(tf.finallyPos)
- tf.finallyPos = -1
- tf.finallyRet = -1
- return nil
- }
- }
- if ex == nil {
- panic(arg)
- }
- return ex
- }
- // Calls to this method must be made from the run() loop and must be the last statement before 'return'.
- // In all other cases exceptions must be thrown using panic().
- func (vm *vm) throw(v interface{}) {
- if ex := vm.handleThrow(v); ex != nil {
- panic(ex)
- }
- }
- func (vm *vm) try(f func()) (ex *Exception) {
- vm.pushTryFrame(tryPanicMarker, -1)
- defer vm.popTryFrame()
- defer func() {
- if x := recover(); x != nil {
- ex = vm.handleThrow(x)
- }
- }()
- f()
- return
- }
- func (vm *vm) runTry() (ex *Exception) {
- vm.pushTryFrame(tryPanicMarker, -1)
- defer vm.popTryFrame()
- for {
- ex = vm.runTryInner()
- if ex != nil || vm.halted() {
- return
- }
- }
- }
- func (vm *vm) runTryInner() (ex *Exception) {
- defer func() {
- if x := recover(); x != nil {
- ex = vm.handleThrow(x)
- }
- }()
- vm.run()
- return
- }
- func (vm *vm) push(v Value) {
- vm.stack.expand(vm.sp)
- vm.stack[vm.sp] = v
- vm.sp++
- }
- func (vm *vm) pop() Value {
- vm.sp--
- return vm.stack[vm.sp]
- }
- func (vm *vm) peek() Value {
- return vm.stack[vm.sp-1]
- }
- func (vm *vm) saveCtx(ctx *context) {
- ctx.prg, ctx.stash, ctx.privEnv, ctx.newTarget, ctx.result, ctx.pc, ctx.sb, ctx.args =
- vm.prg, vm.stash, vm.privEnv, vm.newTarget, vm.result, vm.pc, vm.sb, vm.args
- }
- func (vm *vm) pushCtx() {
- if len(vm.callStack) > vm.maxCallStackSize {
- ex := &StackOverflowError{}
- ex.stack = vm.captureStack(nil, 0)
- panic(ex)
- }
- vm.callStack = append(vm.callStack, context{})
- ctx := &vm.callStack[len(vm.callStack)-1]
- vm.saveCtx(ctx)
- }
- func (vm *vm) restoreCtx(ctx *context) {
- vm.prg, vm.stash, vm.privEnv, vm.newTarget, vm.result, vm.pc, vm.sb, vm.args =
- ctx.prg, ctx.stash, ctx.privEnv, ctx.newTarget, ctx.result, ctx.pc, ctx.sb, ctx.args
- }
- func (vm *vm) popCtx() {
- l := len(vm.callStack) - 1
- ctx := &vm.callStack[l]
- vm.restoreCtx(ctx)
- if ctx.prg != nil {
- *ctx = context{}
- }
- vm.callStack = vm.callStack[:l]
- }
- func (vm *vm) toCallee(v Value) *Object {
- if obj, ok := v.(*Object); ok {
- return obj
- }
- switch unresolved := v.(type) {
- case valueUnresolved:
- unresolved.throw()
- panic("Unreachable")
- case memberUnresolved:
- panic(vm.r.NewTypeError("Object has no member '%s'", unresolved.ref))
- }
- panic(vm.r.NewTypeError("Value is not an object: %s", v.toString()))
- }
- type loadVal struct {
- v Value
- }
- func (l loadVal) exec(vm *vm) {
- vm.push(l.v)
- vm.pc++
- }
- type _loadUndef struct{}
- var loadUndef _loadUndef
- func (_loadUndef) exec(vm *vm) {
- vm.push(_undefined)
- vm.pc++
- }
- type _loadNil struct{}
- var loadNil _loadNil
- func (_loadNil) exec(vm *vm) {
- vm.push(nil)
- vm.pc++
- }
- type _saveResult struct{}
- var saveResult _saveResult
- func (_saveResult) exec(vm *vm) {
- vm.sp--
- vm.result = vm.stack[vm.sp]
- vm.pc++
- }
- type _loadResult struct{}
- var loadResult _loadResult
- func (_loadResult) exec(vm *vm) {
- vm.push(vm.result)
- vm.pc++
- }
- type _clearResult struct{}
- var clearResult _clearResult
- func (_clearResult) exec(vm *vm) {
- vm.result = _undefined
- vm.pc++
- }
- type _loadGlobalObject struct{}
- var loadGlobalObject _loadGlobalObject
- func (_loadGlobalObject) exec(vm *vm) {
- vm.push(vm.r.globalObject)
- vm.pc++
- }
- type loadStack int
- func (l loadStack) exec(vm *vm) {
- // l > 0 -- var<l-1>
- // l == 0 -- this
- if l > 0 {
- vm.push(nilSafe(vm.stack[vm.sb+vm.args+int(l)]))
- } else {
- vm.push(vm.stack[vm.sb])
- }
- vm.pc++
- }
- type loadStack1 int
- func (l loadStack1) exec(vm *vm) {
- // args are in stash
- // l > 0 -- var<l-1>
- // l == 0 -- this
- if l > 0 {
- vm.push(nilSafe(vm.stack[vm.sb+int(l)]))
- } else {
- vm.push(vm.stack[vm.sb])
- }
- vm.pc++
- }
- type loadStackLex int
- func (l loadStackLex) exec(vm *vm) {
- // l < 0 -- arg<-l-1>
- // l > 0 -- var<l-1>
- // l == 0 -- this
- var p *Value
- if l <= 0 {
- arg := int(-l)
- if arg > vm.args {
- vm.push(_undefined)
- vm.pc++
- return
- } else {
- p = &vm.stack[vm.sb+arg]
- }
- } else {
- p = &vm.stack[vm.sb+vm.args+int(l)]
- }
- if *p == nil {
- vm.throw(errAccessBeforeInit)
- return
- }
- vm.push(*p)
- vm.pc++
- }
- type loadStack1Lex int
- func (l loadStack1Lex) exec(vm *vm) {
- p := &vm.stack[vm.sb+int(l)]
- if *p == nil {
- vm.throw(errAccessBeforeInit)
- return
- }
- vm.push(*p)
- vm.pc++
- }
- type _loadCallee struct{}
- var loadCallee _loadCallee
- func (_loadCallee) exec(vm *vm) {
- vm.push(vm.stack[vm.sb-1])
- vm.pc++
- }
- func (vm *vm) storeStack(s int) {
- // l > 0 -- var<l-1>
- if s > 0 {
- vm.stack[vm.sb+vm.args+s] = vm.stack[vm.sp-1]
- } else {
- panic("Illegal stack var index")
- }
- vm.pc++
- }
- func (vm *vm) storeStack1(s int) {
- // args are in stash
- // l > 0 -- var<l-1>
- if s > 0 {
- vm.stack[vm.sb+s] = vm.stack[vm.sp-1]
- } else {
- panic("Illegal stack var index")
- }
- vm.pc++
- }
- func (vm *vm) storeStackLex(s int) {
- // l < 0 -- arg<-l-1>
- // l > 0 -- var<l-1>
- var p *Value
- if s < 0 {
- p = &vm.stack[vm.sb-s]
- } else {
- p = &vm.stack[vm.sb+vm.args+s]
- }
- if *p != nil {
- *p = vm.stack[vm.sp-1]
- } else {
- panic(errAccessBeforeInit)
- }
- vm.pc++
- }
- func (vm *vm) storeStack1Lex(s int) {
- // args are in stash
- // s > 0 -- var<l-1>
- if s <= 0 {
- panic("Illegal stack var index")
- }
- p := &vm.stack[vm.sb+s]
- if *p != nil {
- *p = vm.stack[vm.sp-1]
- } else {
- panic(errAccessBeforeInit)
- }
- vm.pc++
- }
- func (vm *vm) initStack(s int) {
- if s <= 0 {
- vm.stack[vm.sb-s] = vm.stack[vm.sp-1]
- } else {
- vm.stack[vm.sb+vm.args+s] = vm.stack[vm.sp-1]
- }
- vm.pc++
- }
- func (vm *vm) initStack1(s int) {
- if s <= 0 {
- panic("Illegal stack var index")
- }
- vm.stack[vm.sb+s] = vm.stack[vm.sp-1]
- vm.pc++
- }
- type storeStack int
- func (s storeStack) exec(vm *vm) {
- vm.storeStack(int(s))
- }
- type storeStack1 int
- func (s storeStack1) exec(vm *vm) {
- vm.storeStack1(int(s))
- }
- type storeStackLex int
- func (s storeStackLex) exec(vm *vm) {
- vm.storeStackLex(int(s))
- }
- type storeStack1Lex int
- func (s storeStack1Lex) exec(vm *vm) {
- vm.storeStack1Lex(int(s))
- }
- type initStack int
- func (s initStack) exec(vm *vm) {
- vm.initStack(int(s))
- }
- type initStackP int
- func (s initStackP) exec(vm *vm) {
- vm.initStack(int(s))
- vm.sp--
- }
- type initStack1 int
- func (s initStack1) exec(vm *vm) {
- vm.initStack1(int(s))
- }
- type initStack1P int
- func (s initStack1P) exec(vm *vm) {
- vm.initStack1(int(s))
- vm.sp--
- }
- type storeStackP int
- func (s storeStackP) exec(vm *vm) {
- vm.storeStack(int(s))
- vm.sp--
- }
- type storeStack1P int
- func (s storeStack1P) exec(vm *vm) {
- vm.storeStack1(int(s))
- vm.sp--
- }
- type storeStackLexP int
- func (s storeStackLexP) exec(vm *vm) {
- vm.storeStackLex(int(s))
- vm.sp--
- }
- type storeStack1LexP int
- func (s storeStack1LexP) exec(vm *vm) {
- vm.storeStack1Lex(int(s))
- vm.sp--
- }
- type _toNumber struct{}
- var toNumber _toNumber
- func (_toNumber) exec(vm *vm) {
- vm.stack[vm.sp-1] = toNumeric(vm.stack[vm.sp-1])
- vm.pc++
- }
- type _add struct{}
- var add _add
- func (_add) exec(vm *vm) {
- right := vm.stack[vm.sp-1]
- left := vm.stack[vm.sp-2]
- if o, ok := left.(*Object); ok {
- left = o.toPrimitive()
- }
- if o, ok := right.(*Object); ok {
- right = o.toPrimitive()
- }
- var ret Value
- leftString, isLeftString := left.(String)
- rightString, isRightString := right.(String)
- if isLeftString || isRightString {
- if !isLeftString {
- leftString = left.toString()
- }
- if !isRightString {
- rightString = right.toString()
- }
- ret = leftString.Concat(rightString)
- } else {
- switch left := left.(type) {
- case valueInt:
- switch right := right.(type) {
- case valueInt:
- ret = intToValue(int64(left) + int64(right))
- case *valueBigInt:
- panic(errMixBigIntType)
- default:
- ret = floatToValue(float64(left) + right.ToFloat())
- }
- case *valueBigInt:
- if right, ok := right.(*valueBigInt); ok {
- ret = (*valueBigInt)(new(big.Int).Add((*big.Int)(left), (*big.Int)(right)))
- } else {
- panic(errMixBigIntType)
- }
- default:
- if _, ok := right.(*valueBigInt); ok {
- panic(errMixBigIntType)
- }
- ret = floatToValue(left.ToFloat() + right.ToFloat())
- }
- }
- vm.stack[vm.sp-2] = ret
- vm.sp--
- vm.pc++
- }
- type _sub struct{}
- var sub _sub
- func (_sub) exec(vm *vm) {
- right := vm.stack[vm.sp-1]
- left := vm.stack[vm.sp-2]
- left = toNumeric(left)
- right = toNumeric(right)
- var result Value
- switch left := left.(type) {
- case valueInt:
- switch right := right.(type) {
- case valueInt:
- result = intToValue(int64(left) - int64(right))
- goto end
- case *valueBigInt:
- panic(errMixBigIntType)
- }
- case valueFloat:
- if _, ok := right.(*valueBigInt); ok {
- panic(errMixBigIntType)
- }
- case *valueBigInt:
- if right, ok := right.(*valueBigInt); ok {
- result = (*valueBigInt)(new(big.Int).Sub((*big.Int)(left), (*big.Int)(right)))
- goto end
- }
- panic(errMixBigIntType)
- }
- result = floatToValue(left.ToFloat() - right.ToFloat())
- end:
- vm.sp--
- vm.stack[vm.sp-1] = result
- vm.pc++
- }
- type _mul struct{}
- var mul _mul
- func (_mul) exec(vm *vm) {
- left := toNumeric(vm.stack[vm.sp-2])
- right := toNumeric(vm.stack[vm.sp-1])
- var result Value
- switch left := left.(type) {
- case valueInt:
- switch right := right.(type) {
- case valueInt:
- if left == 0 && right == -1 || left == -1 && right == 0 {
- result = _negativeZero
- goto end
- }
- res := left * right
- // check for overflow
- if left == 0 || right == 0 || res/left == right {
- result = intToValue(int64(res))
- goto end
- }
- case *valueBigInt:
- panic(errMixBigIntType)
- }
- case valueFloat:
- if _, ok := right.(*valueBigInt); ok {
- panic(errMixBigIntType)
- }
- case *valueBigInt:
- if right, ok := right.(*valueBigInt); ok {
- result = (*valueBigInt)(new(big.Int).Mul((*big.Int)(left), (*big.Int)(right)))
- goto end
- }
- panic(errMixBigIntType)
- }
- result = floatToValue(left.ToFloat() * right.ToFloat())
- end:
- vm.sp--
- vm.stack[vm.sp-1] = result
- vm.pc++
- }
- type _exp struct{}
- var exp _exp
- func (_exp) exec(vm *vm) {
- vm.sp--
- x := vm.stack[vm.sp-1]
- y := vm.stack[vm.sp]
- x = toNumeric(x)
- y = toNumeric(y)
- var result Value
- if x, ok := x.(*valueBigInt); ok {
- if y, ok := y.(*valueBigInt); ok {
- if (*big.Int)(y).Cmp(big.NewInt(0)) < 0 {
- panic(vm.r.newError(vm.r.getRangeError(), "exponent must be positive"))
- }
- result = (*valueBigInt)(new(big.Int).Exp((*big.Int)(x), (*big.Int)(y), nil))
- goto end
- }
- panic(errMixBigIntType)
- } else if _, ok := y.(*valueBigInt); ok {
- panic(errMixBigIntType)
- }
- result = pow(x, y)
- end:
- vm.stack[vm.sp-1] = result
- vm.pc++
- }
- type _div struct{}
- var div _div
- func (_div) exec(vm *vm) {
- leftValue := toNumeric(vm.stack[vm.sp-2])
- rightValue := toNumeric(vm.stack[vm.sp-1])
- var (
- result Value
- left, right float64
- )
- if left, ok := leftValue.(*valueBigInt); ok {
- if right, ok := rightValue.(*valueBigInt); ok {
- if (*big.Int)(right).Cmp(big.NewInt(0)) == 0 {
- panic(vm.r.newError(vm.r.getRangeError(), "Division by zero"))
- }
- if (*big.Int)(left).CmpAbs((*big.Int)(right)) < 0 {
- result = (*valueBigInt)(big.NewInt(0))
- } else {
- i, _ := new(big.Int).QuoRem((*big.Int)(left), (*big.Int)(right), big.NewInt(0))
- result = (*valueBigInt)(i)
- }
- goto end
- }
- panic(errMixBigIntType)
- } else if _, ok := rightValue.(*valueBigInt); ok {
- panic(errMixBigIntType)
- }
- left, right = leftValue.ToFloat(), rightValue.ToFloat()
- if math.IsNaN(left) || math.IsNaN(right) {
- result = _NaN
- goto end
- }
- if math.IsInf(left, 0) && math.IsInf(right, 0) {
- result = _NaN
- goto end
- }
- if left == 0 && right == 0 {
- result = _NaN
- goto end
- }
- if math.IsInf(left, 0) {
- if math.Signbit(left) == math.Signbit(right) {
- result = _positiveInf
- goto end
- } else {
- result = _negativeInf
- goto end
- }
- }
- if math.IsInf(right, 0) {
- if math.Signbit(left) == math.Signbit(right) {
- result = _positiveZero
- goto end
- } else {
- result = _negativeZero
- goto end
- }
- }
- if right == 0 {
- if math.Signbit(left) == math.Signbit(right) {
- result = _positiveInf
- goto end
- } else {
- result = _negativeInf
- goto end
- }
- }
- result = floatToValue(left / right)
- end:
- vm.sp--
- vm.stack[vm.sp-1] = result
- vm.pc++
- }
- type _mod struct{}
- var mod _mod
- func (_mod) exec(vm *vm) {
- left := toNumeric(vm.stack[vm.sp-2])
- right := toNumeric(vm.stack[vm.sp-1])
- var result Value
- switch left := left.(type) {
- case valueInt:
- switch right := right.(type) {
- case valueInt:
- if right == 0 {
- result = _NaN
- goto end
- }
- r := left % right
- if r == 0 && left < 0 {
- result = _negativeZero
- } else {
- result = intToValue(int64(left % right))
- }
- goto end
- case *valueBigInt:
- panic(errMixBigIntType)
- }
- case valueFloat:
- if _, ok := right.(*valueBigInt); ok {
- panic(errMixBigIntType)
- }
- case *valueBigInt:
- if right, ok := right.(*valueBigInt); ok {
- switch {
- case (*big.Int)(right).Cmp(big.NewInt(0)) == 0:
- panic(vm.r.newError(vm.r.getRangeError(), "Division by zero"))
- case (*big.Int)(left).Cmp(big.NewInt(0)) < 0:
- abs := new(big.Int).Abs((*big.Int)(left))
- v := new(big.Int).Mod(abs, (*big.Int)(right))
- result = (*valueBigInt)(v.Neg(v))
- default:
- result = (*valueBigInt)(new(big.Int).Mod((*big.Int)(left), (*big.Int)(right)))
- }
- goto end
- }
- panic(errMixBigIntType)
- }
- result = floatToValue(math.Mod(left.ToFloat(), right.ToFloat()))
- end:
- vm.sp--
- vm.stack[vm.sp-1] = result
- vm.pc++
- }
- type _neg struct{}
- var neg _neg
- func (_neg) exec(vm *vm) {
- operand := vm.stack[vm.sp-1]
- var result Value
- switch n := toNumeric(operand).(type) {
- case *valueBigInt:
- result = (*valueBigInt)(new(big.Int).Neg((*big.Int)(n)))
- case valueInt:
- if n == 0 {
- result = _negativeZero
- } else {
- result = -n
- }
- default:
- f := operand.ToFloat()
- if !math.IsNaN(f) {
- f = -f
- }
- result = valueFloat(f)
- }
- vm.stack[vm.sp-1] = result
- vm.pc++
- }
- type _plus struct{}
- var plus _plus
- func (_plus) exec(vm *vm) {
- vm.stack[vm.sp-1] = vm.stack[vm.sp-1].ToNumber()
- vm.pc++
- }
- type _inc struct{}
- var inc _inc
- func (_inc) exec(vm *vm) {
- v := vm.stack[vm.sp-1]
- switch n := v.(type) {
- case *valueBigInt:
- v = (*valueBigInt)(new(big.Int).Add((*big.Int)(n), big.NewInt(1)))
- case valueInt:
- v = intToValue(int64(n + 1))
- default:
- v = valueFloat(n.ToFloat() + 1)
- }
- vm.stack[vm.sp-1] = v
- vm.pc++
- }
- type _dec struct{}
- var dec _dec
- func (_dec) exec(vm *vm) {
- v := vm.stack[vm.sp-1]
- switch n := v.(type) {
- case *valueBigInt:
- v = (*valueBigInt)(new(big.Int).Sub((*big.Int)(n), big.NewInt(1)))
- case valueInt:
- v = intToValue(int64(n - 1))
- default:
- v = valueFloat(n.ToFloat() - 1)
- }
- vm.stack[vm.sp-1] = v
- vm.pc++
- }
- type _and struct{}
- var and _and
- func (_and) exec(vm *vm) {
- left := toNumeric(vm.stack[vm.sp-2])
- right := toNumeric(vm.stack[vm.sp-1])
- var result Value
- if left, ok := left.(*valueBigInt); ok {
- if right, ok := right.(*valueBigInt); ok {
- result = (*valueBigInt)(new(big.Int).And((*big.Int)(left), (*big.Int)(right)))
- goto end
- }
- panic(errMixBigIntType)
- } else if _, ok := right.(*valueBigInt); ok {
- panic(errMixBigIntType)
- }
- result = intToValue(int64(toInt32(left) & toInt32(right)))
- end:
- vm.stack[vm.sp-2] = result
- vm.sp--
- vm.pc++
- }
- type _or struct{}
- var or _or
- func (_or) exec(vm *vm) {
- left := toNumeric(vm.stack[vm.sp-2])
- right := toNumeric(vm.stack[vm.sp-1])
- var result Value
- if left, ok := left.(*valueBigInt); ok {
- if right, ok := right.(*valueBigInt); ok {
- result = (*valueBigInt)(new(big.Int).Or((*big.Int)(left), (*big.Int)(right)))
- goto end
- }
- panic(errMixBigIntType)
- } else if _, ok := right.(*valueBigInt); ok {
- panic(errMixBigIntType)
- }
- result = intToValue(int64(toInt32(left) | toInt32(right)))
- end:
- vm.stack[vm.sp-2] = result
- vm.sp--
- vm.pc++
- }
- type _xor struct{}
- var xor _xor
- func (_xor) exec(vm *vm) {
- left := toNumeric(vm.stack[vm.sp-2])
- right := toNumeric(vm.stack[vm.sp-1])
- var result Value
- if left, ok := left.(*valueBigInt); ok {
- if right, ok := right.(*valueBigInt); ok {
- result = (*valueBigInt)(new(big.Int).Xor((*big.Int)(left), (*big.Int)(right)))
- goto end
- }
- panic(errMixBigIntType)
- } else if _, ok := right.(*valueBigInt); ok {
- panic(errMixBigIntType)
- }
- result = intToValue(int64(toInt32(left) ^ toInt32(right)))
- end:
- vm.stack[vm.sp-2] = result
- vm.sp--
- vm.pc++
- }
- type _bnot struct{}
- var bnot _bnot
- func (_bnot) exec(vm *vm) {
- v := vm.stack[vm.sp-1]
- switch n := toNumeric(v).(type) {
- case *valueBigInt:
- v = (*valueBigInt)(new(big.Int).Not((*big.Int)(n)))
- default:
- v = intToValue(int64(^toInt32(n)))
- }
- vm.stack[vm.sp-1] = v
- vm.pc++
- }
- type _sal struct{}
- var sal _sal
- func (_sal) exec(vm *vm) {
- left := toNumeric(vm.stack[vm.sp-2])
- right := toNumeric(vm.stack[vm.sp-1])
- var result Value
- if left, ok := left.(*valueBigInt); ok {
- if right, ok := right.(*valueBigInt); ok {
- n := uint((*big.Int)(right).Uint64())
- if (*big.Int)(right).Sign() < 0 {
- result = (*valueBigInt)(new(big.Int).Rsh((*big.Int)(left), n))
- } else {
- result = (*valueBigInt)(new(big.Int).Lsh((*big.Int)(left), n))
- }
- goto end
- }
- panic(errMixBigIntType)
- } else if _, ok := right.(*valueBigInt); ok {
- panic(errMixBigIntType)
- }
- result = intToValue(int64(toInt32(left) << (toUint32(right) & 0x1F)))
- end:
- vm.stack[vm.sp-2] = result
- vm.sp--
- vm.pc++
- }
- type _sar struct{}
- var sar _sar
- func (_sar) exec(vm *vm) {
- left := toNumeric(vm.stack[vm.sp-2])
- right := toNumeric(vm.stack[vm.sp-1])
- var result Value
- if left, ok := left.(*valueBigInt); ok {
- if right, ok := right.(*valueBigInt); ok {
- n := uint((*big.Int)(right).Uint64())
- if (*big.Int)(right).Sign() < 0 {
- result = (*valueBigInt)(new(big.Int).Lsh((*big.Int)(left), n))
- } else {
- result = (*valueBigInt)(new(big.Int).Rsh((*big.Int)(left), n))
- }
- goto end
- }
- panic(errMixBigIntType)
- } else if _, ok := right.(*valueBigInt); ok {
- panic(errMixBigIntType)
- }
- result = intToValue(int64(toInt32(left) >> (toUint32(right) & 0x1F)))
- end:
- vm.stack[vm.sp-2] = result
- vm.sp--
- vm.pc++
- }
- type _shr struct{}
- var shr _shr
- func (_shr) exec(vm *vm) {
- left := toNumeric(vm.stack[vm.sp-2])
- right := toNumeric(vm.stack[vm.sp-1])
- if _, ok := left.(*valueBigInt); ok {
- _ = toNumeric(right)
- panic(vm.r.NewTypeError("BigInts have no unsigned right shift, use >> instead"))
- } else if _, ok := right.(*valueBigInt); ok {
- panic(vm.r.NewTypeError("BigInts have no unsigned right shift, use >> instead"))
- }
- vm.stack[vm.sp-2] = intToValue(int64(toUint32(left) >> (toUint32(right) & 0x1F)))
- vm.sp--
- vm.pc++
- }
- type jump int32
- func (j jump) exec(vm *vm) {
- vm.pc += int(j)
- }
- type _toPropertyKey struct{}
- func (_toPropertyKey) exec(vm *vm) {
- p := vm.sp - 1
- vm.stack[p] = toPropertyKey(vm.stack[p])
- vm.pc++
- }
- type _toString struct{}
- func (_toString) exec(vm *vm) {
- p := vm.sp - 1
- vm.stack[p] = vm.stack[p].toString()
- vm.pc++
- }
- type _getElemRef struct{}
- var getElemRef _getElemRef
- func (_getElemRef) exec(vm *vm) {
- obj := vm.stack[vm.sp-2].ToObject(vm.r)
- propName := vm.stack[vm.sp-1]
- vm.refStack = append(vm.refStack, &objRef{
- base: obj,
- name: propName,
- })
- vm.sp -= 2
- vm.pc++
- }
- type _getElemRefRecv struct{}
- var getElemRefRecv _getElemRefRecv
- func (_getElemRefRecv) exec(vm *vm) {
- obj := vm.stack[vm.sp-1].ToObject(vm.r)
- propName := vm.stack[vm.sp-2]
- vm.refStack = append(vm.refStack, &objRef{
- base: obj,
- name: propName,
- this: vm.stack[vm.sp-3],
- })
- vm.sp -= 3
- vm.pc++
- }
- type _getElemRefStrict struct{}
- var getElemRefStrict _getElemRefStrict
- func (_getElemRefStrict) exec(vm *vm) {
- obj := vm.stack[vm.sp-2].ToObject(vm.r)
- propName := vm.stack[vm.sp-1]
- vm.refStack = append(vm.refStack, &objRef{
- base: obj,
- name: propName,
- strict: true,
- })
- vm.sp -= 2
- vm.pc++
- }
- type _getElemRefRecvStrict struct{}
- var getElemRefRecvStrict _getElemRefRecvStrict
- func (_getElemRefRecvStrict) exec(vm *vm) {
- obj := vm.stack[vm.sp-1].ToObject(vm.r)
- propName := vm.stack[vm.sp-2]
- vm.refStack = append(vm.refStack, &objRef{
- base: obj,
- name: propName,
- this: vm.stack[vm.sp-3],
- strict: true,
- })
- vm.sp -= 3
- vm.pc++
- }
- type _setElem struct{}
- var setElem _setElem
- func (_setElem) exec(vm *vm) {
- obj := vm.stack[vm.sp-3].ToObject(vm.r)
- propName := toPropertyKey(vm.stack[vm.sp-2])
- val := vm.stack[vm.sp-1]
- obj.setOwn(propName, val, false)
- vm.sp -= 2
- vm.stack[vm.sp-1] = val
- vm.pc++
- }
- type _setElem1 struct{}
- var setElem1 _setElem1
- func (_setElem1) exec(vm *vm) {
- obj := vm.stack[vm.sp-3].ToObject(vm.r)
- propName := vm.stack[vm.sp-2]
- val := vm.stack[vm.sp-1]
- obj.setOwn(propName, val, true)
- vm.sp -= 2
- vm.pc++
- }
- type _setElem1Named struct{}
- var setElem1Named _setElem1Named
- func (_setElem1Named) exec(vm *vm) {
- receiver := vm.stack[vm.sp-3]
- base := receiver.ToObject(vm.r)
- propName := vm.stack[vm.sp-2]
- val := vm.stack[vm.sp-1]
- vm.r.toObject(val).self.defineOwnPropertyStr("name", PropertyDescriptor{
- Value: funcName("", propName),
- Configurable: FLAG_TRUE,
- }, true)
- base.set(propName, val, receiver, true)
- vm.sp -= 2
- vm.pc++
- }
- type defineMethod struct {
- enumerable bool
- }
- func (d *defineMethod) exec(vm *vm) {
- obj := vm.r.toObject(vm.stack[vm.sp-3])
- propName := vm.stack[vm.sp-2]
- method := vm.r.toObject(vm.stack[vm.sp-1])
- method.self.defineOwnPropertyStr("name", PropertyDescriptor{
- Value: funcName("", propName),
- Configurable: FLAG_TRUE,
- }, true)
- obj.defineOwnProperty(propName, PropertyDescriptor{
- Value: method,
- Writable: FLAG_TRUE,
- Configurable: FLAG_TRUE,
- Enumerable: ToFlag(d.enumerable),
- }, true)
- vm.sp -= 2
- vm.pc++
- }
- type _setElemP struct{}
- var setElemP _setElemP
- func (_setElemP) exec(vm *vm) {
- obj := vm.stack[vm.sp-3].ToObject(vm.r)
- propName := toPropertyKey(vm.stack[vm.sp-2])
- val := vm.stack[vm.sp-1]
- obj.setOwn(propName, val, false)
- vm.sp -= 3
- vm.pc++
- }
- type _setElemStrict struct{}
- var setElemStrict _setElemStrict
- func (_setElemStrict) exec(vm *vm) {
- propName := toPropertyKey(vm.stack[vm.sp-2])
- receiver := vm.stack[vm.sp-3]
- val := vm.stack[vm.sp-1]
- if receiverObj, ok := receiver.(*Object); ok {
- receiverObj.setOwn(propName, val, true)
- } else {
- base := receiver.ToObject(vm.r)
- base.set(propName, val, receiver, true)
- }
- vm.sp -= 2
- vm.stack[vm.sp-1] = val
- vm.pc++
- }
- type _setElemRecv struct{}
- var setElemRecv _setElemRecv
- func (_setElemRecv) exec(vm *vm) {
- receiver := vm.stack[vm.sp-4]
- propName := toPropertyKey(vm.stack[vm.sp-3])
- o := vm.stack[vm.sp-2]
- val := vm.stack[vm.sp-1]
- if obj, ok := o.(*Object); ok {
- obj.set(propName, val, receiver, false)
- } else {
- base := o.ToObject(vm.r)
- base.set(propName, val, receiver, false)
- }
- vm.sp -= 3
- vm.stack[vm.sp-1] = val
- vm.pc++
- }
- type _setElemRecvStrict struct{}
- var setElemRecvStrict _setElemRecvStrict
- func (_setElemRecvStrict) exec(vm *vm) {
- receiver := vm.stack[vm.sp-4]
- propName := toPropertyKey(vm.stack[vm.sp-3])
- o := vm.stack[vm.sp-2]
- val := vm.stack[vm.sp-1]
- if obj, ok := o.(*Object); ok {
- obj.set(propName, val, receiver, true)
- } else {
- base := o.ToObject(vm.r)
- base.set(propName, val, receiver, true)
- }
- vm.sp -= 3
- vm.stack[vm.sp-1] = val
- vm.pc++
- }
- type _setElemStrictP struct{}
- var setElemStrictP _setElemStrictP
- func (_setElemStrictP) exec(vm *vm) {
- propName := toPropertyKey(vm.stack[vm.sp-2])
- receiver := vm.stack[vm.sp-3]
- val := vm.stack[vm.sp-1]
- if receiverObj, ok := receiver.(*Object); ok {
- receiverObj.setOwn(propName, val, true)
- } else {
- base := receiver.ToObject(vm.r)
- base.set(propName, val, receiver, true)
- }
- vm.sp -= 3
- vm.pc++
- }
- type _setElemRecvP struct{}
- var setElemRecvP _setElemRecvP
- func (_setElemRecvP) exec(vm *vm) {
- receiver := vm.stack[vm.sp-4]
- propName := toPropertyKey(vm.stack[vm.sp-3])
- o := vm.stack[vm.sp-2]
- val := vm.stack[vm.sp-1]
- if obj, ok := o.(*Object); ok {
- obj.set(propName, val, receiver, false)
- } else {
- base := o.ToObject(vm.r)
- base.set(propName, val, receiver, false)
- }
- vm.sp -= 4
- vm.pc++
- }
- type _setElemRecvStrictP struct{}
- var setElemRecvStrictP _setElemRecvStrictP
- func (_setElemRecvStrictP) exec(vm *vm) {
- receiver := vm.stack[vm.sp-4]
- propName := toPropertyKey(vm.stack[vm.sp-3])
- o := vm.stack[vm.sp-2]
- val := vm.stack[vm.sp-1]
- if obj, ok := o.(*Object); ok {
- obj.set(propName, val, receiver, true)
- } else {
- base := o.ToObject(vm.r)
- base.set(propName, val, receiver, true)
- }
- vm.sp -= 4
- vm.pc++
- }
- type _deleteElem struct{}
- var deleteElem _deleteElem
- func (_deleteElem) exec(vm *vm) {
- obj := vm.stack[vm.sp-2].ToObject(vm.r)
- propName := toPropertyKey(vm.stack[vm.sp-1])
- if obj.delete(propName, false) {
- vm.stack[vm.sp-2] = valueTrue
- } else {
- vm.stack[vm.sp-2] = valueFalse
- }
- vm.sp--
- vm.pc++
- }
- type _deleteElemStrict struct{}
- var deleteElemStrict _deleteElemStrict
- func (_deleteElemStrict) exec(vm *vm) {
- obj := vm.stack[vm.sp-2].ToObject(vm.r)
- propName := toPropertyKey(vm.stack[vm.sp-1])
- obj.delete(propName, true)
- vm.stack[vm.sp-2] = valueTrue
- vm.sp--
- vm.pc++
- }
- type deleteProp unistring.String
- func (d deleteProp) exec(vm *vm) {
- obj := vm.stack[vm.sp-1].ToObject(vm.r)
- if obj.self.deleteStr(unistring.String(d), false) {
- vm.stack[vm.sp-1] = valueTrue
- } else {
- vm.stack[vm.sp-1] = valueFalse
- }
- vm.pc++
- }
- type deletePropStrict unistring.String
- func (d deletePropStrict) exec(vm *vm) {
- obj := vm.stack[vm.sp-1].ToObject(vm.r)
- obj.self.deleteStr(unistring.String(d), true)
- vm.stack[vm.sp-1] = valueTrue
- vm.pc++
- }
- type getPropRef unistring.String
- func (p getPropRef) exec(vm *vm) {
- vm.refStack = append(vm.refStack, &objStrRef{
- base: vm.stack[vm.sp-1].ToObject(vm.r),
- name: unistring.String(p),
- })
- vm.sp--
- vm.pc++
- }
- type getPropRefRecv unistring.String
- func (p getPropRefRecv) exec(vm *vm) {
- vm.refStack = append(vm.refStack, &objStrRef{
- this: vm.stack[vm.sp-2],
- base: vm.stack[vm.sp-1].ToObject(vm.r),
- name: unistring.String(p),
- })
- vm.sp -= 2
- vm.pc++
- }
- type getPropRefStrict unistring.String
- func (p getPropRefStrict) exec(vm *vm) {
- vm.refStack = append(vm.refStack, &objStrRef{
- base: vm.stack[vm.sp-1].ToObject(vm.r),
- name: unistring.String(p),
- strict: true,
- })
- vm.sp--
- vm.pc++
- }
- type getPropRefRecvStrict unistring.String
- func (p getPropRefRecvStrict) exec(vm *vm) {
- vm.refStack = append(vm.refStack, &objStrRef{
- this: vm.stack[vm.sp-2],
- base: vm.stack[vm.sp-1].ToObject(vm.r),
- name: unistring.String(p),
- strict: true,
- })
- vm.sp -= 2
- vm.pc++
- }
- type setProp unistring.String
- func (p setProp) exec(vm *vm) {
- val := vm.stack[vm.sp-1]
- vm.stack[vm.sp-2].ToObject(vm.r).self.setOwnStr(unistring.String(p), val, false)
- vm.stack[vm.sp-2] = val
- vm.sp--
- vm.pc++
- }
- type setPropP unistring.String
- func (p setPropP) exec(vm *vm) {
- val := vm.stack[vm.sp-1]
- vm.stack[vm.sp-2].ToObject(vm.r).self.setOwnStr(unistring.String(p), val, false)
- vm.sp -= 2
- vm.pc++
- }
- type setPropStrict unistring.String
- func (p setPropStrict) exec(vm *vm) {
- receiver := vm.stack[vm.sp-2]
- val := vm.stack[vm.sp-1]
- propName := unistring.String(p)
- if receiverObj, ok := receiver.(*Object); ok {
- receiverObj.self.setOwnStr(propName, val, true)
- } else {
- base := receiver.ToObject(vm.r)
- base.setStr(propName, val, receiver, true)
- }
- vm.stack[vm.sp-2] = val
- vm.sp--
- vm.pc++
- }
- type setPropRecv unistring.String
- func (p setPropRecv) exec(vm *vm) {
- receiver := vm.stack[vm.sp-3]
- o := vm.stack[vm.sp-2]
- val := vm.stack[vm.sp-1]
- propName := unistring.String(p)
- if obj, ok := o.(*Object); ok {
- obj.setStr(propName, val, receiver, false)
- } else {
- base := o.ToObject(vm.r)
- base.setStr(propName, val, receiver, false)
- }
- vm.stack[vm.sp-3] = val
- vm.sp -= 2
- vm.pc++
- }
- type setPropRecvStrict unistring.String
- func (p setPropRecvStrict) exec(vm *vm) {
- receiver := vm.stack[vm.sp-3]
- o := vm.stack[vm.sp-2]
- val := vm.stack[vm.sp-1]
- propName := unistring.String(p)
- if obj, ok := o.(*Object); ok {
- obj.setStr(propName, val, receiver, true)
- } else {
- base := o.ToObject(vm.r)
- base.setStr(propName, val, receiver, true)
- }
- vm.stack[vm.sp-3] = val
- vm.sp -= 2
- vm.pc++
- }
- type setPropRecvP unistring.String
- func (p setPropRecvP) exec(vm *vm) {
- receiver := vm.stack[vm.sp-3]
- o := vm.stack[vm.sp-2]
- val := vm.stack[vm.sp-1]
- propName := unistring.String(p)
- if obj, ok := o.(*Object); ok {
- obj.setStr(propName, val, receiver, false)
- } else {
- base := o.ToObject(vm.r)
- base.setStr(propName, val, receiver, false)
- }
- vm.sp -= 3
- vm.pc++
- }
- type setPropRecvStrictP unistring.String
- func (p setPropRecvStrictP) exec(vm *vm) {
- receiver := vm.stack[vm.sp-3]
- o := vm.stack[vm.sp-2]
- val := vm.stack[vm.sp-1]
- propName := unistring.String(p)
- if obj, ok := o.(*Object); ok {
- obj.setStr(propName, val, receiver, true)
- } else {
- base := o.ToObject(vm.r)
- base.setStr(propName, val, receiver, true)
- }
- vm.sp -= 3
- vm.pc++
- }
- type setPropStrictP unistring.String
- func (p setPropStrictP) exec(vm *vm) {
- receiver := vm.stack[vm.sp-2]
- val := vm.stack[vm.sp-1]
- propName := unistring.String(p)
- if receiverObj, ok := receiver.(*Object); ok {
- receiverObj.self.setOwnStr(propName, val, true)
- } else {
- base := receiver.ToObject(vm.r)
- base.setStr(propName, val, receiver, true)
- }
- vm.sp -= 2
- vm.pc++
- }
- type putProp unistring.String
- func (p putProp) exec(vm *vm) {
- vm.r.toObject(vm.stack[vm.sp-2]).self._putProp(unistring.String(p), vm.stack[vm.sp-1], true, true, true)
- vm.sp--
- vm.pc++
- }
- // used in class declarations instead of putProp because DefineProperty must be observable by Proxy
- type definePropKeyed unistring.String
- func (p definePropKeyed) exec(vm *vm) {
- vm.r.toObject(vm.stack[vm.sp-2]).self.defineOwnPropertyStr(unistring.String(p), PropertyDescriptor{
- Value: vm.stack[vm.sp-1],
- Writable: FLAG_TRUE,
- Configurable: FLAG_TRUE,
- Enumerable: FLAG_TRUE,
- }, true)
- vm.sp--
- vm.pc++
- }
- type defineProp struct{}
- func (defineProp) exec(vm *vm) {
- vm.r.toObject(vm.stack[vm.sp-3]).defineOwnProperty(vm.stack[vm.sp-2], PropertyDescriptor{
- Value: vm.stack[vm.sp-1],
- Writable: FLAG_TRUE,
- Configurable: FLAG_TRUE,
- Enumerable: FLAG_TRUE,
- }, true)
- vm.sp -= 2
- vm.pc++
- }
- type defineMethodKeyed struct {
- key unistring.String
- enumerable bool
- }
- func (d *defineMethodKeyed) exec(vm *vm) {
- obj := vm.r.toObject(vm.stack[vm.sp-2])
- method := vm.r.toObject(vm.stack[vm.sp-1])
- obj.self.defineOwnPropertyStr(d.key, PropertyDescriptor{
- Value: method,
- Writable: FLAG_TRUE,
- Configurable: FLAG_TRUE,
- Enumerable: ToFlag(d.enumerable),
- }, true)
- vm.sp--
- vm.pc++
- }
- type _setProto struct{}
- var setProto _setProto
- func (_setProto) exec(vm *vm) {
- vm.r.setObjectProto(vm.stack[vm.sp-2], vm.stack[vm.sp-1])
- vm.sp--
- vm.pc++
- }
- type defineGetterKeyed struct {
- key unistring.String
- enumerable bool
- }
- func (s *defineGetterKeyed) exec(vm *vm) {
- obj := vm.r.toObject(vm.stack[vm.sp-2])
- val := vm.stack[vm.sp-1]
- method := vm.r.toObject(val)
- method.self.defineOwnPropertyStr("name", PropertyDescriptor{
- Value: asciiString("get ").Concat(stringValueFromRaw(s.key)),
- Configurable: FLAG_TRUE,
- }, true)
- descr := PropertyDescriptor{
- Getter: val,
- Configurable: FLAG_TRUE,
- Enumerable: ToFlag(s.enumerable),
- }
- obj.self.defineOwnPropertyStr(s.key, descr, true)
- vm.sp--
- vm.pc++
- }
- type defineSetterKeyed struct {
- key unistring.String
- enumerable bool
- }
- func (s *defineSetterKeyed) exec(vm *vm) {
- obj := vm.r.toObject(vm.stack[vm.sp-2])
- val := vm.stack[vm.sp-1]
- method := vm.r.toObject(val)
- method.self.defineOwnPropertyStr("name", PropertyDescriptor{
- Value: asciiString("set ").Concat(stringValueFromRaw(s.key)),
- Configurable: FLAG_TRUE,
- }, true)
- descr := PropertyDescriptor{
- Setter: val,
- Configurable: FLAG_TRUE,
- Enumerable: ToFlag(s.enumerable),
- }
- obj.self.defineOwnPropertyStr(s.key, descr, true)
- vm.sp--
- vm.pc++
- }
- type defineGetter struct {
- enumerable bool
- }
- func (s *defineGetter) exec(vm *vm) {
- obj := vm.r.toObject(vm.stack[vm.sp-3])
- propName := vm.stack[vm.sp-2]
- val := vm.stack[vm.sp-1]
- method := vm.r.toObject(val)
- method.self.defineOwnPropertyStr("name", PropertyDescriptor{
- Value: funcName("get ", propName),
- Configurable: FLAG_TRUE,
- }, true)
- descr := PropertyDescriptor{
- Getter: val,
- Configurable: FLAG_TRUE,
- Enumerable: ToFlag(s.enumerable),
- }
- obj.defineOwnProperty(propName, descr, true)
- vm.sp -= 2
- vm.pc++
- }
- type defineSetter struct {
- enumerable bool
- }
- func (s *defineSetter) exec(vm *vm) {
- obj := vm.r.toObject(vm.stack[vm.sp-3])
- propName := vm.stack[vm.sp-2]
- val := vm.stack[vm.sp-1]
- method := vm.r.toObject(val)
- method.self.defineOwnPropertyStr("name", PropertyDescriptor{
- Value: funcName("set ", propName),
- Configurable: FLAG_TRUE,
- }, true)
- descr := PropertyDescriptor{
- Setter: val,
- Configurable: FLAG_TRUE,
- Enumerable: FLAG_TRUE,
- }
- obj.defineOwnProperty(propName, descr, true)
- vm.sp -= 2
- vm.pc++
- }
- type getProp unistring.String
- func (g getProp) exec(vm *vm) {
- v := vm.stack[vm.sp-1]
- obj := v.baseObject(vm.r)
- if obj == nil {
- vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", g))
- return
- }
- vm.stack[vm.sp-1] = nilSafe(obj.self.getStr(unistring.String(g), v))
- vm.pc++
- }
- type getPropRecv unistring.String
- func (g getPropRecv) exec(vm *vm) {
- recv := vm.stack[vm.sp-2]
- v := vm.stack[vm.sp-1]
- obj := v.baseObject(vm.r)
- if obj == nil {
- vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", g))
- return
- }
- vm.stack[vm.sp-2] = nilSafe(obj.self.getStr(unistring.String(g), recv))
- vm.sp--
- vm.pc++
- }
- type getPropRecvCallee unistring.String
- func (g getPropRecvCallee) exec(vm *vm) {
- recv := vm.stack[vm.sp-2]
- v := vm.stack[vm.sp-1]
- obj := v.baseObject(vm.r)
- if obj == nil {
- vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", g))
- return
- }
- n := unistring.String(g)
- prop := obj.self.getStr(n, recv)
- if prop == nil {
- prop = memberUnresolved{valueUnresolved{r: vm.r, ref: n}}
- }
- vm.stack[vm.sp-1] = prop
- vm.pc++
- }
- type getPropCallee unistring.String
- func (g getPropCallee) exec(vm *vm) {
- v := vm.stack[vm.sp-1]
- obj := v.baseObject(vm.r)
- n := unistring.String(g)
- if obj == nil {
- vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined or null", n))
- return
- }
- prop := obj.self.getStr(n, v)
- if prop == nil {
- prop = memberUnresolved{valueUnresolved{r: vm.r, ref: n}}
- }
- vm.push(prop)
- vm.pc++
- }
- type _getElem struct{}
- var getElem _getElem
- func (_getElem) exec(vm *vm) {
- v := vm.stack[vm.sp-2]
- obj := v.baseObject(vm.r)
- if obj == nil {
- vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", vm.stack[vm.sp-1]))
- return
- }
- propName := toPropertyKey(vm.stack[vm.sp-1])
- vm.stack[vm.sp-2] = nilSafe(obj.get(propName, v))
- vm.sp--
- vm.pc++
- }
- type _getElemRecv struct{}
- var getElemRecv _getElemRecv
- func (_getElemRecv) exec(vm *vm) {
- recv := vm.stack[vm.sp-3]
- v := vm.stack[vm.sp-1]
- obj := v.baseObject(vm.r)
- if obj == nil {
- vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", vm.stack[vm.sp-2]))
- return
- }
- propName := toPropertyKey(vm.stack[vm.sp-2])
- vm.stack[vm.sp-3] = nilSafe(obj.get(propName, recv))
- vm.sp -= 2
- vm.pc++
- }
- type _getKey struct{}
- var getKey _getKey
- func (_getKey) exec(vm *vm) {
- v := vm.stack[vm.sp-2]
- obj := v.baseObject(vm.r)
- propName := vm.stack[vm.sp-1]
- if obj == nil {
- vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", propName.String()))
- return
- }
- vm.stack[vm.sp-2] = nilSafe(obj.get(propName, v))
- vm.sp--
- vm.pc++
- }
- type _getElemCallee struct{}
- var getElemCallee _getElemCallee
- func (_getElemCallee) exec(vm *vm) {
- v := vm.stack[vm.sp-2]
- obj := v.baseObject(vm.r)
- if obj == nil {
- vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", vm.stack[vm.sp-1]))
- return
- }
- propName := toPropertyKey(vm.stack[vm.sp-1])
- prop := obj.get(propName, v)
- if prop == nil {
- prop = memberUnresolved{valueUnresolved{r: vm.r, ref: propName.string()}}
- }
- vm.stack[vm.sp-1] = prop
- vm.pc++
- }
- type _getElemRecvCallee struct{}
- var getElemRecvCallee _getElemRecvCallee
- func (_getElemRecvCallee) exec(vm *vm) {
- recv := vm.stack[vm.sp-3]
- v := vm.stack[vm.sp-2]
- obj := v.baseObject(vm.r)
- if obj == nil {
- vm.throw(vm.r.NewTypeError("Cannot read property '%s' of undefined", vm.stack[vm.sp-1]))
- return
- }
- propName := toPropertyKey(vm.stack[vm.sp-1])
- prop := obj.get(propName, recv)
- if prop == nil {
- prop = memberUnresolved{valueUnresolved{r: vm.r, ref: propName.string()}}
- }
- vm.stack[vm.sp-2] = prop
- vm.sp--
- vm.pc++
- }
- type _dup struct{}
- var dup _dup
- func (_dup) exec(vm *vm) {
- vm.push(vm.stack[vm.sp-1])
- vm.pc++
- }
- type dupN uint32
- func (d dupN) exec(vm *vm) {
- vm.push(vm.stack[vm.sp-1-int(d)])
- vm.pc++
- }
- type rdupN uint32
- func (d rdupN) exec(vm *vm) {
- vm.stack[vm.sp-1-int(d)] = vm.stack[vm.sp-1]
- vm.pc++
- }
- type dupLast uint32
- func (d dupLast) exec(vm *vm) {
- e := vm.sp + int(d)
- vm.stack.expand(e)
- copy(vm.stack[vm.sp:e], vm.stack[vm.sp-int(d):])
- vm.sp = e
- vm.pc++
- }
- type _newObject struct{}
- var newObject _newObject
- func (_newObject) exec(vm *vm) {
- vm.push(vm.r.NewObject())
- vm.pc++
- }
- type newArray uint32
- func (l newArray) exec(vm *vm) {
- values := make([]Value, 0, l)
- vm.push(vm.r.newArrayValues(values))
- vm.pc++
- }
- type _pushArrayItem struct{}
- var pushArrayItem _pushArrayItem
- func (_pushArrayItem) exec(vm *vm) {
- arr := vm.stack[vm.sp-2].(*Object).self.(*arrayObject)
- if arr.length < math.MaxUint32 {
- arr.length++
- } else {
- vm.throw(vm.r.newError(vm.r.getRangeError(), "Invalid array length"))
- return
- }
- val := vm.stack[vm.sp-1]
- arr.values = append(arr.values, val)
- if val != nil {
- arr.objCount++
- }
- vm.sp--
- vm.pc++
- }
- type _pushArraySpread struct{}
- var pushArraySpread _pushArraySpread
- func (_pushArraySpread) exec(vm *vm) {
- arr := vm.stack[vm.sp-2].(*Object).self.(*arrayObject)
- vm.r.getIterator(vm.stack[vm.sp-1], nil).iterate(func(val Value) {
- if arr.length < math.MaxUint32 {
- arr.length++
- } else {
- vm.throw(vm.r.newError(vm.r.getRangeError(), "Invalid array length"))
- return
- }
- arr.values = append(arr.values, val)
- arr.objCount++
- })
- vm.sp--
- vm.pc++
- }
- type _pushSpread struct{}
- var pushSpread _pushSpread
- func (_pushSpread) exec(vm *vm) {
- vm.sp--
- obj := vm.stack[vm.sp]
- vm.r.getIterator(obj, nil).iterate(func(val Value) {
- vm.push(val)
- })
- vm.pc++
- }
- type _newArrayFromIter struct{}
- var newArrayFromIter _newArrayFromIter
- func (_newArrayFromIter) exec(vm *vm) {
- var values []Value
- l := len(vm.iterStack) - 1
- iter := vm.iterStack[l].iter
- vm.iterStack[l] = iterStackItem{}
- vm.iterStack = vm.iterStack[:l]
- if iter.iterator != nil {
- iter.iterate(func(val Value) {
- values = append(values, val)
- })
- }
- vm.push(vm.r.newArrayValues(values))
- vm.pc++
- }
- type newRegexp struct {
- pattern *regexpPattern
- src String
- }
- func (n *newRegexp) exec(vm *vm) {
- vm.push(vm.r.newRegExpp(n.pattern.clone(), n.src, vm.r.getRegExpPrototype()).val)
- vm.pc++
- }
- func (vm *vm) setLocalLex(s int) {
- v := vm.stack[vm.sp-1]
- level := s >> 24
- idx := uint32(s & 0x00FFFFFF)
- stash := vm.stash
- for i := 0; i < level; i++ {
- stash = stash.outer
- }
- p := &stash.values[idx]
- if *p == nil {
- panic(errAccessBeforeInit)
- }
- *p = v
- vm.pc++
- }
- func (vm *vm) initLocal(s int) {
- v := vm.stack[vm.sp-1]
- level := s >> 24
- idx := uint32(s & 0x00FFFFFF)
- stash := vm.stash
- for i := 0; i < level; i++ {
- stash = stash.outer
- }
- stash.initByIdx(idx, v)
- vm.pc++
- }
- type storeStash uint32
- func (s storeStash) exec(vm *vm) {
- vm.initLocal(int(s))
- }
- type storeStashP uint32
- func (s storeStashP) exec(vm *vm) {
- vm.initLocal(int(s))
- vm.sp--
- }
- type storeStashLex uint32
- func (s storeStashLex) exec(vm *vm) {
- vm.setLocalLex(int(s))
- }
- type storeStashLexP uint32
- func (s storeStashLexP) exec(vm *vm) {
- vm.setLocalLex(int(s))
- vm.sp--
- }
- type initStash uint32
- func (s initStash) exec(vm *vm) {
- vm.initLocal(int(s))
- }
- type initStashP uint32
- func (s initStashP) exec(vm *vm) {
- vm.initLocal(int(s))
- vm.sp--
- }
- type initGlobalP unistring.String
- func (s initGlobalP) exec(vm *vm) {
- vm.sp--
- vm.r.global.stash.initByName(unistring.String(s), vm.stack[vm.sp])
- vm.pc++
- }
- type initGlobal unistring.String
- func (s initGlobal) exec(vm *vm) {
- vm.r.global.stash.initByName(unistring.String(s), vm.stack[vm.sp])
- vm.pc++
- }
- type resolveVar1 unistring.String
- func (s resolveVar1) exec(vm *vm) {
- name := unistring.String(s)
- var ref ref
- for stash := vm.stash; stash != nil; stash = stash.outer {
- ref = stash.getRefByName(name, false)
- if ref != nil {
- goto end
- }
- }
- ref = &objStrRef{
- base: vm.r.globalObject,
- name: name,
- binding: true,
- }
- end:
- vm.refStack = append(vm.refStack, ref)
- vm.pc++
- }
- type deleteVar unistring.String
- func (d deleteVar) exec(vm *vm) {
- name := unistring.String(d)
- ret := true
- for stash := vm.stash; stash != nil; stash = stash.outer {
- if stash.obj != nil {
- if stashObjHas(stash.obj, name) {
- ret = stash.obj.self.deleteStr(name, false)
- goto end
- }
- } else {
- if idx, exists := stash.names[name]; exists {
- if idx&(maskVar|maskDeletable) == maskVar|maskDeletable {
- stash.deleteBinding(name)
- } else {
- ret = false
- }
- goto end
- }
- }
- }
- if vm.r.globalObject.self.hasPropertyStr(name) {
- ret = vm.r.globalObject.self.deleteStr(name, false)
- }
- end:
- if ret {
- vm.push(valueTrue)
- } else {
- vm.push(valueFalse)
- }
- vm.pc++
- }
- type deleteGlobal unistring.String
- func (d deleteGlobal) exec(vm *vm) {
- name := unistring.String(d)
- var ret bool
- if vm.r.globalObject.self.hasPropertyStr(name) {
- ret = vm.r.globalObject.self.deleteStr(name, false)
- } else {
- ret = true
- }
- if ret {
- vm.push(valueTrue)
- } else {
- vm.push(valueFalse)
- }
- vm.pc++
- }
- type resolveVar1Strict unistring.String
- func (s resolveVar1Strict) exec(vm *vm) {
- name := unistring.String(s)
- var ref ref
- for stash := vm.stash; stash != nil; stash = stash.outer {
- ref = stash.getRefByName(name, true)
- if ref != nil {
- goto end
- }
- }
- if vm.r.globalObject.self.hasPropertyStr(name) {
- ref = &objStrRef{
- base: vm.r.globalObject,
- name: name,
- binding: true,
- strict: true,
- }
- goto end
- }
- ref = &unresolvedRef{
- runtime: vm.r,
- name: name,
- }
- end:
- vm.refStack = append(vm.refStack, ref)
- vm.pc++
- }
- type setGlobal unistring.String
- func (s setGlobal) exec(vm *vm) {
- vm.r.setGlobal(unistring.String(s), vm.peek(), false)
- vm.pc++
- }
- type setGlobalStrict unistring.String
- func (s setGlobalStrict) exec(vm *vm) {
- vm.r.setGlobal(unistring.String(s), vm.peek(), true)
- vm.pc++
- }
- // Load a var from stash
- type loadStash uint32
- func (g loadStash) exec(vm *vm) {
- level := int(g >> 24)
- idx := uint32(g & 0x00FFFFFF)
- stash := vm.stash
- for i := 0; i < level; i++ {
- stash = stash.outer
- }
- vm.push(nilSafe(stash.getByIdx(idx)))
- vm.pc++
- }
- // Load a lexical binding from stash
- type loadStashLex uint32
- func (g loadStashLex) exec(vm *vm) {
- level := int(g >> 24)
- idx := uint32(g & 0x00FFFFFF)
- stash := vm.stash
- for i := 0; i < level; i++ {
- stash = stash.outer
- }
- v := stash.getByIdx(idx)
- if v == nil {
- vm.throw(errAccessBeforeInit)
- return
- }
- vm.push(v)
- vm.pc++
- }
- // scan dynamic stashes up to the given level (encoded as 8 most significant bits of idx), if not found
- // return the indexed var binding value from stash
- type loadMixed struct {
- name unistring.String
- idx uint32
- callee bool
- }
- func (g *loadMixed) exec(vm *vm) {
- level := int(g.idx >> 24)
- idx := g.idx & 0x00FFFFFF
- stash := vm.stash
- name := g.name
- for i := 0; i < level; i++ {
- if v, found := stash.getByName(name); found {
- if g.callee {
- if stash.obj != nil {
- vm.push(stash.obj)
- } else {
- vm.push(_undefined)
- }
- }
- vm.push(v)
- goto end
- }
- stash = stash.outer
- }
- if g.callee {
- vm.push(_undefined)
- }
- if stash != nil {
- vm.push(nilSafe(stash.getByIdx(idx)))
- }
- end:
- vm.pc++
- }
- // scan dynamic stashes up to the given level (encoded as 8 most significant bits of idx), if not found
- // return the indexed lexical binding value from stash
- type loadMixedLex loadMixed
- func (g *loadMixedLex) exec(vm *vm) {
- level := int(g.idx >> 24)
- idx := g.idx & 0x00FFFFFF
- stash := vm.stash
- name := g.name
- for i := 0; i < level; i++ {
- if v, found := stash.getByName(name); found {
- if g.callee {
- if stash.obj != nil {
- vm.push(stash.obj)
- } else {
- vm.push(_undefined)
- }
- }
- vm.push(v)
- goto end
- }
- stash = stash.outer
- }
- if g.callee {
- vm.push(_undefined)
- }
- if stash != nil {
- v := stash.getByIdx(idx)
- if v == nil {
- vm.throw(errAccessBeforeInit)
- return
- }
- vm.push(v)
- }
- end:
- vm.pc++
- }
- // scan dynamic stashes up to the given level (encoded as 8 most significant bits of idx), if not found
- // return the indexed var binding value from stack
- type loadMixedStack struct {
- name unistring.String
- idx int
- level uint8
- callee bool
- }
- // same as loadMixedStack, but the args have been moved to stash (therefore stack layout is different)
- type loadMixedStack1 loadMixedStack
- func (g *loadMixedStack) exec(vm *vm) {
- stash := vm.stash
- name := g.name
- level := int(g.level)
- for i := 0; i < level; i++ {
- if v, found := stash.getByName(name); found {
- if g.callee {
- if stash.obj != nil {
- vm.push(stash.obj)
- } else {
- vm.push(_undefined)
- }
- }
- vm.push(v)
- goto end
- }
- stash = stash.outer
- }
- if g.callee {
- vm.push(_undefined)
- }
- loadStack(g.idx).exec(vm)
- return
- end:
- vm.pc++
- }
- func (g *loadMixedStack1) exec(vm *vm) {
- stash := vm.stash
- name := g.name
- level := int(g.level)
- for i := 0; i < level; i++ {
- if v, found := stash.getByName(name); found {
- if g.callee {
- if stash.obj != nil {
- vm.push(stash.obj)
- } else {
- vm.push(_undefined)
- }
- }
- vm.push(v)
- goto end
- }
- stash = stash.outer
- }
- if g.callee {
- vm.push(_undefined)
- }
- loadStack1(g.idx).exec(vm)
- return
- end:
- vm.pc++
- }
- type loadMixedStackLex loadMixedStack
- // same as loadMixedStackLex but when the arguments have been moved into stash
- type loadMixedStack1Lex loadMixedStack
- func (g *loadMixedStackLex) exec(vm *vm) {
- stash := vm.stash
- name := g.name
- level := int(g.level)
- for i := 0; i < level; i++ {
- if v, found := stash.getByName(name); found {
- if g.callee {
- if stash.obj != nil {
- vm.push(stash.obj)
- } else {
- vm.push(_undefined)
- }
- }
- vm.push(v)
- goto end
- }
- stash = stash.outer
- }
- if g.callee {
- vm.push(_undefined)
- }
- loadStackLex(g.idx).exec(vm)
- return
- end:
- vm.pc++
- }
- func (g *loadMixedStack1Lex) exec(vm *vm) {
- stash := vm.stash
- name := g.name
- level := int(g.level)
- for i := 0; i < level; i++ {
- if v, found := stash.getByName(name); found {
- if g.callee {
- if stash.obj != nil {
- vm.push(stash.obj)
- } else {
- vm.push(_undefined)
- }
- }
- vm.push(v)
- goto end
- }
- stash = stash.outer
- }
- if g.callee {
- vm.push(_undefined)
- }
- loadStack1Lex(g.idx).exec(vm)
- return
- end:
- vm.pc++
- }
- type resolveMixed struct {
- name unistring.String
- idx uint32
- typ varType
- strict bool
- }
- func newStashRef(typ varType, name unistring.String, v *[]Value, idx int) ref {
- switch typ {
- case varTypeVar:
- return &stashRef{
- n: name,
- v: v,
- idx: idx,
- }
- case varTypeLet:
- return &stashRefLex{
- stashRef: stashRef{
- n: name,
- v: v,
- idx: idx,
- },
- }
- case varTypeConst, varTypeStrictConst:
- return &stashRefConst{
- stashRefLex: stashRefLex{
- stashRef: stashRef{
- n: name,
- v: v,
- idx: idx,
- },
- },
- strictConst: typ == varTypeStrictConst,
- }
- }
- panic("unsupported var type")
- }
- func (r *resolveMixed) exec(vm *vm) {
- level := int(r.idx >> 24)
- idx := r.idx & 0x00FFFFFF
- stash := vm.stash
- var ref ref
- for i := 0; i < level; i++ {
- ref = stash.getRefByName(r.name, r.strict)
- if ref != nil {
- goto end
- }
- stash = stash.outer
- }
- if stash != nil {
- ref = newStashRef(r.typ, r.name, &stash.values, int(idx))
- goto end
- }
- ref = &unresolvedRef{
- runtime: vm.r,
- name: r.name,
- }
- end:
- vm.refStack = append(vm.refStack, ref)
- vm.pc++
- }
- type resolveMixedStack struct {
- name unistring.String
- idx int
- typ varType
- level uint8
- strict bool
- }
- type resolveMixedStack1 resolveMixedStack
- func (r *resolveMixedStack) exec(vm *vm) {
- level := int(r.level)
- stash := vm.stash
- var ref ref
- var idx int
- for i := 0; i < level; i++ {
- ref = stash.getRefByName(r.name, r.strict)
- if ref != nil {
- goto end
- }
- stash = stash.outer
- }
- if r.idx > 0 {
- idx = vm.sb + vm.args + r.idx
- } else {
- idx = vm.sb - r.idx
- }
- ref = newStashRef(r.typ, r.name, (*[]Value)(&vm.stack), idx)
- end:
- vm.refStack = append(vm.refStack, ref)
- vm.pc++
- }
- func (r *resolveMixedStack1) exec(vm *vm) {
- level := int(r.level)
- stash := vm.stash
- var ref ref
- for i := 0; i < level; i++ {
- ref = stash.getRefByName(r.name, r.strict)
- if ref != nil {
- goto end
- }
- stash = stash.outer
- }
- ref = newStashRef(r.typ, r.name, (*[]Value)(&vm.stack), vm.sb+r.idx)
- end:
- vm.refStack = append(vm.refStack, ref)
- vm.pc++
- }
- type _getValue struct{}
- var getValue _getValue
- func (_getValue) exec(vm *vm) {
- ref := vm.refStack[len(vm.refStack)-1]
- vm.push(nilSafe(ref.get()))
- vm.pc++
- }
- type _putValue struct{}
- var putValue _putValue
- func (_putValue) exec(vm *vm) {
- l := len(vm.refStack) - 1
- ref := vm.refStack[l]
- vm.refStack[l] = nil
- vm.refStack = vm.refStack[:l]
- ref.set(vm.stack[vm.sp-1])
- vm.pc++
- }
- type _popRef struct{}
- var popRef _popRef
- func (_popRef) exec(vm *vm) {
- l := len(vm.refStack) - 1
- vm.refStack[l] = nil
- vm.refStack = vm.refStack[:l]
- vm.pc++
- }
- type _putValueP struct{}
- var putValueP _putValueP
- func (_putValueP) exec(vm *vm) {
- l := len(vm.refStack) - 1
- ref := vm.refStack[l]
- vm.refStack[l] = nil
- vm.refStack = vm.refStack[:l]
- ref.set(vm.stack[vm.sp-1])
- vm.sp--
- vm.pc++
- }
- type _initValueP struct{}
- var initValueP _initValueP
- func (_initValueP) exec(vm *vm) {
- l := len(vm.refStack) - 1
- ref := vm.refStack[l]
- vm.refStack[l] = nil
- vm.refStack = vm.refStack[:l]
- ref.init(vm.stack[vm.sp-1])
- vm.sp--
- vm.pc++
- }
- type loadDynamic unistring.String
- func (n loadDynamic) exec(vm *vm) {
- name := unistring.String(n)
- var val Value
- for stash := vm.stash; stash != nil; stash = stash.outer {
- if v, exists := stash.getByName(name); exists {
- val = v
- break
- }
- }
- if val == nil {
- val = vm.r.globalObject.self.getStr(name, nil)
- if val == nil {
- vm.throw(vm.r.newReferenceError(name))
- return
- }
- }
- vm.push(val)
- vm.pc++
- }
- type loadDynamicRef unistring.String
- func (n loadDynamicRef) exec(vm *vm) {
- name := unistring.String(n)
- var val Value
- for stash := vm.stash; stash != nil; stash = stash.outer {
- if v, exists := stash.getByName(name); exists {
- val = v
- break
- }
- }
- if val == nil {
- val = vm.r.globalObject.self.getStr(name, nil)
- if val == nil {
- val = valueUnresolved{r: vm.r, ref: name}
- }
- }
- vm.push(val)
- vm.pc++
- }
- type loadDynamicCallee unistring.String
- func (n loadDynamicCallee) exec(vm *vm) {
- name := unistring.String(n)
- var val Value
- var callee *Object
- for stash := vm.stash; stash != nil; stash = stash.outer {
- if v, exists := stash.getByName(name); exists {
- callee = stash.obj
- val = v
- break
- }
- }
- if val == nil {
- val = vm.r.globalObject.self.getStr(name, nil)
- if val == nil {
- val = valueUnresolved{r: vm.r, ref: name}
- }
- }
- if callee != nil {
- vm.push(callee)
- } else {
- vm.push(_undefined)
- }
- vm.push(val)
- vm.pc++
- }
- type _pop struct{}
- var pop _pop
- func (_pop) exec(vm *vm) {
- vm.sp--
- vm.pc++
- }
- func (vm *vm) callEval(n int, strict bool) {
- if vm.r.toObject(vm.stack[vm.sp-n-1]) == vm.r.global.Eval {
- if n > 0 {
- srcVal := vm.stack[vm.sp-n]
- if src, ok := srcVal.(String); ok {
- ret := vm.r.eval(src, true, strict)
- vm.stack[vm.sp-n-2] = ret
- } else {
- vm.stack[vm.sp-n-2] = srcVal
- }
- } else {
- vm.stack[vm.sp-n-2] = _undefined
- }
- vm.sp -= n + 1
- vm.pc++
- } else {
- call(n).exec(vm)
- }
- }
- type callEval uint32
- func (numargs callEval) exec(vm *vm) {
- vm.callEval(int(numargs), false)
- }
- type callEvalStrict uint32
- func (numargs callEvalStrict) exec(vm *vm) {
- vm.callEval(int(numargs), true)
- }
- type _callEvalVariadic struct{}
- var callEvalVariadic _callEvalVariadic
- func (_callEvalVariadic) exec(vm *vm) {
- vm.callEval(vm.countVariadicArgs()-2, false)
- }
- type _callEvalVariadicStrict struct{}
- var callEvalVariadicStrict _callEvalVariadicStrict
- func (_callEvalVariadicStrict) exec(vm *vm) {
- vm.callEval(vm.countVariadicArgs()-2, true)
- }
- type _boxThis struct{}
- var boxThis _boxThis
- func (_boxThis) exec(vm *vm) {
- v := vm.stack[vm.sb]
- if v == _undefined || v == _null {
- vm.stack[vm.sb] = vm.r.globalObject
- } else {
- vm.stack[vm.sb] = v.ToObject(vm.r)
- }
- vm.pc++
- }
- var variadicMarker Value = newSymbol(asciiString("[variadic marker]"))
- type _startVariadic struct{}
- var startVariadic _startVariadic
- func (_startVariadic) exec(vm *vm) {
- vm.push(variadicMarker)
- vm.pc++
- }
- type _callVariadic struct{}
- var callVariadic _callVariadic
- func (vm *vm) countVariadicArgs() int {
- count := 0
- for i := vm.sp - 1; i >= 0; i-- {
- if vm.stack[i] == variadicMarker {
- return count
- }
- count++
- }
- panic("Variadic marker was not found. Compiler bug.")
- }
- func (_callVariadic) exec(vm *vm) {
- call(vm.countVariadicArgs() - 2).exec(vm)
- }
- type _endVariadic struct{}
- var endVariadic _endVariadic
- func (_endVariadic) exec(vm *vm) {
- vm.sp--
- vm.stack[vm.sp-1] = vm.stack[vm.sp]
- vm.pc++
- }
- type call uint32
- func (numargs call) exec(vm *vm) {
- // this
- // callee
- // arg0
- // ...
- // arg<numargs-1>
- n := int(numargs)
- v := vm.stack[vm.sp-n-1] // callee
- obj := vm.toCallee(v)
- obj.self.vmCall(vm, n)
- }
- func (vm *vm) clearStack() {
- sp := vm.sp
- stackTail := vm.stack[sp:]
- for i := range stackTail {
- stackTail[i] = nil
- }
- vm.stack = vm.stack[:sp]
- }
- type enterBlock struct {
- names map[unistring.String]uint32
- stashSize uint32
- stackSize uint32
- }
- func (e *enterBlock) exec(vm *vm) {
- if e.stashSize > 0 {
- vm.newStash()
- vm.stash.values = make([]Value, e.stashSize)
- if len(e.names) > 0 {
- vm.stash.names = e.names
- }
- }
- ss := int(e.stackSize)
- vm.stack.expand(vm.sp + ss - 1)
- vv := vm.stack[vm.sp : vm.sp+ss]
- for i := range vv {
- vv[i] = nil
- }
- vm.sp += ss
- vm.pc++
- }
- type enterCatchBlock struct {
- names map[unistring.String]uint32
- stashSize uint32
- stackSize uint32
- }
- func (e *enterCatchBlock) exec(vm *vm) {
- vm.newStash()
- vm.stash.values = make([]Value, e.stashSize)
- if len(e.names) > 0 {
- vm.stash.names = e.names
- }
- vm.sp--
- vm.stash.values[0] = vm.stack[vm.sp]
- ss := int(e.stackSize)
- vm.stack.expand(vm.sp + ss - 1)
- vv := vm.stack[vm.sp : vm.sp+ss]
- for i := range vv {
- vv[i] = nil
- }
- vm.sp += ss
- vm.pc++
- }
- type leaveBlock struct {
- stackSize uint32
- popStash bool
- }
- func (l *leaveBlock) exec(vm *vm) {
- if l.popStash {
- vm.stash = vm.stash.outer
- }
- if ss := l.stackSize; ss > 0 {
- vm.sp -= int(ss)
- }
- vm.pc++
- }
- type enterFunc struct {
- names map[unistring.String]uint32
- stashSize uint32
- stackSize uint32
- numArgs uint32
- funcType funcType
- argsToStash bool
- extensible bool
- }
- func (e *enterFunc) exec(vm *vm) {
- // Input stack:
- //
- // callee
- // this
- // arg0
- // ...
- // argN
- // <- sp
- // Output stack:
- //
- // this <- sb
- // <local stack vars...>
- // <- sp
- sp := vm.sp
- vm.sb = sp - vm.args - 1
- vm.newStash()
- stash := vm.stash
- stash.funcType = e.funcType
- stash.values = make([]Value, e.stashSize)
- if len(e.names) > 0 {
- if e.extensible {
- m := make(map[unistring.String]uint32, len(e.names))
- for name, idx := range e.names {
- m[name] = idx
- }
- stash.names = m
- } else {
- stash.names = e.names
- }
- }
- ss := int(e.stackSize)
- ea := 0
- if e.argsToStash {
- offset := vm.args - int(e.numArgs)
- copy(stash.values, vm.stack[sp-vm.args:sp])
- if offset > 0 {
- vm.stash.extraArgs = make([]Value, offset)
- copy(stash.extraArgs, vm.stack[sp-offset:])
- } else {
- vv := stash.values[vm.args:e.numArgs]
- for i := range vv {
- vv[i] = _undefined
- }
- }
- sp -= vm.args
- } else {
- d := int(e.numArgs) - vm.args
- if d > 0 {
- ss += d
- ea = d
- vm.args = int(e.numArgs)
- }
- }
- vm.stack.expand(sp + ss - 1)
- if ea > 0 {
- vv := vm.stack[sp : vm.sp+ea]
- for i := range vv {
- vv[i] = _undefined
- }
- }
- vv := vm.stack[sp+ea : sp+ss]
- for i := range vv {
- vv[i] = nil
- }
- vm.sp = sp + ss
- vm.pc++
- }
- // Similar to enterFunc, but for when arguments may be accessed before they are initialised,
- // e.g. by an eval() code or from a closure, or from an earlier initialiser code.
- // In this case the arguments remain on stack, first argsToCopy of them are copied to the stash.
- type enterFunc1 struct {
- names map[unistring.String]uint32
- stashSize uint32
- numArgs uint32
- argsToCopy uint32
- funcType funcType
- extensible bool
- }
- func (e *enterFunc1) exec(vm *vm) {
- sp := vm.sp
- vm.sb = sp - vm.args - 1
- vm.newStash()
- stash := vm.stash
- stash.funcType = e.funcType
- stash.values = make([]Value, e.stashSize)
- if len(e.names) > 0 {
- if e.extensible {
- m := make(map[unistring.String]uint32, len(e.names))
- for name, idx := range e.names {
- m[name] = idx
- }
- stash.names = m
- } else {
- stash.names = e.names
- }
- }
- offset := vm.args - int(e.argsToCopy)
- if offset > 0 {
- copy(stash.values, vm.stack[sp-vm.args:sp-offset])
- if offset := vm.args - int(e.numArgs); offset > 0 {
- vm.stash.extraArgs = make([]Value, offset)
- copy(stash.extraArgs, vm.stack[sp-offset:])
- }
- } else {
- copy(stash.values, vm.stack[sp-vm.args:sp])
- if int(e.argsToCopy) > vm.args {
- vv := stash.values[vm.args:e.argsToCopy]
- for i := range vv {
- vv[i] = _undefined
- }
- }
- }
- vm.pc++
- }
- // Finalises the initialisers section and starts the function body which has its own
- // scope. When used in conjunction with enterFunc1 adjustStack is set to true which
- // causes the arguments to be removed from the stack.
- type enterFuncBody struct {
- enterBlock
- funcType funcType
- extensible bool
- adjustStack bool
- }
- func (e *enterFuncBody) exec(vm *vm) {
- if e.stashSize > 0 || e.extensible {
- vm.newStash()
- stash := vm.stash
- stash.funcType = e.funcType
- stash.values = make([]Value, e.stashSize)
- if len(e.names) > 0 {
- if e.extensible {
- m := make(map[unistring.String]uint32, len(e.names))
- for name, idx := range e.names {
- m[name] = idx
- }
- stash.names = m
- } else {
- stash.names = e.names
- }
- }
- }
- sp := vm.sp
- if e.adjustStack {
- sp -= vm.args
- }
- nsp := sp + int(e.stackSize)
- if e.stackSize > 0 {
- vm.stack.expand(nsp - 1)
- vv := vm.stack[sp:nsp]
- for i := range vv {
- vv[i] = nil
- }
- }
- vm.sp = nsp
- vm.pc++
- }
- type _ret struct{}
- var ret _ret
- func (_ret) exec(vm *vm) {
- // callee -3
- // this -2 <- sb
- // retval -1
- vm.stack[vm.sb-1] = vm.stack[vm.sp-1]
- vm.sp = vm.sb
- vm.popCtx()
- vm.pc++
- }
- type cret uint32
- func (c cret) exec(vm *vm) {
- vm.stack[vm.sb] = *vm.getStashPtr(uint32(c))
- ret.exec(vm)
- }
- type enterFuncStashless struct {
- stackSize uint32
- args uint32
- }
- func (e *enterFuncStashless) exec(vm *vm) {
- sp := vm.sp
- vm.sb = sp - vm.args - 1
- d := int(e.args) - vm.args
- if d > 0 {
- ss := sp + int(e.stackSize) + d
- vm.stack.expand(ss)
- vv := vm.stack[sp : sp+d]
- for i := range vv {
- vv[i] = _undefined
- }
- vv = vm.stack[sp+d : ss]
- for i := range vv {
- vv[i] = nil
- }
- vm.args = int(e.args)
- vm.sp = ss
- } else {
- if e.stackSize > 0 {
- ss := sp + int(e.stackSize)
- vm.stack.expand(ss)
- vv := vm.stack[sp:ss]
- for i := range vv {
- vv[i] = nil
- }
- vm.sp = ss
- }
- }
- vm.pc++
- }
- type newFuncInstruction interface {
- getPrg() *Program
- }
- type newFunc struct {
- prg *Program
- name unistring.String
- source string
- length int
- strict bool
- }
- func (n *newFunc) exec(vm *vm) {
- obj := vm.r.newFunc(n.name, n.length, n.strict)
- obj.prg = n.prg
- obj.stash = vm.stash
- obj.privEnv = vm.privEnv
- obj.src = n.source
- vm.push(obj.val)
- vm.pc++
- }
- func (n *newFunc) getPrg() *Program {
- return n.prg
- }
- type newAsyncFunc struct {
- newFunc
- }
- func (n *newAsyncFunc) exec(vm *vm) {
- obj := vm.r.newAsyncFunc(n.name, n.length, n.strict)
- obj.prg = n.prg
- obj.stash = vm.stash
- obj.privEnv = vm.privEnv
- obj.src = n.source
- vm.push(obj.val)
- vm.pc++
- }
- type newGeneratorFunc struct {
- newFunc
- }
- func (n *newGeneratorFunc) exec(vm *vm) {
- obj := vm.r.newGeneratorFunc(n.name, n.length, n.strict)
- obj.prg = n.prg
- obj.stash = vm.stash
- obj.privEnv = vm.privEnv
- obj.src = n.source
- vm.push(obj.val)
- vm.pc++
- }
- type newMethod struct {
- newFunc
- homeObjOffset uint32
- }
- func (n *newMethod) _exec(vm *vm, obj *methodFuncObject) {
- obj.prg = n.prg
- obj.stash = vm.stash
- obj.privEnv = vm.privEnv
- obj.src = n.source
- if n.homeObjOffset > 0 {
- obj.homeObject = vm.r.toObject(vm.stack[vm.sp-int(n.homeObjOffset)])
- }
- vm.push(obj.val)
- vm.pc++
- }
- func (n *newMethod) exec(vm *vm) {
- n._exec(vm, vm.r.newMethod(n.name, n.length, n.strict))
- }
- type newAsyncMethod struct {
- newMethod
- }
- func (n *newAsyncMethod) exec(vm *vm) {
- obj := vm.r.newAsyncMethod(n.name, n.length, n.strict)
- n._exec(vm, &obj.methodFuncObject)
- }
- type newGeneratorMethod struct {
- newMethod
- }
- func (n *newGeneratorMethod) exec(vm *vm) {
- obj := vm.r.newGeneratorMethod(n.name, n.length, n.strict)
- n._exec(vm, &obj.methodFuncObject)
- }
- type newArrowFunc struct {
- newFunc
- }
- type newAsyncArrowFunc struct {
- newArrowFunc
- }
- func getFuncObject(v Value) *Object {
- if o, ok := v.(*Object); ok {
- if fn, ok := o.self.(*arrowFuncObject); ok {
- return fn.funcObj
- }
- return o
- }
- if v == _undefined {
- return nil
- }
- panic(typeError("Value is not an Object"))
- }
- func getHomeObject(v Value) *Object {
- if o, ok := v.(*Object); ok {
- switch fn := o.self.(type) {
- case *methodFuncObject:
- return fn.homeObject
- case *generatorMethodFuncObject:
- return fn.homeObject
- case *asyncMethodFuncObject:
- return fn.homeObject
- case *classFuncObject:
- return o.runtime.toObject(fn.getStr("prototype", nil))
- case *arrowFuncObject:
- return getHomeObject(fn.funcObj)
- case *asyncArrowFuncObject:
- return getHomeObject(fn.funcObj)
- }
- }
- panic(newTypeError("Compiler bug: getHomeObject() on the wrong value: %T", v))
- }
- func (n *newArrowFunc) _exec(vm *vm, obj *arrowFuncObject) {
- obj.prg = n.prg
- obj.stash = vm.stash
- obj.privEnv = vm.privEnv
- obj.src = n.source
- if vm.sb > 0 {
- obj.funcObj = getFuncObject(vm.stack[vm.sb-1])
- }
- vm.push(obj.val)
- vm.pc++
- }
- func (n *newArrowFunc) exec(vm *vm) {
- n._exec(vm, vm.r.newArrowFunc(n.name, n.length, n.strict))
- }
- func (n *newAsyncArrowFunc) exec(vm *vm) {
- obj := vm.r.newAsyncArrowFunc(n.name, n.length, n.strict)
- n._exec(vm, &obj.arrowFuncObject)
- }
- func (vm *vm) alreadyDeclared(name unistring.String) Value {
- return vm.r.newError(vm.r.getSyntaxError(), "Identifier '%s' has already been declared", name)
- }
- func (vm *vm) checkBindVarsGlobal(names []unistring.String) {
- o := vm.r.globalObject.self
- sn := vm.r.global.stash.names
- if bo, ok := o.(*baseObject); ok {
- // shortcut
- if bo.extensible {
- for _, name := range names {
- if _, exists := sn[name]; exists {
- panic(vm.alreadyDeclared(name))
- }
- }
- } else {
- for _, name := range names {
- if !bo.hasOwnPropertyStr(name) {
- panic(vm.r.NewTypeError("Cannot define global variable '%s', global object is not extensible", name))
- }
- if _, exists := sn[name]; exists {
- panic(vm.alreadyDeclared(name))
- }
- }
- }
- } else {
- for _, name := range names {
- if !o.hasOwnPropertyStr(name) && !o.isExtensible() {
- panic(vm.r.NewTypeError("Cannot define global variable '%s', global object is not extensible", name))
- }
- if _, exists := sn[name]; exists {
- panic(vm.alreadyDeclared(name))
- }
- }
- }
- }
- func (vm *vm) createGlobalVarBindings(names []unistring.String, d bool) {
- o := vm.r.globalObject.self
- if bo, ok := o.(*templatedObject); ok {
- for _, name := range names {
- if !bo.hasOwnPropertyStr(name) && bo.extensible {
- bo._putProp(name, _undefined, true, true, d)
- }
- }
- } else {
- var cf Flag
- if d {
- cf = FLAG_TRUE
- } else {
- cf = FLAG_FALSE
- }
- for _, name := range names {
- if !o.hasOwnPropertyStr(name) && o.isExtensible() {
- o.defineOwnPropertyStr(name, PropertyDescriptor{
- Value: _undefined,
- Writable: FLAG_TRUE,
- Enumerable: FLAG_TRUE,
- Configurable: cf,
- }, true)
- o.setOwnStr(name, _undefined, false)
- }
- }
- }
- }
- func (vm *vm) createGlobalFuncBindings(names []unistring.String, d bool) {
- o := vm.r.globalObject.self
- b := vm.sp - len(names)
- var shortcutObj *templatedObject
- if o, ok := o.(*templatedObject); ok {
- shortcutObj = o
- }
- for i, name := range names {
- var desc PropertyDescriptor
- prop := o.getOwnPropStr(name)
- desc.Value = vm.stack[b+i]
- if shortcutObj != nil && prop == nil && shortcutObj.extensible {
- shortcutObj._putProp(name, desc.Value, true, true, d)
- } else {
- if prop, ok := prop.(*valueProperty); ok && !prop.configurable {
- // no-op
- } else {
- desc.Writable = FLAG_TRUE
- desc.Enumerable = FLAG_TRUE
- if d {
- desc.Configurable = FLAG_TRUE
- } else {
- desc.Configurable = FLAG_FALSE
- }
- }
- if shortcutObj != nil {
- shortcutObj.defineOwnPropertyStr(name, desc, true)
- } else {
- o.defineOwnPropertyStr(name, desc, true)
- o.setOwnStr(name, desc.Value, false) // not a bug, see https://262.ecma-international.org/#sec-createglobalfunctionbinding
- }
- }
- }
- vm.sp = b
- }
- func (vm *vm) checkBindFuncsGlobal(names []unistring.String) {
- o := vm.r.globalObject.self
- sn := vm.r.global.stash.names
- for _, name := range names {
- if _, exists := sn[name]; exists {
- panic(vm.alreadyDeclared(name))
- }
- prop := o.getOwnPropStr(name)
- allowed := true
- switch prop := prop.(type) {
- case nil:
- allowed = o.isExtensible()
- case *valueProperty:
- allowed = prop.configurable || prop.getterFunc == nil && prop.setterFunc == nil && prop.writable && prop.enumerable
- }
- if !allowed {
- panic(vm.r.NewTypeError("Cannot redefine global function '%s'", name))
- }
- }
- }
- func (vm *vm) checkBindLexGlobal(names []unistring.String) {
- o := vm.r.globalObject.self
- s := &vm.r.global.stash
- for _, name := range names {
- if _, exists := s.names[name]; exists {
- goto fail
- }
- if prop, ok := o.getOwnPropStr(name).(*valueProperty); ok && !prop.configurable {
- goto fail
- }
- continue
- fail:
- panic(vm.alreadyDeclared(name))
- }
- }
- type bindVars struct {
- names []unistring.String
- deletable bool
- }
- func (d *bindVars) exec(vm *vm) {
- var target *stash
- for _, name := range d.names {
- for s := vm.stash; s != nil; s = s.outer {
- if idx, exists := s.names[name]; exists && idx&maskVar == 0 {
- vm.throw(vm.alreadyDeclared(name))
- return
- }
- if s.isVariable() {
- target = s
- break
- }
- }
- }
- if target == nil {
- target = vm.stash
- }
- deletable := d.deletable
- for _, name := range d.names {
- target.createBinding(name, deletable)
- }
- vm.pc++
- }
- type bindGlobal struct {
- vars, funcs, lets, consts []unistring.String
- deletable bool
- }
- func (b *bindGlobal) exec(vm *vm) {
- vm.checkBindFuncsGlobal(b.funcs)
- vm.checkBindLexGlobal(b.lets)
- vm.checkBindLexGlobal(b.consts)
- vm.checkBindVarsGlobal(b.vars)
- s := &vm.r.global.stash
- for _, name := range b.lets {
- s.createLexBinding(name, false)
- }
- for _, name := range b.consts {
- s.createLexBinding(name, true)
- }
- vm.createGlobalFuncBindings(b.funcs, b.deletable)
- vm.createGlobalVarBindings(b.vars, b.deletable)
- vm.pc++
- }
- type jneP int32
- func (j jneP) exec(vm *vm) {
- vm.sp--
- if !vm.stack[vm.sp].ToBoolean() {
- vm.pc += int(j)
- } else {
- vm.pc++
- }
- }
- type jeqP int32
- func (j jeqP) exec(vm *vm) {
- vm.sp--
- if vm.stack[vm.sp].ToBoolean() {
- vm.pc += int(j)
- } else {
- vm.pc++
- }
- }
- type jeq int32
- func (j jeq) exec(vm *vm) {
- if vm.stack[vm.sp-1].ToBoolean() {
- vm.pc += int(j)
- } else {
- vm.sp--
- vm.pc++
- }
- }
- type jne int32
- func (j jne) exec(vm *vm) {
- if !vm.stack[vm.sp-1].ToBoolean() {
- vm.pc += int(j)
- } else {
- vm.sp--
- vm.pc++
- }
- }
- type jdef int32
- func (j jdef) exec(vm *vm) {
- if vm.stack[vm.sp-1] != _undefined {
- vm.pc += int(j)
- } else {
- vm.sp--
- vm.pc++
- }
- }
- type jdefP int32
- func (j jdefP) exec(vm *vm) {
- if vm.stack[vm.sp-1] != _undefined {
- vm.pc += int(j)
- } else {
- vm.pc++
- }
- vm.sp--
- }
- type jopt int32
- func (j jopt) exec(vm *vm) {
- switch vm.stack[vm.sp-1] {
- case _null:
- vm.stack[vm.sp-1] = _undefined
- fallthrough
- case _undefined:
- vm.pc += int(j)
- default:
- vm.pc++
- }
- }
- type joptc int32
- func (j joptc) exec(vm *vm) {
- switch vm.stack[vm.sp-1].(type) {
- case valueNull, valueUndefined, memberUnresolved:
- vm.sp--
- vm.stack[vm.sp-1] = _undefined
- vm.pc += int(j)
- default:
- vm.pc++
- }
- }
- type jcoalesc int32
- func (j jcoalesc) exec(vm *vm) {
- switch vm.stack[vm.sp-1] {
- case _undefined, _null:
- vm.sp--
- vm.pc++
- default:
- vm.pc += int(j)
- }
- }
- type jcoalescP int32
- func (j jcoalescP) exec(vm *vm) {
- vm.sp--
- switch vm.stack[vm.sp] {
- case _undefined, _null:
- vm.pc++
- default:
- vm.pc += int(j)
- }
- }
- type _not struct{}
- var not _not
- func (_not) exec(vm *vm) {
- if vm.stack[vm.sp-1].ToBoolean() {
- vm.stack[vm.sp-1] = valueFalse
- } else {
- vm.stack[vm.sp-1] = valueTrue
- }
- vm.pc++
- }
- func toPrimitiveNumber(v Value) Value {
- if o, ok := v.(*Object); ok {
- return o.toPrimitiveNumber()
- }
- return v
- }
- func toPrimitive(v Value) Value {
- if o, ok := v.(*Object); ok {
- return o.toPrimitive()
- }
- return v
- }
- func cmp(px, py Value) Value {
- var ret bool
- xs, isPxString := px.(String)
- ys, isPyString := py.(String)
- if isPxString && isPyString {
- ret = xs.CompareTo(ys) < 0
- goto end
- } else {
- if px, ok := px.(*valueBigInt); ok && isPyString {
- ny, err := stringToBigInt(ys.toTrimmedUTF8())
- if err != nil {
- return _undefined
- }
- ret = (*big.Int)(px).Cmp(ny) < 0
- goto end
- }
- if py, ok := py.(*valueBigInt); ok && isPxString {
- nx, err := stringToBigInt(xs.toTrimmedUTF8())
- if err != nil {
- return _undefined
- }
- ret = nx.Cmp((*big.Int)(py)) < 0
- goto end
- }
- }
- px = toNumeric(px)
- py = toNumeric(py)
- switch nx := px.(type) {
- case valueInt:
- switch ny := py.(type) {
- case valueInt:
- ret = nx < ny
- goto end
- case *valueBigInt:
- ret = big.NewInt(int64(nx)).Cmp((*big.Int)(ny)) < 0
- goto end
- }
- case valueFloat:
- switch ny := py.(type) {
- case *valueBigInt:
- switch {
- case math.IsNaN(float64(nx)):
- return _undefined
- case nx == _negativeInf:
- ret = true
- goto end
- }
- if nx := big.NewFloat(float64(nx)); nx.IsInt() {
- nx, _ := nx.Int(nil)
- ret = nx.Cmp((*big.Int)(ny)) < 0
- } else {
- ret = nx.Cmp(new(big.Float).SetInt((*big.Int)(ny))) < 0
- }
- goto end
- }
- case *valueBigInt:
- switch ny := py.(type) {
- case valueInt:
- ret = (*big.Int)(nx).Cmp(big.NewInt(int64(ny))) < 0
- goto end
- case valueFloat:
- switch {
- case math.IsNaN(float64(ny)):
- return _undefined
- case ny == _positiveInf:
- ret = true
- goto end
- }
- if ny := big.NewFloat(float64(ny)); ny.IsInt() {
- ny, _ := ny.Int(nil)
- ret = (*big.Int)(nx).Cmp(ny) < 0
- } else {
- ret = new(big.Float).SetInt((*big.Int)(nx)).Cmp(ny) < 0
- }
- goto end
- case *valueBigInt:
- ret = (*big.Int)(nx).Cmp((*big.Int)(ny)) < 0
- goto end
- }
- }
- if nx, ny := px.ToFloat(), py.ToFloat(); math.IsNaN(nx) || math.IsNaN(ny) {
- return _undefined
- } else {
- ret = nx < ny
- }
- end:
- if ret {
- return valueTrue
- }
- return valueFalse
- }
- type _op_lt struct{}
- var op_lt _op_lt
- func (_op_lt) exec(vm *vm) {
- left := toPrimitiveNumber(vm.stack[vm.sp-2])
- right := toPrimitiveNumber(vm.stack[vm.sp-1])
- r := cmp(left, right)
- if r == _undefined {
- vm.stack[vm.sp-2] = valueFalse
- } else {
- vm.stack[vm.sp-2] = r
- }
- vm.sp--
- vm.pc++
- }
- type _op_lte struct{}
- var op_lte _op_lte
- func (_op_lte) exec(vm *vm) {
- left := toPrimitiveNumber(vm.stack[vm.sp-2])
- right := toPrimitiveNumber(vm.stack[vm.sp-1])
- r := cmp(right, left)
- if r == _undefined || r == valueTrue {
- vm.stack[vm.sp-2] = valueFalse
- } else {
- vm.stack[vm.sp-2] = valueTrue
- }
- vm.sp--
- vm.pc++
- }
- type _op_gt struct{}
- var op_gt _op_gt
- func (_op_gt) exec(vm *vm) {
- left := toPrimitiveNumber(vm.stack[vm.sp-2])
- right := toPrimitiveNumber(vm.stack[vm.sp-1])
- r := cmp(right, left)
- if r == _undefined {
- vm.stack[vm.sp-2] = valueFalse
- } else {
- vm.stack[vm.sp-2] = r
- }
- vm.sp--
- vm.pc++
- }
- type _op_gte struct{}
- var op_gte _op_gte
- func (_op_gte) exec(vm *vm) {
- left := toPrimitiveNumber(vm.stack[vm.sp-2])
- right := toPrimitiveNumber(vm.stack[vm.sp-1])
- r := cmp(left, right)
- if r == _undefined || r == valueTrue {
- vm.stack[vm.sp-2] = valueFalse
- } else {
- vm.stack[vm.sp-2] = valueTrue
- }
- vm.sp--
- vm.pc++
- }
- type _op_eq struct{}
- var op_eq _op_eq
- func (_op_eq) exec(vm *vm) {
- if vm.stack[vm.sp-2].Equals(vm.stack[vm.sp-1]) {
- vm.stack[vm.sp-2] = valueTrue
- } else {
- vm.stack[vm.sp-2] = valueFalse
- }
- vm.sp--
- vm.pc++
- }
- type _op_neq struct{}
- var op_neq _op_neq
- func (_op_neq) exec(vm *vm) {
- if vm.stack[vm.sp-2].Equals(vm.stack[vm.sp-1]) {
- vm.stack[vm.sp-2] = valueFalse
- } else {
- vm.stack[vm.sp-2] = valueTrue
- }
- vm.sp--
- vm.pc++
- }
- type _op_strict_eq struct{}
- var op_strict_eq _op_strict_eq
- func (_op_strict_eq) exec(vm *vm) {
- if vm.stack[vm.sp-2].StrictEquals(vm.stack[vm.sp-1]) {
- vm.stack[vm.sp-2] = valueTrue
- } else {
- vm.stack[vm.sp-2] = valueFalse
- }
- vm.sp--
- vm.pc++
- }
- type _op_strict_neq struct{}
- var op_strict_neq _op_strict_neq
- func (_op_strict_neq) exec(vm *vm) {
- if vm.stack[vm.sp-2].StrictEquals(vm.stack[vm.sp-1]) {
- vm.stack[vm.sp-2] = valueFalse
- } else {
- vm.stack[vm.sp-2] = valueTrue
- }
- vm.sp--
- vm.pc++
- }
- type _op_instanceof struct{}
- var op_instanceof _op_instanceof
- func (_op_instanceof) exec(vm *vm) {
- left := vm.stack[vm.sp-2]
- right := vm.r.toObject(vm.stack[vm.sp-1])
- if instanceOfOperator(left, right) {
- vm.stack[vm.sp-2] = valueTrue
- } else {
- vm.stack[vm.sp-2] = valueFalse
- }
- vm.sp--
- vm.pc++
- }
- type _op_in struct{}
- var op_in _op_in
- func (_op_in) exec(vm *vm) {
- left := vm.stack[vm.sp-2]
- right := vm.r.toObject(vm.stack[vm.sp-1])
- if right.hasProperty(left) {
- vm.stack[vm.sp-2] = valueTrue
- } else {
- vm.stack[vm.sp-2] = valueFalse
- }
- vm.sp--
- vm.pc++
- }
- type try struct {
- catchOffset int32
- finallyOffset int32
- }
- func (t try) exec(vm *vm) {
- var catchPos, finallyPos int32
- if t.catchOffset > 0 {
- catchPos = int32(vm.pc) + t.catchOffset
- } else {
- catchPos = -1
- }
- if t.finallyOffset > 0 {
- finallyPos = int32(vm.pc) + t.finallyOffset
- } else {
- finallyPos = -1
- }
- vm.pushTryFrame(catchPos, finallyPos)
- vm.pc++
- }
- type leaveTry struct{}
- func (leaveTry) exec(vm *vm) {
- tf := &vm.tryStack[len(vm.tryStack)-1]
- if tf.finallyPos >= 0 {
- tf.finallyRet = int32(vm.pc + 1)
- vm.pc = int(tf.finallyPos)
- tf.finallyPos = -1
- tf.catchPos = -1
- } else {
- vm.popTryFrame()
- vm.pc++
- }
- }
- type enterFinally struct{}
- func (enterFinally) exec(vm *vm) {
- tf := &vm.tryStack[len(vm.tryStack)-1]
- tf.finallyPos = -1
- vm.pc++
- }
- type leaveFinally struct{}
- func (leaveFinally) exec(vm *vm) {
- tf := &vm.tryStack[len(vm.tryStack)-1]
- ex, ret := tf.exception, tf.finallyRet
- tf.exception = nil
- vm.popTryFrame()
- if ex != nil {
- vm.throw(ex)
- return
- } else {
- if ret != -1 {
- vm.pc = int(ret)
- } else {
- vm.pc++
- }
- }
- }
- type _throw struct{}
- var throw _throw
- func (_throw) exec(vm *vm) {
- v := vm.stack[vm.sp-1]
- ex := &Exception{
- val: v,
- }
- if o, ok := v.(*Object); ok {
- if e, ok := o.self.(*errorObject); ok {
- if len(e.stack) > 0 {
- ex.stack = e.stack
- }
- }
- }
- if ex.stack == nil {
- ex.stack = vm.captureStack(make([]StackFrame, 0, len(vm.callStack)+1), 0)
- }
- if ex = vm.handleThrow(ex); ex != nil {
- panic(ex)
- }
- }
- type _newVariadic struct{}
- var newVariadic _newVariadic
- func (_newVariadic) exec(vm *vm) {
- _new(vm.countVariadicArgs() - 1).exec(vm)
- }
- type _new uint32
- func (n _new) exec(vm *vm) {
- sp := vm.sp - int(n)
- obj := vm.stack[sp-1]
- ctor := vm.r.toConstructor(obj)
- vm.stack[sp-1] = ctor(vm.stack[sp:vm.sp], nil)
- vm.sp = sp
- vm.pc++
- }
- type superCall uint32
- func (s superCall) exec(vm *vm) {
- l := len(vm.refStack) - 1
- thisRef := vm.refStack[l]
- vm.refStack[l] = nil
- vm.refStack = vm.refStack[:l]
- obj := vm.r.toObject(vm.stack[vm.sb-1])
- var cls *classFuncObject
- switch fn := obj.self.(type) {
- case *classFuncObject:
- cls = fn
- case *arrowFuncObject:
- cls, _ = fn.funcObj.self.(*classFuncObject)
- }
- if cls == nil {
- vm.throw(vm.r.NewTypeError("wrong callee type for super()"))
- return
- }
- sp := vm.sp - int(s)
- newTarget := vm.r.toObject(vm.newTarget)
- v := cls.createInstance(vm.stack[sp:vm.sp], newTarget)
- thisRef.set(v)
- vm.sp = sp
- cls._initFields(v)
- vm.push(v)
- vm.pc++
- }
- type _superCallVariadic struct{}
- var superCallVariadic _superCallVariadic
- func (_superCallVariadic) exec(vm *vm) {
- superCall(vm.countVariadicArgs()).exec(vm)
- }
- type _loadNewTarget struct{}
- var loadNewTarget _loadNewTarget
- func (_loadNewTarget) exec(vm *vm) {
- if t := vm.newTarget; t != nil {
- vm.push(t)
- } else {
- vm.push(_undefined)
- }
- vm.pc++
- }
- type _typeof struct{}
- var typeof _typeof
- func (_typeof) exec(vm *vm) {
- var r Value
- switch v := vm.stack[vm.sp-1].(type) {
- case valueUndefined, valueUnresolved:
- r = stringUndefined
- case valueNull:
- r = stringObjectC
- case *Object:
- r = v.self.typeOf()
- case valueBool:
- r = stringBoolean
- case String:
- r = stringString
- case valueInt, valueFloat:
- r = stringNumber
- case *valueBigInt:
- r = stringBigInt
- case *Symbol:
- r = stringSymbol
- default:
- panic(newTypeError("Compiler bug: unknown type: %T", v))
- }
- vm.stack[vm.sp-1] = r
- vm.pc++
- }
- type createArgsMapped uint32
- func (formalArgs createArgsMapped) exec(vm *vm) {
- v := &Object{runtime: vm.r}
- args := &argumentsObject{}
- args.extensible = true
- args.prototype = vm.r.global.ObjectPrototype
- args.class = "Arguments"
- v.self = args
- args.val = v
- args.length = vm.args
- args.init()
- i := 0
- c := int(formalArgs)
- if vm.args < c {
- c = vm.args
- }
- for ; i < c; i++ {
- args._put(unistring.String(strconv.Itoa(i)), &mappedProperty{
- valueProperty: valueProperty{
- writable: true,
- configurable: true,
- enumerable: true,
- },
- v: &vm.stash.values[i],
- })
- }
- for _, v := range vm.stash.extraArgs {
- args._put(unistring.String(strconv.Itoa(i)), v)
- i++
- }
- args._putProp("callee", vm.stack[vm.sb-1], true, false, true)
- args._putSym(SymIterator, valueProp(vm.r.getArrayValues(), true, false, true))
- vm.push(v)
- vm.pc++
- }
- type createArgsUnmapped uint32
- func (formalArgs createArgsUnmapped) exec(vm *vm) {
- args := vm.r.newBaseObject(vm.r.global.ObjectPrototype, "Arguments")
- i := 0
- c := int(formalArgs)
- if vm.args < c {
- c = vm.args
- }
- for _, v := range vm.stash.values[:c] {
- args._put(unistring.String(strconv.Itoa(i)), v)
- i++
- }
- for _, v := range vm.stash.extraArgs {
- args._put(unistring.String(strconv.Itoa(i)), v)
- i++
- }
- args._putProp("length", intToValue(int64(vm.args)), true, false, true)
- args._put("callee", vm.r.newThrowerProperty(false))
- args._putSym(SymIterator, valueProp(vm.r.getArrayValues(), true, false, true))
- vm.push(args.val)
- vm.pc++
- }
- type _enterWith struct{}
- var enterWith _enterWith
- func (_enterWith) exec(vm *vm) {
- vm.newStash()
- vm.stash.obj = vm.stack[vm.sp-1].ToObject(vm.r)
- vm.sp--
- vm.pc++
- }
- type _leaveWith struct{}
- var leaveWith _leaveWith
- func (_leaveWith) exec(vm *vm) {
- vm.stash = vm.stash.outer
- vm.pc++
- }
- func emptyIter() (propIterItem, iterNextFunc) {
- return propIterItem{}, nil
- }
- type _enumerate struct{}
- var enumerate _enumerate
- func (_enumerate) exec(vm *vm) {
- v := vm.stack[vm.sp-1]
- if v == _undefined || v == _null {
- vm.iterStack = append(vm.iterStack, iterStackItem{f: emptyIter})
- } else {
- vm.iterStack = append(vm.iterStack, iterStackItem{f: enumerateRecursive(v.ToObject(vm.r))})
- }
- vm.sp--
- vm.pc++
- }
- type enumNext int32
- func (jmp enumNext) exec(vm *vm) {
- l := len(vm.iterStack) - 1
- item, n := vm.iterStack[l].f()
- if n != nil {
- vm.iterStack[l].val = item.name
- vm.iterStack[l].f = n
- vm.pc++
- } else {
- vm.pc += int(jmp)
- }
- }
- type _enumGet struct{}
- var enumGet _enumGet
- func (_enumGet) exec(vm *vm) {
- l := len(vm.iterStack) - 1
- vm.push(vm.iterStack[l].val)
- vm.pc++
- }
- type _enumPop struct{}
- var enumPop _enumPop
- func (_enumPop) exec(vm *vm) {
- l := len(vm.iterStack) - 1
- vm.iterStack[l] = iterStackItem{}
- vm.iterStack = vm.iterStack[:l]
- vm.pc++
- }
- type _enumPopClose struct{}
- var enumPopClose _enumPopClose
- func (_enumPopClose) exec(vm *vm) {
- l := len(vm.iterStack) - 1
- item := vm.iterStack[l]
- vm.iterStack[l] = iterStackItem{}
- vm.iterStack = vm.iterStack[:l]
- if iter := item.iter; iter != nil {
- iter.returnIter()
- }
- vm.pc++
- }
- type _iterateP struct{}
- var iterateP _iterateP
- func (_iterateP) exec(vm *vm) {
- iter := vm.r.getIterator(vm.stack[vm.sp-1], nil)
- vm.iterStack = append(vm.iterStack, iterStackItem{iter: iter})
- vm.sp--
- vm.pc++
- }
- type _iterate struct{}
- var iterate _iterate
- func (_iterate) exec(vm *vm) {
- iter := vm.r.getIterator(vm.stack[vm.sp-1], nil)
- vm.iterStack = append(vm.iterStack, iterStackItem{iter: iter})
- vm.pc++
- }
- type iterNext int32
- func (jmp iterNext) exec(vm *vm) {
- l := len(vm.iterStack) - 1
- iter := vm.iterStack[l].iter
- value, ex := iter.step()
- if ex == nil {
- if value == nil {
- vm.pc += int(jmp)
- } else {
- vm.iterStack[l].val = value
- vm.pc++
- }
- } else {
- l := len(vm.iterStack) - 1
- vm.iterStack[l] = iterStackItem{}
- vm.iterStack = vm.iterStack[:l]
- vm.throw(ex.val)
- return
- }
- }
- type iterGetNextOrUndef struct{}
- func (iterGetNextOrUndef) exec(vm *vm) {
- l := len(vm.iterStack) - 1
- iter := vm.iterStack[l].iter
- var value Value
- if iter.iterator != nil {
- var ex *Exception
- value, ex = iter.step()
- if ex != nil {
- l := len(vm.iterStack) - 1
- vm.iterStack[l] = iterStackItem{}
- vm.iterStack = vm.iterStack[:l]
- vm.throw(ex.val)
- return
- }
- }
- vm.push(nilSafe(value))
- vm.pc++
- }
- type copyStash struct{}
- func (copyStash) exec(vm *vm) {
- oldStash := vm.stash
- newStash := &stash{
- outer: oldStash.outer,
- }
- vm.stashAllocs++
- newStash.values = append([]Value(nil), oldStash.values...)
- newStash.names = oldStash.names
- vm.stash = newStash
- vm.pc++
- }
- type _throwAssignToConst struct{}
- var throwAssignToConst _throwAssignToConst
- func (_throwAssignToConst) exec(vm *vm) {
- vm.throw(errAssignToConst)
- }
- func (r *Runtime) copyDataProperties(target, source Value) {
- targetObj := r.toObject(target)
- if source == _null || source == _undefined {
- return
- }
- sourceObj := source.ToObject(r)
- for item, next := iterateEnumerableProperties(sourceObj)(); next != nil; item, next = next() {
- createDataPropertyOrThrow(targetObj, item.name, item.value)
- }
- }
- type _copySpread struct{}
- var copySpread _copySpread
- func (_copySpread) exec(vm *vm) {
- vm.r.copyDataProperties(vm.stack[vm.sp-2], vm.stack[vm.sp-1])
- vm.sp--
- vm.pc++
- }
- type _copyRest struct{}
- var copyRest _copyRest
- func (_copyRest) exec(vm *vm) {
- vm.push(vm.r.NewObject())
- vm.r.copyDataProperties(vm.stack[vm.sp-1], vm.stack[vm.sp-2])
- vm.pc++
- }
- type _createDestructSrc struct{}
- var createDestructSrc _createDestructSrc
- func (_createDestructSrc) exec(vm *vm) {
- v := vm.stack[vm.sp-1]
- vm.r.checkObjectCoercible(v)
- vm.push(vm.r.newDestructKeyedSource(v))
- vm.pc++
- }
- type _checkObjectCoercible struct{}
- var checkObjectCoercible _checkObjectCoercible
- func (_checkObjectCoercible) exec(vm *vm) {
- vm.r.checkObjectCoercible(vm.stack[vm.sp-1])
- vm.pc++
- }
- type createArgsRestStack int
- func (n createArgsRestStack) exec(vm *vm) {
- var values []Value
- delta := vm.args - int(n)
- if delta > 0 {
- values = make([]Value, delta)
- copy(values, vm.stack[vm.sb+int(n)+1:])
- }
- vm.push(vm.r.newArrayValues(values))
- vm.pc++
- }
- type _createArgsRestStash struct{}
- var createArgsRestStash _createArgsRestStash
- func (_createArgsRestStash) exec(vm *vm) {
- vm.push(vm.r.newArrayValues(vm.stash.extraArgs))
- vm.stash.extraArgs = nil
- vm.pc++
- }
- type concatStrings int
- func (n concatStrings) exec(vm *vm) {
- strs := vm.stack[vm.sp-int(n) : vm.sp]
- length := 0
- allAscii := true
- for i, s := range strs {
- switch s := s.(type) {
- case asciiString:
- length += s.Length()
- case unicodeString:
- length += s.Length()
- allAscii = false
- case *importedString:
- s.ensureScanned()
- if s.u != nil {
- strs[i] = s.u
- length += s.u.Length()
- allAscii = false
- } else {
- strs[i] = asciiString(s.s)
- length += len(s.s)
- }
- default:
- panic(unknownStringTypeErr(s))
- }
- }
- vm.sp -= int(n) - 1
- if allAscii {
- var buf strings.Builder
- buf.Grow(length)
- for _, s := range strs {
- buf.WriteString(string(s.(asciiString)))
- }
- vm.stack[vm.sp-1] = asciiString(buf.String())
- } else {
- var buf unicodeStringBuilder
- buf.ensureStarted(length)
- for _, s := range strs {
- buf.writeString(s.(String))
- }
- vm.stack[vm.sp-1] = buf.String()
- }
- vm.pc++
- }
- type getTaggedTmplObject struct {
- raw, cooked []Value
- }
- // As tagged template objects are not cached (because it's hard to ensure the cache is cleaned without using
- // finalizers) this wrapper is needed to override the equality method so that two objects for the same template
- // literal appeared to be equal from the code's point of view.
- type taggedTemplateArray struct {
- *arrayObject
- idPtr *[]Value
- }
- func (a *taggedTemplateArray) equal(other objectImpl) bool {
- if o, ok := other.(*taggedTemplateArray); ok {
- return a.idPtr == o.idPtr
- }
- return false
- }
- func (c *getTaggedTmplObject) exec(vm *vm) {
- cooked := vm.r.newArrayObject()
- setArrayValues(cooked, c.cooked)
- raw := vm.r.newArrayObject()
- setArrayValues(raw, c.raw)
- cooked.propValueCount = len(c.cooked)
- cooked.lengthProp.writable = false
- raw.propValueCount = len(c.raw)
- raw.lengthProp.writable = false
- raw.preventExtensions(true)
- raw.val.self = &taggedTemplateArray{
- arrayObject: raw,
- idPtr: &c.raw,
- }
- cooked._putProp("raw", raw.val, false, false, false)
- cooked.preventExtensions(true)
- cooked.val.self = &taggedTemplateArray{
- arrayObject: cooked,
- idPtr: &c.cooked,
- }
- vm.push(cooked.val)
- vm.pc++
- }
- type _loadSuper struct{}
- var loadSuper _loadSuper
- func (_loadSuper) exec(vm *vm) {
- homeObject := getHomeObject(vm.stack[vm.sb-1])
- if proto := homeObject.Prototype(); proto != nil {
- vm.push(proto)
- } else {
- vm.push(_undefined)
- }
- vm.pc++
- }
- type newClass struct {
- ctor *Program
- name unistring.String
- source string
- initFields *Program
- privateFields, privateMethods []unistring.String // only set when dynamic resolution is needed
- numPrivateFields, numPrivateMethods uint32
- length int
- hasPrivateEnv bool
- }
- type newDerivedClass struct {
- newClass
- }
- func (vm *vm) createPrivateType(f *classFuncObject, numFields, numMethods uint32) {
- typ := &privateEnvType{}
- typ.numFields = numFields
- typ.numMethods = numMethods
- f.privateEnvType = typ
- f.privateMethods = make([]Value, numMethods)
- }
- func (vm *vm) fillPrivateNamesMap(typ *privateEnvType, privateFields, privateMethods []unistring.String) {
- if len(privateFields) > 0 || len(privateMethods) > 0 {
- penv := vm.privEnv.names
- if penv == nil {
- penv = make(privateNames)
- vm.privEnv.names = penv
- }
- for idx, field := range privateFields {
- penv[field] = &privateId{
- typ: typ,
- idx: uint32(idx),
- }
- }
- for idx, method := range privateMethods {
- penv[method] = &privateId{
- typ: typ,
- idx: uint32(idx),
- isMethod: true,
- }
- }
- }
- }
- func (c *newClass) create(protoParent, ctorParent *Object, vm *vm, derived bool) (prototype, cls *Object) {
- proto := vm.r.newBaseObject(protoParent, classObject)
- f := vm.r.newClassFunc(c.name, c.length, ctorParent, derived)
- f._putProp("prototype", proto.val, false, false, false)
- proto._putProp("constructor", f.val, true, false, true)
- f.prg = c.ctor
- f.stash = vm.stash
- f.src = c.source
- f.initFields = c.initFields
- if c.hasPrivateEnv {
- vm.privEnv = &privateEnv{
- outer: vm.privEnv,
- }
- vm.createPrivateType(f, c.numPrivateFields, c.numPrivateMethods)
- vm.fillPrivateNamesMap(f.privateEnvType, c.privateFields, c.privateMethods)
- vm.privEnv.instanceType = f.privateEnvType
- }
- f.privEnv = vm.privEnv
- return proto.val, f.val
- }
- func (c *newClass) exec(vm *vm) {
- proto, cls := c.create(vm.r.global.ObjectPrototype, vm.r.getFunctionPrototype(), vm, false)
- sp := vm.sp
- vm.stack.expand(sp + 1)
- vm.stack[sp] = proto
- vm.stack[sp+1] = cls
- vm.sp = sp + 2
- vm.pc++
- }
- func (c *newDerivedClass) exec(vm *vm) {
- var protoParent *Object
- var superClass *Object
- if o := vm.stack[vm.sp-1]; o != _null {
- if sc, ok := o.(*Object); !ok || sc.self.assertConstructor() == nil {
- vm.throw(vm.r.NewTypeError("Class extends value is not a constructor or null"))
- return
- } else {
- v := sc.self.getStr("prototype", nil)
- if v != _null {
- if o, ok := v.(*Object); ok {
- protoParent = o
- } else {
- vm.throw(vm.r.NewTypeError("Class extends value does not have valid prototype property"))
- return
- }
- }
- superClass = sc
- }
- } else {
- superClass = vm.r.getFunctionPrototype()
- }
- proto, cls := c.create(protoParent, superClass, vm, true)
- vm.stack[vm.sp-1] = proto
- vm.push(cls)
- vm.pc++
- }
- // Creates a special instance of *classFuncObject which is only used during evaluation of a class declaration
- // to initialise static fields and instance private methods of another class.
- type newStaticFieldInit struct {
- initFields *Program
- numPrivateFields, numPrivateMethods uint32
- }
- func (c *newStaticFieldInit) exec(vm *vm) {
- f := vm.r.newClassFunc("", 0, vm.r.getFunctionPrototype(), false)
- if c.numPrivateFields > 0 || c.numPrivateMethods > 0 {
- vm.createPrivateType(f, c.numPrivateFields, c.numPrivateMethods)
- }
- f.initFields = c.initFields
- f.stash = vm.stash
- vm.push(f.val)
- vm.pc++
- }
- func (vm *vm) loadThis(v Value) {
- if v != nil {
- vm.push(v)
- } else {
- vm.throw(vm.r.newError(vm.r.getReferenceError(), "Must call super constructor in derived class before accessing 'this'"))
- return
- }
- vm.pc++
- }
- type loadThisStash uint32
- func (l loadThisStash) exec(vm *vm) {
- vm.loadThis(*vm.getStashPtr(uint32(l)))
- }
- type loadThisStack struct{}
- func (loadThisStack) exec(vm *vm) {
- vm.loadThis(vm.stack[vm.sb])
- }
- func (vm *vm) getStashPtr(s uint32) *Value {
- level := int(s) >> 24
- idx := s & 0x00FFFFFF
- stash := vm.stash
- for i := 0; i < level; i++ {
- stash = stash.outer
- }
- return &stash.values[idx]
- }
- type getThisDynamic struct{}
- func (getThisDynamic) exec(vm *vm) {
- for stash := vm.stash; stash != nil; stash = stash.outer {
- if stash.obj == nil {
- if v, exists := stash.getByName(thisBindingName); exists {
- vm.push(v)
- vm.pc++
- return
- }
- }
- }
- vm.push(vm.r.globalObject)
- vm.pc++
- }
- type throwConst struct {
- v interface{}
- }
- func (t throwConst) exec(vm *vm) {
- vm.throw(t.v)
- }
- type resolveThisStack struct{}
- func (r resolveThisStack) exec(vm *vm) {
- vm.refStack = append(vm.refStack, &thisRef{v: (*[]Value)(&vm.stack), idx: vm.sb})
- vm.pc++
- }
- type resolveThisStash uint32
- func (r resolveThisStash) exec(vm *vm) {
- level := int(r) >> 24
- idx := r & 0x00FFFFFF
- stash := vm.stash
- for i := 0; i < level; i++ {
- stash = stash.outer
- }
- vm.refStack = append(vm.refStack, &thisRef{v: &stash.values, idx: int(idx)})
- vm.pc++
- }
- type resolveThisDynamic struct{}
- func (resolveThisDynamic) exec(vm *vm) {
- for stash := vm.stash; stash != nil; stash = stash.outer {
- if stash.obj == nil {
- if idx, exists := stash.names[thisBindingName]; exists {
- vm.refStack = append(vm.refStack, &thisRef{v: &stash.values, idx: int(idx &^ maskTyp)})
- vm.pc++
- return
- }
- }
- }
- panic(vm.r.newError(vm.r.getReferenceError(), "Compiler bug: 'this' reference is not found in resolveThisDynamic"))
- }
- type defineComputedKey int
- func (offset defineComputedKey) exec(vm *vm) {
- obj := vm.r.toObject(vm.stack[vm.sp-int(offset)])
- if h, ok := obj.self.(*classFuncObject); ok {
- key := toPropertyKey(vm.stack[vm.sp-1])
- h.computedKeys = append(h.computedKeys, key)
- vm.sp--
- vm.pc++
- return
- }
- panic(vm.r.NewTypeError("Compiler bug: unexpected target for defineComputedKey: %v", obj))
- }
- type loadComputedKey int
- func (idx loadComputedKey) exec(vm *vm) {
- obj := vm.r.toObject(vm.stack[vm.sb-1])
- if h, ok := obj.self.(*classFuncObject); ok {
- vm.push(h.computedKeys[idx])
- vm.pc++
- return
- }
- panic(vm.r.NewTypeError("Compiler bug: unexpected target for loadComputedKey: %v", obj))
- }
- type initStaticElements struct {
- privateFields, privateMethods []unistring.String
- }
- func (i *initStaticElements) exec(vm *vm) {
- cls := vm.stack[vm.sp-1]
- staticInit := vm.r.toObject(vm.stack[vm.sp-3])
- vm.sp -= 2
- if h, ok := staticInit.self.(*classFuncObject); ok {
- h._putProp("prototype", cls, true, true, true) // so that 'super' resolution work
- h.privEnv = vm.privEnv
- if h.privateEnvType != nil {
- vm.privEnv.staticType = h.privateEnvType
- vm.fillPrivateNamesMap(h.privateEnvType, i.privateFields, i.privateMethods)
- }
- h._initFields(vm.r.toObject(cls))
- vm.stack[vm.sp-1] = cls
- vm.pc++
- return
- }
- panic(vm.r.NewTypeError("Compiler bug: unexpected target for initStaticElements: %v", staticInit))
- }
- type definePrivateMethod struct {
- idx int
- targetOffset int
- }
- func (d *definePrivateMethod) getPrivateMethods(vm *vm) []Value {
- obj := vm.r.toObject(vm.stack[vm.sp-d.targetOffset])
- if cls, ok := obj.self.(*classFuncObject); ok {
- return cls.privateMethods
- } else {
- panic(vm.r.NewTypeError("Compiler bug: wrong target type for definePrivateMethod: %T", obj.self))
- }
- }
- func (d *definePrivateMethod) exec(vm *vm) {
- methods := d.getPrivateMethods(vm)
- methods[d.idx] = vm.stack[vm.sp-1]
- vm.sp--
- vm.pc++
- }
- type definePrivateGetter struct {
- definePrivateMethod
- }
- func (d *definePrivateGetter) exec(vm *vm) {
- methods := d.getPrivateMethods(vm)
- val := vm.stack[vm.sp-1]
- method := vm.r.toObject(val)
- p, _ := methods[d.idx].(*valueProperty)
- if p == nil {
- p = &valueProperty{
- accessor: true,
- }
- methods[d.idx] = p
- }
- if p.getterFunc != nil {
- vm.throw(vm.r.NewTypeError("Private getter has already been declared"))
- return
- }
- p.getterFunc = method
- vm.sp--
- vm.pc++
- }
- type definePrivateSetter struct {
- definePrivateMethod
- }
- func (d *definePrivateSetter) exec(vm *vm) {
- methods := d.getPrivateMethods(vm)
- val := vm.stack[vm.sp-1]
- method := vm.r.toObject(val)
- p, _ := methods[d.idx].(*valueProperty)
- if p == nil {
- p = &valueProperty{
- accessor: true,
- }
- methods[d.idx] = p
- }
- if p.setterFunc != nil {
- vm.throw(vm.r.NewTypeError("Private setter has already been declared"))
- return
- }
- p.setterFunc = method
- vm.sp--
- vm.pc++
- }
- type definePrivateProp struct {
- idx int
- }
- func (d *definePrivateProp) exec(vm *vm) {
- f := vm.r.toObject(vm.stack[vm.sb-1]).self.(*classFuncObject)
- obj := vm.r.toObject(vm.stack[vm.sp-2])
- penv := obj.self.getPrivateEnv(f.privateEnvType, false)
- penv.fields[d.idx] = vm.stack[vm.sp-1]
- vm.sp--
- vm.pc++
- }
- type getPrivatePropRes resolvedPrivateName
- func (vm *vm) getPrivateType(level uint8, isStatic bool) *privateEnvType {
- e := vm.privEnv
- for i := uint8(0); i < level; i++ {
- e = e.outer
- }
- if isStatic {
- return e.staticType
- }
- return e.instanceType
- }
- func (g *getPrivatePropRes) _get(base Value, vm *vm) Value {
- return vm.getPrivateProp(base, g.name, vm.getPrivateType(g.level, g.isStatic), g.idx, g.isMethod)
- }
- func (g *getPrivatePropRes) exec(vm *vm) {
- vm.stack[vm.sp-1] = g._get(vm.stack[vm.sp-1], vm)
- vm.pc++
- }
- type getPrivatePropId privateId
- func (g *getPrivatePropId) exec(vm *vm) {
- vm.stack[vm.sp-1] = vm.getPrivateProp(vm.stack[vm.sp-1], g.name, g.typ, g.idx, g.isMethod)
- vm.pc++
- }
- type getPrivatePropIdCallee privateId
- func (g *getPrivatePropIdCallee) exec(vm *vm) {
- prop := vm.getPrivateProp(vm.stack[vm.sp-1], g.name, g.typ, g.idx, g.isMethod)
- if prop == nil {
- prop = memberUnresolved{valueUnresolved{r: vm.r, ref: (*privateId)(g).string()}}
- }
- vm.push(prop)
- vm.pc++
- }
- func (vm *vm) getPrivateProp(base Value, name unistring.String, typ *privateEnvType, idx uint32, isMethod bool) Value {
- obj := vm.r.toObject(base)
- penv := obj.self.getPrivateEnv(typ, false)
- var v Value
- if penv != nil {
- if isMethod {
- v = penv.methods[idx]
- } else {
- v = penv.fields[idx]
- if v == nil {
- panic(vm.r.NewTypeError("Private member #%s is accessed before it is initialized", name))
- }
- }
- } else {
- panic(vm.r.NewTypeError("Cannot read private member #%s from an object whose class did not declare it", name))
- }
- if prop, ok := v.(*valueProperty); ok {
- if prop.getterFunc == nil {
- panic(vm.r.NewTypeError("'#%s' was defined without a getter", name))
- }
- v = prop.get(obj)
- }
- return v
- }
- type getPrivatePropResCallee getPrivatePropRes
- func (g *getPrivatePropResCallee) exec(vm *vm) {
- prop := (*getPrivatePropRes)(g)._get(vm.stack[vm.sp-1], vm)
- if prop == nil {
- prop = memberUnresolved{valueUnresolved{r: vm.r, ref: (*resolvedPrivateName)(g).string()}}
- }
- vm.push(prop)
- vm.pc++
- }
- func (vm *vm) setPrivateProp(base Value, name unistring.String, typ *privateEnvType, idx uint32, isMethod bool, val Value) {
- obj := vm.r.toObject(base)
- penv := obj.self.getPrivateEnv(typ, false)
- if penv != nil {
- if isMethod {
- v := penv.methods[idx]
- if prop, ok := v.(*valueProperty); ok {
- if prop.setterFunc != nil {
- prop.set(base, val)
- } else {
- panic(vm.r.NewTypeError("Cannot assign to read only property '#%s'", name))
- }
- } else {
- panic(vm.r.NewTypeError("Private method '#%s' is not writable", name))
- }
- } else {
- ptr := &penv.fields[idx]
- if *ptr == nil {
- panic(vm.r.NewTypeError("Private member #%s is accessed before it is initialized", name))
- }
- *ptr = val
- }
- } else {
- panic(vm.r.NewTypeError("Cannot write private member #%s from an object whose class did not declare it", name))
- }
- }
- func (vm *vm) exceptionFromValue(x interface{}) *Exception {
- var ex *Exception
- switch x1 := x.(type) {
- case *Object:
- ex = &Exception{
- val: x1,
- }
- if er, ok := x1.self.(*errorObject); ok {
- ex.stack = er.stack
- }
- case Value:
- ex = &Exception{
- val: x1,
- }
- case *Exception:
- ex = x1
- case typeError:
- ex = &Exception{
- val: vm.r.NewTypeError(string(x1)),
- }
- case referenceError:
- ex = &Exception{
- val: vm.r.newError(vm.r.getReferenceError(), string(x1)),
- }
- case rangeError:
- ex = &Exception{
- val: vm.r.newError(vm.r.getRangeError(), string(x1)),
- }
- case syntaxError:
- ex = &Exception{
- val: vm.r.newError(vm.r.getSyntaxError(), string(x1)),
- }
- default:
- /*
- if vm.prg != nil {
- vm.prg.dumpCode(log.Printf)
- }
- log.Print("Stack: ", string(debug.Stack()))
- panic(fmt.Errorf("Panic at %d: %v", vm.pc, x))
- */
- return nil
- }
- if ex.stack == nil {
- ex.stack = vm.captureStack(make([]StackFrame, 0, len(vm.callStack)+1), 0)
- }
- return ex
- }
- type setPrivatePropRes resolvedPrivateName
- func (p *setPrivatePropRes) _set(base Value, val Value, vm *vm) {
- vm.setPrivateProp(base, p.name, vm.getPrivateType(p.level, p.isStatic), p.idx, p.isMethod, val)
- }
- func (p *setPrivatePropRes) exec(vm *vm) {
- v := vm.stack[vm.sp-1]
- p._set(vm.stack[vm.sp-2], v, vm)
- vm.stack[vm.sp-2] = v
- vm.sp--
- vm.pc++
- }
- type setPrivatePropResP setPrivatePropRes
- func (p *setPrivatePropResP) exec(vm *vm) {
- v := vm.stack[vm.sp-1]
- (*setPrivatePropRes)(p)._set(vm.stack[vm.sp-2], v, vm)
- vm.sp -= 2
- vm.pc++
- }
- type setPrivatePropId privateId
- func (p *setPrivatePropId) exec(vm *vm) {
- v := vm.stack[vm.sp-1]
- vm.setPrivateProp(vm.stack[vm.sp-2], p.name, p.typ, p.idx, p.isMethod, v)
- vm.stack[vm.sp-2] = v
- vm.sp--
- vm.pc++
- }
- type setPrivatePropIdP privateId
- func (p *setPrivatePropIdP) exec(vm *vm) {
- v := vm.stack[vm.sp-1]
- vm.setPrivateProp(vm.stack[vm.sp-2], p.name, p.typ, p.idx, p.isMethod, v)
- vm.sp -= 2
- vm.pc++
- }
- type popPrivateEnv struct{}
- func (popPrivateEnv) exec(vm *vm) {
- vm.privEnv = vm.privEnv.outer
- vm.pc++
- }
- type privateInRes resolvedPrivateName
- func (i *privateInRes) exec(vm *vm) {
- obj := vm.r.toObject(vm.stack[vm.sp-1])
- pe := obj.self.getPrivateEnv(vm.getPrivateType(i.level, i.isStatic), false)
- if pe != nil && (i.isMethod && pe.methods[i.idx] != nil || !i.isMethod && pe.fields[i.idx] != nil) {
- vm.stack[vm.sp-1] = valueTrue
- } else {
- vm.stack[vm.sp-1] = valueFalse
- }
- vm.pc++
- }
- type privateInId privateId
- func (i *privateInId) exec(vm *vm) {
- obj := vm.r.toObject(vm.stack[vm.sp-1])
- pe := obj.self.getPrivateEnv(i.typ, false)
- if pe != nil && (i.isMethod && pe.methods[i.idx] != nil || !i.isMethod && pe.fields[i.idx] != nil) {
- vm.stack[vm.sp-1] = valueTrue
- } else {
- vm.stack[vm.sp-1] = valueFalse
- }
- vm.pc++
- }
- type getPrivateRefRes resolvedPrivateName
- func (r *getPrivateRefRes) exec(vm *vm) {
- vm.refStack = append(vm.refStack, &privateRefRes{
- base: vm.stack[vm.sp-1].ToObject(vm.r),
- name: (*resolvedPrivateName)(r),
- })
- vm.sp--
- vm.pc++
- }
- type getPrivateRefId privateId
- func (r *getPrivateRefId) exec(vm *vm) {
- vm.refStack = append(vm.refStack, &privateRefId{
- base: vm.stack[vm.sp-1].ToObject(vm.r),
- id: (*privateId)(r),
- })
- vm.sp--
- vm.pc++
- }
- func (y *yieldMarker) exec(vm *vm) {
- vm.pc = -vm.pc // this will terminate the run loop
- vm.push(y) // marker so the caller knows it's a yield, not a return
- }
- func (y *yieldMarker) String() string {
- if y == yieldEmpty {
- return "empty"
- }
- switch y.resultType {
- case resultYield:
- return "yield"
- case resultYieldRes:
- return "yieldRes"
- case resultYieldDelegate:
- return "yield*"
- case resultYieldDelegateRes:
- return "yield*Res"
- case resultAwait:
- return "await"
- default:
- return "unknown"
- }
- }
|