1#![cfg_attr(not(feature = "std"), no_std)]
22
23#[cfg(feature = "std")]
24pub use gear_sandbox_host::sandbox::{SandboxBackend, env::Instantiate};
25use sp_runtime_interface::{Pointer, runtime_interface};
26use sp_wasm_interface::HostPointer;
27
28#[cfg(feature = "std")]
29pub mod detail;
30
31#[cfg(feature = "std")]
32pub use detail::init;
33
34#[runtime_interface(wasm_only)]
36pub trait Sandbox {
37 fn instantiate(
39 &mut self,
40 dispatch_thunk_id: u32,
41 wasm_code: &[u8],
42 raw_env_def: &[u8],
43 state_ptr: Pointer<u8>,
44 ) -> u32 {
45 detail::instantiate(
46 *self,
47 dispatch_thunk_id,
48 wasm_code,
49 raw_env_def,
50 state_ptr,
51 Instantiate::Version1,
52 )
53 }
54
55 #[version(2)]
57 fn instantiate(
58 &mut self,
59 dispatch_thunk_id: u32,
60 wasm_code: &[u8],
61 raw_env_def: &[u8],
62 state_ptr: Pointer<u8>,
63 ) -> u32 {
64 detail::instantiate(
65 *self,
66 dispatch_thunk_id,
67 wasm_code,
68 raw_env_def,
69 state_ptr,
70 Instantiate::Version2,
71 )
72 }
73
74 fn invoke(
76 &mut self,
77 instance_idx: u32,
78 function: &str,
79 args: &[u8],
80 return_val_ptr: Pointer<u8>,
81 return_val_len: u32,
82 state_ptr: Pointer<u8>,
83 ) -> u32 {
84 detail::invoke(
85 *self,
86 instance_idx,
87 function,
88 args,
89 return_val_ptr,
90 return_val_len,
91 state_ptr,
92 )
93 }
94
95 fn memory_new(&mut self, initial: u32, maximum: u32) -> u32 {
97 detail::memory_new(*self, initial, maximum)
98 }
99
100 fn memory_get(
102 &mut self,
103 memory_idx: u32,
104 offset: u32,
105 buf_ptr: Pointer<u8>,
106 buf_len: u32,
107 ) -> u32 {
108 detail::memory_get(*self, memory_idx, offset, buf_ptr, buf_len)
109 }
110
111 fn memory_set(
113 &mut self,
114 memory_idx: u32,
115 offset: u32,
116 val_ptr: Pointer<u8>,
117 val_len: u32,
118 ) -> u32 {
119 detail::memory_set(*self, memory_idx, offset, val_ptr, val_len)
120 }
121
122 fn memory_teardown(&mut self, memory_idx: u32) {
124 detail::memory_teardown(*self, memory_idx)
125 }
126
127 fn instance_teardown(&mut self, instance_idx: u32) {
129 detail::instance_teardown(*self, instance_idx)
130 }
131
132 fn get_global_val(
137 &mut self,
138 instance_idx: u32,
139 name: &str,
140 ) -> Option<sp_wasm_interface::Value> {
141 detail::get_global_val(*self, instance_idx, name)
142 }
143
144 fn set_global_val(
147 &mut self,
148 instance_idx: u32,
149 name: &str,
150 value: sp_wasm_interface::Value,
151 ) -> u32 {
152 detail::set_global_val(*self, instance_idx, name, value)
153 }
154
155 fn memory_grow(&mut self, memory_idx: u32, size: u32) -> u32 {
156 detail::memory_grow(*self, memory_idx, size)
157 }
158
159 fn memory_size(&mut self, memory_idx: u32) -> u32 {
160 detail::memory_size(*self, memory_idx)
161 }
162
163 fn get_buff(&mut self, memory_idx: u32) -> HostPointer {
164 detail::get_buff(*self, memory_idx)
165 }
166
167 fn get_instance_ptr(&mut self, instance_id: u32) -> HostPointer {
168 detail::get_instance_ptr(*self, instance_id)
169 }
170}