compiler_expr.go 53 KB

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