gitoria

layouts

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit5f82554e5f82554enotes-demo: format Style with nested rules and bare mediamre5f82554e/plugins/web/compile.hl

61.9 KB

  1. // plugins/web/compile.hl — THE FRAMEWORK COMPILES EACH COMPONENT.
  2. //
  3. // Mission 313 (the creator, 15 Sep 14:30: "clientside interpreter + table against
  4. // component code per component, no tables needed"). The old hl:web ships ONE generic client
  5. // that walks a tree the seed carries: it pairs elements with nodes, asks a table which
  6. // members each element shows, asks `view.value` what each bound spot holds, and does it
  7. // again on every paint. hl:web ships the ANSWERS instead — this file writes each
  8. // component's own code, once, where the old hl:web would have walked.
  9. //
  10. // WHAT IS EMITTED, per component, beside the class `hlJs` compiled:
  11. //
  12. // v(c,m,o,host,i,rows,rk,fill,cr,opt) the component's View, unrolled: one
  13. // statement per element, per handler, per
  14. // bound spot. `cr` false CLAIMS the server's
  15. // DOM (take host.children[i], step on), `cr`
  16. // true CREATES it — the two walks the old hl:web
  17. // spells out separately, which differ only in
  18. // where an element comes from.
  19. // b["<for site>"] one `for` body — the row factory the keyed
  20. // region builds a row with.
  21. // t/e["<if site>"] an `if`'s two branches.
  22. // f["<reference site>"] the fragment this component writes into a
  23. // child's `slot` — built in THIS frame, which
  24. // is why it is this component's code.
  25. // p["<element site>"] the paint statements: `p` writes what one
  26. // member feeds (the old hl:web's paint(&m,s,name)),
  27. // `r` rewrites a row's element where the DOM
  28. // does not already hold it (rowPaint).
  29. //
  30. // The generated code calls a FIXED runtime (client.hl) for everything that is not this
  31. // component: the mount records, the keyed region diff, the binding edges, the
  32. // derivations, the socket, navigation. It never looks anything up in a table — the
  33. // names, the sites and the write sets are written into the statements.
  34. //
  35. // It is TEXT the framework produces, served from memory beside the module (WebFramework
  36. // `module`), never a JavaScript file under plugins/.
  37. import { newline, tab } from './view.hl'
  38. import HlwView from './view.hl'
  39. import HlwViewServer from './viewserver.hl'
  40. // ---- the emitted half --------------------------------------------------------------
  41. module(gen, key, nodes, fnTable, fills) {
  42. fns = [] // the sub-walks: row bodies, if branches, fills
  43. done = {}
  44. rowVars = []
  45. tbl = tableOf(gen, key) // what the language says about this file, read once
  46. regions = regionIndex(tbl) // a `for`'s list reads, an `if`'s condition reads
  47. // A SHELL'S VIEW IS A `body` ELEMENT and the document already has one: the walk runs
  48. // against document.body itself, so the root list is that element's children and the
  49. // element only takes the marks a region rebuild on it would need.
  50. let roots = nodes
  51. let bodySite = null
  52. if (nodes.length == 1 && nodes[0].k == 'el' && nodes[0].tag == 'body') {
  53. roots = nodes[0].children
  54. bodySite = nodes[0].site
  55. }
  56. let root = walkTop(key, roots)
  57. // THE NAMED-REGION FILLS this View's root writes (`side { … }`): each is a sub-walk of THIS
  58. // component (its members, its handlers), run by the browser into the region a level above holds
  59. let fillNames = '{'
  60. let fillConds = '{'
  61. if (fills != null) {
  62. for (fl of fills) {
  63. sub(key, fl.children, 'R', fl.site)
  64. fillNames = fillNames + (fillNames == '{' ? '' : ',') + str(fl.name) + ':' + str(fl.site)
  65. // a conditional fill (`if (x) { side { … } }`): the condition, read off the filling instance
  66. if (fl.cond != null) { fillConds = fillConds + (fillConds == '{' ? '' : ',') + str(fl.name) + ':function(inst,r){return ' + rawRead(fl.cond) + ';}' }
  67. }
  68. }
  69. fillNames = fillNames + '}'
  70. fillConds = fillConds + '}'
  71. if (bodySite != null) {
  72. root = root.replace('let r=rows;', 'let r=rows;await c.markHost(m,host,' + str(bodySite) + ');')
  73. }
  74. let src = ';(function(){var g=(typeof globalThis!=="undefined"?globalThis:window);'
  75. src = src + 'var C=(g.__hlC=g.__hlC||{});'
  76. // the element a claim takes or a create makes — the one shape both walks share; a create
  77. // inside an svg (or an svg itself) is made in the SVG namespace, as the parser makes it
  78. // THE PRELUDE, one per module: the element a claim takes or a create makes, and the
  79. // two reads. A read written out in full — `(r["title"]!=null?r["title"]:inst["title"])`
  80. // — is 45 bytes and a component's View holds dozens of them; named, it is 16. The
  81. // rule is the same rule (a `for` row variable in scope wins over a member of the
  82. // same name, and a field path walks into it), spelled once.
  83. // A claim also moves the host's CURSOR — the last node the walk took there — which is
  84. // where a text leaf and a region's mark are looked for next (client.hl `leafNode`,
  85. // `markAt`).
  86. // MISSION 505: the five helpers (A, S, X, RV, RF) are bound off the runtime text (`paintHelpers` below) instead of written
  87. // into every module
  88. src = src + 'var A=g.__hlP.A,S=g.__hlP.S,X=g.__hlP.X,RV=g.__hlP.RV,RF=g.__hlP.RF;'
  89. src = src + 'var B={},T={},E={},F={},P={},D={},R={};'
  90. // ONE FUNCTION PER LIST, however many sites reach it: a nested walk is written once
  91. // and named — emitting it per site made the reference app's home page 215 KB of the
  92. // same code over and over.
  93. let wi = 0
  94. for (f of fns) {
  95. wi = wi + 1
  96. let name = 'w' + wi
  97. src = src + 'var ' + name + '=' + f.src + ';'
  98. for (site of f.sites) { src = src + f.slot + '[' + str(site) + ']=' + name + ';' }
  99. }
  100. let paints = []
  101. collectPaints(&paints, roots)
  102. for (e of paints) { src = src + 'P[' + str(e.site) + ']={p:' + e.paint + ',r:' + e.row + '};' }
  103. src = src + 'C[' + str(key) + ']={v:' + root + ',b:B,t:T,e:E,f:F,p:P,d:D,r:R,rn:' + fillNames + ',rc:' + fillConds
  104. // THE ANSWERS A LOOKUP STILL NEEDS, and only those. A local event's write set stands
  105. // at its own listener and a region's names at its own region — written into the
  106. // call. These three are asked for by NAME at run time and cannot be: an inbound
  107. // frame names its event, a routine handed down names the member that holds it, and
  108. // a write set names the derivations that follow it. `on` is the one thing run FROM the
  109. // module's own scope: the language's instance-scoped handler (`on page.ev`, the instance's
  110. // own bus: runtime `hlInstanceBus`) with the event named at run time, which client.hl
  111. // `wireUp` registers for a page (#193). The bus class is read off the instance's own class
  112. // (`static __events__`): a module imports no `hlScopedBus` unless it writes an `on x.y`, and
  113. // a minified module renames `EventBus`.
  114. src = src + ',ev:' + eventMap() + ',mw:' + memberMap() + ',dv:' + derivList() + ',fn:' + fnMap(fnTable) + ',on:function(i,e,f){if(!Object.prototype.hasOwnProperty.call(i,"__instanceEvents__")){Object.defineProperty(i,"__instanceEvents__",{value:new (i.constructor.__events__.constructor)()});}i.__instanceEvents__.on(e,f);}};'
  115. return src + '})();' + newline
  116. }
  117. // MISSION 505: THE SHARED PRELUDE, once per page. The five helpers every module's paint text calls — the element a claim
  118. // takes or a create makes (A), the text of a value (S), a region's sub-walk (X), the two reads (RV, RF) — were written
  119. // into every module: 699 bytes, 25 times on the reference app's start page. They are appended to the runtime text the
  120. // framework serves (WebFramework `runtime`), as `globalThis.__hlP`, and a module binds them by name (`module` above):
  121. // every module imports the runtime first, so they are there when its paint text runs. The two texts must agree on the
  122. // names; this is the only place the helpers are written.
  123. paintHelpers() {
  124. let s = ';(function(){var g=(typeof globalThis!=="undefined"?globalThis:window);var P=(g.__hlP=g.__hlP||{});'
  125. s = s + 'P.A=function(cr,host,i,tag){if(cr){if(tag==="svg"||(host.namespaceURI==="http://www.w3.org/2000/svg"&&host.localName.toLowerCase()!=="foreignobject")){return document.createElementNS("http://www.w3.org/2000/svg",tag);}return document.createElement(tag);}var e=host.children[i];if(e!=null){host.__hlCur=e;}return e;};'
  126. s = s + 'P.S=function(v){return v==null?"":""+v;};'
  127. s = s + 'P.X=async function(t,r,q){return await g.__hlX(t,q,r);};'
  128. s = s + 'P.RV=function(r,q,n){return r[n]!=null?r[n]:q[n];};'
  129. s = s + 'P.RF=function(r,q,n,p){var v=P.RV(r,q,n);for(var k=0;k<p.length;k++){if(v==null)return null;v=v[p[k]];}return v;};'
  130. s = s + 'P.L=function(u){return import(u);};'
  131. return s + '})();'
  132. }
  133. tbl = null
  134. regions = {}
  135. // WHAT A FUNCTION VALUE WRITES, by the site it was made at (tickets #98, #99): the
  136. // module announces a function's return with its site, and the browser repaints this
  137. // set — the members it writes, closed over the methods it calls and the events it
  138. // emits, the same closure a handler's set gets. A function that writes nothing has
  139. // no entry.
  140. fnMap(fns) {
  141. let bySite = {}
  142. if (fns != null) {
  143. for (f of fns) {
  144. let names = closeWrites(&tbl, f.writes, f.reads)
  145. if (names.length > 0) { bySite[f.site] = names }
  146. }
  147. }
  148. return mapOf(bySite)
  149. }
  150. // what a DECLARED handler writes, by event: one entry per event name, unioned
  151. eventMap() {
  152. let byEvent = {}
  153. for (h of tbl.handlers) {
  154. if (!h.nested) {
  155. let have = byEvent[h.event] == null ? [] : byEvent[h.event]
  156. for (w of closeWrites(&tbl, h.writes, h.reads)) { if (!have.includes(w)) { have.push(w) } }
  157. byEvent[h.event] = have
  158. }
  159. }
  160. return mapOf(byEvent)
  161. }
  162. // A MEMBER HOLDING A ROUTINE: what calling it writes is what its initializer writes
  163. memberMap() {
  164. let byName = {}
  165. for (m of tbl.members) {
  166. if (m.nodes != null) { byName[m.name] = memberNames(m.nodes.writes) }
  167. }
  168. return mapOf(byName)
  169. }
  170. // EVERY INITIALIZER, IN DECLARATION ORDER, with the members it reads — the whole of
  171. // what a derivation needs; the class carries the computation itself as `__derive__`.
  172. // `srv` (written only when true, same rule as `isStatic` on a read — every byte here
  173. // ships to the browser) marks a SERVER REFERENCE SITE (COMPONENTS.md §9.1/§9.3): an
  174. // initializer that calls an imported class (`import Store from './store.hl'`, then
  175. // `Store.chatsOf(…)` — `hlDenotes`'s own `kind: 'class'`, not a second resolution).
  176. // It is evaluated per request, on the SERVER, and its value ships into the page as
  177. // state; `client.hl`'s `moved()` must never call it locally — routger's `conversations
  178. // = … ? store.chatsOf(session.user.id) : []` reached `hl:time`, which ships no client
  179. // half, the moment a session sync (0b17f65b) made `session` move in the browser and
  180. // `moved()` re-ran every derivation that reads it. A fresh value for a server
  181. // reference site ships in `sync` instead (WebFramework.hl `serverSyncNames`).
  182. derivList() {
  183. let src = '['
  184. let first = true
  185. for (d of tbl.derivations) {
  186. if (!first) { src = src + ',' }
  187. first = false
  188. src = src + '{name:' + str(d.name) + ',site:' + str(d.site) + ',reads:' + list(memberNames(d.reads))
  189. if (d.serverSite) { src = src + ',srv:true' }
  190. src = src + '}'
  191. }
  192. return src + ']'
  193. }
  194. // does this read list (RAW — hlDenotes's own kind per name, before `memberNames`
  195. // narrows it to members) reach an imported class or a braced static of one — the two
  196. // forms COMPONENTS.md §9.1/§9.3 calls "a call on an imported class" (`import Store
  197. // from './store.hl'`, `Store.noteRows()`, OR the form the maintained demos actually
  198. // use, `import { postRow } from './store.hl'`, `postRow(id)`)? See `derivList` above.
  199. serverSite(reads) {
  200. if (reads == null) { return false }
  201. for (r of reads) {
  202. if (r.kind == 'class' || r.kind == 'import') { return true }
  203. // AN `hl:` PLUGIN WITH NO BROWSER SIDE (hl:proc, hl:fs) is a server reference too (D2): the compiler says so in the read
  204. // itself (browser == false), so a member that reads one has its value shipped in the answer.
  205. if (r.kind == 'plugin' && r.browser == false) { return true }
  206. }
  207. return false
  208. }
  209. mapOf(byName) {
  210. let src = '{'
  211. let first = true
  212. for (k of byName.keys()) {
  213. if (!first) { src = src + ',' }
  214. first = false
  215. src = src + str(k) + ':' + list(byName[k])
  216. }
  217. return src + '}'
  218. }
  219. // the write set and the read set of the handler at this site, closed over the methods
  220. // it calls and the handlers it emits to — computed here, written into the listener
  221. handlerSets(site) {
  222. for (h of tbl.handlers) {
  223. if (h.nested && h.site == site) {
  224. return list(closeWrites(&tbl, h.writes, h.reads)) + ',' + list(closeReads(&tbl, h.reads))
  225. }
  226. }
  227. return '[],[]'
  228. }
  229. // the named regions an `if`'s branch fills (`if (x) { side { … } }`), as a JS list, `null` for none: the
  230. // browser places those regions again when the condition changes
  231. fillNamesIn(nodes) {
  232. let out = ''
  233. for (c of nodes) {
  234. if (c.k == 'if' && c.regionfill != null) { out = out + (out == '' ? '' : ',') + str(c.regionfill) }
  235. }
  236. return out == '' ? 'null' : '[' + out + ']'
  237. }
  238. regionNamesAt(site) {
  239. let names = regions[site]
  240. return list(names == null ? [] : names)
  241. }
  242. fns = []
  243. // ---- the walk, unrolled ------------------------------------------------------------
  244. // `top` marks the View's own root list: an element there is a COMPOSITION ROOT — a
  245. // reference above may have overwritten some of its events and put classes on it, which
  246. // is what `opt` carries (the old hl:web's rootOverwrites and withRootClasses).
  247. walk(key, nodes) {
  248. return plain(key, nodes)
  249. }
  250. // a list's walk
  251. plain(key, nodes) {
  252. let body = ''
  253. for (n of nodes) { body = body + node(key, n, false, false) }
  254. return 'async function(c,m,o,host,i,rows,rk,fill,cr,opt){let inst=m.instance;let r=rows;' + body + 'return i;}'
  255. }
  256. // the View's own root list, whose elements are composition roots
  257. walkTop(key, nodes) {
  258. let body = ''
  259. for (n of nodes) { body = body + node(key, n, true, false) }
  260. return 'async function(c,m,o,host,i,rows,rk,fill,cr,opt){let inst=m.instance;let r=rows;' + body + 'return i;}'
  261. }
  262. // a sub-walk, registered under its own site so the runtime can reach it by name
  263. // A SUB-WALK IS WRITTEN ONCE, however often the compile reaches its list.
  264. sub(key, nodes, slot, site) {
  265. if (done[slot + ' ' + site] != null) { return null }
  266. done[slot + ' ' + site] = true
  267. fns.push({ slot = slot; sites = [site]; src = walk(key, nodes); })
  268. return null
  269. }
  270. done = {}
  271. node(key, n, top, filterOns) {
  272. if (n.k == 'el') { return elNode(key, n, top) }
  273. if (n.k == 'text') { return 'if(cr){host.appendChild(document.createTextNode(' + str(n.text) + '));}else{await c.leafAt(host,false,i,null,' + str(n.text) + ');}' }
  274. if (n.k == 'on') {
  275. // A REFERENCE ABOVE MAY HAVE OVERWRITTEN THIS EVENT on the composition root:
  276. // the root's own handler for it is dropped and the reference's stands instead
  277. // (the old hl:web's withoutOns, which applies to the root element's direct children).
  278. let guard = filterOns ? 'if(!(opt&&opt.over&&opt.over.indexOf(' + str(n.event) + ')>=0)){' : '{'
  279. return guard + 'await c.onAt(m,host,' + str(n.event) + ',' + str(n.site) + ',rows,' + handlerSets(n.site) + ');}'
  280. }
  281. if (n.k == 'member' && n.name == 'slot') {
  282. // the SHELL's slot is where the route component hangs; a child's slot is the
  283. // fragment its host wrote, and the host's frame builds it
  284. return 'if(m.isShell){i=await c.slotShell(m,host,i,cr);}else if(fill!=null){i=await c.slotFill(m,o,host,i,fill,cr);}'
  285. }
  286. // A TEXT LEAF STANDING IN A LIST — an `if` branch, a `for` body — beside elements or
  287. // alone: its own text node, and a site of the member it reads (or of the row it
  288. // stands in), painted by rewriting that node.
  289. if (n.k == 'member' || n.k == 'field' || n.k == 'expr') {
  290. let names = readNames(n)
  291. return 'await c.textAt(m,host,cr,async function(inst,r){return ' + textRead(n) + ';},' + list(names) + ',rows,rk);'
  292. }
  293. if (n.k == 'for') {
  294. // THE ROW VARIABLE IS IN SCOPE INSIDE THE BODY, and a read of it is the ROW's,
  295. // not a member of the instance — the language resolves it that way and the site
  296. // sets must say the same, or a member of the same name would paint the row.
  297. rowVars.push(n.row)
  298. sub(key, n.body, 'B', n.site)
  299. rowVars.pop()
  300. return 'i=await c.forAt(m,o,host,i,' + str(n.site) + ',' + str(n.row) + ',function(inst,r){return ' + rawRead(n.list) + ';},rows,rk,fill,cr,' + regionNamesAt(n.site) + ');'
  301. }
  302. if (n.k == 'if' && n.regionfill != null) { return '' }
  303. if (n.k == 'if' && n.region != null) {
  304. // A NAMED REGION: the default is this component's own sub-walk; what stands between the
  305. // region's marks is the default, or the fill a lower level wrote (client.hl `regionAt`)
  306. sub(key, n.then, 'D', n.site)
  307. return 'i=await c.namedRegionAt(m,o,host,i,' + str(n.site) + ',' + str(n.region) + ',rows,rk,fill,cr);'
  308. }
  309. if (n.k == 'if') {
  310. sub(key, n.then, 'T', n.site)
  311. sub(key, n.other, 'E', n.site)
  312. return 'i=await c.ifAt(m,o,host,i,' + str(n.site) + ',function(inst,r){return ' + rawRead(n.cond) + ';},rows,rk,fill,cr,' + regionNamesAt(n.site) + ',' + fillNamesIn(n.then) + ');'
  313. }
  314. if (n.k == 'component') {
  315. if (n.fill != null && n.fill.length > 0) { sub(key, n.fill, 'F', n.site) }
  316. return 'i=await c.kidAt(m,host,i,' + kidSpec(n) + ',rows,rk,fill,cr,opt);'
  317. }
  318. return ''
  319. }
  320. // ---- an element --------------------------------------------------------------------
  321. elNode(key, n, top) {
  322. let src = '{let el=A(cr,host,i,' + str(n.tag) + ');if(!cr){i=i+1;}'
  323. src = src + 'if(el==null){await c.acqFail(' + str(n.tag) + ',host);}else{'
  324. // the attributes, on the walk that BUILDS the element (a claim takes the server's)
  325. src = src + 'if(cr){' + createAttrs(n) + (top ? 'await c.rootClasses(el,opt);' : '') + 'host.appendChild(el);}'
  326. src = src + 'el.__hlRows=rows;el.__hlSite=' + str(n.site) + ';el.__hlKey=' + str(key) + ';'
  327. // the element's own named entries, for the literal a handler on it fires
  328. src = src + 'el.__hlLit=function(inst,r){return ' + litNamed(n) + ';};'
  329. let names = siteNames(n)
  330. let valueMember = 'null'
  331. for (a of n.attrs) { if (a.member != null && a.name == 'value') { valueMember = str(a.member) } }
  332. let sited = ''
  333. if (names.length > 0 || valueMember != 'null') {
  334. sited = 'await c.siteAt(m,el,' + list(names) + ',' + str(n.site) + ',' + valueMember + ',' + (n.tag == 'select' ? 'true' : 'false') + ',rows);'
  335. }
  336. let kids = ''
  337. // AN ELEMENT THAT HOLDS ONLY TEXT is painted whole (`textContent`, see the paints):
  338. // built, it gets that text as one node; claimed, the server's is taken as it stands
  339. let whole = textOf(n)
  340. if (whole != null) {
  341. kids = 'if(cr){host.appendChild(document.createTextNode(' + whole + '));}'
  342. for (c of n.children) { if (c.k == 'on') { kids = kids + node(key, c, false, top) } }
  343. }
  344. else if (mixed(n)) { kids = mixedKids(key, n, top) }
  345. else { for (c of n.children) { kids = kids + node(key, c, false, top) } }
  346. let inner = '{let host=el;let i=0;' + kids + '}'
  347. // A SELECT IS SITED AFTER ITS OPTIONS: the member decides which option stands, and
  348. // the read-back that `siteAt` does reports nothing before they exist.
  349. if (n.tag == 'select') { src = src + inner + (selectValue(n) == 'null' ? '' : 'if(cr){await c.applySelectAt(m,el,' + selectValue(n) + ');}') + sited }
  350. else { src = src + sited + inner }
  351. return src + '}}'
  352. }
  353. // A TEXT LEAF BESIDE AN ELEMENT OR A REGION (ticket #86 follow-up): `td { d.n if (…) {…} }`
  354. // cannot be painted through `textContent`, which would wipe the branch. Each RUN of
  355. // adjacent leaves is one text node, held on the element (`__hlLeaves`, by the run's
  356. // index) where the walk builds or claims it, and the paints rewrite that node alone.
  357. mixed(n) {
  358. if (textOf(n) != null) { return false }
  359. for (c of n.children) { if (isReadNode(c)) { return true } }
  360. return false
  361. }
  362. isLeaf(c) { return c.k == 'text' || isReadNode(c) }
  363. // a node that shows a value read at render time: a member, a field path, or an expression
  364. isReadNode(c) { return (c.k == 'member' || c.k == 'field' || c.k == 'expr') && c.name != 'slot' }
  365. // the members whose move repaints a read (`Style` and a `for` row variable are not members
  366. // of the instance), and the guard a paint writes for them
  367. readNames(c) {
  368. let out = []
  369. for (nm of (c.k == 'expr' ? c.names : [c.name])) {
  370. if (nm != 'Style' && !rowVars.includes(nm) && !out.includes(nm)) { out.push(nm) }
  371. }
  372. return out
  373. }
  374. guardOf(c) {
  375. let g = ''
  376. for (nm of readNames(c)) { g = g + (g == '' ? '' : '||') + 'n===' + str(nm) }
  377. return g
  378. }
  379. // the runs of adjacent leaves, in order; an `on` renders nothing and splits none
  380. runsOf(n) {
  381. let out = []
  382. let run = []
  383. for (c of n.children) {
  384. if (isLeaf(c)) { run.push(c) }
  385. else if (c.k != 'on') {
  386. if (run.length > 0) { out.push(run) }
  387. run = []
  388. }
  389. }
  390. if (run.length > 0) { out.push(run) }
  391. return out
  392. }
  393. runText(run) {
  394. let src = '""'
  395. for (c of run) { src = src + '+(' + (c.k == 'text' ? str(c.text) : textRead(c)) + ')' }
  396. return src
  397. }
  398. runFeeds(run) {
  399. for (c of run) { if (c.k != 'text') { return true } }
  400. return false
  401. }
  402. mixedKids(key, n, top) {
  403. let kids = ''
  404. let run = []
  405. let k = 0
  406. for (c of n.children) {
  407. if (isLeaf(c)) { run.push(c) }
  408. else {
  409. if (c.k != 'on' && run.length > 0) { kids = kids + leafRun(run, k) k = k + 1 run = [] }
  410. kids = kids + node(key, c, false, top)
  411. }
  412. }
  413. if (run.length > 0) { kids = kids + leafRun(run, k) }
  414. return kids
  415. }
  416. // EVERY CALL INTO THE CLIENT IS AWAITED: its methods are async in the browser, and one
  417. // not awaited runs its DOM work after the walk has gone on — a built leaf then lands
  418. // after the elements that follow it (ticket #104).
  419. leafRun(run, k) {
  420. if (!runFeeds(run)) { return 'if(cr){host.appendChild(document.createTextNode(' + runText(run) + '));}else{await c.leafAt(host,false,i,null,' + runText(run) + ');}' }
  421. return 'await c.leafAt(host,cr,i,' + k + ',' + runText(run) + ');'
  422. }
  423. // the attributes an element is BUILT with — a bound one written in, a literal one as it
  424. // stands. `value` is a property once the page is live and an attribute as the control's
  425. // default; a textarea has no value attribute at all and a select's value is an option.
  426. createAttrs(n) {
  427. let src = ''
  428. for (a of n.attrs) {
  429. if (isBool(a)) {
  430. src = src + '{' + boolWrite(a, rawRead(a.member != null ? { k = 'member'; name = a.member; } : a.ref)) + '}'
  431. } else if (nullable(a)) {
  432. src = src + '{let v=' + rawRead(a.member != null ? { k = 'member'; name = a.member; } : a.ref) + ';if(v!=null){el.setAttribute(' + str(a.name) + ',""+v);}}'
  433. } else {
  434. let rd = a.member != null ? readOf({ k = 'member'; name = a.member; }) : (a.ref != null ? readOf(a.ref) : str(a.text))
  435. src = src + '{let v=' + rd + ';'
  436. if (a.extra != null) { src = src + 'v=v+" "+' + str(a.extra) + ';' }
  437. if (n.tag == 'select' && a.name == 'value') { src = src + '}' }
  438. else if (a.name == 'value') {
  439. src = src + 'if(el.value!=null){el.value=v;' + (n.tag == 'textarea' ? '' : 'el.setAttribute("value",v);') + '}else{el.setAttribute("value",v);}}'
  440. } else { src = src + 'el.setAttribute(' + str(a.name) + ',v);}' }
  441. }
  442. }
  443. if (boundValue(n)) { src = src + 'el.setAttribute("autocomplete","off");' }
  444. return src
  445. }
  446. // a bound attribute whose null read is NO attribute (#177): not `value` (a control's
  447. // value has its own rules), and not one carrying extra classes
  448. nullable(a) {
  449. return (a.member != null || a.ref != null) && a.name != 'value' && a.extra == null && !v.isBoolAttr(a.name)
  450. }
  451. // a BOUND boolean attribute — a literal one was decided at build (WebFramework `element`)
  452. isBool(a) {
  453. return (a.member != null || a.ref != null) && v.isBoolAttr(a.name)
  454. }
  455. // A BOUND BOOLEAN ATTRIBUTE, WRITTEN (ticket #33): set when the value is on, REMOVED when
  456. // it is off — the markup's rule (view.boolAttrs) — and the DOM property with it, because
  457. // once the page is live `checked` is the property and the attribute only its default.
  458. boolWrite(a, raw) {
  459. let prop = a.name == 'readonly' ? 'readOnly' : a.name
  460. return 'let on=!!' + raw + ';if(on){el.setAttribute(' + str(a.name) + ',"");}else if(el.removeAttribute){el.removeAttribute(' + str(a.name) + ');}if(' + str(prop) + ' in el&&el[' + str(prop) + ']!==on){el[' + str(prop) + ']=on;}'
  461. }
  462. // view.hl's `boundValue`, to the letter: a FORM CONTROL whose `value` reads a member or
  463. // a field, and not one the author gave an `autocomplete` of its own
  464. boundValue(n) {
  465. if (n.tag != 'input' && n.tag != 'textarea' && n.tag != 'select') { return false }
  466. let bound = false
  467. for (a of n.attrs) {
  468. if (a.name == 'autocomplete') { return false }
  469. if (a.name == 'value' && (a.member != null || a.ref != null)) { bound = true }
  470. }
  471. return bound
  472. }
  473. selectValue(n) {
  474. for (a of n.attrs) {
  475. if (a.name == 'value') {
  476. if (a.member != null) { return readOf({ k = 'member'; name = a.member; }) }
  477. if (a.ref != null) { return readOf(a.ref) }
  478. return str(a.text)
  479. }
  480. }
  481. return 'null'
  482. }
  483. // the element's own entries as the literal sees them — its attributes by name, then the
  484. // `for` row variables in scope (the row rides on the value)
  485. litNamed(n) {
  486. let src = '{'
  487. let first = true
  488. for (a of n.attrs) {
  489. if (!first) { src = src + ',' }
  490. first = false
  491. let v = a.member != null ? rawRead({ k = 'member'; name = a.member; }) : (a.ref != null ? rawRead(a.ref) : str(a.text))
  492. src = src + str(a.name) + ':' + v
  493. }
  494. return src + '}'
  495. }
  496. // the reference's own description, written at its use site: what it binds, what it
  497. // overwrites, the classes the host's `#Child` rule puts on it
  498. kidSpec(n) {
  499. let src = '{key:' + str(n.key) + ',kid:' + str(kidKeyOf(n)) + ',site:' + str(n.site) + ',fill:' + ((n.fill != null && n.fill.length > 0) ? 'true' : 'false')
  500. src = src + ',bindings:['
  501. let first = true
  502. for (b of n.bindings) {
  503. if (!first) { src = src + ',' }
  504. first = false
  505. src = src + '{name:' + str(b.name) + ',member:' + (b.member == null ? 'null' : str(b.member))
  506. src = src + ',ref:' + (b.ref == null ? 'null' : 'function(inst,r){return ' + rawRead(b.ref) + ';}')
  507. src = src + ',refName:' + (b.ref == null ? 'null' : str(b.ref.name))
  508. src = src + ',text:' + (b.text == null ? 'null' : 'true') + ',value:' + (b.text == null ? 'null' : refLiteral(b)) + '}'
  509. }
  510. src = src + '],ons:['
  511. first = true
  512. for (o of n.ons) {
  513. if (!first) { src = src + ',' }
  514. first = false
  515. src = src + '{event:' + str(o.event) + ',site:' + str(o.site) + ',sets:[' + handlerSets(o.site) + ']}'
  516. }
  517. src = src + '],classes:' + list(n.classes == null ? [] : n.classes) + '}'
  518. return src
  519. }
  520. // view.hl's kidKey: the reference's path, joined — the key the server mounted it under
  521. kidKeyOf(n) {
  522. let out = ''
  523. for (p of n.path) { out = out + '/' + p }
  524. return out
  525. }
  526. // the value a literal binding carries — view.refValue, folded at compile time
  527. refLiteral(b) {
  528. if (b.kind == 'folded') { return jsValue(b.value) }
  529. if (b.kind == 'number') { return '' + b.text }
  530. if (b.kind == 'boolean') { return b.text == 'true' ? 'true' : 'false' }
  531. if (b.kind == 'null_literal') { return 'null' }
  532. return str(b.text)
  533. }
  534. jsValue(v) {
  535. if (v == null) { return 'null' }
  536. if (hlTypeName(v) == 'String') { return str(v) }
  537. if (hlTypeName(v) == 'Boolean') { return v ? 'true' : 'false' }
  538. if (hlTypeName(v) == 'Number') { return '' + v }
  539. return JSON.stringify(v)
  540. }
  541. // ---- the site set: what this element SHOWS ------------------------------------------
  542. // The old hl:web asked the module's table which members the element at this site reads. The
  543. // answer is a fact about the View, so it is written in: the members its bound
  544. // attributes and its text leaves name. `Style` is folded into a class name at build and
  545. // the shell's `slot` is swapped by navigation — neither is ever painted, and neither is
  546. // a member read that reaches here (a `for` row variable resolves to its row).
  547. siteNames(n) {
  548. let out = []
  549. for (a of n.attrs) {
  550. if (a.member != null && a.member != 'Style' && !rowVars.includes(a.member) && !out.includes(a.member)) { out.push(a.member) }
  551. // A FIELD OF A MEMBER (`value = f.title`, ticket #93) is a read of the member:
  552. // a write to `f.title`, or a new `f`, moves `f`, and this element repaints
  553. if (a.ref != null && a.ref.name != 'Style' && !rowVars.includes(a.ref.name) && !out.includes(a.ref.name)) { out.push(a.ref.name) }
  554. }
  555. for (c of n.children) {
  556. if (isReadNode(c)) {
  557. for (nm of readNames(c)) { if (!out.includes(nm)) { out.push(nm) } }
  558. }
  559. }
  560. return out
  561. }
  562. rowVars = []
  563. // ---- the paints ---------------------------------------------------------------------
  564. collectPaints(&out, nodes) {
  565. for (n of nodes) {
  566. if (n.k == 'el') {
  567. let paint = paintFn(n)
  568. let row = rowFn(n)
  569. if (paint != null || row != 'null') {
  570. out.push({ site = n.site; paint = paint == null ? 'null' : paint; row = row; })
  571. }
  572. collectPaints(&out, n.children)
  573. } else if (n.k == 'for') { collectPaints(&out, n.body) }
  574. else if (n.k == 'if') { collectPaints(&out, n.then) collectPaints(&out, n.other) }
  575. else if (n.k == 'component') { if (n.fill != null) { collectPaints(&out, n.fill) } }
  576. }
  577. return null
  578. }
  579. // `paint(&m, s, name)`, compiled: the bound attributes under the member each reads, and
  580. // the text where the member that moved stands among the leaves.
  581. paintFn(n) {
  582. let body = ''
  583. for (a of n.attrs) {
  584. if (a.member != null) {
  585. body = body + 'if(n===' + str(a.member) + '){' + attrWrite(n, a, readOf({ k = 'member'; name = a.member; })) + '}'
  586. } else if (a.ref != null) {
  587. // a field of a member paints when the MEMBER moves (ticket #93)
  588. body = body + 'if(n===' + str(a.ref.name) + '){' + attrWrite(n, a, readOf(a.ref)) + '}'
  589. }
  590. }
  591. let text = textOf(n)
  592. if (text != null) {
  593. let guard = ''
  594. for (c of n.children) {
  595. if (isReadNode(c)) {
  596. let g = guardOf(c)
  597. if (g != '') {
  598. if (guard != '') { guard = guard + '||' }
  599. guard = guard + g
  600. }
  601. }
  602. }
  603. if (guard != '') { body = body + 'if(' + guard + '){el.textContent=' + text + ';}' }
  604. } else if (mixed(n)) {
  605. let k = 0
  606. for (run of runsOf(n)) {
  607. let guard = ''
  608. for (c of run) {
  609. if (c.k != 'text') {
  610. let g = guardOf(c)
  611. if (g != '') {
  612. if (guard != '') { guard = guard + '||' }
  613. guard = guard + g
  614. }
  615. }
  616. }
  617. if (guard != '') { body = body + 'if((' + guard + ')&&el.__hlLeaves&&el.__hlLeaves[' + k + ']){el.__hlLeaves[' + k + '].data=' + runText(run) + ';}' }
  618. k = k + 1
  619. }
  620. }
  621. if (body == '') { return null }
  622. return 'async function(el,inst,r,n){' + body + '}'
  623. }
  624. // `rowPaint(&m, el)`, compiled: every bound spot, written only where the DOM does not
  625. // already hold it — which is why a push into 2000 rows costs the 1999 nothing.
  626. rowFn(n) {
  627. let body = ''
  628. for (a of n.attrs) {
  629. if (isBool(a)) {
  630. body = body + '{' + boolWrite(a, rawRead(a.member != null ? { k = 'member'; name = a.member; } : a.ref)) + '}'
  631. } else if (nullable(a)) {
  632. body = body + '{let v=' + rawRead(a.member != null ? { k = 'member'; name = a.member; } : a.ref) + ';if(v==null){if(el.hasAttribute(' + str(a.name) + ')){el.removeAttribute(' + str(a.name) + ');}}else{v=""+v;if(el.getAttribute(' + str(a.name) + ')!==v){el.setAttribute(' + str(a.name) + ',v);}}}'
  633. } else if (a.member != null || a.ref != null) {
  634. let one = 'let v=' + readOf(a.member != null ? { k = 'member'; name = a.member; } : a.ref) + ';'
  635. if (a.extra != null) { one = one + 'v=v+" "+' + str(a.extra) + ';' }
  636. if (n.tag == 'select' && a.name == 'value') { one = one + 'if(el.value!==v){el.value=v;}' }
  637. else {
  638. one = one + 'if(el.getAttribute(' + str(a.name) + ')!==v){el.setAttribute(' + str(a.name) + ',v);}'
  639. if (a.name == 'value') { one = one + 'if(el.value!=null&&el.value!==v){el.value=v;}' }
  640. }
  641. body = body + '{' + one + '}'
  642. }
  643. }
  644. let text = textOf(n)
  645. let feeds = false
  646. for (c of n.children) { if (isReadNode(c)) { feeds = true } }
  647. if (text != null && feeds) { body = body + '{let t=' + text + ';if(el.textContent!==t){el.textContent=t;}}' }
  648. if (text == null && feeds) {
  649. let k = 0
  650. for (run of runsOf(n)) {
  651. if (runFeeds(run)) { body = body + '{let x=el.__hlLeaves&&el.__hlLeaves[' + k + '];let t=' + runText(run) + ';if(x&&x.data!==t){x.data=t;}}' }
  652. k = k + 1
  653. }
  654. }
  655. if (body == '') { return 'null' }
  656. return 'async function(el,inst,r){' + body + '}'
  657. }
  658. attrWrite(n, a, v) {
  659. if (isBool(a)) { return boolWrite(a, rawRead(a.member != null ? { k = 'member'; name = a.member; } : a.ref)) }
  660. if (nullable(a)) { return 'let v=' + rawRead(a.member != null ? { k = 'member'; name = a.member; } : a.ref) + ';if(v==null){el.removeAttribute(' + str(a.name) + ');}else{el.setAttribute(' + str(a.name) + ',""+v);}' }
  661. let src = 'let v=' + v + ';'
  662. if (a.extra != null) { src = src + 'v=v+" "+' + str(a.extra) + ';' }
  663. if (n.tag == 'select' && a.name == 'value') { return src + 'el.value=v;' }
  664. src = src + 'el.setAttribute(' + str(a.name) + ',v);'
  665. if (a.name == 'value') { src = src + 'if(el.value!=null){el.value=v;}' }
  666. return src
  667. }
  668. textOf(n) {
  669. let parts = []
  670. let content = 0
  671. let feeds = false
  672. for (c of n.children) {
  673. if (c.k != 'on') {
  674. content = content + 1
  675. if (c.k == 'text') { parts.push(str(c.text)) }
  676. else if (isReadNode(c)) { parts.push(textRead(c)) feeds = true }
  677. }
  678. }
  679. if (!feeds) { return null }
  680. if (parts.length != content) { return null }
  681. let src = '""'
  682. for (p of parts) { src = src + '+(' + p + ')' }
  683. return src
  684. }
  685. // ---- a read, written in ---------------------------------------------------------------
  686. // `view.value`'s own rule as an expression: a `for` row variable in scope wins over a
  687. // member of the same name, and a field path walks into it. `readOf` answers the STRING
  688. // the DOM takes; `rawRead` the value itself (a binding, a list, a condition).
  689. readOf(node) { return '(""+' + rawRead(node) + ')' }
  690. // A READ SHOWN AS TEXT: null is no text, as the server writes it (view.hl `escape`)
  691. textRead(node) { return 'S(' + rawRead(node) + ')' }
  692. rawRead(node) {
  693. if (node == null) { return 'null' }
  694. if (node.k == 'text') { return node.kind == 'folded' && hlTypeName(node.value) == 'Hybrid' ? JSON.stringify(node.value) : str(node.text) }
  695. if (node.k == 'expr') { return '(await X(' + JSON.stringify(node.x) + ',r,inst))' }
  696. if (node.k != 'field') { return 'RV(r,inst,' + str(node.name) + ')' }
  697. return 'RF(r,inst,' + str(node.name) + ',' + list(node.path) + ')'
  698. }
  699. list(names) {
  700. let src = '['
  701. let first = true
  702. for (n of names) {
  703. if (!first) { src = src + ',' }
  704. first = false
  705. src = src + str(n)
  706. }
  707. return src + ']'
  708. }
  709. // a JavaScript string literal — JSON's escaping is a subset of JavaScript's, so a
  710. // source text's quotes, newlines and backslashes survive into the statement unchanged
  711. str(s) {
  712. if (s == null) { return '""' }
  713. return JSON.stringify('' + s)
  714. }
  715. // ---- WHAT THE LANGUAGE SAYS ABOUT THIS FILE, read HERE and written into the code ----
  716. // The old hl:web shipped the language's answers to the browser as data: `hlJs` writes each file's
  717. // members, handlers and methods — every node of every initializer, with the names it
  718. // reads — into the module, and the client reduces that table on every boot. The answers
  719. // are facts about the source, so this file asks for them once, at compile time, and
  720. // writes the RESULT into the statements: a handler's write set stands at the listener
  721. // that needs it, a region's names at the region, the derivations as a list of what to
  722. // re-run. The module carries no table at all (WebFramework `module` drops the one the
  723. // emitter writes).
  724. //
  725. // The reflection is the framework's own analysis generation — the same `gen` the View
  726. // trees come from. Nothing starts a second one: a handle is only valid inside the
  727. // generation that made it.
  728. tableOf(gen, key) {
  729. let members = []
  730. let derivations = []
  731. let handlers = []
  732. let methods = []
  733. accesses = {}
  734. for (m of hlMembers(gen, key)) {
  735. let entry = { name = m.name; nodes = null; }
  736. if (m.hasValue) {
  737. let n = syntaxTree(gen, key, m.node)
  738. entry.nodes = n
  739. accesses[m.name] = accessedMembers(n)
  740. derivations.push({ name = m.name; site = n.site; reads = n.reads; serverSite = serverSite(browserReads(gen, n.reads)); })
  741. nestedHandlers(&handlers, n)
  742. }
  743. members.push(entry)
  744. }
  745. for (h of hlEvents(gen, key).handles) {
  746. let n = syntaxTree(gen, key, h.node)
  747. handlers.push({ event = h.event; site = n.site; reads = n.reads; writes = n.writes; nested = false; })
  748. }
  749. for (mm of hlMethods(gen, key)) {
  750. if (!mm.inlineHandler) {
  751. let n = syntaxTree(gen, key, mm.node)
  752. methods.push({ name = mm.name; reads = n.reads; writes = n.writes; })
  753. }
  754. }
  755. // A SERVER REFERENCE SITE IS ONE THROUGH A LOCAL HELPER TOO (#121): an initializer
  756. // that calls a method or static of this class which calls the import still reaches
  757. // the server only, so it is never re-derived in the browser either.
  758. let post = []
  759. for (d of derivations) {
  760. post.push({ name = d.name; site = d.site; reads = d.reads; serverSite = d.serverSite || reachesServerSite(gen, &members, &methods, d.reads); })
  761. }
  762. // ACCESSING A SERVER SITE'S OBJECT STAYS ON THE SERVER (W207/W224): `st = new Store()`
  763. // makes `nav = st.navSections(page)` a server site too. Merely reading its shipped VALUE
  764. // (`shown = 'srv:' + srv`, or passing `srv` to a browser function) remains a browser derivation.
  765. let grow = true
  766. while (grow) {
  767. grow = false
  768. for (d of post) {
  769. if (d.serverSite) { continue }
  770. for (r of accesses[d.name]) {
  771. for (o of post) { if (o.serverSite && o.name == r && o.name != d.name) { d.serverSite = true grow = true } }
  772. }
  773. }
  774. }
  775. return { members = members; derivations = post; handlers = handlers; methods = methods; }
  776. }
  777. // Read entries resolve names, but do not say whether a name is a receiver or an argument.
  778. // Ask the syntax only for calls and member access; nested handlers run independently.
  779. accessedMembers(n) {
  780. out = []
  781. if (n == null || n.tag == 'on_statement') { return out }
  782. for (r of receiverMembers(n.tag == 'member_expression' ? n.object : (n.tag == 'call_expression' ? n.callee : null))) {
  783. if (!out.includes(r)) { out.push(r) }
  784. }
  785. for (c of [n.value, n.list, n.body, n.condition, n.consequent, n.alternate, n.object, n.property, n.callee, n.left, n.right, n.operand]) {
  786. for (r of accessedMembers(c)) { if (!out.includes(r)) { out.push(r) } }
  787. }
  788. for (l of [n.entries, n.args, n.elements]) {
  789. if (l != null) { for (c of l) { for (r of accessedMembers(c)) { if (!out.includes(r)) { out.push(r) } } } }
  790. }
  791. return out
  792. }
  793. // Only the receiver itself counts: `browserFn(srv).field` does not access `srv`.
  794. // `this.srv` names the same receiver as bare `srv`; reading `this.srv` alone is a value read.
  795. receiverMembers(n) {
  796. if (n == null) { return [] }
  797. if (n.tag == 'identifier') { return memberNames(nodeReads(n)) }
  798. if (n.tag == 'member_expression' && n.object != null && n.object.tag == 'identifier' && n.object.name == 'this' && n.computed != true) {
  799. return memberNames(nodeReads(n.property))
  800. }
  801. return []
  802. }
  803. // AN IMPORTED STATIC FUNCTION THAT TOUCHES NOTHING BUT ITS OWN PARAMETERS IS NOT A SERVER
  804. // REFERENCE (#168): `imported = twice(n)` with `import { twice } from './lib.hl'` ships
  805. // with the page module (a const beside the class) and computes the same in the browser,
  806. // so the member is re-derived there when `n` moves. A braced import whose body reads a
  807. // class, another import, a static or a member stays a server site: the browser's copy
  808. // of that state is not the server's.
  809. browserReads(gen, reads) {
  810. let out = []
  811. if (reads == null) { return out }
  812. for (r of reads) {
  813. if (!(r.kind == 'import' && r.key != null && pureImport(gen, r))) { out.push(r) }
  814. }
  815. return out
  816. }
  817. pureImport(gen, r) {
  818. for (m of hlMembers(gen, r.key)) {
  819. if (m.name == r.name && m.isStatic == true && m.hasValue) {
  820. let n = syntaxTree(gen, r.key, m.node)
  821. if (n == null || n.reads == null) { return false }
  822. for (x of n.reads) {
  823. if (x.kind != 'local' && x.kind != 'none' && x.kind != 'row') { return false }
  824. }
  825. return true
  826. }
  827. }
  828. return false
  829. }
  830. // does a read list reach an imported class THROUGH this class's own methods and statics,
  831. // transitively — the walk of `closeReads`, over the helpers a derivation calls
  832. reachesServerSite(gen, &members, &methods, reads) {
  833. let queue = []
  834. if (reads != null) {
  835. for (r of reads) {
  836. if (r.kind == 'method' || (r.kind == 'member' && r.isStatic == true)) { queue.push(r.kind + ' ' + r.name) }
  837. }
  838. }
  839. let seen = {}
  840. while (queue.length > 0) {
  841. let at = queue.shift()
  842. if (seen[at] == null) {
  843. seen[at] = true
  844. let sp = at.indexOf(' ')
  845. let kind = at.slice(0, sp)
  846. let name = at.slice(sp + 1)
  847. let hop = []
  848. if (kind == 'method') { for (mm of methods) { if (mm.name == name) { hop = mm.reads } } }
  849. else { for (mb of members) { if (mb.name == name && mb.nodes != null) { hop = nodeReads(mb.nodes) } } }
  850. if (serverSite(browserReads(gen, hop))) { return true }
  851. for (r of hop) {
  852. if (r.kind == 'method' || (r.kind == 'member' && r.isStatic == true)) { queue.push(r.kind + ' ' + r.name) }
  853. }
  854. }
  855. }
  856. return false
  857. }
  858. // A NESTED `on` IS A DOM HANDLER, filed under its own site — the id the View node
  859. // carries. Its body is not the literal's syntax, so the node answers for it and the
  860. // walk stops there.
  861. nestedHandlers(&into, n) {
  862. if (n == null) { return null }
  863. if (n.tag == 'on_statement') {
  864. into.push({ event = n.event; site = n.site; reads = n.reads; writes = n.writes; nested = true; })
  865. return null
  866. }
  867. for (c of [n.value, n.list, n.body, n.condition, n.consequent, n.alternate, n.object, n.property, n.callee, n.left, n.right, n.operand]) {
  868. if (c != null) { nestedHandlers(&into, c) }
  869. }
  870. for (l of [n.entries, n.args, n.elements]) {
  871. if (l != null) { for (c of l) { nestedHandlers(&into, c) } }
  872. }
  873. return null
  874. }
  875. // the site with its file's base name: everything after the last '/'. The path part
  876. // is not "up to the first ':'" — a package key has a colon of its own
  877. // ('hl:markdown/Markdown.hl:24:3'), and that reading left a package component's
  878. // sites unshortened, so its regions found no reads and never repainted (#111)
  879. shortSite(site) {
  880. if (site == null) { return null }
  881. let cut = site.lastIndexOf('/')
  882. if (cut < 0) { return site }
  883. return site.slice(cut + 1)
  884. }
  885. // the syntax of one node, recursively — hl:core/AST's `tree`, read off the framework's
  886. // own generation instead of starting one of its own
  887. syntaxTree(gen, key, node) {
  888. let n = hlSyntax(gen, key, node)
  889. if (n == null) { return null }
  890. // A SITE IS NAMED BY THE FILE'S BASE NAME. The reflection answers with the project
  891. // key ('components/sort_list.hl:77:26'); the View tree, the emitter's own
  892. // `__derive__` branches and every site this framework writes use the base name
  893. // ('sort_list.hl:77:26'). One spelling, or a lookup by site finds nothing.
  894. let out = { tag = n.tag; site = shortSite(n.site); reads = n.reads; writes = n.writes; }
  895. if (n.tag == 'object_expression') {
  896. let kids = []
  897. for (h of n.entries) { kids.push(syntaxTree(gen, key, h)) }
  898. out.entries = kids
  899. } else if (n.tag == 'object_property') {
  900. out.key = n.key
  901. out.value = syntaxTree(gen, key, n.value)
  902. } else if (n.tag == 'view_for') {
  903. out.list = syntaxTree(gen, key, n.list)
  904. out.body = syntaxTree(gen, key, n.body)
  905. } else if (n.tag == 'view_if') {
  906. out.condition = syntaxTree(gen, key, n.condition)
  907. out.consequent = syntaxTree(gen, key, n.consequent)
  908. out.alternate = syntaxTree(gen, key, n.alternate)
  909. } else if (n.tag == 'on_statement') {
  910. out.event = n.event
  911. } else if (n.tag == 'member_expression') {
  912. out.object = syntaxTree(gen, key, n.object)
  913. out.property = syntaxTree(gen, key, n.property)
  914. out.computed = n.computed
  915. } else if (n.tag == 'identifier') {
  916. out.name = n.name
  917. } else if (n.tag == 'call_expression') {
  918. out.callee = syntaxTree(gen, key, n.callee)
  919. let args = []
  920. for (h of n.args) { args.push(syntaxTree(gen, key, h)) }
  921. out.args = args
  922. } else if (n.tag == 'binary_expression') {
  923. out.left = syntaxTree(gen, key, n.left)
  924. out.right = syntaxTree(gen, key, n.right)
  925. } else if (n.tag == 'unary_expression') {
  926. out.operand = syntaxTree(gen, key, n.operand)
  927. } else if (n.tag == 'conditional_expression') {
  928. out.condition = syntaxTree(gen, key, n.condition)
  929. out.consequent = syntaxTree(gen, key, n.consequent)
  930. out.alternate = syntaxTree(gen, key, n.alternate)
  931. } else if (n.tag == 'array_expression') {
  932. let els = []
  933. for (h of n.elements) { els.push(syntaxTree(gen, key, h)) }
  934. out.elements = els
  935. }
  936. return out
  937. }
  938. // ---- the reductions the browser used to make on every boot --------------------------
  939. // A `for`'s names are what its list reads, an `if`'s what its condition reads — filed
  940. // under the region's own site.
  941. regionIndex(t) {
  942. let idx = {}
  943. for (m of t.members) { if (m.nodes != null) { indexRegions(&idx, m.nodes) } }
  944. return idx
  945. }
  946. indexRegions(&idx, n) {
  947. if (n == null) { return null }
  948. if (n.tag == 'view_for') {
  949. idx[n.site] = memberNames(nodeReads(n.list))
  950. indexRegions(&idx, n.body)
  951. return null
  952. }
  953. if (n.tag == 'view_if') {
  954. idx[n.site] = memberNames(nodeReads(n.condition))
  955. indexRegions(&idx, n.consequent)
  956. indexRegions(&idx, n.alternate)
  957. return null
  958. }
  959. if (n.tag == 'on_statement') { return null }
  960. for (c of [n.value, n.list, n.body, n.condition, n.consequent, n.alternate, n.object, n.property, n.callee, n.left, n.right, n.operand]) {
  961. if (c != null) { indexRegions(&idx, c) }
  962. }
  963. for (l of [n.entries, n.args, n.elements]) {
  964. if (l != null) { for (c of l) { indexRegions(&idx, c) } }
  965. }
  966. return null
  967. }
  968. // the member reads of a read list — the language resolved every name, so this is a
  969. // filter and not a second resolution
  970. memberNames(reads) {
  971. let out = []
  972. if (reads == null) { return out }
  973. for (r of reads) {
  974. if (r.kind == 'member' && r.isStatic != true && !out.includes(r.name)) { out.push(r.name) }
  975. }
  976. return out
  977. }
  978. nodeReads(n) {
  979. if (n == null) { return [] }
  980. if (n.reads != null) { return n.reads }
  981. let out = []
  982. for (c of [n.value, n.list, n.body, n.condition, n.consequent, n.alternate, n.object, n.property, n.callee, n.left, n.right, n.operand]) {
  983. if (c != null) { for (r of nodeReads(c)) { out.push(r) } }
  984. }
  985. for (l of [n.entries, n.args, n.elements]) {
  986. if (l != null) { for (c of l) { for (r of nodeReads(c)) { out.push(r) } } }
  987. }
  988. return out
  989. }
  990. // A ROUTINE'S WRITES, PLUS THE WRITES OF EVERYTHING IT REACHES — the method it calls
  991. // and the handler it emits to, transitively. Both are READS the language resolved, so
  992. // this is a walk of its answer and never a guess.
  993. closeWrites(&t, writes, reads) {
  994. let out = memberNames(writes)
  995. let queue = []
  996. reached(&queue, reads)
  997. let seen = {}
  998. while (queue.length > 0) {
  999. let at = queue.shift()
  1000. if (seen[at] == null) {
  1001. seen[at] = true
  1002. let sp = at.indexOf(' ')
  1003. let kind = at.slice(0, sp)
  1004. let name = at.slice(sp + 1)
  1005. for (mm of t.methods) {
  1006. if (kind == 'method' && mm.name == name) {
  1007. for (w of memberNames(mm.writes)) { if (!out.includes(w)) { out.push(w) } }
  1008. reached(&queue, mm.reads)
  1009. }
  1010. }
  1011. for (h of t.handlers) {
  1012. if (kind == 'handler' && !h.nested && h.event == name) {
  1013. for (w of memberNames(h.writes)) { if (!out.includes(w)) { out.push(w) } }
  1014. reached(&queue, h.reads)
  1015. }
  1016. }
  1017. for (mb of t.members) {
  1018. if (kind == 'member' && mb.name == name && mb.nodes != null) {
  1019. for (w of memberNames(mb.nodes.writes)) { if (!out.includes(w)) { out.push(w) } }
  1020. reached(&queue, nodeReads(mb.nodes))
  1021. }
  1022. }
  1023. }
  1024. }
  1025. return out
  1026. }
  1027. // the mirror: a handler's reads plus the reads of everything it reaches — the
  1028. // candidates for a routine a host handed down, whose writes only the host's table names
  1029. closeReads(&t, reads) {
  1030. let out = memberNames(reads)
  1031. let queue = []
  1032. reached(&queue, reads)
  1033. let seen = {}
  1034. while (queue.length > 0) {
  1035. let at = queue.shift()
  1036. if (seen[at] == null) {
  1037. seen[at] = true
  1038. let sp = at.indexOf(' ')
  1039. let kind = at.slice(0, sp)
  1040. let name = at.slice(sp + 1)
  1041. for (mm of t.methods) {
  1042. if (kind == 'method' && mm.name == name) {
  1043. for (r of memberNames(mm.reads)) { if (!out.includes(r)) { out.push(r) } }
  1044. reached(&queue, mm.reads)
  1045. }
  1046. }
  1047. for (h of t.handlers) {
  1048. if (kind == 'handler' && !h.nested && h.event == name) {
  1049. for (r of memberNames(h.reads)) { if (!out.includes(r)) { out.push(r) } }
  1050. reached(&queue, h.reads)
  1051. }
  1052. }
  1053. for (mb of t.members) {
  1054. if (kind == 'member' && mb.name == name && mb.nodes != null) {
  1055. for (r of memberNames(nodeReads(mb.nodes))) { if (!out.includes(r)) { out.push(r) } }
  1056. reached(&queue, nodeReads(mb.nodes))
  1057. }
  1058. }
  1059. }
  1060. }
  1061. return out
  1062. }
  1063. reached(&queue, reads) {
  1064. if (reads == null) { return null }
  1065. for (r of reads) {
  1066. if (r.kind == 'method' || r.kind == 'handler' || (r.kind == 'member' && r.isStatic != true)) { queue.push(r.kind + ' ' + r.name) }
  1067. }
  1068. return null
  1069. }
  1070. // ---- THE SERVER RENDER, COMPILED --------------------------------------------------
  1071. // The old hl:web rendered a page by WALKING the View tree per request: for every node it asks
  1072. // what kind it is, for every element it builds the attribute string, and it decides the
  1073. // indentation of every line again, on every request, for every one of the 121 mounts a
  1074. // page may hold. Every one of those decisions is a fact about the View.
  1075. //
  1076. // So a component's render is compiled ONCE into a list of STEPS — constant text, and a
  1077. // read with its own closure — and a request is a concatenation. The structure (tags,
  1078. // static attributes, the readable form's line breaks, which element is a block) is
  1079. // decided at compile time; only the values are asked for per request.
  1080. //
  1081. // KEYED BY THE INDENT IT IS RENDERED AT, because that is the only thing about a
  1082. // component's HTML that its caller decides: the shell renders at one tab, the page at
  1083. // the depth the slot stands, a fill compactly (null). An app has a handful of those.
  1084. // the framework's `(key) => View tree`, for a kid whose steps are not built yet (set after the trees are)
  1085. treeOf = null
  1086. // the steps of a kid mount: the memo when it holds them, else built from the key's tree (read once, when they are built)
  1087. kidSteps(key, indent, classes, slotted) {
  1088. let ck = key + '|' + (indent == null ? '~' : indent) + '|' + joinList(classes) + '|' + (slotted ? '1' : '0')
  1089. if (steps[ck] == null) { return serverSteps(key, treeOf(key), indent, classes, slotted) }
  1090. return steps[ck]
  1091. }
  1092. serverSteps(key, &nodes, indent, classes, slotted) {
  1093. let ck = key + '|' + (indent == null ? '~' : indent) + '|' + joinList(classes) + '|' + (slotted ? '1' : '0')
  1094. if (steps[ck] == null) {
  1095. steps[ck] = blockSteps(classes == null || classes.length == 0 ? nodes : v.withRootClasses(&nodes, &classes), indent, slotted)
  1096. }
  1097. return steps[ck]
  1098. }
  1099. // A `slot` THE HOST FILLED COUNTS AS CONTENT, and an empty one does not — the one
  1100. // thing about a component's shape its caller decides, so it is part of the key.
  1101. staticVoid = [ 'area' 'base' 'br' 'col' 'embed' 'hr' 'img' 'input' 'link' 'meta' 'source' 'track' 'wbr' ]
  1102. staticVerbatim = [ 'textarea' 'pre' 'script' 'style' ]
  1103. blocky(n, slotted) {
  1104. if (staticVerbatim.includes(n.tag)) { return false }
  1105. if (n.children.length == 0) { return false }
  1106. for (c of n.children) {
  1107. if (c.k == 'text' || c.k == 'field' || c.k == 'expr') { return false }
  1108. if (c.k == 'member' && !(c.name == 'slot' && slotted)) { return false }
  1109. }
  1110. return true
  1111. }
  1112. steps = {}
  1113. v = new HlwViewServer
  1114. viewRead = new HlwView
  1115. joinList(l) {
  1116. if (l == null) { return '' }
  1117. let out = ''
  1118. for (c of l) { out = out + ',' + c }
  1119. return out
  1120. }
  1121. // `render(nodes, …, indent)`: the readable form puts each element on its own line at
  1122. // `indent`, the compact one writes them end to end.
  1123. blockSteps(nodes, indent, slotted) {
  1124. let out = []
  1125. for (n of nodes) {
  1126. if (indent != null) {
  1127. if (n.k == 'el') { push(&out, { k = 's'; v = newline + indent; }) nodeSteps(&out, n, indent, slotted) }
  1128. else if (n.k == 'component') { nodeSteps(&out, n, indent, slotted) }
  1129. else if (n.k == 'for') { forSteps(&out, n, indent, slotted) }
  1130. else if (n.k == 'if') { ifSteps(&out, n, indent, slotted) }
  1131. else if (n.k == 'member' && n.name == 'slot') { push(&out, { k = 'slotline'; v = newline + indent; }) }
  1132. else if (n.k != 'on') { nodeSteps(&out, n, indent, slotted) }
  1133. } else {
  1134. nodeSteps(&out, n, null, slotted)
  1135. }
  1136. }
  1137. return out
  1138. }
  1139. forSteps(&out, n, indent, slotted) {
  1140. out.push({ k = 'for'; row = n.row; read = readFn(n.list); body = blockSteps(n.body, indent, slotted); })
  1141. return null
  1142. }
  1143. ifSteps(&out, n, indent, slotted) {
  1144. if (n.regionfill != null) { return null }
  1145. if (n.region != null) {
  1146. out.push({ k = 'region'; name = n.region; ind = indent; late = n.deferred == true; body = n.deferred == true ? [] : blockSteps(n.then, indent, slotted); })
  1147. return null
  1148. }
  1149. out.push({ k = 'if'; read = readFn(n.cond); then = blockSteps(n.then, indent, slotted); other = blockSteps(n.other, indent, slotted); })
  1150. return null
  1151. }
  1152. // a script's children: text and values written as they are (`view.rawText`)
  1153. rawSteps(nodes, slotted) {
  1154. let out = []
  1155. for (n of nodes) {
  1156. if (n.k == 'text') { push(&out, { k = 's'; v = v.rawText(n.text); }) }
  1157. else if (n.k == 'member' || n.k == 'field' || n.k == 'expr') { out.push({ k = 'raw'; read = readFn(n); }) }
  1158. else { nodeSteps(&out, n, null, slotted) }
  1159. }
  1160. return out
  1161. }
  1162. // one node's steps — `html(node, …)`, decided once
  1163. nodeSteps(&out, n, indent, slotted) {
  1164. if (n.k == 'text') { push(&out, { k = 's'; v = v.escape(n.text); }) return null }
  1165. if (n.k == 'on') { return null }
  1166. if (n.k == 'member' && n.name == 'slot') { out.push({ k = 'slot'; }) return null }
  1167. if (n.k == 'member' || n.k == 'field' || n.k == 'expr') { out.push({ k = 'r'; read = readFn(n); }) return null }
  1168. if (n.k == 'for') { forSteps(&out, n, indent, slotted) return null }
  1169. if (n.k == 'if') { ifSteps(&out, n, indent, slotted) return null }
  1170. if (n.k == 'component') {
  1171. out.push({ k = 'kid'; path = n.path; cls = n.cls; key = n.key; classes = n.classes; indent = indent;
  1172. fill = (n.fill == null || n.fill.length == 0) ? null : blockSteps(n.fill, null, slotted); })
  1173. return null
  1174. }
  1175. if (n.k == 'el') { elSteps(&out, n, indent, slotted) return null }
  1176. push(&out, { k = 's'; v = '<!-- ' + n.tag + ' not rendered yet -->'; })
  1177. return null
  1178. }
  1179. elSteps(&out, n, indent, slotted) {
  1180. let attrs = []
  1181. let valueRead = null // a textarea's value is its content
  1182. let selRead = null // a select's value selects an option
  1183. for (a of n.attrs) {
  1184. if (n.tag == 'textarea' && a.name == 'value') { valueRead = attrRead(a) }
  1185. else if (n.tag == 'select' && a.name == 'value') { selRead = attrRead(a) }
  1186. // a bound BOOLEAN attribute is its presence (ticket #33): ` checked=""` or nothing
  1187. else if ((a.member != null || a.ref != null) && v.isBoolAttr(a.name)) { attrs.push({ k = 'b'; name = a.name; read = attrRead(a); }) }
  1188. // a null read is no attribute (#177)
  1189. else if (nullable(a)) { attrs.push({ k = 'a'; name = a.name; read = attrRead(a); }) }
  1190. else if (a.member != null || a.ref != null) {
  1191. push(&attrs, { k = 's'; v = ' ' + a.name + '="'; })
  1192. attrs.push({ k = 'r'; read = attrRead(a); })
  1193. push(&attrs, { k = 's'; v = v.extraOf(a) + '"'; })
  1194. } else {
  1195. push(&attrs, { k = 's'; v = ' ' + a.name + '="' + v.escape(a.text) + '"'; })
  1196. }
  1197. }
  1198. if (v.boundValue(&n)) { push(&attrs, { k = 's'; v = ' autocomplete="off"'; }) }
  1199. // the children, in the form this element takes: a block puts them one per line
  1200. let kids = []
  1201. // a script's and a style's content is raw text (#140, view.rawKids says why)
  1202. if (n.tag == 'script' || n.tag == 'style') { kids = rawSteps(n.children, slotted) }
  1203. else if (indent != null && blocky(n, slotted)) {
  1204. kids = blockSteps(n.children, indent + tab, slotted)
  1205. push(&kids, { k = 's'; v = newline + indent; })
  1206. } else {
  1207. kids = blockSteps(n.children, null, slotted)
  1208. }
  1209. if (valueRead != null) { kids = [{ k = 'r'; read = valueRead; }] }
  1210. // AN OPTION IS THE ONE ELEMENT WHOSE OWN TAG DEPENDS ON THE VALUE ABOVE IT: it
  1211. // carries `selected` when its value is the select's. Everything else is settled.
  1212. if (n.tag == 'option') {
  1213. out.push({ k = 'opt'; attrs = attrs; body = kids; ov = optionRead(n); })
  1214. return null
  1215. }
  1216. push(&out, { k = 's'; v = '<' + n.tag; })
  1217. for (a of attrs) { push(&out, a) }
  1218. if (staticVoid.includes(n.tag)) { push(&out, { k = 's'; v = '>'; }) return null }
  1219. push(&out, { k = 's'; v = '>'; })
  1220. // A SELECT HANDS ITS VALUE TO ITS OPTIONS, however deep they stand: its children
  1221. // run under it, and it is the only element that changes anything for them.
  1222. if (n.tag == 'select') { out.push({ k = 'sel'; read = selRead; body = kids; }) }
  1223. else { for (kk of kids) { push(&out, kk) } }
  1224. push(&out, { k = 's'; v = '</' + n.tag + '>'; })
  1225. return null
  1226. }
  1227. // CONSTANT TEXT MERGES. Two literal steps in a row are one string decided at compile
  1228. // time — which is what makes a request a concatenation of a handful of pieces instead
  1229. // of one per node.
  1230. push(&out, step) {
  1231. if (step.k == 's' && out.length > 0 && out[out.length - 1].k == 's') {
  1232. let last = out[out.length - 1]
  1233. last.v = last.v + step.v
  1234. out[out.length - 1] = last
  1235. return null
  1236. }
  1237. out.push(step)
  1238. return null
  1239. }
  1240. // ---- the reads, each with its own closure ------------------------------------------
  1241. // `view.value`'s rule, split once: a `for` row variable in scope wins over a member of
  1242. // the same name, and a field path walks into it. The closure holds the name — there is
  1243. // no node to look at per request.
  1244. readFn(node) {
  1245. if (node == null) { return (inst, rows) => { return null } }
  1246. if (node.k == 'text') { let t = viewRead.refValue(node) return (inst, rows) => { return t } }
  1247. if (node.k == 'expr') { let x = node.x return (inst, rows) => { return viewRead.evalExpr(&x, &inst, &rows) } }
  1248. let name = node.name
  1249. if (node.k != 'field') {
  1250. return (inst, rows) => { return rows[name] != null ? rows[name] : inst[name] }
  1251. }
  1252. let path = node.path
  1253. return (inst, rows) => {
  1254. let val = rows[name] != null ? rows[name] : inst[name]
  1255. for (f of path) {
  1256. if (val == null) { return null }
  1257. val = val[f]
  1258. }
  1259. return val
  1260. }
  1261. }
  1262. attrRead(a) {
  1263. if (a.member != null) { return readFn({ k = 'member'; name = a.member; }) }
  1264. if (a.ref != null) { return readFn(a.ref) }
  1265. let t = a.text
  1266. return (inst, rows) => { return t }
  1267. }
  1268. // an option's own value: its `value` attribute, or null when it has none (the browser
  1269. // then reports its text, and the run compares against what it rendered)
  1270. optionRead(n) {
  1271. for (a of n.attrs) {
  1272. if (a.name == 'value') { return attrRead(a) }
  1273. }
  1274. return null
  1275. }
  1276. // ---- the run: a concatenation ------------------------------------------------------
  1277. // EVERY REGION IS WRITTEN BETWEEN TWO MARKS (tickets #103, #104): an `if` around the
  1278. // branch that stands, a `for` around its rows and each row around its own nodes. The
  1279. // client claims them as they are and builds the same ones, and a region's nodes are
  1280. // exactly what stands between its two marks — nothing it holds has to be remembered
  1281. // node by node, and text beside a region can never run into the region's text.
  1282. regionOpen = '<!--hl:rg:'
  1283. regionClose = '<!--hl:/rg:'
  1284. markOpen = '<!--[-->'
  1285. markClose = '<!--]-->'
  1286. // THE PIECES ARE JOINED ONCE, AT THE END (ticket #127). `out = out + piece` copied
  1287. // the whole page so far for every piece: a 3.5 MB page of 2000 rows allocated 2.3 GB
  1288. // in growing copies, 7 GB per render all told — 12 s a load once the collector ran
  1289. // by bytes (#126), where the joined pieces take 2.5 s.
  1290. runSteps(&list, &inst, &rows, &ctx, &mounts) {
  1291. let out = []
  1292. runInto(&out, &list, &inst, &rows, &ctx, &mounts)
  1293. return out.join('')
  1294. }
  1295. // the same run, its pieces pushed onto the ONE list the caller joins: a nested region used to be
  1296. // joined into a string and then concatenated into its parent's list, so every piece was copied once
  1297. // per level it sat in (a README's blocks sit several levels deep)
  1298. runInto(&out, &list, &inst, &rows, &ctx, &mounts) {
  1299. for (s of list) {
  1300. if (s.k == 's') { out.push(s.v) }
  1301. else if (s.k == 'r') { let f = s.read out.push(v.escape(f(&inst, &rows))) }
  1302. else if (s.k == 'raw') { let f = s.read out.push(v.rawText(f(&inst, &rows))) }
  1303. else if (s.k == 'a') { let f = s.read let av = f(&inst, &rows) if (av != null) { out.push(' ' + s.name + '="' + v.escape(av) + '"') } }
  1304. else if (s.k == 'b') { let f = s.read if (v.boolOn(f(&inst, &rows))) { out.push(' ' + s.name + '=""') } }
  1305. else if (s.k == 'slot') { if (ctx.slot != null) { out.push(ctx.slot) } }
  1306. else if (s.k == 'slotline') { if (ctx.slot != null) { out.push(s.v + ctx.slot) } }
  1307. else if (s.k == 'for') {
  1308. let f = s.read
  1309. let list2 = f(&inst, &rows)
  1310. out.push(markOpen)
  1311. if (list2 != null) {
  1312. // THE ROW IS SET IN PLACE, and the name it shadows put back after the loop:
  1313. // `rows + {}` copied every enclosing row per iteration, and an outer row holds the
  1314. // whole list its inner `for` walks (a list block holding its N items): N rows of N items
  1315. let held = rows[s.row]
  1316. let ri = 0
  1317. let keys = viewRead.listKeys(&list2)
  1318. for (entry of list2) {
  1319. rows[s.row] = entry
  1320. let rc = v.rowCtx(&ctx, keys[ri])
  1321. out.push(markOpen)
  1322. runInto(&out, &s.body, &inst, &rows, &rc, &mounts)
  1323. out.push(markClose)
  1324. ri = ri + 1
  1325. }
  1326. rows[s.row] = held
  1327. }
  1328. out.push(markClose)
  1329. }
  1330. else if (s.k == 'region') {
  1331. // the region's default, between PLACEHOLDERS the document replaces once the page is known
  1332. // (a level above is rendered before the page below it is constructed): WebFramework `regions`
  1333. out.push(regionOpen + s.name + ':' + (s.ind == null ? '-' : '' + s.ind.length) + (s.late == true ? '!' : '') + '-->')
  1334. runInto(&out, &s.body, &inst, &rows, &ctx, &mounts)
  1335. out.push(regionClose + s.name + '-->')
  1336. }
  1337. else if (s.k == 'if') {
  1338. let f = s.read
  1339. out.push(markOpen)
  1340. if (f(&inst, &rows)) { runInto(&out, &s.then, &inst, &rows, &ctx, &mounts) }
  1341. else { runInto(&out, &s.other, &inst, &rows, &ctx, &mounts) }
  1342. out.push(markClose)
  1343. }
  1344. else if (s.k == 'opt') {
  1345. let head = runSteps(&s.attrs, &inst, &rows, &ctx, &mounts)
  1346. let body = runSteps(&s.body, &inst, &rows, &ctx, &mounts)
  1347. let mark = ''
  1348. if (ctx.sel != null) {
  1349. let ov = body
  1350. if (s.ov != null) { let f = s.ov ov = '' + f(&inst, &rows) }
  1351. if (ov == ctx.sel) { mark = ' selected' }
  1352. }
  1353. out.push('<option' + head + mark + '>' + body + '</option>')
  1354. }
  1355. else if (s.k == 'sel') {
  1356. let f = s.read
  1357. let inner = ctx + {}
  1358. inner.sel = f == null ? null : '' + f(&inst, &rows)
  1359. runInto(&out, &s.body, &inst, &rows, &inner, &mounts)
  1360. }
  1361. else if (s.k == 'kid') {
  1362. // THE KID IS READ IN PLACE: `let kid = ctx.kids[…]` copied the whole mount (its tree, its
  1363. // params, its state, its own kids) once per mount rendered. And the mounts a level holds
  1364. // travel as their own reference (`mounts`), not inside `ctx`: every `for` row copies the
  1365. // ctx, and a ctx holding the kids copied ALL of them per row (N rows of N mounts).
  1366. let kk = v.kidKeyIn(&s.path, &ctx)
  1367. if (mounts[kk] == null) { out.push('<!-- ' + s.cls + ' not mounted -->') }
  1368. else {
  1369. let fillText = s.fill == null ? null : runSteps(&s.fill, &inst, &rows, &ctx, &mounts)
  1370. let kidCtx = { slot = fillText; }
  1371. let empty = {}
  1372. let ksteps = kidSteps(s.key, s.indent, s.classes, fillText != null)
  1373. runInto(&out, &ksteps, &mounts[kk].page, &empty, &kidCtx, &mounts[kk].kids)
  1374. }
  1375. }
  1376. }
  1377. }

