compiler_expr.go 32 KB

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