gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitbe92a560be92a560Merge branch 'records' into mainTicketsbe92a560/plugins/web/compile.hl

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

Branches

Latest commits

  • be92a560Merge branch 'records' into mainTickets
  • 0daf2ea8|||PR records: PR-only workflow, #50 donemre
  • 0185d0b6Log PR 1 deploymre
  • f5bf16e2Mission 031: deploy PR 1mre
  • c69cd5f3Merge branch 'pr-flow' into mainTickets
  • cc56798cLog gitoria PR-only requestmre
  • d876d0b2|||PR README: changes land as PRsmre
  • fd83ac1fconductor: caramboleyo is the user, not owner/creatorApollo
  • 3b858c91Log backup hard-link fixmre
  • 7f6b37a0deploy.sh backup: tar --hard-dereference (hard links broke mpackdb sidecars on restore)mre
  • e93711d9Log E30 livemre
  • 59c02e4dMerge e30: preserve edited state note labels in Markdownmre
  • 30970796Log E30 deploy GOmre
  • 7c8e6d83Limit edited state note fix to Markdown renderingmre
  • 910536d0Log E30 reviewmre
  • 36984386Preserve state transitions when their notes are editedmre
  • d75cb946Log E30 startmre
  • 0bf3fefaMission 030: edited state note labelmre
  • 691565c6Log neutral-ticket rule checkmre
  • bc3bdecerecords: day38b live, #94 purgedmre