vm.go 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184
  1. package goja
  2. import (
  3. "fmt"
  4. "math"
  5. "math/big"
  6. "runtime"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "sync/atomic"
  11. "github.com/dop251/goja/unistring"
  12. )
  13. const (
  14. maxInt = 1 << 53
  15. )
  16. type valueStack []Value
  17. type stash struct {
  18. values []Value
  19. extraArgs []Value
  20. names map[unistring.String]uint32
  21. obj *Object
  22. outer *stash
  23. // true if this stash is a VariableEnvironment, i.e. dynamic var declarations created
  24. // by direct eval go here.
  25. variable bool
  26. }
  27. type context struct {
  28. prg *Program
  29. funcName unistring.String
  30. stash *stash
  31. newTarget Value
  32. result Value
  33. pc, sb int
  34. args int
  35. }
  36. type iterStackItem struct {
  37. val Value
  38. f iterNextFunc
  39. iter *iteratorRecord
  40. }
  41. type ref interface {
  42. get() Value
  43. set(Value)
  44. init(Value)
  45. refname() unistring.String
  46. }
  47. type stashRef struct {
  48. n unistring.String
  49. v *[]Value
  50. idx int
  51. }
  52. func (r *stashRef) get() Value {
  53. return nilSafe((*r.v)[r.idx])
  54. }
  55. func (r *stashRef) set(v Value) {
  56. (*r.v)[r.idx] = v
  57. }
  58. func (r *stashRef) init(v Value) {
  59. r.set(v)
  60. }
  61. func (r *stashRef) refname() unistring.String {
  62. return r.n
  63. }
  64. type stashRefLex struct {
  65. stashRef
  66. }
  67. func (r *stashRefLex) get() Value {
  68. v := (*r.v)[r.idx]
  69. if v == nil {
  70. panic(errAccessBeforeInit)
  71. }
  72. return v
  73. }
  74. func (r *stashRefLex) set(v Value) {
  75. p := &(*r.v)[r.idx]
  76. if *p == nil {
  77. panic(errAccessBeforeInit)
  78. }
  79. *p = v
  80. }
  81. func (r *stashRefLex) init(v Value) {
  82. r.set(v)
  83. }
  84. type stashRefConst struct {
  85. stashRefLex
  86. strictConst bool
  87. }
  88. func (r *stashRefConst) set(v Value) {
  89. if r.strictConst {
  90. panic(errAssignToConst)
  91. }
  92. }
  93. func (r *stashRefConst) init(v Value) {
  94. r.set(v)
  95. }
  96. type objRef struct {
  97. base objectImpl
  98. name unistring.String
  99. strict bool
  100. }
  101. func (r *objRef) get() Value {
  102. return r.base.getStr(r.name, nil)
  103. }
  104. func (r *objRef) set(v Value) {
  105. if r.strict && !r.base.hasOwnPropertyStr(r.name) {
  106. panic(referenceError(fmt.Sprintf("%s is not defined", r.name)))
  107. }
  108. r.base.setOwnStr(r.name, v, r.strict)
  109. }
  110. func (r *objRef) init(v Value) {
  111. r.base.setOwnStr(r.name, v, r.strict)
  112. }
  113. func (r *objRef) refname() unistring.String {
  114. return r.name
  115. }
  116. type unresolvedRef struct {
  117. runtime *Runtime
  118. name unistring.String
  119. }
  120. func (r *unresolvedRef) get() Value {
  121. r.runtime.throwReferenceError(r.name)
  122. panic("Unreachable")
  123. }
  124. func (r *unresolvedRef) set(Value) {
  125. r.get()
  126. }
  127. func (r *unresolvedRef) init(Value) {
  128. r.get()
  129. }
  130. func (r *unresolvedRef) refname() unistring.String {
  131. return r.name
  132. }
  133. type vm struct {
  134. r *Runtime
  135. prg *Program
  136. funcName unistring.String
  137. pc int
  138. stack valueStack
  139. sp, sb, args int
  140. stash *stash
  141. callStack []context
  142. iterStack []iterStackItem
  143. refStack []ref
  144. newTarget Value
  145. result Value
  146. maxCallStackSize int
  147. stashAllocs int
  148. halt bool
  149. interrupted uint32
  150. interruptVal interface{}
  151. interruptLock sync.Mutex
  152. }
  153. type instruction interface {
  154. exec(*vm)
  155. }
  156. func intToValue(i int64) Value {
  157. if i >= -maxInt && i <= maxInt {
  158. if i >= -128 && i <= 127 {
  159. return intCache[i+128]
  160. }
  161. return valueInt(i)
  162. }
  163. return valueFloat(i)
  164. }
  165. func floatToInt(f float64) (result int64, ok bool) {
  166. if (f != 0 || !math.Signbit(f)) && !math.IsInf(f, 0) && f == math.Trunc(f) && f >= -maxInt && f <= maxInt {
  167. return int64(f), true
  168. }
  169. return 0, false
  170. }
  171. func floatToValue(f float64) (result Value) {
  172. if i, ok := floatToInt(f); ok {
  173. return intToValue(i)
  174. }
  175. switch {
  176. case f == 0:
  177. return _negativeZero
  178. case math.IsNaN(f):
  179. return _NaN
  180. case math.IsInf(f, 1):
  181. return _positiveInf
  182. case math.IsInf(f, -1):
  183. return _negativeInf
  184. }
  185. return valueFloat(f)
  186. }
  187. func assertInt64(v Value) (int64, bool) {
  188. num := v.ToNumber()
  189. if i, ok := num.(valueInt); ok {
  190. return int64(i), true
  191. }
  192. if f, ok := num.(valueFloat); ok {
  193. if i, ok := floatToInt(float64(f)); ok {
  194. return i, true
  195. }
  196. }
  197. return 0, false
  198. }
  199. func assertBigInt(v Value) (*big.Int, bool) {
  200. num := v.ToNumber()
  201. if b, ok := num.(valueBigInt); ok {
  202. return b.Int, true
  203. }
  204. return big.NewInt(0), false
  205. }
  206. func (s *valueStack) expand(idx int) {
  207. if idx < len(*s) {
  208. return
  209. }
  210. idx++
  211. if idx < cap(*s) {
  212. *s = (*s)[:idx]
  213. } else {
  214. var newCap int
  215. if idx < 1024 {
  216. newCap = idx * 2
  217. } else {
  218. newCap = (idx + 1025) &^ 1023
  219. }
  220. n := make([]Value, idx, newCap)
  221. copy(n, *s)
  222. *s = n
  223. }
  224. }
  225. func stashObjHas(obj *Object, name unistring.String) bool {
  226. if obj.self.hasPropertyStr(name) {
  227. if unscopables, ok := obj.self.getSym(SymUnscopables, nil).(*Object); ok {
  228. if b := unscopables.self.getStr(name, nil); b != nil {
  229. return !b.ToBoolean()
  230. }
  231. }
  232. return true
  233. }
  234. return false
  235. }
  236. func (s *stash) initByIdx(idx uint32, v Value) {
  237. if s.obj != nil {
  238. panic("Attempt to init by idx into an object scope")
  239. }
  240. s.values[idx] = v
  241. }
  242. func (s *stash) initByName(name unistring.String, v Value) {
  243. if idx, exists := s.names[name]; exists {
  244. s.values[idx&^maskTyp] = v
  245. } else {
  246. panic(referenceError(fmt.Sprintf("%s is not defined", name)))
  247. }
  248. }
  249. func (s *stash) getByIdx(idx uint32) Value {
  250. return s.values[idx]
  251. }
  252. func (s *stash) getByName(name unistring.String) (v Value, exists bool) {
  253. if s.obj != nil {
  254. if stashObjHas(s.obj, name) {
  255. return nilSafe(s.obj.self.getStr(name, nil)), true
  256. }
  257. return nil, false
  258. }
  259. if idx, exists := s.names[name]; exists {
  260. v := s.values[idx&^maskTyp]
  261. if v == nil {
  262. if idx&maskVar == 0 {
  263. panic(errAccessBeforeInit)
  264. } else {
  265. v = _undefined
  266. }
  267. }
  268. return v, true
  269. }
  270. return nil, false
  271. }
  272. func (s *stash) getRefByName(name unistring.String, strict bool) ref {
  273. if obj := s.obj; obj != nil {
  274. if stashObjHas(obj, name) {
  275. return &objRef{
  276. base: obj.self,
  277. name: name,
  278. strict: strict,
  279. }
  280. }
  281. } else {
  282. if idx, exists := s.names[name]; exists {
  283. if idx&maskVar == 0 {
  284. if idx&maskConst == 0 {
  285. return &stashRefLex{
  286. stashRef: stashRef{
  287. n: name,
  288. v: &s.values,
  289. idx: int(idx &^ maskTyp),
  290. },
  291. }
  292. } else {
  293. return &stashRefConst{
  294. stashRefLex: stashRefLex{
  295. stashRef: stashRef{
  296. n: name,
  297. v: &s.values,
  298. idx: int(idx &^ maskTyp),
  299. },
  300. },
  301. strictConst: strict || (idx&maskStrict != 0),
  302. }
  303. }
  304. } else {
  305. return &stashRef{
  306. n: name,
  307. v: &s.values,
  308. idx: int(idx &^ maskTyp),
  309. }
  310. }
  311. }
  312. }
  313. return nil
  314. }
  315. func (s *stash) createBinding(name unistring.String, deletable bool) {
  316. if s.names == nil {
  317. s.names = make(map[unistring.String]uint32)
  318. }
  319. if _, exists := s.names[name]; !exists {
  320. idx := uint32(len(s.names)) | maskVar
  321. if deletable {
  322. idx |= maskDeletable
  323. }
  324. s.names[name] = idx
  325. s.values = append(s.values, _undefined)
  326. }
  327. }
  328. func (s *stash) createLexBinding(name unistring.String, isConst bool) {
  329. if s.names == nil {
  330. s.names = make(map[unistring.String]uint32)
  331. }
  332. if _, exists := s.names[name]; !exists {
  333. idx := uint32(len(s.names))
  334. if isConst {
  335. idx |= maskConst | maskStrict
  336. }
  337. s.names[name] = idx
  338. s.values = append(s.values, nil)
  339. }
  340. }
  341. func (s *stash) deleteBinding(name unistring.String) {
  342. delete(s.names, name)
  343. }
  344. func (vm *vm) newStash() {
  345. vm.stash = &stash{
  346. outer: vm.stash,
  347. }
  348. vm.stashAllocs++
  349. }
  350. func (vm *vm) init() {
  351. vm.sb = -1
  352. vm.stash = &vm.r.global.stash
  353. vm.maxCallStackSize = math.MaxInt32
  354. }
  355. func (vm *vm) run() {
  356. vm.halt = false
  357. interrupted := false
  358. ticks := 0
  359. for !vm.halt {
  360. if interrupted = atomic.LoadUint32(&vm.interrupted) != 0; interrupted {
  361. break
  362. }
  363. vm.prg.code[vm.pc].exec(vm)
  364. ticks++
  365. if ticks > 10000 {
  366. runtime.Gosched()
  367. ticks = 0
  368. }
  369. }
  370. if interrupted {
  371. vm.interruptLock.Lock()
  372. v := &InterruptedError{
  373. iface: vm.interruptVal,
  374. }
  375. atomic.StoreUint32(&vm.interrupted, 0)
  376. vm.interruptVal = nil
  377. vm.interruptLock.Unlock()
  378. panic(&uncatchableException{
  379. stack: &v.stack,
  380. err: v,
  381. })
  382. }
  383. }
  384. func (vm *vm) Interrupt(v interface{}) {
  385. vm.interruptLock.Lock()
  386. vm.interruptVal = v
  387. atomic.StoreUint32(&vm.interrupted, 1)
  388. vm.interruptLock.Unlock()
  389. }
  390. func (vm *vm) ClearInterrupt() {
  391. atomic.StoreUint32(&vm.interrupted, 0)
  392. }
  393. func (vm *vm) captureStack(stack []StackFrame, ctxOffset int) []StackFrame {
  394. // Unroll the context stack
  395. if vm.pc != -1 {
  396. stack = append(stack, StackFrame{prg: vm.prg, pc: vm.pc, funcName: vm.funcName})
  397. }
  398. for i := len(vm.callStack) - 1; i > ctxOffset-1; i-- {
  399. if vm.callStack[i].pc != -1 {
  400. stack = append(stack, StackFrame{prg: vm.callStack[i].prg, pc: vm.callStack[i].pc - 1, funcName: vm.callStack[i].funcName})
  401. }
  402. }
  403. return stack
  404. }
  405. func (vm *vm) try(f func()) (ex *Exception) {
  406. var ctx context
  407. vm.saveCtx(&ctx)
  408. ctxOffset := len(vm.callStack)
  409. sp := vm.sp
  410. iterLen := len(vm.iterStack)
  411. refLen := len(vm.refStack)
  412. defer func() {
  413. if x := recover(); x != nil {
  414. defer func() {
  415. vm.callStack = vm.callStack[:ctxOffset]
  416. vm.restoreCtx(&ctx)
  417. vm.sp = sp
  418. // Restore other stacks
  419. iterTail := vm.iterStack[iterLen:]
  420. for i := range iterTail {
  421. if iter := iterTail[i].iter; iter != nil {
  422. _ = vm.try(func() {
  423. iter.returnIter()
  424. })
  425. }
  426. iterTail[i] = iterStackItem{}
  427. }
  428. vm.iterStack = vm.iterStack[:iterLen]
  429. refTail := vm.refStack[refLen:]
  430. for i := range refTail {
  431. refTail[i] = nil
  432. }
  433. vm.refStack = vm.refStack[:refLen]
  434. }()
  435. switch x1 := x.(type) {
  436. case Value:
  437. ex = &Exception{
  438. val: x1,
  439. }
  440. case *Exception:
  441. ex = x1
  442. case *uncatchableException:
  443. *x1.stack = vm.captureStack(*x1.stack, ctxOffset)
  444. panic(x1)
  445. case typeError:
  446. ex = &Exception{
  447. val: vm.r.NewTypeError(string(x1)),
  448. }
  449. case referenceError:
  450. ex = &Exception{
  451. val: vm.r.newError(vm.r.global.ReferenceError, string(x1)),
  452. }
  453. case rangeError:
  454. ex = &Exception{
  455. val: vm.r.newError(vm.r.global.RangeError, string(x1)),
  456. }
  457. default:
  458. /*
  459. if vm.prg != nil {
  460. vm.prg.dumpCode(log.Printf)
  461. }
  462. log.Print("Stack: ", string(debug.Stack()))
  463. panic(fmt.Errorf("Panic at %d: %v", vm.pc, x))
  464. */
  465. panic(x)
  466. }
  467. ex.stack = vm.captureStack(ex.stack, ctxOffset)
  468. }
  469. }()
  470. f()
  471. return
  472. }
  473. func (vm *vm) runTry() (ex *Exception) {
  474. return vm.try(vm.run)
  475. }
  476. func (vm *vm) push(v Value) {
  477. vm.stack.expand(vm.sp)
  478. vm.stack[vm.sp] = v
  479. vm.sp++
  480. }
  481. func (vm *vm) pop() Value {
  482. vm.sp--
  483. return vm.stack[vm.sp]
  484. }
  485. func (vm *vm) peek() Value {
  486. return vm.stack[vm.sp-1]
  487. }
  488. func (vm *vm) saveCtx(ctx *context) {
  489. ctx.prg, ctx.stash, ctx.newTarget, ctx.result, ctx.pc, ctx.sb, ctx.args =
  490. vm.prg, vm.stash, vm.newTarget, vm.result, vm.pc, vm.sb, vm.args
  491. if vm.funcName != "" {
  492. ctx.funcName = vm.funcName
  493. } else if ctx.prg != nil && ctx.prg.funcName != "" {
  494. ctx.funcName = ctx.prg.funcName
  495. }
  496. }
  497. func (vm *vm) pushCtx() {
  498. if len(vm.callStack) > vm.maxCallStackSize {
  499. ex := &StackOverflowError{}
  500. panic(&uncatchableException{
  501. stack: &ex.stack,
  502. err: ex,
  503. })
  504. }
  505. vm.callStack = append(vm.callStack, context{})
  506. ctx := &vm.callStack[len(vm.callStack)-1]
  507. vm.saveCtx(ctx)
  508. }
  509. func (vm *vm) restoreCtx(ctx *context) {
  510. vm.prg, vm.funcName, vm.stash, vm.newTarget, vm.result, vm.pc, vm.sb, vm.args =
  511. ctx.prg, ctx.funcName, ctx.stash, ctx.newTarget, ctx.result, ctx.pc, ctx.sb, ctx.args
  512. }
  513. func (vm *vm) popCtx() {
  514. l := len(vm.callStack) - 1
  515. ctx := &vm.callStack[l]
  516. vm.restoreCtx(ctx)
  517. ctx.prg = nil
  518. ctx.stash = nil
  519. ctx.result = nil
  520. ctx.newTarget = nil
  521. vm.callStack = vm.callStack[:l]
  522. }
  523. func (vm *vm) toCallee(v Value) *Object {
  524. if obj, ok := v.(*Object); ok {
  525. return obj
  526. }
  527. switch unresolved := v.(type) {
  528. case valueUnresolved:
  529. unresolved.throw()
  530. panic("Unreachable")
  531. case memberUnresolved:
  532. panic(vm.r.NewTypeError("Object has no member '%s'", unresolved.ref))
  533. }
  534. panic(vm.r.NewTypeError("Value is not an object: %s", v.toString()))
  535. }
  536. type loadVal uint32
  537. func (l loadVal) exec(vm *vm) {
  538. vm.push(vm.prg.values[l])
  539. vm.pc++
  540. }
  541. type _loadUndef struct{}
  542. var loadUndef _loadUndef
  543. func (_loadUndef) exec(vm *vm) {
  544. vm.push(_undefined)
  545. vm.pc++
  546. }
  547. type _loadNil struct{}
  548. var loadNil _loadNil
  549. func (_loadNil) exec(vm *vm) {
  550. vm.push(nil)
  551. vm.pc++
  552. }
  553. type _saveResult struct{}
  554. var saveResult _saveResult
  555. func (_saveResult) exec(vm *vm) {
  556. vm.sp--
  557. vm.result = vm.stack[vm.sp]
  558. vm.pc++
  559. }
  560. type _clearResult struct{}
  561. var clearResult _clearResult
  562. func (_clearResult) exec(vm *vm) {
  563. vm.result = _undefined
  564. vm.pc++
  565. }
  566. type _loadGlobalObject struct{}
  567. var loadGlobalObject _loadGlobalObject
  568. func (_loadGlobalObject) exec(vm *vm) {
  569. vm.push(vm.r.globalObject)
  570. vm.pc++
  571. }
  572. type loadStack int
  573. func (l loadStack) exec(vm *vm) {
  574. // l > 0 -- var<l-1>
  575. // l == 0 -- this
  576. if l > 0 {
  577. vm.push(nilSafe(vm.stack[vm.sb+vm.args+int(l)]))
  578. } else {
  579. vm.push(vm.stack[vm.sb])
  580. }
  581. vm.pc++
  582. }
  583. type loadStack1 int
  584. func (l loadStack1) exec(vm *vm) {
  585. // args are in stash
  586. // l > 0 -- var<l-1>
  587. // l == 0 -- this
  588. if l > 0 {
  589. vm.push(nilSafe(vm.stack[vm.sb+int(l)]))
  590. } else {
  591. vm.push(vm.stack[vm.sb])
  592. }
  593. vm.pc++
  594. }
  595. type loadStackLex int
  596. func (l loadStackLex) exec(vm *vm) {
  597. // l < 0 -- arg<-l-1>
  598. // l > 0 -- var<l-1>
  599. var p *Value
  600. if l < 0 {
  601. arg := int(-l)
  602. if arg > vm.args {
  603. vm.push(_undefined)
  604. vm.pc++
  605. return
  606. } else {
  607. p = &vm.stack[vm.sb+arg]
  608. }
  609. } else {
  610. p = &vm.stack[vm.sb+vm.args+int(l)]
  611. }
  612. if *p == nil {
  613. panic(errAccessBeforeInit)
  614. }
  615. vm.push(*p)
  616. vm.pc++
  617. }
  618. type loadStack1Lex int
  619. func (l loadStack1Lex) exec(vm *vm) {
  620. p := &vm.stack[vm.sb+int(l)]
  621. if *p == nil {
  622. panic(errAccessBeforeInit)
  623. }
  624. vm.push(*p)
  625. vm.pc++
  626. }
  627. type _loadCallee struct{}
  628. var loadCallee _loadCallee
  629. func (_loadCallee) exec(vm *vm) {
  630. vm.push(vm.stack[vm.sb-1])
  631. vm.pc++
  632. }
  633. func (vm *vm) storeStack(s int) {
  634. // l > 0 -- var<l-1>
  635. if s > 0 {
  636. vm.stack[vm.sb+vm.args+s] = vm.stack[vm.sp-1]
  637. } else {
  638. panic("Illegal stack var index")
  639. }
  640. vm.pc++
  641. }
  642. func (vm *vm) storeStack1(s int) {
  643. // args are in stash
  644. // l > 0 -- var<l-1>
  645. if s > 0 {
  646. vm.stack[vm.sb+s] = vm.stack[vm.sp-1]
  647. } else {
  648. panic("Illegal stack var index")
  649. }
  650. vm.pc++
  651. }
  652. func (vm *vm) storeStackLex(s int) {
  653. // l < 0 -- arg<-l-1>
  654. // l > 0 -- var<l-1>
  655. var p *Value
  656. if s < 0 {
  657. p = &vm.stack[vm.sb-s]
  658. } else {
  659. p = &vm.stack[vm.sb+vm.args+s]
  660. }
  661. if *p != nil {
  662. *p = vm.stack[vm.sp-1]
  663. } else {
  664. panic(errAccessBeforeInit)
  665. }
  666. vm.pc++
  667. }
  668. func (vm *vm) storeStack1Lex(s int) {
  669. // args are in stash
  670. // s > 0 -- var<l-1>
  671. if s <= 0 {
  672. panic("Illegal stack var index")
  673. }
  674. p := &vm.stack[vm.sb+s]
  675. if *p != nil {
  676. *p = vm.stack[vm.sp-1]
  677. } else {
  678. panic(errAccessBeforeInit)
  679. }
  680. vm.pc++
  681. }
  682. func (vm *vm) initStack(s int) {
  683. if s <= 0 {
  684. vm.stack[vm.sb-s] = vm.stack[vm.sp-1]
  685. } else {
  686. vm.stack[vm.sb+vm.args+s] = vm.stack[vm.sp-1]
  687. }
  688. vm.pc++
  689. }
  690. func (vm *vm) initStack1(s int) {
  691. if s <= 0 {
  692. panic("Illegal stack var index")
  693. }
  694. vm.stack[vm.sb+s] = vm.stack[vm.sp-1]
  695. vm.pc++
  696. }
  697. type storeStack int
  698. func (s storeStack) exec(vm *vm) {
  699. vm.storeStack(int(s))
  700. }
  701. type storeStack1 int
  702. func (s storeStack1) exec(vm *vm) {
  703. vm.storeStack1(int(s))
  704. }
  705. type storeStackLex int
  706. func (s storeStackLex) exec(vm *vm) {
  707. vm.storeStackLex(int(s))
  708. }
  709. type storeStack1Lex int
  710. func (s storeStack1Lex) exec(vm *vm) {
  711. vm.storeStack1Lex(int(s))
  712. }
  713. type initStack int
  714. func (s initStack) exec(vm *vm) {
  715. vm.initStack(int(s))
  716. vm.sp--
  717. }
  718. type initStack1 int
  719. func (s initStack1) exec(vm *vm) {
  720. vm.initStack1(int(s))
  721. vm.sp--
  722. }
  723. type storeStackP int
  724. func (s storeStackP) exec(vm *vm) {
  725. vm.storeStack(int(s))
  726. vm.sp--
  727. }
  728. type storeStack1P int
  729. func (s storeStack1P) exec(vm *vm) {
  730. vm.storeStack1(int(s))
  731. vm.sp--
  732. }
  733. type storeStackLexP int
  734. func (s storeStackLexP) exec(vm *vm) {
  735. vm.storeStackLex(int(s))
  736. vm.sp--
  737. }
  738. type storeStack1LexP int
  739. func (s storeStack1LexP) exec(vm *vm) {
  740. vm.storeStack1Lex(int(s))
  741. vm.sp--
  742. }
  743. type _toNumber struct{}
  744. var toNumber _toNumber
  745. func (_toNumber) exec(vm *vm) {
  746. vm.stack[vm.sp-1] = vm.stack[vm.sp-1].ToNumber()
  747. vm.pc++
  748. }
  749. type _add struct{}
  750. var add _add
  751. func (_add) exec(vm *vm) {
  752. right := vm.stack[vm.sp-1]
  753. left := vm.stack[vm.sp-2]
  754. if o, ok := left.(*Object); ok {
  755. left = o.toPrimitive()
  756. }
  757. if o, ok := right.(*Object); ok {
  758. right = o.toPrimitive()
  759. }
  760. var ret Value
  761. leftString, isLeftString := left.(valueString)
  762. rightString, isRightString := right.(valueString)
  763. if isLeftString || isRightString {
  764. if !isLeftString {
  765. leftString = left.toString()
  766. }
  767. if !isRightString {
  768. rightString = right.toString()
  769. }
  770. ret = leftString.concat(rightString)
  771. } else if leftBigInt, ok := left.(valueBigInt); ok {
  772. rightBigInt, ok := right.(valueBigInt)
  773. if !ok {
  774. panic(vm.r.NewTypeError("Cannot add BigInt to other type"))
  775. }
  776. b := &big.Int{}
  777. b.Add(leftBigInt.Int, rightBigInt.Int)
  778. ret = valueBigInt{b}
  779. } else if _, ok := right.(valueBigInt); ok {
  780. panic(vm.r.NewTypeError("Cannot add BigInt to other type"))
  781. } else {
  782. if leftInt, ok := left.(valueInt); ok {
  783. if rightInt, ok := right.(valueInt); ok {
  784. ret = intToValue(int64(leftInt) + int64(rightInt))
  785. } else {
  786. ret = floatToValue(float64(leftInt) + right.ToFloat())
  787. }
  788. } else {
  789. ret = floatToValue(left.ToFloat() + right.ToFloat())
  790. }
  791. }
  792. vm.stack[vm.sp-2] = ret
  793. vm.sp--
  794. vm.pc++
  795. }
  796. type _sub struct{}
  797. var sub _sub
  798. func (_sub) exec(vm *vm) {
  799. right := vm.stack[vm.sp-1]
  800. left := vm.stack[vm.sp-2]
  801. var result Value
  802. if leftInt, ok := left.(valueInt); ok {
  803. if rightInt, ok := right.(valueInt); ok {
  804. result = intToValue(int64(leftInt) - int64(rightInt))
  805. goto end
  806. }
  807. } else if leftBigInt, ok := left.(valueBigInt); ok {
  808. if rightBigInt, ok := right.(valueBigInt); ok {
  809. b := &big.Int{}
  810. b.Sub(leftBigInt.Int, rightBigInt.Int)
  811. result = valueBigInt{b}
  812. goto end
  813. }
  814. } else if _, ok := right.(valueBigInt); ok {
  815. panic(vm.r.NewTypeError("Cannot subtract BigInt from other type"))
  816. }
  817. result = floatToValue(left.ToFloat() - right.ToFloat())
  818. end:
  819. vm.sp--
  820. vm.stack[vm.sp-1] = result
  821. vm.pc++
  822. }
  823. type _mul struct{}
  824. var mul _mul
  825. func (_mul) exec(vm *vm) {
  826. left := vm.stack[vm.sp-2]
  827. right := vm.stack[vm.sp-1]
  828. if o, ok := left.(*Object); ok {
  829. left = o.toPrimitive()
  830. }
  831. if o, ok := right.(*Object); ok {
  832. right = o.toPrimitive()
  833. }
  834. var result Value
  835. if leftBigInt, ok := left.(valueBigInt); ok {
  836. if rightBigInt, ok := right.(valueBigInt); ok {
  837. b := &big.Int{}
  838. b.Mul(leftBigInt.Int, rightBigInt.Int)
  839. result = valueBigInt{b}
  840. goto end
  841. } else {
  842. panic(vm.r.NewTypeError("Cannot multiply BigInt with other type"))
  843. }
  844. } else if _, ok := right.(valueBigInt); ok {
  845. panic(vm.r.NewTypeError("Cannot multiply BigInt with other type"))
  846. }
  847. if left, ok := assertInt64(left); ok {
  848. if right, ok := assertInt64(right); ok {
  849. if left == 0 && right == -1 || left == -1 && right == 0 {
  850. result = _negativeZero
  851. goto end
  852. }
  853. res := left * right
  854. // check for overflow
  855. if left == 0 || right == 0 || res/left == right {
  856. result = intToValue(res)
  857. goto end
  858. }
  859. }
  860. }
  861. result = floatToValue(left.ToFloat() * right.ToFloat())
  862. end:
  863. vm.sp--
  864. vm.stack[vm.sp-1] = result
  865. vm.pc++
  866. }
  867. type _div struct{}
  868. var div _div
  869. func (_div) exec(vm *vm) {
  870. left := vm.stack[vm.sp-2]
  871. right := vm.stack[vm.sp-1]
  872. if o, ok := left.(*Object); ok {
  873. left = o.toPrimitive()
  874. }
  875. if o, ok := right.(*Object); ok {
  876. right = o.toPrimitive()
  877. }
  878. var result Value
  879. var rightFloat float64
  880. var leftFloat float64
  881. if leftBigInt, ok := left.(valueBigInt); ok {
  882. rightBigInt, ok := right.(valueBigInt)
  883. if !ok {
  884. panic(vm.r.NewTypeError("Cannot divide BigInt by other type"))
  885. }
  886. if rightBigInt.Int64() == 0 {
  887. panic(vm.r.newError(vm.r.global.RangeError, "Cannot divide by zero"))
  888. }
  889. b := &big.Int{}
  890. b.Quo(leftBigInt.Int, rightBigInt.Int)
  891. result = valueBigInt{b}
  892. goto end
  893. } else if _, ok := right.(valueBigInt); ok {
  894. panic(vm.r.NewTypeError("Cannot divide BigInt by other type"))
  895. }
  896. leftFloat = left.ToFloat()
  897. rightFloat = right.ToFloat()
  898. if math.IsNaN(leftFloat) || math.IsNaN(rightFloat) {
  899. result = _NaN
  900. goto end
  901. }
  902. if math.IsInf(leftFloat, 0) && math.IsInf(rightFloat, 0) {
  903. result = _NaN
  904. goto end
  905. }
  906. if leftFloat == 0 && rightFloat == 0 {
  907. result = _NaN
  908. goto end
  909. }
  910. if math.IsInf(leftFloat, 0) {
  911. if math.Signbit(leftFloat) == math.Signbit(rightFloat) {
  912. result = _positiveInf
  913. goto end
  914. } else {
  915. result = _negativeInf
  916. goto end
  917. }
  918. }
  919. if math.IsInf(rightFloat, 0) {
  920. if math.Signbit(leftFloat) == math.Signbit(rightFloat) {
  921. result = _positiveZero
  922. goto end
  923. } else {
  924. result = _negativeZero
  925. goto end
  926. }
  927. }
  928. if rightFloat == 0 {
  929. if math.Signbit(leftFloat) == math.Signbit(rightFloat) {
  930. result = _positiveInf
  931. goto end
  932. } else {
  933. result = _negativeInf
  934. goto end
  935. }
  936. }
  937. result = floatToValue(leftFloat / rightFloat)
  938. end:
  939. vm.sp--
  940. vm.stack[vm.sp-1] = result
  941. vm.pc++
  942. }
  943. type _mod struct{}
  944. var mod _mod
  945. func (_mod) exec(vm *vm) {
  946. left := vm.stack[vm.sp-2]
  947. right := vm.stack[vm.sp-1]
  948. if o, ok := left.(*Object); ok {
  949. left = o.toPrimitive()
  950. }
  951. if o, ok := right.(*Object); ok {
  952. right = o.toPrimitive()
  953. }
  954. var result Value
  955. if leftBigInt, ok := left.(valueBigInt); ok {
  956. rightBigInt, ok := right.(valueBigInt)
  957. if !ok {
  958. panic(vm.r.NewTypeError("Cannot get modulus of BigInt with other type"))
  959. }
  960. if rightBigInt.Int64() == 0 {
  961. panic(vm.r.newError(vm.r.global.RangeError, "Cannot divide by zero"))
  962. }
  963. b := &big.Int{}
  964. b.Rem(leftBigInt.Int, rightBigInt.Int)
  965. result = valueBigInt{b}
  966. goto end
  967. } else if _, ok := right.(valueBigInt); ok {
  968. panic(vm.r.NewTypeError("Cannot get modulus of BigInt with other type"))
  969. } else if leftInt, ok := assertInt64(left); ok {
  970. if rightInt, ok := assertInt64(right); ok {
  971. if rightInt == 0 {
  972. result = _NaN
  973. goto end
  974. }
  975. r := leftInt % rightInt
  976. if r == 0 && leftInt < 0 {
  977. result = _negativeZero
  978. } else {
  979. result = intToValue(leftInt % rightInt)
  980. }
  981. goto end
  982. }
  983. }
  984. result = floatToValue(math.Mod(left.ToFloat(), right.ToFloat()))
  985. end:
  986. vm.sp--
  987. vm.stack[vm.sp-1] = result
  988. vm.pc++
  989. }
  990. type _neg struct{}
  991. var neg _neg
  992. func (_neg) exec(vm *vm) {
  993. operand := vm.stack[vm.sp-1]
  994. var result Value
  995. if b, ok := assertBigInt(operand); ok {
  996. nb := &big.Int{}
  997. nb.Neg(b)
  998. result = valueBigInt{nb}
  999. } else if i, ok := assertInt64(operand); ok {
  1000. if i == 0 {
  1001. result = _negativeZero
  1002. } else {
  1003. result = valueInt(-i)
  1004. }
  1005. } else {
  1006. f := operand.ToFloat()
  1007. if !math.IsNaN(f) {
  1008. f = -f
  1009. }
  1010. result = valueFloat(f)
  1011. }
  1012. vm.stack[vm.sp-1] = result
  1013. vm.pc++
  1014. }
  1015. type _plus struct{}
  1016. var plus _plus
  1017. func (_plus) exec(vm *vm) {
  1018. vm.stack[vm.sp-1] = vm.stack[vm.sp-1].ToNumber()
  1019. vm.pc++
  1020. }
  1021. type _inc struct{}
  1022. var inc _inc
  1023. func (_inc) exec(vm *vm) {
  1024. v := vm.stack[vm.sp-1]
  1025. if i, ok := assertInt64(v); ok {
  1026. v = intToValue(i + 1)
  1027. goto end
  1028. } else if bi, ok := v.(valueBigInt); ok {
  1029. b := &big.Int{}
  1030. b.Add(bi.Int, big.NewInt(1))
  1031. v = valueBigInt{b}
  1032. goto end
  1033. }
  1034. v = valueFloat(v.ToFloat() + 1)
  1035. end:
  1036. vm.stack[vm.sp-1] = v
  1037. vm.pc++
  1038. }
  1039. type _dec struct{}
  1040. var dec _dec
  1041. func (_dec) exec(vm *vm) {
  1042. v := vm.stack[vm.sp-1]
  1043. if i, ok := assertInt64(v); ok {
  1044. v = intToValue(i - 1)
  1045. goto end
  1046. } else if bi, ok := v.(valueBigInt); ok {
  1047. b := &big.Int{}
  1048. b.Sub(bi.Int, big.NewInt(1))
  1049. v = valueBigInt{b}
  1050. goto end
  1051. }
  1052. v = valueFloat(v.ToFloat() - 1)
  1053. end:
  1054. vm.stack[vm.sp-1] = v
  1055. vm.pc++
  1056. }
  1057. type _and struct{}
  1058. var and _and
  1059. func (_and) exec(vm *vm) {
  1060. left := toInt32(vm.stack[vm.sp-2])
  1061. right := toInt32(vm.stack[vm.sp-1])
  1062. vm.stack[vm.sp-2] = intToValue(int64(left & right))
  1063. vm.sp--
  1064. vm.pc++
  1065. }
  1066. type _or struct{}
  1067. var or _or
  1068. func (_or) exec(vm *vm) {
  1069. left := toInt32(vm.stack[vm.sp-2])
  1070. right := toInt32(vm.stack[vm.sp-1])
  1071. vm.stack[vm.sp-2] = intToValue(int64(left | right))
  1072. vm.sp--
  1073. vm.pc++
  1074. }
  1075. type _xor struct{}
  1076. var xor _xor
  1077. func (_xor) exec(vm *vm) {
  1078. left := toInt32(vm.stack[vm.sp-2])
  1079. right := toInt32(vm.stack[vm.sp-1])
  1080. vm.stack[vm.sp-2] = intToValue(int64(left ^ right))
  1081. vm.sp--
  1082. vm.pc++
  1083. }
  1084. type _bnot struct{}
  1085. var bnot _bnot
  1086. func (_bnot) exec(vm *vm) {
  1087. op := toInt32(vm.stack[vm.sp-1])
  1088. vm.stack[vm.sp-1] = intToValue(int64(^op))
  1089. vm.pc++
  1090. }
  1091. type _sal struct{}
  1092. var sal _sal
  1093. func (_sal) exec(vm *vm) {
  1094. left := toInt32(vm.stack[vm.sp-2])
  1095. right := toUint32(vm.stack[vm.sp-1])
  1096. vm.stack[vm.sp-2] = intToValue(int64(left << (right & 0x1F)))
  1097. vm.sp--
  1098. vm.pc++
  1099. }
  1100. type _sar struct{}
  1101. var sar _sar
  1102. func (_sar) exec(vm *vm) {
  1103. left := toInt32(vm.stack[vm.sp-2])
  1104. right := toUint32(vm.stack[vm.sp-1])
  1105. vm.stack[vm.sp-2] = intToValue(int64(left >> (right & 0x1F)))
  1106. vm.sp--
  1107. vm.pc++
  1108. }
  1109. type _shr struct{}
  1110. var shr _shr
  1111. func (_shr) exec(vm *vm) {
  1112. left := toUint32(vm.stack[vm.sp-2])
  1113. right := toUint32(vm.stack[vm.sp-1])
  1114. vm.stack[vm.sp-2] = intToValue(int64(left >> (right & 0x1F)))
  1115. vm.sp--
  1116. vm.pc++
  1117. }
  1118. type _halt struct{}
  1119. var halt _halt
  1120. func (_halt) exec(vm *vm) {
  1121. vm.halt = true
  1122. vm.pc++
  1123. }
  1124. type jump int32
  1125. func (j jump) exec(vm *vm) {
  1126. vm.pc += int(j)
  1127. }
  1128. type _toPropertyKey struct{}
  1129. func (_toPropertyKey) exec(vm *vm) {
  1130. p := vm.sp - 1
  1131. vm.stack[p] = toPropertyKey(vm.stack[p])
  1132. vm.pc++
  1133. }
  1134. type _toString struct{}
  1135. func (_toString) exec(vm *vm) {
  1136. p := vm.sp - 1
  1137. vm.stack[p] = vm.stack[p].toString()
  1138. vm.pc++
  1139. }
  1140. type _getElemRef struct{}
  1141. var getElemRef _getElemRef
  1142. func (_getElemRef) exec(vm *vm) {
  1143. obj := vm.stack[vm.sp-2].ToObject(vm.r)
  1144. propName := toPropertyKey(vm.stack[vm.sp-1])
  1145. vm.refStack = append(vm.refStack, &objRef{
  1146. base: obj.self,
  1147. name: propName.string(),
  1148. })
  1149. vm.sp -= 2
  1150. vm.pc++
  1151. }
  1152. type _getElemRefStrict struct{}
  1153. var getElemRefStrict _getElemRefStrict
  1154. func (_getElemRefStrict) exec(vm *vm) {
  1155. obj := vm.stack[vm.sp-2].ToObject(vm.r)
  1156. propName := toPropertyKey(vm.stack[vm.sp-1])
  1157. vm.refStack = append(vm.refStack, &objRef{
  1158. base: obj.self,
  1159. name: propName.string(),
  1160. strict: true,
  1161. })
  1162. vm.sp -= 2
  1163. vm.pc++
  1164. }
  1165. type _setElem struct{}
  1166. var setElem _setElem
  1167. func (_setElem) exec(vm *vm) {
  1168. obj := vm.stack[vm.sp-3].ToObject(vm.r)
  1169. propName := toPropertyKey(vm.stack[vm.sp-2])
  1170. val := vm.stack[vm.sp-1]
  1171. obj.setOwn(propName, val, false)
  1172. vm.sp -= 2
  1173. vm.stack[vm.sp-1] = val
  1174. vm.pc++
  1175. }
  1176. type _setElem1 struct{}
  1177. var setElem1 _setElem1
  1178. func (_setElem1) exec(vm *vm) {
  1179. obj := vm.stack[vm.sp-3].ToObject(vm.r)
  1180. propName := vm.stack[vm.sp-2]
  1181. val := vm.stack[vm.sp-1]
  1182. obj.setOwn(propName, val, true)
  1183. vm.sp -= 2
  1184. vm.pc++
  1185. }
  1186. type _setElem1Named struct{}
  1187. var setElem1Named _setElem1Named
  1188. func (_setElem1Named) exec(vm *vm) {
  1189. receiver := vm.stack[vm.sp-3]
  1190. base := receiver.ToObject(vm.r)
  1191. propName := vm.stack[vm.sp-2]
  1192. val := vm.stack[vm.sp-1]
  1193. vm.r.toObject(val).self.defineOwnPropertyStr("name", PropertyDescriptor{
  1194. Value: funcName("", propName),
  1195. Configurable: FLAG_TRUE,
  1196. }, true)
  1197. base.set(propName, val, receiver, true)
  1198. vm.sp -= 2
  1199. vm.pc++
  1200. }
  1201. type _setElemP struct{}
  1202. var setElemP _setElemP
  1203. func (_setElemP) exec(vm *vm) {
  1204. obj := vm.stack[vm.sp-3].ToObject(vm.r)
  1205. propName := toPropertyKey(vm.stack[vm.sp-2])
  1206. val := vm.stack[vm.sp-1]
  1207. obj.setOwn(propName, val, false)
  1208. vm.sp -= 3
  1209. vm.pc++
  1210. }
  1211. type _setElemStrict struct{}
  1212. var setElemStrict _setElemStrict
  1213. func (_setElemStrict) exec(vm *vm) {
  1214. propName := toPropertyKey(vm.stack[vm.sp-2])
  1215. receiver := vm.stack[vm.sp-3]
  1216. val := vm.stack[vm.sp-1]
  1217. if receiverObj, ok := receiver.(*Object); ok {
  1218. receiverObj.setOwn(propName, val, true)
  1219. } else {
  1220. base := receiver.ToObject(vm.r)
  1221. base.set(propName, val, receiver, true)
  1222. }
  1223. vm.sp -= 2
  1224. vm.stack[vm.sp-1] = val
  1225. vm.pc++
  1226. }
  1227. type _setElemStrictP struct{}
  1228. var setElemStrictP _setElemStrictP
  1229. func (_setElemStrictP) exec(vm *vm) {
  1230. propName := toPropertyKey(vm.stack[vm.sp-2])
  1231. receiver := vm.stack[vm.sp-3]
  1232. val := vm.stack[vm.sp-1]
  1233. if receiverObj, ok := receiver.(*Object); ok {
  1234. receiverObj.setOwn(propName, val, true)
  1235. } else {
  1236. base := receiver.ToObject(vm.r)
  1237. base.set(propName, val, receiver, true)
  1238. }
  1239. vm.sp -= 3
  1240. vm.pc++
  1241. }
  1242. type _deleteElem struct{}
  1243. var deleteElem _deleteElem
  1244. func (_deleteElem) exec(vm *vm) {
  1245. obj := vm.stack[vm.sp-2].ToObject(vm.r)
  1246. propName := toPropertyKey(vm.stack[vm.sp-1])
  1247. if obj.delete(propName, false) {
  1248. vm.stack[vm.sp-2] = valueTrue
  1249. } else {
  1250. vm.stack[vm.sp-2] = valueFalse
  1251. }
  1252. vm.sp--
  1253. vm.pc++
  1254. }
  1255. type _deleteElemStrict struct{}
  1256. var deleteElemStrict _deleteElemStrict
  1257. func (_deleteElemStrict) exec(vm *vm) {
  1258. obj := vm.stack[vm.sp-2].ToObject(vm.r)
  1259. propName := toPropertyKey(vm.stack[vm.sp-1])
  1260. obj.delete(propName, true)
  1261. vm.stack[vm.sp-2] = valueTrue
  1262. vm.sp--
  1263. vm.pc++
  1264. }
  1265. type deleteProp unistring.String
  1266. func (d deleteProp) exec(vm *vm) {
  1267. obj := vm.r.toObject(vm.stack[vm.sp-1])
  1268. if obj.self.deleteStr(unistring.String(d), false) {
  1269. vm.stack[vm.sp-1] = valueTrue
  1270. } else {
  1271. vm.stack[vm.sp-1] = valueFalse
  1272. }
  1273. vm.pc++
  1274. }
  1275. type deletePropStrict unistring.String
  1276. func (d deletePropStrict) exec(vm *vm) {
  1277. obj := vm.r.toObject(vm.stack[vm.sp-1])
  1278. obj.self.deleteStr(unistring.String(d), true)
  1279. vm.stack[vm.sp-1] = valueTrue
  1280. vm.pc++
  1281. }
  1282. type getPropRef unistring.String
  1283. func (p getPropRef) exec(vm *vm) {
  1284. vm.refStack = append(vm.refStack, &objRef{
  1285. base: vm.stack[vm.sp-1].ToObject(vm.r).self,
  1286. name: unistring.String(p),
  1287. })
  1288. vm.sp--
  1289. vm.pc++
  1290. }
  1291. type getPropRefStrict unistring.String
  1292. func (p getPropRefStrict) exec(vm *vm) {
  1293. vm.refStack = append(vm.refStack, &objRef{
  1294. base: vm.stack[vm.sp-1].ToObject(vm.r).self,
  1295. name: unistring.String(p),
  1296. strict: true,
  1297. })
  1298. vm.sp--
  1299. vm.pc++
  1300. }
  1301. type setProp unistring.String
  1302. func (p setProp) exec(vm *vm) {
  1303. val := vm.stack[vm.sp-1]
  1304. vm.stack[vm.sp-2].ToObject(vm.r).self.setOwnStr(unistring.String(p), val, false)
  1305. vm.stack[vm.sp-2] = val
  1306. vm.sp--
  1307. vm.pc++
  1308. }
  1309. type setPropP unistring.String
  1310. func (p setPropP) exec(vm *vm) {
  1311. val := vm.stack[vm.sp-1]
  1312. vm.stack[vm.sp-2].ToObject(vm.r).self.setOwnStr(unistring.String(p), val, false)
  1313. vm.sp -= 2
  1314. vm.pc++
  1315. }
  1316. type setPropStrict unistring.String
  1317. func (p setPropStrict) exec(vm *vm) {
  1318. receiver := vm.stack[vm.sp-2]
  1319. val := vm.stack[vm.sp-1]
  1320. propName := unistring.String(p)
  1321. if receiverObj, ok := receiver.(*Object); ok {
  1322. receiverObj.self.setOwnStr(propName, val, true)
  1323. } else {
  1324. base := receiver.ToObject(vm.r)
  1325. base.setStr(propName, val, receiver, true)
  1326. }
  1327. vm.stack[vm.sp-2] = val
  1328. vm.sp--
  1329. vm.pc++
  1330. }
  1331. type setPropStrictP unistring.String
  1332. func (p setPropStrictP) exec(vm *vm) {
  1333. receiver := vm.stack[vm.sp-2]
  1334. val := vm.stack[vm.sp-1]
  1335. propName := unistring.String(p)
  1336. if receiverObj, ok := receiver.(*Object); ok {
  1337. receiverObj.self.setOwnStr(propName, val, true)
  1338. } else {
  1339. base := receiver.ToObject(vm.r)
  1340. base.setStr(propName, val, receiver, true)
  1341. }
  1342. vm.sp -= 2
  1343. vm.pc++
  1344. }
  1345. type setProp1 unistring.String
  1346. func (p setProp1) exec(vm *vm) {
  1347. vm.r.toObject(vm.stack[vm.sp-2]).self._putProp(unistring.String(p), vm.stack[vm.sp-1], true, true, true)
  1348. vm.sp--
  1349. vm.pc++
  1350. }
  1351. type _setProto struct{}
  1352. var setProto _setProto
  1353. func (_setProto) exec(vm *vm) {
  1354. vm.r.setObjectProto(vm.stack[vm.sp-2], vm.stack[vm.sp-1])
  1355. vm.sp--
  1356. vm.pc++
  1357. }
  1358. type setPropGetter unistring.String
  1359. func (s setPropGetter) exec(vm *vm) {
  1360. obj := vm.r.toObject(vm.stack[vm.sp-2])
  1361. val := vm.stack[vm.sp-1]
  1362. vm.r.toObject(val).self.defineOwnPropertyStr("name", PropertyDescriptor{
  1363. Value: asciiString("get ").concat(stringValueFromRaw(unistring.String(s))),
  1364. Configurable: FLAG_TRUE,
  1365. }, true)
  1366. descr := PropertyDescriptor{
  1367. Getter: val,
  1368. Configurable: FLAG_TRUE,
  1369. Enumerable: FLAG_TRUE,
  1370. }
  1371. obj.self.defineOwnPropertyStr(unistring.String(s), descr, false)
  1372. vm.sp--
  1373. vm.pc++
  1374. }
  1375. type setPropSetter unistring.String
  1376. func (s setPropSetter) exec(vm *vm) {
  1377. obj := vm.r.toObject(vm.stack[vm.sp-2])
  1378. val := vm.stack[vm.sp-1]
  1379. vm.r.toObject(val).self.defineOwnPropertyStr("name", PropertyDescriptor{
  1380. Value: asciiString("set ").concat(stringValueFromRaw(unistring.String(s))),
  1381. Configurable: FLAG_TRUE,
  1382. }, true)
  1383. descr := PropertyDescriptor{
  1384. Setter: val,
  1385. Configurable: FLAG_TRUE,
  1386. Enumerable: FLAG_TRUE,
  1387. }
  1388. obj.self.defineOwnPropertyStr(unistring.String(s), descr, false)
  1389. vm.sp--
  1390. vm.pc++
  1391. }
  1392. type _setPropGetter1 struct{}
  1393. var setPropGetter1 _setPropGetter1
  1394. func (s _setPropGetter1) exec(vm *vm) {
  1395. obj := vm.r.toObject(vm.stack[vm.sp-3])
  1396. propName := vm.stack[vm.sp-2]
  1397. val := vm.stack[vm.sp-1]
  1398. vm.r.toObject(val).self.defineOwnPropertyStr("name", PropertyDescriptor{
  1399. Value: funcName("get ", propName),
  1400. Configurable: FLAG_TRUE,
  1401. }, true)
  1402. descr := PropertyDescriptor{
  1403. Getter: val,
  1404. Configurable: FLAG_TRUE,
  1405. Enumerable: FLAG_TRUE,
  1406. }
  1407. obj.defineOwnProperty(propName, descr, false)
  1408. vm.sp -= 2
  1409. vm.pc++
  1410. }
  1411. type _setPropSetter1 struct{}
  1412. var setPropSetter1 _setPropSetter1
  1413. func (s _setPropSetter1) exec(vm *vm) {
  1414. obj := vm.r.toObject(vm.stack[vm.sp-3])
  1415. propName := vm.stack[vm.sp-2]
  1416. val := vm.stack[vm.sp-1]
  1417. vm.r.toObject(val).self.defineOwnPropertyStr("name", PropertyDescriptor{
  1418. Value: funcName("set ", propName),
  1419. Configurable: FLAG_TRUE,
  1420. }, true)
  1421. descr := PropertyDescriptor{
  1422. Setter: val,
  1423. Configurable: FLAG_TRUE,
  1424. Enumerable: FLAG_TRUE,
  1425. }
  1426. obj.defineOwnProperty(propName, descr, false)
  1427. vm.sp -= 2
  1428. vm.pc++
  1429. }
  1430. type getProp unistring.String
  1431. func (g getProp) exec(vm *vm) {
  1432. v := vm.stack[vm.sp-1]
  1433. obj := v.baseObject(vm.r)
  1434. if obj == nil {
  1435. panic(vm.r.NewTypeError("Cannot read property '%s' of undefined", g))
  1436. }
  1437. vm.stack[vm.sp-1] = nilSafe(obj.self.getStr(unistring.String(g), v))
  1438. vm.pc++
  1439. }
  1440. type getPropCallee unistring.String
  1441. func (g getPropCallee) exec(vm *vm) {
  1442. v := vm.stack[vm.sp-1]
  1443. obj := v.baseObject(vm.r)
  1444. n := unistring.String(g)
  1445. if obj == nil {
  1446. panic(vm.r.NewTypeError("Cannot read property '%s' of undefined or null", n))
  1447. }
  1448. prop := obj.self.getStr(n, v)
  1449. if prop == nil {
  1450. prop = memberUnresolved{valueUnresolved{r: vm.r, ref: n}}
  1451. }
  1452. vm.stack[vm.sp-1] = prop
  1453. vm.pc++
  1454. }
  1455. type _getElem struct{}
  1456. var getElem _getElem
  1457. func (_getElem) exec(vm *vm) {
  1458. v := vm.stack[vm.sp-2]
  1459. obj := v.baseObject(vm.r)
  1460. propName := toPropertyKey(vm.stack[vm.sp-1])
  1461. if obj == nil {
  1462. panic(vm.r.NewTypeError("Cannot read property '%s' of undefined", propName.String()))
  1463. }
  1464. vm.stack[vm.sp-2] = nilSafe(obj.get(propName, v))
  1465. vm.sp--
  1466. vm.pc++
  1467. }
  1468. type _getKey struct{}
  1469. var getKey _getKey
  1470. func (_getKey) exec(vm *vm) {
  1471. v := vm.stack[vm.sp-2]
  1472. obj := v.baseObject(vm.r)
  1473. propName := vm.stack[vm.sp-1]
  1474. if obj == nil {
  1475. panic(vm.r.NewTypeError("Cannot read property '%s' of undefined", propName.String()))
  1476. }
  1477. vm.stack[vm.sp-2] = nilSafe(obj.get(propName, v))
  1478. vm.sp--
  1479. vm.pc++
  1480. }
  1481. type _getElemCallee struct{}
  1482. var getElemCallee _getElemCallee
  1483. func (_getElemCallee) exec(vm *vm) {
  1484. v := vm.stack[vm.sp-2]
  1485. obj := v.baseObject(vm.r)
  1486. propName := toPropertyKey(vm.stack[vm.sp-1])
  1487. if obj == nil {
  1488. panic(vm.r.NewTypeError("Cannot read property '%s' of undefined", propName.String()))
  1489. }
  1490. prop := obj.get(propName, v)
  1491. if prop == nil {
  1492. prop = memberUnresolved{valueUnresolved{r: vm.r, ref: propName.string()}}
  1493. }
  1494. vm.stack[vm.sp-2] = prop
  1495. vm.sp--
  1496. vm.pc++
  1497. }
  1498. type _dup struct{}
  1499. var dup _dup
  1500. func (_dup) exec(vm *vm) {
  1501. vm.push(vm.stack[vm.sp-1])
  1502. vm.pc++
  1503. }
  1504. type dupN uint32
  1505. func (d dupN) exec(vm *vm) {
  1506. vm.push(vm.stack[vm.sp-1-int(d)])
  1507. vm.pc++
  1508. }
  1509. type rdupN uint32
  1510. func (d rdupN) exec(vm *vm) {
  1511. vm.stack[vm.sp-1-int(d)] = vm.stack[vm.sp-1]
  1512. vm.pc++
  1513. }
  1514. type _newObject struct{}
  1515. var newObject _newObject
  1516. func (_newObject) exec(vm *vm) {
  1517. vm.push(vm.r.NewObject())
  1518. vm.pc++
  1519. }
  1520. type newArray uint32
  1521. func (l newArray) exec(vm *vm) {
  1522. values := make([]Value, 0, l)
  1523. vm.push(vm.r.newArrayValues(values))
  1524. vm.pc++
  1525. }
  1526. type _pushArrayItem struct{}
  1527. var pushArrayItem _pushArrayItem
  1528. func (_pushArrayItem) exec(vm *vm) {
  1529. arr := vm.stack[vm.sp-2].(*Object).self.(*arrayObject)
  1530. if arr.length < math.MaxUint32 {
  1531. arr.length++
  1532. } else {
  1533. panic(vm.r.newError(vm.r.global.RangeError, "Invalid array length"))
  1534. }
  1535. val := vm.stack[vm.sp-1]
  1536. arr.values = append(arr.values, val)
  1537. if val != nil {
  1538. arr.objCount++
  1539. }
  1540. vm.sp--
  1541. vm.pc++
  1542. }
  1543. type _pushArraySpread struct{}
  1544. var pushArraySpread _pushArraySpread
  1545. func (_pushArraySpread) exec(vm *vm) {
  1546. arr := vm.stack[vm.sp-2].(*Object).self.(*arrayObject)
  1547. vm.r.getIterator(vm.stack[vm.sp-1], nil).iterate(func(val Value) {
  1548. if arr.length < math.MaxUint32 {
  1549. arr.length++
  1550. } else {
  1551. panic(vm.r.newError(vm.r.global.RangeError, "Invalid array length"))
  1552. }
  1553. arr.values = append(arr.values, val)
  1554. arr.objCount++
  1555. })
  1556. vm.sp--
  1557. vm.pc++
  1558. }
  1559. type _pushSpread struct{}
  1560. var pushSpread _pushSpread
  1561. func (_pushSpread) exec(vm *vm) {
  1562. vm.sp--
  1563. obj := vm.stack[vm.sp]
  1564. vm.r.getIterator(obj, nil).iterate(func(val Value) {
  1565. vm.push(val)
  1566. })
  1567. vm.pc++
  1568. }
  1569. type _newArrayFromIter struct{}
  1570. var newArrayFromIter _newArrayFromIter
  1571. func (_newArrayFromIter) exec(vm *vm) {
  1572. var values []Value
  1573. l := len(vm.iterStack) - 1
  1574. iter := vm.iterStack[l].iter
  1575. vm.iterStack[l] = iterStackItem{}
  1576. vm.iterStack = vm.iterStack[:l]
  1577. iter.iterate(func(val Value) {
  1578. values = append(values, val)
  1579. })
  1580. vm.push(vm.r.newArrayValues(values))
  1581. vm.pc++
  1582. }
  1583. type newRegexp struct {
  1584. pattern *regexpPattern
  1585. src valueString
  1586. }
  1587. func (n *newRegexp) exec(vm *vm) {
  1588. vm.push(vm.r.newRegExpp(n.pattern.clone(), n.src, vm.r.global.RegExpPrototype).val)
  1589. vm.pc++
  1590. }
  1591. func (vm *vm) setLocalLex(s int) {
  1592. v := vm.stack[vm.sp-1]
  1593. level := s >> 24
  1594. idx := uint32(s & 0x00FFFFFF)
  1595. stash := vm.stash
  1596. for i := 0; i < level; i++ {
  1597. stash = stash.outer
  1598. }
  1599. p := &stash.values[idx]
  1600. if *p == nil {
  1601. panic(errAccessBeforeInit)
  1602. }
  1603. *p = v
  1604. vm.pc++
  1605. }
  1606. func (vm *vm) initLocal(s int) {
  1607. v := vm.stack[vm.sp-1]
  1608. level := s >> 24
  1609. idx := uint32(s & 0x00FFFFFF)
  1610. stash := vm.stash
  1611. for i := 0; i < level; i++ {
  1612. stash = stash.outer
  1613. }
  1614. stash.initByIdx(idx, v)
  1615. vm.pc++
  1616. }
  1617. type storeStash uint32
  1618. func (s storeStash) exec(vm *vm) {
  1619. vm.initLocal(int(s))
  1620. }
  1621. type storeStashP uint32
  1622. func (s storeStashP) exec(vm *vm) {
  1623. vm.initLocal(int(s))
  1624. vm.sp--
  1625. }
  1626. type storeStashLex uint32
  1627. func (s storeStashLex) exec(vm *vm) {
  1628. vm.setLocalLex(int(s))
  1629. }
  1630. type storeStashLexP uint32
  1631. func (s storeStashLexP) exec(vm *vm) {
  1632. vm.setLocalLex(int(s))
  1633. vm.sp--
  1634. }
  1635. type initStash uint32
  1636. func (s initStash) exec(vm *vm) {
  1637. vm.initLocal(int(s))
  1638. vm.sp--
  1639. }
  1640. type initGlobal unistring.String
  1641. func (s initGlobal) exec(vm *vm) {
  1642. vm.sp--
  1643. vm.r.global.stash.initByName(unistring.String(s), vm.stack[vm.sp])
  1644. vm.pc++
  1645. }
  1646. type resolveVar1 unistring.String
  1647. func (s resolveVar1) exec(vm *vm) {
  1648. name := unistring.String(s)
  1649. var ref ref
  1650. for stash := vm.stash; stash != nil; stash = stash.outer {
  1651. ref = stash.getRefByName(name, false)
  1652. if ref != nil {
  1653. goto end
  1654. }
  1655. }
  1656. ref = &objRef{
  1657. base: vm.r.globalObject.self,
  1658. name: name,
  1659. }
  1660. end:
  1661. vm.refStack = append(vm.refStack, ref)
  1662. vm.pc++
  1663. }
  1664. type deleteVar unistring.String
  1665. func (d deleteVar) exec(vm *vm) {
  1666. name := unistring.String(d)
  1667. ret := true
  1668. for stash := vm.stash; stash != nil; stash = stash.outer {
  1669. if stash.obj != nil {
  1670. if stashObjHas(stash.obj, name) {
  1671. ret = stash.obj.self.deleteStr(name, false)
  1672. goto end
  1673. }
  1674. } else {
  1675. if idx, exists := stash.names[name]; exists {
  1676. if idx&(maskVar|maskDeletable) == maskVar|maskDeletable {
  1677. stash.deleteBinding(name)
  1678. } else {
  1679. ret = false
  1680. }
  1681. goto end
  1682. }
  1683. }
  1684. }
  1685. if vm.r.globalObject.self.hasPropertyStr(name) {
  1686. ret = vm.r.globalObject.self.deleteStr(name, false)
  1687. }
  1688. end:
  1689. if ret {
  1690. vm.push(valueTrue)
  1691. } else {
  1692. vm.push(valueFalse)
  1693. }
  1694. vm.pc++
  1695. }
  1696. type deleteGlobal unistring.String
  1697. func (d deleteGlobal) exec(vm *vm) {
  1698. name := unistring.String(d)
  1699. var ret bool
  1700. if vm.r.globalObject.self.hasPropertyStr(name) {
  1701. ret = vm.r.globalObject.self.deleteStr(name, false)
  1702. if ret {
  1703. delete(vm.r.global.varNames, name)
  1704. }
  1705. } else {
  1706. ret = true
  1707. }
  1708. if ret {
  1709. vm.push(valueTrue)
  1710. } else {
  1711. vm.push(valueFalse)
  1712. }
  1713. vm.pc++
  1714. }
  1715. type resolveVar1Strict unistring.String
  1716. func (s resolveVar1Strict) exec(vm *vm) {
  1717. name := unistring.String(s)
  1718. var ref ref
  1719. for stash := vm.stash; stash != nil; stash = stash.outer {
  1720. ref = stash.getRefByName(name, true)
  1721. if ref != nil {
  1722. goto end
  1723. }
  1724. }
  1725. if vm.r.globalObject.self.hasPropertyStr(name) {
  1726. ref = &objRef{
  1727. base: vm.r.globalObject.self,
  1728. name: name,
  1729. strict: true,
  1730. }
  1731. goto end
  1732. }
  1733. ref = &unresolvedRef{
  1734. runtime: vm.r,
  1735. name: name,
  1736. }
  1737. end:
  1738. vm.refStack = append(vm.refStack, ref)
  1739. vm.pc++
  1740. }
  1741. type setGlobal unistring.String
  1742. func (s setGlobal) exec(vm *vm) {
  1743. vm.r.setGlobal(unistring.String(s), vm.peek(), false)
  1744. vm.pc++
  1745. }
  1746. type setGlobalStrict unistring.String
  1747. func (s setGlobalStrict) exec(vm *vm) {
  1748. vm.r.setGlobal(unistring.String(s), vm.peek(), true)
  1749. vm.pc++
  1750. }
  1751. // Load a var from stash
  1752. type loadStash uint32
  1753. func (g loadStash) exec(vm *vm) {
  1754. level := int(g >> 24)
  1755. idx := uint32(g & 0x00FFFFFF)
  1756. stash := vm.stash
  1757. for i := 0; i < level; i++ {
  1758. stash = stash.outer
  1759. }
  1760. vm.push(nilSafe(stash.getByIdx(idx)))
  1761. vm.pc++
  1762. }
  1763. // Load a lexical binding from stash
  1764. type loadStashLex uint32
  1765. func (g loadStashLex) exec(vm *vm) {
  1766. level := int(g >> 24)
  1767. idx := uint32(g & 0x00FFFFFF)
  1768. stash := vm.stash
  1769. for i := 0; i < level; i++ {
  1770. stash = stash.outer
  1771. }
  1772. v := stash.getByIdx(idx)
  1773. if v == nil {
  1774. panic(errAccessBeforeInit)
  1775. }
  1776. vm.push(v)
  1777. vm.pc++
  1778. }
  1779. // scan dynamic stashes up to the given level (encoded as 8 most significant bits of idx), if not found
  1780. // return the indexed var binding value from stash
  1781. type loadMixed struct {
  1782. name unistring.String
  1783. idx uint32
  1784. callee bool
  1785. }
  1786. func (g *loadMixed) exec(vm *vm) {
  1787. level := int(g.idx >> 24)
  1788. idx := g.idx & 0x00FFFFFF
  1789. stash := vm.stash
  1790. name := g.name
  1791. for i := 0; i < level; i++ {
  1792. if v, found := stash.getByName(name); found {
  1793. if g.callee {
  1794. if stash.obj != nil {
  1795. vm.push(stash.obj)
  1796. } else {
  1797. vm.push(_undefined)
  1798. }
  1799. }
  1800. vm.push(v)
  1801. goto end
  1802. }
  1803. stash = stash.outer
  1804. }
  1805. if g.callee {
  1806. vm.push(_undefined)
  1807. }
  1808. if stash != nil {
  1809. vm.push(nilSafe(stash.getByIdx(idx)))
  1810. }
  1811. end:
  1812. vm.pc++
  1813. }
  1814. // scan dynamic stashes up to the given level (encoded as 8 most significant bits of idx), if not found
  1815. // return the indexed lexical binding value from stash
  1816. type loadMixedLex loadMixed
  1817. func (g *loadMixedLex) exec(vm *vm) {
  1818. level := int(g.idx >> 24)
  1819. idx := g.idx & 0x00FFFFFF
  1820. stash := vm.stash
  1821. name := g.name
  1822. for i := 0; i < level; i++ {
  1823. if v, found := stash.getByName(name); found {
  1824. if g.callee {
  1825. if stash.obj != nil {
  1826. vm.push(stash.obj)
  1827. } else {
  1828. vm.push(_undefined)
  1829. }
  1830. }
  1831. vm.push(v)
  1832. goto end
  1833. }
  1834. stash = stash.outer
  1835. }
  1836. if g.callee {
  1837. vm.push(_undefined)
  1838. }
  1839. if stash != nil {
  1840. v := stash.getByIdx(idx)
  1841. if v == nil {
  1842. panic(errAccessBeforeInit)
  1843. }
  1844. vm.push(v)
  1845. }
  1846. end:
  1847. vm.pc++
  1848. }
  1849. // scan dynamic stashes up to the given level (encoded as 8 most significant bits of idx), if not found
  1850. // return the indexed var binding value from stack
  1851. type loadMixedStack struct {
  1852. name unistring.String
  1853. idx int
  1854. level uint8
  1855. callee bool
  1856. }
  1857. // same as loadMixedStack, but the args have been moved to stash (therefore stack layout is different)
  1858. type loadMixedStack1 loadMixedStack
  1859. func (g *loadMixedStack) exec(vm *vm) {
  1860. stash := vm.stash
  1861. name := g.name
  1862. level := int(g.level)
  1863. for i := 0; i < level; i++ {
  1864. if v, found := stash.getByName(name); found {
  1865. if g.callee {
  1866. if stash.obj != nil {
  1867. vm.push(stash.obj)
  1868. } else {
  1869. vm.push(_undefined)
  1870. }
  1871. }
  1872. vm.push(v)
  1873. goto end
  1874. }
  1875. stash = stash.outer
  1876. }
  1877. if g.callee {
  1878. vm.push(_undefined)
  1879. }
  1880. loadStack(g.idx).exec(vm)
  1881. return
  1882. end:
  1883. vm.pc++
  1884. }
  1885. func (g *loadMixedStack1) exec(vm *vm) {
  1886. stash := vm.stash
  1887. name := g.name
  1888. level := int(g.level)
  1889. for i := 0; i < level; i++ {
  1890. if v, found := stash.getByName(name); found {
  1891. if g.callee {
  1892. if stash.obj != nil {
  1893. vm.push(stash.obj)
  1894. } else {
  1895. vm.push(_undefined)
  1896. }
  1897. }
  1898. vm.push(v)
  1899. goto end
  1900. }
  1901. stash = stash.outer
  1902. }
  1903. if g.callee {
  1904. vm.push(_undefined)
  1905. }
  1906. loadStack1(g.idx).exec(vm)
  1907. return
  1908. end:
  1909. vm.pc++
  1910. }
  1911. type loadMixedStackLex loadMixedStack
  1912. // same as loadMixedStackLex but when the arguments have been moved into stash
  1913. type loadMixedStack1Lex loadMixedStack
  1914. func (g *loadMixedStackLex) exec(vm *vm) {
  1915. stash := vm.stash
  1916. name := g.name
  1917. level := int(g.level)
  1918. for i := 0; i < level; i++ {
  1919. if v, found := stash.getByName(name); found {
  1920. if g.callee {
  1921. if stash.obj != nil {
  1922. vm.push(stash.obj)
  1923. } else {
  1924. vm.push(_undefined)
  1925. }
  1926. }
  1927. vm.push(v)
  1928. goto end
  1929. }
  1930. stash = stash.outer
  1931. }
  1932. if g.callee {
  1933. vm.push(_undefined)
  1934. }
  1935. loadStackLex(g.idx).exec(vm)
  1936. return
  1937. end:
  1938. vm.pc++
  1939. }
  1940. func (g *loadMixedStack1Lex) exec(vm *vm) {
  1941. stash := vm.stash
  1942. name := g.name
  1943. level := int(g.level)
  1944. for i := 0; i < level; i++ {
  1945. if v, found := stash.getByName(name); found {
  1946. if g.callee {
  1947. if stash.obj != nil {
  1948. vm.push(stash.obj)
  1949. } else {
  1950. vm.push(_undefined)
  1951. }
  1952. }
  1953. vm.push(v)
  1954. goto end
  1955. }
  1956. stash = stash.outer
  1957. }
  1958. if g.callee {
  1959. vm.push(_undefined)
  1960. }
  1961. loadStack1Lex(g.idx).exec(vm)
  1962. return
  1963. end:
  1964. vm.pc++
  1965. }
  1966. type resolveMixed struct {
  1967. name unistring.String
  1968. idx uint32
  1969. typ varType
  1970. strict bool
  1971. }
  1972. func newStashRef(typ varType, name unistring.String, v *[]Value, idx int) ref {
  1973. switch typ {
  1974. case varTypeVar:
  1975. return &stashRef{
  1976. n: name,
  1977. v: v,
  1978. idx: idx,
  1979. }
  1980. case varTypeLet:
  1981. return &stashRefLex{
  1982. stashRef: stashRef{
  1983. n: name,
  1984. v: v,
  1985. idx: idx,
  1986. },
  1987. }
  1988. case varTypeConst, varTypeStrictConst:
  1989. return &stashRefConst{
  1990. stashRefLex: stashRefLex{
  1991. stashRef: stashRef{
  1992. n: name,
  1993. v: v,
  1994. idx: idx,
  1995. },
  1996. },
  1997. strictConst: typ == varTypeStrictConst,
  1998. }
  1999. }
  2000. panic("unsupported var type")
  2001. }
  2002. func (r *resolveMixed) exec(vm *vm) {
  2003. level := int(r.idx >> 24)
  2004. idx := r.idx & 0x00FFFFFF
  2005. stash := vm.stash
  2006. var ref ref
  2007. for i := 0; i < level; i++ {
  2008. ref = stash.getRefByName(r.name, r.strict)
  2009. if ref != nil {
  2010. goto end
  2011. }
  2012. stash = stash.outer
  2013. }
  2014. if stash != nil {
  2015. ref = newStashRef(r.typ, r.name, &stash.values, int(idx))
  2016. goto end
  2017. }
  2018. ref = &unresolvedRef{
  2019. runtime: vm.r,
  2020. name: r.name,
  2021. }
  2022. end:
  2023. vm.refStack = append(vm.refStack, ref)
  2024. vm.pc++
  2025. }
  2026. type resolveMixedStack struct {
  2027. name unistring.String
  2028. idx int
  2029. typ varType
  2030. level uint8
  2031. strict bool
  2032. }
  2033. type resolveMixedStack1 resolveMixedStack
  2034. func (r *resolveMixedStack) exec(vm *vm) {
  2035. level := int(r.level)
  2036. stash := vm.stash
  2037. var ref ref
  2038. var idx int
  2039. for i := 0; i < level; i++ {
  2040. ref = stash.getRefByName(r.name, r.strict)
  2041. if ref != nil {
  2042. goto end
  2043. }
  2044. stash = stash.outer
  2045. }
  2046. if r.idx > 0 {
  2047. idx = vm.sb + vm.args + r.idx
  2048. } else {
  2049. idx = vm.sb - r.idx
  2050. }
  2051. ref = newStashRef(r.typ, r.name, (*[]Value)(&vm.stack), idx)
  2052. end:
  2053. vm.refStack = append(vm.refStack, ref)
  2054. vm.pc++
  2055. }
  2056. func (r *resolveMixedStack1) exec(vm *vm) {
  2057. level := int(r.level)
  2058. stash := vm.stash
  2059. var ref ref
  2060. for i := 0; i < level; i++ {
  2061. ref = stash.getRefByName(r.name, r.strict)
  2062. if ref != nil {
  2063. goto end
  2064. }
  2065. stash = stash.outer
  2066. }
  2067. ref = newStashRef(r.typ, r.name, (*[]Value)(&vm.stack), vm.sb+r.idx)
  2068. end:
  2069. vm.refStack = append(vm.refStack, ref)
  2070. vm.pc++
  2071. }
  2072. type _getValue struct{}
  2073. var getValue _getValue
  2074. func (_getValue) exec(vm *vm) {
  2075. ref := vm.refStack[len(vm.refStack)-1]
  2076. if v := ref.get(); v != nil {
  2077. vm.push(v)
  2078. } else {
  2079. vm.r.throwReferenceError(ref.refname())
  2080. panic("Unreachable")
  2081. }
  2082. vm.pc++
  2083. }
  2084. type _putValue struct{}
  2085. var putValue _putValue
  2086. func (_putValue) exec(vm *vm) {
  2087. l := len(vm.refStack) - 1
  2088. ref := vm.refStack[l]
  2089. vm.refStack[l] = nil
  2090. vm.refStack = vm.refStack[:l]
  2091. ref.set(vm.stack[vm.sp-1])
  2092. vm.pc++
  2093. }
  2094. type _putValueP struct{}
  2095. var putValueP _putValueP
  2096. func (_putValueP) exec(vm *vm) {
  2097. l := len(vm.refStack) - 1
  2098. ref := vm.refStack[l]
  2099. vm.refStack[l] = nil
  2100. vm.refStack = vm.refStack[:l]
  2101. ref.set(vm.stack[vm.sp-1])
  2102. vm.sp--
  2103. vm.pc++
  2104. }
  2105. type _initValueP struct{}
  2106. var initValueP _initValueP
  2107. func (_initValueP) exec(vm *vm) {
  2108. l := len(vm.refStack) - 1
  2109. ref := vm.refStack[l]
  2110. vm.refStack[l] = nil
  2111. vm.refStack = vm.refStack[:l]
  2112. ref.init(vm.stack[vm.sp-1])
  2113. vm.sp--
  2114. vm.pc++
  2115. }
  2116. type loadDynamic unistring.String
  2117. func (n loadDynamic) exec(vm *vm) {
  2118. name := unistring.String(n)
  2119. var val Value
  2120. for stash := vm.stash; stash != nil; stash = stash.outer {
  2121. if v, exists := stash.getByName(name); exists {
  2122. val = v
  2123. break
  2124. }
  2125. }
  2126. if val == nil {
  2127. val = vm.r.globalObject.self.getStr(name, nil)
  2128. if val == nil {
  2129. vm.r.throwReferenceError(name)
  2130. }
  2131. }
  2132. vm.push(val)
  2133. vm.pc++
  2134. }
  2135. type loadDynamicRef unistring.String
  2136. func (n loadDynamicRef) exec(vm *vm) {
  2137. name := unistring.String(n)
  2138. var val Value
  2139. for stash := vm.stash; stash != nil; stash = stash.outer {
  2140. if v, exists := stash.getByName(name); exists {
  2141. val = v
  2142. break
  2143. }
  2144. }
  2145. if val == nil {
  2146. val = vm.r.globalObject.self.getStr(name, nil)
  2147. if val == nil {
  2148. val = valueUnresolved{r: vm.r, ref: name}
  2149. }
  2150. }
  2151. vm.push(val)
  2152. vm.pc++
  2153. }
  2154. type loadDynamicCallee unistring.String
  2155. func (n loadDynamicCallee) exec(vm *vm) {
  2156. name := unistring.String(n)
  2157. var val Value
  2158. var callee *Object
  2159. for stash := vm.stash; stash != nil; stash = stash.outer {
  2160. if v, exists := stash.getByName(name); exists {
  2161. callee = stash.obj
  2162. val = v
  2163. break
  2164. }
  2165. }
  2166. if val == nil {
  2167. val = vm.r.globalObject.self.getStr(name, nil)
  2168. if val == nil {
  2169. val = valueUnresolved{r: vm.r, ref: name}
  2170. }
  2171. }
  2172. if callee != nil {
  2173. vm.push(callee)
  2174. } else {
  2175. vm.push(_undefined)
  2176. }
  2177. vm.push(val)
  2178. vm.pc++
  2179. }
  2180. type _pop struct{}
  2181. var pop _pop
  2182. func (_pop) exec(vm *vm) {
  2183. vm.sp--
  2184. vm.pc++
  2185. }
  2186. func (vm *vm) callEval(n int, strict bool) {
  2187. if vm.r.toObject(vm.stack[vm.sp-n-1]) == vm.r.global.Eval {
  2188. if n > 0 {
  2189. srcVal := vm.stack[vm.sp-n]
  2190. if src, ok := srcVal.(valueString); ok {
  2191. var this Value
  2192. if vm.sb >= 0 {
  2193. this = vm.stack[vm.sb]
  2194. } else {
  2195. this = vm.r.globalObject
  2196. }
  2197. ret := vm.r.eval(src, true, strict, this)
  2198. vm.stack[vm.sp-n-2] = ret
  2199. } else {
  2200. vm.stack[vm.sp-n-2] = srcVal
  2201. }
  2202. } else {
  2203. vm.stack[vm.sp-n-2] = _undefined
  2204. }
  2205. vm.sp -= n + 1
  2206. vm.pc++
  2207. } else {
  2208. call(n).exec(vm)
  2209. }
  2210. }
  2211. type callEval uint32
  2212. func (numargs callEval) exec(vm *vm) {
  2213. vm.callEval(int(numargs), false)
  2214. }
  2215. type callEvalStrict uint32
  2216. func (numargs callEvalStrict) exec(vm *vm) {
  2217. vm.callEval(int(numargs), true)
  2218. }
  2219. type _callEvalVariadic struct{}
  2220. var callEvalVariadic _callEvalVariadic
  2221. func (_callEvalVariadic) exec(vm *vm) {
  2222. vm.callEval(vm.countVariadicArgs()-2, false)
  2223. }
  2224. type _callEvalVariadicStrict struct{}
  2225. var callEvalVariadicStrict _callEvalVariadicStrict
  2226. func (_callEvalVariadicStrict) exec(vm *vm) {
  2227. vm.callEval(vm.countVariadicArgs()-2, true)
  2228. }
  2229. type _boxThis struct{}
  2230. var boxThis _boxThis
  2231. func (_boxThis) exec(vm *vm) {
  2232. v := vm.stack[vm.sb]
  2233. if v == _undefined || v == _null {
  2234. vm.stack[vm.sb] = vm.r.globalObject
  2235. } else {
  2236. vm.stack[vm.sb] = v.ToObject(vm.r)
  2237. }
  2238. vm.pc++
  2239. }
  2240. var variadicMarker Value = newSymbol(asciiString("[variadic marker]"))
  2241. type _startVariadic struct{}
  2242. var startVariadic _startVariadic
  2243. func (_startVariadic) exec(vm *vm) {
  2244. vm.push(variadicMarker)
  2245. vm.pc++
  2246. }
  2247. type _callVariadic struct{}
  2248. var callVariadic _callVariadic
  2249. func (vm *vm) countVariadicArgs() int {
  2250. count := 0
  2251. for i := vm.sp - 1; i >= 0; i-- {
  2252. if vm.stack[i] == variadicMarker {
  2253. return count
  2254. }
  2255. count++
  2256. }
  2257. panic("Variadic marker was not found. Compiler bug.")
  2258. }
  2259. func (_callVariadic) exec(vm *vm) {
  2260. call(vm.countVariadicArgs() - 2).exec(vm)
  2261. }
  2262. type _endVariadic struct{}
  2263. var endVariadic _endVariadic
  2264. func (_endVariadic) exec(vm *vm) {
  2265. vm.sp--
  2266. vm.stack[vm.sp-1] = vm.stack[vm.sp]
  2267. vm.pc++
  2268. }
  2269. type call uint32
  2270. func (numargs call) exec(vm *vm) {
  2271. // this
  2272. // callee
  2273. // arg0
  2274. // ...
  2275. // arg<numargs-1>
  2276. n := int(numargs)
  2277. v := vm.stack[vm.sp-n-1] // callee
  2278. obj := vm.toCallee(v)
  2279. repeat:
  2280. switch f := obj.self.(type) {
  2281. case *methodFuncObject:
  2282. vm.pc++
  2283. vm.pushCtx()
  2284. vm.args = n
  2285. vm.prg = f.prg
  2286. vm.stash = f.stash
  2287. vm.pc = 0
  2288. vm.stack[vm.sp-n-1], vm.stack[vm.sp-n-2] = vm.stack[vm.sp-n-2], vm.stack[vm.sp-n-1]
  2289. return
  2290. case *funcObject:
  2291. vm.pc++
  2292. vm.pushCtx()
  2293. vm.args = n
  2294. vm.prg = f.prg
  2295. vm.stash = f.stash
  2296. vm.pc = 0
  2297. vm.stack[vm.sp-n-1], vm.stack[vm.sp-n-2] = vm.stack[vm.sp-n-2], vm.stack[vm.sp-n-1]
  2298. return
  2299. case *arrowFuncObject:
  2300. vm.pc++
  2301. vm.pushCtx()
  2302. vm.args = n
  2303. vm.prg = f.prg
  2304. vm.stash = f.stash
  2305. vm.pc = 0
  2306. vm.stack[vm.sp-n-1], vm.stack[vm.sp-n-2] = f.this, vm.stack[vm.sp-n-1]
  2307. vm.newTarget = f.newTarget
  2308. return
  2309. case *nativeFuncObject:
  2310. vm._nativeCall(f, n)
  2311. case *boundFuncObject:
  2312. vm._nativeCall(&f.nativeFuncObject, n)
  2313. case *proxyObject:
  2314. vm.pushCtx()
  2315. vm.prg = nil
  2316. vm.funcName = "proxy"
  2317. ret := f.apply(FunctionCall{This: vm.stack[vm.sp-n-2], Arguments: vm.stack[vm.sp-n : vm.sp]})
  2318. if ret == nil {
  2319. ret = _undefined
  2320. }
  2321. vm.stack[vm.sp-n-2] = ret
  2322. vm.popCtx()
  2323. vm.sp -= n + 1
  2324. vm.pc++
  2325. case *lazyObject:
  2326. obj.self = f.create(obj)
  2327. goto repeat
  2328. default:
  2329. vm.r.typeErrorResult(true, "Not a function: %s", obj.toString())
  2330. }
  2331. }
  2332. func (vm *vm) _nativeCall(f *nativeFuncObject, n int) {
  2333. if f.f != nil {
  2334. vm.pushCtx()
  2335. vm.prg = nil
  2336. vm.funcName = nilSafe(f.getStr("name", nil)).string()
  2337. ret := f.f(FunctionCall{
  2338. Arguments: vm.stack[vm.sp-n : vm.sp],
  2339. This: vm.stack[vm.sp-n-2],
  2340. })
  2341. if ret == nil {
  2342. ret = _undefined
  2343. }
  2344. vm.stack[vm.sp-n-2] = ret
  2345. vm.popCtx()
  2346. } else {
  2347. vm.stack[vm.sp-n-2] = _undefined
  2348. }
  2349. vm.sp -= n + 1
  2350. vm.pc++
  2351. }
  2352. func (vm *vm) clearStack() {
  2353. sp := vm.sp
  2354. stackTail := vm.stack[sp:]
  2355. for i := range stackTail {
  2356. stackTail[i] = nil
  2357. }
  2358. vm.stack = vm.stack[:sp]
  2359. }
  2360. type enterBlock struct {
  2361. names map[unistring.String]uint32
  2362. stashSize uint32
  2363. stackSize uint32
  2364. }
  2365. func (e *enterBlock) exec(vm *vm) {
  2366. if e.stashSize > 0 {
  2367. vm.newStash()
  2368. vm.stash.values = make([]Value, e.stashSize)
  2369. if len(e.names) > 0 {
  2370. vm.stash.names = e.names
  2371. }
  2372. }
  2373. ss := int(e.stackSize)
  2374. vm.stack.expand(vm.sp + ss - 1)
  2375. vv := vm.stack[vm.sp : vm.sp+ss]
  2376. for i := range vv {
  2377. vv[i] = nil
  2378. }
  2379. vm.sp += ss
  2380. vm.pc++
  2381. }
  2382. type enterCatchBlock struct {
  2383. names map[unistring.String]uint32
  2384. stashSize uint32
  2385. stackSize uint32
  2386. }
  2387. func (e *enterCatchBlock) exec(vm *vm) {
  2388. vm.newStash()
  2389. vm.stash.values = make([]Value, e.stashSize)
  2390. if len(e.names) > 0 {
  2391. vm.stash.names = e.names
  2392. }
  2393. vm.sp--
  2394. vm.stash.values[0] = vm.stack[vm.sp]
  2395. ss := int(e.stackSize)
  2396. vm.stack.expand(vm.sp + ss - 1)
  2397. vv := vm.stack[vm.sp : vm.sp+ss]
  2398. for i := range vv {
  2399. vv[i] = nil
  2400. }
  2401. vm.sp += ss
  2402. vm.pc++
  2403. }
  2404. type leaveBlock struct {
  2405. stackSize uint32
  2406. popStash bool
  2407. }
  2408. func (l *leaveBlock) exec(vm *vm) {
  2409. if l.popStash {
  2410. vm.stash = vm.stash.outer
  2411. }
  2412. if ss := l.stackSize; ss > 0 {
  2413. vm.sp -= int(ss)
  2414. }
  2415. vm.pc++
  2416. }
  2417. type enterFunc struct {
  2418. names map[unistring.String]uint32
  2419. stashSize uint32
  2420. stackSize uint32
  2421. numArgs uint32
  2422. argsToStash bool
  2423. extensible bool
  2424. }
  2425. func (e *enterFunc) exec(vm *vm) {
  2426. // Input stack:
  2427. //
  2428. // callee
  2429. // this
  2430. // arg0
  2431. // ...
  2432. // argN
  2433. // <- sp
  2434. // Output stack:
  2435. //
  2436. // this <- sb
  2437. // <local stack vars...>
  2438. // <- sp
  2439. sp := vm.sp
  2440. vm.sb = sp - vm.args - 1
  2441. vm.newStash()
  2442. stash := vm.stash
  2443. stash.variable = true
  2444. stash.values = make([]Value, e.stashSize)
  2445. if len(e.names) > 0 {
  2446. if e.extensible {
  2447. m := make(map[unistring.String]uint32, len(e.names))
  2448. for name, idx := range e.names {
  2449. m[name] = idx
  2450. }
  2451. stash.names = m
  2452. } else {
  2453. stash.names = e.names
  2454. }
  2455. }
  2456. ss := int(e.stackSize)
  2457. ea := 0
  2458. if e.argsToStash {
  2459. offset := vm.args - int(e.numArgs)
  2460. copy(stash.values, vm.stack[sp-vm.args:sp])
  2461. if offset > 0 {
  2462. vm.stash.extraArgs = make([]Value, offset)
  2463. copy(stash.extraArgs, vm.stack[sp-offset:])
  2464. } else {
  2465. vv := stash.values[vm.args:e.numArgs]
  2466. for i := range vv {
  2467. vv[i] = _undefined
  2468. }
  2469. }
  2470. sp -= vm.args
  2471. } else {
  2472. d := int(e.numArgs) - vm.args
  2473. if d > 0 {
  2474. ss += d
  2475. ea = d
  2476. vm.args = int(e.numArgs)
  2477. }
  2478. }
  2479. vm.stack.expand(sp + ss - 1)
  2480. if ea > 0 {
  2481. vv := vm.stack[sp : vm.sp+ea]
  2482. for i := range vv {
  2483. vv[i] = _undefined
  2484. }
  2485. }
  2486. vv := vm.stack[sp+ea : sp+ss]
  2487. for i := range vv {
  2488. vv[i] = nil
  2489. }
  2490. vm.sp = sp + ss
  2491. vm.pc++
  2492. }
  2493. // Similar to enterFunc, but for when arguments may be accessed before they are initialised,
  2494. // e.g. by an eval() code or from a closure, or from an earlier initialiser code.
  2495. // In this case the arguments remain on stack, first argsToCopy of them are copied to the stash.
  2496. type enterFunc1 struct {
  2497. names map[unistring.String]uint32
  2498. stashSize uint32
  2499. numArgs uint32
  2500. argsToCopy uint32
  2501. extensible bool
  2502. }
  2503. func (e *enterFunc1) exec(vm *vm) {
  2504. sp := vm.sp
  2505. vm.sb = sp - vm.args - 1
  2506. vm.newStash()
  2507. stash := vm.stash
  2508. stash.variable = true
  2509. stash.values = make([]Value, e.stashSize)
  2510. if len(e.names) > 0 {
  2511. if e.extensible {
  2512. m := make(map[unistring.String]uint32, len(e.names))
  2513. for name, idx := range e.names {
  2514. m[name] = idx
  2515. }
  2516. stash.names = m
  2517. } else {
  2518. stash.names = e.names
  2519. }
  2520. }
  2521. offset := vm.args - int(e.argsToCopy)
  2522. if offset > 0 {
  2523. copy(stash.values, vm.stack[sp-vm.args:sp-offset])
  2524. if offset := vm.args - int(e.numArgs); offset > 0 {
  2525. vm.stash.extraArgs = make([]Value, offset)
  2526. copy(stash.extraArgs, vm.stack[sp-offset:])
  2527. }
  2528. } else {
  2529. copy(stash.values, vm.stack[sp-vm.args:sp])
  2530. if int(e.argsToCopy) > vm.args {
  2531. vv := stash.values[vm.args:e.argsToCopy]
  2532. for i := range vv {
  2533. vv[i] = _undefined
  2534. }
  2535. }
  2536. }
  2537. vm.pc++
  2538. }
  2539. // Finalises the initialisers section and starts the function body which has its own
  2540. // scope. When used in conjunction with enterFunc1 adjustStack is set to true which
  2541. // causes the arguments to be removed from the stack.
  2542. type enterFuncBody struct {
  2543. enterBlock
  2544. extensible bool
  2545. adjustStack bool
  2546. }
  2547. func (e *enterFuncBody) exec(vm *vm) {
  2548. if e.stashSize > 0 || e.extensible {
  2549. vm.newStash()
  2550. stash := vm.stash
  2551. stash.variable = true
  2552. stash.values = make([]Value, e.stashSize)
  2553. if len(e.names) > 0 {
  2554. if e.extensible {
  2555. m := make(map[unistring.String]uint32, len(e.names))
  2556. for name, idx := range e.names {
  2557. m[name] = idx
  2558. }
  2559. stash.names = m
  2560. } else {
  2561. stash.names = e.names
  2562. }
  2563. }
  2564. }
  2565. sp := vm.sp
  2566. if e.adjustStack {
  2567. sp -= vm.args
  2568. }
  2569. nsp := sp + int(e.stackSize)
  2570. if e.stackSize > 0 {
  2571. vm.stack.expand(nsp - 1)
  2572. vv := vm.stack[sp:nsp]
  2573. for i := range vv {
  2574. vv[i] = nil
  2575. }
  2576. }
  2577. vm.sp = nsp
  2578. vm.pc++
  2579. }
  2580. type _ret struct{}
  2581. var ret _ret
  2582. func (_ret) exec(vm *vm) {
  2583. // callee -3
  2584. // this -2 <- sb
  2585. // retval -1
  2586. vm.stack[vm.sb-1] = vm.stack[vm.sp-1]
  2587. vm.sp = vm.sb
  2588. vm.popCtx()
  2589. if vm.pc < 0 {
  2590. vm.halt = true
  2591. }
  2592. }
  2593. type enterFuncStashless struct {
  2594. stackSize uint32
  2595. args uint32
  2596. }
  2597. func (e *enterFuncStashless) exec(vm *vm) {
  2598. sp := vm.sp
  2599. vm.sb = sp - vm.args - 1
  2600. d := int(e.args) - vm.args
  2601. if d > 0 {
  2602. ss := sp + int(e.stackSize) + d
  2603. vm.stack.expand(ss)
  2604. vv := vm.stack[sp : sp+d]
  2605. for i := range vv {
  2606. vv[i] = _undefined
  2607. }
  2608. vv = vm.stack[sp+d : ss]
  2609. for i := range vv {
  2610. vv[i] = nil
  2611. }
  2612. vm.args = int(e.args)
  2613. vm.sp = ss
  2614. } else {
  2615. if e.stackSize > 0 {
  2616. ss := sp + int(e.stackSize)
  2617. vm.stack.expand(ss)
  2618. vv := vm.stack[sp:ss]
  2619. for i := range vv {
  2620. vv[i] = nil
  2621. }
  2622. vm.sp = ss
  2623. }
  2624. }
  2625. vm.pc++
  2626. }
  2627. type newFunc struct {
  2628. prg *Program
  2629. name unistring.String
  2630. source string
  2631. length int
  2632. strict bool
  2633. }
  2634. func (n *newFunc) exec(vm *vm) {
  2635. obj := vm.r.newFunc(n.name, n.length, n.strict)
  2636. obj.prg = n.prg
  2637. obj.stash = vm.stash
  2638. obj.src = n.source
  2639. vm.push(obj.val)
  2640. vm.pc++
  2641. }
  2642. type newMethod newFunc
  2643. func (n *newMethod) exec(vm *vm) {
  2644. obj := vm.r.newMethod(n.name, n.length, n.strict)
  2645. obj.prg = n.prg
  2646. obj.stash = vm.stash
  2647. obj.src = n.source
  2648. vm.push(obj.val)
  2649. vm.pc++
  2650. }
  2651. type newArrowFunc struct {
  2652. newFunc
  2653. }
  2654. func (n *newArrowFunc) exec(vm *vm) {
  2655. obj := vm.r.newArrowFunc(n.name, n.length, n.strict)
  2656. obj.prg = n.prg
  2657. obj.stash = vm.stash
  2658. obj.src = n.source
  2659. vm.push(obj.val)
  2660. vm.pc++
  2661. }
  2662. func (vm *vm) alreadyDeclared(name unistring.String) Value {
  2663. return vm.r.newError(vm.r.global.SyntaxError, "Identifier '%s' has already been declared", name)
  2664. }
  2665. func (vm *vm) checkBindVarsGlobal(names []unistring.String) {
  2666. o := vm.r.globalObject.self
  2667. sn := vm.r.global.stash.names
  2668. if o, ok := o.(*baseObject); ok {
  2669. // shortcut
  2670. for _, name := range names {
  2671. if !o.hasOwnPropertyStr(name) && !o.extensible {
  2672. panic(vm.r.NewTypeError("Cannot define global variable '%s', global object is not extensible", name))
  2673. }
  2674. if _, exists := sn[name]; exists {
  2675. panic(vm.alreadyDeclared(name))
  2676. }
  2677. }
  2678. } else {
  2679. for _, name := range names {
  2680. if !o.hasOwnPropertyStr(name) && !o.isExtensible() {
  2681. panic(vm.r.NewTypeError("Cannot define global variable '%s', global object is not extensible", name))
  2682. }
  2683. if _, exists := sn[name]; exists {
  2684. panic(vm.alreadyDeclared(name))
  2685. }
  2686. }
  2687. }
  2688. }
  2689. func (vm *vm) createGlobalVarBindings(names []unistring.String, d bool) {
  2690. globalVarNames := vm.r.global.varNames
  2691. if globalVarNames == nil {
  2692. globalVarNames = make(map[unistring.String]struct{})
  2693. vm.r.global.varNames = globalVarNames
  2694. }
  2695. o := vm.r.globalObject.self
  2696. if o, ok := o.(*baseObject); ok {
  2697. for _, name := range names {
  2698. if !o.hasOwnPropertyStr(name) && o.extensible {
  2699. o._putProp(name, _undefined, true, true, d)
  2700. }
  2701. globalVarNames[name] = struct{}{}
  2702. }
  2703. } else {
  2704. var cf Flag
  2705. if d {
  2706. cf = FLAG_TRUE
  2707. } else {
  2708. cf = FLAG_FALSE
  2709. }
  2710. for _, name := range names {
  2711. if !o.hasOwnPropertyStr(name) && o.isExtensible() {
  2712. o.defineOwnPropertyStr(name, PropertyDescriptor{
  2713. Value: _undefined,
  2714. Writable: FLAG_TRUE,
  2715. Enumerable: FLAG_TRUE,
  2716. Configurable: cf,
  2717. }, true)
  2718. o.setOwnStr(name, _undefined, false)
  2719. }
  2720. globalVarNames[name] = struct{}{}
  2721. }
  2722. }
  2723. }
  2724. func (vm *vm) createGlobalFuncBindings(names []unistring.String, d bool) {
  2725. globalVarNames := vm.r.global.varNames
  2726. if globalVarNames == nil {
  2727. globalVarNames = make(map[unistring.String]struct{})
  2728. vm.r.global.varNames = globalVarNames
  2729. }
  2730. o := vm.r.globalObject.self
  2731. b := vm.sp - len(names)
  2732. var shortcutObj *baseObject
  2733. if o, ok := o.(*baseObject); ok {
  2734. shortcutObj = o
  2735. }
  2736. for i, name := range names {
  2737. var desc PropertyDescriptor
  2738. prop := o.getOwnPropStr(name)
  2739. desc.Value = vm.stack[b+i]
  2740. if shortcutObj != nil && prop == nil && shortcutObj.extensible {
  2741. shortcutObj._putProp(name, desc.Value, true, true, d)
  2742. } else {
  2743. if prop, ok := prop.(*valueProperty); ok && !prop.configurable {
  2744. // no-op
  2745. } else {
  2746. desc.Writable = FLAG_TRUE
  2747. desc.Enumerable = FLAG_TRUE
  2748. if d {
  2749. desc.Configurable = FLAG_TRUE
  2750. } else {
  2751. desc.Configurable = FLAG_FALSE
  2752. }
  2753. }
  2754. if shortcutObj != nil {
  2755. shortcutObj.defineOwnPropertyStr(name, desc, true)
  2756. } else {
  2757. o.defineOwnPropertyStr(name, desc, true)
  2758. o.setOwnStr(name, desc.Value, false) // not a bug, see https://262.ecma-international.org/#sec-createglobalfunctionbinding
  2759. }
  2760. }
  2761. globalVarNames[name] = struct{}{}
  2762. }
  2763. vm.sp = b
  2764. }
  2765. func (vm *vm) checkBindFuncsGlobal(names []unistring.String) {
  2766. o := vm.r.globalObject.self
  2767. sn := vm.r.global.stash.names
  2768. for _, name := range names {
  2769. if _, exists := sn[name]; exists {
  2770. panic(vm.alreadyDeclared(name))
  2771. }
  2772. prop := o.getOwnPropStr(name)
  2773. allowed := true
  2774. switch prop := prop.(type) {
  2775. case nil:
  2776. allowed = o.isExtensible()
  2777. case *valueProperty:
  2778. allowed = prop.configurable || prop.getterFunc == nil && prop.setterFunc == nil && prop.writable && prop.enumerable
  2779. }
  2780. if !allowed {
  2781. panic(vm.r.NewTypeError("Cannot redefine global function '%s'", name))
  2782. }
  2783. }
  2784. }
  2785. func (vm *vm) checkBindLexGlobal(names []unistring.String) {
  2786. o := vm.r.globalObject.self
  2787. s := &vm.r.global.stash
  2788. for _, name := range names {
  2789. if _, exists := vm.r.global.varNames[name]; exists {
  2790. goto fail
  2791. }
  2792. if _, exists := s.names[name]; exists {
  2793. goto fail
  2794. }
  2795. if prop, ok := o.getOwnPropStr(name).(*valueProperty); ok && !prop.configurable {
  2796. goto fail
  2797. }
  2798. continue
  2799. fail:
  2800. panic(vm.alreadyDeclared(name))
  2801. }
  2802. }
  2803. type bindVars struct {
  2804. names []unistring.String
  2805. deletable bool
  2806. }
  2807. func (d *bindVars) exec(vm *vm) {
  2808. var target *stash
  2809. for _, name := range d.names {
  2810. for s := vm.stash; s != nil; s = s.outer {
  2811. if idx, exists := s.names[name]; exists && idx&maskVar == 0 {
  2812. panic(vm.alreadyDeclared(name))
  2813. }
  2814. if s.variable {
  2815. target = s
  2816. break
  2817. }
  2818. }
  2819. }
  2820. if target == nil {
  2821. target = vm.stash
  2822. }
  2823. deletable := d.deletable
  2824. for _, name := range d.names {
  2825. target.createBinding(name, deletable)
  2826. }
  2827. vm.pc++
  2828. }
  2829. type bindGlobal struct {
  2830. vars, funcs, lets, consts []unistring.String
  2831. deletable bool
  2832. }
  2833. func (b *bindGlobal) exec(vm *vm) {
  2834. vm.checkBindFuncsGlobal(b.funcs)
  2835. vm.checkBindLexGlobal(b.lets)
  2836. vm.checkBindLexGlobal(b.consts)
  2837. vm.checkBindVarsGlobal(b.vars)
  2838. s := &vm.r.global.stash
  2839. for _, name := range b.lets {
  2840. s.createLexBinding(name, false)
  2841. }
  2842. for _, name := range b.consts {
  2843. s.createLexBinding(name, true)
  2844. }
  2845. vm.createGlobalFuncBindings(b.funcs, b.deletable)
  2846. vm.createGlobalVarBindings(b.vars, b.deletable)
  2847. vm.pc++
  2848. }
  2849. type jne int32
  2850. func (j jne) exec(vm *vm) {
  2851. vm.sp--
  2852. if !vm.stack[vm.sp].ToBoolean() {
  2853. vm.pc += int(j)
  2854. } else {
  2855. vm.pc++
  2856. }
  2857. }
  2858. type jeq int32
  2859. func (j jeq) exec(vm *vm) {
  2860. vm.sp--
  2861. if vm.stack[vm.sp].ToBoolean() {
  2862. vm.pc += int(j)
  2863. } else {
  2864. vm.pc++
  2865. }
  2866. }
  2867. type jeq1 int32
  2868. func (j jeq1) exec(vm *vm) {
  2869. if vm.stack[vm.sp-1].ToBoolean() {
  2870. vm.pc += int(j)
  2871. } else {
  2872. vm.pc++
  2873. }
  2874. }
  2875. type jneq1 int32
  2876. func (j jneq1) exec(vm *vm) {
  2877. if !vm.stack[vm.sp-1].ToBoolean() {
  2878. vm.pc += int(j)
  2879. } else {
  2880. vm.pc++
  2881. }
  2882. }
  2883. type jdef int32
  2884. func (j jdef) exec(vm *vm) {
  2885. if vm.stack[vm.sp-1] != _undefined {
  2886. vm.pc += int(j)
  2887. } else {
  2888. vm.sp--
  2889. vm.pc++
  2890. }
  2891. }
  2892. type jdefP int32
  2893. func (j jdefP) exec(vm *vm) {
  2894. if vm.stack[vm.sp-1] != _undefined {
  2895. vm.pc += int(j)
  2896. } else {
  2897. vm.pc++
  2898. }
  2899. vm.sp--
  2900. }
  2901. type _not struct{}
  2902. var not _not
  2903. func (_not) exec(vm *vm) {
  2904. if vm.stack[vm.sp-1].ToBoolean() {
  2905. vm.stack[vm.sp-1] = valueFalse
  2906. } else {
  2907. vm.stack[vm.sp-1] = valueTrue
  2908. }
  2909. vm.pc++
  2910. }
  2911. func toPrimitiveNumber(v Value) Value {
  2912. if o, ok := v.(*Object); ok {
  2913. return o.toPrimitiveNumber()
  2914. }
  2915. return v
  2916. }
  2917. func toPrimitive(v Value) Value {
  2918. if o, ok := v.(*Object); ok {
  2919. return o.toPrimitive()
  2920. }
  2921. return v
  2922. }
  2923. func cmp(px, py Value) Value {
  2924. var ret bool
  2925. var nx, ny float64
  2926. if xs, ok := px.(valueString); ok {
  2927. if ys, ok := py.(valueString); ok {
  2928. ret = xs.compareTo(ys) < 0
  2929. goto end
  2930. }
  2931. }
  2932. if xi, ok := px.(valueInt); ok {
  2933. if yi, ok := py.(valueInt); ok {
  2934. ret = xi < yi
  2935. goto end
  2936. }
  2937. }
  2938. nx = px.ToFloat()
  2939. ny = py.ToFloat()
  2940. if math.IsNaN(nx) || math.IsNaN(ny) {
  2941. return _undefined
  2942. }
  2943. ret = nx < ny
  2944. end:
  2945. if ret {
  2946. return valueTrue
  2947. }
  2948. return valueFalse
  2949. }
  2950. type _op_lt struct{}
  2951. var op_lt _op_lt
  2952. func (_op_lt) exec(vm *vm) {
  2953. left := toPrimitiveNumber(vm.stack[vm.sp-2])
  2954. right := toPrimitiveNumber(vm.stack[vm.sp-1])
  2955. r := cmp(left, right)
  2956. if r == _undefined {
  2957. vm.stack[vm.sp-2] = valueFalse
  2958. } else {
  2959. vm.stack[vm.sp-2] = r
  2960. }
  2961. vm.sp--
  2962. vm.pc++
  2963. }
  2964. type _op_lte struct{}
  2965. var op_lte _op_lte
  2966. func (_op_lte) exec(vm *vm) {
  2967. left := toPrimitiveNumber(vm.stack[vm.sp-2])
  2968. right := toPrimitiveNumber(vm.stack[vm.sp-1])
  2969. r := cmp(right, left)
  2970. if r == _undefined || r == valueTrue {
  2971. vm.stack[vm.sp-2] = valueFalse
  2972. } else {
  2973. vm.stack[vm.sp-2] = valueTrue
  2974. }
  2975. vm.sp--
  2976. vm.pc++
  2977. }
  2978. type _op_gt struct{}
  2979. var op_gt _op_gt
  2980. func (_op_gt) exec(vm *vm) {
  2981. left := toPrimitiveNumber(vm.stack[vm.sp-2])
  2982. right := toPrimitiveNumber(vm.stack[vm.sp-1])
  2983. r := cmp(right, left)
  2984. if r == _undefined {
  2985. vm.stack[vm.sp-2] = valueFalse
  2986. } else {
  2987. vm.stack[vm.sp-2] = r
  2988. }
  2989. vm.sp--
  2990. vm.pc++
  2991. }
  2992. type _op_gte struct{}
  2993. var op_gte _op_gte
  2994. func (_op_gte) exec(vm *vm) {
  2995. left := toPrimitiveNumber(vm.stack[vm.sp-2])
  2996. right := toPrimitiveNumber(vm.stack[vm.sp-1])
  2997. r := cmp(left, right)
  2998. if r == _undefined || r == valueTrue {
  2999. vm.stack[vm.sp-2] = valueFalse
  3000. } else {
  3001. vm.stack[vm.sp-2] = valueTrue
  3002. }
  3003. vm.sp--
  3004. vm.pc++
  3005. }
  3006. type _op_eq struct{}
  3007. var op_eq _op_eq
  3008. func (_op_eq) exec(vm *vm) {
  3009. if vm.stack[vm.sp-2].Equals(vm.stack[vm.sp-1]) {
  3010. vm.stack[vm.sp-2] = valueTrue
  3011. } else {
  3012. vm.stack[vm.sp-2] = valueFalse
  3013. }
  3014. vm.sp--
  3015. vm.pc++
  3016. }
  3017. type _op_neq struct{}
  3018. var op_neq _op_neq
  3019. func (_op_neq) exec(vm *vm) {
  3020. if vm.stack[vm.sp-2].Equals(vm.stack[vm.sp-1]) {
  3021. vm.stack[vm.sp-2] = valueFalse
  3022. } else {
  3023. vm.stack[vm.sp-2] = valueTrue
  3024. }
  3025. vm.sp--
  3026. vm.pc++
  3027. }
  3028. type _op_strict_eq struct{}
  3029. var op_strict_eq _op_strict_eq
  3030. func (_op_strict_eq) exec(vm *vm) {
  3031. if vm.stack[vm.sp-2].StrictEquals(vm.stack[vm.sp-1]) {
  3032. vm.stack[vm.sp-2] = valueTrue
  3033. } else {
  3034. vm.stack[vm.sp-2] = valueFalse
  3035. }
  3036. vm.sp--
  3037. vm.pc++
  3038. }
  3039. type _op_strict_neq struct{}
  3040. var op_strict_neq _op_strict_neq
  3041. func (_op_strict_neq) exec(vm *vm) {
  3042. if vm.stack[vm.sp-2].StrictEquals(vm.stack[vm.sp-1]) {
  3043. vm.stack[vm.sp-2] = valueFalse
  3044. } else {
  3045. vm.stack[vm.sp-2] = valueTrue
  3046. }
  3047. vm.sp--
  3048. vm.pc++
  3049. }
  3050. type _op_instanceof struct{}
  3051. var op_instanceof _op_instanceof
  3052. func (_op_instanceof) exec(vm *vm) {
  3053. left := vm.stack[vm.sp-2]
  3054. right := vm.r.toObject(vm.stack[vm.sp-1])
  3055. if instanceOfOperator(left, right) {
  3056. vm.stack[vm.sp-2] = valueTrue
  3057. } else {
  3058. vm.stack[vm.sp-2] = valueFalse
  3059. }
  3060. vm.sp--
  3061. vm.pc++
  3062. }
  3063. type _op_in struct{}
  3064. var op_in _op_in
  3065. func (_op_in) exec(vm *vm) {
  3066. left := vm.stack[vm.sp-2]
  3067. right := vm.r.toObject(vm.stack[vm.sp-1])
  3068. if right.hasProperty(left) {
  3069. vm.stack[vm.sp-2] = valueTrue
  3070. } else {
  3071. vm.stack[vm.sp-2] = valueFalse
  3072. }
  3073. vm.sp--
  3074. vm.pc++
  3075. }
  3076. type try struct {
  3077. catchOffset int32
  3078. finallyOffset int32
  3079. }
  3080. func (t try) exec(vm *vm) {
  3081. o := vm.pc
  3082. vm.pc++
  3083. ex := vm.runTry()
  3084. if ex != nil && t.catchOffset > 0 {
  3085. // run the catch block (in try)
  3086. vm.pc = o + int(t.catchOffset)
  3087. // TODO: if ex.val is an Error, set the stack property
  3088. vm.push(ex.val)
  3089. ex = vm.runTry()
  3090. }
  3091. if t.finallyOffset > 0 {
  3092. pc := vm.pc
  3093. // Run finally
  3094. vm.pc = o + int(t.finallyOffset)
  3095. vm.run()
  3096. if vm.prg.code[vm.pc] == retFinally {
  3097. vm.pc = pc
  3098. } else {
  3099. // break or continue out of finally, dropping exception
  3100. ex = nil
  3101. }
  3102. }
  3103. vm.halt = false
  3104. if ex != nil {
  3105. vm.pc = -1 // to prevent the current position from being captured in the stacktrace
  3106. panic(ex)
  3107. }
  3108. }
  3109. type _retFinally struct{}
  3110. var retFinally _retFinally
  3111. func (_retFinally) exec(vm *vm) {
  3112. vm.pc++
  3113. }
  3114. type _throw struct{}
  3115. var throw _throw
  3116. func (_throw) exec(vm *vm) {
  3117. panic(vm.stack[vm.sp-1])
  3118. }
  3119. type _newVariadic struct{}
  3120. var newVariadic _newVariadic
  3121. func (_newVariadic) exec(vm *vm) {
  3122. _new(vm.countVariadicArgs() - 1).exec(vm)
  3123. }
  3124. type _new uint32
  3125. func (n _new) exec(vm *vm) {
  3126. sp := vm.sp - int(n)
  3127. obj := vm.stack[sp-1]
  3128. ctor := vm.r.toConstructor(obj)
  3129. vm.stack[sp-1] = ctor(vm.stack[sp:vm.sp], nil)
  3130. vm.sp = sp
  3131. vm.pc++
  3132. }
  3133. type _loadNewTarget struct{}
  3134. var loadNewTarget _loadNewTarget
  3135. func (_loadNewTarget) exec(vm *vm) {
  3136. if t := vm.newTarget; t != nil {
  3137. vm.push(t)
  3138. } else {
  3139. vm.push(_undefined)
  3140. }
  3141. vm.pc++
  3142. }
  3143. type _typeof struct{}
  3144. var typeof _typeof
  3145. func (_typeof) exec(vm *vm) {
  3146. var r Value
  3147. switch v := vm.stack[vm.sp-1].(type) {
  3148. case valueUndefined, valueUnresolved:
  3149. r = stringUndefined
  3150. case valueNull:
  3151. r = stringObjectC
  3152. case *Object:
  3153. repeat:
  3154. switch s := v.self.(type) {
  3155. case *methodFuncObject, *funcObject, *nativeFuncObject, *boundFuncObject, *arrowFuncObject:
  3156. r = stringFunction
  3157. case *proxyObject:
  3158. if s.call == nil {
  3159. r = stringObjectC
  3160. } else {
  3161. r = stringFunction
  3162. }
  3163. case *lazyObject:
  3164. v.self = s.create(v)
  3165. goto repeat
  3166. default:
  3167. r = stringObjectC
  3168. }
  3169. case valueBool:
  3170. r = stringBoolean
  3171. case valueString:
  3172. r = stringString
  3173. case valueInt, valueFloat:
  3174. r = stringNumber
  3175. case *Symbol:
  3176. r = stringSymbol
  3177. default:
  3178. panic(fmt.Errorf("Unknown type: %T", v))
  3179. }
  3180. vm.stack[vm.sp-1] = r
  3181. vm.pc++
  3182. }
  3183. type createArgsMapped uint32
  3184. func (formalArgs createArgsMapped) exec(vm *vm) {
  3185. v := &Object{runtime: vm.r}
  3186. args := &argumentsObject{}
  3187. args.extensible = true
  3188. args.prototype = vm.r.global.ObjectPrototype
  3189. args.class = "Arguments"
  3190. v.self = args
  3191. args.val = v
  3192. args.length = vm.args
  3193. args.init()
  3194. i := 0
  3195. c := int(formalArgs)
  3196. if vm.args < c {
  3197. c = vm.args
  3198. }
  3199. for ; i < c; i++ {
  3200. args._put(unistring.String(strconv.Itoa(i)), &mappedProperty{
  3201. valueProperty: valueProperty{
  3202. writable: true,
  3203. configurable: true,
  3204. enumerable: true,
  3205. },
  3206. v: &vm.stash.values[i],
  3207. })
  3208. }
  3209. for _, v := range vm.stash.extraArgs {
  3210. args._put(unistring.String(strconv.Itoa(i)), v)
  3211. i++
  3212. }
  3213. args._putProp("callee", vm.stack[vm.sb-1], true, false, true)
  3214. args._putSym(SymIterator, valueProp(vm.r.global.arrayValues, true, false, true))
  3215. vm.push(v)
  3216. vm.pc++
  3217. }
  3218. type createArgsUnmapped uint32
  3219. func (formalArgs createArgsUnmapped) exec(vm *vm) {
  3220. args := vm.r.newBaseObject(vm.r.global.ObjectPrototype, "Arguments")
  3221. i := 0
  3222. c := int(formalArgs)
  3223. if vm.args < c {
  3224. c = vm.args
  3225. }
  3226. for _, v := range vm.stash.values[:c] {
  3227. args._put(unistring.String(strconv.Itoa(i)), v)
  3228. i++
  3229. }
  3230. for _, v := range vm.stash.extraArgs {
  3231. args._put(unistring.String(strconv.Itoa(i)), v)
  3232. i++
  3233. }
  3234. args._putProp("length", intToValue(int64(vm.args)), true, false, true)
  3235. args._put("callee", vm.r.global.throwerProperty)
  3236. args._putSym(SymIterator, valueProp(vm.r.global.arrayValues, true, false, true))
  3237. vm.push(args.val)
  3238. vm.pc++
  3239. }
  3240. type _enterWith struct{}
  3241. var enterWith _enterWith
  3242. func (_enterWith) exec(vm *vm) {
  3243. vm.newStash()
  3244. vm.stash.obj = vm.stack[vm.sp-1].ToObject(vm.r)
  3245. vm.sp--
  3246. vm.pc++
  3247. }
  3248. type _leaveWith struct{}
  3249. var leaveWith _leaveWith
  3250. func (_leaveWith) exec(vm *vm) {
  3251. vm.stash = vm.stash.outer
  3252. vm.pc++
  3253. }
  3254. func emptyIter() (propIterItem, iterNextFunc) {
  3255. return propIterItem{}, nil
  3256. }
  3257. type _enumerate struct{}
  3258. var enumerate _enumerate
  3259. func (_enumerate) exec(vm *vm) {
  3260. v := vm.stack[vm.sp-1]
  3261. if v == _undefined || v == _null {
  3262. vm.iterStack = append(vm.iterStack, iterStackItem{f: emptyIter})
  3263. } else {
  3264. vm.iterStack = append(vm.iterStack, iterStackItem{f: enumerateRecursive(v.ToObject(vm.r))})
  3265. }
  3266. vm.sp--
  3267. vm.pc++
  3268. }
  3269. type enumNext int32
  3270. func (jmp enumNext) exec(vm *vm) {
  3271. l := len(vm.iterStack) - 1
  3272. item, n := vm.iterStack[l].f()
  3273. if n != nil {
  3274. vm.iterStack[l].val = item.name
  3275. vm.iterStack[l].f = n
  3276. vm.pc++
  3277. } else {
  3278. vm.pc += int(jmp)
  3279. }
  3280. }
  3281. type _enumGet struct{}
  3282. var enumGet _enumGet
  3283. func (_enumGet) exec(vm *vm) {
  3284. l := len(vm.iterStack) - 1
  3285. vm.push(vm.iterStack[l].val)
  3286. vm.pc++
  3287. }
  3288. type _enumPop struct{}
  3289. var enumPop _enumPop
  3290. func (_enumPop) exec(vm *vm) {
  3291. l := len(vm.iterStack) - 1
  3292. vm.iterStack[l] = iterStackItem{}
  3293. vm.iterStack = vm.iterStack[:l]
  3294. vm.pc++
  3295. }
  3296. type _enumPopClose struct{}
  3297. var enumPopClose _enumPopClose
  3298. func (_enumPopClose) exec(vm *vm) {
  3299. l := len(vm.iterStack) - 1
  3300. item := vm.iterStack[l]
  3301. vm.iterStack[l] = iterStackItem{}
  3302. vm.iterStack = vm.iterStack[:l]
  3303. if iter := item.iter; iter != nil {
  3304. iter.returnIter()
  3305. }
  3306. vm.pc++
  3307. }
  3308. type _iterateP struct{}
  3309. var iterateP _iterateP
  3310. func (_iterateP) exec(vm *vm) {
  3311. iter := vm.r.getIterator(vm.stack[vm.sp-1], nil)
  3312. vm.iterStack = append(vm.iterStack, iterStackItem{iter: iter})
  3313. vm.sp--
  3314. vm.pc++
  3315. }
  3316. type _iterate struct{}
  3317. var iterate _iterate
  3318. func (_iterate) exec(vm *vm) {
  3319. iter := vm.r.getIterator(vm.stack[vm.sp-1], nil)
  3320. vm.iterStack = append(vm.iterStack, iterStackItem{iter: iter})
  3321. vm.pc++
  3322. }
  3323. type iterNext int32
  3324. func (jmp iterNext) exec(vm *vm) {
  3325. l := len(vm.iterStack) - 1
  3326. iter := vm.iterStack[l].iter
  3327. value, ex := iter.step()
  3328. if ex == nil {
  3329. if value == nil {
  3330. vm.pc += int(jmp)
  3331. } else {
  3332. vm.iterStack[l].val = value
  3333. vm.pc++
  3334. }
  3335. } else {
  3336. l := len(vm.iterStack) - 1
  3337. vm.iterStack[l] = iterStackItem{}
  3338. vm.iterStack = vm.iterStack[:l]
  3339. panic(ex.val)
  3340. }
  3341. }
  3342. type iterGetNextOrUndef struct{}
  3343. func (iterGetNextOrUndef) exec(vm *vm) {
  3344. l := len(vm.iterStack) - 1
  3345. iter := vm.iterStack[l].iter
  3346. var value Value
  3347. if iter.iterator != nil {
  3348. var ex *Exception
  3349. value, ex = iter.step()
  3350. if ex != nil {
  3351. l := len(vm.iterStack) - 1
  3352. vm.iterStack[l] = iterStackItem{}
  3353. vm.iterStack = vm.iterStack[:l]
  3354. panic(ex.val)
  3355. }
  3356. }
  3357. vm.push(nilSafe(value))
  3358. vm.pc++
  3359. }
  3360. type copyStash struct{}
  3361. func (copyStash) exec(vm *vm) {
  3362. oldStash := vm.stash
  3363. newStash := &stash{
  3364. outer: oldStash.outer,
  3365. }
  3366. vm.stashAllocs++
  3367. newStash.values = append([]Value(nil), oldStash.values...)
  3368. newStash.names = oldStash.names
  3369. vm.stash = newStash
  3370. vm.pc++
  3371. }
  3372. type _throwAssignToConst struct{}
  3373. var throwAssignToConst _throwAssignToConst
  3374. func (_throwAssignToConst) exec(vm *vm) {
  3375. panic(errAssignToConst)
  3376. }
  3377. func (r *Runtime) copyDataProperties(target, source Value) {
  3378. targetObj := r.toObject(target)
  3379. if source == _null || source == _undefined {
  3380. return
  3381. }
  3382. sourceObj := source.ToObject(r)
  3383. for item, next := iterateEnumerableProperties(sourceObj)(); next != nil; item, next = next() {
  3384. createDataPropertyOrThrow(targetObj, item.name, item.value)
  3385. }
  3386. }
  3387. type _copySpread struct{}
  3388. var copySpread _copySpread
  3389. func (_copySpread) exec(vm *vm) {
  3390. vm.r.copyDataProperties(vm.stack[vm.sp-2], vm.stack[vm.sp-1])
  3391. vm.sp--
  3392. vm.pc++
  3393. }
  3394. type _copyRest struct{}
  3395. var copyRest _copyRest
  3396. func (_copyRest) exec(vm *vm) {
  3397. vm.push(vm.r.NewObject())
  3398. vm.r.copyDataProperties(vm.stack[vm.sp-1], vm.stack[vm.sp-2])
  3399. vm.pc++
  3400. }
  3401. type _createDestructSrc struct{}
  3402. var createDestructSrc _createDestructSrc
  3403. func (_createDestructSrc) exec(vm *vm) {
  3404. v := vm.stack[vm.sp-1]
  3405. vm.r.checkObjectCoercible(v)
  3406. vm.push(vm.r.newDestructKeyedSource(v))
  3407. vm.pc++
  3408. }
  3409. type _checkObjectCoercible struct{}
  3410. var checkObjectCoercible _checkObjectCoercible
  3411. func (_checkObjectCoercible) exec(vm *vm) {
  3412. vm.r.checkObjectCoercible(vm.stack[vm.sp-1])
  3413. vm.pc++
  3414. }
  3415. type createArgsRestStack int
  3416. func (n createArgsRestStack) exec(vm *vm) {
  3417. var values []Value
  3418. delta := vm.args - int(n)
  3419. if delta > 0 {
  3420. values = make([]Value, delta)
  3421. copy(values, vm.stack[vm.sb+int(n)+1:])
  3422. }
  3423. vm.push(vm.r.newArrayValues(values))
  3424. vm.pc++
  3425. }
  3426. type _createArgsRestStash struct{}
  3427. var createArgsRestStash _createArgsRestStash
  3428. func (_createArgsRestStash) exec(vm *vm) {
  3429. vm.push(vm.r.newArrayValues(vm.stash.extraArgs))
  3430. vm.stash.extraArgs = nil
  3431. vm.pc++
  3432. }
  3433. type concatStrings int
  3434. func (n concatStrings) exec(vm *vm) {
  3435. strs := vm.stack[vm.sp-int(n) : vm.sp]
  3436. length := 0
  3437. allAscii := true
  3438. for _, s := range strs {
  3439. if allAscii {
  3440. if _, ok := s.(unicodeString); ok {
  3441. allAscii = false
  3442. }
  3443. }
  3444. length += s.(valueString).length()
  3445. }
  3446. vm.sp -= int(n) - 1
  3447. if allAscii {
  3448. var buf strings.Builder
  3449. buf.Grow(length)
  3450. for _, s := range strs {
  3451. buf.WriteString(string(s.(asciiString)))
  3452. }
  3453. vm.stack[vm.sp-1] = asciiString(buf.String())
  3454. } else {
  3455. var buf unicodeStringBuilder
  3456. buf.Grow(length)
  3457. for _, s := range strs {
  3458. buf.WriteString(s.(valueString))
  3459. }
  3460. vm.stack[vm.sp-1] = buf.String()
  3461. }
  3462. vm.pc++
  3463. }
  3464. type getTaggedTmplObject struct {
  3465. raw, cooked []Value
  3466. }
  3467. // As tagged template objects are not cached (because it's hard to ensure the cache is cleaned without using
  3468. // finalizers) this wrapper is needed to override the equality method so that two objects for the same template
  3469. // literal appeared be equal from the code's point of view.
  3470. type taggedTemplateArray struct {
  3471. *arrayObject
  3472. idPtr *[]Value
  3473. }
  3474. func (a *taggedTemplateArray) equal(other objectImpl) bool {
  3475. if o, ok := other.(*taggedTemplateArray); ok {
  3476. return a.idPtr == o.idPtr
  3477. }
  3478. return false
  3479. }
  3480. func (c *getTaggedTmplObject) exec(vm *vm) {
  3481. cooked := vm.r.newArrayObject()
  3482. setArrayValues(cooked, c.cooked)
  3483. cooked.lengthProp.writable = false
  3484. raw := vm.r.newArrayObject()
  3485. setArrayValues(raw, c.raw)
  3486. raw.lengthProp.writable = false
  3487. raw.preventExtensions(true)
  3488. raw.val.self = &taggedTemplateArray{
  3489. arrayObject: raw,
  3490. idPtr: &c.raw,
  3491. }
  3492. cooked._putProp("raw", raw.val, false, false, false)
  3493. cooked.preventExtensions(true)
  3494. cooked.val.self = &taggedTemplateArray{
  3495. arrayObject: cooked,
  3496. idPtr: &c.cooked,
  3497. }
  3498. vm.push(cooked.val)
  3499. vm.pc++
  3500. }