compiler_expr.go 54 KB

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