gitoriaLog in with ident

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit68dcb60368dcb603deploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre68dcb603/plugins/web/compile.hl

46.6 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 View from './view.hl'
  39. // ---- the emitted half --------------------------------------------------------------
  40. module(gen, key, nodes, fnTable) {
  41. fns = [] // the sub-walks: row bodies, if branches, fills
  42. done = {}
  43. rowVars = []
  44. tbl = tableOf(gen, key) // what the language says about this file, read once
  45. regions = regionIndex(tbl) // a `for`'s list reads, an `if`'s condition reads
  46. // A SHELL'S VIEW IS A `body` ELEMENT and the document already has one: the walk runs
  47. // against document.body itself, so the root list is that element's children and the
  48. // element only takes the marks a region rebuild on it would need.
  49. let roots = nodes
  50. let bodySite = null
  51. if (nodes.length == 1 && nodes[0].k == 'el' && nodes[0].tag == 'body') {
  52. roots = nodes[0].children
  53. bodySite = nodes[0].site
  54. }
  55. let root = walkTop(key, roots)
  56. if (bodySite != null) {
  57. root = root.replace('let r=rows;', 'let r=rows;await c.markHost(m,host,' + str(bodySite) + ');')
  58. }
  59. let src = ';(function(){var g=(typeof globalThis!=="undefined"?globalThis:window);'
  60. src = src + 'var C=(g.__hlC=g.__hlC||{});'
  61. // the element a claim takes or a create makes — the one shape both walks share
  62. // THE PRELUDE, one per module: the element a claim takes or a create makes, and the
  63. // two reads. A read written out in full — `(r["title"]!=null?r["title"]:inst["title"])`
  64. // — is 45 bytes and a component's View holds dozens of them; named, it is 16. The
  65. // rule is the same rule (a `for` row variable in scope wins over a member of the
  66. // same name, and a field path walks into it), spelled once.
  67. // A claim also moves the host's CURSOR — the last node the walk took there — which is
  68. // where a text leaf and a region's mark are looked for next (client.hl `leafNode`,
  69. // `markAt`).
  70. src = src + 'function A(cr,host,i,tag){if(cr){return document.createElement(tag);}var e=host.children[i];if(e!=null){host.__hlCur=e;}return e;}'
  71. src = src + 'function S(v){return v==null?"":""+v;}'
  72. src = src + 'function RV(r,q,n){return r[n]!=null?r[n]:q[n];}'
  73. src = src + 'function RF(r,q,n,p){var v=RV(r,q,n);for(var k=0;k<p.length;k++){if(v==null)return null;v=v[p[k]];}return v;}'
  74. src = src + 'var B={},T={},E={},F={},P={};'
  75. // ONE FUNCTION PER LIST, however many sites reach it: a nested walk is written once
  76. // and named — emitting it per site made the reference app's home page 215 KB of the
  77. // same code over and over.
  78. let wi = 0
  79. for (f of fns) {
  80. wi = wi + 1
  81. let name = 'w' + wi
  82. src = src + 'var ' + name + '=' + f.src + ';'
  83. for (site of f.sites) { src = src + f.slot + '[' + str(site) + ']=' + name + ';' }
  84. }
  85. let paints = []
  86. collectPaints(&paints, roots)
  87. for (e of paints) { src = src + 'P[' + str(e.site) + ']={p:' + e.paint + ',r:' + e.row + '};' }
  88. src = src + 'C[' + str(key) + ']={v:' + root + ',b:B,t:T,e:E,f:F,p:P'
  89. // THE ANSWERS A LOOKUP STILL NEEDS, and only those. A local event's write set stands
  90. // at its own listener and a region's names at its own region — written into the
  91. // call. These three are asked for by NAME at run time and cannot be: an inbound
  92. // frame names its event, a routine handed down names the member that holds it, and
  93. // a write set names the derivations that follow it.
  94. src = src + ',ev:' + eventMap() + ',mw:' + memberMap() + ',dv:' + derivList() + ',fn:' + fnMap(fnTable) + '};'
  95. return src + '})();' + newline
  96. }
  97. tbl = null
  98. regions = {}
  99. // WHAT A FUNCTION VALUE WRITES, by the site it was made at (tickets #98, #99): the
  100. // module announces a function's return with its site, and the browser repaints this
  101. // set — the members it writes, closed over the methods it calls and the events it
  102. // emits, the same closure a handler's set gets. A function that writes nothing has
  103. // no entry.
  104. fnMap(fns) {
  105. let bySite = {}
  106. if (fns != null) {
  107. for (f of fns) {
  108. let names = closeWrites(tbl, f.writes, f.reads)
  109. if (names.length > 0) { bySite[f.site] = names }
  110. }
  111. }
  112. return mapOf(bySite)
  113. }
  114. // what a DECLARED handler writes, by event: one entry per event name, unioned
  115. eventMap() {
  116. let byEvent = {}
  117. for (h of tbl.handlers) {
  118. if (!h.nested) {
  119. let have = byEvent[h.event] == null ? [] : byEvent[h.event]
  120. for (w of closeWrites(tbl, h.writes, h.reads)) { if (!have.includes(w)) { have.push(w) } }
  121. byEvent[h.event] = have
  122. }
  123. }
  124. return mapOf(byEvent)
  125. }
  126. // A MEMBER HOLDING A ROUTINE: what calling it writes is what its initializer writes
  127. memberMap() {
  128. let byName = {}
  129. for (m of tbl.members) {
  130. if (m.nodes != null) { byName[m.name] = memberNames(m.nodes.writes) }
  131. }
  132. return mapOf(byName)
  133. }
  134. // EVERY INITIALIZER, IN DECLARATION ORDER, with the members it reads — the whole of
  135. // what a derivation needs; the class carries the computation itself as `__derive__`
  136. derivList() {
  137. let src = '['
  138. let first = true
  139. for (d of tbl.derivations) {
  140. if (!first) { src = src + ',' }
  141. first = false
  142. src = src + '{name:' + str(d.name) + ',site:' + str(d.site) + ',reads:' + list(memberNames(d.reads)) + '}'
  143. }
  144. return src + ']'
  145. }
  146. mapOf(byName) {
  147. let src = '{'
  148. let first = true
  149. for (k of byName.keys()) {
  150. if (!first) { src = src + ',' }
  151. first = false
  152. src = src + str(k) + ':' + list(byName[k])
  153. }
  154. return src + '}'
  155. }
  156. // the write set and the read set of the handler at this site, closed over the methods
  157. // it calls and the handlers it emits to — computed here, written into the listener
  158. handlerSets(site) {
  159. for (h of tbl.handlers) {
  160. if (h.nested && h.site == site) {
  161. return list(closeWrites(tbl, h.writes, h.reads)) + ',' + list(closeReads(tbl, h.reads))
  162. }
  163. }
  164. return '[],[]'
  165. }
  166. regionNamesAt(site) {
  167. let names = regions[site]
  168. return list(names == null ? [] : names)
  169. }
  170. fns = []
  171. // ---- the walk, unrolled ------------------------------------------------------------
  172. // `top` marks the View's own root list: an element there is a COMPOSITION ROOT — a
  173. // reference above may have overwritten some of its events and put classes on it, which
  174. // is what `opt` carries (the old hl:web's rootOverwrites and withRootClasses).
  175. walk(key, nodes) {
  176. return plain(key, nodes)
  177. }
  178. // a list's walk
  179. plain(key, nodes) {
  180. let body = ''
  181. for (n of nodes) { body = body + node(key, n, false, false) }
  182. return 'async function(c,m,o,host,i,rows,rk,fill,cr,opt){let inst=m.instance;let r=rows;' + body + 'return i;}'
  183. }
  184. // the View's own root list, whose elements are composition roots
  185. walkTop(key, nodes) {
  186. let body = ''
  187. for (n of nodes) { body = body + node(key, n, true, false) }
  188. return 'async function(c,m,o,host,i,rows,rk,fill,cr,opt){let inst=m.instance;let r=rows;' + body + 'return i;}'
  189. }
  190. // a sub-walk, registered under its own site so the runtime can reach it by name
  191. // A SUB-WALK IS WRITTEN ONCE, however often the compile reaches its list.
  192. sub(key, nodes, slot, site) {
  193. if (done[slot + ' ' + site] != null) { return null }
  194. done[slot + ' ' + site] = true
  195. fns.push({ slot = slot; sites = [site]; src = walk(key, nodes); })
  196. return null
  197. }
  198. done = {}
  199. node(key, n, top, filterOns) {
  200. if (n.k == 'el') { return elNode(key, n, top) }
  201. 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) + ');}' }
  202. if (n.k == 'on') {
  203. // A REFERENCE ABOVE MAY HAVE OVERWRITTEN THIS EVENT on the composition root:
  204. // the root's own handler for it is dropped and the reference's stands instead
  205. // (the old hl:web's withoutOns, which applies to the root element's direct children).
  206. let guard = filterOns ? 'if(!(opt&&opt.over&&opt.over.indexOf(' + str(n.event) + ')>=0)){' : '{'
  207. return guard + 'await c.onAt(m,host,' + str(n.event) + ',' + str(n.site) + ',rows,' + handlerSets(n.site) + ');}'
  208. }
  209. if (n.k == 'member' && n.name == 'slot') {
  210. // the SHELL's slot is where the route component hangs; a child's slot is the
  211. // fragment its host wrote, and the host's frame builds it
  212. 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);}'
  213. }
  214. // A TEXT LEAF STANDING IN A LIST — an `if` branch, a `for` body — beside elements or
  215. // alone: its own text node, and a site of the member it reads (or of the row it
  216. // stands in), painted by rewriting that node.
  217. if (n.k == 'member' || n.k == 'field') {
  218. let names = (n.name == 'Style' || rowVars.includes(n.name)) ? [] : [n.name]
  219. return 'await c.textAt(m,host,cr,function(inst,r){return ' + textRead(n) + ';},' + list(names) + ',rows,rk);'
  220. }
  221. if (n.k == 'for') {
  222. // THE ROW VARIABLE IS IN SCOPE INSIDE THE BODY, and a read of it is the ROW's,
  223. // not a member of the instance — the language resolves it that way and the site
  224. // sets must say the same, or a member of the same name would paint the row.
  225. rowVars.push(n.row)
  226. sub(key, n.body, 'B', n.site)
  227. rowVars.pop()
  228. 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) + ');'
  229. }
  230. if (n.k == 'if') {
  231. sub(key, n.then, 'T', n.site)
  232. sub(key, n.other, 'E', n.site)
  233. 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) + ');'
  234. }
  235. if (n.k == 'component') {
  236. if (n.fill != null && n.fill.length > 0) { sub(key, n.fill, 'F', n.site) }
  237. return 'i=await c.kidAt(m,host,i,' + kidSpec(n) + ',rows,rk,fill,cr,opt);'
  238. }
  239. return ''
  240. }
  241. // ---- an element --------------------------------------------------------------------
  242. elNode(key, n, top) {
  243. let src = '{let el=A(cr,host,i,' + str(n.tag) + ');if(!cr){i=i+1;}'
  244. src = src + 'if(el==null){await c.acqFail(' + str(n.tag) + ',host);}else{'
  245. // the attributes, on the walk that BUILDS the element (a claim takes the server's)
  246. src = src + 'if(cr){' + createAttrs(n) + (top ? 'await c.rootClasses(el,opt);' : '') + 'host.appendChild(el);}'
  247. src = src + 'el.__hlRows=rows;el.__hlSite=' + str(n.site) + ';el.__hlKey=' + str(key) + ';'
  248. // the element's own named entries, for the literal a handler on it fires
  249. src = src + 'el.__hlLit=function(inst,r){return ' + litNamed(n) + ';};'
  250. let names = siteNames(n)
  251. let valueMember = 'null'
  252. for (a of n.attrs) { if (a.member != null && a.name == 'value') { valueMember = str(a.member) } }
  253. let sited = ''
  254. if (names.length > 0 || valueMember != 'null') {
  255. sited = 'await c.siteAt(m,el,' + list(names) + ',' + str(n.site) + ',' + valueMember + ',' + (n.tag == 'select' ? 'true' : 'false') + ',rows);'
  256. }
  257. let kids = ''
  258. // AN ELEMENT THAT HOLDS ONLY TEXT is painted whole (`textContent`, see the paints):
  259. // built, it gets that text as one node; claimed, the server's is taken as it stands
  260. let whole = textOf(n)
  261. if (whole != null) {
  262. kids = 'if(cr){host.appendChild(document.createTextNode(' + whole + '));}'
  263. for (c of n.children) { if (c.k == 'on') { kids = kids + node(key, c, false, top) } }
  264. }
  265. else if (mixed(n)) { kids = mixedKids(key, n, top) }
  266. else { for (c of n.children) { kids = kids + node(key, c, false, top) } }
  267. let inner = '{let host=el;let i=0;' + kids + '}'
  268. // A SELECT IS SITED AFTER ITS OPTIONS: the member decides which option stands, and
  269. // the read-back that `siteAt` does reports nothing before they exist.
  270. if (n.tag == 'select') { src = src + inner + 'if(cr){await c.applySelectAt(m,el,' + selectValue(n) + ');}' + sited }
  271. else { src = src + sited + inner }
  272. return src + '}}'
  273. }
  274. // A TEXT LEAF BESIDE AN ELEMENT OR A REGION (ticket #86 follow-up): `td { d.n if (…) {…} }`
  275. // cannot be painted through `textContent`, which would wipe the branch. Each RUN of
  276. // adjacent leaves is one text node, held on the element (`__hlLeaves`, by the run's
  277. // index) where the walk builds or claims it, and the paints rewrite that node alone.
  278. mixed(n) {
  279. if (textOf(n) != null) { return false }
  280. for (c of n.children) { if ((c.k == 'member' || c.k == 'field') && c.name != 'slot') { return true } }
  281. return false
  282. }
  283. isLeaf(c) { return c.k == 'text' || ((c.k == 'member' || c.k == 'field') && c.name != 'slot') }
  284. // the runs of adjacent leaves, in order; an `on` renders nothing and splits none
  285. runsOf(n) {
  286. let out = []
  287. let run = []
  288. for (c of n.children) {
  289. if (isLeaf(c)) { run.push(c) }
  290. else if (c.k != 'on') {
  291. if (run.length > 0) { out.push(run) }
  292. run = []
  293. }
  294. }
  295. if (run.length > 0) { out.push(run) }
  296. return out
  297. }
  298. runText(run) {
  299. let src = '""'
  300. for (c of run) { src = src + '+(' + (c.k == 'text' ? str(c.text) : textRead(c)) + ')' }
  301. return src
  302. }
  303. runFeeds(run) {
  304. for (c of run) { if (c.k != 'text') { return true } }
  305. return false
  306. }
  307. mixedKids(key, n, top) {
  308. let kids = ''
  309. let run = []
  310. let k = 0
  311. for (c of n.children) {
  312. if (isLeaf(c)) { run.push(c) }
  313. else {
  314. if (c.k != 'on' && run.length > 0) { kids = kids + leafRun(run, k) k = k + 1 run = [] }
  315. kids = kids + node(key, c, false, top)
  316. }
  317. }
  318. if (run.length > 0) { kids = kids + leafRun(run, k) }
  319. return kids
  320. }
  321. // EVERY CALL INTO THE CLIENT IS AWAITED: its methods are async in the browser, and one
  322. // not awaited runs its DOM work after the walk has gone on — a built leaf then lands
  323. // after the elements that follow it (ticket #104).
  324. leafRun(run, k) {
  325. if (!runFeeds(run)) { return 'if(cr){host.appendChild(document.createTextNode(' + runText(run) + '));}else{await c.leafAt(host,false,i,null,' + runText(run) + ');}' }
  326. return 'await c.leafAt(host,cr,i,' + k + ',' + runText(run) + ');'
  327. }
  328. // the attributes an element is BUILT with — a bound one written in, a literal one as it
  329. // stands. `value` is a property once the page is live and an attribute as the control's
  330. // default; a textarea has no value attribute at all and a select's value is an option.
  331. createAttrs(n) {
  332. let src = ''
  333. for (a of n.attrs) {
  334. if (isBool(a)) {
  335. src = src + '{' + boolWrite(a, rawRead(a.member != null ? { k = 'member'; name = a.member; } : a.ref)) + '}'
  336. } else {
  337. let rd = a.member != null ? readOf({ k = 'member'; name = a.member; }) : (a.ref != null ? readOf(a.ref) : str(a.text))
  338. src = src + '{let v=' + rd + ';'
  339. if (a.extra != null) { src = src + 'v=v+" "+' + str(a.extra) + ';' }
  340. if (n.tag == 'select' && a.name == 'value') { src = src + '}' }
  341. else if (a.name == 'value') {
  342. src = src + 'if(el.value!=null){el.value=v;' + (n.tag == 'textarea' ? '' : 'el.setAttribute("value",v);') + '}else{el.setAttribute("value",v);}}'
  343. } else { src = src + 'el.setAttribute(' + str(a.name) + ',v);}' }
  344. }
  345. }
  346. if (boundValue(n)) { src = src + 'el.setAttribute("autocomplete","off");' }
  347. return src
  348. }
  349. // a BOUND boolean attribute — a literal one was decided at build (WebFramework `element`)
  350. isBool(a) {
  351. return (a.member != null || a.ref != null) && v.isBoolAttr(a.name)
  352. }
  353. // A BOUND BOOLEAN ATTRIBUTE, WRITTEN (ticket #33): set when the value is on, REMOVED when
  354. // it is off — the markup's rule (view.boolAttrs) — and the DOM property with it, because
  355. // once the page is live `checked` is the property and the attribute only its default.
  356. boolWrite(a, raw) {
  357. let prop = a.name == 'readonly' ? 'readOnly' : a.name
  358. 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;}'
  359. }
  360. // view.hl's `boundValue`, to the letter: a FORM CONTROL whose `value` reads a member or
  361. // a field, and not one the author gave an `autocomplete` of its own
  362. boundValue(n) {
  363. if (n.tag != 'input' && n.tag != 'textarea' && n.tag != 'select') { return false }
  364. let bound = false
  365. for (a of n.attrs) {
  366. if (a.name == 'autocomplete') { return false }
  367. if (a.name == 'value' && (a.member != null || a.ref != null)) { bound = true }
  368. }
  369. return bound
  370. }
  371. selectValue(n) {
  372. for (a of n.attrs) {
  373. if (a.name == 'value') {
  374. if (a.member != null) { return readOf({ k = 'member'; name = a.member; }) }
  375. if (a.ref != null) { return readOf(a.ref) }
  376. return str(a.text)
  377. }
  378. }
  379. return 'null'
  380. }
  381. // the element's own entries as the literal sees them — its attributes by name, then the
  382. // `for` row variables in scope (the row rides on the value)
  383. litNamed(n) {
  384. let src = '{'
  385. let first = true
  386. for (a of n.attrs) {
  387. if (!first) { src = src + ',' }
  388. first = false
  389. let v = a.member != null ? rawRead({ k = 'member'; name = a.member; }) : (a.ref != null ? rawRead(a.ref) : str(a.text))
  390. src = src + str(a.name) + ':' + v
  391. }
  392. return src + '}'
  393. }
  394. // the reference's own description, written at its use site: what it binds, what it
  395. // overwrites, the classes the host's `#Child` rule puts on it
  396. kidSpec(n) {
  397. let src = '{key:' + str(n.key) + ',kid:' + str(kidKeyOf(n)) + ',site:' + str(n.site) + ',fill:' + ((n.fill != null && n.fill.length > 0) ? 'true' : 'false')
  398. src = src + ',bindings:['
  399. let first = true
  400. for (b of n.bindings) {
  401. if (!first) { src = src + ',' }
  402. first = false
  403. src = src + '{name:' + str(b.name) + ',member:' + (b.member == null ? 'null' : str(b.member))
  404. src = src + ',ref:' + (b.ref == null ? 'null' : 'function(inst,r){return ' + rawRead(b.ref) + ';}')
  405. src = src + ',refName:' + (b.ref == null ? 'null' : str(b.ref.name))
  406. src = src + ',text:' + (b.text == null ? 'null' : 'true') + ',value:' + (b.text == null ? 'null' : refLiteral(b)) + '}'
  407. }
  408. src = src + '],ons:['
  409. first = true
  410. for (o of n.ons) {
  411. if (!first) { src = src + ',' }
  412. first = false
  413. src = src + '{event:' + str(o.event) + ',site:' + str(o.site) + ',sets:[' + handlerSets(o.site) + ']}'
  414. }
  415. src = src + '],classes:' + list(n.classes == null ? [] : n.classes) + '}'
  416. return src
  417. }
  418. // view.hl's kidKey: the reference's path, joined — the key the server mounted it under
  419. kidKeyOf(n) {
  420. let out = ''
  421. for (p of n.path) { out = out + '/' + p }
  422. return out
  423. }
  424. // the value a literal binding carries — view.refValue, folded at compile time
  425. refLiteral(b) {
  426. if (b.kind == 'folded') { return jsValue(b.value) }
  427. if (b.kind == 'number') { return '' + b.text }
  428. if (b.kind == 'boolean') { return b.text == 'true' ? 'true' : 'false' }
  429. if (b.kind == 'null_literal') { return 'null' }
  430. return str(b.text)
  431. }
  432. jsValue(v) {
  433. if (v == null) { return 'null' }
  434. if (hlTypeName(v) == 'String') { return str(v) }
  435. if (hlTypeName(v) == 'Boolean') { return v ? 'true' : 'false' }
  436. if (hlTypeName(v) == 'Number') { return '' + v }
  437. return JSON.stringify(v)
  438. }
  439. // ---- the site set: what this element SHOWS ------------------------------------------
  440. // The old hl:web asked the module's table which members the element at this site reads. The
  441. // answer is a fact about the View, so it is written in: the members its bound
  442. // attributes and its text leaves name. `Style` is folded into a class name at build and
  443. // the shell's `slot` is swapped by navigation — neither is ever painted, and neither is
  444. // a member read that reaches here (a `for` row variable resolves to its row).
  445. siteNames(n) {
  446. let out = []
  447. for (a of n.attrs) {
  448. if (a.member != null && a.member != 'Style' && !rowVars.includes(a.member) && !out.includes(a.member)) { out.push(a.member) }
  449. // A FIELD OF A MEMBER (`value = f.title`, ticket #93) is a read of the member:
  450. // a write to `f.title`, or a new `f`, moves `f`, and this element repaints
  451. if (a.ref != null && a.ref.name != 'Style' && !rowVars.includes(a.ref.name) && !out.includes(a.ref.name)) { out.push(a.ref.name) }
  452. }
  453. for (c of n.children) {
  454. if ((c.k == 'member' || c.k == 'field') && c.name != 'slot' && c.name != 'Style' && !rowVars.includes(c.name)) {
  455. if (!out.includes(c.name)) { out.push(c.name) }
  456. }
  457. }
  458. return out
  459. }
  460. rowVars = []
  461. // ---- the paints ---------------------------------------------------------------------
  462. collectPaints(&out, nodes) {
  463. for (n of nodes) {
  464. if (n.k == 'el') {
  465. let paint = paintFn(n)
  466. let row = rowFn(n)
  467. if (paint != null || row != 'null') {
  468. out.push({ site = n.site; paint = paint == null ? 'null' : paint; row = row; })
  469. }
  470. collectPaints(&out, n.children)
  471. } else if (n.k == 'for') { collectPaints(&out, n.body) }
  472. else if (n.k == 'if') { collectPaints(&out, n.then) collectPaints(&out, n.other) }
  473. else if (n.k == 'component') { if (n.fill != null) { collectPaints(&out, n.fill) } }
  474. }
  475. return null
  476. }
  477. // `paint(&m, s, name)`, compiled: the bound attributes under the member each reads, and
  478. // the text where the member that moved stands among the leaves.
  479. paintFn(n) {
  480. let body = ''
  481. for (a of n.attrs) {
  482. if (a.member != null) {
  483. body = body + 'if(n===' + str(a.member) + '){' + attrWrite(n, a, readOf({ k = 'member'; name = a.member; })) + '}'
  484. } else if (a.ref != null) {
  485. // a field of a member paints when the MEMBER moves (ticket #93)
  486. body = body + 'if(n===' + str(a.ref.name) + '){' + attrWrite(n, a, readOf(a.ref)) + '}'
  487. }
  488. }
  489. let text = textOf(n)
  490. if (text != null) {
  491. let guard = ''
  492. for (c of n.children) {
  493. if ((c.k == 'member' || c.k == 'field') && c.name != 'slot') {
  494. if (guard != '') { guard = guard + '||' }
  495. guard = guard + 'n===' + str(c.name)
  496. }
  497. }
  498. if (guard != '') { body = body + 'if(' + guard + '){el.textContent=' + text + ';}' }
  499. } else if (mixed(n)) {
  500. let k = 0
  501. for (run of runsOf(n)) {
  502. let guard = ''
  503. for (c of run) {
  504. if (c.k != 'text') {
  505. if (guard != '') { guard = guard + '||' }
  506. guard = guard + 'n===' + str(c.name)
  507. }
  508. }
  509. if (guard != '') { body = body + 'if((' + guard + ')&&el.__hlLeaves&&el.__hlLeaves[' + k + ']){el.__hlLeaves[' + k + '].data=' + runText(run) + ';}' }
  510. k = k + 1
  511. }
  512. }
  513. if (body == '') { return null }
  514. return 'function(el,inst,r,n){' + body + '}'
  515. }
  516. // `rowPaint(&m, el)`, compiled: every bound spot, written only where the DOM does not
  517. // already hold it — which is why a push into 2000 rows costs the 1999 nothing.
  518. rowFn(n) {
  519. let body = ''
  520. for (a of n.attrs) {
  521. if (isBool(a)) {
  522. body = body + '{' + boolWrite(a, rawRead(a.member != null ? { k = 'member'; name = a.member; } : a.ref)) + '}'
  523. } else if (a.member != null || a.ref != null) {
  524. let one = 'let v=' + readOf(a.member != null ? { k = 'member'; name = a.member; } : a.ref) + ';'
  525. if (a.extra != null) { one = one + 'v=v+" "+' + str(a.extra) + ';' }
  526. if (n.tag == 'select' && a.name == 'value') { one = one + 'if(el.value!==v){el.value=v;}' }
  527. else {
  528. one = one + 'if(el.getAttribute(' + str(a.name) + ')!==v){el.setAttribute(' + str(a.name) + ',v);}'
  529. if (a.name == 'value') { one = one + 'if(el.value!=null&&el.value!==v){el.value=v;}' }
  530. }
  531. body = body + '{' + one + '}'
  532. }
  533. }
  534. let text = textOf(n)
  535. let feeds = false
  536. for (c of n.children) { if ((c.k == 'member' || c.k == 'field') && c.name != 'slot') { feeds = true } }
  537. if (text != null && feeds) { body = body + '{let t=' + text + ';if(el.textContent!==t){el.textContent=t;}}' }
  538. if (text == null && feeds) {
  539. let k = 0
  540. for (run of runsOf(n)) {
  541. if (runFeeds(run)) { body = body + '{let x=el.__hlLeaves&&el.__hlLeaves[' + k + '];let t=' + runText(run) + ';if(x&&x.data!==t){x.data=t;}}' }
  542. k = k + 1
  543. }
  544. }
  545. if (body == '') { return 'null' }
  546. return 'function(el,inst,r){' + body + '}'
  547. }
  548. attrWrite(n, a, v) {
  549. if (isBool(a)) { return boolWrite(a, rawRead(a.member != null ? { k = 'member'; name = a.member; } : a.ref)) }
  550. let src = 'let v=' + v + ';'
  551. if (a.extra != null) { src = src + 'v=v+" "+' + str(a.extra) + ';' }
  552. if (n.tag == 'select' && a.name == 'value') { return src + 'el.value=v;' }
  553. src = src + 'el.setAttribute(' + str(a.name) + ',v);'
  554. if (a.name == 'value') { src = src + 'if(el.value!=null){el.value=v;}' }
  555. return src
  556. }
  557. textOf(n) {
  558. let parts = []
  559. let content = 0
  560. let feeds = false
  561. for (c of n.children) {
  562. if (c.k != 'on') {
  563. content = content + 1
  564. if (c.k == 'text') { parts.push(str(c.text)) }
  565. else if ((c.k == 'member' || c.k == 'field') && c.name != 'slot') { parts.push(textRead(c)) feeds = true }
  566. }
  567. }
  568. if (!feeds) { return null }
  569. if (parts.length != content) { return null }
  570. let src = '""'
  571. for (p of parts) { src = src + '+(' + p + ')' }
  572. return src
  573. }
  574. // ---- a read, written in ---------------------------------------------------------------
  575. // `view.value`'s own rule as an expression: a `for` row variable in scope wins over a
  576. // member of the same name, and a field path walks into it. `readOf` answers the STRING
  577. // the DOM takes; `rawRead` the value itself (a binding, a list, a condition).
  578. readOf(node) { return '(""+' + rawRead(node) + ')' }
  579. // A READ SHOWN AS TEXT: null is no text, as the server writes it (view.hl `escape`)
  580. textRead(node) { return 'S(' + rawRead(node) + ')' }
  581. rawRead(node) {
  582. if (node == null) { return 'null' }
  583. if (node.k == 'text') { return str(node.text) }
  584. if (node.k != 'field') { return 'RV(r,inst,' + str(node.name) + ')' }
  585. return 'RF(r,inst,' + str(node.name) + ',' + list(node.path) + ')'
  586. }
  587. list(names) {
  588. let src = '['
  589. let first = true
  590. for (n of names) {
  591. if (!first) { src = src + ',' }
  592. first = false
  593. src = src + str(n)
  594. }
  595. return src + ']'
  596. }
  597. // a JavaScript string literal — JSON's escaping is a subset of JavaScript's, so a
  598. // source text's quotes, newlines and backslashes survive into the statement unchanged
  599. str(s) {
  600. if (s == null) { return '""' }
  601. return JSON.stringify('' + s)
  602. }
  603. // ---- WHAT THE LANGUAGE SAYS ABOUT THIS FILE, read HERE and written into the code ----
  604. // The old hl:web shipped the language's answers to the browser as data: `hlJs` writes each file's
  605. // members, handlers and methods — every node of every initializer, with the names it
  606. // reads — into the module, and the client reduces that table on every boot. The answers
  607. // are facts about the source, so this file asks for them once, at compile time, and
  608. // writes the RESULT into the statements: a handler's write set stands at the listener
  609. // that needs it, a region's names at the region, the derivations as a list of what to
  610. // re-run. The module carries no table at all (WebFramework `module` drops the one the
  611. // emitter writes).
  612. //
  613. // The reflection is the framework's own analysis generation — the same `gen` the View
  614. // trees come from. Nothing starts a second one: a handle is only valid inside the
  615. // generation that made it.
  616. tableOf(gen, key) {
  617. let members = []
  618. let derivations = []
  619. let handlers = []
  620. let methods = []
  621. for (m of hlMembers(gen, key)) {
  622. let entry = { name = m.name; nodes = null; }
  623. if (m.hasValue) {
  624. let n = syntaxTree(gen, key, m.node)
  625. entry.nodes = n
  626. derivations.push({ name = m.name; site = n.site; reads = n.reads; })
  627. nestedHandlers(&handlers, n)
  628. }
  629. members.push(entry)
  630. }
  631. for (h of hlEvents(gen, key).handles) {
  632. let n = syntaxTree(gen, key, h.node)
  633. handlers.push({ event = h.event; site = n.site; reads = n.reads; writes = n.writes; nested = false; })
  634. }
  635. for (mm of hlMethods(gen, key)) {
  636. if (!mm.inlineHandler) {
  637. let n = syntaxTree(gen, key, mm.node)
  638. methods.push({ name = mm.name; reads = n.reads; writes = n.writes; })
  639. }
  640. }
  641. return { members = members; derivations = derivations; handlers = handlers; methods = methods; }
  642. }
  643. // A NESTED `on` IS A DOM HANDLER, filed under its own site — the id the View node
  644. // carries. Its body is not the literal's syntax, so the node answers for it and the
  645. // walk stops there.
  646. nestedHandlers(&into, n) {
  647. if (n == null) { return null }
  648. if (n.tag == 'on_statement') {
  649. into.push({ event = n.event; site = n.site; reads = n.reads; writes = n.writes; nested = true; })
  650. return null
  651. }
  652. 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]) {
  653. if (c != null) { nestedHandlers(&into, c) }
  654. }
  655. for (l of [n.entries, n.args, n.elements]) {
  656. if (l != null) { for (c of l) { nestedHandlers(&into, c) } }
  657. }
  658. return null
  659. }
  660. shortSite(site) {
  661. if (site == null) { return null }
  662. let at = site.indexOf(':')
  663. if (at < 0) { return site }
  664. let path = site.slice(0, at)
  665. let cut = path.lastIndexOf('/')
  666. if (cut < 0) { return site }
  667. return path.slice(cut + 1) + site.slice(at)
  668. }
  669. // the syntax of one node, recursively — hl:core/AST's `tree`, read off the framework's
  670. // own generation instead of starting one of its own
  671. syntaxTree(gen, key, node) {
  672. let n = hlSyntax(gen, key, node)
  673. if (n == null) { return null }
  674. // A SITE IS NAMED BY THE FILE'S BASE NAME. The reflection answers with the project
  675. // key ('components/sort_list.hl:77:26'); the View tree, the emitter's own
  676. // `__derive__` branches and every site this framework writes use the base name
  677. // ('sort_list.hl:77:26'). One spelling, or a lookup by site finds nothing.
  678. let out = { tag = n.tag; site = shortSite(n.site); reads = n.reads; writes = n.writes; }
  679. if (n.tag == 'object_expression') {
  680. let kids = []
  681. for (h of n.entries) { kids.push(syntaxTree(gen, key, h)) }
  682. out.entries = kids
  683. } else if (n.tag == 'object_property') {
  684. out.key = n.key
  685. out.value = syntaxTree(gen, key, n.value)
  686. } else if (n.tag == 'view_for') {
  687. out.list = syntaxTree(gen, key, n.list)
  688. out.body = syntaxTree(gen, key, n.body)
  689. } else if (n.tag == 'view_if') {
  690. out.condition = syntaxTree(gen, key, n.condition)
  691. out.consequent = syntaxTree(gen, key, n.consequent)
  692. out.alternate = syntaxTree(gen, key, n.alternate)
  693. } else if (n.tag == 'on_statement') {
  694. out.event = n.event
  695. } else if (n.tag == 'member_expression') {
  696. out.object = syntaxTree(gen, key, n.object)
  697. out.property = syntaxTree(gen, key, n.property)
  698. } else if (n.tag == 'call_expression') {
  699. out.callee = syntaxTree(gen, key, n.callee)
  700. let args = []
  701. for (h of n.args) { args.push(syntaxTree(gen, key, h)) }
  702. out.args = args
  703. } else if (n.tag == 'binary_expression') {
  704. out.left = syntaxTree(gen, key, n.left)
  705. out.right = syntaxTree(gen, key, n.right)
  706. } else if (n.tag == 'unary_expression') {
  707. out.operand = syntaxTree(gen, key, n.operand)
  708. } else if (n.tag == 'conditional_expression') {
  709. out.condition = syntaxTree(gen, key, n.condition)
  710. out.consequent = syntaxTree(gen, key, n.consequent)
  711. out.alternate = syntaxTree(gen, key, n.alternate)
  712. } else if (n.tag == 'array_expression') {
  713. let els = []
  714. for (h of n.elements) { els.push(syntaxTree(gen, key, h)) }
  715. out.elements = els
  716. }
  717. return out
  718. }
  719. // ---- the reductions the browser used to make on every boot --------------------------
  720. // A `for`'s names are what its list reads, an `if`'s what its condition reads — filed
  721. // under the region's own site.
  722. regionIndex(t) {
  723. let idx = {}
  724. for (m of t.members) { if (m.nodes != null) { indexRegions(&idx, m.nodes) } }
  725. return idx
  726. }
  727. indexRegions(&idx, n) {
  728. if (n == null) { return null }
  729. if (n.tag == 'view_for') {
  730. idx[n.site] = memberNames(nodeReads(n.list))
  731. indexRegions(&idx, n.body)
  732. return null
  733. }
  734. if (n.tag == 'view_if') {
  735. idx[n.site] = memberNames(nodeReads(n.condition))
  736. indexRegions(&idx, n.consequent)
  737. indexRegions(&idx, n.alternate)
  738. return null
  739. }
  740. if (n.tag == 'on_statement') { return null }
  741. 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]) {
  742. if (c != null) { indexRegions(&idx, c) }
  743. }
  744. for (l of [n.entries, n.args, n.elements]) {
  745. if (l != null) { for (c of l) { indexRegions(&idx, c) } }
  746. }
  747. return null
  748. }
  749. // the member reads of a read list — the language resolved every name, so this is a
  750. // filter and not a second resolution
  751. memberNames(reads) {
  752. let out = []
  753. if (reads == null) { return out }
  754. for (r of reads) {
  755. if (r.kind == 'member' && r.isStatic != true && !out.includes(r.name)) { out.push(r.name) }
  756. }
  757. return out
  758. }
  759. nodeReads(n) {
  760. if (n == null) { return [] }
  761. if (n.reads != null) { return n.reads }
  762. let out = []
  763. 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]) {
  764. if (c != null) { for (r of nodeReads(c)) { out.push(r) } }
  765. }
  766. for (l of [n.entries, n.args, n.elements]) {
  767. if (l != null) { for (c of l) { for (r of nodeReads(c)) { out.push(r) } } }
  768. }
  769. return out
  770. }
  771. // A ROUTINE'S WRITES, PLUS THE WRITES OF EVERYTHING IT REACHES — the method it calls
  772. // and the handler it emits to, transitively. Both are READS the language resolved, so
  773. // this is a walk of its answer and never a guess.
  774. closeWrites(t, writes, reads) {
  775. let out = memberNames(writes)
  776. let queue = []
  777. reached(&queue, reads)
  778. let seen = {}
  779. while (queue.length > 0) {
  780. let at = queue.shift()
  781. if (seen[at] == null) {
  782. seen[at] = true
  783. let sp = at.indexOf(' ')
  784. let kind = at.slice(0, sp)
  785. let name = at.slice(sp + 1)
  786. for (mm of t.methods) {
  787. if (kind == 'method' && mm.name == name) {
  788. for (w of memberNames(mm.writes)) { if (!out.includes(w)) { out.push(w) } }
  789. reached(&queue, mm.reads)
  790. }
  791. }
  792. for (h of t.handlers) {
  793. if (kind == 'handler' && !h.nested && h.event == name) {
  794. for (w of memberNames(h.writes)) { if (!out.includes(w)) { out.push(w) } }
  795. reached(&queue, h.reads)
  796. }
  797. }
  798. for (mb of t.members) {
  799. if (kind == 'member' && mb.name == name && mb.nodes != null) {
  800. for (w of memberNames(mb.nodes.writes)) { if (!out.includes(w)) { out.push(w) } }
  801. reached(&queue, nodeReads(mb.nodes))
  802. }
  803. }
  804. }
  805. }
  806. return out
  807. }
  808. // the mirror: a handler's reads plus the reads of everything it reaches — the
  809. // candidates for a routine a host handed down, whose writes only the host's table names
  810. closeReads(t, reads) {
  811. let out = memberNames(reads)
  812. let queue = []
  813. reached(&queue, reads)
  814. let seen = {}
  815. while (queue.length > 0) {
  816. let at = queue.shift()
  817. if (seen[at] == null) {
  818. seen[at] = true
  819. let sp = at.indexOf(' ')
  820. let kind = at.slice(0, sp)
  821. let name = at.slice(sp + 1)
  822. for (mm of t.methods) {
  823. if (kind == 'method' && mm.name == name) {
  824. for (r of memberNames(mm.reads)) { if (!out.includes(r)) { out.push(r) } }
  825. reached(&queue, mm.reads)
  826. }
  827. }
  828. for (h of t.handlers) {
  829. if (kind == 'handler' && !h.nested && h.event == name) {
  830. for (r of memberNames(h.reads)) { if (!out.includes(r)) { out.push(r) } }
  831. reached(&queue, h.reads)
  832. }
  833. }
  834. for (mb of t.members) {
  835. if (kind == 'member' && mb.name == name && mb.nodes != null) {
  836. for (r of memberNames(nodeReads(mb.nodes))) { if (!out.includes(r)) { out.push(r) } }
  837. reached(&queue, nodeReads(mb.nodes))
  838. }
  839. }
  840. }
  841. }
  842. return out
  843. }
  844. reached(&queue, reads) {
  845. if (reads == null) { return null }
  846. for (r of reads) {
  847. if (r.kind == 'method' || r.kind == 'handler' || (r.kind == 'member' && r.isStatic != true)) { queue.push(r.kind + ' ' + r.name) }
  848. }
  849. return null
  850. }
  851. // ---- THE SERVER RENDER, COMPILED --------------------------------------------------
  852. // The old hl:web rendered a page by WALKING the View tree per request: for every node it asks
  853. // what kind it is, for every element it builds the attribute string, and it decides the
  854. // indentation of every line again, on every request, for every one of the 121 mounts a
  855. // page may hold. Every one of those decisions is a fact about the View.
  856. //
  857. // So a component's render is compiled ONCE into a list of STEPS — constant text, and a
  858. // read with its own closure — and a request is a concatenation. The structure (tags,
  859. // static attributes, the readable form's line breaks, which element is a block) is
  860. // decided at compile time; only the values are asked for per request.
  861. //
  862. // KEYED BY THE INDENT IT IS RENDERED AT, because that is the only thing about a
  863. // component's HTML that its caller decides: the shell renders at one tab, the page at
  864. // the depth the slot stands, a fill compactly (null). An app has a handful of those.
  865. serverSteps(key, nodes, indent, classes, slotted) {
  866. let ck = key + '|' + (indent == null ? '~' : indent) + '|' + joinList(classes) + '|' + (slotted ? '1' : '0')
  867. if (steps[ck] == null) {
  868. steps[ck] = blockSteps(classes == null || classes.length == 0 ? nodes : v.withRootClasses(nodes, classes), indent, slotted)
  869. }
  870. return steps[ck]
  871. }
  872. // A `slot` THE HOST FILLED COUNTS AS CONTENT, and an empty one does not — the one
  873. // thing about a component's shape its caller decides, so it is part of the key.
  874. staticVoid = [ 'area' 'base' 'br' 'col' 'embed' 'hr' 'img' 'input' 'link' 'meta' 'source' 'track' 'wbr' ]
  875. staticVerbatim = [ 'textarea' 'pre' 'script' 'style' ]
  876. blocky(n, slotted) {
  877. if (staticVerbatim.includes(n.tag)) { return false }
  878. if (n.children.length == 0) { return false }
  879. for (c of n.children) {
  880. if (c.k == 'text' || c.k == 'field') { return false }
  881. if (c.k == 'member' && !(c.name == 'slot' && slotted)) { return false }
  882. }
  883. return true
  884. }
  885. steps = {}
  886. v = new View
  887. joinList(l) {
  888. if (l == null) { return '' }
  889. let out = ''
  890. for (c of l) { out = out + ',' + c }
  891. return out
  892. }
  893. // `render(nodes, …, indent)`: the readable form puts each element on its own line at
  894. // `indent`, the compact one writes them end to end.
  895. blockSteps(nodes, indent, slotted) {
  896. let out = []
  897. for (n of nodes) {
  898. if (indent != null) {
  899. if (n.k == 'el') { push(&out, { k = 's'; v = newline + indent; }) nodeSteps(&out, n, indent, slotted) }
  900. else if (n.k == 'component') { nodeSteps(&out, n, indent, slotted) }
  901. else if (n.k == 'for') { forSteps(&out, n, indent, slotted) }
  902. else if (n.k == 'if') { ifSteps(&out, n, indent, slotted) }
  903. else if (n.k == 'member' && n.name == 'slot') { push(&out, { k = 'slotline'; v = newline + indent; }) }
  904. else if (n.k != 'on') { nodeSteps(&out, n, indent, slotted) }
  905. } else {
  906. nodeSteps(&out, n, null, slotted)
  907. }
  908. }
  909. return out
  910. }
  911. forSteps(&out, n, indent, slotted) {
  912. out.push({ k = 'for'; row = n.row; read = readFn(n.list); body = blockSteps(n.body, indent, slotted); })
  913. return null
  914. }
  915. ifSteps(&out, n, indent, slotted) {
  916. out.push({ k = 'if'; read = readFn(n.cond); then = blockSteps(n.then, indent, slotted); other = blockSteps(n.other, indent, slotted); })
  917. return null
  918. }
  919. // one node's steps — `html(node, …)`, decided once
  920. nodeSteps(&out, n, indent, slotted) {
  921. if (n.k == 'text') { push(&out, { k = 's'; v = v.escape(n.text); }) return null }
  922. if (n.k == 'on') { return null }
  923. if (n.k == 'member' && n.name == 'slot') { out.push({ k = 'slot'; }) return null }
  924. if (n.k == 'member' || n.k == 'field') { out.push({ k = 'r'; read = readFn(n); }) return null }
  925. if (n.k == 'for') { forSteps(&out, n, indent, slotted) return null }
  926. if (n.k == 'if') { ifSteps(&out, n, indent, slotted) return null }
  927. if (n.k == 'component') {
  928. out.push({ k = 'kid'; path = n.path; cls = n.cls; key = n.key; classes = n.classes; indent = indent;
  929. fill = (n.fill == null || n.fill.length == 0) ? null : blockSteps(n.fill, null, slotted); })
  930. return null
  931. }
  932. if (n.k == 'el') { elSteps(&out, n, indent, slotted) return null }
  933. push(&out, { k = 's'; v = '<!-- ' + n.tag + ' not rendered yet -->'; })
  934. return null
  935. }
  936. elSteps(&out, n, indent, slotted) {
  937. let attrs = []
  938. let valueRead = null // a textarea's value is its content
  939. let selRead = null // a select's value selects an option
  940. for (a of n.attrs) {
  941. if (n.tag == 'textarea' && a.name == 'value') { valueRead = attrRead(a) }
  942. else if (n.tag == 'select' && a.name == 'value') { selRead = attrRead(a) }
  943. // a bound BOOLEAN attribute is its presence (ticket #33): ` checked=""` or nothing
  944. else if ((a.member != null || a.ref != null) && v.isBoolAttr(a.name)) { attrs.push({ k = 'b'; name = a.name; read = attrRead(a); }) }
  945. else if (a.member != null || a.ref != null) {
  946. push(&attrs, { k = 's'; v = ' ' + a.name + '="'; })
  947. attrs.push({ k = 'r'; read = attrRead(a); })
  948. push(&attrs, { k = 's'; v = v.extraOf(a) + '"'; })
  949. } else {
  950. push(&attrs, { k = 's'; v = ' ' + a.name + '="' + v.escape(a.text) + '"'; })
  951. }
  952. }
  953. if (v.boundValue(n)) { push(&attrs, { k = 's'; v = ' autocomplete="off"'; }) }
  954. // the children, in the form this element takes: a block puts them one per line
  955. let kids = []
  956. if (indent != null && blocky(n, slotted)) {
  957. kids = blockSteps(n.children, indent + tab, slotted)
  958. push(&kids, { k = 's'; v = newline + indent; })
  959. } else {
  960. kids = blockSteps(n.children, null, slotted)
  961. }
  962. if (valueRead != null) { kids = [{ k = 'r'; read = valueRead; }] }
  963. // AN OPTION IS THE ONE ELEMENT WHOSE OWN TAG DEPENDS ON THE VALUE ABOVE IT: it
  964. // carries `selected` when its value is the select's. Everything else is settled.
  965. if (n.tag == 'option') {
  966. out.push({ k = 'opt'; attrs = attrs; body = kids; ov = optionRead(n); })
  967. return null
  968. }
  969. push(&out, { k = 's'; v = '<' + n.tag; })
  970. for (a of attrs) { push(&out, a) }
  971. if (staticVoid.includes(n.tag)) { push(&out, { k = 's'; v = '>'; }) return null }
  972. push(&out, { k = 's'; v = '>'; })
  973. // A SELECT HANDS ITS VALUE TO ITS OPTIONS, however deep they stand: its children
  974. // run under it, and it is the only element that changes anything for them.
  975. if (n.tag == 'select') { out.push({ k = 'sel'; read = selRead; body = kids; }) }
  976. else { for (kk of kids) { push(&out, kk) } }
  977. push(&out, { k = 's'; v = '</' + n.tag + '>'; })
  978. return null
  979. }
  980. // CONSTANT TEXT MERGES. Two literal steps in a row are one string decided at compile
  981. // time — which is what makes a request a concatenation of a handful of pieces instead
  982. // of one per node.
  983. push(&out, step) {
  984. if (step.k == 's' && out.length > 0 && out[out.length - 1].k == 's') {
  985. let last = out[out.length - 1]
  986. last.v = last.v + step.v
  987. out[out.length - 1] = last
  988. return null
  989. }
  990. out.push(step)
  991. return null
  992. }
  993. // ---- the reads, each with its own closure ------------------------------------------
  994. // `view.value`'s rule, split once: a `for` row variable in scope wins over a member of
  995. // the same name, and a field path walks into it. The closure holds the name — there is
  996. // no node to look at per request.
  997. readFn(node) {
  998. if (node == null) { return (inst, rows) => { return null } }
  999. if (node.k == 'text') { let t = v.literalValue(node.kind, node.text) return (inst, rows) => { return t } }
  1000. let name = node.name
  1001. if (node.k != 'field') {
  1002. return (inst, rows) => { return rows[name] != null ? rows[name] : inst[name] }
  1003. }
  1004. let path = node.path
  1005. return (inst, rows) => {
  1006. let val = rows[name] != null ? rows[name] : inst[name]
  1007. for (f of path) {
  1008. if (val == null) { return null }
  1009. val = val[f]
  1010. }
  1011. return val
  1012. }
  1013. }
  1014. attrRead(a) {
  1015. if (a.member != null) { return readFn({ k = 'member'; name = a.member; }) }
  1016. if (a.ref != null) { return readFn(a.ref) }
  1017. let t = a.text
  1018. return (inst, rows) => { return t }
  1019. }
  1020. // an option's own value: its `value` attribute, or null when it has none (the browser
  1021. // then reports its text, and the run compares against what it rendered)
  1022. optionRead(n) {
  1023. for (a of n.attrs) {
  1024. if (a.name == 'value') { return attrRead(a) }
  1025. }
  1026. return null
  1027. }
  1028. // ---- the run: a concatenation ------------------------------------------------------
  1029. // EVERY REGION IS WRITTEN BETWEEN TWO MARKS (tickets #103, #104): an `if` around the
  1030. // branch that stands, a `for` around its rows and each row around its own nodes. The
  1031. // client claims them as they are and builds the same ones, and a region's nodes are
  1032. // exactly what stands between its two marks — nothing it holds has to be remembered
  1033. // node by node, and text beside a region can never run into the region's text.
  1034. markOpen = '<!--[-->'
  1035. markClose = '<!--]-->'
  1036. runSteps(list, &inst, &rows, &ctx) {
  1037. let out = ''
  1038. for (s of list) {
  1039. if (s.k == 's') { out = out + s.v }
  1040. else if (s.k == 'r') { let f = s.read out = out + v.escape(f(&inst, &rows)) }
  1041. else if (s.k == 'b') { let f = s.read if (v.boolOn(f(&inst, &rows))) { out = out + ' ' + s.name + '=""' } }
  1042. else if (s.k == 'slot') { if (ctx.slot != null) { out = out + ctx.slot } }
  1043. else if (s.k == 'slotline') { if (ctx.slot != null) { out = out + s.v + ctx.slot } }
  1044. else if (s.k == 'for') {
  1045. let f = s.read
  1046. let list2 = f(&inst, &rows)
  1047. out = out + markOpen
  1048. if (list2 != null) {
  1049. let ri = 0
  1050. for (entry of list2) {
  1051. let inner = rows + {}
  1052. inner[s.row] = entry
  1053. let rc = v.rowCtx(&ctx, &entry, ri)
  1054. out = out + markOpen + runSteps(s.body, &inst, &inner, &rc) + markClose
  1055. ri = ri + 1
  1056. }
  1057. }
  1058. out = out + markClose
  1059. }
  1060. else if (s.k == 'if') {
  1061. let f = s.read
  1062. if (f(&inst, &rows)) { out = out + markOpen + runSteps(s.then, &inst, &rows, &ctx) + markClose }
  1063. else { out = out + markOpen + runSteps(s.other, &inst, &rows, &ctx) + markClose }
  1064. }
  1065. else if (s.k == 'opt') {
  1066. let head = runSteps(s.attrs, &inst, &rows, &ctx)
  1067. let body = runSteps(s.body, &inst, &rows, &ctx)
  1068. let mark = ''
  1069. if (ctx.sel != null) {
  1070. let ov = body
  1071. if (s.ov != null) { let f = s.ov ov = '' + f(&inst, &rows) }
  1072. if (ov == ctx.sel) { mark = ' selected' }
  1073. }
  1074. out = out + '<option' + head + mark + '>' + body + '</option>'
  1075. }
  1076. else if (s.k == 'sel') {
  1077. let f = s.read
  1078. let inner = ctx + {}
  1079. inner.sel = f == null ? null : '' + f(&inst, &rows)
  1080. out = out + runSteps(s.body, &inst, &rows, &inner)
  1081. }
  1082. else if (s.k == 'kid') {
  1083. let kid = ctx.kids[v.kidKeyIn(s.path, ctx)]
  1084. if (kid == null) { out = out + '<!-- ' + s.cls + ' not mounted -->' }
  1085. else {
  1086. let fillText = s.fill == null ? null : runSteps(s.fill, &inst, &rows, &ctx)
  1087. let kidCtx = { slot = fillText; kids = kid.kids; }
  1088. let kidInst = kid.page
  1089. let empty = {}
  1090. out = out + runSteps(serverSteps(s.key, kid.view, s.indent, s.classes, fillText != null), &kidInst, &empty, &kidCtx)
  1091. }
  1092. }
  1093. }
  1094. return out
  1095. }

Branches

Latest commits

  • 68dcb603deploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
  • e2deed6dgitoria#20: "Add code" only on the Code page of an empty repository, no collapsiblemre
  • 8bb97ffddeploy.sh: never send .git or .gitignore to Byrodinmre
  • fd981932State of 2026-09-27; bin/ no longer tracked (Hybriel commit is in README)mre
  • 4a2d7125initial commitmre