// Regression: the STDLIB web framework must work end-to-end in a browser. // // web/dom lives in src/ now and the module runs in a Worker, so every DOM call // in this file crosses the page<->Worker relay: requests go over a MessagePort // and answers come back through a SharedArrayBuffer. Nothing here can pass by // accident - each step asserts what the document actually holds, and a wrong // answer panics (and a panic is fatal), so a green browser run is a positive // claim about the round trip rather than the absence of an error. // // The second half of the file is the OTHER direction: the page calls INTO the // module. main returns after asking for a timer, and from then on the module is // passive - the page produces a timer tick, a fetch reply, a bounding box and a // resize observation, and the relay delivers each one to code that registered // for it. The chain ends in the PASS line, so a callback that never arrives // leaves the expected output short and phase 5 fails on the difference: without // that, "the callback never fired" would look exactly like success. // // There is no native reference to compare against - natively the wasm-only // packages are panic stubs, so a native run panics by design. The marker below // says so once: phase 0 still compiles it natively (that is what keeps the // stubs compiling into an app) and skips only the run, while phase 5 takes the // browser run against tests/wasm/expected/web_dom_roundtrip.txt as the result. //:wasmtest browser-only package main import ( "web/dom" ) const boxID = "moxie-framework-probe" func assert(ok bool, what string) { if !ok { panic("dom round trip: " | what) } println("ok:", what) } func main() { // Termination: a batch program must still be stoppable, so the entry // handles the channel the runtime closes on SIGINT/SIGTERM. select { case <-shutdown: return default: } // Handle 0 is the body by contract: the page registers document.body there // before any module asks, so an app never has to look it up. body := dom.Body() assert(body == 0, "body is handle 0") // Create, mark, and attach. box := dom.CreateElement("div") assert(box != 0, "create_element returns a handle") dom.SetAttribute(box, "id", boxID) dom.SetTextContent(box, "framework ok") dom.AppendChild(body, box) // Find it back through the document, not through our own handle: that is // the round trip through the JS bridge and back. found := dom.GetElementById(boxID) assert(found != 0, "attached element is in the document") assert(dom.GetAttribute(found, "id") == boxID, "attribute round trip") assert(dom.GetProperty(found, "textContent") == "framework ok", "property round trip") // A selector lookup, which runs on the page against the real document. assert(dom.QuerySelectorFrom(body, "#" | boxID) != 0, "selector finds the element") // A child of its own, then a removal that must actually remove. kid := dom.CreateElement("span") dom.SetTextContent(kid, "child") dom.AppendChild(found, kid) assert(dom.FirstElementChild(found) != 0, "appended child is the first element child") assert(dom.GetProperty(kid, "textContent") == "child", "child text round trip") dom.RemoveChild(found, kid) assert(dom.FirstElementChild(found) == 0, "removed child is detached") // Calls answered on the page that have nothing to do with the tree. assert(dom.EncodeURIComponent("a b&c") == "a%20b%26c", "encode_uri_component round trip") assert(dom.NowSeconds() > 0, "now_seconds comes back as a positive int64") assert(dom.GetViewportWidth() > 0, "viewport width is readable") assert(dom.GetPath() != "", "page path is readable") // The release side: the page drops its references and the document loses the // element. The probe box stays attached - the callbacks below need it. gone := dom.CreateElement("p") dom.SetAttribute(gone, "id", "moxie-gone") dom.AppendChild(body, gone) dom.Remove(gone) assert(dom.GetElementById("moxie-gone") == 0, "removed element left the document") dom.ReleaseAll([]int32{int32(kid), int32(gone)}) // The last act of main. Everything after this is the page calling back into // a module that has already returned. dom.SetTimeout(onTimer, 0) } // The callbacks below must not capture a local - a Moxie closure takes only its // parameters and globals - so each one finds the element again through the // document, which is also a second round trip through the relay. func onTimer() { println("ok: set_timeout callback") dom.FetchText("/jsruntime/dom-ops.mjs", onFetchText) } func onFetchText(text string) { assert(len(text) > 1000, "fetch_text callback carries the file") dom.GetBoundingRect(dom.GetElementById(boxID), onBoundingRect) } func onBoundingRect(l, t, r, b, w, h int32) { assert(w > 0 && h > 0, "get_bounding_rect callback reports a real box") dom.ObserveResize(dom.GetElementById(boxID), dom.RegisterCallback(onResize)) dom.SetStyle(dom.GetElementById(boxID), "width", "40px") } // resizeChain is the idempotence gate. It is a sovereign type because a // package global may not be mutated outside init: the mutation goes through // the method, which is the allowed shape. type resizeChain struct { done bool } func (r *resizeChain) first() (ok bool) { if r.done { return false } r.done = true return true } var resizeChainState resizeChain func onResize() { // The observer is registered for good, so a second layout notification would // run this whole tail again and print the PASS line twice. Measured: it did, // intermittently, even with UnobserveResize first - a notification already // queued still runs - so the chain is made idempotent here. The gate still // requires the callback to arrive at least once, which is what it is for. if !resizeChainState.first() { return } dom.UnobserveResize(dom.GetElementById(boxID)) println("ok: observe_resize callback") dom.RequestAnimationFrame(onFrame) } func onFrame() { println("PASS: stdlib web framework round trip") }