1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! WebAssembly export glue for plugins compiled to `wasm32`.
//!
//! A plugin crate compiled as a `cdylib` calls [`export_plugin!`](crate::export_plugin) to expose
//! the two functions the host's WASM runtime imports: `ty_plugin_alloc` (so the host can write a
//! request into the plugin's linear memory) and `ty_plugin_handle` (which decodes the JSON request,
//! dispatches it through [`Plugin::handle_json`](crate::Plugin::handle_json), and returns the JSON
//! response). The wire shape is JSON in and JSON out, matching `handle_json`, so the same plugin
//! type runs unchanged in-process or across the sandbox boundary.
//!
//! This module only exists on `wasm32` targets; on native targets [`export_plugin!`] expands to
//! nothing, so a plugin crate still compiles for the host (e.g. for its own unit tests).
use cratePlugin;
/// The response returned when a request cannot be decoded or dispatched. Kept in sync with the
/// serialized form of [`PluginResponse::NoChange`](ty_plugin_protocol::PluginResponse::NoChange).
const NO_CHANGE_RESPONSE: &str = "{\"kind\":\"no-change\"}";
/// Reserve `len` bytes in the module's linear memory and return the offset the host should write to.
///
/// The buffer is deliberately leaked: the host reads it back within the same call to [`handle`],
/// and the host discards the whole store afterwards, so there is nothing to reclaim.
/// Decode the JSON request at `[ptr, ptr + len)`, dispatch it, and return a packed
/// `(response_ptr << 32) | response_len` locating the JSON response in linear memory.
///
/// The host upholds the memory contract: `ptr`/`len` describe a region previously returned by
/// [`alloc`] into which it wrote exactly `len` initialized bytes. A request that fails to decode or
/// dispatch degrades to a `no-change` response rather than trapping.
/// Leak an owned byte buffer into linear memory and return its offset.