summaryrefslogtreecommitdiff
path: root/runtime/worker.rs
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/worker.rs')
-rw-r--r--runtime/worker.rs94
1 files changed, 94 insertions, 0 deletions
diff --git a/runtime/worker.rs b/runtime/worker.rs
index 79b1b5537..632805e6d 100644
--- a/runtime/worker.rs
+++ b/runtime/worker.rs
@@ -11,6 +11,9 @@ use deno_core::error::AnyError;
use deno_core::error::JsError;
use deno_core::futures::Future;
use deno_core::located_script_name;
+use deno_core::serde_json::json;
+use deno_core::serde_v8;
+use deno_core::v8;
use deno_core::CompiledWasmModuleStore;
use deno_core::Extension;
use deno_core::GetErrorClassFn;
@@ -59,6 +62,10 @@ pub struct MainWorker {
pub js_runtime: JsRuntime,
should_break_on_first_statement: bool,
exit_code: ExitCode,
+ js_run_tests_callback: v8::Global<v8::Function>,
+ js_run_benchmarks_callback: v8::Global<v8::Function>,
+ js_enable_test_callback: v8::Global<v8::Function>,
+ js_enable_bench_callback: v8::Global<v8::Function>,
}
pub struct WorkerOptions {
@@ -86,6 +93,15 @@ pub struct WorkerOptions {
pub stdio: Stdio,
}
+fn grab_cb(
+ scope: &mut v8::HandleScope,
+ path: &str,
+) -> v8::Global<v8::Function> {
+ let cb = JsRuntime::grab_global::<v8::Function>(scope, path)
+ .unwrap_or_else(|| panic!("{} must be defined", path));
+ v8::Global::new(scope, cb)
+}
+
impl MainWorker {
pub fn bootstrap_from_options(
main_module: ModuleSpecifier,
@@ -198,10 +214,29 @@ impl MainWorker {
);
}
+ let (
+ js_run_tests_callback,
+ js_run_benchmarks_callback,
+ js_enable_test_callback,
+ js_enable_bench_callback,
+ ) = {
+ let scope = &mut js_runtime.handle_scope();
+ (
+ grab_cb(scope, "__bootstrap.testing.runTests"),
+ grab_cb(scope, "__bootstrap.testing.runBenchmarks"),
+ grab_cb(scope, "__bootstrap.testing.enableTest"),
+ grab_cb(scope, "__bootstrap.testing.enableBench"),
+ )
+ };
+
Self {
js_runtime,
should_break_on_first_statement: options.should_break_on_first_statement,
exit_code,
+ js_run_tests_callback,
+ js_run_benchmarks_callback,
+ js_enable_test_callback,
+ js_enable_bench_callback,
}
}
@@ -289,6 +324,65 @@ impl MainWorker {
self.evaluate_module(id).await
}
+ /// Run tests declared with `Deno.test()`. Test events will be dispatched
+ /// by calling ops which are currently only implemented in the CLI crate.
+ // TODO(nayeemrmn): Move testing ops to deno_runtime and redesign/unhide.
+ #[doc(hidden)]
+ pub async fn run_tests(
+ &mut self,
+ shuffle: &Option<u64>,
+ ) -> Result<(), AnyError> {
+ let promise = {
+ let scope = &mut self.js_runtime.handle_scope();
+ let cb = self.js_run_tests_callback.open(scope);
+ let this = v8::undefined(scope).into();
+ let options =
+ serde_v8::to_v8(scope, json!({ "shuffle": shuffle })).unwrap();
+ let promise = cb.call(scope, this, &[options]).unwrap();
+ v8::Global::new(scope, promise)
+ };
+ self.js_runtime.resolve_value(promise).await?;
+ Ok(())
+ }
+
+ /// Run benches declared with `Deno.bench()`. Bench events will be dispatched
+ /// by calling ops which are currently only implemented in the CLI crate.
+ // TODO(nayeemrmn): Move benchmark ops to deno_runtime and redesign/unhide.
+ #[doc(hidden)]
+ pub async fn run_benchmarks(&mut self) -> Result<(), AnyError> {
+ let promise = {
+ let scope = &mut self.js_runtime.handle_scope();
+ let cb = self.js_run_benchmarks_callback.open(scope);
+ let this = v8::undefined(scope).into();
+ let promise = cb.call(scope, this, &[]).unwrap();
+ v8::Global::new(scope, promise)
+ };
+ self.js_runtime.resolve_value(promise).await?;
+ Ok(())
+ }
+
+ /// Enable `Deno.test()`. If this isn't called before executing user code,
+ /// `Deno.test()` calls will noop.
+ // TODO(nayeemrmn): Move testing ops to deno_runtime and redesign/unhide.
+ #[doc(hidden)]
+ pub fn enable_test(&mut self) {
+ let scope = &mut self.js_runtime.handle_scope();
+ let cb = self.js_enable_test_callback.open(scope);
+ let this = v8::undefined(scope).into();
+ cb.call(scope, this, &[]).unwrap();
+ }
+
+ /// Enable `Deno.bench()`. If this isn't called before executing user code,
+ /// `Deno.bench()` calls will noop.
+ // TODO(nayeemrmn): Move benchmark ops to deno_runtime and redesign/unhide.
+ #[doc(hidden)]
+ pub fn enable_bench(&mut self) {
+ let scope = &mut self.js_runtime.handle_scope();
+ let cb = self.js_enable_bench_callback.open(scope);
+ let this = v8::undefined(scope).into();
+ cb.call(scope, this, &[]).unwrap();
+ }
+
fn wait_for_inspector_session(&mut self) {
if self.should_break_on_first_statement {
self