compiler_expr.go 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180
  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. emitRef()
  17. emitUnary(prepare, body func(), postfix, putOnStack bool)
  18. deleteExpr() compiledExpr
  19. constant() bool
  20. addSrcMap()
  21. }
  22. type compiledExprOrRef interface {
  23. compiledExpr
  24. emitGetterOrRef()
  25. }
  26. type compiledCallExpr struct {
  27. baseCompiledExpr
  28. args []compiledExpr
  29. callee compiledExpr
  30. }
  31. type compiledObjectLiteral struct {
  32. baseCompiledExpr
  33. expr *ast.ObjectLiteral
  34. }
  35. type compiledArrayLiteral struct {
  36. baseCompiledExpr
  37. expr *ast.ArrayLiteral
  38. }
  39. type compiledRegexpLiteral struct {
  40. baseCompiledExpr
  41. expr *ast.RegExpLiteral
  42. }
  43. type compiledLiteral struct {
  44. baseCompiledExpr
  45. val Value
  46. }
  47. type compiledAssignExpr struct {
  48. baseCompiledExpr
  49. left, right compiledExpr
  50. operator token.Token
  51. }
  52. type compiledObjectAssignmentPattern struct {
  53. baseCompiledExpr
  54. expr *ast.ObjectPattern
  55. }
  56. type compiledArrayAssignmentPattern struct {
  57. baseCompiledExpr
  58. expr *ast.ArrayPattern
  59. }
  60. type deleteGlobalExpr struct {
  61. baseCompiledExpr
  62. name unistring.String
  63. }
  64. type deleteVarExpr struct {
  65. baseCompiledExpr
  66. name unistring.String
  67. }
  68. type deletePropExpr struct {
  69. baseCompiledExpr
  70. left compiledExpr
  71. name unistring.String
  72. }
  73. type deleteElemExpr struct {
  74. baseCompiledExpr
  75. left, member compiledExpr
  76. }
  77. type constantExpr struct {
  78. baseCompiledExpr
  79. val Value
  80. }
  81. type baseCompiledExpr struct {
  82. c *compiler
  83. offset int
  84. }
  85. type compiledIdentifierExpr struct {
  86. baseCompiledExpr
  87. name unistring.String
  88. }
  89. type compiledFunctionLiteral struct {
  90. baseCompiledExpr
  91. expr *ast.FunctionLiteral
  92. lhsName unistring.String
  93. strict *ast.StringLiteral
  94. isExpr bool
  95. }
  96. type compiledBracketExpr struct {
  97. baseCompiledExpr
  98. left, member compiledExpr
  99. }
  100. type compiledThisExpr struct {
  101. baseCompiledExpr
  102. }
  103. type compiledNewExpr struct {
  104. baseCompiledExpr
  105. callee compiledExpr
  106. args []compiledExpr
  107. }
  108. type compiledNewTarget struct {
  109. baseCompiledExpr
  110. }
  111. type compiledSequenceExpr struct {
  112. baseCompiledExpr
  113. sequence []compiledExpr
  114. }
  115. type compiledUnaryExpr struct {
  116. baseCompiledExpr
  117. operand compiledExpr
  118. operator token.Token
  119. postfix bool
  120. }
  121. type compiledConditionalExpr struct {
  122. baseCompiledExpr
  123. test, consequent, alternate compiledExpr
  124. }
  125. type compiledLogicalOr struct {
  126. baseCompiledExpr
  127. left, right compiledExpr
  128. }
  129. type compiledLogicalAnd struct {
  130. baseCompiledExpr
  131. left, right compiledExpr
  132. }
  133. type compiledBinaryExpr struct {
  134. baseCompiledExpr
  135. left, right compiledExpr
  136. operator token.Token
  137. }
  138. type compiledEnumGetExpr struct {
  139. baseCompiledExpr
  140. }
  141. type defaultDeleteExpr struct {
  142. baseCompiledExpr
  143. expr compiledExpr
  144. }
  145. func (e *defaultDeleteExpr) emitGetter(putOnStack bool) {
  146. e.expr.emitGetter(false)
  147. if putOnStack {
  148. e.c.emit(loadVal(e.c.p.defineLiteralValue(valueTrue)))
  149. }
  150. }
  151. func (c *compiler) compileExpression(v ast.Expression) compiledExpr {
  152. // log.Printf("compileExpression: %T", v)
  153. switch v := v.(type) {
  154. case nil:
  155. return nil
  156. case *ast.AssignExpression:
  157. return c.compileAssignExpression(v)
  158. case *ast.NumberLiteral:
  159. return c.compileNumberLiteral(v)
  160. case *ast.StringLiteral:
  161. return c.compileStringLiteral(v)
  162. case *ast.BooleanLiteral:
  163. return c.compileBooleanLiteral(v)
  164. case *ast.NullLiteral:
  165. r := &compiledLiteral{
  166. val: _null,
  167. }
  168. r.init(c, v.Idx0())
  169. return r
  170. case *ast.Identifier:
  171. return c.compileIdentifierExpression(v)
  172. case *ast.CallExpression:
  173. return c.compileCallExpression(v)
  174. case *ast.ObjectLiteral:
  175. return c.compileObjectLiteral(v)
  176. case *ast.ArrayLiteral:
  177. return c.compileArrayLiteral(v)
  178. case *ast.RegExpLiteral:
  179. return c.compileRegexpLiteral(v)
  180. case *ast.BinaryExpression:
  181. return c.compileBinaryExpression(v)
  182. case *ast.UnaryExpression:
  183. return c.compileUnaryExpression(v)
  184. case *ast.ConditionalExpression:
  185. return c.compileConditionalExpression(v)
  186. case *ast.FunctionLiteral:
  187. return c.compileFunctionLiteral(v, true)
  188. case *ast.DotExpression:
  189. r := &compiledDotExpr{
  190. left: c.compileExpression(v.Left),
  191. name: v.Identifier.Name,
  192. }
  193. r.init(c, v.Idx0())
  194. return r
  195. case *ast.BracketExpression:
  196. r := &compiledBracketExpr{
  197. left: c.compileExpression(v.Left),
  198. member: c.compileExpression(v.Member),
  199. }
  200. r.init(c, v.Idx0())
  201. return r
  202. case *ast.ThisExpression:
  203. r := &compiledThisExpr{}
  204. r.init(c, v.Idx0())
  205. return r
  206. case *ast.SequenceExpression:
  207. return c.compileSequenceExpression(v)
  208. case *ast.NewExpression:
  209. return c.compileNewExpression(v)
  210. case *ast.MetaProperty:
  211. return c.compileMetaProperty(v)
  212. case *ast.ObjectPattern:
  213. return c.compileObjectAssignmentPattern(v)
  214. case *ast.ArrayPattern:
  215. return c.compileArrayAssignmentPattern(v)
  216. default:
  217. panic(fmt.Errorf("Unknown expression type: %T", v))
  218. }
  219. }
  220. func (e *baseCompiledExpr) constant() bool {
  221. return false
  222. }
  223. func (e *baseCompiledExpr) init(c *compiler, idx file.Idx) {
  224. e.c = c
  225. e.offset = int(idx) - 1
  226. }
  227. func (e *baseCompiledExpr) emitSetter(compiledExpr, bool) {
  228. e.c.throwSyntaxError(e.offset, "Not a valid left-value expression")
  229. }
  230. func (e *baseCompiledExpr) emitRef() {
  231. e.c.throwSyntaxError(e.offset, "Cannot emit reference for this type of expression")
  232. }
  233. func (e *baseCompiledExpr) deleteExpr() compiledExpr {
  234. r := &constantExpr{
  235. val: valueTrue,
  236. }
  237. r.init(e.c, file.Idx(e.offset+1))
  238. return r
  239. }
  240. func (e *baseCompiledExpr) emitUnary(func(), func(), bool, bool) {
  241. e.c.throwSyntaxError(e.offset, "Not a valid left-value expression")
  242. }
  243. func (e *baseCompiledExpr) addSrcMap() {
  244. if e.offset > 0 {
  245. e.c.p.srcMap = append(e.c.p.srcMap, srcMapItem{pc: len(e.c.p.code), srcPos: e.offset})
  246. }
  247. }
  248. func (e *constantExpr) emitGetter(putOnStack bool) {
  249. if putOnStack {
  250. e.addSrcMap()
  251. e.c.emit(loadVal(e.c.p.defineLiteralValue(e.val)))
  252. }
  253. }
  254. func (e *compiledIdentifierExpr) emitGetter(putOnStack bool) {
  255. e.addSrcMap()
  256. if b, noDynamics := e.c.scope.lookupName(e.name); noDynamics {
  257. if b != nil {
  258. if putOnStack {
  259. b.emitGet()
  260. } else {
  261. b.emitGetP()
  262. }
  263. } else {
  264. panic("No dynamics and not found")
  265. }
  266. } else {
  267. if b != nil {
  268. b.emitGetVar(false)
  269. } else {
  270. e.c.emit(loadDynamic(e.name))
  271. }
  272. if !putOnStack {
  273. e.c.emit(pop)
  274. }
  275. }
  276. }
  277. func (e *compiledIdentifierExpr) emitGetterOrRef() {
  278. e.addSrcMap()
  279. if b, noDynamics := e.c.scope.lookupName(e.name); noDynamics {
  280. if b != nil {
  281. b.emitGet()
  282. } else {
  283. panic("No dynamics and not found")
  284. }
  285. } else {
  286. if b != nil {
  287. b.emitGetVar(false)
  288. } else {
  289. e.c.emit(loadDynamicRef(e.name))
  290. }
  291. }
  292. }
  293. func (e *compiledIdentifierExpr) emitGetterAndCallee() {
  294. e.addSrcMap()
  295. if b, noDynamics := e.c.scope.lookupName(e.name); noDynamics {
  296. if b != nil {
  297. e.c.emit(loadUndef)
  298. b.emitGet()
  299. } else {
  300. panic("No dynamics and not found")
  301. }
  302. } else {
  303. if b != nil {
  304. b.emitGetVar(true)
  305. } else {
  306. e.c.emit(loadDynamicCallee(e.name))
  307. }
  308. }
  309. }
  310. func (c *compiler) emitVarSetter1(name unistring.String, offset int, putOnStack bool, emitRight func(isRef bool)) {
  311. if c.scope.strict {
  312. c.checkIdentifierLName(name, offset)
  313. }
  314. if b, noDynamics := c.scope.lookupName(name); noDynamics {
  315. emitRight(false)
  316. if b != nil {
  317. if putOnStack {
  318. b.emitSet()
  319. } else {
  320. b.emitSetP()
  321. }
  322. } else {
  323. if c.scope.strict {
  324. c.emit(setGlobalStrict(name))
  325. } else {
  326. c.emit(setGlobal(name))
  327. }
  328. if !putOnStack {
  329. c.emit(pop)
  330. }
  331. }
  332. } else {
  333. if b != nil {
  334. b.emitResolveVar(c.scope.strict)
  335. } else {
  336. if c.scope.strict {
  337. c.emit(resolveVar1Strict(name))
  338. } else {
  339. c.emit(resolveVar1(name))
  340. }
  341. }
  342. emitRight(true)
  343. if putOnStack {
  344. c.emit(putValue)
  345. } else {
  346. c.emit(putValueP)
  347. }
  348. }
  349. }
  350. func (c *compiler) emitVarSetter(name unistring.String, offset int, valueExpr compiledExpr, putOnStack bool) {
  351. c.emitVarSetter1(name, offset, putOnStack, func(bool) {
  352. c.emitExpr(valueExpr, true)
  353. })
  354. }
  355. func (c *compiler) emitVarRef(name unistring.String, offset int) {
  356. if c.scope.strict {
  357. c.checkIdentifierLName(name, offset)
  358. }
  359. b, _ := c.scope.lookupName(name)
  360. if b != nil {
  361. b.emitResolveVar(c.scope.strict)
  362. } else {
  363. if c.scope.strict {
  364. c.emit(resolveVar1Strict(name))
  365. } else {
  366. c.emit(resolveVar1(name))
  367. }
  368. }
  369. }
  370. func (e *compiledIdentifierExpr) emitRef() {
  371. e.c.emitVarRef(e.name, e.offset)
  372. }
  373. func (e *compiledIdentifierExpr) emitSetter(valueExpr compiledExpr, putOnStack bool) {
  374. e.c.emitVarSetter(e.name, e.offset, valueExpr, putOnStack)
  375. }
  376. func (e *compiledIdentifierExpr) emitUnary(prepare, body func(), postfix, putOnStack bool) {
  377. if putOnStack {
  378. e.c.emitVarSetter1(e.name, e.offset, true, func(isRef bool) {
  379. e.c.emit(loadUndef)
  380. if isRef {
  381. e.c.emit(getValue)
  382. } else {
  383. e.emitGetter(true)
  384. }
  385. if prepare != nil {
  386. prepare()
  387. }
  388. if !postfix {
  389. body()
  390. }
  391. e.c.emit(rdupN(1))
  392. if postfix {
  393. body()
  394. }
  395. })
  396. e.c.emit(pop)
  397. } else {
  398. e.c.emitVarSetter1(e.name, e.offset, false, func(isRef bool) {
  399. if isRef {
  400. e.c.emit(getValue)
  401. } else {
  402. e.emitGetter(true)
  403. }
  404. body()
  405. })
  406. }
  407. }
  408. func (e *compiledIdentifierExpr) deleteExpr() compiledExpr {
  409. if e.c.scope.strict {
  410. e.c.throwSyntaxError(e.offset, "Delete of an unqualified identifier in strict mode")
  411. panic("Unreachable")
  412. }
  413. if b, noDynamics := e.c.scope.lookupName(e.name); noDynamics {
  414. if b == nil {
  415. r := &deleteGlobalExpr{
  416. name: e.name,
  417. }
  418. r.init(e.c, file.Idx(0))
  419. return r
  420. }
  421. } else {
  422. if b == nil {
  423. r := &deleteVarExpr{
  424. name: e.name,
  425. }
  426. r.init(e.c, file.Idx(e.offset+1))
  427. return r
  428. }
  429. }
  430. r := &compiledLiteral{
  431. val: valueFalse,
  432. }
  433. r.init(e.c, file.Idx(e.offset+1))
  434. return r
  435. }
  436. type compiledDotExpr struct {
  437. baseCompiledExpr
  438. left compiledExpr
  439. name unistring.String
  440. }
  441. func (e *compiledDotExpr) emitGetter(putOnStack bool) {
  442. e.left.emitGetter(true)
  443. e.addSrcMap()
  444. e.c.emit(getProp(e.name))
  445. if !putOnStack {
  446. e.c.emit(pop)
  447. }
  448. }
  449. func (e *compiledDotExpr) emitRef() {
  450. e.left.emitGetter(true)
  451. if e.c.scope.strict {
  452. e.c.emit(getPropRefStrict(e.name))
  453. } else {
  454. e.c.emit(getPropRef(e.name))
  455. }
  456. }
  457. func (e *compiledDotExpr) emitSetter(valueExpr compiledExpr, putOnStack bool) {
  458. e.left.emitGetter(true)
  459. valueExpr.emitGetter(true)
  460. if e.c.scope.strict {
  461. if putOnStack {
  462. e.c.emit(setPropStrict(e.name))
  463. } else {
  464. e.c.emit(setPropStrictP(e.name))
  465. }
  466. } else {
  467. if putOnStack {
  468. e.c.emit(setProp(e.name))
  469. } else {
  470. e.c.emit(setPropP(e.name))
  471. }
  472. }
  473. }
  474. func (e *compiledDotExpr) emitUnary(prepare, body func(), postfix, putOnStack bool) {
  475. if !putOnStack {
  476. e.left.emitGetter(true)
  477. e.c.emit(dup)
  478. e.c.emit(getProp(e.name))
  479. body()
  480. if e.c.scope.strict {
  481. e.c.emit(setPropStrict(e.name), pop)
  482. } else {
  483. e.c.emit(setProp(e.name), pop)
  484. }
  485. } else {
  486. if !postfix {
  487. e.left.emitGetter(true)
  488. e.c.emit(dup)
  489. e.c.emit(getProp(e.name))
  490. if prepare != nil {
  491. prepare()
  492. }
  493. body()
  494. if e.c.scope.strict {
  495. e.c.emit(setPropStrict(e.name))
  496. } else {
  497. e.c.emit(setProp(e.name))
  498. }
  499. } else {
  500. e.c.emit(loadUndef)
  501. e.left.emitGetter(true)
  502. e.c.emit(dup)
  503. e.c.emit(getProp(e.name))
  504. if prepare != nil {
  505. prepare()
  506. }
  507. e.c.emit(rdupN(2))
  508. body()
  509. if e.c.scope.strict {
  510. e.c.emit(setPropStrict(e.name))
  511. } else {
  512. e.c.emit(setProp(e.name))
  513. }
  514. e.c.emit(pop)
  515. }
  516. }
  517. }
  518. func (e *compiledDotExpr) deleteExpr() compiledExpr {
  519. r := &deletePropExpr{
  520. left: e.left,
  521. name: e.name,
  522. }
  523. r.init(e.c, file.Idx(0))
  524. return r
  525. }
  526. func (e *compiledBracketExpr) emitGetter(putOnStack bool) {
  527. e.left.emitGetter(true)
  528. e.member.emitGetter(true)
  529. e.addSrcMap()
  530. e.c.emit(getElem)
  531. if !putOnStack {
  532. e.c.emit(pop)
  533. }
  534. }
  535. func (e *compiledBracketExpr) emitRef() {
  536. e.left.emitGetter(true)
  537. e.member.emitGetter(true)
  538. if e.c.scope.strict {
  539. e.c.emit(getElemRefStrict)
  540. } else {
  541. e.c.emit(getElemRef)
  542. }
  543. }
  544. func (e *compiledBracketExpr) emitSetter(valueExpr compiledExpr, putOnStack bool) {
  545. e.left.emitGetter(true)
  546. e.member.emitGetter(true)
  547. valueExpr.emitGetter(true)
  548. if e.c.scope.strict {
  549. if putOnStack {
  550. e.c.emit(setElemStrict)
  551. } else {
  552. e.c.emit(setElemStrictP)
  553. }
  554. } else {
  555. if putOnStack {
  556. e.c.emit(setElem)
  557. } else {
  558. e.c.emit(setElemP)
  559. }
  560. }
  561. }
  562. func (e *compiledBracketExpr) emitUnary(prepare, body func(), postfix, putOnStack bool) {
  563. if !putOnStack {
  564. e.left.emitGetter(true)
  565. e.member.emitGetter(true)
  566. e.c.emit(dupN(1), dupN(1))
  567. e.c.emit(getElem)
  568. body()
  569. if e.c.scope.strict {
  570. e.c.emit(setElemStrict, pop)
  571. } else {
  572. e.c.emit(setElem, pop)
  573. }
  574. } else {
  575. if !postfix {
  576. e.left.emitGetter(true)
  577. e.member.emitGetter(true)
  578. e.c.emit(dupN(1), dupN(1))
  579. e.c.emit(getElem)
  580. if prepare != nil {
  581. prepare()
  582. }
  583. body()
  584. if e.c.scope.strict {
  585. e.c.emit(setElemStrict)
  586. } else {
  587. e.c.emit(setElem)
  588. }
  589. } else {
  590. e.c.emit(loadUndef)
  591. e.left.emitGetter(true)
  592. e.member.emitGetter(true)
  593. e.c.emit(dupN(1), dupN(1))
  594. e.c.emit(getElem)
  595. if prepare != nil {
  596. prepare()
  597. }
  598. e.c.emit(rdupN(3))
  599. body()
  600. if e.c.scope.strict {
  601. e.c.emit(setElemStrict, pop)
  602. } else {
  603. e.c.emit(setElem, pop)
  604. }
  605. }
  606. }
  607. }
  608. func (e *compiledBracketExpr) deleteExpr() compiledExpr {
  609. r := &deleteElemExpr{
  610. left: e.left,
  611. member: e.member,
  612. }
  613. r.init(e.c, file.Idx(0))
  614. return r
  615. }
  616. func (e *deleteElemExpr) emitGetter(putOnStack bool) {
  617. e.left.emitGetter(true)
  618. e.member.emitGetter(true)
  619. e.addSrcMap()
  620. if e.c.scope.strict {
  621. e.c.emit(deleteElemStrict)
  622. } else {
  623. e.c.emit(deleteElem)
  624. }
  625. if !putOnStack {
  626. e.c.emit(pop)
  627. }
  628. }
  629. func (e *deletePropExpr) emitGetter(putOnStack bool) {
  630. e.left.emitGetter(true)
  631. e.addSrcMap()
  632. if e.c.scope.strict {
  633. e.c.emit(deletePropStrict(e.name))
  634. } else {
  635. e.c.emit(deleteProp(e.name))
  636. }
  637. if !putOnStack {
  638. e.c.emit(pop)
  639. }
  640. }
  641. func (e *deleteVarExpr) emitGetter(putOnStack bool) {
  642. /*if e.c.scope.strict {
  643. e.c.throwSyntaxError(e.offset, "Delete of an unqualified identifier in strict mode")
  644. return
  645. }*/
  646. e.c.emit(deleteVar(e.name))
  647. if !putOnStack {
  648. e.c.emit(pop)
  649. }
  650. }
  651. func (e *deleteGlobalExpr) emitGetter(putOnStack bool) {
  652. /*if e.c.scope.strict {
  653. e.c.throwSyntaxError(e.offset, "Delete of an unqualified identifier in strict mode")
  654. return
  655. }*/
  656. e.c.emit(deleteGlobal(e.name))
  657. if !putOnStack {
  658. e.c.emit(pop)
  659. }
  660. }
  661. func (e *compiledAssignExpr) emitGetter(putOnStack bool) {
  662. e.addSrcMap()
  663. switch e.operator {
  664. case token.ASSIGN:
  665. if fn, ok := e.right.(*compiledFunctionLiteral); ok {
  666. if fn.expr.Name == nil {
  667. if id, ok := e.left.(*compiledIdentifierExpr); ok {
  668. fn.lhsName = id.name
  669. }
  670. }
  671. }
  672. e.left.emitSetter(e.right, putOnStack)
  673. case token.PLUS:
  674. e.left.emitUnary(nil, func() {
  675. e.right.emitGetter(true)
  676. e.c.emit(add)
  677. }, false, putOnStack)
  678. case token.MINUS:
  679. e.left.emitUnary(nil, func() {
  680. e.right.emitGetter(true)
  681. e.c.emit(sub)
  682. }, false, putOnStack)
  683. case token.MULTIPLY:
  684. e.left.emitUnary(nil, func() {
  685. e.right.emitGetter(true)
  686. e.c.emit(mul)
  687. }, false, putOnStack)
  688. case token.SLASH:
  689. e.left.emitUnary(nil, func() {
  690. e.right.emitGetter(true)
  691. e.c.emit(div)
  692. }, false, putOnStack)
  693. case token.REMAINDER:
  694. e.left.emitUnary(nil, func() {
  695. e.right.emitGetter(true)
  696. e.c.emit(mod)
  697. }, false, putOnStack)
  698. case token.OR:
  699. e.left.emitUnary(nil, func() {
  700. e.right.emitGetter(true)
  701. e.c.emit(or)
  702. }, false, putOnStack)
  703. case token.AND:
  704. e.left.emitUnary(nil, func() {
  705. e.right.emitGetter(true)
  706. e.c.emit(and)
  707. }, false, putOnStack)
  708. case token.EXCLUSIVE_OR:
  709. e.left.emitUnary(nil, func() {
  710. e.right.emitGetter(true)
  711. e.c.emit(xor)
  712. }, false, putOnStack)
  713. case token.SHIFT_LEFT:
  714. e.left.emitUnary(nil, func() {
  715. e.right.emitGetter(true)
  716. e.c.emit(sal)
  717. }, false, putOnStack)
  718. case token.SHIFT_RIGHT:
  719. e.left.emitUnary(nil, func() {
  720. e.right.emitGetter(true)
  721. e.c.emit(sar)
  722. }, false, putOnStack)
  723. case token.UNSIGNED_SHIFT_RIGHT:
  724. e.left.emitUnary(nil, func() {
  725. e.right.emitGetter(true)
  726. e.c.emit(shr)
  727. }, false, putOnStack)
  728. default:
  729. panic(fmt.Errorf("Unknown assign operator: %s", e.operator.String()))
  730. }
  731. }
  732. func (e *compiledLiteral) emitGetter(putOnStack bool) {
  733. if putOnStack {
  734. e.addSrcMap()
  735. e.c.emit(loadVal(e.c.p.defineLiteralValue(e.val)))
  736. }
  737. }
  738. func (e *compiledLiteral) constant() bool {
  739. return true
  740. }
  741. func (c *compiler) compileParameterBindingIdentifier(name unistring.String, offset int) (*binding, bool) {
  742. if c.scope.strict {
  743. c.checkIdentifierName(name, offset)
  744. c.checkIdentifierLName(name, offset)
  745. }
  746. b, unique := c.scope.bindNameShadow(name)
  747. if !unique && c.scope.strict {
  748. c.throwSyntaxError(offset, "Strict mode function may not have duplicate parameter names (%s)", name)
  749. return nil, false
  750. }
  751. return b, unique
  752. }
  753. func (c *compiler) compileParameterPatternIdBinding(name unistring.String, offset int) {
  754. if _, unique := c.compileParameterBindingIdentifier(name, offset); !unique {
  755. c.throwSyntaxError(offset, "Duplicate parameter name not allowed in this context")
  756. }
  757. }
  758. func (c *compiler) compileParameterPatternBinding(item ast.Expression) {
  759. c.createBindings(item, c.compileParameterPatternIdBinding)
  760. }
  761. func (e *compiledFunctionLiteral) emitGetter(putOnStack bool) {
  762. savedPrg := e.c.p
  763. e.c.p = &Program{
  764. src: e.c.p.src,
  765. }
  766. e.c.newScope()
  767. e.c.scope.function = true
  768. var name unistring.String
  769. if e.expr.Name != nil {
  770. name = e.expr.Name.Name
  771. } else {
  772. name = e.lhsName
  773. }
  774. if name != "" {
  775. e.c.p.funcName = name
  776. }
  777. savedBlock := e.c.block
  778. defer func() {
  779. e.c.block = savedBlock
  780. }()
  781. e.c.block = &block{
  782. typ: blockScope,
  783. }
  784. if !e.c.scope.strict {
  785. e.c.scope.strict = e.strict != nil
  786. }
  787. hasPatterns := false
  788. hasInits := false
  789. firstDupIdx := -1
  790. length := 0
  791. if e.expr.ParameterList.Rest != nil {
  792. hasPatterns = true // strictly speaking not, but we need to activate all the checks
  793. }
  794. // First, make sure that the first bindings correspond to the formal parameters
  795. for _, item := range e.expr.ParameterList.List {
  796. switch tgt := item.Target.(type) {
  797. case *ast.Identifier:
  798. offset := int(tgt.Idx) - 1
  799. b, unique := e.c.compileParameterBindingIdentifier(tgt.Name, offset)
  800. if !unique {
  801. firstDupIdx = offset
  802. }
  803. b.isArg = true
  804. b.isVar = true
  805. case ast.Pattern:
  806. b := e.c.scope.addBinding(int(item.Idx0()) - 1)
  807. b.isArg = true
  808. hasPatterns = true
  809. default:
  810. e.c.throwSyntaxError(int(item.Idx0())-1, "Unsupported BindingElement type: %T", item)
  811. return
  812. }
  813. if item.Initializer != nil {
  814. hasInits = true
  815. }
  816. if hasPatterns || hasInits {
  817. if firstDupIdx >= 0 {
  818. e.c.throwSyntaxError(firstDupIdx, "Duplicate parameter name not allowed in this context")
  819. return
  820. }
  821. if e.strict != nil {
  822. e.c.throwSyntaxError(int(e.strict.Idx)-1, "Illegal 'use strict' directive in function with non-simple parameter list")
  823. return
  824. }
  825. }
  826. if !hasInits {
  827. length++
  828. }
  829. }
  830. // create pattern bindings
  831. if hasPatterns {
  832. for _, item := range e.expr.ParameterList.List {
  833. switch tgt := item.Target.(type) {
  834. case *ast.Identifier:
  835. // we already created those in the previous loop, skipping
  836. default:
  837. e.c.compileParameterPatternBinding(tgt)
  838. }
  839. }
  840. if rest := e.expr.ParameterList.Rest; rest != nil {
  841. e.c.compileParameterPatternBinding(rest)
  842. }
  843. }
  844. paramsCount := len(e.expr.ParameterList.List)
  845. e.c.scope.numArgs = paramsCount
  846. body := e.expr.Body.List
  847. funcs := e.c.extractFunctions(body)
  848. s := e.c.scope
  849. var calleeBinding *binding
  850. if e.isExpr && e.expr.Name != nil {
  851. if b, created := s.bindNameLexical(e.expr.Name.Name, false, 0); created {
  852. b.isConst = true
  853. calleeBinding = b
  854. }
  855. }
  856. preambleLen := 4 // enter, boxThis, createArgs, set
  857. e.c.p.code = make([]instruction, preambleLen, 8)
  858. if calleeBinding != nil {
  859. e.c.emit(loadCallee)
  860. calleeBinding.emitInit()
  861. }
  862. emitArgsRestMark := -1
  863. firstForwardRef := -1
  864. enterFunc2Mark := -1
  865. if hasPatterns || hasInits {
  866. for i, item := range e.expr.ParameterList.List {
  867. if pattern, ok := item.Target.(ast.Pattern); ok {
  868. i := i
  869. e.c.compilePatternInitExpr(func() {
  870. if firstForwardRef == -1 {
  871. s.bindings[i].emitGet()
  872. } else {
  873. e.c.emit(loadStackLex(-i - 1))
  874. }
  875. }, item.Initializer, item.Target.Idx0()).emitGetter(true)
  876. e.c.emitPattern(pattern, func(target, init compiledExpr) {
  877. e.c.emitPatternLexicalAssign(target, init, false)
  878. }, false)
  879. } else if item.Initializer != nil {
  880. markGet := len(e.c.p.code)
  881. e.c.emit(nil)
  882. mark := len(e.c.p.code)
  883. e.c.emit(nil)
  884. e.c.compileExpression(item.Initializer).emitGetter(true)
  885. if firstForwardRef == -1 && (s.isDynamic() || s.bindings[i].useCount() > 0) {
  886. firstForwardRef = i
  887. }
  888. if firstForwardRef == -1 {
  889. s.bindings[i].emitGetAt(markGet)
  890. } else {
  891. e.c.p.code[markGet] = loadStackLex(-i - 1)
  892. }
  893. s.bindings[i].emitInit()
  894. e.c.p.code[mark] = jdefP(len(e.c.p.code) - mark)
  895. } else {
  896. if firstForwardRef == -1 && s.bindings[i].useCount() > 0 {
  897. firstForwardRef = i
  898. }
  899. if firstForwardRef != -1 {
  900. e.c.emit(loadStackLex(-i - 1))
  901. s.bindings[i].emitInit()
  902. }
  903. }
  904. }
  905. if rest := e.expr.ParameterList.Rest; rest != nil {
  906. e.c.emitAssign(rest, e.c.compileEmitterExpr(
  907. func() {
  908. emitArgsRestMark = len(e.c.p.code)
  909. e.c.emit(createArgsRestStack(paramsCount))
  910. }, rest.Idx0()),
  911. func(target, init compiledExpr) {
  912. e.c.emitPatternLexicalAssign(target, init, false)
  913. })
  914. }
  915. if firstForwardRef != -1 {
  916. for _, b := range s.bindings {
  917. b.inStash = true
  918. }
  919. s.argsInStash = true
  920. s.needStash = true
  921. }
  922. e.c.newBlockScope()
  923. varScope := e.c.scope
  924. varScope.variable = true
  925. enterFunc2Mark = len(e.c.p.code)
  926. e.c.emit(nil)
  927. e.c.compileDeclList(e.expr.DeclarationList, true)
  928. e.c.createFunctionBindings(funcs)
  929. e.c.compileLexicalDeclarationsFuncBody(body, calleeBinding)
  930. for _, b := range varScope.bindings {
  931. if b.isVar {
  932. if parentBinding := s.boundNames[b.name]; parentBinding != nil && parentBinding != calleeBinding {
  933. parentBinding.emitGet()
  934. b.emitSetP()
  935. }
  936. }
  937. }
  938. } else {
  939. e.c.compileDeclList(e.expr.DeclarationList, true)
  940. e.c.createFunctionBindings(funcs)
  941. e.c.compileLexicalDeclarations(body, true)
  942. }
  943. e.c.compileFunctions(funcs)
  944. e.c.compileStatements(body, false)
  945. var last ast.Statement
  946. if l := len(body); l > 0 {
  947. last = body[l-1]
  948. }
  949. if _, ok := last.(*ast.ReturnStatement); !ok {
  950. e.c.emit(loadUndef, ret)
  951. }
  952. delta := 0
  953. code := e.c.p.code
  954. if calleeBinding != nil && !s.isDynamic() && calleeBinding.useCount() == 1 {
  955. s.deleteBinding(calleeBinding)
  956. preambleLen += 2
  957. }
  958. if !s.argsInStash && (s.argsNeeded || s.isDynamic()) {
  959. s.moveArgsToStash()
  960. }
  961. if s.argsNeeded {
  962. pos := preambleLen - 2
  963. delta += 2
  964. if s.strict || hasPatterns || hasInits {
  965. code[pos] = createArgsUnmapped(paramsCount)
  966. } else {
  967. code[pos] = createArgsMapped(paramsCount)
  968. }
  969. pos++
  970. b, _ := s.bindNameLexical("arguments", false, 0)
  971. if s.strict {
  972. b.isConst = true
  973. } else {
  974. b.isVar = true
  975. }
  976. b.markAccessPointAtScope(s, pos)
  977. code[pos] = storeStashP(0)
  978. }
  979. stashSize, stackSize := s.finaliseVarAlloc(0)
  980. if !s.strict && s.thisNeeded {
  981. delta++
  982. code[preambleLen-delta] = boxThis
  983. }
  984. delta++
  985. delta = preambleLen - delta
  986. var enter instruction
  987. if stashSize > 0 || s.argsInStash {
  988. if firstForwardRef == -1 {
  989. enter1 := enterFunc{
  990. numArgs: uint32(paramsCount),
  991. argsToStash: s.argsInStash,
  992. stashSize: uint32(stashSize),
  993. stackSize: uint32(stackSize),
  994. extensible: s.dynamic,
  995. }
  996. if s.isDynamic() {
  997. enter1.names = s.makeNamesMap()
  998. }
  999. enter = &enter1
  1000. if enterFunc2Mark != -1 {
  1001. ef2 := &enterFuncBody{
  1002. extensible: e.c.scope.dynamic,
  1003. }
  1004. e.c.updateEnterBlock(&ef2.enterBlock)
  1005. e.c.p.code[enterFunc2Mark] = ef2
  1006. }
  1007. } else {
  1008. enter1 := enterFunc1{
  1009. stashSize: uint32(stashSize),
  1010. numArgs: uint32(paramsCount),
  1011. argsToCopy: uint32(firstForwardRef),
  1012. extensible: s.dynamic,
  1013. }
  1014. if s.isDynamic() {
  1015. enter1.names = s.makeNamesMap()
  1016. }
  1017. enter = &enter1
  1018. if enterFunc2Mark != -1 {
  1019. ef2 := &enterFuncBody{
  1020. adjustStack: true,
  1021. extensible: e.c.scope.dynamic,
  1022. }
  1023. e.c.updateEnterBlock(&ef2.enterBlock)
  1024. e.c.p.code[enterFunc2Mark] = ef2
  1025. }
  1026. }
  1027. if emitArgsRestMark != -1 {
  1028. e.c.p.code[emitArgsRestMark] = createArgsRestStash
  1029. }
  1030. } else {
  1031. enter = &enterFuncStashless{
  1032. stackSize: uint32(stackSize),
  1033. args: uint32(paramsCount),
  1034. }
  1035. if enterFunc2Mark != -1 {
  1036. ef2 := &enterFuncBody{
  1037. extensible: e.c.scope.dynamic,
  1038. }
  1039. e.c.updateEnterBlock(&ef2.enterBlock)
  1040. e.c.p.code[enterFunc2Mark] = ef2
  1041. }
  1042. }
  1043. code[delta] = enter
  1044. if delta != 0 {
  1045. e.c.p.code = code[delta:]
  1046. for i := range e.c.p.srcMap {
  1047. e.c.p.srcMap[i].pc -= delta
  1048. }
  1049. s.adjustBase(-delta)
  1050. }
  1051. strict := s.strict
  1052. p := e.c.p
  1053. // e.c.p.dumpCode()
  1054. if enterFunc2Mark != -1 {
  1055. e.c.popScope()
  1056. }
  1057. e.c.popScope()
  1058. e.c.p = savedPrg
  1059. 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})
  1060. if !putOnStack {
  1061. e.c.emit(pop)
  1062. }
  1063. }
  1064. func (c *compiler) compileFunctionLiteral(v *ast.FunctionLiteral, isExpr bool) *compiledFunctionLiteral {
  1065. strictBody := c.isStrictStatement(v.Body)
  1066. if v.Name != nil && (c.scope.strict || strictBody != nil) {
  1067. c.checkIdentifierLName(v.Name.Name, int(v.Name.Idx)-1)
  1068. }
  1069. r := &compiledFunctionLiteral{
  1070. expr: v,
  1071. isExpr: isExpr,
  1072. strict: strictBody,
  1073. }
  1074. r.init(c, v.Idx0())
  1075. return r
  1076. }
  1077. func (e *compiledThisExpr) emitGetter(putOnStack bool) {
  1078. if putOnStack {
  1079. e.addSrcMap()
  1080. scope := e.c.scope
  1081. for ; scope != nil && !scope.function && !scope.eval; scope = scope.outer {
  1082. }
  1083. if scope != nil {
  1084. scope.thisNeeded = true
  1085. e.c.emit(loadStack(0))
  1086. } else {
  1087. e.c.emit(loadGlobalObject)
  1088. }
  1089. }
  1090. }
  1091. func (e *compiledNewExpr) emitGetter(putOnStack bool) {
  1092. e.callee.emitGetter(true)
  1093. for _, expr := range e.args {
  1094. expr.emitGetter(true)
  1095. }
  1096. e.addSrcMap()
  1097. e.c.emit(_new(len(e.args)))
  1098. if !putOnStack {
  1099. e.c.emit(pop)
  1100. }
  1101. }
  1102. func (c *compiler) compileNewExpression(v *ast.NewExpression) compiledExpr {
  1103. args := make([]compiledExpr, len(v.ArgumentList))
  1104. for i, expr := range v.ArgumentList {
  1105. args[i] = c.compileExpression(expr)
  1106. }
  1107. r := &compiledNewExpr{
  1108. callee: c.compileExpression(v.Callee),
  1109. args: args,
  1110. }
  1111. r.init(c, v.Idx0())
  1112. return r
  1113. }
  1114. func (e *compiledNewTarget) emitGetter(putOnStack bool) {
  1115. if putOnStack {
  1116. e.addSrcMap()
  1117. e.c.emit(loadNewTarget)
  1118. }
  1119. }
  1120. func (c *compiler) compileMetaProperty(v *ast.MetaProperty) compiledExpr {
  1121. if v.Meta.Name == "new" || v.Property.Name != "target" {
  1122. r := &compiledNewTarget{}
  1123. r.init(c, v.Idx0())
  1124. return r
  1125. }
  1126. c.throwSyntaxError(int(v.Idx)-1, "Unsupported meta property: %s.%s", v.Meta.Name, v.Property.Name)
  1127. return nil
  1128. }
  1129. func (e *compiledSequenceExpr) emitGetter(putOnStack bool) {
  1130. if len(e.sequence) > 0 {
  1131. for i := 0; i < len(e.sequence)-1; i++ {
  1132. e.sequence[i].emitGetter(false)
  1133. }
  1134. e.sequence[len(e.sequence)-1].emitGetter(putOnStack)
  1135. }
  1136. }
  1137. func (c *compiler) compileSequenceExpression(v *ast.SequenceExpression) compiledExpr {
  1138. s := make([]compiledExpr, len(v.Sequence))
  1139. for i, expr := range v.Sequence {
  1140. s[i] = c.compileExpression(expr)
  1141. }
  1142. r := &compiledSequenceExpr{
  1143. sequence: s,
  1144. }
  1145. var idx file.Idx
  1146. if len(v.Sequence) > 0 {
  1147. idx = v.Idx0()
  1148. }
  1149. r.init(c, idx)
  1150. return r
  1151. }
  1152. func (c *compiler) emitThrow(v Value) {
  1153. if o, ok := v.(*Object); ok {
  1154. t := nilSafe(o.self.getStr("name", nil)).toString().String()
  1155. switch t {
  1156. case "TypeError":
  1157. c.emit(loadDynamic(t))
  1158. msg := o.self.getStr("message", nil)
  1159. if msg != nil {
  1160. c.emit(loadVal(c.p.defineLiteralValue(msg)))
  1161. c.emit(_new(1))
  1162. } else {
  1163. c.emit(_new(0))
  1164. }
  1165. c.emit(throw)
  1166. return
  1167. }
  1168. }
  1169. panic(fmt.Errorf("unknown exception type thrown while evaliating constant expression: %s", v.String()))
  1170. }
  1171. func (c *compiler) emitConst(expr compiledExpr, putOnStack bool) {
  1172. v, ex := c.evalConst(expr)
  1173. if ex == nil {
  1174. if putOnStack {
  1175. c.emit(loadVal(c.p.defineLiteralValue(v)))
  1176. }
  1177. } else {
  1178. c.emitThrow(ex.val)
  1179. }
  1180. }
  1181. func (c *compiler) emitExpr(expr compiledExpr, putOnStack bool) {
  1182. if expr.constant() {
  1183. c.emitConst(expr, putOnStack)
  1184. } else {
  1185. expr.emitGetter(putOnStack)
  1186. }
  1187. }
  1188. func (c *compiler) evalConst(expr compiledExpr) (Value, *Exception) {
  1189. if expr, ok := expr.(*compiledLiteral); ok {
  1190. return expr.val, nil
  1191. }
  1192. if c.evalVM == nil {
  1193. c.evalVM = New().vm
  1194. }
  1195. var savedPrg *Program
  1196. createdPrg := false
  1197. if c.evalVM.prg == nil {
  1198. c.evalVM.prg = &Program{}
  1199. savedPrg = c.p
  1200. c.p = c.evalVM.prg
  1201. createdPrg = true
  1202. }
  1203. savedPc := len(c.p.code)
  1204. expr.emitGetter(true)
  1205. c.emit(halt)
  1206. c.evalVM.pc = savedPc
  1207. ex := c.evalVM.runTry()
  1208. if createdPrg {
  1209. c.evalVM.prg = nil
  1210. c.evalVM.pc = 0
  1211. c.p = savedPrg
  1212. } else {
  1213. c.evalVM.prg.code = c.evalVM.prg.code[:savedPc]
  1214. c.p.code = c.evalVM.prg.code
  1215. }
  1216. if ex == nil {
  1217. return c.evalVM.pop(), nil
  1218. }
  1219. return nil, ex
  1220. }
  1221. func (e *compiledUnaryExpr) constant() bool {
  1222. return e.operand.constant()
  1223. }
  1224. func (e *compiledUnaryExpr) emitGetter(putOnStack bool) {
  1225. var prepare, body func()
  1226. toNumber := func() {
  1227. e.c.emit(toNumber)
  1228. }
  1229. switch e.operator {
  1230. case token.NOT:
  1231. e.operand.emitGetter(true)
  1232. e.c.emit(not)
  1233. goto end
  1234. case token.BITWISE_NOT:
  1235. e.operand.emitGetter(true)
  1236. e.c.emit(bnot)
  1237. goto end
  1238. case token.TYPEOF:
  1239. if o, ok := e.operand.(compiledExprOrRef); ok {
  1240. o.emitGetterOrRef()
  1241. } else {
  1242. e.operand.emitGetter(true)
  1243. }
  1244. e.c.emit(typeof)
  1245. goto end
  1246. case token.DELETE:
  1247. e.operand.deleteExpr().emitGetter(putOnStack)
  1248. return
  1249. case token.MINUS:
  1250. e.c.emitExpr(e.operand, true)
  1251. e.c.emit(neg)
  1252. goto end
  1253. case token.PLUS:
  1254. e.c.emitExpr(e.operand, true)
  1255. e.c.emit(plus)
  1256. goto end
  1257. case token.INCREMENT:
  1258. prepare = toNumber
  1259. body = func() {
  1260. e.c.emit(inc)
  1261. }
  1262. case token.DECREMENT:
  1263. prepare = toNumber
  1264. body = func() {
  1265. e.c.emit(dec)
  1266. }
  1267. case token.VOID:
  1268. e.c.emitExpr(e.operand, false)
  1269. if putOnStack {
  1270. e.c.emit(loadUndef)
  1271. }
  1272. return
  1273. default:
  1274. panic(fmt.Errorf("Unknown unary operator: %s", e.operator.String()))
  1275. }
  1276. e.operand.emitUnary(prepare, body, e.postfix, putOnStack)
  1277. return
  1278. end:
  1279. if !putOnStack {
  1280. e.c.emit(pop)
  1281. }
  1282. }
  1283. func (c *compiler) compileUnaryExpression(v *ast.UnaryExpression) compiledExpr {
  1284. r := &compiledUnaryExpr{
  1285. operand: c.compileExpression(v.Operand),
  1286. operator: v.Operator,
  1287. postfix: v.Postfix,
  1288. }
  1289. r.init(c, v.Idx0())
  1290. return r
  1291. }
  1292. func (e *compiledConditionalExpr) emitGetter(putOnStack bool) {
  1293. e.test.emitGetter(true)
  1294. j := len(e.c.p.code)
  1295. e.c.emit(nil)
  1296. e.consequent.emitGetter(putOnStack)
  1297. j1 := len(e.c.p.code)
  1298. e.c.emit(nil)
  1299. e.c.p.code[j] = jne(len(e.c.p.code) - j)
  1300. e.alternate.emitGetter(putOnStack)
  1301. e.c.p.code[j1] = jump(len(e.c.p.code) - j1)
  1302. }
  1303. func (c *compiler) compileConditionalExpression(v *ast.ConditionalExpression) compiledExpr {
  1304. r := &compiledConditionalExpr{
  1305. test: c.compileExpression(v.Test),
  1306. consequent: c.compileExpression(v.Consequent),
  1307. alternate: c.compileExpression(v.Alternate),
  1308. }
  1309. r.init(c, v.Idx0())
  1310. return r
  1311. }
  1312. func (e *compiledLogicalOr) constant() bool {
  1313. if e.left.constant() {
  1314. if v, ex := e.c.evalConst(e.left); ex == nil {
  1315. if v.ToBoolean() {
  1316. return true
  1317. }
  1318. return e.right.constant()
  1319. } else {
  1320. return true
  1321. }
  1322. }
  1323. return false
  1324. }
  1325. func (e *compiledLogicalOr) emitGetter(putOnStack bool) {
  1326. if e.left.constant() {
  1327. if v, ex := e.c.evalConst(e.left); ex == nil {
  1328. if !v.ToBoolean() {
  1329. e.c.emitExpr(e.right, putOnStack)
  1330. } else {
  1331. if putOnStack {
  1332. e.c.emit(loadVal(e.c.p.defineLiteralValue(v)))
  1333. }
  1334. }
  1335. } else {
  1336. e.c.emitThrow(ex.val)
  1337. }
  1338. return
  1339. }
  1340. e.c.emitExpr(e.left, true)
  1341. j := len(e.c.p.code)
  1342. e.addSrcMap()
  1343. e.c.emit(nil)
  1344. e.c.emit(pop)
  1345. e.c.emitExpr(e.right, true)
  1346. e.c.p.code[j] = jeq1(len(e.c.p.code) - j)
  1347. if !putOnStack {
  1348. e.c.emit(pop)
  1349. }
  1350. }
  1351. func (e *compiledLogicalAnd) constant() bool {
  1352. if e.left.constant() {
  1353. if v, ex := e.c.evalConst(e.left); ex == nil {
  1354. if !v.ToBoolean() {
  1355. return true
  1356. } else {
  1357. return e.right.constant()
  1358. }
  1359. } else {
  1360. return true
  1361. }
  1362. }
  1363. return false
  1364. }
  1365. func (e *compiledLogicalAnd) emitGetter(putOnStack bool) {
  1366. var j int
  1367. if e.left.constant() {
  1368. if v, ex := e.c.evalConst(e.left); ex == nil {
  1369. if !v.ToBoolean() {
  1370. e.c.emit(loadVal(e.c.p.defineLiteralValue(v)))
  1371. } else {
  1372. e.c.emitExpr(e.right, putOnStack)
  1373. }
  1374. } else {
  1375. e.c.emitThrow(ex.val)
  1376. }
  1377. return
  1378. }
  1379. e.left.emitGetter(true)
  1380. j = len(e.c.p.code)
  1381. e.addSrcMap()
  1382. e.c.emit(nil)
  1383. e.c.emit(pop)
  1384. e.c.emitExpr(e.right, true)
  1385. e.c.p.code[j] = jneq1(len(e.c.p.code) - j)
  1386. if !putOnStack {
  1387. e.c.emit(pop)
  1388. }
  1389. }
  1390. func (e *compiledBinaryExpr) constant() bool {
  1391. return e.left.constant() && e.right.constant()
  1392. }
  1393. func (e *compiledBinaryExpr) emitGetter(putOnStack bool) {
  1394. e.c.emitExpr(e.left, true)
  1395. e.c.emitExpr(e.right, true)
  1396. e.addSrcMap()
  1397. switch e.operator {
  1398. case token.LESS:
  1399. e.c.emit(op_lt)
  1400. case token.GREATER:
  1401. e.c.emit(op_gt)
  1402. case token.LESS_OR_EQUAL:
  1403. e.c.emit(op_lte)
  1404. case token.GREATER_OR_EQUAL:
  1405. e.c.emit(op_gte)
  1406. case token.EQUAL:
  1407. e.c.emit(op_eq)
  1408. case token.NOT_EQUAL:
  1409. e.c.emit(op_neq)
  1410. case token.STRICT_EQUAL:
  1411. e.c.emit(op_strict_eq)
  1412. case token.STRICT_NOT_EQUAL:
  1413. e.c.emit(op_strict_neq)
  1414. case token.PLUS:
  1415. e.c.emit(add)
  1416. case token.MINUS:
  1417. e.c.emit(sub)
  1418. case token.MULTIPLY:
  1419. e.c.emit(mul)
  1420. case token.SLASH:
  1421. e.c.emit(div)
  1422. case token.REMAINDER:
  1423. e.c.emit(mod)
  1424. case token.AND:
  1425. e.c.emit(and)
  1426. case token.OR:
  1427. e.c.emit(or)
  1428. case token.EXCLUSIVE_OR:
  1429. e.c.emit(xor)
  1430. case token.INSTANCEOF:
  1431. e.c.emit(op_instanceof)
  1432. case token.IN:
  1433. e.c.emit(op_in)
  1434. case token.SHIFT_LEFT:
  1435. e.c.emit(sal)
  1436. case token.SHIFT_RIGHT:
  1437. e.c.emit(sar)
  1438. case token.UNSIGNED_SHIFT_RIGHT:
  1439. e.c.emit(shr)
  1440. default:
  1441. panic(fmt.Errorf("Unknown operator: %s", e.operator.String()))
  1442. }
  1443. if !putOnStack {
  1444. e.c.emit(pop)
  1445. }
  1446. }
  1447. func (c *compiler) compileBinaryExpression(v *ast.BinaryExpression) compiledExpr {
  1448. switch v.Operator {
  1449. case token.LOGICAL_OR:
  1450. return c.compileLogicalOr(v.Left, v.Right, v.Idx0())
  1451. case token.LOGICAL_AND:
  1452. return c.compileLogicalAnd(v.Left, v.Right, v.Idx0())
  1453. }
  1454. r := &compiledBinaryExpr{
  1455. left: c.compileExpression(v.Left),
  1456. right: c.compileExpression(v.Right),
  1457. operator: v.Operator,
  1458. }
  1459. r.init(c, v.Idx0())
  1460. return r
  1461. }
  1462. func (c *compiler) compileLogicalOr(left, right ast.Expression, idx file.Idx) compiledExpr {
  1463. r := &compiledLogicalOr{
  1464. left: c.compileExpression(left),
  1465. right: c.compileExpression(right),
  1466. }
  1467. r.init(c, idx)
  1468. return r
  1469. }
  1470. func (c *compiler) compileLogicalAnd(left, right ast.Expression, idx file.Idx) compiledExpr {
  1471. r := &compiledLogicalAnd{
  1472. left: c.compileExpression(left),
  1473. right: c.compileExpression(right),
  1474. }
  1475. r.init(c, idx)
  1476. return r
  1477. }
  1478. func (e *compiledObjectLiteral) emitGetter(putOnStack bool) {
  1479. e.addSrcMap()
  1480. e.c.emit(newObject)
  1481. for _, prop := range e.expr.Value {
  1482. switch prop := prop.(type) {
  1483. case *ast.PropertyKeyed:
  1484. keyExpr := e.c.compileExpression(prop.Key)
  1485. computed := false
  1486. var key unistring.String
  1487. switch keyExpr := keyExpr.(type) {
  1488. case *compiledLiteral:
  1489. key = keyExpr.val.string()
  1490. default:
  1491. keyExpr.emitGetter(true)
  1492. computed = true
  1493. //e.c.throwSyntaxError(e.offset, "non-literal properties in object literal are not supported yet")
  1494. }
  1495. valueExpr := e.c.compileExpression(prop.Value)
  1496. var anonFn *compiledFunctionLiteral
  1497. if fn, ok := valueExpr.(*compiledFunctionLiteral); ok {
  1498. if fn.expr.Name == nil {
  1499. anonFn = fn
  1500. fn.lhsName = key
  1501. }
  1502. }
  1503. if computed {
  1504. valueExpr.emitGetter(true)
  1505. switch prop.Kind {
  1506. case ast.PropertyKindValue, ast.PropertyKindMethod:
  1507. if anonFn != nil {
  1508. e.c.emit(setElem1Named)
  1509. } else {
  1510. e.c.emit(setElem1)
  1511. }
  1512. case ast.PropertyKindGet:
  1513. e.c.emit(setPropGetter1)
  1514. case ast.PropertyKindSet:
  1515. e.c.emit(setPropSetter1)
  1516. default:
  1517. panic(fmt.Errorf("unknown property kind: %s", prop.Kind))
  1518. }
  1519. } else {
  1520. if anonFn != nil {
  1521. anonFn.lhsName = key
  1522. }
  1523. valueExpr.emitGetter(true)
  1524. switch prop.Kind {
  1525. case ast.PropertyKindValue:
  1526. if key == __proto__ {
  1527. e.c.emit(setProto)
  1528. } else {
  1529. e.c.emit(setProp1(key))
  1530. }
  1531. case ast.PropertyKindMethod:
  1532. e.c.emit(setProp1(key))
  1533. case ast.PropertyKindGet:
  1534. e.c.emit(setPropGetter(key))
  1535. case ast.PropertyKindSet:
  1536. e.c.emit(setPropSetter(key))
  1537. default:
  1538. panic(fmt.Errorf("unknown property kind: %s", prop.Kind))
  1539. }
  1540. }
  1541. case *ast.PropertyShort:
  1542. key := prop.Name.Name
  1543. if prop.Initializer != nil {
  1544. e.c.throwSyntaxError(int(prop.Initializer.Idx0())-1, "Invalid shorthand property initializer")
  1545. }
  1546. if e.c.scope.strict && key == "let" {
  1547. e.c.throwSyntaxError(e.offset, "'let' cannot be used as a shorthand property in strict mode")
  1548. }
  1549. e.c.compileIdentifierExpression(&prop.Name).emitGetter(true)
  1550. e.c.emit(setProp1(key))
  1551. case *ast.SpreadElement:
  1552. e.c.compileExpression(prop.Expression).emitGetter(true)
  1553. e.c.emit(copySpread)
  1554. default:
  1555. panic(fmt.Errorf("unknown Property type: %T", prop))
  1556. }
  1557. }
  1558. if !putOnStack {
  1559. e.c.emit(pop)
  1560. }
  1561. }
  1562. func (c *compiler) compileObjectLiteral(v *ast.ObjectLiteral) compiledExpr {
  1563. r := &compiledObjectLiteral{
  1564. expr: v,
  1565. }
  1566. r.init(c, v.Idx0())
  1567. return r
  1568. }
  1569. func (e *compiledArrayLiteral) emitGetter(putOnStack bool) {
  1570. e.addSrcMap()
  1571. hasSpread := false
  1572. mark := len(e.c.p.code)
  1573. e.c.emit(nil)
  1574. for _, v := range e.expr.Value {
  1575. if spread, ok := v.(*ast.SpreadElement); ok {
  1576. hasSpread = true
  1577. e.c.compileExpression(spread.Expression).emitGetter(true)
  1578. e.c.emit(pushArraySpread)
  1579. } else {
  1580. if v != nil {
  1581. e.c.compileExpression(v).emitGetter(true)
  1582. } else {
  1583. e.c.emit(loadNil)
  1584. }
  1585. e.c.emit(pushArrayItem)
  1586. }
  1587. }
  1588. var objCount uint32
  1589. if !hasSpread {
  1590. objCount = uint32(len(e.expr.Value))
  1591. }
  1592. e.c.p.code[mark] = newArray(objCount)
  1593. if !putOnStack {
  1594. e.c.emit(pop)
  1595. }
  1596. }
  1597. func (c *compiler) compileArrayLiteral(v *ast.ArrayLiteral) compiledExpr {
  1598. r := &compiledArrayLiteral{
  1599. expr: v,
  1600. }
  1601. r.init(c, v.Idx0())
  1602. return r
  1603. }
  1604. func (e *compiledRegexpLiteral) emitGetter(putOnStack bool) {
  1605. if putOnStack {
  1606. pattern, err := compileRegexp(e.expr.Pattern, e.expr.Flags)
  1607. if err != nil {
  1608. e.c.throwSyntaxError(e.offset, err.Error())
  1609. }
  1610. e.c.emit(&newRegexp{pattern: pattern, src: newStringValue(e.expr.Pattern)})
  1611. }
  1612. }
  1613. func (c *compiler) compileRegexpLiteral(v *ast.RegExpLiteral) compiledExpr {
  1614. r := &compiledRegexpLiteral{
  1615. expr: v,
  1616. }
  1617. r.init(c, v.Idx0())
  1618. return r
  1619. }
  1620. func (e *compiledCallExpr) emitGetter(putOnStack bool) {
  1621. var calleeName unistring.String
  1622. switch callee := e.callee.(type) {
  1623. case *compiledDotExpr:
  1624. callee.left.emitGetter(true)
  1625. e.c.emit(dup)
  1626. e.c.emit(getPropCallee(callee.name))
  1627. case *compiledBracketExpr:
  1628. callee.left.emitGetter(true)
  1629. e.c.emit(dup)
  1630. callee.member.emitGetter(true)
  1631. e.c.emit(getElemCallee)
  1632. case *compiledIdentifierExpr:
  1633. calleeName = callee.name
  1634. callee.emitGetterAndCallee()
  1635. default:
  1636. e.c.emit(loadUndef)
  1637. callee.emitGetter(true)
  1638. }
  1639. for _, expr := range e.args {
  1640. expr.emitGetter(true)
  1641. }
  1642. e.addSrcMap()
  1643. if calleeName == "eval" {
  1644. foundFunc, foundVar := false, false
  1645. for sc := e.c.scope; sc != nil; sc = sc.outer {
  1646. if !foundFunc && sc.function {
  1647. foundFunc = true
  1648. sc.thisNeeded, sc.argsNeeded = true, true
  1649. }
  1650. if !foundVar && (sc.variable || sc.function) {
  1651. foundVar = true
  1652. if !sc.strict {
  1653. sc.dynamic = true
  1654. }
  1655. }
  1656. sc.dynLookup = true
  1657. }
  1658. if e.c.scope.strict {
  1659. e.c.emit(callEvalStrict(len(e.args)))
  1660. } else {
  1661. e.c.emit(callEval(len(e.args)))
  1662. }
  1663. } else {
  1664. e.c.emit(call(len(e.args)))
  1665. }
  1666. if !putOnStack {
  1667. e.c.emit(pop)
  1668. }
  1669. }
  1670. func (e *compiledCallExpr) deleteExpr() compiledExpr {
  1671. r := &defaultDeleteExpr{
  1672. expr: e,
  1673. }
  1674. r.init(e.c, file.Idx(e.offset+1))
  1675. return r
  1676. }
  1677. func (c *compiler) compileCallExpression(v *ast.CallExpression) compiledExpr {
  1678. args := make([]compiledExpr, len(v.ArgumentList))
  1679. for i, argExpr := range v.ArgumentList {
  1680. args[i] = c.compileExpression(argExpr)
  1681. }
  1682. r := &compiledCallExpr{
  1683. args: args,
  1684. callee: c.compileExpression(v.Callee),
  1685. }
  1686. r.init(c, v.LeftParenthesis)
  1687. return r
  1688. }
  1689. func (c *compiler) compileIdentifierExpression(v *ast.Identifier) compiledExpr {
  1690. if c.scope.strict {
  1691. c.checkIdentifierName(v.Name, int(v.Idx)-1)
  1692. }
  1693. r := &compiledIdentifierExpr{
  1694. name: v.Name,
  1695. }
  1696. r.offset = int(v.Idx) - 1
  1697. r.init(c, v.Idx0())
  1698. return r
  1699. }
  1700. func (c *compiler) compileNumberLiteral(v *ast.NumberLiteral) compiledExpr {
  1701. if c.scope.strict && octalRegexp.MatchString(v.Literal) {
  1702. c.throwSyntaxError(int(v.Idx)-1, "Octal literals are not allowed in strict mode")
  1703. panic("Unreachable")
  1704. }
  1705. var val Value
  1706. switch num := v.Value.(type) {
  1707. case int64:
  1708. val = intToValue(num)
  1709. case float64:
  1710. val = floatToValue(num)
  1711. default:
  1712. panic(fmt.Errorf("Unsupported number literal type: %T", v.Value))
  1713. }
  1714. r := &compiledLiteral{
  1715. val: val,
  1716. }
  1717. r.init(c, v.Idx0())
  1718. return r
  1719. }
  1720. func (c *compiler) compileStringLiteral(v *ast.StringLiteral) compiledExpr {
  1721. r := &compiledLiteral{
  1722. val: stringValueFromRaw(v.Value),
  1723. }
  1724. r.init(c, v.Idx0())
  1725. return r
  1726. }
  1727. func (c *compiler) compileBooleanLiteral(v *ast.BooleanLiteral) compiledExpr {
  1728. var val Value
  1729. if v.Value {
  1730. val = valueTrue
  1731. } else {
  1732. val = valueFalse
  1733. }
  1734. r := &compiledLiteral{
  1735. val: val,
  1736. }
  1737. r.init(c, v.Idx0())
  1738. return r
  1739. }
  1740. func (c *compiler) compileAssignExpression(v *ast.AssignExpression) compiledExpr {
  1741. // log.Printf("compileAssignExpression(): %+v", v)
  1742. r := &compiledAssignExpr{
  1743. left: c.compileExpression(v.Left),
  1744. right: c.compileExpression(v.Right),
  1745. operator: v.Operator,
  1746. }
  1747. r.init(c, v.Idx0())
  1748. return r
  1749. }
  1750. func (e *compiledEnumGetExpr) emitGetter(putOnStack bool) {
  1751. e.c.emit(enumGet)
  1752. if !putOnStack {
  1753. e.c.emit(pop)
  1754. }
  1755. }
  1756. func (c *compiler) compileObjectAssignmentPattern(v *ast.ObjectPattern) compiledExpr {
  1757. r := &compiledObjectAssignmentPattern{
  1758. expr: v,
  1759. }
  1760. r.init(c, v.Idx0())
  1761. return r
  1762. }
  1763. func (e *compiledObjectAssignmentPattern) emitGetter(putOnStack bool) {
  1764. if putOnStack {
  1765. e.c.emit(loadUndef)
  1766. }
  1767. }
  1768. func (c *compiler) compileArrayAssignmentPattern(v *ast.ArrayPattern) compiledExpr {
  1769. r := &compiledArrayAssignmentPattern{
  1770. expr: v,
  1771. }
  1772. r.init(c, v.Idx0())
  1773. return r
  1774. }
  1775. func (e *compiledArrayAssignmentPattern) emitGetter(putOnStack bool) {
  1776. if putOnStack {
  1777. e.c.emit(loadUndef)
  1778. }
  1779. }
  1780. func (c *compiler) emitNamed(expr compiledExpr, name unistring.String) {
  1781. if en, ok := expr.(interface {
  1782. emitNamed(name unistring.String)
  1783. }); ok {
  1784. en.emitNamed(name)
  1785. } else {
  1786. expr.emitGetter(true)
  1787. }
  1788. }
  1789. func (e *compiledFunctionLiteral) emitNamed(name unistring.String) {
  1790. e.lhsName = name
  1791. e.emitGetter(true)
  1792. }
  1793. func (c *compiler) emitPattern(pattern ast.Pattern, emitter func(target, init compiledExpr), putOnStack bool) {
  1794. switch pattern := pattern.(type) {
  1795. case *ast.ObjectPattern:
  1796. c.emitObjectPattern(pattern, emitter, putOnStack)
  1797. case *ast.ArrayPattern:
  1798. c.emitArrayPattern(pattern, emitter, putOnStack)
  1799. default:
  1800. panic(fmt.Errorf("unsupported Pattern: %T", pattern))
  1801. }
  1802. }
  1803. func (c *compiler) emitAssign(target ast.Expression, init compiledExpr, emitAssignSimple func(target, init compiledExpr)) {
  1804. pattern, isPattern := target.(ast.Pattern)
  1805. if isPattern {
  1806. init.emitGetter(true)
  1807. c.emitPattern(pattern, emitAssignSimple, false)
  1808. } else {
  1809. emitAssignSimple(c.compileExpression(target), init)
  1810. }
  1811. }
  1812. func (c *compiler) emitObjectPattern(pattern *ast.ObjectPattern, emitAssign func(target, init compiledExpr), putOnStack bool) {
  1813. if pattern.Rest != nil {
  1814. c.emit(createDestructSrc)
  1815. } else {
  1816. c.emit(checkObjectCoercible)
  1817. }
  1818. for _, prop := range pattern.Properties {
  1819. switch prop := prop.(type) {
  1820. case *ast.PropertyShort:
  1821. c.emit(dup)
  1822. emitAssign(c.compileIdentifierExpression(&prop.Name), c.compilePatternInitExpr(func() {
  1823. c.emit(getProp(prop.Name.Name))
  1824. }, prop.Initializer, prop.Idx0()))
  1825. case *ast.PropertyKeyed:
  1826. c.emit(dup)
  1827. c.compileExpression(prop.Key).emitGetter(true)
  1828. var target ast.Expression
  1829. var initializer ast.Expression
  1830. if e, ok := prop.Value.(*ast.AssignExpression); ok {
  1831. target = e.Left
  1832. initializer = e.Right
  1833. } else {
  1834. target = prop.Value
  1835. }
  1836. c.emitAssign(target, c.compilePatternInitExpr(func() {
  1837. c.emit(getElem)
  1838. }, initializer, prop.Idx0()), emitAssign)
  1839. default:
  1840. c.throwSyntaxError(int(prop.Idx0()-1), "Unsupported AssignmentProperty type: %T", prop)
  1841. }
  1842. }
  1843. if pattern.Rest != nil {
  1844. emitAssign(c.compileExpression(pattern.Rest), c.compileEmitterExpr(func() {
  1845. c.emit(copyRest)
  1846. }, pattern.Rest.Idx0()))
  1847. c.emit(pop)
  1848. }
  1849. if !putOnStack {
  1850. c.emit(pop)
  1851. }
  1852. }
  1853. func (c *compiler) emitArrayPattern(pattern *ast.ArrayPattern, emitAssign func(target, init compiledExpr), putOnStack bool) {
  1854. var marks []int
  1855. c.emit(iterate)
  1856. for _, elt := range pattern.Elements {
  1857. switch elt := elt.(type) {
  1858. case nil:
  1859. marks = append(marks, len(c.p.code))
  1860. c.emit(nil)
  1861. case *ast.AssignExpression:
  1862. c.emitAssign(elt.Left, c.compilePatternInitExpr(func() {
  1863. marks = append(marks, len(c.p.code))
  1864. c.emit(nil, enumGet)
  1865. }, elt.Right, elt.Idx0()), emitAssign)
  1866. default:
  1867. c.emitAssign(elt, c.compileEmitterExpr(func() {
  1868. marks = append(marks, len(c.p.code))
  1869. c.emit(nil, enumGet)
  1870. }, elt.Idx0()), emitAssign)
  1871. }
  1872. }
  1873. if pattern.Rest != nil {
  1874. c.emitAssign(pattern.Rest, c.compileEmitterExpr(func() {
  1875. c.emit(newArrayFromIter)
  1876. }, pattern.Rest.Idx0()), emitAssign)
  1877. } else {
  1878. c.emit(enumPopClose)
  1879. }
  1880. mark1 := len(c.p.code)
  1881. c.emit(nil)
  1882. for i, elt := range pattern.Elements {
  1883. switch elt := elt.(type) {
  1884. case nil:
  1885. c.p.code[marks[i]] = iterNext(len(c.p.code) - marks[i])
  1886. case *ast.Identifier:
  1887. emitAssign(c.compileIdentifierExpression(elt), c.compileEmitterExpr(func() {
  1888. c.p.code[marks[i]] = iterNext(len(c.p.code) - marks[i])
  1889. c.emit(loadUndef)
  1890. }, elt.Idx0()))
  1891. case *ast.AssignExpression:
  1892. c.emitAssign(elt.Left, c.compileNamedEmitterExpr(func(name unistring.String) {
  1893. c.p.code[marks[i]] = iterNext(len(c.p.code) - marks[i])
  1894. c.emitNamed(c.compileExpression(elt.Right), name)
  1895. }, elt.Idx0()), emitAssign)
  1896. default:
  1897. c.emitAssign(elt, c.compileEmitterExpr(
  1898. func() {
  1899. c.p.code[marks[i]] = iterNext(len(c.p.code) - marks[i])
  1900. c.emit(loadUndef)
  1901. }, elt.Idx0()), emitAssign)
  1902. }
  1903. }
  1904. c.emit(enumPop)
  1905. if pattern.Rest != nil {
  1906. c.emitAssign(pattern.Rest, c.compileExpression(
  1907. &ast.ArrayLiteral{
  1908. LeftBracket: pattern.Rest.Idx0(),
  1909. RightBracket: pattern.Rest.Idx0(),
  1910. }), emitAssign)
  1911. }
  1912. c.p.code[mark1] = jump(len(c.p.code) - mark1)
  1913. if !putOnStack {
  1914. c.emit(pop)
  1915. }
  1916. }
  1917. func (e *compiledObjectAssignmentPattern) emitSetter(valueExpr compiledExpr, putOnStack bool) {
  1918. valueExpr.emitGetter(true)
  1919. e.c.emitObjectPattern(e.expr, e.c.emitPatternAssign, putOnStack)
  1920. }
  1921. func (e *compiledArrayAssignmentPattern) emitSetter(valueExpr compiledExpr, putOnStack bool) {
  1922. valueExpr.emitGetter(true)
  1923. e.c.emitArrayPattern(e.expr, e.c.emitPatternAssign, putOnStack)
  1924. }
  1925. type compiledPatternInitExpr struct {
  1926. baseCompiledExpr
  1927. emitSrc func()
  1928. def compiledExpr
  1929. }
  1930. func (e *compiledPatternInitExpr) emitGetter(putOnStack bool) {
  1931. if !putOnStack {
  1932. return
  1933. }
  1934. e.emitSrc()
  1935. if e.def != nil {
  1936. mark := len(e.c.p.code)
  1937. e.c.emit(nil)
  1938. e.def.emitGetter(true)
  1939. e.c.p.code[mark] = jdef(len(e.c.p.code) - mark)
  1940. }
  1941. }
  1942. func (e *compiledPatternInitExpr) emitNamed(name unistring.String) {
  1943. e.emitSrc()
  1944. if e.def != nil {
  1945. mark := len(e.c.p.code)
  1946. e.c.emit(nil)
  1947. e.c.emitNamed(e.def, name)
  1948. e.c.p.code[mark] = jdef(len(e.c.p.code) - mark)
  1949. }
  1950. }
  1951. func (c *compiler) compilePatternInitExpr(emitSrc func(), def ast.Expression, idx file.Idx) compiledExpr {
  1952. r := &compiledPatternInitExpr{
  1953. emitSrc: emitSrc,
  1954. def: c.compileExpression(def),
  1955. }
  1956. r.init(c, idx)
  1957. return r
  1958. }
  1959. type compiledEmitterExpr struct {
  1960. baseCompiledExpr
  1961. emitter func()
  1962. namedEmitter func(name unistring.String)
  1963. }
  1964. func (e *compiledEmitterExpr) emitGetter(putOnStack bool) {
  1965. if e.emitter != nil {
  1966. e.emitter()
  1967. } else {
  1968. e.namedEmitter("")
  1969. }
  1970. if !putOnStack {
  1971. e.c.emit(pop)
  1972. }
  1973. }
  1974. func (e *compiledEmitterExpr) emitNamed(name unistring.String) {
  1975. if e.namedEmitter != nil {
  1976. e.namedEmitter(name)
  1977. } else {
  1978. e.emitter()
  1979. }
  1980. }
  1981. func (c *compiler) compileEmitterExpr(emitter func(), idx file.Idx) *compiledEmitterExpr {
  1982. r := &compiledEmitterExpr{
  1983. emitter: emitter,
  1984. }
  1985. r.init(c, idx)
  1986. return r
  1987. }
  1988. func (c *compiler) compileNamedEmitterExpr(namedEmitter func(unistring.String), idx file.Idx) *compiledEmitterExpr {
  1989. r := &compiledEmitterExpr{
  1990. namedEmitter: namedEmitter,
  1991. }
  1992. r.init(c, idx)
  1993. return r
  1994. }