diff --git a/src/binding.cc b/src/binding.cc index af27a091..21d216d8 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -104,6 +104,10 @@ void v8__Isolate__Enter(v8::Isolate* isolate) { isolate->Enter(); } void v8__Isolate__Exit(v8::Isolate* isolate) { isolate->Exit(); } +v8::Context* v8__Isolate__GetCurrentContext(v8::Isolate* isolate) { + return local_to_ptr(isolate->GetCurrentContext()); +} + void v8__Isolate__SetData(v8::Isolate* isolate, uint32_t slot, void* data) { isolate->SetData(slot, data); } diff --git a/src/isolate.rs b/src/isolate.rs index 29208aa3..121c10ec 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -68,6 +68,7 @@ extern "C" { fn v8__Isolate__GetNumberOfDataSlots(this: *const Isolate) -> u32; fn v8__Isolate__Enter(this: *mut Isolate); fn v8__Isolate__Exit(this: *mut Isolate); + fn v8__Isolate__GetCurrentContext(this: *mut Isolate) -> *mut Context; fn v8__Isolate__SetCaptureStackTraceForUncaughtExceptions( this: *mut Isolate, caputre: bool, @@ -177,6 +178,12 @@ impl Isolate { unsafe { v8__Isolate__Exit(self) } } + /// Returns the context of the currently running JavaScript, or the context + /// on the top of the stack if no JavaScript is running. + pub fn get_current_context<'sc>(&mut self) -> Local<'sc, Context> { + unsafe { Local::from_raw(v8__Isolate__GetCurrentContext(self)).unwrap() } + } + /// Tells V8 to capture current stack trace when uncaught exception occurs /// and report it to the message listeners. The option is off by default. pub fn set_capture_stack_trace_for_uncaught_exceptions( diff --git a/tests/test_api.rs b/tests/test_api.rs index 5d05cbae..646f1591 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -922,14 +922,13 @@ extern "C" fn fn_callback2(info: &FunctionCallbackInfo) { assert_eq!(info.length(), 2); let isolate = info.get_isolate(); let mut locker = v8::Locker::new(&isolate); + let mut context = isolate.get_current_context(); let arg1 = info.get_argument(0); let arg2 = info.get_argument(1); let rv = &mut info.get_return_value(); - { let mut hs = v8::HandleScope::new(&mut locker); let scope = hs.enter(); - let mut context = v8::Context::new(scope); context.enter(); let arg1_val = v8::String::new(scope, "arg1").unwrap();