-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwasm_config.rs
385 lines (345 loc) · 12 KB
/
wasm_config.rs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#[cfg(feature = "wasi")]
use std::collections::HashMap;
use std::fmt::{Debug, Formatter, Result as FmtResult};
use godot::prelude::*;
use tracing::warn;
use crate::godot_util::to_lower_inline_smol_str;
#[cfg(feature = "epoch-timeout")]
use crate::variant_dispatch;
#[cfg(feature = "wasi")]
use crate::wasi_ctx::WasiContext;
#[cfg(feature = "epoch-timeout")]
use crate::wasm_util::{EPOCH_DEADLINE, EPOCH_MULTIPLIER};
#[derive(Default)]
pub struct Config {
#[cfg(feature = "epoch-timeout")]
pub with_epoch: bool,
#[cfg(feature = "epoch-timeout")]
pub epoch_autoreset: bool,
#[cfg(feature = "epoch-timeout")]
pub epoch_timeout: u64,
#[cfg(feature = "memory-limiter")]
pub max_memory: Option<u64>,
#[cfg(feature = "memory-limiter")]
pub max_entries: Option<u64>,
#[cfg(feature = "wasi")]
pub with_wasi: bool,
#[cfg(feature = "wasi")]
pub wasi_context: Option<Gd<WasiContext>>,
#[cfg(feature = "wasi")]
pub wasi_args: Vec<String>,
#[cfg(feature = "wasi")]
pub wasi_envs: HashMap<String, String>,
#[cfg(feature = "wasi")]
pub wasi_fs_readonly: bool,
#[cfg(feature = "wasi")]
pub wasi_stdin: PipeBindingType,
#[cfg(feature = "wasi")]
pub wasi_stdout: PipeBindingType,
#[cfg(feature = "wasi")]
pub wasi_stderr: PipeBindingType,
#[cfg(feature = "wasi")]
pub wasi_stdout_buffer: PipeBufferType,
#[cfg(feature = "wasi")]
pub wasi_stderr_buffer: PipeBufferType,
#[cfg(feature = "wasi")]
pub wasi_stdin_data: Option<PackedByteArray>,
//#[cfg(feature = "wasi")]
//pub wasi_stdin_file: Option<String>,
// Not worth cfg() it
#[allow(dead_code)]
pub extern_bind: ExternBindingType,
}
impl Debug for Config {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
let mut f = f.debug_struct("Config");
#[cfg(feature = "epoch-timeout")]
f.field("with_epoch", &self.with_epoch);
#[cfg(feature = "epoch-timeout")]
f.field("epoch_autoreset", &self.epoch_autoreset);
#[cfg(feature = "epoch-timeout")]
f.field("epoch_timeout", &self.epoch_timeout);
#[cfg(feature = "memory-limiter")]
f.field("max_memory", &self.max_memory);
#[cfg(feature = "memory-limiter")]
f.field("max_entries", &self.max_entries);
#[cfg(feature = "wasi")]
f.field("with_wasi", &self.with_wasi);
#[cfg(feature = "wasi")]
f.field("wasi_context", &self.wasi_context);
#[cfg(feature = "wasi")]
f.field("wasi_args", &self.wasi_args);
#[cfg(feature = "wasi")]
f.field("wasi_fs_readonly", &self.wasi_fs_readonly);
#[cfg(feature = "wasi")]
f.field("wasi_stdin", &self.wasi_stdin);
#[cfg(feature = "wasi")]
f.field("wasi_stdout", &self.wasi_stdout);
#[cfg(feature = "wasi")]
f.field("wasi_stderr", &self.wasi_stderr);
#[cfg(feature = "wasi")]
f.field("wasi_stdout_buffer", &self.wasi_stdout_buffer);
#[cfg(feature = "wasi")]
f.field("wasi_stderr_buffer", &self.wasi_stderr_buffer);
#[cfg(feature = "wasi")]
f.field(
"wasi_stdin_data_len",
&self.wasi_stdin_data.as_ref().map(|v| v.len()),
);
f.field("extern_bind", &self.extern_bind);
f.finish_non_exhaustive()
}
}
fn get_field<T: FromGodot>(
d: &Dictionary,
names: impl IntoIterator<Item = &'static str>,
) -> Result<Option<T>, ConvertError> {
for name in names {
if let Some(v) = d.get(name) {
return Some(v.try_to()).transpose();
}
}
Ok(None)
}
#[cfg(feature = "epoch-timeout")]
fn compute_epoch(v: Option<Variant>) -> Result<u64, ConvertError> {
const DEFAULT: u64 = EPOCH_DEADLINE.saturating_mul(EPOCH_MULTIPLIER);
Ok(match v {
None => DEFAULT,
Some(v) => variant_dispatch!(v {
NIL => DEFAULT,
INT => (v as u64).saturating_mul(EPOCH_MULTIPLIER),
FLOAT => (v * (EPOCH_MULTIPLIER as f64)).trunc() as _,
_ => return Err(ConvertError::with_error_value("Unknown value", v)),
}),
}
.max(1))
}
#[cfg(feature = "wasi")]
fn get_wasi_args(v: Option<Variant>) -> Result<Vec<String>, ConvertError> {
let v = match v {
Some(v) => v.try_to::<VariantArray>()?,
None => return Ok(Vec::new()),
};
let mut ret = Vec::with_capacity(v.len());
for i in v.iter_shared() {
ret.push(i.try_to::<String>()?);
}
Ok(ret)
}
#[cfg(feature = "wasi")]
fn get_wasi_envs(v: Option<Variant>) -> Result<HashMap<String, String>, ConvertError> {
let v = match v {
Some(v) => v.try_to::<Dictionary>()?,
None => return Ok(HashMap::new()),
};
let mut ret = HashMap::with_capacity(v.len());
for (k, v) in v.iter_shared() {
ret.insert(k.try_to::<String>()?, v.try_to::<String>()?);
}
Ok(ret)
}
impl Config {
fn convert(dict: Dictionary) -> Result<Self, ConvertError> {
Ok(Self {
#[cfg(feature = "epoch-timeout")]
with_epoch: get_field(&dict, ["epoch.enable", "engine.use_epoch"])?.unwrap_or_default(),
#[cfg(feature = "epoch-timeout")]
epoch_autoreset: get_field(&dict, ["epoch.useAutoreset", "engine.epoch_autoreset"])?
.unwrap_or_default(),
#[cfg(feature = "epoch-timeout")]
epoch_timeout: compute_epoch(
dict.get("epoch.timeout")
.or_else(|| dict.get("engine.epoch_timeout")),
)?,
#[cfg(feature = "memory-limiter")]
max_memory: get_field::<i64>(&dict, ["memory.maxGrowBytes", "engine.max_memory"])?
.map(|v| v as _),
#[cfg(feature = "memory-limiter")]
max_entries: get_field::<i64>(&dict, ["table.maxGrowEntries", "engine.max_entries"])?
.map(|v| v as _),
#[cfg(feature = "wasi")]
with_wasi: get_field(&dict, ["wasi.enable", "engine.use_wasi"])?.unwrap_or_default(),
#[cfg(feature = "wasi")]
wasi_context: get_field(&dict, ["wasi.context", "wasi.wasi_context"])?,
#[cfg(feature = "wasi")]
wasi_args: get_wasi_args(dict.get("wasi.args"))?,
#[cfg(feature = "wasi")]
wasi_envs: get_wasi_envs(dict.get("wasi.envs"))?,
#[cfg(feature = "wasi")]
wasi_fs_readonly: get_field(&dict, ["wasi.fsReadonly", "wasi.fs_readonly"])?
.unwrap_or_default(),
#[cfg(feature = "wasi")]
wasi_stdin: get_field::<PipeBindingType>(&dict, ["wasi.stdin.bindMode", "wasi.stdin"])?
.inspect(|&v| {
if let PipeBindingType::Bypass | PipeBindingType::Context = v {
warn!(binding = ?v, "Stdin binding type is unsupported.");
godot_warn!("Stdin binding type {v:?} is unsupported.");
}
})
.unwrap_or(PipeBindingType::Unbound),
#[cfg(feature = "wasi")]
wasi_stdout: get_field(&dict, ["wasi.stdout.bindMode", "wasi.stdout"])?
.unwrap_or_default(),
#[cfg(feature = "wasi")]
wasi_stderr: get_field(&dict, ["wasi.stderr.bindMode", "wasi.stderr"])?
.unwrap_or_default(),
#[cfg(feature = "wasi")]
wasi_stdout_buffer: get_field(&dict, ["wasi.stdout.bufferMode", "wasi.stdout_buffer"])?
.unwrap_or_default(),
#[cfg(feature = "wasi")]
wasi_stderr_buffer: get_field(&dict, ["wasi.stderr.bufferMode", "wasi.stderr_buffer"])?
.unwrap_or_default(),
#[cfg(feature = "wasi")]
wasi_stdin_data: get_field(&dict, ["wasi.stdin.inputData", "wasi.stdin_data"])?,
//#[cfg(feature = "wasi")]
//wasi_stdin_file: get_field(&dict, ["wasi.stdin.inputFile", "wasi.stdin_file"])?,
extern_bind: get_field(&dict, ["extern.bindMode", "godot.extern_binding"])?
.unwrap_or_default(),
})
}
}
impl GodotConvert for Config {
type Via = Dictionary;
}
impl FromGodot for Config {
fn try_from_variant(v: &Variant) -> Result<Self, ConvertError> {
if v.is_nil() {
return Ok(Self::default());
}
Self::convert(v.try_to()?)
}
fn try_from_godot(via: Self::Via) -> Result<Self, ConvertError> {
Self::convert(via)
}
fn from_godot(via: Self::Via) -> Self {
Self::convert(via).unwrap()
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ExternBindingType {
None,
#[cfg(feature = "object-registry-compat")]
Registry,
#[cfg(feature = "object-registry-extern")]
Native,
}
impl Default for ExternBindingType {
fn default() -> Self {
Self::None
}
}
impl GodotConvert for ExternBindingType {
type Via = GString;
}
impl FromGodot for ExternBindingType {
fn try_from_godot(via: Self::Via) -> Result<Self, ConvertError> {
Ok(match to_lower_inline_smol_str(via.chars()).as_deref() {
Some("" | "none" | "no_binding") => Self::None,
#[cfg(feature = "object-registry-compat")]
Some("compat" | "registry") => Self::Registry,
#[cfg(feature = "object-registry-extern")]
Some("extern" | "native") => Self::Native,
_ => return Err(ConvertError::with_error_value("Unknown value", via)),
})
}
}
impl ToGodot for ExternBindingType {
type ToVia<'a> = Self::Via;
fn to_godot(&self) -> Self::ToVia<'_> {
match self {
Self::None => "none",
#[cfg(feature = "object-registry-compat")]
Self::Registry => "registry",
#[cfg(feature = "object-registry-extern")]
Self::Native => "native",
}
.into()
}
}
#[cfg(feature = "wasi")]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum PipeBindingType {
Unbound,
Bypass,
Instance,
Context,
}
#[cfg(feature = "wasi")]
impl Default for PipeBindingType {
fn default() -> Self {
Self::Context
}
}
#[cfg(feature = "wasi")]
impl GodotConvert for PipeBindingType {
type Via = GString;
}
#[cfg(feature = "wasi")]
impl FromGodot for PipeBindingType {
fn try_from_godot(via: Self::Via) -> Result<Self, ConvertError> {
Ok(match to_lower_inline_smol_str(via.chars()).as_deref() {
Some("" | "unbound") => Self::Unbound,
Some("bypass") => Self::Bypass,
Some("instance") => Self::Instance,
Some("context") => Self::Context,
_ => return Err(ConvertError::with_error_value("Unknown value", via)),
})
}
}
#[cfg(feature = "wasi")]
impl ToGodot for PipeBindingType {
type ToVia<'a> = Self::Via;
fn to_godot(&self) -> Self::ToVia<'_> {
match self {
Self::Unbound => "unbound",
Self::Bypass => "bypass",
Self::Instance => "instance",
Self::Context => "context",
}
.into()
}
}
#[cfg(feature = "wasi")]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum PipeBufferType {
Unbuffered,
LineBuffer,
BlockBuffer,
}
#[cfg(feature = "wasi")]
impl Default for PipeBufferType {
fn default() -> Self {
Self::LineBuffer
}
}
#[cfg(feature = "wasi")]
impl GodotConvert for PipeBufferType {
type Via = GString;
}
#[cfg(feature = "wasi")]
impl FromGodot for PipeBufferType {
fn try_from_godot(via: Self::Via) -> Result<Self, ConvertError> {
Ok(match to_lower_inline_smol_str(via.chars()).as_deref() {
Some("" | "unbuffered") => Self::Unbuffered,
Some("line") => Self::LineBuffer,
Some("block") => Self::BlockBuffer,
_ => return Err(ConvertError::with_error_value("Unknown variant", via)),
})
}
}
#[cfg(feature = "wasi")]
impl ToGodot for PipeBufferType {
type ToVia<'a> = Self::Via;
fn to_godot(&self) -> Self::ToVia<'_> {
match self {
Self::Unbuffered => "unbuffered",
Self::LineBuffer => "line",
Self::BlockBuffer => "block",
}
.into()
}
}