compiler_expr.go 52 KB

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