compiler_expr.go 33 KB

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