-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmod.rs
More file actions
149 lines (135 loc) · 4.33 KB
/
mod.rs
File metadata and controls
149 lines (135 loc) · 4.33 KB
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
use anyhow::Context;
use bytes::Bytes;
use bytestring::ByteString;
use derive_more::Display;
use enum_map::Enum;
use once_cell::sync::OnceCell;
use spacetimedb_lib::bsatn;
use spacetimedb_lib::de::serde::SeedWrapper;
use spacetimedb_lib::de::DeserializeSeed;
use spacetimedb_lib::ProductValue;
use spacetimedb_schema::def::deserialize::ReducerArgsDeserializeSeed;
mod disk_storage;
mod host_controller;
mod module_common;
#[allow(clippy::too_many_arguments)]
pub mod module_host;
pub mod scheduler;
pub mod wasmtime;
// Visible for integration testing.
pub mod instance_env;
pub mod v8; // only pub for testing
mod wasm_common;
pub use disk_storage::DiskStorage;
pub use host_controller::{
extract_schema, DurabilityProvider, ExternalDurability, ExternalStorage, HostController, ProgramStorage,
ReducerCallResult, ReducerOutcome, StartSnapshotWatcher,
};
pub use module_host::{ModuleHost, NoSuchModule, ReducerCallError, UpdateDatabaseResult};
pub use scheduler::Scheduler;
#[derive(Debug)]
pub enum ReducerArgs {
Json(ByteString),
Bsatn(Bytes),
Nullary,
}
impl ReducerArgs {
fn into_tuple(self, seed: ReducerArgsDeserializeSeed) -> Result<ArgsTuple, InvalidReducerArguments> {
self._into_tuple(seed).map_err(|err| InvalidReducerArguments {
err,
reducer: (*seed.reducer_def().name).into(),
})
}
fn _into_tuple(self, seed: ReducerArgsDeserializeSeed) -> anyhow::Result<ArgsTuple> {
Ok(match self {
ReducerArgs::Json(json) => ArgsTuple {
tuple: from_json_seed(&json, SeedWrapper(seed))?,
bsatn: OnceCell::new(),
json: OnceCell::with_value(json),
},
ReducerArgs::Bsatn(bytes) => ArgsTuple {
tuple: seed.deserialize(bsatn::Deserializer::new(&mut &bytes[..]))?,
bsatn: OnceCell::with_value(bytes),
json: OnceCell::new(),
},
ReducerArgs::Nullary => {
anyhow::ensure!(
seed.reducer_def().params.elements.is_empty(),
"failed to typecheck args"
);
ArgsTuple::nullary()
}
})
}
}
#[derive(Debug, Clone)]
pub struct ArgsTuple {
tuple: ProductValue,
bsatn: OnceCell<Bytes>,
json: OnceCell<ByteString>,
}
impl ArgsTuple {
pub fn nullary() -> Self {
ArgsTuple {
tuple: spacetimedb_sats::product![],
bsatn: OnceCell::with_value(Bytes::new()),
json: OnceCell::with_value(ByteString::from_static("[]")),
}
}
pub fn get_bsatn(&self) -> &Bytes {
self.bsatn.get_or_init(|| bsatn::to_vec(&self.tuple).unwrap().into())
}
pub fn get_json(&self) -> &ByteString {
use spacetimedb_sats::ser::serde::SerializeWrapper;
self.json.get_or_init(|| {
serde_json::to_string(SerializeWrapper::from_ref(&self.tuple))
.unwrap()
.into()
})
}
}
impl Default for ArgsTuple {
fn default() -> Self {
Self::nullary()
}
}
// TODO(noa): replace imports from this module with imports straight from primitives.
pub use spacetimedb_primitives::ReducerId;
#[derive(thiserror::Error, Debug)]
#[error("invalid arguments for reducer {reducer}: {err}")]
pub struct InvalidReducerArguments {
#[source]
err: anyhow::Error,
reducer: Box<str>,
}
fn from_json_seed<'de, T: serde::de::DeserializeSeed<'de>>(s: &'de str, seed: T) -> anyhow::Result<T::Value> {
let mut de = serde_json::Deserializer::from_str(s);
let mut track = serde_path_to_error::Track::new();
let out = seed
.deserialize(serde_path_to_error::Deserializer::new(&mut de, &mut track))
.context(track.path())?;
de.end()?;
Ok(out)
}
/// Tags for each call that a `WasmInstanceEnv` can make.
#[derive(Debug, Display, Enum, Clone, Copy, strum::AsRefStr)]
pub enum AbiCall {
TableIdFromName,
IndexIdFromName,
DatastoreTableRowCount,
DatastoreTableScanBsatn,
DatastoreIndexScanRangeBsatn,
RowIterBsatnAdvance,
RowIterBsatnClose,
DatastoreInsertBsatn,
DatastoreUpdateBsatn,
DatastoreDeleteByIndexScanRangeBsatn,
DatastoreDeleteAllByEqBsatn,
BytesSourceRead,
BytesSinkWrite,
ConsoleLog,
ConsoleTimerStart,
ConsoleTimerEnd,
Identity,
VolatileNonatomicScheduleImmediate,
}