compiler_expr.go 32 KB

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