compiler_expr.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  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, 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. })
  1279. }
  1280. }
  1281. func (c *compiler) compileRegexpLiteral(v *ast.RegExpLiteral) compiledExpr {
  1282. r := &compiledRegexpLiteral{
  1283. expr: v,
  1284. }
  1285. r.init(c, v.Idx0())
  1286. return r
  1287. }
  1288. func (e *compiledCallExpr) emitGetter(putOnStack bool) {
  1289. var calleeName string
  1290. switch callee := e.callee.(type) {
  1291. case *compiledDotExpr:
  1292. callee.left.emitGetter(true)
  1293. e.c.emit(dup)
  1294. e.c.emit(getPropCallee(callee.name))
  1295. case *compiledBracketExpr:
  1296. callee.left.emitGetter(true)
  1297. e.c.emit(dup)
  1298. callee.member.emitGetter(true)
  1299. e.c.emit(getElemCallee)
  1300. case *compiledIdentifierExpr:
  1301. e.c.emit(loadUndef)
  1302. calleeName = callee.name
  1303. callee.emitGetterOrRef()
  1304. default:
  1305. e.c.emit(loadUndef)
  1306. callee.emitGetter(true)
  1307. }
  1308. for _, expr := range e.args {
  1309. expr.emitGetter(true)
  1310. }
  1311. e.addSrcMap()
  1312. if calleeName == "eval" {
  1313. e.c.scope.dynamic = true
  1314. e.c.scope.thisNeeded = true
  1315. if e.c.scope.lexical {
  1316. e.c.scope.outer.dynamic = true
  1317. }
  1318. e.c.scope.accessed = true
  1319. if e.c.scope.strict {
  1320. e.c.emit(callEvalStrict(len(e.args)))
  1321. } else {
  1322. e.c.emit(callEval(len(e.args)))
  1323. }
  1324. } else {
  1325. e.c.emit(call(len(e.args)))
  1326. }
  1327. if !putOnStack {
  1328. e.c.emit(pop)
  1329. }
  1330. }
  1331. func (e *compiledCallExpr) deleteExpr() compiledExpr {
  1332. r := &defaultDeleteExpr{
  1333. expr: e,
  1334. }
  1335. r.init(e.c, file.Idx(e.offset+1))
  1336. return r
  1337. }
  1338. func (c *compiler) compileCallExpression(v *ast.CallExpression) compiledExpr {
  1339. args := make([]compiledExpr, len(v.ArgumentList))
  1340. for i, argExpr := range v.ArgumentList {
  1341. args[i] = c.compileExpression(argExpr)
  1342. }
  1343. r := &compiledCallExpr{
  1344. args: args,
  1345. callee: c.compileExpression(v.Callee),
  1346. }
  1347. r.init(c, v.LeftParenthesis)
  1348. return r
  1349. }
  1350. func (c *compiler) compileIdentifierExpression(v *ast.Identifier) compiledExpr {
  1351. if c.scope.strict {
  1352. c.checkIdentifierName(v.Name, int(v.Idx)-1)
  1353. }
  1354. r := &compiledIdentifierExpr{
  1355. name: v.Name,
  1356. }
  1357. r.offset = int(v.Idx) - 1
  1358. r.init(c, v.Idx0())
  1359. return r
  1360. }
  1361. func (c *compiler) compileNumberLiteral(v *ast.NumberLiteral) compiledExpr {
  1362. if c.scope.strict && octalRegexp.MatchString(v.Literal) {
  1363. c.throwSyntaxError(int(v.Idx)-1, "Octal literals are not allowed in strict mode")
  1364. panic("Unreachable")
  1365. }
  1366. var val Value
  1367. switch num := v.Value.(type) {
  1368. case int64:
  1369. val = intToValue(num)
  1370. case float64:
  1371. val = floatToValue(num)
  1372. default:
  1373. panic(fmt.Errorf("Unsupported number literal type: %T", v.Value))
  1374. }
  1375. r := &compiledLiteral{
  1376. val: val,
  1377. }
  1378. r.init(c, v.Idx0())
  1379. return r
  1380. }
  1381. func (c *compiler) compileStringLiteral(v *ast.StringLiteral) compiledExpr {
  1382. r := &compiledLiteral{
  1383. val: newStringValue(v.Value),
  1384. }
  1385. r.init(c, v.Idx0())
  1386. return r
  1387. }
  1388. func (c *compiler) compileBooleanLiteral(v *ast.BooleanLiteral) compiledExpr {
  1389. var val Value
  1390. if v.Value {
  1391. val = valueTrue
  1392. } else {
  1393. val = valueFalse
  1394. }
  1395. r := &compiledLiteral{
  1396. val: val,
  1397. }
  1398. r.init(c, v.Idx0())
  1399. return r
  1400. }
  1401. func (c *compiler) compileAssignExpression(v *ast.AssignExpression) compiledExpr {
  1402. // log.Printf("compileAssignExpression(): %+v", v)
  1403. r := &compiledAssignExpr{
  1404. left: c.compileExpression(v.Left),
  1405. right: c.compileExpression(v.Right),
  1406. operator: v.Operator,
  1407. }
  1408. r.init(c, v.Idx0())
  1409. return r
  1410. }
  1411. func (e *compiledEnumGetExpr) emitGetter(putOnStack bool) {
  1412. e.c.emit(enumGet)
  1413. if !putOnStack {
  1414. e.c.emit(pop)
  1415. }
  1416. }