vm.go 75 KB

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