compiler_expr.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  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 += 3
  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] = setLocal(idx)
  762. pos++
  763. code[pos] = pop
  764. pos++
  765. }
  766. copy(code[l:], e.c.p.code[maxPreambleLen:])
  767. e.c.p.code = code
  768. for i, _ := range e.c.p.srcMap {
  769. e.c.p.srcMap[i].pc += l - maxPreambleLen
  770. }
  771. }
  772. strict := e.c.scope.strict
  773. p := e.c.p
  774. // e.c.p.dumpCode()
  775. e.c.popScope()
  776. e.c.p = savedPrg
  777. e.c.blockStart = savedBlockStart
  778. name := ""
  779. if e.expr.Name != nil {
  780. name = e.expr.Name.Name
  781. }
  782. 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})
  783. if !putOnStack {
  784. e.c.emit(pop)
  785. }
  786. }
  787. func (c *compiler) compileFunctionLiteral(v *ast.FunctionLiteral, isExpr bool) compiledExpr {
  788. if v.Name != nil && c.scope.strict {
  789. c.checkIdentifierLName(v.Name.Name, int(v.Name.Idx)-1)
  790. }
  791. r := &compiledFunctionLiteral{
  792. expr: v,
  793. isExpr: isExpr,
  794. }
  795. r.init(c, v.Idx0())
  796. return r
  797. }
  798. func nearestNonLexical(s *scope) *scope {
  799. for ; s != nil && s.lexical; s = s.outer {
  800. }
  801. return s
  802. }
  803. func (e *compiledThisExpr) emitGetter(putOnStack bool) {
  804. if putOnStack {
  805. e.addSrcMap()
  806. if e.c.scope.eval || e.c.scope.isFunction() {
  807. nearestNonLexical(e.c.scope).thisNeeded = true
  808. e.c.emit(loadStack(0))
  809. } else {
  810. e.c.emit(loadGlobalObject)
  811. }
  812. }
  813. }
  814. /*
  815. func (e *compiledThisExpr) deleteExpr() compiledExpr {
  816. r := &compiledLiteral{
  817. val: valueTrue,
  818. }
  819. r.init(e.c, 0)
  820. return r
  821. }
  822. */
  823. func (e *compiledNewExpr) emitGetter(putOnStack bool) {
  824. e.callee.emitGetter(true)
  825. for _, expr := range e.args {
  826. expr.emitGetter(true)
  827. }
  828. e.addSrcMap()
  829. e.c.emit(_new(len(e.args)))
  830. if !putOnStack {
  831. e.c.emit(pop)
  832. }
  833. }
  834. func (c *compiler) compileNewExpression(v *ast.NewExpression) compiledExpr {
  835. args := make([]compiledExpr, len(v.ArgumentList))
  836. for i, expr := range v.ArgumentList {
  837. args[i] = c.compileExpression(expr)
  838. }
  839. r := &compiledNewExpr{
  840. callee: c.compileExpression(v.Callee),
  841. args: args,
  842. }
  843. r.init(c, v.Idx0())
  844. return r
  845. }
  846. func (e *compiledSequenceExpr) emitGetter(putOnStack bool) {
  847. if len(e.sequence) > 0 {
  848. for i := 0; i < len(e.sequence)-1; i++ {
  849. e.sequence[i].emitGetter(false)
  850. }
  851. e.sequence[len(e.sequence)-1].emitGetter(putOnStack)
  852. }
  853. }
  854. func (c *compiler) compileSequenceExpression(v *ast.SequenceExpression) compiledExpr {
  855. s := make([]compiledExpr, len(v.Sequence))
  856. for i, expr := range v.Sequence {
  857. s[i] = c.compileExpression(expr)
  858. }
  859. r := &compiledSequenceExpr{
  860. sequence: s,
  861. }
  862. var idx file.Idx
  863. if len(v.Sequence) > 0 {
  864. idx = v.Idx0()
  865. }
  866. r.init(c, idx)
  867. return r
  868. }
  869. func (c *compiler) emitThrow(v Value) {
  870. if o, ok := v.(*Object); ok {
  871. t := o.self.getStr("name").String()
  872. switch t {
  873. case "TypeError":
  874. c.emit(getVar1(t))
  875. msg := o.self.getStr("message")
  876. if msg != nil {
  877. c.emit(loadVal(c.p.defineLiteralValue(msg)))
  878. c.emit(_new(1))
  879. } else {
  880. c.emit(_new(0))
  881. }
  882. c.emit(throw)
  883. return
  884. }
  885. }
  886. panic(fmt.Errorf("Unknown exception type thrown while evaliating constant expression: %s", v.String()))
  887. }
  888. func (c *compiler) emitConst(expr compiledExpr, putOnStack bool) {
  889. v, ex := c.evalConst(expr)
  890. if ex == nil {
  891. if putOnStack {
  892. c.emit(loadVal(c.p.defineLiteralValue(v)))
  893. }
  894. } else {
  895. c.emitThrow(ex.val)
  896. }
  897. }
  898. func (c *compiler) emitExpr(expr compiledExpr, putOnStack bool) {
  899. if expr.constant() {
  900. c.emitConst(expr, putOnStack)
  901. } else {
  902. expr.emitGetter(putOnStack)
  903. }
  904. }
  905. func (c *compiler) evalConst(expr compiledExpr) (Value, *Exception) {
  906. if expr, ok := expr.(*compiledLiteral); ok {
  907. return expr.val, nil
  908. }
  909. var savedPrg *Program
  910. createdPrg := false
  911. if c.evalVM.prg == nil {
  912. c.evalVM.prg = &Program{}
  913. savedPrg = c.p
  914. c.p = c.evalVM.prg
  915. createdPrg = true
  916. }
  917. savedPc := len(c.p.code)
  918. expr.emitGetter(true)
  919. c.emit(halt)
  920. c.evalVM.pc = savedPc
  921. ex := c.evalVM.runTry()
  922. if createdPrg {
  923. c.evalVM.prg = nil
  924. c.evalVM.pc = 0
  925. c.p = savedPrg
  926. } else {
  927. c.evalVM.prg.code = c.evalVM.prg.code[:savedPc]
  928. c.p.code = c.evalVM.prg.code
  929. }
  930. if ex == nil {
  931. return c.evalVM.pop(), nil
  932. }
  933. return nil, ex
  934. }
  935. func (e *compiledUnaryExpr) constant() bool {
  936. return e.operand.constant()
  937. }
  938. func (e *compiledUnaryExpr) emitGetter(putOnStack bool) {
  939. var prepare, body func()
  940. toNumber := func() {
  941. e.c.emit(toNumber)
  942. }
  943. switch e.operator {
  944. case token.NOT:
  945. e.operand.emitGetter(true)
  946. e.c.emit(not)
  947. goto end
  948. case token.BITWISE_NOT:
  949. e.operand.emitGetter(true)
  950. e.c.emit(bnot)
  951. goto end
  952. case token.TYPEOF:
  953. if o, ok := e.operand.(compiledExprOrRef); ok {
  954. o.emitGetterOrRef()
  955. } else {
  956. e.operand.emitGetter(true)
  957. }
  958. e.c.emit(typeof)
  959. goto end
  960. case token.DELETE:
  961. e.operand.deleteExpr().emitGetter(putOnStack)
  962. return
  963. case token.MINUS:
  964. e.c.emitExpr(e.operand, true)
  965. e.c.emit(neg)
  966. goto end
  967. case token.PLUS:
  968. e.c.emitExpr(e.operand, true)
  969. e.c.emit(plus)
  970. goto end
  971. case token.INCREMENT:
  972. prepare = toNumber
  973. body = func() {
  974. e.c.emit(inc)
  975. }
  976. case token.DECREMENT:
  977. prepare = toNumber
  978. body = func() {
  979. e.c.emit(dec)
  980. }
  981. case token.VOID:
  982. e.c.emitExpr(e.operand, false)
  983. if putOnStack {
  984. e.c.emit(loadUndef)
  985. }
  986. return
  987. default:
  988. panic(fmt.Errorf("Unknown unary operator: %s", e.operator.String()))
  989. }
  990. e.operand.emitUnary(prepare, body, e.postfix, putOnStack)
  991. return
  992. end:
  993. if !putOnStack {
  994. e.c.emit(pop)
  995. }
  996. }
  997. func (c *compiler) compileUnaryExpression(v *ast.UnaryExpression) compiledExpr {
  998. r := &compiledUnaryExpr{
  999. operand: c.compileExpression(v.Operand),
  1000. operator: v.Operator,
  1001. postfix: v.Postfix,
  1002. }
  1003. r.init(c, v.Idx0())
  1004. return r
  1005. }
  1006. func (e *compiledConditionalExpr) emitGetter(putOnStack bool) {
  1007. e.test.emitGetter(true)
  1008. j := len(e.c.p.code)
  1009. e.c.emit(nil)
  1010. e.consequent.emitGetter(putOnStack)
  1011. j1 := len(e.c.p.code)
  1012. e.c.emit(nil)
  1013. e.c.p.code[j] = jne(len(e.c.p.code) - j)
  1014. e.alternate.emitGetter(putOnStack)
  1015. e.c.p.code[j1] = jump(len(e.c.p.code) - j1)
  1016. }
  1017. func (c *compiler) compileConditionalExpression(v *ast.ConditionalExpression) compiledExpr {
  1018. r := &compiledConditionalExpr{
  1019. test: c.compileExpression(v.Test),
  1020. consequent: c.compileExpression(v.Consequent),
  1021. alternate: c.compileExpression(v.Alternate),
  1022. }
  1023. r.init(c, v.Idx0())
  1024. return r
  1025. }
  1026. func (e *compiledLogicalOr) constant() bool {
  1027. if e.left.constant() {
  1028. if v, ex := e.c.evalConst(e.left); ex == nil {
  1029. if v.ToBoolean() {
  1030. return true
  1031. }
  1032. return e.right.constant()
  1033. } else {
  1034. return true
  1035. }
  1036. }
  1037. return false
  1038. }
  1039. func (e *compiledLogicalOr) emitGetter(putOnStack bool) {
  1040. if e.left.constant() {
  1041. if v, ex := e.c.evalConst(e.left); ex == nil {
  1042. if !v.ToBoolean() {
  1043. e.c.emitExpr(e.right, putOnStack)
  1044. } else {
  1045. if putOnStack {
  1046. e.c.emit(loadVal(e.c.p.defineLiteralValue(v)))
  1047. }
  1048. }
  1049. } else {
  1050. e.c.emitThrow(ex.val)
  1051. }
  1052. return
  1053. }
  1054. e.c.emitExpr(e.left, true)
  1055. e.c.markBlockStart()
  1056. j := len(e.c.p.code)
  1057. e.addSrcMap()
  1058. e.c.emit(nil)
  1059. e.c.emit(pop)
  1060. e.c.emitExpr(e.right, true)
  1061. e.c.p.code[j] = jeq1(len(e.c.p.code) - j)
  1062. if !putOnStack {
  1063. e.c.emit(pop)
  1064. }
  1065. }
  1066. func (e *compiledLogicalAnd) constant() bool {
  1067. if e.left.constant() {
  1068. if v, ex := e.c.evalConst(e.left); ex == nil {
  1069. if !v.ToBoolean() {
  1070. return true
  1071. } else {
  1072. return e.right.constant()
  1073. }
  1074. } else {
  1075. return true
  1076. }
  1077. }
  1078. return false
  1079. }
  1080. func (e *compiledLogicalAnd) emitGetter(putOnStack bool) {
  1081. var j int
  1082. if e.left.constant() {
  1083. if v, ex := e.c.evalConst(e.left); ex == nil {
  1084. if !v.ToBoolean() {
  1085. e.c.emit(loadVal(e.c.p.defineLiteralValue(v)))
  1086. } else {
  1087. e.c.emitExpr(e.right, putOnStack)
  1088. }
  1089. } else {
  1090. e.c.emitThrow(ex.val)
  1091. }
  1092. return
  1093. }
  1094. e.left.emitGetter(true)
  1095. e.c.markBlockStart()
  1096. j = len(e.c.p.code)
  1097. e.addSrcMap()
  1098. e.c.emit(nil)
  1099. e.c.emit(pop)
  1100. e.c.emitExpr(e.right, true)
  1101. e.c.p.code[j] = jneq1(len(e.c.p.code) - j)
  1102. if !putOnStack {
  1103. e.c.emit(pop)
  1104. }
  1105. }
  1106. func (e *compiledBinaryExpr) constant() bool {
  1107. return e.left.constant() && e.right.constant()
  1108. }
  1109. func (e *compiledBinaryExpr) emitGetter(putOnStack bool) {
  1110. e.c.emitExpr(e.left, true)
  1111. e.c.emitExpr(e.right, true)
  1112. e.addSrcMap()
  1113. switch e.operator {
  1114. case token.LESS:
  1115. e.c.emit(op_lt)
  1116. case token.GREATER:
  1117. e.c.emit(op_gt)
  1118. case token.LESS_OR_EQUAL:
  1119. e.c.emit(op_lte)
  1120. case token.GREATER_OR_EQUAL:
  1121. e.c.emit(op_gte)
  1122. case token.EQUAL:
  1123. e.c.emit(op_eq)
  1124. case token.NOT_EQUAL:
  1125. e.c.emit(op_neq)
  1126. case token.STRICT_EQUAL:
  1127. e.c.emit(op_strict_eq)
  1128. case token.STRICT_NOT_EQUAL:
  1129. e.c.emit(op_strict_neq)
  1130. case token.PLUS:
  1131. e.c.emit(add)
  1132. case token.MINUS:
  1133. e.c.emit(sub)
  1134. case token.MULTIPLY:
  1135. e.c.emit(mul)
  1136. case token.SLASH:
  1137. e.c.emit(div)
  1138. case token.REMAINDER:
  1139. e.c.emit(mod)
  1140. case token.AND:
  1141. e.c.emit(and)
  1142. case token.OR:
  1143. e.c.emit(or)
  1144. case token.EXCLUSIVE_OR:
  1145. e.c.emit(xor)
  1146. case token.INSTANCEOF:
  1147. e.c.emit(op_instanceof)
  1148. case token.IN:
  1149. e.c.emit(op_in)
  1150. case token.SHIFT_LEFT:
  1151. e.c.emit(sal)
  1152. case token.SHIFT_RIGHT:
  1153. e.c.emit(sar)
  1154. case token.UNSIGNED_SHIFT_RIGHT:
  1155. e.c.emit(shr)
  1156. default:
  1157. panic(fmt.Errorf("Unknown operator: %s", e.operator.String()))
  1158. }
  1159. if !putOnStack {
  1160. e.c.emit(pop)
  1161. }
  1162. }
  1163. func (c *compiler) compileBinaryExpression(v *ast.BinaryExpression) compiledExpr {
  1164. switch v.Operator {
  1165. case token.LOGICAL_OR:
  1166. return c.compileLogicalOr(v.Left, v.Right, v.Idx0())
  1167. case token.LOGICAL_AND:
  1168. return c.compileLogicalAnd(v.Left, v.Right, v.Idx0())
  1169. }
  1170. r := &compiledBinaryExpr{
  1171. left: c.compileExpression(v.Left),
  1172. right: c.compileExpression(v.Right),
  1173. operator: v.Operator,
  1174. }
  1175. r.init(c, v.Idx0())
  1176. return r
  1177. }
  1178. func (c *compiler) compileLogicalOr(left, right ast.Expression, idx file.Idx) compiledExpr {
  1179. r := &compiledLogicalOr{
  1180. left: c.compileExpression(left),
  1181. right: c.compileExpression(right),
  1182. }
  1183. r.init(c, idx)
  1184. return r
  1185. }
  1186. func (c *compiler) compileLogicalAnd(left, right ast.Expression, idx file.Idx) compiledExpr {
  1187. r := &compiledLogicalAnd{
  1188. left: c.compileExpression(left),
  1189. right: c.compileExpression(right),
  1190. }
  1191. r.init(c, idx)
  1192. return r
  1193. }
  1194. func (e *compiledVariableExpr) emitGetter(putOnStack bool) {
  1195. if e.initializer != nil {
  1196. idExpr := &compiledIdentifierExpr{
  1197. name: e.name,
  1198. }
  1199. idExpr.init(e.c, file.Idx(0))
  1200. idExpr.emitSetter(e.initializer)
  1201. if !putOnStack {
  1202. e.c.emit(pop)
  1203. }
  1204. } else {
  1205. if putOnStack {
  1206. e.c.emit(loadUndef)
  1207. }
  1208. }
  1209. }
  1210. func (c *compiler) compileVariableExpression(v *ast.VariableExpression) compiledExpr {
  1211. r := &compiledVariableExpr{
  1212. name: v.Name,
  1213. initializer: c.compileExpression(v.Initializer),
  1214. }
  1215. r.init(c, v.Idx0())
  1216. return r
  1217. }
  1218. func (e *compiledObjectLiteral) emitGetter(putOnStack bool) {
  1219. e.addSrcMap()
  1220. e.c.emit(newObject)
  1221. for _, prop := range e.expr.Value {
  1222. e.c.compileExpression(prop.Value).emitGetter(true)
  1223. switch prop.Kind {
  1224. case "value":
  1225. if prop.Key == "__proto__" {
  1226. e.c.emit(setProto)
  1227. } else {
  1228. e.c.emit(setProp1(prop.Key))
  1229. }
  1230. case "get":
  1231. e.c.emit(setPropGetter(prop.Key))
  1232. case "set":
  1233. e.c.emit(setPropSetter(prop.Key))
  1234. default:
  1235. panic(fmt.Errorf("Unknown property kind: %s", prop.Kind))
  1236. }
  1237. }
  1238. if !putOnStack {
  1239. e.c.emit(pop)
  1240. }
  1241. }
  1242. func (c *compiler) compileObjectLiteral(v *ast.ObjectLiteral) compiledExpr {
  1243. r := &compiledObjectLiteral{
  1244. expr: v,
  1245. }
  1246. r.init(c, v.Idx0())
  1247. return r
  1248. }
  1249. func (e *compiledArrayLiteral) emitGetter(putOnStack bool) {
  1250. e.addSrcMap()
  1251. for _, v := range e.expr.Value {
  1252. if v != nil {
  1253. e.c.compileExpression(v).emitGetter(true)
  1254. } else {
  1255. e.c.emit(loadNil)
  1256. }
  1257. }
  1258. e.c.emit(newArray(len(e.expr.Value)))
  1259. if !putOnStack {
  1260. e.c.emit(pop)
  1261. }
  1262. }
  1263. func (c *compiler) compileArrayLiteral(v *ast.ArrayLiteral) compiledExpr {
  1264. r := &compiledArrayLiteral{
  1265. expr: v,
  1266. }
  1267. r.init(c, v.Idx0())
  1268. return r
  1269. }
  1270. func (e *compiledRegexpLiteral) emitGetter(putOnStack bool) {
  1271. if putOnStack {
  1272. pattern, global, ignoreCase, multiline, err := compileRegexp(e.expr.Pattern, e.expr.Flags)
  1273. if err != nil {
  1274. e.c.throwSyntaxError(e.offset, err.Error())
  1275. }
  1276. e.c.emit(&newRegexp{pattern: pattern,
  1277. src: newStringValue(e.expr.Pattern),
  1278. global: global,
  1279. ignoreCase: ignoreCase,
  1280. multiline: multiline,
  1281. })
  1282. }
  1283. }
  1284. func (c *compiler) compileRegexpLiteral(v *ast.RegExpLiteral) compiledExpr {
  1285. r := &compiledRegexpLiteral{
  1286. expr: v,
  1287. }
  1288. r.init(c, v.Idx0())
  1289. return r
  1290. }
  1291. func (e *compiledCallExpr) emitGetter(putOnStack bool) {
  1292. var calleeName string
  1293. switch callee := e.callee.(type) {
  1294. case *compiledDotExpr:
  1295. callee.left.emitGetter(true)
  1296. e.c.emit(dup)
  1297. e.c.emit(getPropCallee(callee.name))
  1298. case *compiledBracketExpr:
  1299. callee.left.emitGetter(true)
  1300. e.c.emit(dup)
  1301. callee.member.emitGetter(true)
  1302. e.c.emit(getElemCallee)
  1303. case *compiledIdentifierExpr:
  1304. e.c.emit(loadUndef)
  1305. calleeName = callee.name
  1306. callee.emitGetterOrRef()
  1307. default:
  1308. e.c.emit(loadUndef)
  1309. callee.emitGetter(true)
  1310. }
  1311. for _, expr := range e.args {
  1312. expr.emitGetter(true)
  1313. }
  1314. e.addSrcMap()
  1315. if calleeName == "eval" {
  1316. e.c.scope.dynamic = true
  1317. e.c.scope.thisNeeded = true
  1318. if e.c.scope.lexical {
  1319. e.c.scope.outer.dynamic = true
  1320. }
  1321. e.c.scope.accessed = true
  1322. if e.c.scope.strict {
  1323. e.c.emit(callEvalStrict(len(e.args)))
  1324. } else {
  1325. e.c.emit(callEval(len(e.args)))
  1326. }
  1327. } else {
  1328. e.c.emit(call(len(e.args)))
  1329. }
  1330. if !putOnStack {
  1331. e.c.emit(pop)
  1332. }
  1333. }
  1334. func (e *compiledCallExpr) deleteExpr() compiledExpr {
  1335. r := &defaultDeleteExpr{
  1336. expr: e,
  1337. }
  1338. r.init(e.c, file.Idx(e.offset+1))
  1339. return r
  1340. }
  1341. func (c *compiler) compileCallExpression(v *ast.CallExpression) compiledExpr {
  1342. args := make([]compiledExpr, len(v.ArgumentList))
  1343. for i, argExpr := range v.ArgumentList {
  1344. args[i] = c.compileExpression(argExpr)
  1345. }
  1346. r := &compiledCallExpr{
  1347. args: args,
  1348. callee: c.compileExpression(v.Callee),
  1349. }
  1350. r.init(c, v.LeftParenthesis)
  1351. return r
  1352. }
  1353. func (c *compiler) compileIdentifierExpression(v *ast.Identifier) compiledExpr {
  1354. if c.scope.strict {
  1355. c.checkIdentifierName(v.Name, int(v.Idx)-1)
  1356. }
  1357. r := &compiledIdentifierExpr{
  1358. name: v.Name,
  1359. }
  1360. r.offset = int(v.Idx) - 1
  1361. r.init(c, v.Idx0())
  1362. return r
  1363. }
  1364. func (c *compiler) compileNumberLiteral(v *ast.NumberLiteral) compiledExpr {
  1365. if c.scope.strict && octalRegexp.MatchString(v.Literal) {
  1366. c.throwSyntaxError(int(v.Idx)-1, "Octal literals are not allowed in strict mode")
  1367. panic("Unreachable")
  1368. }
  1369. var val Value
  1370. switch num := v.Value.(type) {
  1371. case int64:
  1372. val = intToValue(num)
  1373. case float64:
  1374. val = floatToValue(num)
  1375. default:
  1376. panic(fmt.Errorf("Unsupported number literal type: %T", v.Value))
  1377. }
  1378. r := &compiledLiteral{
  1379. val: val,
  1380. }
  1381. r.init(c, v.Idx0())
  1382. return r
  1383. }
  1384. func (c *compiler) compileStringLiteral(v *ast.StringLiteral) compiledExpr {
  1385. r := &compiledLiteral{
  1386. val: newStringValue(v.Value),
  1387. }
  1388. r.init(c, v.Idx0())
  1389. return r
  1390. }
  1391. func (c *compiler) compileBooleanLiteral(v *ast.BooleanLiteral) compiledExpr {
  1392. var val Value
  1393. if v.Value {
  1394. val = valueTrue
  1395. } else {
  1396. val = valueFalse
  1397. }
  1398. r := &compiledLiteral{
  1399. val: val,
  1400. }
  1401. r.init(c, v.Idx0())
  1402. return r
  1403. }
  1404. func (c *compiler) compileAssignExpression(v *ast.AssignExpression) compiledExpr {
  1405. // log.Printf("compileAssignExpression(): %+v", v)
  1406. r := &compiledAssignExpr{
  1407. left: c.compileExpression(v.Left),
  1408. right: c.compileExpression(v.Right),
  1409. operator: v.Operator,
  1410. }
  1411. r.init(c, v.Idx0())
  1412. return r
  1413. }
  1414. func (e *compiledEnumGetExpr) emitGetter(putOnStack bool) {
  1415. e.c.emit(enumGet)
  1416. if !putOnStack {
  1417. e.c.emit(pop)
  1418. }
  1419. }