compiler_expr.go 53 KB

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