compiler_expr.go 53 KB

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