1 // Regression: a second bundle the page holds a port to.
2 //
3 // A page is not always one module. An app like smesh runs a store worker, a feed
4 // worker, a signer worker - each a Moxie wasm bundle that answers messages from
5 // the page - and each needs the page to hold its Worker to post to it and read
6 // its replies. startBundle gives the page that handle and a MessagePort to it;
7 // the bundle's own bridge receives the port and turns page messages into module
8 // callbacks.
9 //
10 // The exchange is deterministic in both directions: the module posts first (so
11 // the page knows the bundle is up), the page answers that message, and the
12 // module prints what came back. A missing port, a dropped message or a callback
13 // that never fires all leave the expected output short, which the harness diffs.
14 //
15 // It has no native build (a wasm import has nothing to resolve off target), so
16 // it lives with the wasm harness: tests/wasm/emit_check.sh builds it, runs it
17 // through the emitted runtime, and diffs tests/wasm/expected/bundle_port.txt.
18 package main
19 20 import (
21 "unsafe"
22 23 "web/callback"
24 )
25 26 // Not in any stdlib host: these are the bundle's own protocol, answered by
27 // tests/wasm/bundle-port-bridge.mjs.
28 //
29 //:wasmimport bridge bundle_port_on_message
30 func portOnMessage(cbID int32)
31 32 //:wasmimport bridge bundle_port_post
33 func portPost(ptr *byte, length int32, capacity int32)
34 35 func main() {
36 // Termination: a batch program must still be stoppable, so the entry
37 // handles the channel the runtime closes on SIGINT/SIGTERM.
38 select {
39 case <-shutdown:
40 return
41 default:
42 }
43 portOnMessage(callback.RegisterSOnce(onPageMessage))
44 postMessage("hello-from-module")
45 }
46 47 // A wasm import takes a pointer and a length, not a string: the bridge reads the
48 // bytes straight out of the module's memory.
49 func postMessage(s string) {
50 portPost(unsafe.StringData(s), int32(len(s)), int32(len(s)))
51 }
52 53 func onPageMessage(s string) {
54 println("module got:", s)
55 println("PASS: bundle port round trip")
56 }
57