Skip to content

Commit 6d1cb88

Browse files
committed
v8: move some InterruptCallbacks to budget.rs
1 parent 187b6d6 commit 6d1cb88

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! as V8 has no native notion of gas/fuel,
55
//! so we have to invent one using time and timeouts.
66
7+
use super::env_on_isolate;
78
use crate::host::wasm_common::module_host_actor::EnergyStats;
89
use crate::host::wasmtime::{epoch_ticker, ticks_in_duration};
910
use core::ptr;
@@ -41,6 +42,21 @@ pub(super) fn with_timeout_and_cb_every<R>(
4142
/// A callback passed to [`IsolateHandle::request_interrupt`].
4243
pub(super) type InterruptCallback = extern "C" fn(&mut Isolate, *mut c_void);
4344

45+
/// An [`InterruptCallback`] used by `call_reducer`,
46+
/// and called by a thread separate to V8 execution
47+
/// every [`EPOCH_TICKS_PER_SECOND`] ticks (~every 1 second)
48+
/// to log that the reducer is still running.
49+
pub(super) extern "C" fn cb_log_long_running(isolate: &mut Isolate, _: *mut c_void) {
50+
let env = env_on_isolate(isolate);
51+
let database = env.instance_env.replica_ctx.database_identity;
52+
let reducer = env.reducer_name();
53+
let dur = env.reducer_start().elapsed();
54+
tracing::warn!(reducer, ?database, "JavaScript has been running for {dur:?}");
55+
}
56+
57+
/// An [`InterruptCallback`] that does nothing.
58+
pub(super) extern "C" fn cb_noop(_: &mut Isolate, _: *mut c_void) {}
59+
4460
/// Spawns a thread that will terminate execution
4561
/// when `budget` has been used up.
4662
///

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use self::budget::{cb_log_long_running, cb_noop};
12
use self::budget::{energy_from_elapsed, with_timeout_and_cb_every};
23
use self::error::{
34
catch_exception, exception_already_thrown, log_traceback, BufferTooSmall, CodeError, ExcResult, JsStackTrace,
@@ -17,7 +18,6 @@ use crate::host::wasm_common::{RowIters, TimingSpanSet};
1718
use crate::host::wasmtime::EPOCH_TICKS_PER_SECOND;
1819
use crate::host::Scheduler;
1920
use crate::{module_host_context::ModuleCreationContext, replica_context::ReplicaContext};
20-
use core::ffi::c_void;
2121
use core::str;
2222
use spacetimedb_client_api_messages::energy::ReducerBudget;
2323
use spacetimedb_datastore::locking_tx_datastore::MutTxId;
@@ -316,17 +316,6 @@ impl JsInstance {
316316

317317
self.common
318318
.call_reducer_with_tx(replica_ctx, tx, params, log_traceback, |tx, op, budget| {
319-
/// Called by a thread separate to V8 execution
320-
/// every [`EPOCH_TICKS_PER_SECOND`] ticks (~every 1 second)
321-
/// to log that the reducer is still running.
322-
extern "C" fn cb_log_long_running(isolate: &mut Isolate, _: *mut c_void) {
323-
let env = env_on_isolate(isolate);
324-
let database = env.instance_env.replica_ctx.database_identity;
325-
let reducer = env.reducer_name();
326-
let dur = env.reducer_start().elapsed();
327-
tracing::warn!(reducer, ?database, "JavaScript has been running for {dur:?}");
328-
}
329-
330319
// TODO(v8): snapshots
331320
// Prepare the isolate with the env.
332321
let mut isolate = Isolate::new(<_>::default());
@@ -504,11 +493,10 @@ fn call_free_fun<'scope>(
504493
fn extract_description(program: &str) -> Result<RawModuleDef, DescribeError> {
505494
let budget = ReducerBudget::DEFAULT_BUDGET;
506495
let callback_every = EPOCH_TICKS_PER_SECOND;
507-
extern "C" fn callback(_: &mut Isolate, _: *mut c_void) {}
508496

509497
let mut isolate = Isolate::new(<_>::default());
510498
let handle = isolate.thread_safe_handle();
511-
with_timeout_and_cb_every(handle, callback_every, callback, budget, || {
499+
with_timeout_and_cb_every(handle, callback_every, cb_noop, budget, || {
512500
with_scope(&mut isolate, |scope| {
513501
eval_user_module_catch(scope, program).map_err(DescribeError::Setup)?;
514502

0 commit comments

Comments
 (0)