compiler_expr.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  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)
  16. emitUnary(prepare, body func(), postfix, putOnStack bool)
  17. deleteExpr() compiledExpr
  18. constant() bool
  19. addSrcMap()
  20. }
  21. type compiledExprOrRef interface {
  22. compiledExpr
  23. emitGetterOrRef()
  24. }
  25. type compiledCallExpr struct {
  26. baseCompiledExpr
  27. args []compiledExpr
  28. callee compiledExpr
  29. }
  30. type compiledObjectLiteral struct {
  31. baseCompiledExpr
  32. expr *ast.ObjectLiteral
  33. }
  34. type compiledArrayLiteral struct {
  35. baseCompiledExpr
  36. expr *ast.ArrayLiteral
  37. }
  38. type compiledRegexpLiteral struct {
  39. baseCompiledExpr
  40. expr *ast.RegExpLiteral
  41. }
  42. type compiledLiteral struct {
  43. baseCompiledExpr
  44. val Value
  45. }
  46. type compiledAssignExpr struct {
  47. baseCompiledExpr
  48. left, right compiledExpr
  49. operator token.Token
  50. }
  51. type deleteGlobalExpr struct {
  52. baseCompiledExpr
  53. name unistring.String
  54. }
  55. type deleteVarExpr struct {
  56. baseCompiledExpr
  57. name unistring.String
  58. }
  59. type deletePropExpr struct {
  60. baseCompiledExpr
  61. left compiledExpr
  62. name unistring.String
  63. }
  64. type deleteElemExpr struct {
  65. baseCompiledExpr
  66. left, member compiledExpr
  67. }
  68. type constantExpr struct {
  69. baseCompiledExpr
  70. val Value
  71. }
  72. type baseCompiledExpr struct {
  73. c *compiler
  74. offset int
  75. }
  76. type compiledIdentifierExpr struct {
  77. baseCompiledExpr
  78. name unistring.String
  79. }
  80. type compiledFunctionLiteral struct {
  81. baseCompiledExpr
  82. expr *ast.FunctionLiteral
  83. isExpr bool
  84. }
  85. type compiledBracketExpr struct {
  86. baseCompiledExpr
  87. left, member compiledExpr
  88. }
  89. type compiledThisExpr struct {
  90. baseCompiledExpr
  91. }
  92. type compiledNewExpr struct {
  93. baseCompiledExpr
  94. callee compiledExpr
  95. args []compiledExpr
  96. }
  97. type compiledNewTarget struct {
  98. baseCompiledExpr
  99. }
  100. type compiledSequenceExpr struct {
  101. baseCompiledExpr
  102. sequence []compiledExpr
  103. }
  104. type compiledUnaryExpr struct {
  105. baseCompiledExpr
  106. operand compiledExpr
  107. operator token.Token
  108. postfix bool
  109. }
  110. type compiledConditionalExpr struct {
  111. baseCompiledExpr
  112. test, consequent, alternate compiledExpr
  113. }
  114. type compiledLogicalOr struct {
  115. baseCompiledExpr
  116. left, right compiledExpr
  117. }
  118. type compiledLogicalAnd struct {
  119. baseCompiledExpr
  120. left, right compiledExpr
  121. }
  122. type compiledBinaryExpr struct {
  123. baseCompiledExpr
  124. left, right compiledExpr
  125. operator token.Token
  126. }
  127. type compiledVariableExpr struct {
  128. baseCompiledExpr
  129. name unistring.String
  130. initializer compiledExpr
  131. expr *ast.VariableExpression
  132. }
  133. type compiledEnumGetExpr struct {
  134. baseCompiledExpr
  135. }
  136. type defaultDeleteExpr struct {
  137. baseCompiledExpr
  138. expr compiledExpr
  139. }
  140. func (e *defaultDeleteExpr) emitGetter(putOnStack bool) {
  141. e.expr.emitGetter(false)
  142. if putOnStack {
  143. e.c.emit(loadVal(e.c.p.defineLiteralValue(valueTrue)))
  144. }
  145. }
  146. func (c *compiler) compileExpression(v ast.Expression) compiledExpr {
  147. // log.Printf("compileExpression: %T", v)
  148. switch v := v.(type) {
  149. case nil:
  150. return nil
  151. case *ast.AssignExpression:
  152. return c.compileAssignExpression(v)
  153. case *ast.NumberLiteral:
  154. return c.compileNumberLiteral(v)
  155. case *ast.StringLiteral:
  156. return c.compileStringLiteral(v)
  157. case *ast.BooleanLiteral:
  158. return c.compileBooleanLiteral(v)
  159. case *ast.NullLiteral:
  160. r := &compiledLiteral{
  161. val: _null,
  162. }
  163. r.init(c, v.Idx0())
  164. return r
  165. case *ast.Identifier:
  166. return c.compileIdentifierExpression(v)
  167. case *ast.CallExpression:
  168. return c.compileCallExpression(v)
  169. case *ast.ObjectLiteral:
  170. return c.compileObjectLiteral(v)
  171. case *ast.ArrayLiteral:
  172. return c.compileArrayLiteral(v)
  173. case *ast.RegExpLiteral:
  174. return c.compileRegexpLiteral(v)
  175. case *ast.VariableExpression:
  176. return c.compileVariableExpression(v)
  177. case *ast.BinaryExpression:
  178. return c.compileBinaryExpression(v)
  179. case *ast.UnaryExpression:
  180. return c.compileUnaryExpression(v)
  181. case *ast.ConditionalExpression:
  182. return c.compileConditionalExpression(v)
  183. case *ast.FunctionLiteral:
  184. return c.compileFunctionLiteral(v, true)
  185. case *ast.DotExpression:
  186. r := &compiledDotExpr{
  187. left: c.compileExpression(v.Left),
  188. name: v.Identifier.Name,
  189. }
  190. r.init(c, v.Idx0())
  191. return r
  192. case *ast.BracketExpression:
  193. r := &compiledBracketExpr{
  194. left: c.compileExpression(v.Left),
  195. member: c.compileExpression(v.Member),
  196. }
  197. r.init(c, v.Idx0())
  198. return r
  199. case *ast.ThisExpression:
  200. r := &compiledThisExpr{}
  201. r.init(c, v.Idx0())
  202. return r
  203. case *ast.SequenceExpression:
  204. return c.compileSequenceExpression(v)
  205. case *ast.NewExpression:
  206. return c.compileNewExpression(v)
  207. case *ast.MetaProperty:
  208. return c.compileMetaProperty(v)
  209. default:
  210. panic(fmt.Errorf("Unknown expression type: %T", v))
  211. }
  212. }
  213. func (e *baseCompiledExpr) constant() bool {
  214. return false
  215. }
  216. func (e *baseCompiledExpr) init(c *compiler, idx file.Idx) {
  217. e.c = c
  218. e.offset = int(idx) - 1
  219. }
  220. func (e *baseCompiledExpr) emitSetter(compiledExpr) {
  221. e.c.throwSyntaxError(e.offset, "Not a valid left-value expression")
  222. }
  223. func (e *baseCompiledExpr) deleteExpr() compiledExpr {
  224. r := &constantExpr{
  225. val: valueTrue,
  226. }
  227. r.init(e.c, file.Idx(e.offset+1))
  228. return r
  229. }
  230. func (e *baseCompiledExpr) emitUnary(func(), func(), bool, bool) {
  231. e.c.throwSyntaxError(e.offset, "Not a valid left-value expression")
  232. }
  233. func (e *baseCompiledExpr) addSrcMap() {
  234. if e.offset > 0 {
  235. e.c.p.srcMap = append(e.c.p.srcMap, srcMapItem{pc: len(e.c.p.code), srcPos: e.offset})
  236. }
  237. }
  238. func (e *constantExpr) emitGetter(putOnStack bool) {
  239. if putOnStack {
  240. e.addSrcMap()
  241. e.c.emit(loadVal(e.c.p.defineLiteralValue(e.val)))
  242. }
  243. }
  244. func (e *compiledIdentifierExpr) emitGetter(putOnStack bool) {
  245. e.addSrcMap()
  246. if idx, found, noDynamics := e.c.scope.lookupName(e.name); noDynamics {
  247. if found {
  248. if putOnStack {
  249. e.c.emit(getLocal(idx))
  250. }
  251. } else {
  252. panic("No dynamics and not found")
  253. }
  254. } else {
  255. if found {
  256. e.c.emit(getVar{name: e.name, idx: idx})
  257. } else {
  258. e.c.emit(getVar1(e.name))
  259. }
  260. if !putOnStack {
  261. e.c.emit(pop)
  262. }
  263. }
  264. }
  265. func (e *compiledIdentifierExpr) emitGetterOrRef() {
  266. e.addSrcMap()
  267. if idx, found, noDynamics := e.c.scope.lookupName(e.name); noDynamics {
  268. if found {
  269. e.c.emit(getLocal(idx))
  270. } else {
  271. panic("No dynamics and not found")
  272. }
  273. } else {
  274. if found {
  275. e.c.emit(getVar{name: e.name, idx: idx, ref: true})
  276. } else {
  277. e.c.emit(getVar1Ref(e.name))
  278. }
  279. }
  280. }
  281. func (e *compiledIdentifierExpr) emitGetterAndCallee() {
  282. e.addSrcMap()
  283. if idx, found, noDynamics := e.c.scope.lookupName(e.name); noDynamics {
  284. if found {
  285. e.c.emit(loadUndef)
  286. e.c.emit(getLocal(idx))
  287. } else {
  288. panic("No dynamics and not found")
  289. }
  290. } else {
  291. if found {
  292. e.c.emit(getVar{name: e.name, idx: idx, ref: true, callee: true})
  293. } else {
  294. e.c.emit(getVar1Callee(e.name))
  295. }
  296. }
  297. }
  298. func (c *compiler) emitVarSetter1(name unistring.String, offset int, emitRight func(isRef bool)) {
  299. if c.scope.strict {
  300. c.checkIdentifierLName(name, offset)
  301. }
  302. if idx, found, noDynamics := c.scope.lookupName(name); noDynamics {
  303. emitRight(false)
  304. if found {
  305. c.emit(setLocal(idx))
  306. } else {
  307. if c.scope.strict {
  308. c.emit(setGlobalStrict(name))
  309. } else {
  310. c.emit(setGlobal(name))
  311. }
  312. }
  313. } else {
  314. if found {
  315. c.emit(resolveVar{name: name, idx: idx, strict: c.scope.strict})
  316. emitRight(true)
  317. c.emit(putValue)
  318. } else {
  319. if c.scope.strict {
  320. c.emit(resolveVar1Strict(name))
  321. } else {
  322. c.emit(resolveVar1(name))
  323. }
  324. emitRight(true)
  325. c.emit(putValue)
  326. }
  327. }
  328. }
  329. func (c *compiler) emitVarSetter(name unistring.String, offset int, valueExpr compiledExpr) {
  330. c.emitVarSetter1(name, offset, func(bool) {
  331. c.emitExpr(valueExpr, true)
  332. })
  333. }
  334. func (e *compiledVariableExpr) emitSetter(valueExpr compiledExpr) {
  335. e.c.emitVarSetter(e.name, e.offset, valueExpr)
  336. }
  337. func (e *compiledIdentifierExpr) emitSetter(valueExpr compiledExpr) {
  338. e.c.emitVarSetter(e.name, e.offset, valueExpr)
  339. }
  340. func (e *compiledIdentifierExpr) emitUnary(prepare, body func(), postfix, putOnStack bool) {
  341. if putOnStack {
  342. e.c.emitVarSetter1(e.name, e.offset, func(isRef bool) {
  343. e.c.emit(loadUndef)
  344. if isRef {
  345. e.c.emit(getValue)
  346. } else {
  347. e.emitGetter(true)
  348. }
  349. if prepare != nil {
  350. prepare()
  351. }
  352. if !postfix {
  353. body()
  354. }
  355. e.c.emit(rdupN(1))
  356. if postfix {
  357. body()
  358. }
  359. })
  360. e.c.emit(pop)
  361. } else {
  362. e.c.emitVarSetter1(e.name, e.offset, func(isRef bool) {
  363. if isRef {
  364. e.c.emit(getValue)
  365. } else {
  366. e.emitGetter(true)
  367. }
  368. body()
  369. })
  370. e.c.emit(pop)
  371. }
  372. }
  373. func (e *compiledIdentifierExpr) deleteExpr() compiledExpr {
  374. if e.c.scope.strict {
  375. e.c.throwSyntaxError(e.offset, "Delete of an unqualified identifier in strict mode")
  376. panic("Unreachable")
  377. }
  378. if _, found, noDynamics := e.c.scope.lookupName(e.name); noDynamics {
  379. if !found {
  380. r := &deleteGlobalExpr{
  381. name: e.name,
  382. }
  383. r.init(e.c, file.Idx(0))
  384. return r
  385. } else {
  386. r := &constantExpr{
  387. val: valueFalse,
  388. }
  389. r.init(e.c, file.Idx(0))
  390. return r
  391. }
  392. } else {
  393. r := &deleteVarExpr{
  394. name: e.name,
  395. }
  396. r.init(e.c, file.Idx(e.offset+1))
  397. return r
  398. }
  399. }
  400. type compiledDotExpr struct {
  401. baseCompiledExpr
  402. left compiledExpr
  403. name unistring.String
  404. }
  405. func (e *compiledDotExpr) emitGetter(putOnStack bool) {
  406. e.left.emitGetter(true)
  407. e.addSrcMap()
  408. e.c.emit(getProp(e.name))
  409. if !putOnStack {
  410. e.c.emit(pop)
  411. }
  412. }
  413. func (e *compiledDotExpr) emitSetter(valueExpr compiledExpr) {
  414. e.left.emitGetter(true)
  415. valueExpr.emitGetter(true)
  416. if e.c.scope.strict {
  417. e.c.emit(setPropStrict(e.name))
  418. } else {
  419. e.c.emit(setProp(e.name))
  420. }
  421. }
  422. func (e *compiledDotExpr) emitUnary(prepare, body func(), postfix, putOnStack bool) {
  423. if !putOnStack {
  424. e.left.emitGetter(true)
  425. e.c.emit(dup)
  426. e.c.emit(getProp(e.name))
  427. body()
  428. if e.c.scope.strict {
  429. e.c.emit(setPropStrict(e.name), pop)
  430. } else {
  431. e.c.emit(setProp(e.name), pop)
  432. }
  433. } else {
  434. if !postfix {
  435. e.left.emitGetter(true)
  436. e.c.emit(dup)
  437. e.c.emit(getProp(e.name))
  438. if prepare != nil {
  439. prepare()
  440. }
  441. body()
  442. if e.c.scope.strict {
  443. e.c.emit(setPropStrict(e.name))
  444. } else {
  445. e.c.emit(setProp(e.name))
  446. }
  447. } else {
  448. e.c.emit(loadUndef)
  449. e.left.emitGetter(true)
  450. e.c.emit(dup)
  451. e.c.emit(getProp(e.name))
  452. if prepare != nil {
  453. prepare()
  454. }
  455. e.c.emit(rdupN(2))
  456. body()
  457. if e.c.scope.strict {
  458. e.c.emit(setPropStrict(e.name))
  459. } else {
  460. e.c.emit(setProp(e.name))
  461. }
  462. e.c.emit(pop)
  463. }
  464. }
  465. }
  466. func (e *compiledDotExpr) deleteExpr() compiledExpr {
  467. r := &deletePropExpr{
  468. left: e.left,
  469. name: e.name,
  470. }
  471. r.init(e.c, file.Idx(0))
  472. return r
  473. }
  474. func (e *compiledBracketExpr) emitGetter(putOnStack bool) {
  475. e.left.emitGetter(true)
  476. e.member.emitGetter(true)
  477. e.addSrcMap()
  478. e.c.emit(getElem)
  479. if !putOnStack {
  480. e.c.emit(pop)
  481. }
  482. }
  483. func (e *compiledBracketExpr) emitSetter(valueExpr compiledExpr) {
  484. e.left.emitGetter(true)
  485. e.member.emitGetter(true)
  486. valueExpr.emitGetter(true)
  487. if e.c.scope.strict {
  488. e.c.emit(setElemStrict)
  489. } else {
  490. e.c.emit(setElem)
  491. }
  492. }
  493. func (e *compiledBracketExpr) emitUnary(prepare, body func(), postfix, putOnStack bool) {
  494. if !putOnStack {
  495. e.left.emitGetter(true)
  496. e.member.emitGetter(true)
  497. e.c.emit(dupN(1), dupN(1))
  498. e.c.emit(getElem)
  499. body()
  500. if e.c.scope.strict {
  501. e.c.emit(setElemStrict, pop)
  502. } else {
  503. e.c.emit(setElem, pop)
  504. }
  505. } else {
  506. if !postfix {
  507. e.left.emitGetter(true)
  508. e.member.emitGetter(true)
  509. e.c.emit(dupN(1), dupN(1))
  510. e.c.emit(getElem)
  511. if prepare != nil {
  512. prepare()
  513. }
  514. body()
  515. if e.c.scope.strict {
  516. e.c.emit(setElemStrict)
  517. } else {
  518. e.c.emit(setElem)
  519. }
  520. } else {
  521. e.c.emit(loadUndef)
  522. e.left.emitGetter(true)
  523. e.member.emitGetter(true)
  524. e.c.emit(dupN(1), dupN(1))
  525. e.c.emit(getElem)
  526. if prepare != nil {
  527. prepare()
  528. }
  529. e.c.emit(rdupN(3))
  530. body()
  531. if e.c.scope.strict {
  532. e.c.emit(setElemStrict, pop)
  533. } else {
  534. e.c.emit(setElem, pop)
  535. }
  536. }
  537. }
  538. }
  539. func (e *compiledBracketExpr) deleteExpr() compiledExpr {
  540. r := &deleteElemExpr{
  541. left: e.left,
  542. member: e.member,
  543. }
  544. r.init(e.c, file.Idx(0))
  545. return r
  546. }
  547. func (e *deleteElemExpr) emitGetter(putOnStack bool) {
  548. e.left.emitGetter(true)
  549. e.member.emitGetter(true)
  550. e.addSrcMap()
  551. if e.c.scope.strict {
  552. e.c.emit(deleteElemStrict)
  553. } else {
  554. e.c.emit(deleteElem)
  555. }
  556. if !putOnStack {
  557. e.c.emit(pop)
  558. }
  559. }
  560. func (e *deletePropExpr) emitGetter(putOnStack bool) {
  561. e.left.emitGetter(true)
  562. e.addSrcMap()
  563. if e.c.scope.strict {
  564. e.c.emit(deletePropStrict(e.name))
  565. } else {
  566. e.c.emit(deleteProp(e.name))
  567. }
  568. if !putOnStack {
  569. e.c.emit(pop)
  570. }
  571. }
  572. func (e *deleteVarExpr) emitGetter(putOnStack bool) {
  573. /*if e.c.scope.strict {
  574. e.c.throwSyntaxError(e.offset, "Delete of an unqualified identifier in strict mode")
  575. return
  576. }*/
  577. e.c.emit(deleteVar(e.name))
  578. if !putOnStack {
  579. e.c.emit(pop)
  580. }
  581. }
  582. func (e *deleteGlobalExpr) emitGetter(putOnStack bool) {
  583. /*if e.c.scope.strict {
  584. e.c.throwSyntaxError(e.offset, "Delete of an unqualified identifier in strict mode")
  585. return
  586. }*/
  587. e.c.emit(deleteGlobal(e.name))
  588. if !putOnStack {
  589. e.c.emit(pop)
  590. }
  591. }
  592. func (e *compiledAssignExpr) emitGetter(putOnStack bool) {
  593. e.addSrcMap()
  594. switch e.operator {
  595. case token.ASSIGN:
  596. e.left.emitSetter(e.right)
  597. case token.PLUS:
  598. e.left.emitUnary(nil, func() {
  599. e.right.emitGetter(true)
  600. e.c.emit(add)
  601. }, false, putOnStack)
  602. return
  603. case token.MINUS:
  604. e.left.emitUnary(nil, func() {
  605. e.right.emitGetter(true)
  606. e.c.emit(sub)
  607. }, false, putOnStack)
  608. return
  609. case token.MULTIPLY:
  610. e.left.emitUnary(nil, func() {
  611. e.right.emitGetter(true)
  612. e.c.emit(mul)
  613. }, false, putOnStack)
  614. return
  615. case token.SLASH:
  616. e.left.emitUnary(nil, func() {
  617. e.right.emitGetter(true)
  618. e.c.emit(div)
  619. }, false, putOnStack)
  620. return
  621. case token.REMAINDER:
  622. e.left.emitUnary(nil, func() {
  623. e.right.emitGetter(true)
  624. e.c.emit(mod)
  625. }, false, putOnStack)
  626. return
  627. case token.OR:
  628. e.left.emitUnary(nil, func() {
  629. e.right.emitGetter(true)
  630. e.c.emit(or)
  631. }, false, putOnStack)
  632. return
  633. case token.AND:
  634. e.left.emitUnary(nil, func() {
  635. e.right.emitGetter(true)
  636. e.c.emit(and)
  637. }, false, putOnStack)
  638. return
  639. case token.EXCLUSIVE_OR:
  640. e.left.emitUnary(nil, func() {
  641. e.right.emitGetter(true)
  642. e.c.emit(xor)
  643. }, false, putOnStack)
  644. return
  645. case token.SHIFT_LEFT:
  646. e.left.emitUnary(nil, func() {
  647. e.right.emitGetter(true)
  648. e.c.emit(sal)
  649. }, false, putOnStack)
  650. return
  651. case token.SHIFT_RIGHT:
  652. e.left.emitUnary(nil, func() {
  653. e.right.emitGetter(true)
  654. e.c.emit(sar)
  655. }, false, putOnStack)
  656. return
  657. case token.UNSIGNED_SHIFT_RIGHT:
  658. e.left.emitUnary(nil, func() {
  659. e.right.emitGetter(true)
  660. e.c.emit(shr)
  661. }, false, putOnStack)
  662. return
  663. default:
  664. panic(fmt.Errorf("Unknown assign operator: %s", e.operator.String()))
  665. }
  666. if !putOnStack {
  667. e.c.emit(pop)
  668. }
  669. }
  670. func (e *compiledLiteral) emitGetter(putOnStack bool) {
  671. if putOnStack {
  672. e.addSrcMap()
  673. e.c.emit(loadVal(e.c.p.defineLiteralValue(e.val)))
  674. }
  675. }
  676. func (e *compiledLiteral) constant() bool {
  677. return true
  678. }
  679. func (e *compiledFunctionLiteral) emitGetter(putOnStack bool) {
  680. e.c.newScope()
  681. savedBlockStart := e.c.blockStart
  682. savedPrg := e.c.p
  683. e.c.p = &Program{
  684. src: e.c.p.src,
  685. }
  686. e.c.blockStart = 0
  687. if e.expr.Name != nil {
  688. e.c.p.funcName = e.expr.Name.Name
  689. }
  690. block := e.c.block
  691. e.c.block = nil
  692. defer func() {
  693. e.c.block = block
  694. }()
  695. if !e.c.scope.strict {
  696. e.c.scope.strict = e.c.isStrictStatement(e.expr.Body)
  697. }
  698. if e.c.scope.strict {
  699. if e.expr.Name != nil {
  700. e.c.checkIdentifierLName(e.expr.Name.Name, int(e.expr.Name.Idx)-1)
  701. }
  702. for _, item := range e.expr.ParameterList.List {
  703. e.c.checkIdentifierName(item.Name, int(item.Idx)-1)
  704. e.c.checkIdentifierLName(item.Name, int(item.Idx)-1)
  705. }
  706. }
  707. length := len(e.expr.ParameterList.List)
  708. for _, item := range e.expr.ParameterList.List {
  709. _, unique := e.c.scope.bindNameShadow(item.Name)
  710. if !unique && e.c.scope.strict {
  711. e.c.throwSyntaxError(int(item.Idx)-1, "Strict mode function may not have duplicate parameter names (%s)", item.Name)
  712. return
  713. }
  714. }
  715. paramsCount := len(e.c.scope.names)
  716. e.c.compileDeclList(e.expr.DeclarationList, true)
  717. var needCallee bool
  718. var calleeIdx uint32
  719. if e.isExpr && e.expr.Name != nil {
  720. if idx, ok := e.c.scope.bindName(e.expr.Name.Name); ok {
  721. calleeIdx = idx
  722. needCallee = true
  723. }
  724. }
  725. maxPreambleLen := 2
  726. e.c.p.code = make([]instruction, maxPreambleLen)
  727. if needCallee {
  728. e.c.emit(loadCallee, setLocalP(calleeIdx))
  729. }
  730. e.c.compileFunctions(e.expr.DeclarationList)
  731. e.c.markBlockStart()
  732. e.c.compileStatement(e.expr.Body, false)
  733. if e.c.blockStart >= len(e.c.p.code)-1 || e.c.p.code[len(e.c.p.code)-1] != ret {
  734. e.c.emit(loadUndef, ret)
  735. }
  736. if !e.c.scope.dynamic && !e.c.scope.accessed {
  737. // log.Printf("Function can use inline stash")
  738. l := 0
  739. if !e.c.scope.strict && e.c.scope.thisNeeded {
  740. l = 2
  741. e.c.p.code = e.c.p.code[maxPreambleLen-2:]
  742. e.c.p.code[1] = boxThis
  743. } else {
  744. l = 1
  745. e.c.p.code = e.c.p.code[maxPreambleLen-1:]
  746. }
  747. e.c.convertFunctionToStashless(e.c.p.code, paramsCount)
  748. for i := range e.c.p.srcMap {
  749. e.c.p.srcMap[i].pc -= maxPreambleLen - l
  750. }
  751. } else {
  752. l := 1 + len(e.c.scope.names)
  753. if e.c.scope.argsNeeded {
  754. l += 2
  755. }
  756. if !e.c.scope.strict && e.c.scope.thisNeeded {
  757. l++
  758. }
  759. code := make([]instruction, l+len(e.c.p.code)-maxPreambleLen)
  760. code[0] = enterFunc(length)
  761. for name, nameIdx := range e.c.scope.names {
  762. code[nameIdx+1] = bindName(name)
  763. }
  764. pos := 1 + len(e.c.scope.names)
  765. if !e.c.scope.strict && e.c.scope.thisNeeded {
  766. code[pos] = boxThis
  767. pos++
  768. }
  769. if e.c.scope.argsNeeded {
  770. if e.c.scope.strict {
  771. code[pos] = createArgsStrict(length)
  772. } else {
  773. code[pos] = createArgs(length)
  774. }
  775. pos++
  776. idx, exists := e.c.scope.names["arguments"]
  777. if !exists {
  778. panic("No arguments")
  779. }
  780. code[pos] = setLocalP(idx)
  781. pos++
  782. }
  783. copy(code[l:], e.c.p.code[maxPreambleLen:])
  784. e.c.p.code = code
  785. for i := range e.c.p.srcMap {
  786. e.c.p.srcMap[i].pc += l - maxPreambleLen
  787. }
  788. }
  789. strict := e.c.scope.strict
  790. p := e.c.p
  791. // e.c.p.dumpCode()
  792. e.c.popScope()
  793. e.c.p = savedPrg
  794. e.c.blockStart = savedBlockStart
  795. var name unistring.String
  796. if e.expr.Name != nil {
  797. name = e.expr.Name.Name
  798. }
  799. e.c.emit(&newFunc{prg: p, length: uint32(length), name: name, srcStart: uint32(e.expr.Idx0() - 1), srcEnd: uint32(e.expr.Idx1() - 1), strict: strict})
  800. if !putOnStack {
  801. e.c.emit(pop)
  802. }
  803. }
  804. func (c *compiler) compileFunctionLiteral(v *ast.FunctionLiteral, isExpr bool) compiledExpr {
  805. if v.Name != nil && c.scope.strict {
  806. c.checkIdentifierLName(v.Name.Name, int(v.Name.Idx)-1)
  807. }
  808. r := &compiledFunctionLiteral{
  809. expr: v,
  810. isExpr: isExpr,
  811. }
  812. r.init(c, v.Idx0())
  813. return r
  814. }
  815. func nearestNonLexical(s *scope) *scope {
  816. for ; s != nil && s.lexical; s = s.outer {
  817. }
  818. return s
  819. }
  820. func (e *compiledThisExpr) emitGetter(putOnStack bool) {
  821. if putOnStack {
  822. e.addSrcMap()
  823. if e.c.scope.eval || e.c.scope.isFunction() {
  824. nearestNonLexical(e.c.scope).thisNeeded = true
  825. e.c.emit(loadStack(0))
  826. } else {
  827. e.c.emit(loadGlobalObject)
  828. }
  829. }
  830. }
  831. /*
  832. func (e *compiledThisExpr) deleteExpr() compiledExpr {
  833. r := &compiledLiteral{
  834. val: valueTrue,
  835. }
  836. r.init(e.c, 0)
  837. return r
  838. }
  839. */
  840. func (e *compiledNewExpr) emitGetter(putOnStack bool) {
  841. e.callee.emitGetter(true)
  842. for _, expr := range e.args {
  843. expr.emitGetter(true)
  844. }
  845. e.addSrcMap()
  846. e.c.emit(_new(len(e.args)))
  847. if !putOnStack {
  848. e.c.emit(pop)
  849. }
  850. }
  851. func (c *compiler) compileNewExpression(v *ast.NewExpression) compiledExpr {
  852. args := make([]compiledExpr, len(v.ArgumentList))
  853. for i, expr := range v.ArgumentList {
  854. args[i] = c.compileExpression(expr)
  855. }
  856. r := &compiledNewExpr{
  857. callee: c.compileExpression(v.Callee),
  858. args: args,
  859. }
  860. r.init(c, v.Idx0())
  861. return r
  862. }
  863. func (e *compiledNewTarget) emitGetter(putOnStack bool) {
  864. if putOnStack {
  865. e.addSrcMap()
  866. e.c.emit(loadNewTarget)
  867. }
  868. }
  869. func (c *compiler) compileMetaProperty(v *ast.MetaProperty) compiledExpr {
  870. if v.Meta.Name == "new" || v.Property.Name != "target" {
  871. r := &compiledNewTarget{}
  872. r.init(c, v.Idx0())
  873. return r
  874. }
  875. c.throwSyntaxError(int(v.Idx)-1, "Unsupported meta property: %s.%s", v.Meta.Name, v.Property.Name)
  876. return nil
  877. }
  878. func (e *compiledSequenceExpr) emitGetter(putOnStack bool) {
  879. if len(e.sequence) > 0 {
  880. for i := 0; i < len(e.sequence)-1; i++ {
  881. e.sequence[i].emitGetter(false)
  882. }
  883. e.sequence[len(e.sequence)-1].emitGetter(putOnStack)
  884. }
  885. }
  886. func (c *compiler) compileSequenceExpression(v *ast.SequenceExpression) compiledExpr {
  887. s := make([]compiledExpr, len(v.Sequence))
  888. for i, expr := range v.Sequence {
  889. s[i] = c.compileExpression(expr)
  890. }
  891. r := &compiledSequenceExpr{
  892. sequence: s,
  893. }
  894. var idx file.Idx
  895. if len(v.Sequence) > 0 {
  896. idx = v.Idx0()
  897. }
  898. r.init(c, idx)
  899. return r
  900. }
  901. func (c *compiler) emitThrow(v Value) {
  902. if o, ok := v.(*Object); ok {
  903. t := nilSafe(o.self.getStr("name", nil)).toString().String()
  904. switch t {
  905. case "TypeError":
  906. c.emit(getVar1(t))
  907. msg := o.self.getStr("message", nil)
  908. if msg != nil {
  909. c.emit(loadVal(c.p.defineLiteralValue(msg)))
  910. c.emit(_new(1))
  911. } else {
  912. c.emit(_new(0))
  913. }
  914. c.emit(throw)
  915. return
  916. }
  917. }
  918. panic(fmt.Errorf("unknown exception type thrown while evaliating constant expression: %s", v.String()))
  919. }
  920. func (c *compiler) emitConst(expr compiledExpr, putOnStack bool) {
  921. v, ex := c.evalConst(expr)
  922. if ex == nil {
  923. if putOnStack {
  924. c.emit(loadVal(c.p.defineLiteralValue(v)))
  925. }
  926. } else {
  927. c.emitThrow(ex.val)
  928. }
  929. }
  930. func (c *compiler) emitExpr(expr compiledExpr, putOnStack bool) {
  931. if expr.constant() {
  932. c.emitConst(expr, putOnStack)
  933. } else {
  934. expr.emitGetter(putOnStack)
  935. }
  936. }
  937. func (c *compiler) evalConst(expr compiledExpr) (Value, *Exception) {
  938. if expr, ok := expr.(*compiledLiteral); ok {
  939. return expr.val, nil
  940. }
  941. if c.evalVM == nil {
  942. c.evalVM = New().vm
  943. }
  944. var savedPrg *Program
  945. createdPrg := false
  946. if c.evalVM.prg == nil {
  947. c.evalVM.prg = &Program{}
  948. savedPrg = c.p
  949. c.p = c.evalVM.prg
  950. createdPrg = true
  951. }
  952. savedPc := len(c.p.code)
  953. expr.emitGetter(true)
  954. c.emit(halt)
  955. c.evalVM.pc = savedPc
  956. ex := c.evalVM.runTry()
  957. if createdPrg {
  958. c.evalVM.prg = nil
  959. c.evalVM.pc = 0
  960. c.p = savedPrg
  961. } else {
  962. c.evalVM.prg.code = c.evalVM.prg.code[:savedPc]
  963. c.p.code = c.evalVM.prg.code
  964. }
  965. if ex == nil {
  966. return c.evalVM.pop(), nil
  967. }
  968. return nil, ex
  969. }
  970. func (e *compiledUnaryExpr) constant() bool {
  971. return e.operand.constant()
  972. }
  973. func (e *compiledUnaryExpr) emitGetter(putOnStack bool) {
  974. var prepare, body func()
  975. toNumber := func() {
  976. e.c.emit(toNumber)
  977. }
  978. switch e.operator {
  979. case token.NOT:
  980. e.operand.emitGetter(true)
  981. e.c.emit(not)
  982. goto end
  983. case token.BITWISE_NOT:
  984. e.operand.emitGetter(true)
  985. e.c.emit(bnot)
  986. goto end
  987. case token.TYPEOF:
  988. if o, ok := e.operand.(compiledExprOrRef); ok {
  989. o.emitGetterOrRef()
  990. } else {
  991. e.operand.emitGetter(true)
  992. }
  993. e.c.emit(typeof)
  994. goto end
  995. case token.DELETE:
  996. e.operand.deleteExpr().emitGetter(putOnStack)
  997. return
  998. case token.MINUS:
  999. e.c.emitExpr(e.operand, true)
  1000. e.c.emit(neg)
  1001. goto end
  1002. case token.PLUS:
  1003. e.c.emitExpr(e.operand, true)
  1004. e.c.emit(plus)
  1005. goto end
  1006. case token.INCREMENT:
  1007. prepare = toNumber
  1008. body = func() {
  1009. e.c.emit(inc)
  1010. }
  1011. case token.DECREMENT:
  1012. prepare = toNumber
  1013. body = func() {
  1014. e.c.emit(dec)
  1015. }
  1016. case token.VOID:
  1017. e.c.emitExpr(e.operand, false)
  1018. if putOnStack {
  1019. e.c.emit(loadUndef)
  1020. }
  1021. return
  1022. default:
  1023. panic(fmt.Errorf("Unknown unary operator: %s", e.operator.String()))
  1024. }
  1025. e.operand.emitUnary(prepare, body, e.postfix, putOnStack)
  1026. return
  1027. end:
  1028. if !putOnStack {
  1029. e.c.emit(pop)
  1030. }
  1031. }
  1032. func (c *compiler) compileUnaryExpression(v *ast.UnaryExpression) compiledExpr {
  1033. r := &compiledUnaryExpr{
  1034. operand: c.compileExpression(v.Operand),
  1035. operator: v.Operator,
  1036. postfix: v.Postfix,
  1037. }
  1038. r.init(c, v.Idx0())
  1039. return r
  1040. }
  1041. func (e *compiledConditionalExpr) emitGetter(putOnStack bool) {
  1042. e.test.emitGetter(true)
  1043. j := len(e.c.p.code)
  1044. e.c.emit(nil)
  1045. e.consequent.emitGetter(putOnStack)
  1046. j1 := len(e.c.p.code)
  1047. e.c.emit(nil)
  1048. e.c.p.code[j] = jne(len(e.c.p.code) - j)
  1049. e.alternate.emitGetter(putOnStack)
  1050. e.c.p.code[j1] = jump(len(e.c.p.code) - j1)
  1051. }
  1052. func (c *compiler) compileConditionalExpression(v *ast.ConditionalExpression) compiledExpr {
  1053. r := &compiledConditionalExpr{
  1054. test: c.compileExpression(v.Test),
  1055. consequent: c.compileExpression(v.Consequent),
  1056. alternate: c.compileExpression(v.Alternate),
  1057. }
  1058. r.init(c, v.Idx0())
  1059. return r
  1060. }
  1061. func (e *compiledLogicalOr) constant() bool {
  1062. if e.left.constant() {
  1063. if v, ex := e.c.evalConst(e.left); ex == nil {
  1064. if v.ToBoolean() {
  1065. return true
  1066. }
  1067. return e.right.constant()
  1068. } else {
  1069. return true
  1070. }
  1071. }
  1072. return false
  1073. }
  1074. func (e *compiledLogicalOr) emitGetter(putOnStack bool) {
  1075. if e.left.constant() {
  1076. if v, ex := e.c.evalConst(e.left); ex == nil {
  1077. if !v.ToBoolean() {
  1078. e.c.emitExpr(e.right, putOnStack)
  1079. } else {
  1080. if putOnStack {
  1081. e.c.emit(loadVal(e.c.p.defineLiteralValue(v)))
  1082. }
  1083. }
  1084. } else {
  1085. e.c.emitThrow(ex.val)
  1086. }
  1087. return
  1088. }
  1089. e.c.emitExpr(e.left, true)
  1090. e.c.markBlockStart()
  1091. j := len(e.c.p.code)
  1092. e.addSrcMap()
  1093. e.c.emit(nil)
  1094. e.c.emit(pop)
  1095. e.c.emitExpr(e.right, true)
  1096. e.c.p.code[j] = jeq1(len(e.c.p.code) - j)
  1097. if !putOnStack {
  1098. e.c.emit(pop)
  1099. }
  1100. }
  1101. func (e *compiledLogicalAnd) constant() bool {
  1102. if e.left.constant() {
  1103. if v, ex := e.c.evalConst(e.left); ex == nil {
  1104. if !v.ToBoolean() {
  1105. return true
  1106. } else {
  1107. return e.right.constant()
  1108. }
  1109. } else {
  1110. return true
  1111. }
  1112. }
  1113. return false
  1114. }
  1115. func (e *compiledLogicalAnd) emitGetter(putOnStack bool) {
  1116. var j int
  1117. if e.left.constant() {
  1118. if v, ex := e.c.evalConst(e.left); ex == nil {
  1119. if !v.ToBoolean() {
  1120. e.c.emit(loadVal(e.c.p.defineLiteralValue(v)))
  1121. } else {
  1122. e.c.emitExpr(e.right, putOnStack)
  1123. }
  1124. } else {
  1125. e.c.emitThrow(ex.val)
  1126. }
  1127. return
  1128. }
  1129. e.left.emitGetter(true)
  1130. e.c.markBlockStart()
  1131. j = len(e.c.p.code)
  1132. e.addSrcMap()
  1133. e.c.emit(nil)
  1134. e.c.emit(pop)
  1135. e.c.emitExpr(e.right, true)
  1136. e.c.p.code[j] = jneq1(len(e.c.p.code) - j)
  1137. if !putOnStack {
  1138. e.c.emit(pop)
  1139. }
  1140. }
  1141. func (e *compiledBinaryExpr) constant() bool {
  1142. return e.left.constant() && e.right.constant()
  1143. }
  1144. func (e *compiledBinaryExpr) emitGetter(putOnStack bool) {
  1145. e.c.emitExpr(e.left, true)
  1146. e.c.emitExpr(e.right, true)
  1147. e.addSrcMap()
  1148. switch e.operator {
  1149. case token.LESS:
  1150. e.c.emit(op_lt)
  1151. case token.GREATER:
  1152. e.c.emit(op_gt)
  1153. case token.LESS_OR_EQUAL:
  1154. e.c.emit(op_lte)
  1155. case token.GREATER_OR_EQUAL:
  1156. e.c.emit(op_gte)
  1157. case token.EQUAL:
  1158. e.c.emit(op_eq)
  1159. case token.NOT_EQUAL:
  1160. e.c.emit(op_neq)
  1161. case token.STRICT_EQUAL:
  1162. e.c.emit(op_strict_eq)
  1163. case token.STRICT_NOT_EQUAL:
  1164. e.c.emit(op_strict_neq)
  1165. case token.PLUS:
  1166. e.c.emit(add)
  1167. case token.MINUS:
  1168. e.c.emit(sub)
  1169. case token.MULTIPLY:
  1170. e.c.emit(mul)
  1171. case token.SLASH:
  1172. e.c.emit(div)
  1173. case token.REMAINDER:
  1174. e.c.emit(mod)
  1175. case token.AND:
  1176. e.c.emit(and)
  1177. case token.OR:
  1178. e.c.emit(or)
  1179. case token.EXCLUSIVE_OR:
  1180. e.c.emit(xor)
  1181. case token.INSTANCEOF:
  1182. e.c.emit(op_instanceof)
  1183. case token.IN:
  1184. e.c.emit(op_in)
  1185. case token.SHIFT_LEFT:
  1186. e.c.emit(sal)
  1187. case token.SHIFT_RIGHT:
  1188. e.c.emit(sar)
  1189. case token.UNSIGNED_SHIFT_RIGHT:
  1190. e.c.emit(shr)
  1191. default:
  1192. panic(fmt.Errorf("Unknown operator: %s", e.operator.String()))
  1193. }
  1194. if !putOnStack {
  1195. e.c.emit(pop)
  1196. }
  1197. }
  1198. func (c *compiler) compileBinaryExpression(v *ast.BinaryExpression) compiledExpr {
  1199. switch v.Operator {
  1200. case token.LOGICAL_OR:
  1201. return c.compileLogicalOr(v.Left, v.Right, v.Idx0())
  1202. case token.LOGICAL_AND:
  1203. return c.compileLogicalAnd(v.Left, v.Right, v.Idx0())
  1204. }
  1205. r := &compiledBinaryExpr{
  1206. left: c.compileExpression(v.Left),
  1207. right: c.compileExpression(v.Right),
  1208. operator: v.Operator,
  1209. }
  1210. r.init(c, v.Idx0())
  1211. return r
  1212. }
  1213. func (c *compiler) compileLogicalOr(left, right ast.Expression, idx file.Idx) compiledExpr {
  1214. r := &compiledLogicalOr{
  1215. left: c.compileExpression(left),
  1216. right: c.compileExpression(right),
  1217. }
  1218. r.init(c, idx)
  1219. return r
  1220. }
  1221. func (c *compiler) compileLogicalAnd(left, right ast.Expression, idx file.Idx) compiledExpr {
  1222. r := &compiledLogicalAnd{
  1223. left: c.compileExpression(left),
  1224. right: c.compileExpression(right),
  1225. }
  1226. r.init(c, idx)
  1227. return r
  1228. }
  1229. func (e *compiledVariableExpr) emitGetter(putOnStack bool) {
  1230. if e.initializer != nil {
  1231. idExpr := &compiledIdentifierExpr{
  1232. name: e.name,
  1233. }
  1234. idExpr.init(e.c, file.Idx(0))
  1235. idExpr.emitSetter(e.initializer)
  1236. if !putOnStack {
  1237. e.c.emit(pop)
  1238. }
  1239. } else {
  1240. if putOnStack {
  1241. e.c.emit(loadUndef)
  1242. }
  1243. }
  1244. }
  1245. func (c *compiler) compileVariableExpression(v *ast.VariableExpression) compiledExpr {
  1246. r := &compiledVariableExpr{
  1247. name: v.Name,
  1248. initializer: c.compileExpression(v.Initializer),
  1249. }
  1250. r.init(c, v.Idx0())
  1251. return r
  1252. }
  1253. func (e *compiledObjectLiteral) emitGetter(putOnStack bool) {
  1254. e.addSrcMap()
  1255. e.c.emit(newObject)
  1256. for _, prop := range e.expr.Value {
  1257. keyExpr := e.c.compileExpression(prop.Key)
  1258. cl, ok := keyExpr.(*compiledLiteral)
  1259. if !ok {
  1260. e.c.throwSyntaxError(e.offset, "non-literal properties in object literal are not supported yet")
  1261. }
  1262. key := cl.val.string()
  1263. e.c.compileExpression(prop.Value).emitGetter(true)
  1264. switch prop.Kind {
  1265. case "value":
  1266. if key == __proto__ {
  1267. e.c.emit(setProto)
  1268. } else {
  1269. e.c.emit(setProp1(key))
  1270. }
  1271. case "get":
  1272. e.c.emit(setPropGetter(key))
  1273. case "set":
  1274. e.c.emit(setPropSetter(key))
  1275. default:
  1276. panic(fmt.Errorf("Unknown property kind: %s", prop.Kind))
  1277. }
  1278. }
  1279. if !putOnStack {
  1280. e.c.emit(pop)
  1281. }
  1282. }
  1283. func (c *compiler) compileObjectLiteral(v *ast.ObjectLiteral) compiledExpr {
  1284. r := &compiledObjectLiteral{
  1285. expr: v,
  1286. }
  1287. r.init(c, v.Idx0())
  1288. return r
  1289. }
  1290. func (e *compiledArrayLiteral) emitGetter(putOnStack bool) {
  1291. e.addSrcMap()
  1292. objCount := 0
  1293. for _, v := range e.expr.Value {
  1294. if v != nil {
  1295. e.c.compileExpression(v).emitGetter(true)
  1296. objCount++
  1297. } else {
  1298. e.c.emit(loadNil)
  1299. }
  1300. }
  1301. if objCount == len(e.expr.Value) {
  1302. e.c.emit(newArray(objCount))
  1303. } else {
  1304. e.c.emit(&newArraySparse{
  1305. l: len(e.expr.Value),
  1306. objCount: objCount,
  1307. })
  1308. }
  1309. if !putOnStack {
  1310. e.c.emit(pop)
  1311. }
  1312. }
  1313. func (c *compiler) compileArrayLiteral(v *ast.ArrayLiteral) compiledExpr {
  1314. r := &compiledArrayLiteral{
  1315. expr: v,
  1316. }
  1317. r.init(c, v.Idx0())
  1318. return r
  1319. }
  1320. func (e *compiledRegexpLiteral) emitGetter(putOnStack bool) {
  1321. if putOnStack {
  1322. pattern, err := compileRegexp(e.expr.Pattern, e.expr.Flags)
  1323. if err != nil {
  1324. e.c.throwSyntaxError(e.offset, err.Error())
  1325. }
  1326. e.c.emit(&newRegexp{pattern: pattern, src: newStringValue(e.expr.Pattern)})
  1327. }
  1328. }
  1329. func (c *compiler) compileRegexpLiteral(v *ast.RegExpLiteral) compiledExpr {
  1330. r := &compiledRegexpLiteral{
  1331. expr: v,
  1332. }
  1333. r.init(c, v.Idx0())
  1334. return r
  1335. }
  1336. func (e *compiledCallExpr) emitGetter(putOnStack bool) {
  1337. var calleeName unistring.String
  1338. switch callee := e.callee.(type) {
  1339. case *compiledDotExpr:
  1340. callee.left.emitGetter(true)
  1341. e.c.emit(dup)
  1342. e.c.emit(getPropCallee(callee.name))
  1343. case *compiledBracketExpr:
  1344. callee.left.emitGetter(true)
  1345. e.c.emit(dup)
  1346. callee.member.emitGetter(true)
  1347. e.c.emit(getElemCallee)
  1348. case *compiledIdentifierExpr:
  1349. calleeName = callee.name
  1350. callee.emitGetterAndCallee()
  1351. default:
  1352. e.c.emit(loadUndef)
  1353. callee.emitGetter(true)
  1354. }
  1355. for _, expr := range e.args {
  1356. expr.emitGetter(true)
  1357. }
  1358. e.addSrcMap()
  1359. if calleeName == "eval" {
  1360. e.c.scope.dynamic = true
  1361. e.c.scope.thisNeeded = true
  1362. if e.c.scope.lexical {
  1363. e.c.scope.outer.dynamic = true
  1364. }
  1365. e.c.scope.accessed = true
  1366. if e.c.scope.strict {
  1367. e.c.emit(callEvalStrict(len(e.args)))
  1368. } else {
  1369. e.c.emit(callEval(len(e.args)))
  1370. }
  1371. } else {
  1372. e.c.emit(call(len(e.args)))
  1373. }
  1374. if !putOnStack {
  1375. e.c.emit(pop)
  1376. }
  1377. }
  1378. func (e *compiledCallExpr) deleteExpr() compiledExpr {
  1379. r := &defaultDeleteExpr{
  1380. expr: e,
  1381. }
  1382. r.init(e.c, file.Idx(e.offset+1))
  1383. return r
  1384. }
  1385. func (c *compiler) compileCallExpression(v *ast.CallExpression) compiledExpr {
  1386. args := make([]compiledExpr, len(v.ArgumentList))
  1387. for i, argExpr := range v.ArgumentList {
  1388. args[i] = c.compileExpression(argExpr)
  1389. }
  1390. r := &compiledCallExpr{
  1391. args: args,
  1392. callee: c.compileExpression(v.Callee),
  1393. }
  1394. r.init(c, v.LeftParenthesis)
  1395. return r
  1396. }
  1397. func (c *compiler) compileIdentifierExpression(v *ast.Identifier) compiledExpr {
  1398. if c.scope.strict {
  1399. c.checkIdentifierName(v.Name, int(v.Idx)-1)
  1400. }
  1401. r := &compiledIdentifierExpr{
  1402. name: v.Name,
  1403. }
  1404. r.offset = int(v.Idx) - 1
  1405. r.init(c, v.Idx0())
  1406. return r
  1407. }
  1408. func (c *compiler) compileNumberLiteral(v *ast.NumberLiteral) compiledExpr {
  1409. if c.scope.strict && octalRegexp.MatchString(v.Literal) {
  1410. c.throwSyntaxError(int(v.Idx)-1, "Octal literals are not allowed in strict mode")
  1411. panic("Unreachable")
  1412. }
  1413. var val Value
  1414. switch num := v.Value.(type) {
  1415. case int64:
  1416. val = intToValue(num)
  1417. case float64:
  1418. val = floatToValue(num)
  1419. default:
  1420. panic(fmt.Errorf("Unsupported number literal type: %T", v.Value))
  1421. }
  1422. r := &compiledLiteral{
  1423. val: val,
  1424. }
  1425. r.init(c, v.Idx0())
  1426. return r
  1427. }
  1428. func (c *compiler) compileStringLiteral(v *ast.StringLiteral) compiledExpr {
  1429. r := &compiledLiteral{
  1430. val: stringValueFromRaw(v.Value),
  1431. }
  1432. r.init(c, v.Idx0())
  1433. return r
  1434. }
  1435. func (c *compiler) compileBooleanLiteral(v *ast.BooleanLiteral) compiledExpr {
  1436. var val Value
  1437. if v.Value {
  1438. val = valueTrue
  1439. } else {
  1440. val = valueFalse
  1441. }
  1442. r := &compiledLiteral{
  1443. val: val,
  1444. }
  1445. r.init(c, v.Idx0())
  1446. return r
  1447. }
  1448. func (c *compiler) compileAssignExpression(v *ast.AssignExpression) compiledExpr {
  1449. // log.Printf("compileAssignExpression(): %+v", v)
  1450. r := &compiledAssignExpr{
  1451. left: c.compileExpression(v.Left),
  1452. right: c.compileExpression(v.Right),
  1453. operator: v.Operator,
  1454. }
  1455. r.init(c, v.Idx0())
  1456. return r
  1457. }
  1458. func (e *compiledEnumGetExpr) emitGetter(putOnStack bool) {
  1459. e.c.emit(enumGet)
  1460. if !putOnStack {
  1461. e.c.emit(pop)
  1462. }
  1463. }