main.mx raw

   1  //:wasmtest skip
   2  // A spawn target reached across a package boundary, and an ordinary function
   3  // that only LOOKS like one.
   4  //
   5  // WHAT THIS PROVES: main imports lib, so main's type checker sees lib's
   6  // signatures as they arrive from lib's export data, where result names are not
   7  // carried. The spawn of lib.Worker therefore depends on the `closed` result
   8  // name surviving that boundary: the compiler writes it on the func line as
   9  // `func (closed) Worker <sig>`, because a name inside the signature descriptor
  10  // would split interface-method matching, and the parenthesised marker can never
  11  // be read as a function identifier. The direct call to lib.Plain depends
  12  // on the absence of that marker: a function returning `(out chan []byte)` is
  13  // not a spawn target and must run rather than die in a prologue.
  14  //
  15  // WHAT IT DOES NOT PROVE: which cache path the build took. A build that
  16  // re-type-checks lib in the same process has the name in hand and would pass
  17  // without the marker; the worker path and any warm build read the .mxh. This
  18  // fixture is the regression guard for the behaviour, and the mxh itself is
  19  // checked directly when the round is documented.
  20  package main
  21  
  22  import (
  23  	"fmt"
  24  	"tests/regressions/should_compile/spawn_closed_crosspkg/lib"
  25  )
  26  
  27  func main() {
  28  	// Termination: a batch program must still be stoppable, so the entry
  29  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  30  	select {
  31  	case <-shutdown:
  32  		return
  33  	default:
  34  	}
  35  
  36  	// An ordinary function with a `chan []byte` result, called directly.
  37  	plain := chan []byte{}
  38  	lib.Plain(plain)
  39  	got := <-plain
  40  	fmt.Println("direct len", len(got))
  41  
  42  	// A spawn target in another package.
  43  	in := chan []byte{}
  44  	out := chan []byte{}
  45  	done := spawn(lib.Worker, in, out)
  46  	in <- []byte("ping")
  47  	msg := <-out
  48  	fmt.Println("echo len", len(msg))
  49  	close(done)
  50  	<-done
  51  	fmt.Println("crosspkg spawn ok")
  52  }
  53