WebAssembly / WASI

WebAssembly System Interface
Other
4.86k stars 253 forks source link

witx: globals in WASI APIs #419

Closed sunfishcode closed 3 years ago

sunfishcode commented 3 years ago

I'm investingating what we'll need to convert wasi-clocks to use handles: https://github.com/WebAssembly/wasi-clocks/issues/4

I think the most straightforward way to do this is to make the clock constants available as exported globals that a program can import, something like this:

;;; A handle providing a clock source.
(typename $clock (handle))

...

  ;;; A clock providing [POSIX time].
  ;;;
  ;;; [POSIX time]: https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xbd_chap04.html#tag_21_04_16
  (@interface global (export "posix") $clock)

  ;;; A clock where each successive timestamp is guaranteed to be at least as
  ;;; great as the previous.
  ;;;
  ;;; This clock starts at a nondeterministic time.
  ;;;
  ;;; This clock should reflect wall-clock elapsed time, except that it may
  ;;; nondeterministically suspend when the program itself is is suspended.
  (@interface global (export "monotonic") $clock)

The witx tooling currently only supports exported functions, so this would require implementing support for exported globals.

@alexcrichton Does this sound like it would fit in the current ABI work? To keep it simple I think we could limit it to only supporting non-mutable handles.

alexcrichton commented 3 years ago

I think this should be technically feasible at the witx layer and others, but I'd be worried about language support for this and compatibility with future interface types. AFAIK C/C++/Rust (aka LLVM) has no way of specifying an import of a custom global? Additionally I'm not sure if globals are expected to be supported with the interface types specification or if interface modules will only have adapter functions and core wasm imports/exports. (cc @lukewagner on the globals-in-interface-types question)

sunfishcode commented 3 years ago

In the absence of globals or exported values, we can use constructor functions with no arguments, which should work fine for now.