compiler_expr.go 32 KB

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