gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/plugins/web/view.hl

7.4 KB

  1. // The View's READERS — realm-neutral: the same code reads values on the server and (compiled to JS) in the
  2. // browser; the browser's bundle carries this file and nothing server-only (viewserver.hl). It reads a TREE, the
  3. // syntax of a View member reflected into plain hybrids by the server (see
  4. // server.hl `tree()`), against an INSTANCE whose members supply the values.
  5. //
  6. // Tree node shapes:
  7. // { k = 'el' tag key path = [ keys… ] site attrs = [ { name text } | { name member } ] children = [ … ] }
  8. // `site` is `file:line:col` of the literal — the id its class is minted under
  9. // `tag` is what the DOM gets (a custom key kebab-cased), `key` the source key
  10. // { k = 'text' text }
  11. // { k = 'member' name } an ordered entry naming a member of the class — a SITE
  12. // { k = 'field' name path } `p.title`: a path off a member or a `for` row variable
  13. // { k = 'for' row list body } `for (row of list) { … }`, body rendered per entry
  14. // { k = 'if' cond then other } `if (cond) { … } else { … }`
  15. // { k = 'on' event } a DOM event the owning literal handles (its inline `on`)
  16. // { k = 'component' cls key path bindings ons } `Child { count = count on done(v) { … } }`:
  17. // a composed component — its own mount renders here
  18. // { k = 'other' tag } a construct this round does not render yet (if / for / on)
  19. //
  20. // `ctx` rides through every call: `ctx.slot` is the HTML a shell's `slot` member
  21. // stands for (null when rendering a page alone), `ctx.kids` the child mounts of
  22. // the instance being rendered, keyed by the component node's path joined with
  23. // '/'. A component node renders ITS mount: its own instance, its own kids.
  24. // the language has no escapes: a newline and a tab are written as themselves
  25. static newline = "
  26. "
  27. static tab = " "
  28. // a component node's key in a mount's `kids`: its path, '/'-joined (a list has no
  29. // join in the language; the walk is written out)
  30. kidKey(&path) {
  31. let out = ''
  32. for (p of path) { out = out + '/' + p }
  33. return out
  34. }
  35. // THE KEY OF ONE ROW — the record's id, then @id (hl:mpackdb documents), and by
  36. // the row's index where the row is not a record or carries neither key (a list of strings
  37. // is one: demo-blog's paragraphs). ONE rule, called by everything that has to tell
  38. // one row from the next: the server's HTML walk below, the mount key of a row's
  39. // children (WebFramework `mountKids`), and the browser's keyed region (client.hl).
  40. // Two rows with the SAME id are two rows to the language and one key to this rule;
  41. // the browser's region gives the second one a key of its own, the server does not.
  42. rowKey(&entry, i) {
  43. if (entry != null) {
  44. t = hlTypeName(&entry)
  45. if (t == 'Hybrid' || t == 'Instance') {
  46. if (entry.id != null) { return '' + entry.id }
  47. if (entry.@id != null) { return '' + entry.@id }
  48. }
  49. }
  50. return '' + i
  51. }
  52. // THE KEYS OF A LIST, in order: rowKey of each row, and where one id stands twice the second and later
  53. // rows get ':' + their index (#119). The browser's region (client listKeys) keys a list the same way, so a
  54. // child of the second row of one id is its own mount on the server too, not the first row's.
  55. listKeys(&entries) {
  56. let out = []
  57. let seen = {}
  58. let ri = 0
  59. for (entry of entries) {
  60. let k = rowKey(&entry, ri)
  61. if (seen[k] != null) { k = k + ':' + ri }
  62. seen[k] = true
  63. out.push(k)
  64. ri = ri + 1
  65. }
  66. return out
  67. }
  68. // A LITERAL'S VALUE, TYPED — COMPONENTS.md §12, "typed, not stringified". `hlSyntax`
  69. // hands a literal back as its source text and its KIND, and a reference binding carries
  70. // the value rather than the text: `Level2 { n = 1 }` gives the child a Number, so `n * 97`
  71. // is arithmetic and not "operator '*' does not apply to String and Number" (measured
  72. // 2026-09-14, demo-nested wall 3). An attribute and a text child keep the text, because
  73. // HTML is text; only what crosses into a member of another class is typed.
  74. literalValue(kind, text) {
  75. if (kind == 'number') { return toNumber(text) }
  76. if (kind == 'boolean') { return text == 'true' }
  77. if (kind == 'null_literal') { return null }
  78. return text
  79. }
  80. // THE VALUE A REFERENCE BINDING CARRIES when it is a literal or a folded static — the
  81. // one place the two are read, so the server's mount and the browser's do it alike. A
  82. // static is folded to its VALUE and carried as one ('folded'), never printed and parsed
  83. // back; a literal carries its source text and its kind, which is all the syntax has.
  84. refValue(b) {
  85. if (b.kind == 'folded') { return b.value }
  86. if (b.kind != null) { return literalValue(b.kind, b.text) }
  87. return b.text
  88. }
  89. // THE VALUE a read names: a `for` row variable in scope wins over a member of the
  90. // same name (nesting is scope); a field path walks into it.
  91. value(&node, &page, &rows) {
  92. if (node.k == 'text') { return refValue(node) }
  93. if (node.k == 'expr') { return evalExpr(&node.x, &page, &rows) }
  94. // READ IN PLACE: `v = rows[node.name]` copied the whole row value (a block with all its spans) and each
  95. // `v = v[f]` step copied the rest again, per read, per mount; a path of up to three steps is one indexed read
  96. let name = node.name
  97. let inRow = rows[name] != null
  98. if (node.k == 'field') {
  99. let path = node.path
  100. let n = path.length
  101. if (n == 1) {
  102. if (inRow) { return rows[name] == null ? null : rows[name][path[0]] }
  103. return page[name] == null ? null : page[name][path[0]]
  104. }
  105. if (n == 2) {
  106. if (inRow) { return rows[name][path[0]] == null ? null : rows[name][path[0]][path[1]] }
  107. return page[name][path[0]] == null ? null : page[name][path[0]][path[1]]
  108. }
  109. }
  110. let v = null
  111. if (inRow) { v = rows[name] } else { v = page[name] }
  112. if (node.k == 'field') {
  113. for (f of node.path) {
  114. if (v == null) { return null }
  115. v = v[f]
  116. }
  117. }
  118. return v
  119. }
  120. // A VIEW EXPRESSION (`p { "x" + n }`, `a - b`, `n > 3 ? 'big' : 'small'`) is evaluated by
  121. // the LANGUAGE'S OWN OPERATORS: this walk only picks the operator the author wrote and
  122. // applies it, so what `+` gives null and a string, what `==` says of two types, what `&&`
  123. // returns — all of it is the runtime's answer, on the server (the interpreter or a
  124. // binary) and in the browser (the client target), and hl:web has no rule of its own.
  125. // The leaves are reads (`value`) and literals. `&&`, `||` and `?:` evaluate what they
  126. // pick only, as the language does.
  127. evalExpr(&x, &page, &rows) {
  128. if (x.k == 'bin') {
  129. let l = evalExpr(&x.l, &page, &rows)
  130. let op = x.op
  131. if (op == 'logical_and') { return l && evalExpr(&x.r, &page, &rows) }
  132. if (op == 'logical_or') { return l || evalExpr(&x.r, &page, &rows) }
  133. let r = evalExpr(&x.r, &page, &rows)
  134. if (op == 'add') { return l + r }
  135. if (op == 'subtract') { return l - r }
  136. if (op == 'multiply') { return l * r }
  137. if (op == 'divide') { return l / r }
  138. if (op == 'modulo') { return l % r }
  139. if (op == 'equal') { return l == r }
  140. if (op == 'not_equal') { return l != r }
  141. if (op == 'less_than') { return l < r }
  142. if (op == 'greater_than') { return l > r }
  143. if (op == 'less_equal') { return l <= r }
  144. if (op == 'greater_equal') { return l >= r }
  145. if (op == 'bit_and') { return l & r }
  146. if (op == 'bit_or') { return l | r }
  147. if (op == 'bit_xor') { return l ^ r }
  148. if (op == 'shift_left') { return l << r }
  149. return l >> r
  150. }
  151. if (x.k == 'un') {
  152. let o = evalExpr(&x.x, &page, &rows)
  153. if (x.op == 'logical_not') { return !o }
  154. if (x.op == 'negate') { return -o }
  155. return ~o
  156. }
  157. if (x.k == 'cond') { return evalExpr(&x.c, &page, &rows) ? evalExpr(&x.a, &page, &rows) : evalExpr(&x.b, &page, &rows) }
  158. return value(&x, &page, &rows)
  159. }

Branches

Latest commits

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre