Skip to content

Commit dfaf3c5

Browse files
Adjust protocol.md
1 parent 8ef2c3a commit dfaf3c5

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

protocol.md

+17-7
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,33 @@ Valid plugins need to import two functions (that will be provided by the runtime
1818

1919
Write the arguments for the current function into the buffer pointed at by `ptr`.
2020

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.
2222

2323
- `(import "typst_env" "wasm_minimal_protocol_send_result_to_host" (func (param i32 i32)))`
2424

2525
The first parameter is a pointer to a buffer (`ptr`), the second is the length of the buffer (`len`).
2626

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).
2828

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.
3030

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
3242

3343
To conform to the protocol, an exported function should:
3444

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`.
3646
- The function should first allocate a buffer `buf` of length `a₁ + a₂ + ⋯ + aₙ`, and call `wasm_minimal_protocol_write_args_to_buffer(buf.ptr)`.
3747
- The `a₁` first bytes of the buffer constitute the first argument, the `a₂` next bytes the second argument, and so on.
3848
- 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

Comments
 (0)