0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-24 15:19:31 -05:00

add test for multiple snapshots

This commit is contained in:
Bartek Iwańczuk 2022-05-29 23:50:29 +02:00 committed by Bert Belder
parent 90cd7401de
commit 558742e9b5
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
2 changed files with 74 additions and 3 deletions

View file

@ -310,10 +310,10 @@ impl Context {
/// Create a new context from a (non-default) context snapshot. There /// Create a new context from a (non-default) context snapshot. There
/// is no way to provide a global object template since we do not create /// is no way to provide a global object template since we do not create
/// a new global object from template, but we can reuse a global object. /// a new global object from template, but we can reuse a global object.
pub fn from_snapshot<'a>( pub fn from_snapshot<'s>(
scope: &'a mut HandleScope, scope: &mut HandleScope<'s, ()>,
context_snapshot_index: usize, context_snapshot_index: usize,
) -> Option<Local<'a, Context>> { ) -> Option<Local<'s, Context>> {
unsafe { unsafe {
scope.cast_local(|sd| { scope.cast_local(|sd| {
v8__Context__FromSnapshot(sd.get_isolate_mut(), context_snapshot_index) v8__Context__FromSnapshot(sd.get_isolate_mut(), context_snapshot_index)

View file

@ -3576,6 +3576,77 @@ fn snapshot_creator() {
} }
} }
#[test]
fn snapshot_creator_multiple_contexts() {
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(scope, "var f = function() { return 1; }").unwrap();
let script = v8::Script::compile(scope, source, None).unwrap();
script.run(scope).unwrap();
snapshot_creator.set_default_context(context);
}
{
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 2; }").unwrap();
let script = v8::Script::compile(scope, source, None).unwrap();
script.run(scope).unwrap();
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
.create_blob(v8::FunctionCodeHandling::Clear)
.unwrap()
};
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, "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());
}
}
#[test] #[test]
fn external_references() { fn external_references() {
let _setup_guard = setup(); let _setup_guard = setup();