1 // Regression: the STDLIB web framework must work end-to-end in a browser.
2 //
3 // web/dom lives in src/ now and the module runs in a Worker, so every DOM call
4 // in this file crosses the page<->Worker relay: requests go over a MessagePort
5 // and answers come back through a SharedArrayBuffer. Nothing here can pass by
6 // accident - each step asserts what the document actually holds, and a wrong
7 // answer panics (and a panic is fatal), so a green browser run is a positive
8 // claim about the round trip rather than the absence of an error.
9 //
10 // The second half of the file is the OTHER direction: the page calls INTO the
11 // module. main returns after asking for a timer, and from then on the module is
12 // passive - the page produces a timer tick, a fetch reply, a bounding box and a
13 // resize observation, and the relay delivers each one to code that registered
14 // for it. The chain ends in the PASS line, so a callback that never arrives
15 // leaves the expected output short and phase 5 fails on the difference: without
16 // that, "the callback never fired" would look exactly like success.
17 //
18 // There is no native reference to compare against - natively the wasm-only
19 // packages are panic stubs, so a native run panics by design. The marker below
20 // says so once: phase 0 still compiles it natively (that is what keeps the
21 // stubs compiling into an app) and skips only the run, while phase 5 takes the
22 // browser run against tests/wasm/expected/web_dom_roundtrip.txt as the result.
23 //:wasmtest browser-only
24 package main
25 26 import (
27 "web/dom"
28 )
29 30 const boxID = "moxie-framework-probe"
31 32 func assert(ok bool, what string) {
33 if !ok {
34 panic("dom round trip: " | what)
35 }
36 println("ok:", what)
37 }
38 39 func main() {
40 // Termination: a batch program must still be stoppable, so the entry
41 // handles the channel the runtime closes on SIGINT/SIGTERM.
42 select {
43 case <-shutdown:
44 return
45 default:
46 }
47 48 // Handle 0 is the body by contract: the page registers document.body there
49 // before any module asks, so an app never has to look it up.
50 body := dom.Body()
51 assert(body == 0, "body is handle 0")
52 53 // Create, mark, and attach.
54 box := dom.CreateElement("div")
55 assert(box != 0, "create_element returns a handle")
56 dom.SetAttribute(box, "id", boxID)
57 dom.SetTextContent(box, "framework ok")
58 dom.AppendChild(body, box)
59 60 // Find it back through the document, not through our own handle: that is
61 // the round trip through the JS bridge and back.
62 found := dom.GetElementById(boxID)
63 assert(found != 0, "attached element is in the document")
64 assert(dom.GetAttribute(found, "id") == boxID, "attribute round trip")
65 assert(dom.GetProperty(found, "textContent") == "framework ok", "property round trip")
66 67 // A selector lookup, which runs on the page against the real document.
68 assert(dom.QuerySelectorFrom(body, "#" | boxID) != 0, "selector finds the element")
69 70 // A child of its own, then a removal that must actually remove.
71 kid := dom.CreateElement("span")
72 dom.SetTextContent(kid, "child")
73 dom.AppendChild(found, kid)
74 assert(dom.FirstElementChild(found) != 0, "appended child is the first element child")
75 assert(dom.GetProperty(kid, "textContent") == "child", "child text round trip")
76 dom.RemoveChild(found, kid)
77 assert(dom.FirstElementChild(found) == 0, "removed child is detached")
78 79 // Calls answered on the page that have nothing to do with the tree.
80 assert(dom.EncodeURIComponent("a b&c") == "a%20b%26c", "encode_uri_component round trip")
81 assert(dom.NowSeconds() > 0, "now_seconds comes back as a positive int64")
82 assert(dom.GetViewportWidth() > 0, "viewport width is readable")
83 assert(dom.GetPath() != "", "page path is readable")
84 85 // The release side: the page drops its references and the document loses the
86 // element. The probe box stays attached - the callbacks below need it.
87 gone := dom.CreateElement("p")
88 dom.SetAttribute(gone, "id", "moxie-gone")
89 dom.AppendChild(body, gone)
90 dom.Remove(gone)
91 assert(dom.GetElementById("moxie-gone") == 0, "removed element left the document")
92 dom.ReleaseAll([]int32{int32(kid), int32(gone)})
93 94 // The last act of main. Everything after this is the page calling back into
95 // a module that has already returned.
96 dom.SetTimeout(onTimer, 0)
97 }
98 99 // The callbacks below must not capture a local - a Moxie closure takes only its
100 // parameters and globals - so each one finds the element again through the
101 // document, which is also a second round trip through the relay.
102 103 func onTimer() {
104 println("ok: set_timeout callback")
105 dom.FetchText("/jsruntime/dom-ops.mjs", onFetchText)
106 }
107 108 func onFetchText(text string) {
109 assert(len(text) > 1000, "fetch_text callback carries the file")
110 dom.GetBoundingRect(dom.GetElementById(boxID), onBoundingRect)
111 }
112 113 func onBoundingRect(l, t, r, b, w, h int32) {
114 assert(w > 0 && h > 0, "get_bounding_rect callback reports a real box")
115 dom.ObserveResize(dom.GetElementById(boxID), dom.RegisterCallback(onResize))
116 dom.SetStyle(dom.GetElementById(boxID), "width", "40px")
117 }
118 119 // resizeChain is the idempotence gate. It is a sovereign type because a
120 // package global may not be mutated outside init: the mutation goes through
121 // the method, which is the allowed shape.
122 type resizeChain struct {
123 done bool
124 }
125 126 func (r *resizeChain) first() (ok bool) {
127 if r.done {
128 return false
129 }
130 r.done = true
131 return true
132 }
133 134 var resizeChainState resizeChain
135 136 func onResize() {
137 // The observer is registered for good, so a second layout notification would
138 // run this whole tail again and print the PASS line twice. Measured: it did,
139 // intermittently, even with UnobserveResize first - a notification already
140 // queued still runs - so the chain is made idempotent here. The gate still
141 // requires the callback to arrive at least once, which is what it is for.
142 if !resizeChainState.first() {
143 return
144 }
145 dom.UnobserveResize(dom.GetElementById(boxID))
146 println("ok: observe_resize callback")
147 dom.RequestAnimationFrame(onFrame)
148 }
149 150 func onFrame() {
151 println("PASS: stdlib web framework round trip")
152 }
153