From a5dfe4b7225ff33a5b9b8d1234f5264138ef6936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 6 Jun 2022 15:11:42 +0200 Subject: [PATCH] use test from Joyee --- tests/test_api.rs | 227 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 190 insertions(+), 37 deletions(-) diff --git a/tests/test_api.rs b/tests/test_api.rs index db554273..3ccee135 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -3586,10 +3586,22 @@ 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, "var f = function() { return 1; }").unwrap(); + let source = v8::String::new( + 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") + .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)); + } snapshot_creator.set_default_context(context); } { @@ -3597,16 +3609,21 @@ 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, "var f = function() { return 2; }").unwrap(); + v8::String::new(scope, "globalThis.__bootstrap = { context0Prop: 2 };") + .unwrap(); let script = v8::Script::compile(scope, source, None).unwrap(); script.run(scope).unwrap(); + { + let source = + v8::String::new(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)); + } assert_eq!(0, snapshot_creator.add_context(context)); } - { - let scope = &mut v8::HandleScope::new(&mut isolate); - let context = v8::Context::new(scope); - assert_eq!(1, snapshot_creator.add_context(context)); - } std::mem::forget(isolate); // TODO(ry) this shouldn't be necessary. snapshot_creator @@ -3614,36 +3631,172 @@ fn snapshot_creator_multiple_contexts() { .unwrap() }; - let params = v8::Isolate::create_params().snapshot_blob(startup_data); - let isolate = &mut v8::Isolate::new(params); + let startup_data = { + let mut snapshot_creator = + v8::SnapshotCreator::new(None, Some(&startup_data)); + 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(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") + .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 = 3;", + ) + .unwrap(); + let script = v8::Script::compile(scope, source, None).unwrap(); + script.run(scope).unwrap(); + let source = + v8::String::new(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)); + } + snapshot_creator.set_default_context(context); + } + { + let scope = &mut v8::HandleScope::new(&mut isolate); + 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") + .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") + .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;") + .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)); + } + assert_eq!(snapshot_creator.add_context(context), 0); + } + std::mem::forget(isolate); // TODO(ry) this shouldn't be necessary. + snapshot_creator + .create_blob(v8::FunctionCodeHandling::Clear) + .unwrap() + }; { - let scope = &mut v8::HandleScope::new(isolate); - let context = v8::Context::new(scope); - let scope = &mut v8::ContextScope::new(scope, context); - let source = v8::String::new(scope, "f()").unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let result = script.run(scope).unwrap(); - let one_val = v8::Number::new(scope, 1.0).into(); - assert!(result.same_value(one_val)); - } - { - let scope = &mut v8::HandleScope::new(isolate); - let context = v8::Context::from_snapshot(scope, 0).unwrap(); - let scope = &mut v8::ContextScope::new(scope, context); - let source = v8::String::new(scope, "f()").unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let result = script.run(scope).unwrap(); - let two_val = v8::Number::new(scope, 2.0).into(); - assert!(result.same_value(two_val)); - } - { - let scope = &mut v8::HandleScope::new(isolate); - let context = v8::Context::from_snapshot(scope, 1).unwrap(); - let scope = &mut v8::ContextScope::new(scope, context); - let source = v8::String::new(scope, "this.f").unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let result = script.run(scope).unwrap(); - assert!(result.is_undefined()); + let params = v8::Isolate::create_params().snapshot_blob(startup_data); + let isolate = &mut v8::Isolate::new(params); + { + let scope = &mut v8::HandleScope::new(isolate); + let context = v8::Context::new(scope); + let scope = &mut v8::ContextScope::new(scope, context); + { + let source = + v8::String::new(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") + .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") + .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") + .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)); + } + } + { + let scope = &mut v8::HandleScope::new(isolate); + 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") + .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") + .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") + .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") + .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)); + } + } } }