compiler_expr.go 32 KB

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