1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

refactor: create JsRuntime outside of run_in_task (#17667)

This commit is contained in:
Leo Kettmeir 2023-02-06 14:49:26 +01:00 committed by GitHub
parent 84a96110cd
commit fd511d5755
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 78 deletions

View file

@ -1803,15 +1803,15 @@ import "/a.js";
}
}
// Test an erroneous dynamic import where the specified module isn't found.
run_in_task(|cx| {
let loader = Rc::new(DynImportErrLoader::default());
let count = loader.count.clone();
let mut runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(loader),
..Default::default()
});
let loader = Rc::new(DynImportErrLoader::default());
let count = loader.count.clone();
let mut runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(loader),
..Default::default()
});
// Test an erroneous dynamic import where the specified module isn't found.
run_in_task(move |cx| {
runtime
.execute_script(
"file:///dyn_import2.js",
@ -1886,16 +1886,15 @@ import "/a.js";
#[test]
fn dyn_import_ok() {
run_in_task(|cx| {
let loader = Rc::new(DynImportOkLoader::default());
let prepare_load_count = loader.prepare_load_count.clone();
let resolve_count = loader.resolve_count.clone();
let load_count = loader.load_count.clone();
let mut runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(loader),
..Default::default()
});
let loader = Rc::new(DynImportOkLoader::default());
let prepare_load_count = loader.prepare_load_count.clone();
let resolve_count = loader.resolve_count.clone();
let load_count = loader.load_count.clone();
let mut runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(loader),
..Default::default()
});
run_in_task(move |cx| {
// Dynamically import mod_b
runtime
.execute_script(
@ -1935,13 +1934,14 @@ import "/a.js";
#[test]
fn dyn_import_borrow_mut_error() {
// https://github.com/denoland/deno/issues/6054
run_in_task(|cx| {
let loader = Rc::new(DynImportOkLoader::default());
let prepare_load_count = loader.prepare_load_count.clone();
let mut runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(loader),
..Default::default()
});
let loader = Rc::new(DynImportOkLoader::default());
let prepare_load_count = loader.prepare_load_count.clone();
let mut runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(loader),
..Default::default()
});
run_in_task(move |cx| {
runtime
.execute_script(
"file:///dyn_import3.js",
@ -2193,13 +2193,14 @@ import "/a.js";
#[test]
fn slow_never_ready_modules() {
run_in_task(|cx| {
let loader = MockLoader::new();
let loads = loader.loads.clone();
let mut runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(loader),
..Default::default()
});
let loader = MockLoader::new();
let loads = loader.loads.clone();
let mut runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(loader),
..Default::default()
});
run_in_task(move |cx| {
let spec = resolve_url("file:///main.js").unwrap();
let mut recursive_load =
runtime.load_main_module(&spec, None).boxed_local();
@ -2238,12 +2239,13 @@ import "/a.js";
#[test]
fn loader_disappears_after_error() {
run_in_task(|cx| {
let loader = MockLoader::new();
let mut runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(loader),
..Default::default()
});
let loader = MockLoader::new();
let mut runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(loader),
..Default::default()
});
run_in_task(move |cx| {
let spec = resolve_url("file:///bad_import.js").unwrap();
let mut load_fut = runtime.load_main_module(&spec, None).boxed_local();
let result = load_fut.poll_unpin(cx);

View file

@ -2730,7 +2730,7 @@ pub mod tests {
pub fn run_in_task<F>(f: F)
where
F: FnOnce(&mut Context) + Send + 'static,
F: FnOnce(&mut Context) + 'static,
{
futures::executor::block_on(lazy(move |cx| f(cx)));
}
@ -2962,8 +2962,8 @@ pub mod tests {
#[tokio::test]
async fn test_poll_value() {
run_in_task(|cx| {
let mut runtime = JsRuntime::new(Default::default());
let mut runtime = JsRuntime::new(Default::default());
run_in_task(move |cx| {
let value_global = runtime
.execute_script("a.js", "Promise.resolve(1 + 2)")
.unwrap();
@ -3162,8 +3162,8 @@ pub mod tests {
#[test]
fn test_encode_decode() {
run_in_task(|cx| {
let (mut runtime, _dispatch_count) = setup(Mode::Async);
let (mut runtime, _dispatch_count) = setup(Mode::Async);
run_in_task(move |cx| {
runtime
.execute_script(
"encode_decode_test.js",
@ -3178,8 +3178,8 @@ pub mod tests {
#[test]
fn test_serialize_deserialize() {
run_in_task(|cx| {
let (mut runtime, _dispatch_count) = setup(Mode::Async);
let (mut runtime, _dispatch_count) = setup(Mode::Async);
run_in_task(move |cx| {
runtime
.execute_script(
"serialize_deserialize_test.js",
@ -3203,15 +3203,15 @@ pub mod tests {
"DOMExceptionOperationError"
}
run_in_task(|cx| {
let ext = Extension::builder("test_ext")
.ops(vec![op_err::decl()])
.build();
let mut runtime = JsRuntime::new(RuntimeOptions {
extensions: vec![ext],
get_error_class_fn: Some(&get_error_class_name),
..Default::default()
});
let ext = Extension::builder("test_ext")
.ops(vec![op_err::decl()])
.build();
let mut runtime = JsRuntime::new(RuntimeOptions {
extensions: vec![ext],
get_error_class_fn: Some(&get_error_class_name),
..Default::default()
});
run_in_task(move |cx| {
runtime
.execute_script(
"error_builder_test.js",
@ -3725,8 +3725,8 @@ main();
#[test]
fn test_error_async_stack() {
run_in_task(|cx| {
let mut runtime = JsRuntime::new(RuntimeOptions::default());
let mut runtime = JsRuntime::new(RuntimeOptions::default());
run_in_task(move |cx| {
runtime
.execute_script(
"error_async_stack.js",
@ -3774,15 +3774,15 @@ main();
Err(anyhow!("original async error").context("higher-level async error"))
}
run_in_task(|cx| {
let ext = Extension::builder("test_ext")
.ops(vec![op_err_sync::decl(), op_err_async::decl()])
.build();
let mut runtime = JsRuntime::new(RuntimeOptions {
extensions: vec![ext],
..Default::default()
});
let ext = Extension::builder("test_ext")
.ops(vec![op_err_sync::decl(), op_err_async::decl()])
.build();
let mut runtime = JsRuntime::new(RuntimeOptions {
extensions: vec![ext],
..Default::default()
});
run_in_task(move |cx| {
runtime
.execute_script(
"test_error_context_sync.js",
@ -3830,8 +3830,8 @@ Deno.core.initializeAsyncOps();
#[test]
fn test_pump_message_loop() {
run_in_task(|cx| {
let mut runtime = JsRuntime::new(RuntimeOptions::default());
let mut runtime = JsRuntime::new(RuntimeOptions::default());
run_in_task(move |cx| {
runtime
.execute_script(
"pump_message_loop.js",
@ -4808,19 +4808,20 @@ Deno.core.ops.op_async_serialize_object_with_numbers_as_keys({
#[tokio::test]
async fn js_realm_ref_unref_ops() {
run_in_task(|cx| {
// Never resolves.
#[op]
async fn op_pending() {
futures::future::pending().await
}
// Never resolves.
#[op]
async fn op_pending() {
futures::future::pending().await
}
let mut runtime = JsRuntime::new(RuntimeOptions {
extensions: vec![Extension::builder("test_ext")
.ops(vec![op_pending::decl()])
.build()],
..Default::default()
});
let mut runtime = JsRuntime::new(RuntimeOptions {
extensions: vec![Extension::builder("test_ext")
.ops(vec![op_pending::decl()])
.build()],
..Default::default()
});
run_in_task(move |cx| {
let main_realm = runtime.global_realm();
let other_realm = runtime.create_realm().unwrap();