vm.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433
  1. package goja
  2. import (
  3. "fmt"
  4. "math"
  5. "runtime"
  6. "strconv"
  7. "sync"
  8. "sync/atomic"
  9. )
  10. const (
  11. maxInt = 1 << 53
  12. )
  13. type valueStack []Value
  14. type stash struct {
  15. values valueStack
  16. extraArgs valueStack
  17. names map[string]uint32
  18. obj objectImpl
  19. outer *stash
  20. }
  21. type context struct {
  22. prg *Program
  23. funcName string
  24. stash *stash
  25. newTarget Value
  26. pc, sb int
  27. args int
  28. }
  29. type iterStackItem struct {
  30. val Value
  31. f iterNextFunc
  32. }
  33. type ref interface {
  34. get() Value
  35. set(Value)
  36. refname() string
  37. }
  38. type stashRef struct {
  39. v *Value
  40. n string
  41. }
  42. func (r stashRef) get() Value {
  43. return *r.v
  44. }
  45. func (r *stashRef) set(v Value) {
  46. *r.v = v
  47. }
  48. func (r *stashRef) refname() string {
  49. return r.n
  50. }
  51. type objRef struct {
  52. base objectImpl
  53. name string
  54. strict bool
  55. }
  56. func (r *objRef) get() Value {
  57. return r.base.getStr(r.name, nil)
  58. }
  59. func (r *objRef) set(v Value) {
  60. r.base.setOwnStr(r.name, v, r.strict)
  61. }
  62. func (r *objRef) refname() string {
  63. return r.name
  64. }
  65. type unresolvedRef struct {
  66. runtime *Runtime
  67. name string
  68. }
  69. func (r *unresolvedRef) get() Value {
  70. r.runtime.throwReferenceError(r.name)
  71. panic("Unreachable")
  72. }
  73. func (r *unresolvedRef) set(Value) {
  74. r.get()
  75. }
  76. func (r *unresolvedRef) refname() string {
  77. return r.name
  78. }
  79. type vm struct {
  80. r *Runtime
  81. prg *Program
  82. funcName string
  83. pc int
  84. stack valueStack
  85. sp, sb, args int
  86. stash *stash
  87. callStack []context
  88. iterStack []iterStackItem
  89. refStack []ref
  90. newTarget Value
  91. stashAllocs int
  92. halt bool
  93. interrupted uint32
  94. interruptVal interface{}
  95. interruptLock sync.Mutex
  96. }
  97. type instruction interface {
  98. exec(*vm)
  99. }
  100. func intToValue(i int64) Value {
  101. if i >= -maxInt && i <= maxInt {
  102. if i >= -128 && i <= 127 {
  103. return intCache[i+128]
  104. }
  105. return valueInt(i)
  106. }
  107. return valueFloat(float64(i))
  108. }
  109. func floatToInt(f float64) (result int64, ok bool) {
  110. if (f != 0 || !math.Signbit(f)) && !math.IsInf(f, 0) && f == math.Trunc(f) && f >= -maxInt && f <= maxInt {
  111. return int64(f), true
  112. }
  113. return 0, false
  114. }
  115. func floatToValue(f float64) (result Value) {
  116. if i, ok := floatToInt(f); ok {
  117. return intToValue(i)
  118. }
  119. switch {
  120. case f == 0:
  121. return _negativeZero
  122. case math.IsNaN(f):
  123. return _NaN
  124. case math.IsInf(f, 1):
  125. return _positiveInf
  126. case math.IsInf(f, -1):
  127. return _negativeInf
  128. }
  129. return valueFloat(f)
  130. }
  131. func toInt64(v Value) (int64, bool) {
  132. num := v.ToNumber()
  133. if i, ok := num.(valueInt); ok {
  134. return int64(i), true
  135. }
  136. if f, ok := num.(valueFloat); ok {
  137. if i, ok := floatToInt(float64(f)); ok {
  138. return i, true
  139. }
  140. }
  141. return 0, false
  142. }
  143. func toIntIgnoreNegZero(v Value) (int64, bool) {
  144. num := v.ToNumber()
  145. if i, ok := num.(valueInt); ok {
  146. return int64(i), true
  147. }
  148. if f, ok := num.(valueFloat); ok {
  149. if v == _negativeZero {
  150. return 0, true
  151. }
  152. if i, ok := floatToInt(float64(f)); ok {
  153. return i, true
  154. }
  155. }
  156. return 0, false
  157. }
  158. func (s *valueStack) expand(idx int) {
  159. if idx < len(*s) {
  160. return
  161. }
  162. if idx < cap(*s) {
  163. *s = (*s)[:idx+1]
  164. } else {
  165. n := make([]Value, idx+1, (idx+1)<<1)
  166. copy(n, *s)
  167. *s = n
  168. }
  169. }
  170. func (s *stash) put(name string, v Value) bool {
  171. if s.obj != nil {
  172. if found := s.obj.getStr(name, nil); found != nil {
  173. s.obj.setOwnStr(name, v, false)
  174. return true
  175. }
  176. return false
  177. } else {
  178. if idx, found := s.names[name]; found {
  179. s.values.expand(int(idx))
  180. s.values[idx] = v
  181. return true
  182. }
  183. return false
  184. }
  185. }
  186. func (s *stash) putByIdx(idx uint32, v Value) {
  187. if s.obj != nil {
  188. panic("Attempt to put by idx into an object scope")
  189. }
  190. s.values.expand(int(idx))
  191. s.values[idx] = v
  192. }
  193. func (s *stash) getByIdx(idx uint32) Value {
  194. if int(idx) < len(s.values) {
  195. return s.values[idx]
  196. }
  197. return _undefined
  198. }
  199. func (s *stash) getByName(name string, _ *vm) (v Value, exists bool) {
  200. if s.obj != nil {
  201. if s.obj.hasPropertyStr(name) {
  202. return nilSafe(s.obj.getStr(name, nil)), true
  203. }
  204. return nil, false
  205. }
  206. if idx, exists := s.names[name]; exists {
  207. return s.values[idx], true
  208. }
  209. return nil, false
  210. //return valueUnresolved{r: vm.r, ref: name}, false
  211. }
  212. func (s *stash) createBinding(name string) {
  213. if s.names == nil {
  214. s.names = make(map[string]uint32)
  215. }
  216. if _, exists := s.names[name]; !exists {
  217. s.names[name] = uint32(len(s.names))
  218. s.values = append(s.values, _undefined)
  219. }
  220. }
  221. func (s *stash) deleteBinding(name string) bool {
  222. if s.obj != nil {
  223. return s.obj.deleteStr(name, false)
  224. }
  225. if idx, found := s.names[name]; found {
  226. s.values[idx] = nil
  227. delete(s.names, name)
  228. return true
  229. }
  230. return false
  231. }
  232. func (vm *vm) newStash() {
  233. vm.stash = &stash{
  234. outer: vm.stash,
  235. }
  236. vm.stashAllocs++
  237. }
  238. func (vm *vm) init() {
  239. }
  240. func (vm *vm) run() {
  241. vm.halt = false
  242. interrupted := false
  243. ticks := 0
  244. for !vm.halt {
  245. if interrupted = atomic.LoadUint32(&vm.interrupted) != 0; interrupted {
  246. break
  247. }
  248. vm.prg.code[vm.pc].exec(vm)
  249. ticks++
  250. if ticks > 10000 {
  251. runtime.Gosched()
  252. ticks = 0
  253. }
  254. }
  255. if interrupted {
  256. vm.interruptLock.Lock()
  257. v := &InterruptedError{
  258. iface: vm.interruptVal,
  259. }
  260. atomic.StoreUint32(&vm.interrupted, 0)
  261. vm.interruptVal = nil
  262. vm.interruptLock.Unlock()
  263. panic(v)
  264. }
  265. }
  266. func (vm *vm) Interrupt(v interface{}) {
  267. vm.interruptLock.Lock()
  268. vm.interruptVal = v
  269. atomic.StoreUint32(&vm.interrupted, 1)
  270. vm.interruptLock.Unlock()
  271. }
  272. func (vm *vm) ClearInterrupt() {
  273. atomic.StoreUint32(&vm.interrupted, 0)
  274. }
  275. func (vm *vm) captureStack(stack []StackFrame, ctxOffset int) []StackFrame {
  276. // Unroll the context stack
  277. stack = append(stack, StackFrame{prg: vm.prg, pc: vm.pc, funcName: vm.funcName})
  278. for i := len(vm.callStack) - 1; i > ctxOffset-1; i-- {
  279. if vm.callStack[i].pc != -1 {
  280. stack = append(stack, StackFrame{prg: vm.callStack[i].prg, pc: vm.callStack[i].pc - 1, funcName: vm.callStack[i].funcName})
  281. }
  282. }
  283. return stack
  284. }
  285. func (vm *vm) try(f func()) (ex *Exception) {
  286. var ctx context
  287. vm.saveCtx(&ctx)
  288. ctxOffset := len(vm.callStack)
  289. sp := vm.sp
  290. iterLen := len(vm.iterStack)
  291. refLen := len(vm.refStack)
  292. defer func() {
  293. if x := recover(); x != nil {
  294. defer func() {
  295. vm.callStack = vm.callStack[:ctxOffset]
  296. vm.restoreCtx(&ctx)
  297. vm.sp = sp
  298. // Restore other stacks
  299. iterTail := vm.iterStack[iterLen:]
  300. for i := range iterTail {
  301. iterTail[i] = iterStackItem{}
  302. }
  303. vm.iterStack = vm.iterStack[:iterLen]
  304. refTail := vm.refStack[refLen:]
  305. for i := range refTail {
  306. refTail[i] = nil
  307. }
  308. vm.refStack = vm.refStack[:refLen]
  309. }()
  310. switch x1 := x.(type) {
  311. case Value:
  312. ex = &Exception{
  313. val: x1,
  314. }
  315. case *InterruptedError:
  316. x1.stack = vm.captureStack(x1.stack, ctxOffset)
  317. panic(x1)
  318. case *Exception:
  319. ex = x1
  320. case typeError:
  321. ex = &Exception{
  322. val: vm.r.NewTypeError(string(x1)),
  323. }
  324. default:
  325. /*
  326. if vm.prg != nil {
  327. vm.prg.dumpCode(log.Printf)
  328. }
  329. log.Print("Stack: ", string(debug.Stack()))
  330. panic(fmt.Errorf("Panic at %d: %v", vm.pc, x))
  331. */
  332. panic(x)
  333. }
  334. ex.stack = vm.captureStack(ex.stack, ctxOffset)
  335. }
  336. }()
  337. f()
  338. return
  339. }
  340. func (vm *vm) runTry() (ex *Exception) {
  341. return vm.try(vm.run)
  342. }
  343. func (vm *vm) push(v Value) {
  344. vm.stack.expand(vm.sp)
  345. vm.stack[vm.sp] = v
  346. vm.sp++
  347. }
  348. func (vm *vm) pop() Value {
  349. vm.sp--
  350. return vm.stack[vm.sp]
  351. }
  352. func (vm *vm) peek() Value {
  353. return vm.stack[vm.sp-1]
  354. }
  355. func (vm *vm) saveCtx(ctx *context) {
  356. ctx.prg = vm.prg
  357. if vm.funcName != "" {
  358. ctx.funcName = vm.funcName
  359. } else if ctx.prg != nil && ctx.prg.funcName != "" {
  360. ctx.funcName = ctx.prg.funcName
  361. }
  362. ctx.stash = vm.stash
  363. ctx.newTarget = vm.newTarget
  364. ctx.pc = vm.pc
  365. ctx.sb = vm.sb
  366. ctx.args = vm.args
  367. }
  368. func (vm *vm) pushCtx() {
  369. /*
  370. vm.ctxStack = append(vm.ctxStack, context{
  371. prg: vm.prg,
  372. stash: vm.stash,
  373. pc: vm.pc,
  374. sb: vm.sb,
  375. args: vm.args,
  376. })*/
  377. vm.callStack = append(vm.callStack, context{})
  378. vm.saveCtx(&vm.callStack[len(vm.callStack)-1])
  379. }
  380. func (vm *vm) restoreCtx(ctx *context) {
  381. vm.prg = ctx.prg
  382. vm.funcName = ctx.funcName
  383. vm.pc = ctx.pc
  384. vm.stash = ctx.stash
  385. vm.sb = ctx.sb
  386. vm.args = ctx.args
  387. vm.newTarget = ctx.newTarget
  388. }
  389. func (vm *vm) popCtx() {
  390. l := len(vm.callStack) - 1
  391. vm.prg = vm.callStack[l].prg
  392. vm.callStack[l].prg = nil
  393. vm.funcName = vm.callStack[l].funcName
  394. vm.pc = vm.callStack[l].pc
  395. vm.stash = vm.callStack[l].stash
  396. vm.callStack[l].stash = nil
  397. vm.sb = vm.callStack[l].sb
  398. vm.args = vm.callStack[l].args
  399. vm.callStack = vm.callStack[:l]
  400. }
  401. func (vm *vm) toCallee(v Value) *Object {
  402. if obj, ok := v.(*Object); ok {
  403. return obj
  404. }
  405. switch unresolved := v.(type) {
  406. case valueUnresolved:
  407. unresolved.throw()
  408. panic("Unreachable")
  409. case memberUnresolved:
  410. panic(vm.r.NewTypeError("Object has no member '%s'", unresolved.ref))
  411. }
  412. panic(vm.r.NewTypeError("Value is not an object: %s", v.toString()))
  413. }
  414. type _newStash struct{}
  415. var newStash _newStash
  416. func (_newStash) exec(vm *vm) {
  417. vm.newStash()
  418. vm.pc++
  419. }
  420. type loadVal uint32
  421. func (l loadVal) exec(vm *vm) {
  422. vm.push(vm.prg.values[l])
  423. vm.pc++
  424. }
  425. type _loadUndef struct{}
  426. var loadUndef _loadUndef
  427. func (_loadUndef) exec(vm *vm) {
  428. vm.push(_undefined)
  429. vm.pc++
  430. }
  431. type _loadNil struct{}
  432. var loadNil _loadNil
  433. func (_loadNil) exec(vm *vm) {
  434. vm.push(nil)
  435. vm.pc++
  436. }
  437. type _loadGlobalObject struct{}
  438. var loadGlobalObject _loadGlobalObject
  439. func (_loadGlobalObject) exec(vm *vm) {
  440. vm.push(vm.r.globalObject)
  441. vm.pc++
  442. }
  443. type loadStack int
  444. func (l loadStack) exec(vm *vm) {
  445. // l < 0 -- arg<-l-1>
  446. // l > 0 -- var<l-1>
  447. // l == 0 -- this
  448. if l < 0 {
  449. arg := int(-l)
  450. if arg > vm.args {
  451. vm.push(_undefined)
  452. } else {
  453. vm.push(vm.stack[vm.sb+arg])
  454. }
  455. } else if l > 0 {
  456. vm.push(vm.stack[vm.sb+vm.args+int(l)])
  457. } else {
  458. vm.push(vm.stack[vm.sb])
  459. }
  460. vm.pc++
  461. }
  462. type _loadCallee struct{}
  463. var loadCallee _loadCallee
  464. func (_loadCallee) exec(vm *vm) {
  465. vm.push(vm.stack[vm.sb-1])
  466. vm.pc++
  467. }
  468. func (vm *vm) storeStack(s int) {
  469. // l < 0 -- arg<-l-1>
  470. // l > 0 -- var<l-1>
  471. // l == 0 -- this
  472. if s < 0 {
  473. vm.stack[vm.sb-s] = vm.stack[vm.sp-1]
  474. } else if s > 0 {
  475. vm.stack[vm.sb+vm.args+s] = vm.stack[vm.sp-1]
  476. } else {
  477. panic("Attempt to modify this")
  478. }
  479. vm.pc++
  480. }
  481. type storeStack int
  482. func (s storeStack) exec(vm *vm) {
  483. vm.storeStack(int(s))
  484. }
  485. type storeStackP int
  486. func (s storeStackP) exec(vm *vm) {
  487. vm.storeStack(int(s))
  488. vm.sp--
  489. }
  490. type _toNumber struct{}
  491. var toNumber _toNumber
  492. func (_toNumber) exec(vm *vm) {
  493. vm.stack[vm.sp-1] = vm.stack[vm.sp-1].ToNumber()
  494. vm.pc++
  495. }
  496. type _add struct{}
  497. var add _add
  498. func (_add) exec(vm *vm) {
  499. right := vm.stack[vm.sp-1]
  500. left := vm.stack[vm.sp-2]
  501. if o, ok := left.(*Object); ok {
  502. left = o.self.toPrimitive()
  503. }
  504. if o, ok := right.(*Object); ok {
  505. right = o.self.toPrimitive()
  506. }
  507. var ret Value
  508. leftString, isLeftString := left.(valueString)
  509. rightString, isRightString := right.(valueString)
  510. if isLeftString || isRightString {
  511. if !isLeftString {
  512. leftString = left.toString()
  513. }
  514. if !isRightString {
  515. rightString = right.toString()
  516. }
  517. ret = leftString.concat(rightString)
  518. } else {
  519. if leftInt, ok := left.(valueInt); ok {
  520. if rightInt, ok := right.(valueInt); ok {
  521. ret = intToValue(int64(leftInt) + int64(rightInt))
  522. } else {
  523. ret = floatToValue(float64(leftInt) + right.ToFloat())
  524. }
  525. } else {
  526. ret = floatToValue(left.ToFloat() + right.ToFloat())
  527. }
  528. }
  529. vm.stack[vm.sp-2] = ret
  530. vm.sp--
  531. vm.pc++
  532. }
  533. type _sub struct{}
  534. var sub _sub
  535. func (_sub) exec(vm *vm) {
  536. right := vm.stack[vm.sp-1]
  537. left := vm.stack[vm.sp-2]
  538. var result Value
  539. if left, ok := left.(valueInt); ok {
  540. if right, ok := right.(valueInt); ok {
  541. result = intToValue(int64(left) - int64(right))
  542. goto end
  543. }
  544. }
  545. result = floatToValue(left.ToFloat() - right.ToFloat())
  546. end:
  547. vm.sp--
  548. vm.stack[vm.sp-1] = result
  549. vm.pc++
  550. }
  551. type _mul struct{}
  552. var mul _mul
  553. func (_mul) exec(vm *vm) {
  554. left := vm.stack[vm.sp-2]
  555. right := vm.stack[vm.sp-1]
  556. var result Value
  557. if left, ok := toInt64(left); ok {
  558. if right, ok := toInt64(right); ok {
  559. if left == 0 && right == -1 || left == -1 && right == 0 {
  560. result = _negativeZero
  561. goto end
  562. }
  563. res := left * right
  564. // check for overflow
  565. if left == 0 || right == 0 || res/left == right {
  566. result = intToValue(res)
  567. goto end
  568. }
  569. }
  570. }
  571. result = floatToValue(left.ToFloat() * right.ToFloat())
  572. end:
  573. vm.sp--
  574. vm.stack[vm.sp-1] = result
  575. vm.pc++
  576. }
  577. type _div struct{}
  578. var div _div
  579. func (_div) exec(vm *vm) {
  580. left := vm.stack[vm.sp-2].ToFloat()
  581. right := vm.stack[vm.sp-1].ToFloat()
  582. var result Value
  583. if math.IsNaN(left) || math.IsNaN(right) {
  584. result = _NaN
  585. goto end
  586. }
  587. if math.IsInf(left, 0) && math.IsInf(right, 0) {
  588. result = _NaN
  589. goto end
  590. }
  591. if left == 0 && right == 0 {
  592. result = _NaN
  593. goto end
  594. }
  595. if math.IsInf(left, 0) {
  596. if math.Signbit(left) == math.Signbit(right) {
  597. result = _positiveInf
  598. goto end
  599. } else {
  600. result = _negativeInf
  601. goto end
  602. }
  603. }
  604. if math.IsInf(right, 0) {
  605. if math.Signbit(left) == math.Signbit(right) {
  606. result = _positiveZero
  607. goto end
  608. } else {
  609. result = _negativeZero
  610. goto end
  611. }
  612. }
  613. if right == 0 {
  614. if math.Signbit(left) == math.Signbit(right) {
  615. result = _positiveInf
  616. goto end
  617. } else {
  618. result = _negativeInf
  619. goto end
  620. }
  621. }
  622. result = floatToValue(left / right)
  623. end:
  624. vm.sp--
  625. vm.stack[vm.sp-1] = result
  626. vm.pc++
  627. }
  628. type _mod struct{}
  629. var mod _mod
  630. func (_mod) exec(vm *vm) {
  631. left := vm.stack[vm.sp-2]
  632. right := vm.stack[vm.sp-1]
  633. var result Value
  634. if leftInt, ok := toInt64(left); ok {
  635. if rightInt, ok := toInt64(right); ok {
  636. if rightInt == 0 {
  637. result = _NaN
  638. goto end
  639. }
  640. r := leftInt % rightInt
  641. if r == 0 && leftInt < 0 {
  642. result = _negativeZero
  643. } else {
  644. result = intToValue(leftInt % rightInt)
  645. }
  646. goto end
  647. }
  648. }
  649. result = floatToValue(math.Mod(left.ToFloat(), right.ToFloat()))
  650. end:
  651. vm.sp--
  652. vm.stack[vm.sp-1] = result
  653. vm.pc++
  654. }
  655. type _neg struct{}
  656. var neg _neg
  657. func (_neg) exec(vm *vm) {
  658. operand := vm.stack[vm.sp-1]
  659. var result Value
  660. if i, ok := toInt64(operand); ok {
  661. if i == 0 {
  662. result = _negativeZero
  663. } else {
  664. result = valueInt(-i)
  665. }
  666. } else {
  667. f := operand.ToFloat()
  668. if !math.IsNaN(f) {
  669. f = -f
  670. }
  671. result = valueFloat(f)
  672. }
  673. vm.stack[vm.sp-1] = result
  674. vm.pc++
  675. }
  676. type _plus struct{}
  677. var plus _plus
  678. func (_plus) exec(vm *vm) {
  679. vm.stack[vm.sp-1] = vm.stack[vm.sp-1].ToNumber()
  680. vm.pc++
  681. }
  682. type _inc struct{}
  683. var inc _inc
  684. func (_inc) exec(vm *vm) {
  685. v := vm.stack[vm.sp-1]
  686. if i, ok := toInt64(v); ok {
  687. v = intToValue(i + 1)
  688. goto end
  689. }
  690. v = valueFloat(v.ToFloat() + 1)
  691. end:
  692. vm.stack[vm.sp-1] = v
  693. vm.pc++
  694. }
  695. type _dec struct{}
  696. var dec _dec
  697. func (_dec) exec(vm *vm) {
  698. v := vm.stack[vm.sp-1]
  699. if i, ok := toInt64(v); ok {
  700. v = intToValue(i - 1)
  701. goto end
  702. }
  703. v = valueFloat(v.ToFloat() - 1)
  704. end:
  705. vm.stack[vm.sp-1] = v
  706. vm.pc++
  707. }
  708. type _and struct{}
  709. var and _and
  710. func (_and) exec(vm *vm) {
  711. left := toInt32(vm.stack[vm.sp-2])
  712. right := toInt32(vm.stack[vm.sp-1])
  713. vm.stack[vm.sp-2] = intToValue(int64(left & right))
  714. vm.sp--
  715. vm.pc++
  716. }
  717. type _or struct{}
  718. var or _or
  719. func (_or) exec(vm *vm) {
  720. left := toInt32(vm.stack[vm.sp-2])
  721. right := toInt32(vm.stack[vm.sp-1])
  722. vm.stack[vm.sp-2] = intToValue(int64(left | right))
  723. vm.sp--
  724. vm.pc++
  725. }
  726. type _xor struct{}
  727. var xor _xor
  728. func (_xor) exec(vm *vm) {
  729. left := toInt32(vm.stack[vm.sp-2])
  730. right := toInt32(vm.stack[vm.sp-1])
  731. vm.stack[vm.sp-2] = intToValue(int64(left ^ right))
  732. vm.sp--
  733. vm.pc++
  734. }
  735. type _bnot struct{}
  736. var bnot _bnot
  737. func (_bnot) exec(vm *vm) {
  738. op := toInt32(vm.stack[vm.sp-1])
  739. vm.stack[vm.sp-1] = intToValue(int64(^op))
  740. vm.pc++
  741. }
  742. type _sal struct{}
  743. var sal _sal
  744. func (_sal) exec(vm *vm) {
  745. left := toInt32(vm.stack[vm.sp-2])
  746. right := toUInt32(vm.stack[vm.sp-1])
  747. vm.stack[vm.sp-2] = intToValue(int64(left << (right & 0x1F)))
  748. vm.sp--
  749. vm.pc++
  750. }
  751. type _sar struct{}
  752. var sar _sar
  753. func (_sar) exec(vm *vm) {
  754. left := toInt32(vm.stack[vm.sp-2])
  755. right := toUInt32(vm.stack[vm.sp-1])
  756. vm.stack[vm.sp-2] = intToValue(int64(left >> (right & 0x1F)))
  757. vm.sp--
  758. vm.pc++
  759. }
  760. type _shr struct{}
  761. var shr _shr
  762. func (_shr) exec(vm *vm) {
  763. left := toUInt32(vm.stack[vm.sp-2])
  764. right := toUInt32(vm.stack[vm.sp-1])
  765. vm.stack[vm.sp-2] = intToValue(int64(left >> (right & 0x1F)))
  766. vm.sp--
  767. vm.pc++
  768. }
  769. type _halt struct{}
  770. var halt _halt
  771. func (_halt) exec(vm *vm) {
  772. vm.halt = true
  773. vm.pc++
  774. }
  775. type jump int32
  776. func (j jump) exec(vm *vm) {
  777. vm.pc += int(j)
  778. }
  779. type _setElem struct{}
  780. var setElem _setElem
  781. func (_setElem) exec(vm *vm) {
  782. obj := vm.stack[vm.sp-3].ToObject(vm.r)
  783. propName := toPropertyKey(vm.stack[vm.sp-2])
  784. val := vm.stack[vm.sp-1]
  785. obj.setOwn(propName, val, false)
  786. vm.sp -= 2
  787. vm.stack[vm.sp-1] = val
  788. vm.pc++
  789. }
  790. type _setElemStrict struct{}
  791. var setElemStrict _setElemStrict
  792. func (_setElemStrict) exec(vm *vm) {
  793. obj := vm.r.toObject(vm.stack[vm.sp-3])
  794. propName := toPropertyKey(vm.stack[vm.sp-2])
  795. val := vm.stack[vm.sp-1]
  796. obj.setOwn(propName, val, true)
  797. vm.sp -= 2
  798. vm.stack[vm.sp-1] = val
  799. vm.pc++
  800. }
  801. type _deleteElem struct{}
  802. var deleteElem _deleteElem
  803. func (_deleteElem) exec(vm *vm) {
  804. obj := vm.r.toObject(vm.stack[vm.sp-2])
  805. propName := toPropertyKey(vm.stack[vm.sp-1])
  806. if obj.delete(propName, false) {
  807. vm.stack[vm.sp-2] = valueTrue
  808. } else {
  809. vm.stack[vm.sp-2] = valueFalse
  810. }
  811. vm.sp--
  812. vm.pc++
  813. }
  814. type _deleteElemStrict struct{}
  815. var deleteElemStrict _deleteElemStrict
  816. func (_deleteElemStrict) exec(vm *vm) {
  817. obj := vm.r.toObject(vm.stack[vm.sp-2])
  818. propName := toPropertyKey(vm.stack[vm.sp-1])
  819. obj.delete(propName, true)
  820. vm.stack[vm.sp-2] = valueTrue
  821. vm.sp--
  822. vm.pc++
  823. }
  824. type deleteProp string
  825. func (d deleteProp) exec(vm *vm) {
  826. obj := vm.r.toObject(vm.stack[vm.sp-1])
  827. if obj.self.deleteStr(string(d), false) {
  828. vm.stack[vm.sp-1] = valueTrue
  829. } else {
  830. vm.stack[vm.sp-1] = valueFalse
  831. }
  832. vm.pc++
  833. }
  834. type deletePropStrict string
  835. func (d deletePropStrict) exec(vm *vm) {
  836. obj := vm.r.toObject(vm.stack[vm.sp-1])
  837. obj.self.deleteStr(string(d), true)
  838. vm.stack[vm.sp-1] = valueTrue
  839. vm.pc++
  840. }
  841. type setProp string
  842. func (p setProp) exec(vm *vm) {
  843. val := vm.stack[vm.sp-1]
  844. vm.stack[vm.sp-2].ToObject(vm.r).self.setOwnStr(string(p), val, false)
  845. vm.stack[vm.sp-2] = val
  846. vm.sp--
  847. vm.pc++
  848. }
  849. type setPropStrict string
  850. func (p setPropStrict) exec(vm *vm) {
  851. obj := vm.stack[vm.sp-2]
  852. val := vm.stack[vm.sp-1]
  853. obj1 := vm.r.toObject(obj)
  854. obj1.self.setOwnStr(string(p), val, true)
  855. vm.stack[vm.sp-2] = val
  856. vm.sp--
  857. vm.pc++
  858. }
  859. type setProp1 string
  860. func (p setProp1) exec(vm *vm) {
  861. vm.r.toObject(vm.stack[vm.sp-2]).self._putProp(string(p), vm.stack[vm.sp-1], true, true, true)
  862. vm.sp--
  863. vm.pc++
  864. }
  865. type _setProto struct{}
  866. var setProto _setProto
  867. func (_setProto) exec(vm *vm) {
  868. vm.r.toObject(vm.stack[vm.sp-2]).self.setProto(vm.r.toProto(vm.stack[vm.sp-1]), true)
  869. vm.sp--
  870. vm.pc++
  871. }
  872. type setPropGetter string
  873. func (s setPropGetter) exec(vm *vm) {
  874. obj := vm.r.toObject(vm.stack[vm.sp-2])
  875. val := vm.stack[vm.sp-1]
  876. descr := PropertyDescriptor{
  877. Getter: val,
  878. Configurable: FLAG_TRUE,
  879. Enumerable: FLAG_TRUE,
  880. }
  881. obj.self.defineOwnPropertyStr(string(s), descr, false)
  882. vm.sp--
  883. vm.pc++
  884. }
  885. type setPropSetter string
  886. func (s setPropSetter) exec(vm *vm) {
  887. obj := vm.r.toObject(vm.stack[vm.sp-2])
  888. val := vm.stack[vm.sp-1]
  889. descr := PropertyDescriptor{
  890. Setter: val,
  891. Configurable: FLAG_TRUE,
  892. Enumerable: FLAG_TRUE,
  893. }
  894. obj.self.defineOwnPropertyStr(string(s), descr, false)
  895. vm.sp--
  896. vm.pc++
  897. }
  898. type getProp string
  899. func (g getProp) exec(vm *vm) {
  900. v := vm.stack[vm.sp-1]
  901. obj := v.baseObject(vm.r)
  902. if obj == nil {
  903. panic(vm.r.NewTypeError("Cannot read property '%s' of undefined", g))
  904. }
  905. vm.stack[vm.sp-1] = nilSafe(obj.self.getStr(string(g), v))
  906. vm.pc++
  907. }
  908. type getPropCallee string
  909. func (g getPropCallee) exec(vm *vm) {
  910. v := vm.stack[vm.sp-1]
  911. obj := v.baseObject(vm.r)
  912. if obj == nil {
  913. panic(vm.r.NewTypeError("Cannot read property '%s' of undefined or null", g))
  914. }
  915. prop := obj.self.getStr(string(g), v)
  916. if prop == nil {
  917. prop = memberUnresolved{valueUnresolved{r: vm.r, ref: string(g)}}
  918. }
  919. vm.stack[vm.sp-1] = prop
  920. vm.pc++
  921. }
  922. type _getElem struct{}
  923. var getElem _getElem
  924. func (_getElem) exec(vm *vm) {
  925. v := vm.stack[vm.sp-2]
  926. obj := v.baseObject(vm.r)
  927. propName := toPropertyKey(vm.stack[vm.sp-1])
  928. if obj == nil {
  929. panic(vm.r.NewTypeError("Cannot read property '%s' of undefined", propName.String()))
  930. }
  931. vm.stack[vm.sp-2] = nilSafe(obj.get(propName, v))
  932. vm.sp--
  933. vm.pc++
  934. }
  935. type _getElemCallee struct{}
  936. var getElemCallee _getElemCallee
  937. func (_getElemCallee) exec(vm *vm) {
  938. v := vm.stack[vm.sp-2]
  939. obj := v.baseObject(vm.r)
  940. propName := toPropertyKey(vm.stack[vm.sp-1])
  941. if obj == nil {
  942. panic(vm.r.NewTypeError("Cannot read property '%s' of undefined", propName.String()))
  943. }
  944. prop := obj.get(propName, v)
  945. if prop == nil {
  946. prop = memberUnresolved{valueUnresolved{r: vm.r, ref: propName.String()}}
  947. }
  948. vm.stack[vm.sp-2] = prop
  949. vm.sp--
  950. vm.pc++
  951. }
  952. type _dup struct{}
  953. var dup _dup
  954. func (_dup) exec(vm *vm) {
  955. vm.push(vm.stack[vm.sp-1])
  956. vm.pc++
  957. }
  958. type dupN uint32
  959. func (d dupN) exec(vm *vm) {
  960. vm.push(vm.stack[vm.sp-1-int(d)])
  961. vm.pc++
  962. }
  963. type rdupN uint32
  964. func (d rdupN) exec(vm *vm) {
  965. vm.stack[vm.sp-1-int(d)] = vm.stack[vm.sp-1]
  966. vm.pc++
  967. }
  968. type _newObject struct{}
  969. var newObject _newObject
  970. func (_newObject) exec(vm *vm) {
  971. vm.push(vm.r.NewObject())
  972. vm.pc++
  973. }
  974. type newArray uint32
  975. func (l newArray) exec(vm *vm) {
  976. values := make([]Value, l)
  977. if l > 0 {
  978. copy(values, vm.stack[vm.sp-int(l):vm.sp])
  979. }
  980. obj := vm.r.newArrayValues(values)
  981. if l > 0 {
  982. vm.sp -= int(l) - 1
  983. vm.stack[vm.sp-1] = obj
  984. } else {
  985. vm.push(obj)
  986. }
  987. vm.pc++
  988. }
  989. type newArraySparse struct {
  990. l, objCount int
  991. }
  992. func (n *newArraySparse) exec(vm *vm) {
  993. values := make([]Value, n.l)
  994. copy(values, vm.stack[vm.sp-int(n.l):vm.sp])
  995. arr := vm.r.newArrayObject()
  996. setArrayValues(arr, values)
  997. arr.objCount = n.objCount
  998. vm.sp -= int(n.l) - 1
  999. vm.stack[vm.sp-1] = arr.val
  1000. vm.pc++
  1001. }
  1002. type newRegexp struct {
  1003. pattern regexpPattern
  1004. src valueString
  1005. global, ignoreCase, multiline, sticky bool
  1006. }
  1007. func (n *newRegexp) exec(vm *vm) {
  1008. vm.push(vm.r.newRegExpp(n.pattern, n.src, n.global, n.ignoreCase, n.multiline, n.sticky, vm.r.global.RegExpPrototype))
  1009. vm.pc++
  1010. }
  1011. func (vm *vm) setLocal(s int) {
  1012. v := vm.stack[vm.sp-1]
  1013. level := s >> 24
  1014. idx := uint32(s & 0x00FFFFFF)
  1015. stash := vm.stash
  1016. for i := 0; i < level; i++ {
  1017. stash = stash.outer
  1018. }
  1019. stash.putByIdx(idx, v)
  1020. vm.pc++
  1021. }
  1022. type setLocal uint32
  1023. func (s setLocal) exec(vm *vm) {
  1024. vm.setLocal(int(s))
  1025. }
  1026. type setLocalP uint32
  1027. func (s setLocalP) exec(vm *vm) {
  1028. vm.setLocal(int(s))
  1029. vm.sp--
  1030. }
  1031. type setVar struct {
  1032. name string
  1033. idx uint32
  1034. }
  1035. func (s setVar) exec(vm *vm) {
  1036. v := vm.peek()
  1037. level := int(s.idx >> 24)
  1038. idx := s.idx & 0x00FFFFFF
  1039. stash := vm.stash
  1040. name := s.name
  1041. for i := 0; i < level; i++ {
  1042. if stash.put(name, v) {
  1043. goto end
  1044. }
  1045. stash = stash.outer
  1046. }
  1047. if stash != nil {
  1048. stash.putByIdx(idx, v)
  1049. } else {
  1050. vm.r.globalObject.self.setOwnStr(name, v, false)
  1051. }
  1052. end:
  1053. vm.pc++
  1054. }
  1055. type resolveVar1 string
  1056. func (s resolveVar1) exec(vm *vm) {
  1057. name := string(s)
  1058. var ref ref
  1059. for stash := vm.stash; stash != nil; stash = stash.outer {
  1060. if stash.obj != nil {
  1061. if stash.obj.hasPropertyStr(name) {
  1062. ref = &objRef{
  1063. base: stash.obj,
  1064. name: name,
  1065. }
  1066. goto end
  1067. }
  1068. } else {
  1069. if idx, exists := stash.names[name]; exists {
  1070. ref = &stashRef{
  1071. v: &stash.values[idx],
  1072. }
  1073. goto end
  1074. }
  1075. }
  1076. }
  1077. ref = &objRef{
  1078. base: vm.r.globalObject.self,
  1079. name: name,
  1080. }
  1081. end:
  1082. vm.refStack = append(vm.refStack, ref)
  1083. vm.pc++
  1084. }
  1085. type deleteVar string
  1086. func (d deleteVar) exec(vm *vm) {
  1087. name := string(d)
  1088. ret := true
  1089. for stash := vm.stash; stash != nil; stash = stash.outer {
  1090. if stash.obj != nil {
  1091. if stash.obj.hasPropertyStr(name) {
  1092. ret = stash.obj.deleteStr(name, false)
  1093. goto end
  1094. }
  1095. } else {
  1096. if _, exists := stash.names[name]; exists {
  1097. ret = false
  1098. goto end
  1099. }
  1100. }
  1101. }
  1102. if vm.r.globalObject.self.hasPropertyStr(name) {
  1103. ret = vm.r.globalObject.self.deleteStr(name, false)
  1104. }
  1105. end:
  1106. if ret {
  1107. vm.push(valueTrue)
  1108. } else {
  1109. vm.push(valueFalse)
  1110. }
  1111. vm.pc++
  1112. }
  1113. type deleteGlobal string
  1114. func (d deleteGlobal) exec(vm *vm) {
  1115. name := string(d)
  1116. var ret bool
  1117. if vm.r.globalObject.self.hasPropertyStr(name) {
  1118. ret = vm.r.globalObject.self.deleteStr(name, false)
  1119. } else {
  1120. ret = true
  1121. }
  1122. if ret {
  1123. vm.push(valueTrue)
  1124. } else {
  1125. vm.push(valueFalse)
  1126. }
  1127. vm.pc++
  1128. }
  1129. type resolveVar1Strict string
  1130. func (s resolveVar1Strict) exec(vm *vm) {
  1131. name := string(s)
  1132. var ref ref
  1133. for stash := vm.stash; stash != nil; stash = stash.outer {
  1134. if stash.obj != nil {
  1135. if stash.obj.hasPropertyStr(name) {
  1136. ref = &objRef{
  1137. base: stash.obj,
  1138. name: name,
  1139. strict: true,
  1140. }
  1141. goto end
  1142. }
  1143. } else {
  1144. if idx, exists := stash.names[name]; exists {
  1145. ref = &stashRef{
  1146. v: &stash.values[idx],
  1147. }
  1148. goto end
  1149. }
  1150. }
  1151. }
  1152. if vm.r.globalObject.self.hasPropertyStr(name) {
  1153. ref = &objRef{
  1154. base: vm.r.globalObject.self,
  1155. name: name,
  1156. strict: true,
  1157. }
  1158. goto end
  1159. }
  1160. ref = &unresolvedRef{
  1161. runtime: vm.r,
  1162. name: string(s),
  1163. }
  1164. end:
  1165. vm.refStack = append(vm.refStack, ref)
  1166. vm.pc++
  1167. }
  1168. type setGlobal string
  1169. func (s setGlobal) exec(vm *vm) {
  1170. v := vm.peek()
  1171. vm.r.globalObject.self.setOwnStr(string(s), v, false)
  1172. vm.pc++
  1173. }
  1174. type setGlobalStrict string
  1175. func (s setGlobalStrict) exec(vm *vm) {
  1176. v := vm.peek()
  1177. name := string(s)
  1178. o := vm.r.globalObject.self
  1179. if o.hasOwnPropertyStr(name) {
  1180. o.setOwnStr(name, v, true)
  1181. } else {
  1182. vm.r.throwReferenceError(name)
  1183. }
  1184. vm.pc++
  1185. }
  1186. type getLocal uint32
  1187. func (g getLocal) exec(vm *vm) {
  1188. level := int(g >> 24)
  1189. idx := uint32(g & 0x00FFFFFF)
  1190. stash := vm.stash
  1191. for i := 0; i < level; i++ {
  1192. stash = stash.outer
  1193. }
  1194. vm.push(stash.getByIdx(idx))
  1195. vm.pc++
  1196. }
  1197. type getVar struct {
  1198. name string
  1199. idx uint32
  1200. ref bool
  1201. }
  1202. func (g getVar) exec(vm *vm) {
  1203. level := int(g.idx >> 24)
  1204. idx := uint32(g.idx & 0x00FFFFFF)
  1205. stash := vm.stash
  1206. name := g.name
  1207. for i := 0; i < level; i++ {
  1208. if v, found := stash.getByName(name, vm); found {
  1209. vm.push(v)
  1210. goto end
  1211. }
  1212. stash = stash.outer
  1213. }
  1214. if stash != nil {
  1215. vm.push(stash.getByIdx(idx))
  1216. } else {
  1217. v := vm.r.globalObject.self.getStr(name, nil)
  1218. if v == nil {
  1219. if g.ref {
  1220. v = valueUnresolved{r: vm.r, ref: name}
  1221. } else {
  1222. vm.r.throwReferenceError(name)
  1223. }
  1224. }
  1225. vm.push(v)
  1226. }
  1227. end:
  1228. vm.pc++
  1229. }
  1230. type resolveVar struct {
  1231. name string
  1232. idx uint32
  1233. strict bool
  1234. }
  1235. func (r resolveVar) exec(vm *vm) {
  1236. level := int(r.idx >> 24)
  1237. idx := r.idx & 0x00FFFFFF
  1238. stash := vm.stash
  1239. var ref ref
  1240. for i := 0; i < level; i++ {
  1241. if stash.obj != nil {
  1242. if stash.obj.hasPropertyStr(r.name) {
  1243. ref = &objRef{
  1244. base: stash.obj,
  1245. name: r.name,
  1246. strict: r.strict,
  1247. }
  1248. goto end
  1249. }
  1250. } else {
  1251. if idx, exists := stash.names[r.name]; exists {
  1252. ref = &stashRef{
  1253. v: &stash.values[idx],
  1254. }
  1255. goto end
  1256. }
  1257. }
  1258. stash = stash.outer
  1259. }
  1260. if stash != nil {
  1261. ref = &stashRef{
  1262. v: &stash.values[idx],
  1263. }
  1264. goto end
  1265. } /*else {
  1266. if vm.r.globalObject.self.hasProperty(nameVal) {
  1267. ref = &objRef{
  1268. base: vm.r.globalObject.self,
  1269. name: r.name,
  1270. }
  1271. goto end
  1272. }
  1273. } */
  1274. ref = &unresolvedRef{
  1275. runtime: vm.r,
  1276. name: r.name,
  1277. }
  1278. end:
  1279. vm.refStack = append(vm.refStack, ref)
  1280. vm.pc++
  1281. }
  1282. type _getValue struct{}
  1283. var getValue _getValue
  1284. func (_getValue) exec(vm *vm) {
  1285. ref := vm.refStack[len(vm.refStack)-1]
  1286. if v := ref.get(); v != nil {
  1287. vm.push(v)
  1288. } else {
  1289. vm.r.throwReferenceError(ref.refname())
  1290. panic("Unreachable")
  1291. }
  1292. vm.pc++
  1293. }
  1294. type _putValue struct{}
  1295. var putValue _putValue
  1296. func (_putValue) exec(vm *vm) {
  1297. l := len(vm.refStack) - 1
  1298. ref := vm.refStack[l]
  1299. vm.refStack[l] = nil
  1300. vm.refStack = vm.refStack[:l]
  1301. ref.set(vm.stack[vm.sp-1])
  1302. vm.pc++
  1303. }
  1304. type getVar1 string
  1305. func (n getVar1) exec(vm *vm) {
  1306. name := string(n)
  1307. var val Value
  1308. for stash := vm.stash; stash != nil; stash = stash.outer {
  1309. if v, exists := stash.getByName(name, vm); exists {
  1310. val = v
  1311. break
  1312. }
  1313. }
  1314. if val == nil {
  1315. val = vm.r.globalObject.self.getStr(name, nil)
  1316. if val == nil {
  1317. vm.r.throwReferenceError(name)
  1318. }
  1319. }
  1320. vm.push(val)
  1321. vm.pc++
  1322. }
  1323. type getVar1Callee string
  1324. func (n getVar1Callee) exec(vm *vm) {
  1325. name := string(n)
  1326. var val Value
  1327. for stash := vm.stash; stash != nil; stash = stash.outer {
  1328. if v, exists := stash.getByName(name, vm); exists {
  1329. val = v
  1330. break
  1331. }
  1332. }
  1333. if val == nil {
  1334. val = vm.r.globalObject.self.getStr(name, nil)
  1335. if val == nil {
  1336. val = valueUnresolved{r: vm.r, ref: name}
  1337. }
  1338. }
  1339. vm.push(val)
  1340. vm.pc++
  1341. }
  1342. type _pop struct{}
  1343. var pop _pop
  1344. func (_pop) exec(vm *vm) {
  1345. vm.sp--
  1346. vm.pc++
  1347. }
  1348. func (vm *vm) callEval(n int, strict bool) {
  1349. if vm.r.toObject(vm.stack[vm.sp-n-1]) == vm.r.global.Eval {
  1350. if n > 0 {
  1351. srcVal := vm.stack[vm.sp-n]
  1352. if src, ok := srcVal.(valueString); ok {
  1353. var this Value
  1354. if vm.sb != 0 {
  1355. this = vm.stack[vm.sb]
  1356. } else {
  1357. this = vm.r.globalObject
  1358. }
  1359. ret := vm.r.eval(src.String(), true, strict, this)
  1360. vm.stack[vm.sp-n-2] = ret
  1361. } else {
  1362. vm.stack[vm.sp-n-2] = srcVal
  1363. }
  1364. } else {
  1365. vm.stack[vm.sp-n-2] = _undefined
  1366. }
  1367. vm.sp -= n + 1
  1368. vm.pc++
  1369. } else {
  1370. call(n).exec(vm)
  1371. }
  1372. }
  1373. type callEval uint32
  1374. func (numargs callEval) exec(vm *vm) {
  1375. vm.callEval(int(numargs), false)
  1376. }
  1377. type callEvalStrict uint32
  1378. func (numargs callEvalStrict) exec(vm *vm) {
  1379. vm.callEval(int(numargs), true)
  1380. }
  1381. type _boxThis struct{}
  1382. var boxThis _boxThis
  1383. func (_boxThis) exec(vm *vm) {
  1384. v := vm.stack[vm.sb]
  1385. if v == _undefined || v == _null {
  1386. vm.stack[vm.sb] = vm.r.globalObject
  1387. } else {
  1388. vm.stack[vm.sb] = v.ToObject(vm.r)
  1389. }
  1390. vm.pc++
  1391. }
  1392. type call uint32
  1393. func (numargs call) exec(vm *vm) {
  1394. // this
  1395. // callee
  1396. // arg0
  1397. // ...
  1398. // arg<numargs-1>
  1399. n := int(numargs)
  1400. v := vm.stack[vm.sp-n-1] // callee
  1401. obj := vm.toCallee(v)
  1402. repeat:
  1403. switch f := obj.self.(type) {
  1404. case *funcObject:
  1405. vm.pc++
  1406. vm.pushCtx()
  1407. vm.args = n
  1408. vm.prg = f.prg
  1409. vm.stash = f.stash
  1410. vm.pc = 0
  1411. vm.stack[vm.sp-n-1], vm.stack[vm.sp-n-2] = vm.stack[vm.sp-n-2], vm.stack[vm.sp-n-1]
  1412. return
  1413. case *nativeFuncObject:
  1414. vm._nativeCall(f, n)
  1415. case *boundFuncObject:
  1416. vm._nativeCall(&f.nativeFuncObject, n)
  1417. case *proxyObject:
  1418. vm.pushCtx()
  1419. vm.prg = nil
  1420. vm.funcName = "proxy"
  1421. ret := f.apply(FunctionCall{This: vm.stack[vm.sp-n-2], Arguments: vm.stack[vm.sp-n : vm.sp]})
  1422. if ret == nil {
  1423. ret = _undefined
  1424. }
  1425. vm.stack[vm.sp-n-2] = ret
  1426. vm.popCtx()
  1427. vm.sp -= n + 1
  1428. vm.pc++
  1429. case *lazyObject:
  1430. obj.self = f.create(obj)
  1431. goto repeat
  1432. default:
  1433. vm.r.typeErrorResult(true, "Not a function: %s", obj.toString())
  1434. }
  1435. }
  1436. func (vm *vm) _nativeCall(f *nativeFuncObject, n int) {
  1437. if f.f != nil {
  1438. vm.pushCtx()
  1439. vm.prg = nil
  1440. vm.funcName = f.nameProp.get(nil).String()
  1441. ret := f.f(FunctionCall{
  1442. Arguments: vm.stack[vm.sp-n : vm.sp],
  1443. This: vm.stack[vm.sp-n-2],
  1444. })
  1445. if ret == nil {
  1446. ret = _undefined
  1447. }
  1448. vm.stack[vm.sp-n-2] = ret
  1449. vm.popCtx()
  1450. } else {
  1451. vm.stack[vm.sp-n-2] = _undefined
  1452. }
  1453. vm.sp -= n + 1
  1454. vm.pc++
  1455. }
  1456. func (vm *vm) clearStack() {
  1457. stackTail := vm.stack[vm.sp:]
  1458. for i := range stackTail {
  1459. stackTail[i] = nil
  1460. }
  1461. vm.stack = vm.stack[:vm.sp]
  1462. }
  1463. type enterFunc uint32
  1464. func (e enterFunc) exec(vm *vm) {
  1465. // Input stack:
  1466. //
  1467. // callee
  1468. // this
  1469. // arg0
  1470. // ...
  1471. // argN
  1472. // <- sp
  1473. // Output stack:
  1474. //
  1475. // this <- sb
  1476. // <- sp
  1477. vm.newStash()
  1478. offset := vm.args - int(e)
  1479. vm.stash.values = make([]Value, e)
  1480. if offset > 0 {
  1481. copy(vm.stash.values, vm.stack[vm.sp-vm.args:])
  1482. vm.stash.extraArgs = make([]Value, offset)
  1483. copy(vm.stash.extraArgs, vm.stack[vm.sp-offset:])
  1484. } else {
  1485. copy(vm.stash.values, vm.stack[vm.sp-vm.args:])
  1486. vv := vm.stash.values[vm.args:]
  1487. for i := range vv {
  1488. vv[i] = _undefined
  1489. }
  1490. }
  1491. vm.sp -= vm.args
  1492. vm.sb = vm.sp - 1
  1493. vm.pc++
  1494. }
  1495. type _ret struct{}
  1496. var ret _ret
  1497. func (_ret) exec(vm *vm) {
  1498. // callee -3
  1499. // this -2
  1500. // retval -1
  1501. vm.stack[vm.sp-3] = vm.stack[vm.sp-1]
  1502. vm.sp -= 2
  1503. vm.popCtx()
  1504. if vm.pc < 0 {
  1505. vm.halt = true
  1506. }
  1507. }
  1508. type enterFuncStashless struct {
  1509. stackSize uint32
  1510. args uint32
  1511. }
  1512. func (e enterFuncStashless) exec(vm *vm) {
  1513. vm.sb = vm.sp - vm.args - 1
  1514. var ss int
  1515. d := int(e.args) - vm.args
  1516. if d > 0 {
  1517. ss = int(e.stackSize) + d
  1518. vm.args = int(e.args)
  1519. } else {
  1520. ss = int(e.stackSize)
  1521. }
  1522. sp := vm.sp
  1523. if ss > 0 {
  1524. vm.sp += int(ss)
  1525. vm.stack.expand(vm.sp)
  1526. s := vm.stack[sp:vm.sp]
  1527. for i := range s {
  1528. s[i] = _undefined
  1529. }
  1530. }
  1531. vm.pc++
  1532. }
  1533. type _retStashless struct{}
  1534. var retStashless _retStashless
  1535. func (_retStashless) exec(vm *vm) {
  1536. retval := vm.stack[vm.sp-1]
  1537. vm.sp = vm.sb
  1538. vm.stack[vm.sp-1] = retval
  1539. vm.popCtx()
  1540. if vm.pc < 0 {
  1541. vm.halt = true
  1542. }
  1543. }
  1544. type newFunc struct {
  1545. prg *Program
  1546. name string
  1547. length uint32
  1548. strict bool
  1549. srcStart, srcEnd uint32
  1550. }
  1551. func (n *newFunc) exec(vm *vm) {
  1552. obj := vm.r.newFunc(n.name, int(n.length), n.strict)
  1553. obj.prg = n.prg
  1554. obj.stash = vm.stash
  1555. obj.src = n.prg.src.src[n.srcStart:n.srcEnd]
  1556. vm.push(obj.val)
  1557. vm.pc++
  1558. }
  1559. type bindName string
  1560. func (d bindName) exec(vm *vm) {
  1561. if vm.stash != nil {
  1562. vm.stash.createBinding(string(d))
  1563. } else {
  1564. vm.r.globalObject.self._putProp(string(d), _undefined, true, true, false)
  1565. }
  1566. vm.pc++
  1567. }
  1568. type jne int32
  1569. func (j jne) exec(vm *vm) {
  1570. vm.sp--
  1571. if !vm.stack[vm.sp].ToBoolean() {
  1572. vm.pc += int(j)
  1573. } else {
  1574. vm.pc++
  1575. }
  1576. }
  1577. type jeq int32
  1578. func (j jeq) exec(vm *vm) {
  1579. vm.sp--
  1580. if vm.stack[vm.sp].ToBoolean() {
  1581. vm.pc += int(j)
  1582. } else {
  1583. vm.pc++
  1584. }
  1585. }
  1586. type jeq1 int32
  1587. func (j jeq1) exec(vm *vm) {
  1588. if vm.stack[vm.sp-1].ToBoolean() {
  1589. vm.pc += int(j)
  1590. } else {
  1591. vm.pc++
  1592. }
  1593. }
  1594. type jneq1 int32
  1595. func (j jneq1) exec(vm *vm) {
  1596. if !vm.stack[vm.sp-1].ToBoolean() {
  1597. vm.pc += int(j)
  1598. } else {
  1599. vm.pc++
  1600. }
  1601. }
  1602. type _not struct{}
  1603. var not _not
  1604. func (_not) exec(vm *vm) {
  1605. if vm.stack[vm.sp-1].ToBoolean() {
  1606. vm.stack[vm.sp-1] = valueFalse
  1607. } else {
  1608. vm.stack[vm.sp-1] = valueTrue
  1609. }
  1610. vm.pc++
  1611. }
  1612. func toPrimitiveNumber(v Value) Value {
  1613. if o, ok := v.(*Object); ok {
  1614. return o.self.toPrimitiveNumber()
  1615. }
  1616. return v
  1617. }
  1618. func cmp(px, py Value) Value {
  1619. var ret bool
  1620. var nx, ny float64
  1621. if xs, ok := px.(valueString); ok {
  1622. if ys, ok := py.(valueString); ok {
  1623. ret = xs.compareTo(ys) < 0
  1624. goto end
  1625. }
  1626. }
  1627. if xi, ok := px.(valueInt); ok {
  1628. if yi, ok := py.(valueInt); ok {
  1629. ret = xi < yi
  1630. goto end
  1631. }
  1632. }
  1633. nx = px.ToFloat()
  1634. ny = py.ToFloat()
  1635. if math.IsNaN(nx) || math.IsNaN(ny) {
  1636. return _undefined
  1637. }
  1638. ret = nx < ny
  1639. end:
  1640. if ret {
  1641. return valueTrue
  1642. }
  1643. return valueFalse
  1644. }
  1645. type _op_lt struct{}
  1646. var op_lt _op_lt
  1647. func (_op_lt) exec(vm *vm) {
  1648. left := toPrimitiveNumber(vm.stack[vm.sp-2])
  1649. right := toPrimitiveNumber(vm.stack[vm.sp-1])
  1650. r := cmp(left, right)
  1651. if r == _undefined {
  1652. vm.stack[vm.sp-2] = valueFalse
  1653. } else {
  1654. vm.stack[vm.sp-2] = r
  1655. }
  1656. vm.sp--
  1657. vm.pc++
  1658. }
  1659. type _op_lte struct{}
  1660. var op_lte _op_lte
  1661. func (_op_lte) exec(vm *vm) {
  1662. left := toPrimitiveNumber(vm.stack[vm.sp-2])
  1663. right := toPrimitiveNumber(vm.stack[vm.sp-1])
  1664. r := cmp(right, left)
  1665. if r == _undefined || r == valueTrue {
  1666. vm.stack[vm.sp-2] = valueFalse
  1667. } else {
  1668. vm.stack[vm.sp-2] = valueTrue
  1669. }
  1670. vm.sp--
  1671. vm.pc++
  1672. }
  1673. type _op_gt struct{}
  1674. var op_gt _op_gt
  1675. func (_op_gt) exec(vm *vm) {
  1676. left := toPrimitiveNumber(vm.stack[vm.sp-2])
  1677. right := toPrimitiveNumber(vm.stack[vm.sp-1])
  1678. r := cmp(right, left)
  1679. if r == _undefined {
  1680. vm.stack[vm.sp-2] = valueFalse
  1681. } else {
  1682. vm.stack[vm.sp-2] = r
  1683. }
  1684. vm.sp--
  1685. vm.pc++
  1686. }
  1687. type _op_gte struct{}
  1688. var op_gte _op_gte
  1689. func (_op_gte) exec(vm *vm) {
  1690. left := toPrimitiveNumber(vm.stack[vm.sp-2])
  1691. right := toPrimitiveNumber(vm.stack[vm.sp-1])
  1692. r := cmp(left, right)
  1693. if r == _undefined || r == valueTrue {
  1694. vm.stack[vm.sp-2] = valueFalse
  1695. } else {
  1696. vm.stack[vm.sp-2] = valueTrue
  1697. }
  1698. vm.sp--
  1699. vm.pc++
  1700. }
  1701. type _op_eq struct{}
  1702. var op_eq _op_eq
  1703. func (_op_eq) exec(vm *vm) {
  1704. if vm.stack[vm.sp-2].Equals(vm.stack[vm.sp-1]) {
  1705. vm.stack[vm.sp-2] = valueTrue
  1706. } else {
  1707. vm.stack[vm.sp-2] = valueFalse
  1708. }
  1709. vm.sp--
  1710. vm.pc++
  1711. }
  1712. type _op_neq struct{}
  1713. var op_neq _op_neq
  1714. func (_op_neq) exec(vm *vm) {
  1715. if vm.stack[vm.sp-2].Equals(vm.stack[vm.sp-1]) {
  1716. vm.stack[vm.sp-2] = valueFalse
  1717. } else {
  1718. vm.stack[vm.sp-2] = valueTrue
  1719. }
  1720. vm.sp--
  1721. vm.pc++
  1722. }
  1723. type _op_strict_eq struct{}
  1724. var op_strict_eq _op_strict_eq
  1725. func (_op_strict_eq) exec(vm *vm) {
  1726. if vm.stack[vm.sp-2].StrictEquals(vm.stack[vm.sp-1]) {
  1727. vm.stack[vm.sp-2] = valueTrue
  1728. } else {
  1729. vm.stack[vm.sp-2] = valueFalse
  1730. }
  1731. vm.sp--
  1732. vm.pc++
  1733. }
  1734. type _op_strict_neq struct{}
  1735. var op_strict_neq _op_strict_neq
  1736. func (_op_strict_neq) exec(vm *vm) {
  1737. if vm.stack[vm.sp-2].StrictEquals(vm.stack[vm.sp-1]) {
  1738. vm.stack[vm.sp-2] = valueFalse
  1739. } else {
  1740. vm.stack[vm.sp-2] = valueTrue
  1741. }
  1742. vm.sp--
  1743. vm.pc++
  1744. }
  1745. type _op_instanceof struct{}
  1746. var op_instanceof _op_instanceof
  1747. func (_op_instanceof) exec(vm *vm) {
  1748. left := vm.stack[vm.sp-2]
  1749. right := vm.r.toObject(vm.stack[vm.sp-1])
  1750. if instanceOfOperator(left, right) {
  1751. vm.stack[vm.sp-2] = valueTrue
  1752. } else {
  1753. vm.stack[vm.sp-2] = valueFalse
  1754. }
  1755. vm.sp--
  1756. vm.pc++
  1757. }
  1758. type _op_in struct{}
  1759. var op_in _op_in
  1760. func (_op_in) exec(vm *vm) {
  1761. left := vm.stack[vm.sp-2]
  1762. right := vm.r.toObject(vm.stack[vm.sp-1])
  1763. if right.hasProperty(left) {
  1764. vm.stack[vm.sp-2] = valueTrue
  1765. } else {
  1766. vm.stack[vm.sp-2] = valueFalse
  1767. }
  1768. vm.sp--
  1769. vm.pc++
  1770. }
  1771. type try struct {
  1772. catchOffset int32
  1773. finallyOffset int32
  1774. dynamic bool
  1775. }
  1776. func (t try) exec(vm *vm) {
  1777. o := vm.pc
  1778. vm.pc++
  1779. ex := vm.runTry()
  1780. if ex != nil && t.catchOffset > 0 {
  1781. // run the catch block (in try)
  1782. vm.pc = o + int(t.catchOffset)
  1783. // TODO: if ex.val is an Error, set the stack property
  1784. if t.dynamic {
  1785. vm.newStash()
  1786. vm.stash.putByIdx(0, ex.val)
  1787. } else {
  1788. vm.push(ex.val)
  1789. }
  1790. ex = vm.runTry()
  1791. if t.dynamic {
  1792. vm.stash = vm.stash.outer
  1793. }
  1794. }
  1795. if t.finallyOffset > 0 {
  1796. pc := vm.pc
  1797. // Run finally
  1798. vm.pc = o + int(t.finallyOffset)
  1799. vm.run()
  1800. if vm.prg.code[vm.pc] == retFinally {
  1801. vm.pc = pc
  1802. } else {
  1803. // break or continue out of finally, dropping exception
  1804. ex = nil
  1805. }
  1806. }
  1807. vm.halt = false
  1808. if ex != nil {
  1809. panic(ex)
  1810. }
  1811. }
  1812. type _retFinally struct{}
  1813. var retFinally _retFinally
  1814. func (_retFinally) exec(vm *vm) {
  1815. vm.pc++
  1816. }
  1817. type enterCatch string
  1818. func (varName enterCatch) exec(vm *vm) {
  1819. vm.stash.names = map[string]uint32{
  1820. string(varName): 0,
  1821. }
  1822. vm.pc++
  1823. }
  1824. type _throw struct{}
  1825. var throw _throw
  1826. func (_throw) exec(vm *vm) {
  1827. panic(vm.stack[vm.sp-1])
  1828. }
  1829. type _new uint32
  1830. func (n _new) exec(vm *vm) {
  1831. sp := vm.sp - int(n)
  1832. obj := vm.stack[sp-1]
  1833. ctor := vm.r.toConstructor(obj)
  1834. vm.stack[sp-1] = ctor(vm.stack[sp:vm.sp], nil)
  1835. vm.sp = sp
  1836. vm.pc++
  1837. }
  1838. type _loadNewTarget struct{}
  1839. var loadNewTarget _loadNewTarget
  1840. func (_loadNewTarget) exec(vm *vm) {
  1841. if t := vm.newTarget; t != nil {
  1842. vm.push(t)
  1843. } else {
  1844. vm.push(_undefined)
  1845. }
  1846. vm.pc++
  1847. }
  1848. type _typeof struct{}
  1849. var typeof _typeof
  1850. func (_typeof) exec(vm *vm) {
  1851. var r Value
  1852. switch v := vm.stack[vm.sp-1].(type) {
  1853. case valueUndefined, valueUnresolved:
  1854. r = stringUndefined
  1855. case valueNull:
  1856. r = stringObjectC
  1857. case *Object:
  1858. repeat:
  1859. switch s := v.self.(type) {
  1860. case *funcObject, *nativeFuncObject, *boundFuncObject:
  1861. r = stringFunction
  1862. case *lazyObject:
  1863. v.self = s.create(v)
  1864. goto repeat
  1865. default:
  1866. r = stringObjectC
  1867. }
  1868. case valueBool:
  1869. r = stringBoolean
  1870. case valueString:
  1871. r = stringString
  1872. case valueInt, valueFloat:
  1873. r = stringNumber
  1874. case *valueSymbol:
  1875. r = stringSymbol
  1876. default:
  1877. panic(fmt.Errorf("Unknown type: %T", v))
  1878. }
  1879. vm.stack[vm.sp-1] = r
  1880. vm.pc++
  1881. }
  1882. type createArgs uint32
  1883. func (formalArgs createArgs) exec(vm *vm) {
  1884. v := &Object{runtime: vm.r}
  1885. args := &argumentsObject{}
  1886. args.extensible = true
  1887. args.prototype = vm.r.global.ObjectPrototype
  1888. args.class = "Arguments"
  1889. v.self = args
  1890. args.val = v
  1891. args.length = vm.args
  1892. args.init()
  1893. i := 0
  1894. c := int(formalArgs)
  1895. if vm.args < c {
  1896. c = vm.args
  1897. }
  1898. for ; i < c; i++ {
  1899. args._put(strconv.Itoa(i), &mappedProperty{
  1900. valueProperty: valueProperty{
  1901. writable: true,
  1902. configurable: true,
  1903. enumerable: true,
  1904. },
  1905. v: &vm.stash.values[i],
  1906. })
  1907. }
  1908. for _, v := range vm.stash.extraArgs {
  1909. args._put(strconv.Itoa(i), v)
  1910. i++
  1911. }
  1912. args._putProp("callee", vm.stack[vm.sb-1], true, false, true)
  1913. vm.push(v)
  1914. vm.pc++
  1915. }
  1916. type createArgsStrict uint32
  1917. func (formalArgs createArgsStrict) exec(vm *vm) {
  1918. args := vm.r.newBaseObject(vm.r.global.ObjectPrototype, "Arguments")
  1919. i := 0
  1920. c := int(formalArgs)
  1921. if vm.args < c {
  1922. c = vm.args
  1923. }
  1924. for _, v := range vm.stash.values[:c] {
  1925. args._put(strconv.Itoa(i), v)
  1926. i++
  1927. }
  1928. for _, v := range vm.stash.extraArgs {
  1929. args._put(strconv.Itoa(i), v)
  1930. i++
  1931. }
  1932. args._putProp("length", intToValue(int64(vm.args)), true, false, true)
  1933. args._put("callee", vm.r.global.throwerProperty)
  1934. args._put("caller", vm.r.global.throwerProperty)
  1935. vm.push(args.val)
  1936. vm.pc++
  1937. }
  1938. type _enterWith struct{}
  1939. var enterWith _enterWith
  1940. func (_enterWith) exec(vm *vm) {
  1941. vm.newStash()
  1942. vm.stash.obj = vm.stack[vm.sp-1].ToObject(vm.r).self
  1943. vm.sp--
  1944. vm.pc++
  1945. }
  1946. type _leaveWith struct{}
  1947. var leaveWith _leaveWith
  1948. func (_leaveWith) exec(vm *vm) {
  1949. vm.stash = vm.stash.outer
  1950. vm.pc++
  1951. }
  1952. func emptyIter() (propIterItem, iterNextFunc) {
  1953. return propIterItem{}, nil
  1954. }
  1955. type _enumerate struct{}
  1956. var enumerate _enumerate
  1957. func (_enumerate) exec(vm *vm) {
  1958. v := vm.stack[vm.sp-1]
  1959. if v == _undefined || v == _null {
  1960. vm.iterStack = append(vm.iterStack, iterStackItem{f: emptyIter})
  1961. } else {
  1962. vm.iterStack = append(vm.iterStack, iterStackItem{f: v.ToObject(vm.r).self.enumerate()})
  1963. }
  1964. vm.sp--
  1965. vm.pc++
  1966. }
  1967. type enumNext int32
  1968. func (jmp enumNext) exec(vm *vm) {
  1969. l := len(vm.iterStack) - 1
  1970. item, n := vm.iterStack[l].f()
  1971. if n != nil {
  1972. vm.iterStack[l].val = newStringValue(item.name)
  1973. vm.iterStack[l].f = n
  1974. vm.pc++
  1975. } else {
  1976. vm.pc += int(jmp)
  1977. }
  1978. }
  1979. type _enumGet struct{}
  1980. var enumGet _enumGet
  1981. func (_enumGet) exec(vm *vm) {
  1982. l := len(vm.iterStack) - 1
  1983. vm.push(vm.iterStack[l].val)
  1984. vm.pc++
  1985. }
  1986. type _enumPop struct{}
  1987. var enumPop _enumPop
  1988. func (_enumPop) exec(vm *vm) {
  1989. l := len(vm.iterStack) - 1
  1990. vm.iterStack[l] = iterStackItem{}
  1991. vm.iterStack = vm.iterStack[:l]
  1992. vm.pc++
  1993. }