From 90cd7401debe44d9938c4dca52bd2eb9384bb636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sun, 29 May 2022 14:45:36 +0200 Subject: [PATCH] v8::Context::from_snapshot() --- src/binding.cc | 7 +++++++ src/context.rs | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/binding.cc b/src/binding.cc index 339c6be4..5a908f68 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -1682,6 +1682,13 @@ void v8__Context__SetPromiseHooks(v8::Context& self, v8::Function& init_hook, ptr_to_local(&after_hook), ptr_to_local(&resolve_hook)); } +const v8::Context* v8__Context__FromSnapshot(v8::Isolate* isolate, + size_t context_snapshot_index) { + v8::MaybeLocal maybe_local = v8::Context::FromSnapshot( + isolate, context_snapshot_index); + return maybe_local_to_ptr(maybe_local); +} + const v8::String* v8__Message__Get(const v8::Message& self) { return local_to_ptr(self.Get()); } diff --git a/src/context.rs b/src/context.rs index d086e256..e48de6ee 100644 --- a/src/context.rs +++ b/src/context.rs @@ -44,6 +44,10 @@ extern "C" { index: c_int, value: *mut c_void, ); + fn v8__Context__FromSnapshot( + isolate: *mut Isolate, + context_snapshot_index: usize, + ) -> *const Context; } impl Context { @@ -302,6 +306,20 @@ impl Context { }; } } + + /// 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 + /// a new global object from template, but we can reuse a global object. + pub fn from_snapshot<'a>( + scope: &'a mut HandleScope, + context_snapshot_index: usize, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Context__FromSnapshot(sd.get_isolate_mut(), context_snapshot_index) + }) + } + } } struct ContextAnnex {