From 4cc07a5d5dd845af3c7e88acee3fab69128d5fd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 7 Jun 2022 15:00:13 +0200 Subject: [PATCH] add compile_and_run helper fn --- tests/test_api.rs | 133 +++++++++++++++++----------------------------- 1 file changed, 50 insertions(+), 83 deletions(-) diff --git a/tests/test_api.rs b/tests/test_api.rs index 3ccee135..53f10929 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -3578,27 +3578,32 @@ fn snapshot_creator() { #[test] fn snapshot_creator_multiple_contexts() { + fn compile_and_run<'s>( + scope: &mut v8::HandleScope<'s>, + source: &str, + ) -> Option> { + let source = v8::String::new(scope, source).unwrap(); + let script = v8::Script::compile(scope, source, None).unwrap(); + script.run(scope) + } + let _setup_guard = setup(); let startup_data = { let mut snapshot_creator = v8::SnapshotCreator::new(None, None); let mut isolate = unsafe { snapshot_creator.get_owned_isolate() }; { - let scope = &mut v8::HandleScope::new(&mut isolate); - let context = v8::Context::new(scope); - let scope = &mut v8::ContextScope::new(scope, context); - let source = v8::String::new( + let mut scope = v8::HandleScope::new(&mut isolate); + let context = v8::Context::new(&mut scope); + let scope = &mut v8::ContextScope::new(&mut scope, context); + compile_and_run( scope, "globalThis.__bootstrap = { defaultContextProp: 1};", ) .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - script.run(scope).unwrap(); { - let source = - v8::String::new(scope, "globalThis.__bootstrap.defaultContextProp") + let value = + compile_and_run(scope, "globalThis.__bootstrap.defaultContextProp") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); let one_val = v8::Number::new(scope, 1.0).into(); assert!(value.same_value(one_val)); } @@ -3608,17 +3613,12 @@ fn snapshot_creator_multiple_contexts() { let scope = &mut v8::HandleScope::new(&mut isolate); let context = v8::Context::new(scope); let scope = &mut v8::ContextScope::new(scope, context); - let source = - v8::String::new(scope, "globalThis.__bootstrap = { context0Prop: 2 };") - .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - script.run(scope).unwrap(); + compile_and_run(scope, "globalThis.__bootstrap = { context0Prop: 2 };") + .unwrap(); { - let source = - v8::String::new(scope, "globalThis.__bootstrap.context0Prop") + let value = + compile_and_run(scope, "globalThis.__bootstrap.context0Prop") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); let two_val = v8::Number::new(scope, 2.0).into(); assert!(value.same_value(two_val)); } @@ -3640,35 +3640,27 @@ fn snapshot_creator_multiple_contexts() { let context = v8::Context::new(scope); let scope = &mut v8::ContextScope::new(scope, context); { - let source = - v8::String::new(scope, "globalThis.__bootstrap.defaultContextProp") + let value = + compile_and_run(scope, "globalThis.__bootstrap.defaultContextProp") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); let one_val = v8::Number::new(scope, 1.0).into(); assert!(value.same_value(one_val)); } { - let source = - v8::String::new(scope, "globalThis.__bootstrap.context0Prop") + let value = + compile_and_run(scope, "globalThis.__bootstrap.context0Prop") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); assert!(value.is_undefined()); } { - let source = v8::String::new( + compile_and_run( scope, "globalThis.__bootstrap.defaultContextProp2 = 3;", ) .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - script.run(scope).unwrap(); - let source = - v8::String::new(scope, "globalThis.__bootstrap.defaultContextProp2") + let value = + compile_and_run(scope, "globalThis.__bootstrap.defaultContextProp2") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); let three_val = v8::Number::new(scope, 3.0).into(); assert!(value.same_value(three_val)); } @@ -3679,33 +3671,24 @@ fn snapshot_creator_multiple_contexts() { let context = v8::Context::from_snapshot(scope, 0).unwrap(); let scope = &mut v8::ContextScope::new(scope, context); { - let source = - v8::String::new(scope, "globalThis.__bootstrap.defaultContextProp") + let value = + compile_and_run(scope, "globalThis.__bootstrap.defaultContextProp") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); assert!(value.is_undefined()); } { - let source = - v8::String::new(scope, "globalThis.__bootstrap.context0Prop") + let value = + compile_and_run(scope, "globalThis.__bootstrap.context0Prop") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); let two_val = v8::Number::new(scope, 2.0).into(); assert!(value.same_value(two_val)); } { - let source = - v8::String::new(scope, "globalThis.__bootstrap.context0Prop2 = 4;") + compile_and_run(scope, "globalThis.__bootstrap.context0Prop2 = 4;") + .unwrap(); + let value = + compile_and_run(scope, "globalThis.__bootstrap.context0Prop2") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - script.run(scope).unwrap(); - let source = - v8::String::new(scope, "globalThis.__bootstrap.context0Prop2") - .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); let four_val = v8::Number::new(scope, 4.0).into(); assert!(value.same_value(four_val)); } @@ -3724,36 +3707,28 @@ fn snapshot_creator_multiple_contexts() { let context = v8::Context::new(scope); let scope = &mut v8::ContextScope::new(scope, context); { - let source = - v8::String::new(scope, "globalThis.__bootstrap.context0Prop") + let value = + compile_and_run(scope, "globalThis.__bootstrap.context0Prop") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); assert!(value.is_undefined()); } { - let source = - v8::String::new(scope, "globalThis.__bootstrap.context0Prop2") + let value = + compile_and_run(scope, "globalThis.__bootstrap.context0Prop2") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); assert!(value.is_undefined()); } { - let source = - v8::String::new(scope, "globalThis.__bootstrap.defaultContextProp") + let value = + compile_and_run(scope, "globalThis.__bootstrap.defaultContextProp") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); let one_val = v8::Number::new(scope, 1.0).into(); assert!(value.same_value(one_val)); } { - let source = - v8::String::new(scope, "globalThis.__bootstrap.defaultContextProp2") + let value = + compile_and_run(scope, "globalThis.__bootstrap.defaultContextProp2") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); let three_val = v8::Number::new(scope, 3.0).into(); assert!(value.same_value(three_val)); } @@ -3763,36 +3738,28 @@ fn snapshot_creator_multiple_contexts() { let context = v8::Context::from_snapshot(scope, 0).unwrap(); let scope = &mut v8::ContextScope::new(scope, context); { - let source = - v8::String::new(scope, "globalThis.__bootstrap.defaultContextProp") + let value = + compile_and_run(scope, "globalThis.__bootstrap.defaultContextProp") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); assert!(value.is_undefined()); } { - let source = - v8::String::new(scope, "globalThis.__bootstrap.defaultContextProp2") + let value = + compile_and_run(scope, "globalThis.__bootstrap.defaultContextProp2") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); assert!(value.is_undefined()); } { - let source = - v8::String::new(scope, "globalThis.__bootstrap.context0Prop") + let value = + compile_and_run(scope, "globalThis.__bootstrap.context0Prop") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); let two_val = v8::Number::new(scope, 2.0).into(); assert!(value.same_value(two_val)); } { - let source = - v8::String::new(scope, "globalThis.__bootstrap.context0Prop2") + let value = + compile_and_run(scope, "globalThis.__bootstrap.context0Prop2") .unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let value = script.run(scope).unwrap(); let four_val = v8::Number::new(scope, 4.0).into(); assert!(value.same_value(four_val)); }