vm.go 42 KB

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