Skip to content

Commit edcb07c

Browse files
authored
Add V8Runtime skeleton + HostType::Js (#2923)
1 parent 4dcf5f8 commit edcb07c

9 files changed

Lines changed: 133 additions & 14 deletions

File tree

crates/core/src/db/datastore/system_tables.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,12 +816,15 @@ pub struct ModuleKind(u8);
816816
impl ModuleKind {
817817
/// The [`ModuleKind`] of WASM-based modules.
818818
pub const WASM: ModuleKind = ModuleKind(0);
819+
/// The [`ModuleKind`] of JS modules.
820+
pub const JS: ModuleKind = ModuleKind(1);
819821
}
820822

821823
impl From<crate::messages::control_db::HostType> for ModuleKind {
822824
fn from(host_type: crate::messages::control_db::HostType) -> Self {
823825
match host_type {
824826
crate::messages::control_db::HostType::Wasm => Self::WASM,
827+
crate::messages::control_db::HostType::Js => Self::JS,
825828
}
826829
}
827830
}

crates/core/src/host/host_controller.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use crate::db::db_metrics::DB_METRICS;
99
use crate::db::relational_db::{self, DiskSizeFn, RelationalDB, Txdata};
1010
use crate::db::{self, spawn_tx_metrics_recorder};
1111
use crate::energy::{EnergyMonitor, EnergyQuanta, NullEnergyMonitor};
12+
use crate::host::module_host::ModuleRuntime as _;
13+
use crate::host::v8::V8Runtime;
1214
use crate::messages::control_db::{Database, HostType};
1315
use crate::module_host_context::ModuleCreationContext;
1416
use crate::replica_context::ReplicaContext;
@@ -106,12 +108,14 @@ pub struct HostController {
106108

107109
struct HostRuntimes {
108110
wasmtime: WasmtimeRuntime,
111+
v8: V8Runtime,
109112
}
110113

111114
impl HostRuntimes {
112115
fn new(data_dir: Option<&ServerDataDir>) -> Arc<Self> {
113116
let wasmtime = WasmtimeRuntime::new(data_dir);
114-
Arc::new(Self { wasmtime })
117+
let v8 = V8Runtime::default();
118+
Arc::new(Self { wasmtime, v8 })
115119
}
116120
}
117121

@@ -671,19 +675,25 @@ async fn make_module_host(
671675
// threads, but those aren't for computation. Also, wasmtime uses rayon
672676
// to run compilation in parallel, so it'll need to run stuff in rayon anyway.
673677
asyncify(move || {
678+
let mcc = ModuleCreationContext {
679+
replica_ctx,
680+
scheduler,
681+
program: &program,
682+
energy_monitor,
683+
};
684+
685+
let start = Instant::now();
674686
let module_host = match host_type {
675687
HostType::Wasm => {
676-
let mcc = ModuleCreationContext {
677-
replica_ctx,
678-
scheduler,
679-
program: &program,
680-
energy_monitor,
681-
};
682-
let start = Instant::now();
683688
let actor = runtimes.wasmtime.make_actor(mcc)?;
684689
trace!("wasmtime::make_actor blocked for {:?}", start.elapsed());
685690
ModuleHost::new(actor, unregister, core)
686691
}
692+
HostType::Js => {
693+
let actor = runtimes.v8.make_actor(mcc)?;
694+
trace!("v8::make_actor blocked for {:?}", start.elapsed());
695+
ModuleHost::new(actor, unregister, core)
696+
}
687697
};
688698
Ok((program, module_host))
689699
})

crates/core/src/host/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ mod host_controller;
1616
pub mod module_host;
1717
pub mod scheduler;
1818
pub mod wasmtime;
19+
1920
// Visible for integration testing.
2021
pub mod instance_env;
22+
pub mod v8; // only pub for testing
2123
mod wasm_common;
2224

2325
pub use disk_storage::DiskStorage;

crates/core/src/host/module_host.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::execution_context::{ExecutionContext, ReducerContext, Workload, Workl
1212
use crate::hash::Hash;
1313
use crate::identity::Identity;
1414
use crate::messages::control_db::Database;
15+
use crate::module_host_context::ModuleCreationContext;
1516
use crate::replica_context::ReplicaContext;
1617
use crate::sql::ast::SchemaViewer;
1718
use crate::sql::parser::RowLevelExpr;
@@ -306,6 +307,12 @@ impl ReducersMap {
306307
}
307308
}
308309

310+
/// A runtime that can create modules.
311+
pub trait ModuleRuntime {
312+
/// Creates a module based on the context `mcc`.
313+
fn make_actor(&self, mcc: ModuleCreationContext<'_>) -> anyhow::Result<impl Module>;
314+
}
315+
309316
pub trait DynModule: Send + Sync + 'static {
310317
fn replica_ctx(&self) -> &Arc<ReplicaContext>;
311318
fn scheduler(&self) -> &Scheduler;

crates/core/src/host/v8/mod.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
use crate::{
2+
db::datastore::locking_tx_datastore::MutTxId,
3+
host::{
4+
module_host::{DynModule, Module, ModuleInfo, ModuleInstance, ModuleRuntime},
5+
Scheduler,
6+
},
7+
module_host_context::ModuleCreationContext,
8+
replica_context::ReplicaContext,
9+
};
10+
use anyhow::anyhow;
11+
use std::sync::{Arc, LazyLock};
12+
13+
use super::module_host::CallReducerParams;
14+
15+
/// The V8 runtime, for modules written in e.g., JS or TypeScript.
16+
#[derive(Default)]
17+
pub struct V8Runtime {
18+
_priv: (),
19+
}
20+
21+
impl ModuleRuntime for V8Runtime {
22+
fn make_actor(&self, mcc: ModuleCreationContext<'_>) -> anyhow::Result<impl Module> {
23+
V8_RUNTIME_GLOBAL.make_actor(mcc)
24+
}
25+
}
26+
27+
static V8_RUNTIME_GLOBAL: LazyLock<V8RuntimeInner> = LazyLock::new(V8RuntimeInner::new);
28+
29+
/// The actual V8 runtime, with initialization of V8.
30+
struct V8RuntimeInner {
31+
_priv: (),
32+
}
33+
34+
impl V8RuntimeInner {
35+
#[allow(clippy::new_without_default)]
36+
const fn new() -> Self {
37+
// TODO: actually setup V8.
38+
39+
Self { _priv: () }
40+
}
41+
42+
fn make_actor(&self, _: ModuleCreationContext<'_>) -> anyhow::Result<impl Module> {
43+
Err::<JsModule, _>(anyhow!("v8_todo"))
44+
}
45+
}
46+
47+
#[derive(Clone)]
48+
struct JsModule;
49+
50+
impl DynModule for JsModule {
51+
fn replica_ctx(&self) -> &Arc<ReplicaContext> {
52+
todo!()
53+
}
54+
55+
fn scheduler(&self) -> &Scheduler {
56+
todo!()
57+
}
58+
}
59+
60+
impl Module for JsModule {
61+
type Instance = JsInstance;
62+
63+
type InitialInstances<'a> = std::iter::Empty<JsInstance>;
64+
65+
fn initial_instances(&mut self) -> Self::InitialInstances<'_> {
66+
std::iter::empty()
67+
}
68+
69+
fn info(&self) -> Arc<ModuleInfo> {
70+
todo!()
71+
}
72+
73+
fn create_instance(&self) -> Self::Instance {
74+
todo!()
75+
}
76+
}
77+
78+
struct JsInstance;
79+
80+
impl ModuleInstance for JsInstance {
81+
fn trapped(&self) -> bool {
82+
todo!()
83+
}
84+
85+
fn update_database(
86+
&mut self,
87+
_program: crate::db::datastore::traits::Program,
88+
_old_module_info: Arc<ModuleInfo>,
89+
) -> anyhow::Result<super::UpdateDatabaseResult> {
90+
todo!()
91+
}
92+
93+
fn call_reducer(&mut self, _tx: Option<MutTxId>, _params: CallReducerParams) -> super::ReducerCallResult {
94+
todo!()
95+
}
96+
}

crates/core/src/host/wasm_common/module_host_actor.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ pub enum DescribeError {
122122
Decode(#[from] DecodeError),
123123
#[error(transparent)]
124124
RuntimeError(anyhow::Error),
125-
#[error("unimplemented RawModuleDef version")]
126-
UnimplementedRawModuleDefVersion,
127125
}
128126

129127
impl<T: WasmModule> WasmModuleHostActor<T> {

crates/core/src/host/wasmtime/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use wasmtime::{Engine, Linker, Module, StoreContext, StoreContextMut};
77

88
use crate::energy::{EnergyQuanta, ReducerBudget};
99
use crate::error::NodesError;
10+
use crate::host::module_host::ModuleRuntime;
1011
use crate::module_host_context::ModuleCreationContext;
1112

1213
mod wasm_instance_env;
@@ -80,11 +81,10 @@ impl WasmtimeRuntime {
8081
config.cache_config_load(tmpfile.path())?;
8182
Ok(())
8283
}
84+
}
8385

84-
pub fn make_actor(
85-
&self,
86-
mcc: ModuleCreationContext,
87-
) -> Result<impl super::module_host::Module, ModuleCreationError> {
86+
impl ModuleRuntime for WasmtimeRuntime {
87+
fn make_actor(&self, mcc: ModuleCreationContext) -> anyhow::Result<impl super::module_host::Module> {
8888
let module = Module::new(&self.engine, &mcc.program.bytes).map_err(ModuleCreationError::WasmCompileError)?;
8989

9090
let func_imports = module

crates/core/src/messages/control_db.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,5 @@ pub struct NodeStatus {
7474
#[repr(i32)]
7575
pub enum HostType {
7676
Wasm = 0,
77+
Js,
7778
}

crates/standalone/src/subcommands/extract_schema.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ struct Args {
2222
#[derive(clap::ValueEnum, Copy, Clone)]
2323
enum HostType {
2424
Wasm,
25+
Js,
2526
}
2627

2728
impl From<HostType> for control_db::HostType {
2829
fn from(x: HostType) -> Self {
2930
match x {
3031
HostType::Wasm => control_db::HostType::Wasm,
32+
HostType::Js => control_db::HostType::Js,
3133
}
3234
}
3335
}

0 commit comments

Comments
 (0)