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. lenBefore := len(e.c.scope.names)
  703. namesBefore := make([]string, 0, lenBefore)
  704. for key, _ := range e.c.scope.names {
  705. namesBefore = append(namesBefore, key)
  706. }
  707. maxPreambleLen := 2
  708. e.c.p.code = make([]instruction, maxPreambleLen)
  709. if needCallee {
  710. e.c.emit(loadCallee, setLocalP(calleeIdx))
  711. }
  712. e.c.compileFunctions(e.expr.DeclarationList)
  713. e.c.compileStatement(e.expr.Body, false)
  714. if e.c.blockStart >= len(e.c.p.code)-1 || e.c.p.code[len(e.c.p.code)-1] != ret {
  715. e.c.emit(loadUndef, ret)
  716. }
  717. if !e.c.scope.dynamic && !e.c.scope.accessed {
  718. // log.Printf("Function can use inline stash")
  719. l := 0
  720. if !e.c.scope.strict && e.c.scope.thisNeeded {
  721. l = 2
  722. e.c.p.code = e.c.p.code[maxPreambleLen-2:]
  723. e.c.p.code[1] = boxThis
  724. } else {
  725. l = 1
  726. e.c.p.code = e.c.p.code[maxPreambleLen-1:]
  727. }
  728. e.c.convertFunctionToStashless(e.c.p.code, paramsCount)
  729. for i, _ := range e.c.p.srcMap {
  730. e.c.p.srcMap[i].pc -= maxPreambleLen - l
  731. }
  732. } else {
  733. l := 1 + len(e.c.scope.names)
  734. if e.c.scope.argsNeeded {
  735. l += 2
  736. }
  737. if !e.c.scope.strict && e.c.scope.thisNeeded {
  738. l++
  739. }
  740. code := make([]instruction, l+len(e.c.p.code)-maxPreambleLen)
  741. code[0] = enterFunc(length)
  742. for name, nameIdx := range e.c.scope.names {
  743. code[nameIdx+1] = bindName(name)
  744. }
  745. pos := 1 + len(e.c.scope.names)
  746. if !e.c.scope.strict && e.c.scope.thisNeeded {
  747. code[pos] = boxThis
  748. pos++
  749. }
  750. if e.c.scope.argsNeeded {
  751. if e.c.scope.strict {
  752. code[pos] = createArgsStrict(length)
  753. } else {
  754. code[pos] = createArgs(length)
  755. }
  756. pos++
  757. idx, exists := e.c.scope.names["arguments"]
  758. if !exists {
  759. panic("No arguments")
  760. }
  761. code[pos] = setLocalP(idx)
  762. pos++
  763. }
  764. copy(code[l:], e.c.p.code[maxPreambleLen:])
  765. e.c.p.code = code
  766. for i, _ := range e.c.p.srcMap {
  767. e.c.p.srcMap[i].pc += l - maxPreambleLen
  768. }
  769. }
  770. strict := e.c.scope.strict
  771. p := e.c.p
  772. // e.c.p.dumpCode()
  773. e.c.popScope()
  774. e.c.p = savedPrg
  775. e.c.blockStart = savedBlockStart
  776. name := ""
  777. if e.expr.Name != nil {
  778. name = e.expr.Name.Name
  779. }
  780. 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})
  781. if !putOnStack {
  782. e.c.emit(pop)
  783. }
  784. }
  785. func (c *compiler) compileFunctionLiteral(v *ast.FunctionLiteral, isExpr bool) compiledExpr {
  786. if v.Name != nil && c.scope.strict {
  787. c.checkIdentifierLName(v.Name.Name, int(v.Name.Idx)-1)
  788. }
  789. r := &compiledFunctionLiteral{
  790. expr: v,
  791. isExpr: isExpr,
  792. }
  793. r.init(c, v.Idx0())
  794. return r
  795. }
  796. func nearestNonLexical(s *scope) *scope {
  797. for ; s != nil && s.lexical; s = s.outer {
  798. }
  799. return s
  800. }
  801. func (e *compiledThisExpr) emitGetter(putOnStack bool) {
  802. if putOnStack {
  803. e.addSrcMap()
  804. if e.c.scope.eval || e.c.scope.isFunction() {
  805. nearestNonLexical(e.c.scope).thisNeeded = true
  806. e.c.emit(loadStack(0))
  807. } else {
  808. e.c.emit(loadGlobalObject)
  809. }
  810. }
  811. }
  812. /*
  813. func (e *compiledThisExpr) deleteExpr() compiledExpr {
  814. r := &compiledLiteral{
  815. val: valueTrue,
  816. }
  817. r.init(e.c, 0)
  818. return r
  819. }
  820. */
  821. func (e *compiledNewExpr) emitGetter(putOnStack bool) {
  822. e.callee.emitGetter(true)
  823. for _, expr := range e.args {
  824. expr.emitGetter(true)
  825. }
  826. e.addSrcMap()
  827. e.c.emit(_new(len(e.args)))
  828. if !putOnStack {
  829. e.c.emit(pop)
  830. }
  831. }
  832. func (c *compiler) compileNewExpression(v *ast.NewExpression) compiledExpr {
  833. args := make([]compiledExpr, len(v.ArgumentList))
  834. for i, expr := range v.ArgumentList {
  835. args[i] = c.compileExpression(expr)
  836. }
  837. r := &compiledNewExpr{
  838. callee: c.compileExpression(v.Callee),
  839. args: args,
  840. }
  841. r.init(c, v.Idx0())
  842. return r
  843. }
  844. func (e *compiledSequenceExpr) emitGetter(putOnStack bool) {
  845. if len(e.sequence) > 0 {
  846. for i := 0; i < len(e.sequence)-1; i++ {
  847. e.sequence[i].emitGetter(false)
  848. }
  849. e.sequence[len(e.sequence)-1].emitGetter(putOnStack)
  850. }
  851. }
  852. func (c *compiler) compileSequenceExpression(v *ast.SequenceExpression) compiledExpr {
  853. s := make([]compiledExpr, len(v.Sequence))
  854. for i, expr := range v.Sequence {
  855. s[i] = c.compileExpression(expr)
  856. }
  857. r := &compiledSequenceExpr{
  858. sequence: s,
  859. }
  860. var idx file.Idx
  861. if len(v.Sequence) > 0 {
  862. idx = v.Idx0()
  863. }
  864. r.init(c, idx)
  865. return r
  866. }
  867. func (c *compiler) emitThrow(v Value) {
  868. if o, ok := v.(*Object); ok {
  869. t := o.self.getStr("name").String()
  870. switch t {
  871. case "TypeError":
  872. c.emit(getVar1(t))
  873. msg := o.self.getStr("message")
  874. if msg != nil {
  875. c.emit(loadVal(c.p.defineLiteralValue(msg)))
  876. c.emit(_new(1))
  877. } else {
  878. c.emit(_new(0))
  879. }
  880. c.emit(throw)
  881. return
  882. }
  883. }
  884. panic(fmt.Errorf("Unknown exception type thrown while evaliating constant expression: %s", v.String()))
  885. }
  886. func (c *compiler) emitConst(expr compiledExpr, putOnStack bool) {
  887. v, ex := c.evalConst(expr)
  888. if ex == nil {
  889. if putOnStack {
  890. c.emit(loadVal(c.p.defineLiteralValue(v)))
  891. }
  892. } else {
  893. c.emitThrow(ex.val)
  894. }
  895. }
  896. func (c *compiler) emitExpr(expr compiledExpr, putOnStack bool) {
  897. if expr.constant() {
  898. c.emitConst(expr, putOnStack)
  899. } else {
  900. expr.emitGetter(putOnStack)
  901. }
  902. }
  903. func (c *compiler) evalConst(expr compiledExpr) (Value, *Exception) {
  904. if expr, ok := expr.(*compiledLiteral); ok {
  905. return expr.val, nil
  906. }
  907. var savedPrg *Program
  908. createdPrg := false
  909. if c.evalVM.prg == nil {
  910. c.evalVM.prg = &Program{}
  911. savedPrg = c.p
  912. c.p = c.evalVM.prg
  913. createdPrg = true
  914. }
  915. savedPc := len(c.p.code)
  916. expr.emitGetter(true)
  917. c.emit(halt)
  918. c.evalVM.pc = savedPc
  919. ex := c.evalVM.runTry()
  920. if createdPrg {
  921. c.evalVM.prg = nil
  922. c.evalVM.pc = 0
  923. c.p = savedPrg
  924. } else {
  925. c.evalVM.prg.code = c.evalVM.prg.code[:savedPc]
  926. c.p.code = c.evalVM.prg.code
  927. }
  928. if ex == nil {
  929. return c.evalVM.pop(), nil
  930. }
  931. return nil, ex
  932. }
  933. func (e *compiledUnaryExpr) constant() bool {
  934. return e.operand.constant()
  935. }
  936. func (e *compiledUnaryExpr) emitGetter(putOnStack bool) {
  937. var prepare, body func()
  938. toNumber := func() {
  939. e.c.emit(toNumber)
  940. }
  941. switch e.operator {
  942. case token.NOT:
  943. e.operand.emitGetter(true)
  944. e.c.emit(not)
  945. goto end
  946. case token.BITWISE_NOT:
  947. e.operand.emitGetter(true)
  948. e.c.emit(bnot)
  949. goto end
  950. case token.TYPEOF:
  951. if o, ok := e.operand.(compiledExprOrRef); ok {
  952. o.emitGetterOrRef()
  953. } else {
  954. e.operand.emitGetter(true)
  955. }
  956. e.c.emit(typeof)
  957. goto end
  958. case token.DELETE:
  959. e.operand.deleteExpr().emitGetter(putOnStack)
  960. return
  961. case token.MINUS:
  962. e.c.emitExpr(e.operand, true)
  963. e.c.emit(neg)
  964. goto end
  965. case token.PLUS:
  966. e.c.emitExpr(e.operand, true)
  967. e.c.emit(plus)
  968. goto end
  969. case token.INCREMENT:
  970. prepare = toNumber
  971. body = func() {
  972. e.c.emit(inc)
  973. }
  974. case token.DECREMENT:
  975. prepare = toNumber
  976. body = func() {
  977. e.c.emit(dec)
  978. }
  979. case token.VOID:
  980. e.c.emitExpr(e.operand, false)
  981. if putOnStack {
  982. e.c.emit(loadUndef)
  983. }
  984. return
  985. default:
  986. panic(fmt.Errorf("Unknown unary operator: %s", e.operator.String()))
  987. }
  988. e.operand.emitUnary(prepare, body, e.postfix, putOnStack)
  989. return
  990. end:
  991. if !putOnStack {
  992. e.c.emit(pop)
  993. }
  994. }
  995. func (c *compiler) compileUnaryExpression(v *ast.UnaryExpression) compiledExpr {
  996. r := &compiledUnaryExpr{
  997. operand: c.compileExpression(v.Operand),
  998. operator: v.Operator,
  999. postfix: v.Postfix,
  1000. }
  1001. r.init(c, v.Idx0())
  1002. return r
  1003. }
  1004. func (e *compiledConditionalExpr) emitGetter(putOnStack bool) {
  1005. e.test.emitGetter(true)
  1006. j := len(e.c.p.code)
  1007. e.c.emit(nil)
  1008. e.consequent.emitGetter(putOnStack)
  1009. j1 := len(e.c.p.code)
  1010. e.c.emit(nil)
  1011. e.c.p.code[j] = jne(len(e.c.p.code) - j)
  1012. e.alternate.emitGetter(putOnStack)
  1013. e.c.p.code[j1] = jump(len(e.c.p.code) - j1)
  1014. }
  1015. func (c *compiler) compileConditionalExpression(v *ast.ConditionalExpression) compiledExpr {
  1016. r := &compiledConditionalExpr{
  1017. test: c.compileExpression(v.Test),
  1018. consequent: c.compileExpression(v.Consequent),
  1019. alternate: c.compileExpression(v.Alternate),
  1020. }
  1021. r.init(c, v.Idx0())
  1022. return r
  1023. }
  1024. func (e *compiledLogicalOr) constant() bool {
  1025. if e.left.constant() {
  1026. if v, ex := e.c.evalConst(e.left); ex == nil {
  1027. if v.ToBoolean() {
  1028. return true
  1029. }
  1030. return e.right.constant()
  1031. } else {
  1032. return true
  1033. }
  1034. }
  1035. return false
  1036. }
  1037. func (e *compiledLogicalOr) emitGetter(putOnStack bool) {
  1038. if e.left.constant() {
  1039. if v, ex := e.c.evalConst(e.left); ex == nil {
  1040. if !v.ToBoolean() {
  1041. e.c.emitExpr(e.right, putOnStack)
  1042. } else {
  1043. if putOnStack {
  1044. e.c.emit(loadVal(e.c.p.defineLiteralValue(v)))
  1045. }
  1046. }
  1047. } else {
  1048. e.c.emitThrow(ex.val)
  1049. }
  1050. return
  1051. }
  1052. e.c.emitExpr(e.left, true)
  1053. e.c.markBlockStart()
  1054. j := len(e.c.p.code)
  1055. e.addSrcMap()
  1056. e.c.emit(nil)
  1057. e.c.emit(pop)
  1058. e.c.emitExpr(e.right, true)
  1059. e.c.p.code[j] = jeq1(len(e.c.p.code) - j)
  1060. if !putOnStack {
  1061. e.c.emit(pop)
  1062. }
  1063. }
  1064. func (e *compiledLogicalAnd) constant() bool {
  1065. if e.left.constant() {
  1066. if v, ex := e.c.evalConst(e.left); ex == nil {
  1067. if !v.ToBoolean() {
  1068. return true
  1069. } else {
  1070. return e.right.constant()
  1071. }
  1072. } else {
  1073. return true
  1074. }
  1075. }
  1076. return false
  1077. }
  1078. func (e *compiledLogicalAnd) emitGetter(putOnStack bool) {
  1079. var j int
  1080. if e.left.constant() {
  1081. if v, ex := e.c.evalConst(e.left); ex == nil {
  1082. if !v.ToBoolean() {
  1083. e.c.emit(loadVal(e.c.p.defineLiteralValue(v)))
  1084. } else {
  1085. e.c.emitExpr(e.right, putOnStack)
  1086. }
  1087. } else {
  1088. e.c.emitThrow(ex.val)
  1089. }
  1090. return
  1091. }
  1092. e.left.emitGetter(true)
  1093. e.c.markBlockStart()
  1094. j = len(e.c.p.code)
  1095. e.addSrcMap()
  1096. e.c.emit(nil)
  1097. e.c.emit(pop)
  1098. e.c.emitExpr(e.right, true)
  1099. e.c.p.code[j] = jneq1(len(e.c.p.code) - j)
  1100. if !putOnStack {
  1101. e.c.emit(pop)
  1102. }
  1103. }
  1104. func (e *compiledBinaryExpr) constant() bool {
  1105. return e.left.constant() && e.right.constant()
  1106. }
  1107. func (e *compiledBinaryExpr) emitGetter(putOnStack bool) {
  1108. e.c.emitExpr(e.left, true)
  1109. e.c.emitExpr(e.right, true)
  1110. e.addSrcMap()
  1111. switch e.operator {
  1112. case token.LESS:
  1113. e.c.emit(op_lt)
  1114. case token.GREATER:
  1115. e.c.emit(op_gt)
  1116. case token.LESS_OR_EQUAL:
  1117. e.c.emit(op_lte)
  1118. case token.GREATER_OR_EQUAL:
  1119. e.c.emit(op_gte)
  1120. case token.EQUAL:
  1121. e.c.emit(op_eq)
  1122. case token.NOT_EQUAL:
  1123. e.c.emit(op_neq)
  1124. case token.STRICT_EQUAL:
  1125. e.c.emit(op_strict_eq)
  1126. case token.STRICT_NOT_EQUAL:
  1127. e.c.emit(op_strict_neq)
  1128. case token.PLUS:
  1129. e.c.emit(add)
  1130. case token.MINUS:
  1131. e.c.emit(sub)
  1132. case token.MULTIPLY:
  1133. e.c.emit(mul)
  1134. case token.SLASH:
  1135. e.c.emit(div)
  1136. case token.REMAINDER:
  1137. e.c.emit(mod)
  1138. case token.AND:
  1139. e.c.emit(and)
  1140. case token.OR:
  1141. e.c.emit(or)
  1142. case token.EXCLUSIVE_OR:
  1143. e.c.emit(xor)
  1144. case token.INSTANCEOF:
  1145. e.c.emit(op_instanceof)
  1146. case token.IN:
  1147. e.c.emit(op_in)
  1148. case token.SHIFT_LEFT:
  1149. e.c.emit(sal)
  1150. case token.SHIFT_RIGHT:
  1151. e.c.emit(sar)
  1152. case token.UNSIGNED_SHIFT_RIGHT:
  1153. e.c.emit(shr)
  1154. default:
  1155. panic(fmt.Errorf("Unknown operator: %s", e.operator.String()))
  1156. }
  1157. if !putOnStack {
  1158. e.c.emit(pop)
  1159. }
  1160. }
  1161. func (c *compiler) compileBinaryExpression(v *ast.BinaryExpression) compiledExpr {
  1162. switch v.Operator {
  1163. case token.LOGICAL_OR:
  1164. return c.compileLogicalOr(v.Left, v.Right, v.Idx0())
  1165. case token.LOGICAL_AND:
  1166. return c.compileLogicalAnd(v.Left, v.Right, v.Idx0())
  1167. }
  1168. r := &compiledBinaryExpr{
  1169. left: c.compileExpression(v.Left),
  1170. right: c.compileExpression(v.Right),
  1171. operator: v.Operator,
  1172. }
  1173. r.init(c, v.Idx0())
  1174. return r
  1175. }
  1176. func (c *compiler) compileLogicalOr(left, right ast.Expression, idx file.Idx) compiledExpr {
  1177. r := &compiledLogicalOr{
  1178. left: c.compileExpression(left),
  1179. right: c.compileExpression(right),
  1180. }
  1181. r.init(c, idx)
  1182. return r
  1183. }
  1184. func (c *compiler) compileLogicalAnd(left, right ast.Expression, idx file.Idx) compiledExpr {
  1185. r := &compiledLogicalAnd{
  1186. left: c.compileExpression(left),
  1187. right: c.compileExpression(right),
  1188. }
  1189. r.init(c, idx)
  1190. return r
  1191. }
  1192. func (e *compiledVariableExpr) emitGetter(putOnStack bool) {
  1193. if e.initializer != nil {
  1194. idExpr := &compiledIdentifierExpr{
  1195. name: e.name,
  1196. }
  1197. idExpr.init(e.c, file.Idx(0))
  1198. idExpr.emitSetter(e.initializer)
  1199. if !putOnStack {
  1200. e.c.emit(pop)
  1201. }
  1202. } else {
  1203. if putOnStack {
  1204. e.c.emit(loadUndef)
  1205. }
  1206. }
  1207. }
  1208. func (c *compiler) compileVariableExpression(v *ast.VariableExpression) compiledExpr {
  1209. r := &compiledVariableExpr{
  1210. name: v.Name,
  1211. initializer: c.compileExpression(v.Initializer),
  1212. }
  1213. r.init(c, v.Idx0())
  1214. return r
  1215. }
  1216. func (e *compiledObjectLiteral) emitGetter(putOnStack bool) {
  1217. e.addSrcMap()
  1218. e.c.emit(newObject)
  1219. for _, prop := range e.expr.Value {
  1220. e.c.compileExpression(prop.Value).emitGetter(true)
  1221. switch prop.Kind {
  1222. case "value":
  1223. if prop.Key == "__proto__" {
  1224. e.c.emit(setProto)
  1225. } else {
  1226. e.c.emit(setProp1(prop.Key))
  1227. }
  1228. case "get":
  1229. e.c.emit(setPropGetter(prop.Key))
  1230. case "set":
  1231. e.c.emit(setPropSetter(prop.Key))
  1232. default:
  1233. panic(fmt.Errorf("Unknown property kind: %s", prop.Kind))
  1234. }
  1235. }
  1236. if !putOnStack {
  1237. e.c.emit(pop)
  1238. }
  1239. }
  1240. func (c *compiler) compileObjectLiteral(v *ast.ObjectLiteral) compiledExpr {
  1241. r := &compiledObjectLiteral{
  1242. expr: v,
  1243. }
  1244. r.init(c, v.Idx0())
  1245. return r
  1246. }
  1247. func (e *compiledArrayLiteral) emitGetter(putOnStack bool) {
  1248. e.addSrcMap()
  1249. for _, v := range e.expr.Value {
  1250. if v != nil {
  1251. e.c.compileExpression(v).emitGetter(true)
  1252. } else {
  1253. e.c.emit(loadNil)
  1254. }
  1255. }
  1256. e.c.emit(newArray(len(e.expr.Value)))
  1257. if !putOnStack {
  1258. e.c.emit(pop)
  1259. }
  1260. }
  1261. func (c *compiler) compileArrayLiteral(v *ast.ArrayLiteral) compiledExpr {
  1262. r := &compiledArrayLiteral{
  1263. expr: v,
  1264. }
  1265. r.init(c, v.Idx0())
  1266. return r
  1267. }
  1268. func (e *compiledRegexpLiteral) emitGetter(putOnStack bool) {
  1269. if putOnStack {
  1270. pattern, global, ignoreCase, multiline, err := compileRegexp(e.expr.Pattern, e.expr.Flags)
  1271. if err != nil {
  1272. e.c.throwSyntaxError(e.offset, err.Error())
  1273. }
  1274. e.c.emit(&newRegexp{pattern: pattern,
  1275. src: newStringValue(e.expr.Pattern),
  1276. global: global,
  1277. ignoreCase: ignoreCase,
  1278. multiline: multiline,
  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. }