main.mx raw

   1  // Compile-only fixture for the js bridge's mutator spelling (phase 1c).
   2  //
   3  // Set, Delete, SetIndex and Release are package functions whose first argument
   4  // is the handle, not methods. A method would need a pointer receiver to mutate,
   5  // and the dominant spelling in real code is a chain -
   6  // js.Global().Get("x") - which is not addressable, so a pointer receiver could
   7  // not be used there. This fixture pins the shapes that must keep compiling:
   8  // every mutator, and the non-addressable chain as their first argument.
   9  package main
  10  
  11  import "syscall/js"
  12  
  13  func main() {
  14  	// Termination: a batch program must still be stoppable, so the entry
  15  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  16  	select {
  17  	case <-shutdown:
  18  		return
  19  	default:
  20  	}
  21  	obj := js.Global().Get("Object").New()
  22  
  23  	js.Set(obj, "method", js.ValueOfString("GET"))
  24  	js.Delete(obj, "method")
  25  	js.SetIndex(obj, 0, js.ValueOfInt(1))
  26  
  27  	// The shape that motivated package functions: no variable to take the
  28  	// address of, and no temporary either.
  29  	js.Set(js.Global().Get("Object").New(), "x", js.ValueOfInt(1))
  30  	js.SetIndex(js.Global().Get("Array").New(), 0, js.ValueOfInt(2))
  31  
  32  	fn := js.FuncOf(func(this js.Value, args []js.Value) (r js.Value) {
  33  		return js.Undefined()
  34  	})
  35  	js.Set(fn.Value, "name", js.ValueOfString("bridge"))
  36  	js.Release(fn)
  37  }
  38