compiler_expr.go 53 KB

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