compiler_expr.go 33 KB

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