compiler_expr.go 51 KB

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