Branches

Latest commits

  • 5f82554enotes-demo: format Style with nested rules and bare mediamre
  • 3dc27d78mail-demo: format Style with nested rules and bare mediamre
  • cf9648afhome: format Style with nested rules and bare mediamre
  • 846a6242user-menu: format Style with nested rules and bare mediamre
  • 4d58708cthree-pane: format Style with nested rules and bare mediamre
  • 0bbfc553main-menu: format Style with nested rules and bare mediamre
  • 7755199dlist-first: format Style with nested rules and bare mediamre
  • 35199303content-first: format Style with nested rules and bare mediamre
  • 00f6e698app-sidebar: format Style with nested rules and bare mediamre
  • bb2f29c9app-header: format Style with nested rules and bare mediamre
  • 3aca2d7fSync components from 8176eb1 with reviewed nested stylesmre
  • 87e4003dRe-vendor layouts to Hybriel set day35mre
  • d8628cfflayouts: a startup error prints its message before exit 1 (hybriel#247)mre
  • 180fd76elayouts: MdEditor from components 59704ce (text on the clipboard always pastes, a file manager copy still attaches; pipe rows; empty Table-button table; tickets#49, notes#5)mre
  • a3eda8afRe-vendor Hybriel master 7a684920 (binary 74ab3715): plugins whole, startup fault exits 1; files-upload gate 340/0mre
  • 025d5880WIP re-vendor Hybriel set-day27b (a9712e61, master f5ff97bf): plugins whole, startup fault exits 1; NOT deployable: files-upload emit refused (see .scratch/rv27-upload)mre
  • 42183a71Merge sb3-standby: layouts deploys through its standbymre
  • 408de1f2layouts: no jobs switch needed in the standbymre
  • 0b85f57elayouts: a standby container (port 45019, profile standby); deploy hands over through standby-swap.sh without a gapmre
  • 31e40b8bMerge dm3-media: range handling in the layouts media routemre