waPC Specification
The waPC protocol standardizes communication and error handling for native code calling into WebAssembly and WebAssembly calling out to native code.
Terms
waPC Hosts
A waPC host is the native environment that initializes and make requests of waPC guests.
waPC Guests
A waPC guest is a WebAssembly module that exposes commands (functions) that a host can call.
Guest call
A request from a host to a guest.
Host call
A request from a guest to a host.
RPC Exchange flow
The following is an outline of which functions are invoked and in which order to support a waPC exchange flow. waPC Host libraries should hide the details of this flow from their users.
- Host invokes
__guest_callon the WebAssembly module. - Guest calls the
__guest_requestfunction to instruct the host to write the request parameters to linear memory. - Guest uses the
op_lenandmsg_lenparameters along with the pointer values it generated in step 2 to retrieve the operation (UTF-8 string) and payload (opaque byte array). - Guest performs work.
- (Optional) Guest invokes
__host_callon host with pointers and lengths indicating thebinding,namespace,operation, and payload. - (Optional) Guest can use
__host_responseandhost_response_lenfunctions to obtain and interpret results. - (Optional) Guest can use
__host_error_lenand__host_errorto obtain the host error if indicated (__host_callreturns 0). - A guest can repeat 5-7 as many times as it needs.
- Guest will call
guest_errorto indicate if an error occurred during processing. - Guest will call
guest_responseto store the opaque response payload. - Guest will return 0 (error) or 1 (success) at the end of
__guest_call.
Required Host Exports
List of functions that must be exported by the host.
| Module | Function | Parameters | Return value | Description |
|---|---|---|---|---|
| wapc | __host_call | bind_ptr:i32bind_len:i32ns_ptr:i32ns_len:i32cmd_ptr:i32cmd_len:i32payload_ptr:i32payload_len:i32 | i32 | Invoked to initiate a host call. |
| wapc | __console_log | ptr:i32len:i32 | Allows guest to log to stdout | |
| wapc | __guest_request | op_ptr:i32ptr:i32 | Writes the guest request payload and operation name to linear memory at the designated locations | |
| wapc | __host_response | ptr:i32 | Instructs host to write the host response payload to the given location in linear memory | |
| wapc | __host_response_len | i32 | Obtains the length of the current host response | |
| wapc | __guest_response | ptr:i32len:i32 | Tells the host the size and location of the current guest response payload | |
| wapc | __guest_error | ptr:i32len:i32 | Tells the host the size and location of the current guest error payload | |
| wapc | __host_error | ptr:i32 | Instructs the host to write the host error payload to the given location | |
| wapc | __host_error_len | i32 | Queries the host for the length of the current host error (0 if none) |
Required Guest Exports
List of functions that must be exported by the guest.
| Function | Parameters | Description |
|---|---|---|
| __guest_call | op_len:i32msg_len:i32 | Invoked by the host to start an RPC exchange with the guest module |