compiler_expr.go 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305
  1. package goja
  2. import (
  3. "fmt"
  4. "regexp"
  5. "github.com/dop251/goja/ast"
  6. "github.com/dop251/goja/file"
  7. "github.com/dop251/goja/token"
  8. "github.com/dop251/goja/unistring"
  9. )
  10. var (
  11. octalRegexp = regexp.MustCompile(`^0[0-7]`)
  12. )
  13. type compiledExpr interface {
  14. emitGetter(putOnStack bool)
  15. emitSetter(valueExpr compiledExpr, putOnStack bool)
  16. emitRef()
  17. emitUnary(prepare, body func(), postfix, putOnStack bool)
  18. deleteExpr() compiledExpr
  19. constant() bool
  20. addSrcMap()
  21. }
  22. type compiledExprOrRef interface {
  23. compiledExpr
  24. emitGetterOrRef()
  25. }
  26. type compiledCallExpr struct {
  27. baseCompiledExpr
  28. args []compiledExpr
  29. callee compiledExpr
  30. isVariadic bool
  31. }
  32. type compiledNewExpr struct {
  33. compiledCallExpr
  34. }
  35. type compiledObjectLiteral struct {
  36. baseCompiledExpr
  37. expr *ast.ObjectLiteral
  38. }
  39. type compiledArrayLiteral struct {
  40. baseCompiledExpr
  41. expr *ast.ArrayLiteral
  42. }
  43. type compiledRegexpLiteral struct {
  44. baseCompiledExpr
  45. expr *ast.RegExpLiteral
  46. }
  47. type compiledLiteral struct {
  48. baseCompiledExpr
  49. val Value
  50. }
  51. type compiledAssignExpr struct {
  52. baseCompiledExpr
  53. left, right compiledExpr
  54. operator token.Token
  55. }
  56. type compiledObjectAssignmentPattern struct {
  57. baseCompiledExpr
  58. expr *ast.ObjectPattern
  59. }
  60. type compiledArrayAssignmentPattern struct {
  61. baseCompiledExpr
  62. expr *ast.ArrayPattern
  63. }
  64. type deleteGlobalExpr struct {
  65. baseCompiledExpr
  66. name unistring.String
  67. }
  68. type deleteVarExpr struct {
  69. baseCompiledExpr
  70. name unistring.String
  71. }
  72. type deletePropExpr struct {
  73. baseCompiledExpr
  74. left compiledExpr
  75. name unistring.String
  76. }
  77. type deleteElemExpr struct {
  78. baseCompiledExpr
  79. left, member compiledExpr
  80. }
  81. type constantExpr struct {
  82. baseCompiledExpr
  83. val Value
  84. }
  85. type baseCompiledExpr struct {
  86. c *compiler
  87. offset int
  88. }
  89. type compiledIdentifierExpr struct {
  90. baseCompiledExpr
  91. name unistring.String
  92. }
  93. type compiledFunctionLiteral struct {
  94. baseCompiledExpr
  95. name *ast.Identifier
  96. parameterList *ast.ParameterList
  97. body []ast.Statement
  98. source string
  99. declarationList []*ast.VariableDeclaration
  100. lhsName unistring.String
  101. strict *ast.StringLiteral
  102. isExpr bool
  103. isArrow bool
  104. }
  105. type compiledBracketExpr struct {
  106. baseCompiledExpr
  107. left, member compiledExpr
  108. }
  109. type compiledThisExpr struct {
  110. baseCompiledExpr
  111. }
  112. type compiledNewTarget struct {
  113. baseCompiledExpr
  114. }
  115. type compiledSequenceExpr struct {
  116. baseCompiledExpr
  117. sequence []compiledExpr
  118. }
  119. type compiledUnaryExpr struct {
  120. baseCompiledExpr
  121. operand compiledExpr
  122. operator token.Token
  123. postfix bool
  124. }
  125. type compiledConditionalExpr struct {
  126. baseCompiledExpr
  127. test, consequent, alternate compiledExpr
  128. }
  129. type compiledLogicalOr struct {
  130. baseCompiledExpr
  131. left, right compiledExpr
  132. }
  133. type compiledLogicalAnd struct {
  134. baseCompiledExpr
  135. left, right compiledExpr
  136. }
  137. type compiledBinaryExpr struct {
  138. baseCompiledExpr
  139. left, right compiledExpr
  140. operator token.Token
  141. }
  142. type compiledEnumGetExpr struct {
  143. baseCompiledExpr
  144. }
  145. type defaultDeleteExpr struct {
  146. baseCompiledExpr
  147. expr compiledExpr
  148. }
  149. type compiledSpreadCallArgument struct {
  150. baseCompiledExpr
  151. expr compiledExpr
  152. }
  153. func (e *defaultDeleteExpr) emitGetter(putOnStack bool) {
  154. e.expr.emitGetter(false)
  155. if putOnStack {
  156. e.c.emit(loadVal(e.c.p.defineLiteralValue(valueTrue)))
  157. }
  158. }
  159. func (c *compiler) compileExpression(v ast.Expression) compiledExpr {
  160. // log.Printf("compileExpression: %T", v)
  161. switch v := v.(type) {
  162. case nil:
  163. return nil
  164. case *ast.AssignExpression:
  165. return c.compileAssignExpression(v)
  166. case *ast.NumberLiteral:
  167. return c.compileNumberLiteral(v)
  168. case *ast.StringLiteral:
  169. return c.compileStringLiteral(v)
  170. case *ast.BooleanLiteral:
  171. return c.compileBooleanLiteral(v)
  172. case *ast.NullLiteral:
  173. r := &compiledLiteral{
  174. val: _null,
  175. }
  176. r.init(c, v.Idx0())
  177. return r
  178. case *ast.Identifier:
  179. return c.compileIdentifierExpression(v)
  180. case *ast.CallExpression:
  181. return c.compileCallExpression(v)
  182. case *ast.ObjectLiteral:
  183. return c.compileObjectLiteral(v)
  184. case *ast.ArrayLiteral:
  185. return c.compileArrayLiteral(v)
  186. case *ast.RegExpLiteral:
  187. return c.compileRegexpLiteral(v)
  188. case *ast.BinaryExpression:
  189. return c.compileBinaryExpression(v)
  190. case *ast.UnaryExpression:
  191. return c.compileUnaryExpression(v)
  192. case *ast.ConditionalExpression:
  193. return c.compileConditionalExpression(v)
  194. case *ast.FunctionLiteral:
  195. return c.compileFunctionLiteral(v, true)
  196. case *ast.ArrowFunctionLiteral:
  197. return c.compileArrowFunctionLiteral(v)
  198. case *ast.DotExpression:
  199. r := &compiledDotExpr{
  200. left: c.compileExpression(v.Left),
  201. name: v.Identifier.Name,
  202. }
  203. r.init(c, v.Idx0())
  204. return r
  205. case *ast.BracketExpression:
  206. r := &compiledBracketExpr{
  207. left: c.compileExpression(v.Left),
  208. member: c.compileExpression(v.Member),
  209. }
  210. r.init(c, v.Idx0())
  211. return r
  212. case *ast.ThisExpression:
  213. r := &compiledThisExpr{}
  214. r.init(c, v.Idx0())
  215. return r
  216. case *ast.SequenceExpression:
  217. return c.compileSequenceExpression(v)
  218. case *ast.NewExpression:
  219. return c.compileNewExpression(v)
  220. case *ast.MetaProperty:
  221. return c.compileMetaProperty(v)
  222. case *ast.ObjectPattern:
  223. return c.compileObjectAssignmentPattern(v)
  224. case *ast.ArrayPattern:
  225. return c.compileArrayAssignmentPattern(v)
  226. default:
  227. panic(fmt.Errorf("Unknown expression type: %T", v))
  228. }
  229. }
  230. func (e *baseCompiledExpr) constant() bool {
  231. return false
  232. }
  233. func (e *baseCompiledExpr) init(c *compiler, idx file.Idx) {
  234. e.c = c
  235. e.offset = int(idx) - 1
  236. }
  237. func (e *baseCompiledExpr) emitSetter(compiledExpr, bool) {
  238. e.c.throwSyntaxError(e.offset, "Not a valid left-value expression")
  239. }
  240. func (e *baseCompiledExpr) emitRef() {
  241. e.c.throwSyntaxError(e.offset, "Cannot emit reference for this type of expression")
  242. }
  243. func (e *baseCompiledExpr) deleteExpr() compiledExpr {
  244. r := &constantExpr{
  245. val: valueTrue,
  246. }
  247. r.init(e.c, file.Idx(e.offset+1))
  248. return r
  249. }
  250. func (e *baseCompiledExpr) emitUnary(func(), func(), bool, bool) {
  251. e.c.throwSyntaxError(e.offset, "Not a valid left-value expression")
  252. }
  253. func (e *baseCompiledExpr) addSrcMap() {
  254. if e.offset > 0 {
  255. e.c.p.srcMap = append(e.c.p.srcMap, srcMapItem{pc: len(e.c.p.code), srcPos: e.offset})
  256. }
  257. }
  258. func (e *constantExpr) emitGetter(putOnStack bool) {
  259. if putOnStack {
  260. e.addSrcMap()
  261. e.c.emit(loadVal(e.c.p.defineLiteralValue(e.val)))
  262. }
  263. }
  264. func (e *compiledIdentifierExpr) emitGetter(putOnStack bool) {
  265. e.addSrcMap()
  266. if b, noDynamics := e.c.scope.lookupName(e.name); noDynamics {
  267. if b != nil {
  268. if putOnStack {
  269. b.emitGet()
  270. } else {
  271. b.emitGetP()
  272. }
  273. } else {
  274. panic("No dynamics and not found")
  275. }
  276. } else {
  277. if b != nil {
  278. b.emitGetVar(false)
  279. } else {
  280. e.c.emit(loadDynamic(e.name))
  281. }
  282. if !putOnStack {
  283. e.c.emit(pop)
  284. }
  285. }
  286. }
  287. func (e *compiledIdentifierExpr) emitGetterOrRef() {
  288. e.addSrcMap()
  289. if b, noDynamics := e.c.scope.lookupName(e.name); noDynamics {
  290. if b != nil {
  291. b.emitGet()
  292. } else {
  293. panic("No dynamics and not found")
  294. }
  295. } else {
  296. if b != nil {
  297. b.emitGetVar(false)
  298. } else {
  299. e.c.emit(loadDynamicRef(e.name))
  300. }
  301. }
  302. }
  303. func (e *compiledIdentifierExpr) emitGetterAndCallee() {
  304. e.addSrcMap()
  305. if b, noDynamics := e.c.scope.lookupName(e.name); noDynamics {
  306. if b != nil {
  307. e.c.emit(loadUndef)
  308. b.emitGet()
  309. } else {
  310. panic("No dynamics and not found")
  311. }
  312. } else {
  313. if b != nil {
  314. b.emitGetVar(true)
  315. } else {
  316. e.c.emit(loadDynamicCallee(e.name))
  317. }
  318. }
  319. }
  320. func (c *compiler) emitVarSetter1(name unistring.String, offset int, putOnStack bool, emitRight func(isRef bool)) {
  321. if c.scope.strict {
  322. c.checkIdentifierLName(name, offset)
  323. }
  324. if b, noDynamics := c.scope.lookupName(name); noDynamics {
  325. emitRight(false)
  326. if b != nil {
  327. if putOnStack {
  328. b.emitSet()
  329. } else {
  330. b.emitSetP()
  331. }
  332. } else {
  333. if c.scope.strict {
  334. c.emit(setGlobalStrict(name))
  335. } else {
  336. c.emit(setGlobal(name))
  337. }
  338. if !putOnStack {
  339. c.emit(pop)
  340. }
  341. }
  342. } else {
  343. if b != nil {
  344. b.emitResolveVar(c.scope.strict)
  345. } else {
  346. if c.scope.strict {
  347. c.emit(resolveVar1Strict(name))
  348. } else {
  349. c.emit(resolveVar1(name))
  350. }
  351. }
  352. emitRight(true)
  353. if putOnStack {
  354. c.emit(putValue)
  355. } else {
  356. c.emit(putValueP)
  357. }
  358. }
  359. }
  360. func (c *compiler) emitVarSetter(name unistring.String, offset int, valueExpr compiledExpr, putOnStack bool) {
  361. c.emitVarSetter1(name, offset, putOnStack, func(bool) {
  362. c.emitExpr(valueExpr, true)
  363. })
  364. }
  365. func (c *compiler) emitVarRef(name unistring.String, offset int) {
  366. if c.scope.strict {
  367. c.checkIdentifierLName(name, offset)
  368. }
  369. b, _ := c.scope.lookupName(name)
  370. if b != nil {
  371. b.emitResolveVar(c.scope.strict)
  372. } else {
  373. if c.scope.strict {
  374. c.emit(resolveVar1Strict(name))
  375. } else {
  376. c.emit(resolveVar1(name))
  377. }
  378. }
  379. }
  380. func (e *compiledIdentifierExpr) emitRef() {
  381. e.c.emitVarRef(e.name, e.offset)
  382. }
  383. func (e *compiledIdentifierExpr) emitSetter(valueExpr compiledExpr, putOnStack bool) {
  384. e.c.emitVarSetter(e.name, e.offset, valueExpr, putOnStack)
  385. }
  386. func (e *compiledIdentifierExpr) emitUnary(prepare, body func(), postfix, putOnStack bool) {
  387. if putOnStack {
  388. e.c.emitVarSetter1(e.name, e.offset, true, func(isRef bool) {
  389. e.c.emit(loadUndef)
  390. if isRef {
  391. e.c.emit(getValue)
  392. } else {
  393. e.emitGetter(true)
  394. }
  395. if prepare != nil {
  396. prepare()
  397. }
  398. if !postfix {
  399. body()
  400. }
  401. e.c.emit(rdupN(1))
  402. if postfix {
  403. body()
  404. }
  405. })
  406. e.c.emit(pop)
  407. } else {
  408. e.c.emitVarSetter1(e.name, e.offset, false, func(isRef bool) {
  409. if isRef {
  410. e.c.emit(getValue)
  411. } else {
  412. e.emitGetter(true)
  413. }
  414. body()
  415. })
  416. }
  417. }
  418. func (e *compiledIdentifierExpr) deleteExpr() compiledExpr {
  419. if e.c.scope.strict {
  420. e.c.throwSyntaxError(e.offset, "Delete of an unqualified identifier in strict mode")
  421. panic("Unreachable")
  422. }
  423. if b, noDynamics := e.c.scope.lookupName(e.name); noDynamics {
  424. if b == nil {
  425. r := &deleteGlobalExpr{
  426. name: e.name,
  427. }
  428. r.init(e.c, file.Idx(0))
  429. return r
  430. }
  431. } else {
  432. if b == nil {
  433. r := &deleteVarExpr{
  434. name: e.name,
  435. }
  436. r.init(e.c, file.Idx(e.offset+1))
  437. return r
  438. }
  439. }
  440. r := &compiledLiteral{
  441. val: valueFalse,
  442. }
  443. r.init(e.c, file.Idx(e.offset+1))
  444. return r
  445. }
  446. type compiledDotExpr struct {
  447. baseCompiledExpr
  448. left compiledExpr
  449. name unistring.String
  450. }
  451. func (e *compiledDotExpr) emitGetter(putOnStack bool) {
  452. e.left.emitGetter(true)
  453. e.addSrcMap()
  454. e.c.emit(getProp(e.name))
  455. if !putOnStack {
  456. e.c.emit(pop)
  457. }
  458. }
  459. func (e *compiledDotExpr) emitRef() {
  460. e.left.emitGetter(true)
  461. if e.c.scope.strict {
  462. e.c.emit(getPropRefStrict(e.name))
  463. } else {
  464. e.c.emit(getPropRef(e.name))
  465. }
  466. }
  467. func (e *compiledDotExpr) emitSetter(valueExpr compiledExpr, putOnStack bool) {
  468. e.left.emitGetter(true)
  469. valueExpr.emitGetter(true)
  470. if e.c.scope.strict {
  471. if putOnStack {
  472. e.c.emit(setPropStrict(e.name))
  473. } else {
  474. e.c.emit(setPropStrictP(e.name))
  475. }
  476. } else {
  477. if putOnStack {
  478. e.c.emit(setProp(e.name))
  479. } else {
  480. e.c.emit(setPropP(e.name))
  481. }
  482. }
  483. }
  484. func (e *compiledDotExpr) emitUnary(prepare, body func(), postfix, putOnStack bool) {
  485. if !putOnStack {
  486. e.left.emitGetter(true)
  487. e.c.emit(dup)
  488. e.c.emit(getProp(e.name))
  489. body()
  490. if e.c.scope.strict {
  491. e.c.emit(setPropStrict(e.name), pop)
  492. } else {
  493. e.c.emit(setProp(e.name), pop)
  494. }
  495. } else {
  496. if !postfix {
  497. e.left.emitGetter(true)
  498. e.c.emit(dup)
  499. e.c.emit(getProp(e.name))
  500. if prepare != nil {
  501. prepare()
  502. }
  503. body()
  504. if e.c.scope.strict {
  505. e.c.emit(setPropStrict(e.name))
  506. } else {
  507. e.c.emit(setProp(e.name))
  508. }
  509. } else {
  510. e.c.emit(loadUndef)
  511. e.left.emitGetter(true)
  512. e.c.emit(dup)
  513. e.c.emit(getProp(e.name))
  514. if prepare != nil {
  515. prepare()
  516. }
  517. e.c.emit(rdupN(2))
  518. body()
  519. if e.c.scope.strict {
  520. e.c.emit(setPropStrict(e.name))
  521. } else {
  522. e.c.emit(setProp(e.name))
  523. }
  524. e.c.emit(pop)
  525. }
  526. }
  527. }
  528. func (e *compiledDotExpr) deleteExpr() compiledExpr {
  529. r := &deletePropExpr{
  530. left: e.left,
  531. name: e.name,
  532. }
  533. r.init(e.c, file.Idx(0))
  534. return r
  535. }
  536. func (e *compiledBracketExpr) emitGetter(putOnStack bool) {
  537. e.left.emitGetter(true)
  538. e.member.emitGetter(true)
  539. e.addSrcMap()
  540. e.c.emit(getElem)
  541. if !putOnStack {
  542. e.c.emit(pop)
  543. }
  544. }
  545. func (e *compiledBracketExpr) emitRef() {
  546. e.left.emitGetter(true)
  547. e.member.emitGetter(true)
  548. if e.c.scope.strict {
  549. e.c.emit(getElemRefStrict)
  550. } else {
  551. e.c.emit(getElemRef)
  552. }
  553. }
  554. func (e *compiledBracketExpr) emitSetter(valueExpr compiledExpr, putOnStack bool) {
  555. e.left.emitGetter(true)
  556. e.member.emitGetter(true)
  557. valueExpr.emitGetter(true)
  558. if e.c.scope.strict {
  559. if putOnStack {
  560. e.c.emit(setElemStrict)
  561. } else {
  562. e.c.emit(setElemStrictP)
  563. }
  564. } else {
  565. if putOnStack {
  566. e.c.emit(setElem)
  567. } else {
  568. e.c.emit(setElemP)
  569. }
  570. }
  571. }
  572. func (e *compiledBracketExpr) emitUnary(prepare, body func(), postfix, putOnStack bool) {
  573. if !putOnStack {
  574. e.left.emitGetter(true)
  575. e.member.emitGetter(true)
  576. e.c.emit(dupN(1), dupN(1))
  577. e.c.emit(getElem)
  578. body()
  579. if e.c.scope.strict {
  580. e.c.emit(setElemStrict, pop)
  581. } else {
  582. e.c.emit(setElem, pop)
  583. }
  584. } else {
  585. if !postfix {
  586. e.left.emitGetter(true)
  587. e.member.emitGetter(true)
  588. e.c.emit(dupN(1), dupN(1))
  589. e.c.emit(getElem)
  590. if prepare != nil {
  591. prepare()
  592. }
  593. body()
  594. if e.c.scope.strict {
  595. e.c.emit(setElemStrict)
  596. } else {
  597. e.c.emit(setElem)
  598. }
  599. } else {
  600. e.c.emit(loadUndef)
  601. e.left.emitGetter(true)
  602. e.member.emitGetter(true)
  603. e.c.emit(dupN(1), dupN(1))
  604. e.c.emit(getElem)
  605. if prepare != nil {
  606. prepare()
  607. }
  608. e.c.emit(rdupN(3))
  609. body()
  610. if e.c.scope.strict {
  611. e.c.emit(setElemStrict, pop)
  612. } else {
  613. e.c.emit(setElem, pop)
  614. }
  615. }
  616. }
  617. }
  618. func (e *compiledBracketExpr) deleteExpr() compiledExpr {
  619. r := &deleteElemExpr{
  620. left: e.left,
  621. member: e.member,
  622. }
  623. r.init(e.c, file.Idx(0))
  624. return r
  625. }
  626. func (e *deleteElemExpr) emitGetter(putOnStack bool) {
  627. e.left.emitGetter(true)
  628. e.member.emitGetter(true)
  629. e.addSrcMap()
  630. if e.c.scope.strict {
  631. e.c.emit(deleteElemStrict)
  632. } else {
  633. e.c.emit(deleteElem)
  634. }
  635. if !putOnStack {
  636. e.c.emit(pop)
  637. }
  638. }
  639. func (e *deletePropExpr) emitGetter(putOnStack bool) {
  640. e.left.emitGetter(true)
  641. e.addSrcMap()
  642. if e.c.scope.strict {
  643. e.c.emit(deletePropStrict(e.name))
  644. } else {
  645. e.c.emit(deleteProp(e.name))
  646. }
  647. if !putOnStack {
  648. e.c.emit(pop)
  649. }
  650. }
  651. func (e *deleteVarExpr) emitGetter(putOnStack bool) {
  652. /*if e.c.scope.strict {
  653. e.c.throwSyntaxError(e.offset, "Delete of an unqualified identifier in strict mode")
  654. return
  655. }*/
  656. e.c.emit(deleteVar(e.name))
  657. if !putOnStack {
  658. e.c.emit(pop)
  659. }
  660. }
  661. func (e *deleteGlobalExpr) emitGetter(putOnStack bool) {
  662. /*if e.c.scope.strict {
  663. e.c.throwSyntaxError(e.offset, "Delete of an unqualified identifier in strict mode")
  664. return
  665. }*/
  666. e.c.emit(deleteGlobal(e.name))
  667. if !putOnStack {
  668. e.c.emit(pop)
  669. }
  670. }
  671. func (e *compiledAssignExpr) emitGetter(putOnStack bool) {
  672. e.addSrcMap()
  673. switch e.operator {
  674. case token.ASSIGN:
  675. if fn, ok := e.right.(*compiledFunctionLiteral); ok {
  676. if fn.name == nil {
  677. if id, ok := e.left.(*compiledIdentifierExpr); ok {
  678. fn.lhsName = id.name
  679. }
  680. }
  681. }
  682. e.left.emitSetter(e.right, putOnStack)
  683. case token.PLUS:
  684. e.left.emitUnary(nil, func() {
  685. e.right.emitGetter(true)
  686. e.c.emit(add)
  687. }, false, putOnStack)
  688. case token.MINUS:
  689. e.left.emitUnary(nil, func() {
  690. e.right.emitGetter(true)
  691. e.c.emit(sub)
  692. }, false, putOnStack)
  693. case token.MULTIPLY:
  694. e.left.emitUnary(nil, func() {
  695. e.right.emitGetter(true)
  696. e.c.emit(mul)
  697. }, false, putOnStack)
  698. case token.SLASH:
  699. e.left.emitUnary(nil, func() {
  700. e.right.emitGetter(true)
  701. e.c.emit(div)
  702. }, false, putOnStack)
  703. case token.REMAINDER:
  704. e.left.emitUnary(nil, func() {
  705. e.right.emitGetter(true)
  706. e.c.emit(mod)
  707. }, false, putOnStack)
  708. case token.OR:
  709. e.left.emitUnary(nil, func() {
  710. e.right.emitGetter(true)
  711. e.c.emit(or)
  712. }, false, putOnStack)
  713. case token.AND:
  714. e.left.emitUnary(nil, func() {
  715. e.right.emitGetter(true)
  716. e.c.emit(and)
  717. }, false, putOnStack)
  718. case token.EXCLUSIVE_OR:
  719. e.left.emitUnary(nil, func() {
  720. e.right.emitGetter(true)
  721. e.c.emit(xor)
  722. }, false, putOnStack)
  723. case token.SHIFT_LEFT:
  724. e.left.emitUnary(nil, func() {
  725. e.right.emitGetter(true)
  726. e.c.emit(sal)
  727. }, false, putOnStack)
  728. case token.SHIFT_RIGHT:
  729. e.left.emitUnary(nil, func() {
  730. e.right.emitGetter(true)
  731. e.c.emit(sar)
  732. }, false, putOnStack)
  733. case token.UNSIGNED_SHIFT_RIGHT:
  734. e.left.emitUnary(nil, func() {
  735. e.right.emitGetter(true)
  736. e.c.emit(shr)
  737. }, false, putOnStack)
  738. default:
  739. panic(fmt.Errorf("Unknown assign operator: %s", e.operator.String()))
  740. }
  741. }
  742. func (e *compiledLiteral) emitGetter(putOnStack bool) {
  743. if putOnStack {
  744. e.addSrcMap()
  745. e.c.emit(loadVal(e.c.p.defineLiteralValue(e.val)))
  746. }
  747. }
  748. func (e *compiledLiteral) constant() bool {
  749. return true
  750. }
  751. func (c *compiler) compileParameterBindingIdentifier(name unistring.String, offset int) (*binding, bool) {
  752. if c.scope.strict {
  753. c.checkIdentifierName(name, offset)
  754. c.checkIdentifierLName(name, offset)
  755. }
  756. return c.scope.bindNameShadow(name)
  757. }
  758. func (c *compiler) compileParameterPatternIdBinding(name unistring.String, offset int) {
  759. if _, unique := c.compileParameterBindingIdentifier(name, offset); !unique {
  760. c.throwSyntaxError(offset, "Duplicate parameter name not allowed in this context")
  761. }
  762. }
  763. func (c *compiler) compileParameterPatternBinding(item ast.Expression) {
  764. c.createBindings(item, c.compileParameterPatternIdBinding)
  765. }
  766. func (e *compiledFunctionLiteral) emitGetter(putOnStack bool) {
  767. savedPrg := e.c.p
  768. e.c.p = &Program{
  769. src: e.c.p.src,
  770. }
  771. e.c.newScope()
  772. s := e.c.scope
  773. s.function = true
  774. s.arrow = e.isArrow
  775. var name unistring.String
  776. if e.name != nil {
  777. name = e.name.Name
  778. } else {
  779. name = e.lhsName
  780. }
  781. if name != "" {
  782. e.c.p.funcName = name
  783. }
  784. savedBlock := e.c.block
  785. defer func() {
  786. e.c.block = savedBlock
  787. }()
  788. e.c.block = &block{
  789. typ: blockScope,
  790. }
  791. if !s.strict {
  792. s.strict = e.strict != nil
  793. }
  794. hasPatterns := false
  795. hasInits := false
  796. firstDupIdx := -1
  797. length := 0
  798. if e.parameterList.Rest != nil {
  799. hasPatterns = true // strictly speaking not, but we need to activate all the checks
  800. }
  801. // First, make sure that the first bindings correspond to the formal parameters
  802. for _, item := range e.parameterList.List {
  803. switch tgt := item.Target.(type) {
  804. case *ast.Identifier:
  805. offset := int(tgt.Idx) - 1
  806. b, unique := e.c.compileParameterBindingIdentifier(tgt.Name, offset)
  807. if !unique {
  808. firstDupIdx = offset
  809. }
  810. b.isArg = true
  811. case ast.Pattern:
  812. b := s.addBinding(int(item.Idx0()) - 1)
  813. b.isArg = true
  814. hasPatterns = true
  815. default:
  816. e.c.throwSyntaxError(int(item.Idx0())-1, "Unsupported BindingElement type: %T", item)
  817. return
  818. }
  819. if item.Initializer != nil {
  820. hasInits = true
  821. }
  822. if firstDupIdx >= 0 && (hasPatterns || hasInits || s.strict || e.isArrow) {
  823. e.c.throwSyntaxError(firstDupIdx, "Duplicate parameter name not allowed in this context")
  824. return
  825. }
  826. if (hasPatterns || hasInits) && e.strict != nil {
  827. e.c.throwSyntaxError(int(e.strict.Idx)-1, "Illegal 'use strict' directive in function with non-simple parameter list")
  828. return
  829. }
  830. if !hasInits {
  831. length++
  832. }
  833. }
  834. // create pattern bindings
  835. if hasPatterns {
  836. for _, item := range e.parameterList.List {
  837. switch tgt := item.Target.(type) {
  838. case *ast.Identifier:
  839. // we already created those in the previous loop, skipping
  840. default:
  841. e.c.compileParameterPatternBinding(tgt)
  842. }
  843. }
  844. if rest := e.parameterList.Rest; rest != nil {
  845. e.c.compileParameterPatternBinding(rest)
  846. }
  847. }
  848. paramsCount := len(e.parameterList.List)
  849. s.numArgs = paramsCount
  850. body := e.body
  851. funcs := e.c.extractFunctions(body)
  852. var calleeBinding *binding
  853. preambleLen := 4 // enter, boxThis, createArgs, set
  854. e.c.p.code = make([]instruction, preambleLen, 8)
  855. emitArgsRestMark := -1
  856. firstForwardRef := -1
  857. enterFunc2Mark := -1
  858. if hasPatterns || hasInits {
  859. if e.isExpr && e.name != nil {
  860. if b, created := s.bindNameLexical(e.name.Name, false, 0); created {
  861. b.isConst = true
  862. calleeBinding = b
  863. }
  864. }
  865. if calleeBinding != nil {
  866. e.c.emit(loadCallee)
  867. calleeBinding.emitInit()
  868. }
  869. for i, item := range e.parameterList.List {
  870. if pattern, ok := item.Target.(ast.Pattern); ok {
  871. i := i
  872. e.c.compilePatternInitExpr(func() {
  873. if firstForwardRef == -1 {
  874. s.bindings[i].emitGet()
  875. } else {
  876. e.c.emit(loadStackLex(-i - 1))
  877. }
  878. }, item.Initializer, item.Target.Idx0()).emitGetter(true)
  879. e.c.emitPattern(pattern, func(target, init compiledExpr) {
  880. e.c.emitPatternLexicalAssign(target, init, false)
  881. }, false)
  882. } else if item.Initializer != nil {
  883. markGet := len(e.c.p.code)
  884. e.c.emit(nil)
  885. mark := len(e.c.p.code)
  886. e.c.emit(nil)
  887. e.c.compileExpression(item.Initializer).emitGetter(true)
  888. if firstForwardRef == -1 && (s.isDynamic() || s.bindings[i].useCount() > 0) {
  889. firstForwardRef = i
  890. }
  891. if firstForwardRef == -1 {
  892. s.bindings[i].emitGetAt(markGet)
  893. } else {
  894. e.c.p.code[markGet] = loadStackLex(-i - 1)
  895. }
  896. s.bindings[i].emitInit()
  897. e.c.p.code[mark] = jdefP(len(e.c.p.code) - mark)
  898. } else {
  899. if firstForwardRef == -1 && s.bindings[i].useCount() > 0 {
  900. firstForwardRef = i
  901. }
  902. if firstForwardRef != -1 {
  903. e.c.emit(loadStackLex(-i - 1))
  904. s.bindings[i].emitInit()
  905. }
  906. }
  907. }
  908. if rest := e.parameterList.Rest; rest != nil {
  909. e.c.emitAssign(rest, e.c.compileEmitterExpr(
  910. func() {
  911. emitArgsRestMark = len(e.c.p.code)
  912. e.c.emit(createArgsRestStack(paramsCount))
  913. }, rest.Idx0()),
  914. func(target, init compiledExpr) {
  915. e.c.emitPatternLexicalAssign(target, init, false)
  916. })
  917. }
  918. if firstForwardRef != -1 {
  919. for _, b := range s.bindings {
  920. b.inStash = true
  921. }
  922. s.argsInStash = true
  923. s.needStash = true
  924. }
  925. e.c.newBlockScope()
  926. varScope := e.c.scope
  927. varScope.variable = true
  928. enterFunc2Mark = len(e.c.p.code)
  929. e.c.emit(nil)
  930. e.c.compileDeclList(e.declarationList, false)
  931. e.c.createFunctionBindings(funcs)
  932. e.c.compileLexicalDeclarationsFuncBody(body, calleeBinding)
  933. for _, b := range varScope.bindings {
  934. if b.isVar {
  935. if parentBinding := s.boundNames[b.name]; parentBinding != nil && parentBinding != calleeBinding {
  936. parentBinding.emitGet()
  937. b.emitSetP()
  938. }
  939. }
  940. }
  941. } else {
  942. // To avoid triggering variable conflict when binding from non-strict direct eval().
  943. // Parameters are supposed to be in a parent scope, hence no conflict.
  944. for _, b := range s.bindings[:paramsCount] {
  945. b.isVar = true
  946. }
  947. e.c.compileDeclList(e.declarationList, true)
  948. e.c.createFunctionBindings(funcs)
  949. e.c.compileLexicalDeclarations(body, true)
  950. if e.isExpr && e.name != nil {
  951. if b, created := s.bindNameLexical(e.name.Name, false, 0); created {
  952. b.isConst = true
  953. calleeBinding = b
  954. }
  955. }
  956. if calleeBinding != nil {
  957. e.c.emit(loadCallee)
  958. calleeBinding.emitInit()
  959. }
  960. }
  961. e.c.compileFunctions(funcs)
  962. e.c.compileStatements(body, false)
  963. var last ast.Statement
  964. if l := len(body); l > 0 {
  965. last = body[l-1]
  966. }
  967. if _, ok := last.(*ast.ReturnStatement); !ok {
  968. e.c.emit(loadUndef, ret)
  969. }
  970. delta := 0
  971. code := e.c.p.code
  972. if calleeBinding != nil && !s.isDynamic() && calleeBinding.useCount() == 1 {
  973. s.deleteBinding(calleeBinding)
  974. preambleLen += 2
  975. }
  976. if !s.argsInStash && (s.argsNeeded || s.isDynamic()) {
  977. s.moveArgsToStash()
  978. }
  979. if s.argsNeeded {
  980. b, created := s.bindNameLexical("arguments", false, 0)
  981. if !created && !b.isVar {
  982. s.argsNeeded = false
  983. } else {
  984. if s.strict {
  985. b.isConst = true
  986. } else {
  987. b.isVar = e.c.scope.function
  988. }
  989. pos := preambleLen - 2
  990. delta += 2
  991. if s.strict || hasPatterns || hasInits {
  992. code[pos] = createArgsUnmapped(paramsCount)
  993. } else {
  994. code[pos] = createArgsMapped(paramsCount)
  995. }
  996. pos++
  997. b.markAccessPointAtScope(s, pos)
  998. code[pos] = storeStashP(0)
  999. }
  1000. }
  1001. stashSize, stackSize := s.finaliseVarAlloc(0)
  1002. if !s.strict && s.thisNeeded {
  1003. delta++
  1004. code[preambleLen-delta] = boxThis
  1005. }
  1006. delta++
  1007. delta = preambleLen - delta
  1008. var enter instruction
  1009. if stashSize > 0 || s.argsInStash {
  1010. if firstForwardRef == -1 {
  1011. enter1 := enterFunc{
  1012. numArgs: uint32(paramsCount),
  1013. argsToStash: s.argsInStash,
  1014. stashSize: uint32(stashSize),
  1015. stackSize: uint32(stackSize),
  1016. extensible: s.dynamic,
  1017. }
  1018. if s.isDynamic() {
  1019. enter1.names = s.makeNamesMap()
  1020. }
  1021. enter = &enter1
  1022. if enterFunc2Mark != -1 {
  1023. ef2 := &enterFuncBody{
  1024. extensible: e.c.scope.dynamic,
  1025. }
  1026. e.c.updateEnterBlock(&ef2.enterBlock)
  1027. e.c.p.code[enterFunc2Mark] = ef2
  1028. }
  1029. } else {
  1030. enter1 := enterFunc1{
  1031. stashSize: uint32(stashSize),
  1032. numArgs: uint32(paramsCount),
  1033. argsToCopy: uint32(firstForwardRef),
  1034. extensible: s.dynamic,
  1035. }
  1036. if s.isDynamic() {
  1037. enter1.names = s.makeNamesMap()
  1038. }
  1039. enter = &enter1
  1040. if enterFunc2Mark != -1 {
  1041. ef2 := &enterFuncBody{
  1042. adjustStack: true,
  1043. extensible: e.c.scope.dynamic,
  1044. }
  1045. e.c.updateEnterBlock(&ef2.enterBlock)
  1046. e.c.p.code[enterFunc2Mark] = ef2
  1047. }
  1048. }
  1049. if emitArgsRestMark != -1 && s.argsInStash {
  1050. e.c.p.code[emitArgsRestMark] = createArgsRestStash
  1051. }
  1052. } else {
  1053. enter = &enterFuncStashless{
  1054. stackSize: uint32(stackSize),
  1055. args: uint32(paramsCount),
  1056. }
  1057. if enterFunc2Mark != -1 {
  1058. ef2 := &enterFuncBody{
  1059. extensible: e.c.scope.dynamic,
  1060. }
  1061. e.c.updateEnterBlock(&ef2.enterBlock)
  1062. e.c.p.code[enterFunc2Mark] = ef2
  1063. }
  1064. }
  1065. code[delta] = enter
  1066. if delta != 0 {
  1067. e.c.p.code = code[delta:]
  1068. for i := range e.c.p.srcMap {
  1069. e.c.p.srcMap[i].pc -= delta
  1070. }
  1071. s.adjustBase(-delta)
  1072. }
  1073. strict := s.strict
  1074. p := e.c.p
  1075. // e.c.p.dumpCode()
  1076. if enterFunc2Mark != -1 {
  1077. e.c.popScope()
  1078. }
  1079. e.c.popScope()
  1080. e.c.p = savedPrg
  1081. if e.isArrow {
  1082. e.c.emit(&newArrowFunc{newFunc: newFunc{prg: p, length: uint32(length), name: name, source: e.source, strict: strict}})
  1083. } else {
  1084. e.c.emit(&newFunc{prg: p, length: uint32(length), name: name, source: e.source, strict: strict})
  1085. }
  1086. if !putOnStack {
  1087. e.c.emit(pop)
  1088. }
  1089. }
  1090. func (c *compiler) compileFunctionLiteral(v *ast.FunctionLiteral, isExpr bool) *compiledFunctionLiteral {
  1091. strictBody := c.isStrictStatement(v.Body)
  1092. if v.Name != nil && (c.scope.strict || strictBody != nil) {
  1093. c.checkIdentifierLName(v.Name.Name, int(v.Name.Idx)-1)
  1094. }
  1095. r := &compiledFunctionLiteral{
  1096. name: v.Name,
  1097. parameterList: v.ParameterList,
  1098. body: v.Body.List,
  1099. source: v.Source,
  1100. declarationList: v.DeclarationList,
  1101. isExpr: isExpr,
  1102. strict: strictBody,
  1103. }
  1104. r.init(c, v.Idx0())
  1105. return r
  1106. }
  1107. func (c *compiler) compileArrowFunctionLiteral(v *ast.ArrowFunctionLiteral) *compiledFunctionLiteral {
  1108. var strictBody *ast.StringLiteral
  1109. var body []ast.Statement
  1110. switch b := v.Body.(type) {
  1111. case *ast.BlockStatement:
  1112. strictBody = c.isStrictStatement(b)
  1113. body = b.List
  1114. case *ast.ExpressionBody:
  1115. body = []ast.Statement{
  1116. &ast.ReturnStatement{
  1117. Argument: b.Expression,
  1118. },
  1119. }
  1120. default:
  1121. c.throwSyntaxError(int(b.Idx0())-1, "Unsupported ConciseBody type: %T", b)
  1122. }
  1123. r := &compiledFunctionLiteral{
  1124. parameterList: v.ParameterList,
  1125. body: body,
  1126. source: v.Source,
  1127. declarationList: v.DeclarationList,
  1128. isExpr: true,
  1129. isArrow: true,
  1130. strict: strictBody,
  1131. }
  1132. r.init(c, v.Idx0())
  1133. return r
  1134. }
  1135. func (e *compiledThisExpr) emitGetter(putOnStack bool) {
  1136. if putOnStack {
  1137. e.addSrcMap()
  1138. scope := e.c.scope
  1139. for ; scope != nil && !scope.function && !scope.eval; scope = scope.outer {
  1140. }
  1141. if scope != nil {
  1142. if !scope.arrow {
  1143. scope.thisNeeded = true
  1144. }
  1145. e.c.emit(loadStack(0))
  1146. } else {
  1147. e.c.emit(loadGlobalObject)
  1148. }
  1149. }
  1150. }
  1151. func (e *compiledNewExpr) emitGetter(putOnStack bool) {
  1152. if e.isVariadic {
  1153. e.c.emit(startVariadic)
  1154. }
  1155. e.callee.emitGetter(true)
  1156. for _, expr := range e.args {
  1157. expr.emitGetter(true)
  1158. }
  1159. e.addSrcMap()
  1160. if e.isVariadic {
  1161. e.c.emit(newVariadic, endVariadic)
  1162. } else {
  1163. e.c.emit(_new(len(e.args)))
  1164. }
  1165. if !putOnStack {
  1166. e.c.emit(pop)
  1167. }
  1168. }
  1169. func (c *compiler) compileCallArgs(list []ast.Expression) (args []compiledExpr, isVariadic bool) {
  1170. args = make([]compiledExpr, len(list))
  1171. for i, argExpr := range list {
  1172. if spread, ok := argExpr.(*ast.SpreadElement); ok {
  1173. args[i] = c.compileSpreadCallArgument(spread)
  1174. isVariadic = true
  1175. } else {
  1176. args[i] = c.compileExpression(argExpr)
  1177. }
  1178. }
  1179. return
  1180. }
  1181. func (c *compiler) compileNewExpression(v *ast.NewExpression) compiledExpr {
  1182. args, isVariadic := c.compileCallArgs(v.ArgumentList)
  1183. r := &compiledNewExpr{
  1184. compiledCallExpr: compiledCallExpr{
  1185. callee: c.compileExpression(v.Callee),
  1186. args: args,
  1187. isVariadic: isVariadic,
  1188. },
  1189. }
  1190. r.init(c, v.Idx0())
  1191. return r
  1192. }
  1193. func (e *compiledNewTarget) emitGetter(putOnStack bool) {
  1194. if putOnStack {
  1195. e.addSrcMap()
  1196. e.c.emit(loadNewTarget)
  1197. }
  1198. }
  1199. func (c *compiler) compileMetaProperty(v *ast.MetaProperty) compiledExpr {
  1200. if v.Meta.Name == "new" || v.Property.Name != "target" {
  1201. r := &compiledNewTarget{}
  1202. r.init(c, v.Idx0())
  1203. return r
  1204. }
  1205. c.throwSyntaxError(int(v.Idx)-1, "Unsupported meta property: %s.%s", v.Meta.Name, v.Property.Name)
  1206. return nil
  1207. }
  1208. func (e *compiledSequenceExpr) emitGetter(putOnStack bool) {
  1209. if len(e.sequence) > 0 {
  1210. for i := 0; i < len(e.sequence)-1; i++ {
  1211. e.sequence[i].emitGetter(false)
  1212. }
  1213. e.sequence[len(e.sequence)-1].emitGetter(putOnStack)
  1214. }
  1215. }
  1216. func (c *compiler) compileSequenceExpression(v *ast.SequenceExpression) compiledExpr {
  1217. s := make([]compiledExpr, len(v.Sequence))
  1218. for i, expr := range v.Sequence {
  1219. s[i] = c.compileExpression(expr)
  1220. }
  1221. r := &compiledSequenceExpr{
  1222. sequence: s,
  1223. }
  1224. var idx file.Idx
  1225. if len(v.Sequence) > 0 {
  1226. idx = v.Idx0()
  1227. }
  1228. r.init(c, idx)
  1229. return r
  1230. }
  1231. func (c *compiler) emitThrow(v Value) {
  1232. if o, ok := v.(*Object); ok {
  1233. t := nilSafe(o.self.getStr("name", nil)).toString().String()
  1234. switch t {
  1235. case "TypeError":
  1236. c.emit(loadDynamic(t))
  1237. msg := o.self.getStr("message", nil)
  1238. if msg != nil {
  1239. c.emit(loadVal(c.p.defineLiteralValue(msg)))
  1240. c.emit(_new(1))
  1241. } else {
  1242. c.emit(_new(0))
  1243. }
  1244. c.emit(throw)
  1245. return
  1246. }
  1247. }
  1248. panic(fmt.Errorf("unknown exception type thrown while evaliating constant expression: %s", v.String()))
  1249. }
  1250. func (c *compiler) emitConst(expr compiledExpr, putOnStack bool) {
  1251. v, ex := c.evalConst(expr)
  1252. if ex == nil {
  1253. if putOnStack {
  1254. c.emit(loadVal(c.p.defineLiteralValue(v)))
  1255. }
  1256. } else {
  1257. c.emitThrow(ex.val)
  1258. }
  1259. }
  1260. func (c *compiler) emitExpr(expr compiledExpr, putOnStack bool) {
  1261. if expr.constant() {
  1262. c.emitConst(expr, putOnStack)
  1263. } else {
  1264. expr.emitGetter(putOnStack)
  1265. }
  1266. }
  1267. func (c *compiler) evalConst(expr compiledExpr) (Value, *Exception) {
  1268. if expr, ok := expr.(*compiledLiteral); ok {
  1269. return expr.val, nil
  1270. }
  1271. if c.evalVM == nil {
  1272. c.evalVM = New().vm
  1273. }
  1274. var savedPrg *Program
  1275. createdPrg := false
  1276. if c.evalVM.prg == nil {
  1277. c.evalVM.prg = &Program{}
  1278. savedPrg = c.p
  1279. c.p = c.evalVM.prg
  1280. createdPrg = true
  1281. }
  1282. savedPc := len(c.p.code)
  1283. expr.emitGetter(true)
  1284. c.emit(halt)
  1285. c.evalVM.pc = savedPc
  1286. ex := c.evalVM.runTry()
  1287. if createdPrg {
  1288. c.evalVM.prg = nil
  1289. c.evalVM.pc = 0
  1290. c.p = savedPrg
  1291. } else {
  1292. c.evalVM.prg.code = c.evalVM.prg.code[:savedPc]
  1293. c.p.code = c.evalVM.prg.code
  1294. }
  1295. if ex == nil {
  1296. return c.evalVM.pop(), nil
  1297. }
  1298. return nil, ex
  1299. }
  1300. func (e *compiledUnaryExpr) constant() bool {
  1301. return e.operand.constant()
  1302. }
  1303. func (e *compiledUnaryExpr) emitGetter(putOnStack bool) {
  1304. var prepare, body func()
  1305. toNumber := func() {
  1306. e.c.emit(toNumber)
  1307. }
  1308. switch e.operator {
  1309. case token.NOT:
  1310. e.operand.emitGetter(true)
  1311. e.c.emit(not)
  1312. goto end
  1313. case token.BITWISE_NOT:
  1314. e.operand.emitGetter(true)
  1315. e.c.emit(bnot)
  1316. goto end
  1317. case token.TYPEOF:
  1318. if o, ok := e.operand.(compiledExprOrRef); ok {
  1319. o.emitGetterOrRef()
  1320. } else {
  1321. e.operand.emitGetter(true)
  1322. }
  1323. e.c.emit(typeof)
  1324. goto end
  1325. case token.DELETE:
  1326. e.operand.deleteExpr().emitGetter(putOnStack)
  1327. return
  1328. case token.MINUS:
  1329. e.c.emitExpr(e.operand, true)
  1330. e.c.emit(neg)
  1331. goto end
  1332. case token.PLUS:
  1333. e.c.emitExpr(e.operand, true)
  1334. e.c.emit(plus)
  1335. goto end
  1336. case token.INCREMENT:
  1337. prepare = toNumber
  1338. body = func() {
  1339. e.c.emit(inc)
  1340. }
  1341. case token.DECREMENT:
  1342. prepare = toNumber
  1343. body = func() {
  1344. e.c.emit(dec)
  1345. }
  1346. case token.VOID:
  1347. e.c.emitExpr(e.operand, false)
  1348. if putOnStack {
  1349. e.c.emit(loadUndef)
  1350. }
  1351. return
  1352. default:
  1353. panic(fmt.Errorf("Unknown unary operator: %s", e.operator.String()))
  1354. }
  1355. e.operand.emitUnary(prepare, body, e.postfix, putOnStack)
  1356. return
  1357. end:
  1358. if !putOnStack {
  1359. e.c.emit(pop)
  1360. }
  1361. }
  1362. func (c *compiler) compileUnaryExpression(v *ast.UnaryExpression) compiledExpr {
  1363. r := &compiledUnaryExpr{
  1364. operand: c.compileExpression(v.Operand),
  1365. operator: v.Operator,
  1366. postfix: v.Postfix,
  1367. }
  1368. r.init(c, v.Idx0())
  1369. return r
  1370. }
  1371. func (e *compiledConditionalExpr) emitGetter(putOnStack bool) {
  1372. e.test.emitGetter(true)
  1373. j := len(e.c.p.code)
  1374. e.c.emit(nil)
  1375. e.consequent.emitGetter(putOnStack)
  1376. j1 := len(e.c.p.code)
  1377. e.c.emit(nil)
  1378. e.c.p.code[j] = jne(len(e.c.p.code) - j)
  1379. e.alternate.emitGetter(putOnStack)
  1380. e.c.p.code[j1] = jump(len(e.c.p.code) - j1)
  1381. }
  1382. func (c *compiler) compileConditionalExpression(v *ast.ConditionalExpression) compiledExpr {
  1383. r := &compiledConditionalExpr{
  1384. test: c.compileExpression(v.Test),
  1385. consequent: c.compileExpression(v.Consequent),
  1386. alternate: c.compileExpression(v.Alternate),
  1387. }
  1388. r.init(c, v.Idx0())
  1389. return r
  1390. }
  1391. func (e *compiledLogicalOr) constant() bool {
  1392. if e.left.constant() {
  1393. if v, ex := e.c.evalConst(e.left); ex == nil {
  1394. if v.ToBoolean() {
  1395. return true
  1396. }
  1397. return e.right.constant()
  1398. } else {
  1399. return true
  1400. }
  1401. }
  1402. return false
  1403. }
  1404. func (e *compiledLogicalOr) emitGetter(putOnStack bool) {
  1405. if e.left.constant() {
  1406. if v, ex := e.c.evalConst(e.left); ex == nil {
  1407. if !v.ToBoolean() {
  1408. e.c.emitExpr(e.right, putOnStack)
  1409. } else {
  1410. if putOnStack {
  1411. e.c.emit(loadVal(e.c.p.defineLiteralValue(v)))
  1412. }
  1413. }
  1414. } else {
  1415. e.c.emitThrow(ex.val)
  1416. }
  1417. return
  1418. }
  1419. e.c.emitExpr(e.left, true)
  1420. j := len(e.c.p.code)
  1421. e.addSrcMap()
  1422. e.c.emit(nil)
  1423. e.c.emit(pop)
  1424. e.c.emitExpr(e.right, true)
  1425. e.c.p.code[j] = jeq1(len(e.c.p.code) - j)
  1426. if !putOnStack {
  1427. e.c.emit(pop)
  1428. }
  1429. }
  1430. func (e *compiledLogicalAnd) constant() bool {
  1431. if e.left.constant() {
  1432. if v, ex := e.c.evalConst(e.left); ex == nil {
  1433. if !v.ToBoolean() {
  1434. return true
  1435. } else {
  1436. return e.right.constant()
  1437. }
  1438. } else {
  1439. return true
  1440. }
  1441. }
  1442. return false
  1443. }
  1444. func (e *compiledLogicalAnd) emitGetter(putOnStack bool) {
  1445. var j int
  1446. if e.left.constant() {
  1447. if v, ex := e.c.evalConst(e.left); ex == nil {
  1448. if !v.ToBoolean() {
  1449. e.c.emit(loadVal(e.c.p.defineLiteralValue(v)))
  1450. } else {
  1451. e.c.emitExpr(e.right, putOnStack)
  1452. }
  1453. } else {
  1454. e.c.emitThrow(ex.val)
  1455. }
  1456. return
  1457. }
  1458. e.left.emitGetter(true)
  1459. j = len(e.c.p.code)
  1460. e.addSrcMap()
  1461. e.c.emit(nil)
  1462. e.c.emit(pop)
  1463. e.c.emitExpr(e.right, true)
  1464. e.c.p.code[j] = jneq1(len(e.c.p.code) - j)
  1465. if !putOnStack {
  1466. e.c.emit(pop)
  1467. }
  1468. }
  1469. func (e *compiledBinaryExpr) constant() bool {
  1470. return e.left.constant() && e.right.constant()
  1471. }
  1472. func (e *compiledBinaryExpr) emitGetter(putOnStack bool) {
  1473. e.c.emitExpr(e.left, true)
  1474. e.c.emitExpr(e.right, true)
  1475. e.addSrcMap()
  1476. switch e.operator {
  1477. case token.LESS:
  1478. e.c.emit(op_lt)
  1479. case token.GREATER:
  1480. e.c.emit(op_gt)
  1481. case token.LESS_OR_EQUAL:
  1482. e.c.emit(op_lte)
  1483. case token.GREATER_OR_EQUAL:
  1484. e.c.emit(op_gte)
  1485. case token.EQUAL:
  1486. e.c.emit(op_eq)
  1487. case token.NOT_EQUAL:
  1488. e.c.emit(op_neq)
  1489. case token.STRICT_EQUAL:
  1490. e.c.emit(op_strict_eq)
  1491. case token.STRICT_NOT_EQUAL:
  1492. e.c.emit(op_strict_neq)
  1493. case token.PLUS:
  1494. e.c.emit(add)
  1495. case token.MINUS:
  1496. e.c.emit(sub)
  1497. case token.MULTIPLY:
  1498. e.c.emit(mul)
  1499. case token.SLASH:
  1500. e.c.emit(div)
  1501. case token.REMAINDER:
  1502. e.c.emit(mod)
  1503. case token.AND:
  1504. e.c.emit(and)
  1505. case token.OR:
  1506. e.c.emit(or)
  1507. case token.EXCLUSIVE_OR:
  1508. e.c.emit(xor)
  1509. case token.INSTANCEOF:
  1510. e.c.emit(op_instanceof)
  1511. case token.IN:
  1512. e.c.emit(op_in)
  1513. case token.SHIFT_LEFT:
  1514. e.c.emit(sal)
  1515. case token.SHIFT_RIGHT:
  1516. e.c.emit(sar)
  1517. case token.UNSIGNED_SHIFT_RIGHT:
  1518. e.c.emit(shr)
  1519. default:
  1520. panic(fmt.Errorf("Unknown operator: %s", e.operator.String()))
  1521. }
  1522. if !putOnStack {
  1523. e.c.emit(pop)
  1524. }
  1525. }
  1526. func (c *compiler) compileBinaryExpression(v *ast.BinaryExpression) compiledExpr {
  1527. switch v.Operator {
  1528. case token.LOGICAL_OR:
  1529. return c.compileLogicalOr(v.Left, v.Right, v.Idx0())
  1530. case token.LOGICAL_AND:
  1531. return c.compileLogicalAnd(v.Left, v.Right, v.Idx0())
  1532. }
  1533. r := &compiledBinaryExpr{
  1534. left: c.compileExpression(v.Left),
  1535. right: c.compileExpression(v.Right),
  1536. operator: v.Operator,
  1537. }
  1538. r.init(c, v.Idx0())
  1539. return r
  1540. }
  1541. func (c *compiler) compileLogicalOr(left, right ast.Expression, idx file.Idx) compiledExpr {
  1542. r := &compiledLogicalOr{
  1543. left: c.compileExpression(left),
  1544. right: c.compileExpression(right),
  1545. }
  1546. r.init(c, idx)
  1547. return r
  1548. }
  1549. func (c *compiler) compileLogicalAnd(left, right ast.Expression, idx file.Idx) compiledExpr {
  1550. r := &compiledLogicalAnd{
  1551. left: c.compileExpression(left),
  1552. right: c.compileExpression(right),
  1553. }
  1554. r.init(c, idx)
  1555. return r
  1556. }
  1557. func (e *compiledObjectLiteral) emitGetter(putOnStack bool) {
  1558. e.addSrcMap()
  1559. e.c.emit(newObject)
  1560. for _, prop := range e.expr.Value {
  1561. switch prop := prop.(type) {
  1562. case *ast.PropertyKeyed:
  1563. keyExpr := e.c.compileExpression(prop.Key)
  1564. computed := false
  1565. var key unistring.String
  1566. switch keyExpr := keyExpr.(type) {
  1567. case *compiledLiteral:
  1568. key = keyExpr.val.string()
  1569. default:
  1570. keyExpr.emitGetter(true)
  1571. computed = true
  1572. }
  1573. valueExpr := e.c.compileExpression(prop.Value)
  1574. var anonFn *compiledFunctionLiteral
  1575. if fn, ok := valueExpr.(*compiledFunctionLiteral); ok {
  1576. if fn.name == nil {
  1577. anonFn = fn
  1578. fn.lhsName = key
  1579. }
  1580. }
  1581. if computed {
  1582. e.c.emit(_toPropertyKey{})
  1583. valueExpr.emitGetter(true)
  1584. switch prop.Kind {
  1585. case ast.PropertyKindValue, ast.PropertyKindMethod:
  1586. if anonFn != nil {
  1587. e.c.emit(setElem1Named)
  1588. } else {
  1589. e.c.emit(setElem1)
  1590. }
  1591. case ast.PropertyKindGet:
  1592. e.c.emit(setPropGetter1)
  1593. case ast.PropertyKindSet:
  1594. e.c.emit(setPropSetter1)
  1595. default:
  1596. panic(fmt.Errorf("unknown property kind: %s", prop.Kind))
  1597. }
  1598. } else {
  1599. if anonFn != nil {
  1600. anonFn.lhsName = key
  1601. }
  1602. valueExpr.emitGetter(true)
  1603. switch prop.Kind {
  1604. case ast.PropertyKindValue:
  1605. if key == __proto__ {
  1606. e.c.emit(setProto)
  1607. } else {
  1608. e.c.emit(setProp1(key))
  1609. }
  1610. case ast.PropertyKindMethod:
  1611. e.c.emit(setProp1(key))
  1612. case ast.PropertyKindGet:
  1613. e.c.emit(setPropGetter(key))
  1614. case ast.PropertyKindSet:
  1615. e.c.emit(setPropSetter(key))
  1616. default:
  1617. panic(fmt.Errorf("unknown property kind: %s", prop.Kind))
  1618. }
  1619. }
  1620. case *ast.PropertyShort:
  1621. key := prop.Name.Name
  1622. if prop.Initializer != nil {
  1623. e.c.throwSyntaxError(int(prop.Initializer.Idx0())-1, "Invalid shorthand property initializer")
  1624. }
  1625. if e.c.scope.strict && key == "let" {
  1626. e.c.throwSyntaxError(e.offset, "'let' cannot be used as a shorthand property in strict mode")
  1627. }
  1628. e.c.compileIdentifierExpression(&prop.Name).emitGetter(true)
  1629. e.c.emit(setProp1(key))
  1630. case *ast.SpreadElement:
  1631. e.c.compileExpression(prop.Expression).emitGetter(true)
  1632. e.c.emit(copySpread)
  1633. default:
  1634. panic(fmt.Errorf("unknown Property type: %T", prop))
  1635. }
  1636. }
  1637. if !putOnStack {
  1638. e.c.emit(pop)
  1639. }
  1640. }
  1641. func (c *compiler) compileObjectLiteral(v *ast.ObjectLiteral) compiledExpr {
  1642. r := &compiledObjectLiteral{
  1643. expr: v,
  1644. }
  1645. r.init(c, v.Idx0())
  1646. return r
  1647. }
  1648. func (e *compiledArrayLiteral) emitGetter(putOnStack bool) {
  1649. e.addSrcMap()
  1650. hasSpread := false
  1651. mark := len(e.c.p.code)
  1652. e.c.emit(nil)
  1653. for _, v := range e.expr.Value {
  1654. if spread, ok := v.(*ast.SpreadElement); ok {
  1655. hasSpread = true
  1656. e.c.compileExpression(spread.Expression).emitGetter(true)
  1657. e.c.emit(pushArraySpread)
  1658. } else {
  1659. if v != nil {
  1660. e.c.compileExpression(v).emitGetter(true)
  1661. } else {
  1662. e.c.emit(loadNil)
  1663. }
  1664. e.c.emit(pushArrayItem)
  1665. }
  1666. }
  1667. var objCount uint32
  1668. if !hasSpread {
  1669. objCount = uint32(len(e.expr.Value))
  1670. }
  1671. e.c.p.code[mark] = newArray(objCount)
  1672. if !putOnStack {
  1673. e.c.emit(pop)
  1674. }
  1675. }
  1676. func (c *compiler) compileArrayLiteral(v *ast.ArrayLiteral) compiledExpr {
  1677. r := &compiledArrayLiteral{
  1678. expr: v,
  1679. }
  1680. r.init(c, v.Idx0())
  1681. return r
  1682. }
  1683. func (e *compiledRegexpLiteral) emitGetter(putOnStack bool) {
  1684. if putOnStack {
  1685. pattern, err := compileRegexp(e.expr.Pattern, e.expr.Flags)
  1686. if err != nil {
  1687. e.c.throwSyntaxError(e.offset, err.Error())
  1688. }
  1689. e.c.emit(&newRegexp{pattern: pattern, src: newStringValue(e.expr.Pattern)})
  1690. }
  1691. }
  1692. func (c *compiler) compileRegexpLiteral(v *ast.RegExpLiteral) compiledExpr {
  1693. r := &compiledRegexpLiteral{
  1694. expr: v,
  1695. }
  1696. r.init(c, v.Idx0())
  1697. return r
  1698. }
  1699. func (e *compiledCallExpr) emitGetter(putOnStack bool) {
  1700. var calleeName unistring.String
  1701. if e.isVariadic {
  1702. e.c.emit(startVariadic)
  1703. }
  1704. switch callee := e.callee.(type) {
  1705. case *compiledDotExpr:
  1706. callee.left.emitGetter(true)
  1707. e.c.emit(dup)
  1708. e.c.emit(getPropCallee(callee.name))
  1709. case *compiledBracketExpr:
  1710. callee.left.emitGetter(true)
  1711. e.c.emit(dup)
  1712. callee.member.emitGetter(true)
  1713. e.c.emit(getElemCallee)
  1714. case *compiledIdentifierExpr:
  1715. calleeName = callee.name
  1716. callee.emitGetterAndCallee()
  1717. default:
  1718. e.c.emit(loadUndef)
  1719. callee.emitGetter(true)
  1720. }
  1721. for _, expr := range e.args {
  1722. expr.emitGetter(true)
  1723. }
  1724. e.addSrcMap()
  1725. if calleeName == "eval" {
  1726. foundFunc, foundVar := false, false
  1727. for sc := e.c.scope; sc != nil; sc = sc.outer {
  1728. if !foundFunc && sc.function && !sc.arrow {
  1729. foundFunc = true
  1730. sc.thisNeeded, sc.argsNeeded = true, true
  1731. }
  1732. if !foundVar && (sc.variable || sc.function) {
  1733. foundVar = true
  1734. if !sc.strict {
  1735. sc.dynamic = true
  1736. }
  1737. }
  1738. sc.dynLookup = true
  1739. }
  1740. if e.c.scope.strict {
  1741. if e.isVariadic {
  1742. e.c.emit(callEvalVariadicStrict)
  1743. } else {
  1744. e.c.emit(callEvalStrict(len(e.args)))
  1745. }
  1746. } else {
  1747. if e.isVariadic {
  1748. e.c.emit(callEvalVariadic)
  1749. } else {
  1750. e.c.emit(callEval(len(e.args)))
  1751. }
  1752. }
  1753. } else {
  1754. if e.isVariadic {
  1755. e.c.emit(callVariadic)
  1756. } else {
  1757. e.c.emit(call(len(e.args)))
  1758. }
  1759. }
  1760. if e.isVariadic {
  1761. e.c.emit(endVariadic)
  1762. }
  1763. if !putOnStack {
  1764. e.c.emit(pop)
  1765. }
  1766. }
  1767. func (e *compiledCallExpr) deleteExpr() compiledExpr {
  1768. r := &defaultDeleteExpr{
  1769. expr: e,
  1770. }
  1771. r.init(e.c, file.Idx(e.offset+1))
  1772. return r
  1773. }
  1774. func (c *compiler) compileSpreadCallArgument(spread *ast.SpreadElement) compiledExpr {
  1775. r := &compiledSpreadCallArgument{
  1776. expr: c.compileExpression(spread.Expression),
  1777. }
  1778. r.init(c, spread.Idx0())
  1779. return r
  1780. }
  1781. func (c *compiler) compileCallExpression(v *ast.CallExpression) compiledExpr {
  1782. args := make([]compiledExpr, len(v.ArgumentList))
  1783. isVariadic := false
  1784. for i, argExpr := range v.ArgumentList {
  1785. if spread, ok := argExpr.(*ast.SpreadElement); ok {
  1786. args[i] = c.compileSpreadCallArgument(spread)
  1787. isVariadic = true
  1788. } else {
  1789. args[i] = c.compileExpression(argExpr)
  1790. }
  1791. }
  1792. r := &compiledCallExpr{
  1793. args: args,
  1794. callee: c.compileExpression(v.Callee),
  1795. isVariadic: isVariadic,
  1796. }
  1797. r.init(c, v.LeftParenthesis)
  1798. return r
  1799. }
  1800. func (c *compiler) compileIdentifierExpression(v *ast.Identifier) compiledExpr {
  1801. if c.scope.strict {
  1802. c.checkIdentifierName(v.Name, int(v.Idx)-1)
  1803. }
  1804. r := &compiledIdentifierExpr{
  1805. name: v.Name,
  1806. }
  1807. r.offset = int(v.Idx) - 1
  1808. r.init(c, v.Idx0())
  1809. return r
  1810. }
  1811. func (c *compiler) compileNumberLiteral(v *ast.NumberLiteral) compiledExpr {
  1812. if c.scope.strict && octalRegexp.MatchString(v.Literal) {
  1813. c.throwSyntaxError(int(v.Idx)-1, "Octal literals are not allowed in strict mode")
  1814. panic("Unreachable")
  1815. }
  1816. var val Value
  1817. switch num := v.Value.(type) {
  1818. case int64:
  1819. val = intToValue(num)
  1820. case float64:
  1821. val = floatToValue(num)
  1822. default:
  1823. panic(fmt.Errorf("Unsupported number literal type: %T", v.Value))
  1824. }
  1825. r := &compiledLiteral{
  1826. val: val,
  1827. }
  1828. r.init(c, v.Idx0())
  1829. return r
  1830. }
  1831. func (c *compiler) compileStringLiteral(v *ast.StringLiteral) compiledExpr {
  1832. r := &compiledLiteral{
  1833. val: stringValueFromRaw(v.Value),
  1834. }
  1835. r.init(c, v.Idx0())
  1836. return r
  1837. }
  1838. func (c *compiler) compileBooleanLiteral(v *ast.BooleanLiteral) compiledExpr {
  1839. var val Value
  1840. if v.Value {
  1841. val = valueTrue
  1842. } else {
  1843. val = valueFalse
  1844. }
  1845. r := &compiledLiteral{
  1846. val: val,
  1847. }
  1848. r.init(c, v.Idx0())
  1849. return r
  1850. }
  1851. func (c *compiler) compileAssignExpression(v *ast.AssignExpression) compiledExpr {
  1852. // log.Printf("compileAssignExpression(): %+v", v)
  1853. r := &compiledAssignExpr{
  1854. left: c.compileExpression(v.Left),
  1855. right: c.compileExpression(v.Right),
  1856. operator: v.Operator,
  1857. }
  1858. r.init(c, v.Idx0())
  1859. return r
  1860. }
  1861. func (e *compiledEnumGetExpr) emitGetter(putOnStack bool) {
  1862. e.c.emit(enumGet)
  1863. if !putOnStack {
  1864. e.c.emit(pop)
  1865. }
  1866. }
  1867. func (c *compiler) compileObjectAssignmentPattern(v *ast.ObjectPattern) compiledExpr {
  1868. r := &compiledObjectAssignmentPattern{
  1869. expr: v,
  1870. }
  1871. r.init(c, v.Idx0())
  1872. return r
  1873. }
  1874. func (e *compiledObjectAssignmentPattern) emitGetter(putOnStack bool) {
  1875. if putOnStack {
  1876. e.c.emit(loadUndef)
  1877. }
  1878. }
  1879. func (c *compiler) compileArrayAssignmentPattern(v *ast.ArrayPattern) compiledExpr {
  1880. r := &compiledArrayAssignmentPattern{
  1881. expr: v,
  1882. }
  1883. r.init(c, v.Idx0())
  1884. return r
  1885. }
  1886. func (e *compiledArrayAssignmentPattern) emitGetter(putOnStack bool) {
  1887. if putOnStack {
  1888. e.c.emit(loadUndef)
  1889. }
  1890. }
  1891. func (c *compiler) emitNamed(expr compiledExpr, name unistring.String) {
  1892. if en, ok := expr.(interface {
  1893. emitNamed(name unistring.String)
  1894. }); ok {
  1895. en.emitNamed(name)
  1896. } else {
  1897. expr.emitGetter(true)
  1898. }
  1899. }
  1900. func (e *compiledFunctionLiteral) emitNamed(name unistring.String) {
  1901. e.lhsName = name
  1902. e.emitGetter(true)
  1903. }
  1904. func (c *compiler) emitPattern(pattern ast.Pattern, emitter func(target, init compiledExpr), putOnStack bool) {
  1905. switch pattern := pattern.(type) {
  1906. case *ast.ObjectPattern:
  1907. c.emitObjectPattern(pattern, emitter, putOnStack)
  1908. case *ast.ArrayPattern:
  1909. c.emitArrayPattern(pattern, emitter, putOnStack)
  1910. default:
  1911. panic(fmt.Errorf("unsupported Pattern: %T", pattern))
  1912. }
  1913. }
  1914. func (c *compiler) emitAssign(target ast.Expression, init compiledExpr, emitAssignSimple func(target, init compiledExpr)) {
  1915. pattern, isPattern := target.(ast.Pattern)
  1916. if isPattern {
  1917. init.emitGetter(true)
  1918. c.emitPattern(pattern, emitAssignSimple, false)
  1919. } else {
  1920. emitAssignSimple(c.compileExpression(target), init)
  1921. }
  1922. }
  1923. func (c *compiler) emitObjectPattern(pattern *ast.ObjectPattern, emitAssign func(target, init compiledExpr), putOnStack bool) {
  1924. if pattern.Rest != nil {
  1925. c.emit(createDestructSrc)
  1926. } else {
  1927. c.emit(checkObjectCoercible)
  1928. }
  1929. for _, prop := range pattern.Properties {
  1930. switch prop := prop.(type) {
  1931. case *ast.PropertyShort:
  1932. c.emit(dup)
  1933. emitAssign(c.compileIdentifierExpression(&prop.Name), c.compilePatternInitExpr(func() {
  1934. c.emit(getProp(prop.Name.Name))
  1935. }, prop.Initializer, prop.Idx0()))
  1936. case *ast.PropertyKeyed:
  1937. c.emit(dup)
  1938. c.compileExpression(prop.Key).emitGetter(true)
  1939. c.emit(_toPropertyKey{})
  1940. var target ast.Expression
  1941. var initializer ast.Expression
  1942. if e, ok := prop.Value.(*ast.AssignExpression); ok {
  1943. target = e.Left
  1944. initializer = e.Right
  1945. } else {
  1946. target = prop.Value
  1947. }
  1948. c.emitAssign(target, c.compilePatternInitExpr(func() {
  1949. c.emit(getKey)
  1950. }, initializer, prop.Idx0()), emitAssign)
  1951. default:
  1952. c.throwSyntaxError(int(prop.Idx0()-1), "Unsupported AssignmentProperty type: %T", prop)
  1953. }
  1954. }
  1955. if pattern.Rest != nil {
  1956. emitAssign(c.compileExpression(pattern.Rest), c.compileEmitterExpr(func() {
  1957. c.emit(copyRest)
  1958. }, pattern.Rest.Idx0()))
  1959. c.emit(pop)
  1960. }
  1961. if !putOnStack {
  1962. c.emit(pop)
  1963. }
  1964. }
  1965. func (c *compiler) emitArrayPattern(pattern *ast.ArrayPattern, emitAssign func(target, init compiledExpr), putOnStack bool) {
  1966. var marks []int
  1967. c.emit(iterate)
  1968. for _, elt := range pattern.Elements {
  1969. switch elt := elt.(type) {
  1970. case nil:
  1971. marks = append(marks, len(c.p.code))
  1972. c.emit(nil)
  1973. case *ast.AssignExpression:
  1974. c.emitAssign(elt.Left, c.compilePatternInitExpr(func() {
  1975. marks = append(marks, len(c.p.code))
  1976. c.emit(nil, enumGet)
  1977. }, elt.Right, elt.Idx0()), emitAssign)
  1978. default:
  1979. c.emitAssign(elt, c.compileEmitterExpr(func() {
  1980. marks = append(marks, len(c.p.code))
  1981. c.emit(nil, enumGet)
  1982. }, elt.Idx0()), emitAssign)
  1983. }
  1984. }
  1985. if pattern.Rest != nil {
  1986. c.emitAssign(pattern.Rest, c.compileEmitterExpr(func() {
  1987. c.emit(newArrayFromIter)
  1988. }, pattern.Rest.Idx0()), emitAssign)
  1989. } else {
  1990. c.emit(enumPopClose)
  1991. }
  1992. mark1 := len(c.p.code)
  1993. c.emit(nil)
  1994. for i, elt := range pattern.Elements {
  1995. switch elt := elt.(type) {
  1996. case nil:
  1997. c.p.code[marks[i]] = iterNext(len(c.p.code) - marks[i])
  1998. case *ast.Identifier:
  1999. emitAssign(c.compileIdentifierExpression(elt), c.compileEmitterExpr(func() {
  2000. c.p.code[marks[i]] = iterNext(len(c.p.code) - marks[i])
  2001. c.emit(loadUndef)
  2002. }, elt.Idx0()))
  2003. case *ast.AssignExpression:
  2004. c.emitAssign(elt.Left, c.compileNamedEmitterExpr(func(name unistring.String) {
  2005. c.p.code[marks[i]] = iterNext(len(c.p.code) - marks[i])
  2006. c.emitNamed(c.compileExpression(elt.Right), name)
  2007. }, elt.Idx0()), emitAssign)
  2008. default:
  2009. c.emitAssign(elt, c.compileEmitterExpr(
  2010. func() {
  2011. c.p.code[marks[i]] = iterNext(len(c.p.code) - marks[i])
  2012. c.emit(loadUndef)
  2013. }, elt.Idx0()), emitAssign)
  2014. }
  2015. }
  2016. c.emit(enumPop)
  2017. if pattern.Rest != nil {
  2018. c.emitAssign(pattern.Rest, c.compileExpression(
  2019. &ast.ArrayLiteral{
  2020. LeftBracket: pattern.Rest.Idx0(),
  2021. RightBracket: pattern.Rest.Idx0(),
  2022. }), emitAssign)
  2023. }
  2024. c.p.code[mark1] = jump(len(c.p.code) - mark1)
  2025. if !putOnStack {
  2026. c.emit(pop)
  2027. }
  2028. }
  2029. func (e *compiledObjectAssignmentPattern) emitSetter(valueExpr compiledExpr, putOnStack bool) {
  2030. valueExpr.emitGetter(true)
  2031. e.c.emitObjectPattern(e.expr, e.c.emitPatternAssign, putOnStack)
  2032. }
  2033. func (e *compiledArrayAssignmentPattern) emitSetter(valueExpr compiledExpr, putOnStack bool) {
  2034. valueExpr.emitGetter(true)
  2035. e.c.emitArrayPattern(e.expr, e.c.emitPatternAssign, putOnStack)
  2036. }
  2037. type compiledPatternInitExpr struct {
  2038. baseCompiledExpr
  2039. emitSrc func()
  2040. def compiledExpr
  2041. }
  2042. func (e *compiledPatternInitExpr) emitGetter(putOnStack bool) {
  2043. if !putOnStack {
  2044. return
  2045. }
  2046. e.emitSrc()
  2047. if e.def != nil {
  2048. mark := len(e.c.p.code)
  2049. e.c.emit(nil)
  2050. e.def.emitGetter(true)
  2051. e.c.p.code[mark] = jdef(len(e.c.p.code) - mark)
  2052. }
  2053. }
  2054. func (e *compiledPatternInitExpr) emitNamed(name unistring.String) {
  2055. e.emitSrc()
  2056. if e.def != nil {
  2057. mark := len(e.c.p.code)
  2058. e.c.emit(nil)
  2059. e.c.emitNamed(e.def, name)
  2060. e.c.p.code[mark] = jdef(len(e.c.p.code) - mark)
  2061. }
  2062. }
  2063. func (c *compiler) compilePatternInitExpr(emitSrc func(), def ast.Expression, idx file.Idx) compiledExpr {
  2064. r := &compiledPatternInitExpr{
  2065. emitSrc: emitSrc,
  2066. def: c.compileExpression(def),
  2067. }
  2068. r.init(c, idx)
  2069. return r
  2070. }
  2071. type compiledEmitterExpr struct {
  2072. baseCompiledExpr
  2073. emitter func()
  2074. namedEmitter func(name unistring.String)
  2075. }
  2076. func (e *compiledEmitterExpr) emitGetter(putOnStack bool) {
  2077. if e.emitter != nil {
  2078. e.emitter()
  2079. } else {
  2080. e.namedEmitter("")
  2081. }
  2082. if !putOnStack {
  2083. e.c.emit(pop)
  2084. }
  2085. }
  2086. func (e *compiledEmitterExpr) emitNamed(name unistring.String) {
  2087. if e.namedEmitter != nil {
  2088. e.namedEmitter(name)
  2089. } else {
  2090. e.emitter()
  2091. }
  2092. }
  2093. func (c *compiler) compileEmitterExpr(emitter func(), idx file.Idx) *compiledEmitterExpr {
  2094. r := &compiledEmitterExpr{
  2095. emitter: emitter,
  2096. }
  2097. r.init(c, idx)
  2098. return r
  2099. }
  2100. func (c *compiler) compileNamedEmitterExpr(namedEmitter func(unistring.String), idx file.Idx) *compiledEmitterExpr {
  2101. r := &compiledEmitterExpr{
  2102. namedEmitter: namedEmitter,
  2103. }
  2104. r.init(c, idx)
  2105. return r
  2106. }
  2107. func (e *compiledSpreadCallArgument) emitGetter(putOnStack bool) {
  2108. e.expr.emitGetter(putOnStack)
  2109. if putOnStack {
  2110. e.c.emit(pushSpread)
  2111. }
  2112. }