compiler_expr.go 34 KB

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