-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmod.rs
More file actions
223 lines (200 loc) · 7.2 KB
/
mod.rs
File metadata and controls
223 lines (200 loc) · 7.2 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
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
use std::{fmt::Display, fs, ops, path::PathBuf};
use aiscript_arena::{Arena, Mutation, Rootable, arena::CollectionPhase};
use sqlx::{PgPool, SqlitePool};
pub use state::State;
use crate::{
ReturnValue, Value,
ai::AiConfig,
ast::ChunkId,
builtins, stdlib,
string::{InternedString, InternedStringSet},
};
use fuel::Fuel;
mod extra;
mod fuel;
mod state;
#[derive(Debug)]
pub enum VmError {
CompileError,
RuntimeError(std::string::String),
}
impl std::error::Error for VmError {}
impl Display for VmError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::CompileError => write!(f, "CompileError"),
Self::RuntimeError(s) => write!(f, "RuntimeError: {s}"),
}
}
}
impl Default for Vm {
fn default() -> Self {
Self::new(None, None, None, AiConfig::default())
}
}
pub struct Vm {
arena: Arena<Rootable![State<'_>]>,
}
impl Vm {
pub fn new(
pg_connection: Option<PgPool>,
sqlite_connection: Option<SqlitePool>,
redis_connection: Option<redis::aio::MultiplexedConnection>,
ai_config: AiConfig,
) -> Self {
let mut vm = Vm {
arena: Arena::<Rootable![State<'_>]>::new(|mc| {
let mut state = State::new(mc);
state.pg_connection = pg_connection;
state.sqlite_connection = sqlite_connection;
state.redis_connection = redis_connection;
state.ai_config = ai_config;
state
}),
};
vm.init_stdlib();
vm
}
pub fn run_file(&mut self, path: PathBuf) {
match fs::read_to_string(&path) {
Ok(source) => {
let source: &'static str = Box::leak(source.into_boxed_str());
if let Err(VmError::CompileError) = self.compile(source) {
std::process::exit(65);
}
if let Err(VmError::RuntimeError(err)) = self.interpret() {
eprintln!("{err}");
std::process::exit(70);
}
}
Err(err) => {
eprintln!("Failed to read file '{}': {}", path.display(), err);
std::process::exit(1);
}
}
}
fn init_stdlib(&mut self) {
self.arena.mutate_root(|_mc, state| {
let ctx = state.get_context();
state.builtin_methods.init(ctx);
state.globals.insert(
ctx.intern(b"ValidationError!"),
Value::Class(builtins::create_validation_error(ctx)),
);
// Initialize standard library modules
state.module_manager.register_native_module(
ctx.intern(b"std.auth.jwt"),
stdlib::create_jwt_module(ctx),
);
state
.module_manager
.register_native_module(ctx.intern(b"std.env"), stdlib::create_env_module(ctx));
state
.module_manager
.register_native_module(ctx.intern(b"std.math"), stdlib::create_math_module(ctx));
state
.module_manager
.register_native_module(ctx.intern(b"std.http"), stdlib::create_http_module(ctx));
state
.module_manager
.register_native_module(ctx.intern(b"std.io"), stdlib::create_io_module(ctx));
state
.module_manager
.register_native_module(ctx.intern(b"std.time"), stdlib::create_time_module(ctx));
state.module_manager.register_native_module(
ctx.intern(b"std.random"),
stdlib::create_random_module(ctx),
);
state
.module_manager
.register_native_module(ctx.intern(b"std.serde"), stdlib::create_serde_module(ctx));
state
.module_manager
.register_native_module(ctx.intern(b"std.db.pg"), stdlib::create_pg_module(ctx));
state.module_manager.register_native_module(
ctx.intern(b"std.db.sqlite"),
stdlib::create_sqlite_module(ctx),
);
state.module_manager.register_native_module(
ctx.intern(b"std.db.redis"),
stdlib::create_redis_module(ctx),
);
});
}
pub fn compile(&mut self, source: &'static str) -> Result<(), VmError> {
self.arena.mutate_root(|_mc, state| {
let context = state.get_context();
state.chunks = crate::compiler::compile(context, source)?;
builtins::define_builtin_functions(state);
// The script function's chunk id is always the highest chunk id.
let script_chunk_id = state.chunks.keys().max().copied().unwrap();
let function = state.get_chunk(script_chunk_id)?;
state.call_function(function, &[])
})
}
pub fn eval_function(
&mut self,
chunk_id: ChunkId,
params: &[serde_json::Value],
) -> Result<ReturnValue, VmError> {
self.arena.mutate_root(|_mc, state| {
let ctx = state.get_context();
let return_value = state.eval_function_with_id(
chunk_id,
¶ms
.iter()
.map(|v| Value::from_serde_value(ctx, v))
.collect::<Vec<_>>(),
)?;
Ok(ReturnValue::from(return_value))
})
}
pub fn interpret(&mut self) -> Result<ReturnValue, VmError> {
loop {
const FUEL_PER_GC: i32 = 1024 * 10;
let mut fuel = Fuel::new(FUEL_PER_GC);
// periodically exit the arena in order to collect garbage concurrently with running the VM.
let result = self.arena.mutate_root(|_, state| state.step(&mut fuel));
const COLLECTOR_GRANULARITY: f64 = 10240.0;
if self.arena.metrics().allocation_debt() > COLLECTOR_GRANULARITY {
// Do garbage collection.
#[cfg(feature = "debug")]
println!("Collecting...");
if self.arena.collection_phase() == CollectionPhase::Sweeping {
self.arena.collect_debt();
} else {
// Immediately transition to `CollectionPhase::Sweeping`.
self.arena.mark_all().unwrap().start_sweeping();
}
}
match result {
Ok(result) => {
if let Some(value) = result {
return Ok(value);
}
}
Err(err) => return Err(err),
}
}
}
}
#[derive(Copy, Clone)]
pub struct Context<'gc> {
pub mutation: &'gc Mutation<'gc>,
pub strings: InternedStringSet<'gc>,
}
impl<'gc> Context<'gc> {
pub fn intern(self, s: &[u8]) -> InternedString<'gc> {
self.strings.intern(&self, s)
}
#[allow(unused)]
pub fn intern_static(self, s: &'static str) -> InternedString<'gc> {
self.strings.intern_static(&self, s.as_bytes())
}
}
impl<'gc> ops::Deref for Context<'gc> {
type Target = Mutation<'gc>;
fn deref(&self) -> &Self::Target {
self.mutation
}
}