vm.go 71 KB

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