2022-03-11 17:07:02 -05:00
|
|
|
use crate::tools::bench::BenchEvent;
|
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
2022-03-11 17:07:02 -05:00
|
|
|
use deno_core::Extension;
|
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_runtime::permissions::create_child_permissions;
|
|
|
|
use deno_runtime::permissions::ChildPermissionsArg;
|
|
|
|
use deno_runtime::permissions::Permissions;
|
|
|
|
use std::time;
|
|
|
|
use tokio::sync::mpsc::UnboundedSender;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2022-03-23 11:33:42 -04:00
|
|
|
pub fn init(sender: UnboundedSender<BenchEvent>, unstable: bool) -> Extension {
|
2022-03-11 17:07:02 -05:00
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![
|
2022-03-14 13:44:15 -04:00
|
|
|
op_pledge_test_permissions::decl(),
|
|
|
|
op_restore_test_permissions::decl(),
|
|
|
|
op_get_bench_origin::decl(),
|
|
|
|
op_dispatch_bench_event::decl(),
|
|
|
|
op_bench_now::decl(),
|
2022-03-23 11:33:42 -04:00
|
|
|
op_bench_check_unstable::decl(),
|
2022-03-11 17:07:02 -05:00
|
|
|
])
|
|
|
|
.state(move |state| {
|
|
|
|
state.put(sender.clone());
|
2022-03-23 11:33:42 -04:00
|
|
|
state.put(Unstable(unstable));
|
2022-03-11 17:07:02 -05:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
2022-03-23 11:33:42 -04:00
|
|
|
pub struct Unstable(pub bool);
|
|
|
|
|
|
|
|
fn check_unstable(state: &OpState, api_name: &str) {
|
|
|
|
let unstable = state.borrow::<Unstable>();
|
|
|
|
|
|
|
|
if !unstable.0 {
|
|
|
|
eprintln!(
|
|
|
|
"Unstable API '{}'. The --unstable flag must be provided.",
|
|
|
|
api_name
|
|
|
|
);
|
|
|
|
std::process::exit(70);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
2022-05-13 04:36:31 -04:00
|
|
|
fn op_bench_check_unstable(state: &mut OpState) {
|
2022-03-23 11:33:42 -04:00
|
|
|
check_unstable(state, "Deno.bench");
|
|
|
|
}
|
|
|
|
|
2022-03-11 17:07:02 -05:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct PermissionsHolder(Uuid, Permissions);
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-11 17:07:02 -05:00
|
|
|
pub fn op_pledge_test_permissions(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ChildPermissionsArg,
|
|
|
|
) -> Result<Uuid, AnyError> {
|
|
|
|
let token = Uuid::new_v4();
|
|
|
|
let parent_permissions = state.borrow_mut::<Permissions>();
|
|
|
|
let worker_permissions = create_child_permissions(parent_permissions, args)?;
|
|
|
|
let parent_permissions = parent_permissions.clone();
|
|
|
|
|
2022-04-17 11:47:24 -04:00
|
|
|
if state.try_take::<PermissionsHolder>().is_some() {
|
|
|
|
panic!("pledge test permissions called before restoring previous pledge");
|
|
|
|
}
|
|
|
|
|
2022-03-11 17:07:02 -05:00
|
|
|
state.put::<PermissionsHolder>(PermissionsHolder(token, parent_permissions));
|
|
|
|
|
|
|
|
// NOTE: This call overrides current permission set for the worker
|
|
|
|
state.put::<Permissions>(worker_permissions);
|
|
|
|
|
|
|
|
Ok(token)
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-11 17:07:02 -05:00
|
|
|
pub fn op_restore_test_permissions(
|
|
|
|
state: &mut OpState,
|
|
|
|
token: Uuid,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
if let Some(permissions_holder) = state.try_take::<PermissionsHolder>() {
|
|
|
|
if token != permissions_holder.0 {
|
|
|
|
panic!("restore test permissions token does not match the stored token");
|
|
|
|
}
|
|
|
|
|
|
|
|
let permissions = permissions_holder.1;
|
|
|
|
state.put::<Permissions>(permissions);
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(generic_error("no permissions to restore"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-05-13 04:36:31 -04:00
|
|
|
fn op_get_bench_origin(state: &mut OpState) -> String {
|
|
|
|
state.borrow::<ModuleSpecifier>().to_string()
|
2022-03-11 17:07:02 -05:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-05-13 04:36:31 -04:00
|
|
|
fn op_dispatch_bench_event(state: &mut OpState, event: BenchEvent) {
|
2022-03-11 17:07:02 -05:00
|
|
|
let sender = state.borrow::<UnboundedSender<BenchEvent>>().clone();
|
|
|
|
sender.send(event).ok();
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_bench_now(state: &mut OpState) -> Result<u64, AnyError> {
|
2022-03-11 17:07:02 -05:00
|
|
|
let ns = state.borrow::<time::Instant>().elapsed().as_nanos();
|
|
|
|
let ns_u64 = u64::try_from(ns)?;
|
|
|
|
Ok(ns_u64)
|
|
|
|
}
|