compiler_expr.go 32 KB

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