0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-23 15:50:11 -05:00

Support setting OOM callback on Isolate (#585)

This commit is contained in:
Heyang Zhou 2021-01-16 17:30:38 +08:00 committed by GitHub
parent 3fd7cae994
commit bcbe7e9348
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 3 deletions

View file

@ -245,6 +245,11 @@ void v8__Isolate__RemoveNearHeapLimitCallback(
isolate->RemoveNearHeapLimitCallback(callback, heap_limit);
}
void v8__Isolate__SetOOMErrorHandler(v8::Isolate* isolate,
v8::OOMErrorCallback callback) {
isolate->SetOOMErrorHandler(callback);
}
const v8::Value* v8__Isolate__ThrowException(v8::Isolate* isolate,
const v8::Value& exception) {
return local_to_ptr(isolate->ThrowException(ptr_to_local(&exception)));

View file

@ -30,6 +30,7 @@ use std::fmt::{self, Debug, Formatter};
use std::mem::MaybeUninit;
use std::ops::Deref;
use std::ops::DerefMut;
use std::os::raw::c_char;
use std::ptr::null_mut;
use std::ptr::NonNull;
use std::sync::Arc;
@ -122,6 +123,9 @@ pub type NearHeapLimitCallback = extern "C" fn(
initial_heap_limit: usize,
) -> usize;
pub type OOMErrorCallback =
extern "C" fn(location: *const c_char, is_heap_oom: bool);
/// Collection of V8 heap information.
///
/// Instances of this class can be passed to v8::Isolate::GetHeapStatistics to
@ -161,6 +165,10 @@ extern "C" {
callback: NearHeapLimitCallback,
heap_limit: usize,
);
fn v8__Isolate__SetOOMErrorHandler(
isolate: *mut Isolate,
callback: OOMErrorCallback,
);
fn v8__Isolate__SetPromiseHook(isolate: *mut Isolate, hook: PromiseHook);
fn v8__Isolate__SetPromiseRejectCallback(
isolate: *mut Isolate,
@ -532,6 +540,10 @@ impl Isolate {
};
}
pub fn set_oom_error_handler(&mut self, callback: OOMErrorCallback) {
unsafe { v8__Isolate__SetOOMErrorHandler(self, callback) };
}
/// Returns the policy controlling how Microtasks are invoked.
pub fn get_microtasks_policy(&self) -> MicrotasksPolicy {
unsafe { v8__Isolate__GetMicrotasksPolicy(self) }

View file

@ -720,7 +720,7 @@ macro_rules! impl_c_fn_from {
F: UnitType + Fn($($ty),*) -> R,
{
(F::get())($($arg),*)
};
}
c_fn::<F, R, $($ty),*>
}
}

View file

@ -374,7 +374,7 @@ fn get_isolate_from_handle() {
if let Some(expected_some) = expect_some {
assert_eq!(maybe_ptr.is_some(), expected_some);
}
};
}
fn check_handle<'s, F, D>(
scope: &mut v8::HandleScope<'s>,
@ -393,7 +393,7 @@ fn get_isolate_from_handle() {
let global = v8::Global::new(scope, local);
let local2 = v8::Local::new(scope, &global);
check_handle_helper(scope, expect_some, local2);
};
}
fn check_eval<'s>(
scope: &mut v8::HandleScope<'s>,
@ -4515,3 +4515,18 @@ fn run_with_rust_allocator() {
let count_loaded = count.load(Ordering::SeqCst);
assert_eq!(count_loaded, 0);
}
#[test]
fn oom_callback() {
extern "C" fn oom_handler(_: *const std::os::raw::c_char, _: bool) {
unreachable!()
}
let _setup_guard = setup();
let params = v8::CreateParams::default().heap_limits(0, 1048576 * 8);
let isolate = &mut v8::Isolate::new(params);
isolate.set_oom_error_handler(oom_handler);
// Don't attempt to trigger the OOM callback since we don't have a safe way to
// recover from it.
}