You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: protocol.md
+17-7
Original file line number
Diff line number
Diff line change
@@ -18,23 +18,33 @@ Valid plugins need to import two functions (that will be provided by the runtime
18
18
19
19
Write the arguments for the current function into the buffer pointed at by `ptr`.
20
20
21
-
Each function for the protocol receives lengths as its arguments (see [Exported functions](#exported-functions)). The capacity of the buffer pointed at by `ptr` should be at least the sum of all those lengths.
21
+
Each function for the protocol receives lengths as its arguments (see [User-defined functions](#user-defined-functions)). The capacity of the buffer pointed at by `ptr` should be at least the sum of all those lengths.
The first parameter is a pointer to a buffer (`ptr`), the second is the length of the buffer (`len`).
26
26
27
-
Reads`len`bytes pointed at by `ptr`into host memory. The memory pointed at by `ptr` can be freed immediately after this function returns.
27
+
Send`len`and `ptr`to host memory. The buffer must not be freed by the end of the function: it will be freed by the runtime by calling [`wasm_minimal_protocol_send_result_to_host`](#exports).
28
28
29
-
If the message should be interpreted as an error message (see [Exported functions](#exported-functions)), it should be encoded as UTF-8.
29
+
If the message should be interpreted as an error message (see [User-defined functions](#user-defined-functions)), it should be encoded as UTF-8.
30
30
31
-
# Exported functions
31
+
### Note
32
+
33
+
If [`wasm_minimal_protocol_send_result_to_host`](#exports) calls `free` (or a similar routine), be careful that the buffer does not point to static memory.
34
+
35
+
# Exports
36
+
37
+
Valid plugins need to export a function named `wasm_minimal_protocol_send_result_to_host`, that has signature `func (param i32 i32)`.
38
+
39
+
This function will be used by the runtime to free the block of memory returned by a [user-defined](#user-defined-functions) function.
40
+
41
+
# User-defined functions
32
42
33
43
To conform to the protocol, an exported function should:
34
44
35
-
- Take `n` arguments `a₁`, `a₂`, ..., `aₙ` of type `u32` (interpreted as lengths, so `usize/size_t` may be preferable), and return one `i32`.
45
+
- Take `n` arguments `a₁`, `a₂`, ..., `aₙ` of type `u32` (interpreted as lengths, so `usize/size_t` may be preferable), and return one `i32`. We will call the return `return_code`.
36
46
- The function should first allocate a buffer `buf` of length `a₁ + a₂ + ⋯ + aₙ`, and call `wasm_minimal_protocol_write_args_to_buffer(buf.ptr)`.
37
47
- The `a₁` first bytes of the buffer constitute the first argument, the `a₂` next bytes the second argument, and so on.
38
48
- Before returning, the function should call `wasm_minimal_protocol_send_result_to_host` to send its result back to the host.
39
-
- To signal success, the function should return`0`.
40
-
- To signal an error, the function should return `1`. The written buffer is then interpreted as an error message.
49
+
- To signal success, `return_code` must be`0`.
50
+
- To signal an error, `return_code` must be `1`. The sent buffer is then interpreted as an error message, and must be encoded as UTF-8.
0 commit comments