0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-11 08:34:01 -05:00

Add Isolate::clear_kept_objects() (#507)

Refs: https://github.com/denoland/deno/issues/7674
This commit is contained in:
Ben Noordhuis 2020-10-21 08:00:44 +02:00 committed by GitHub
parent b42dca49ef
commit b8a2e06dc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 1 deletions

View file

@ -123,6 +123,10 @@ void v8__Isolate__Enter(v8::Isolate* isolate) { isolate->Enter(); }
void v8__Isolate__Exit(v8::Isolate* isolate) { isolate->Exit(); }
void v8__Isolate__ClearKeptObjects(v8::Isolate* isolate) {
isolate->ClearKeptObjects();
}
void v8__Isolate__LowMemoryNotification(v8::Isolate* isolate) {
isolate->LowMemoryNotification();
}

View file

@ -132,6 +132,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__ClearKeptObjects(isolate: *mut Isolate);
fn v8__Isolate__LowMemoryNotification(isolate: *mut Isolate);
fn v8__Isolate__GetHeapStatistics(this: *mut Isolate, s: *mut HeapStatistics);
fn v8__Isolate__SetCaptureStackTraceForUncaughtExceptions(
@ -389,6 +390,21 @@ impl Isolate {
unsafe { v8__Isolate__Exit(self) }
}
/// Clears the set of objects held strongly by the heap. This set of
/// objects are originally built when a WeakRef is created or
/// successfully dereferenced.
///
/// This is invoked automatically after microtasks are run. See
/// MicrotasksPolicy for when microtasks are run.
///
/// This needs to be manually invoked only if the embedder is manually
/// running microtasks via a custom MicrotaskQueue class's PerformCheckpoint.
/// In that case, it is the embedder's responsibility to make this call at a
/// time which does not interrupt synchronous ECMAScript code execution.
pub fn clear_kept_objects(&mut self) {
unsafe { v8__Isolate__ClearKeptObjects(self) }
}
/// Optional notification that the system is running low on memory.
/// V8 uses these notifications to attempt to free memory.
pub fn low_memory_notification(&mut self) {

View file

@ -31,6 +31,7 @@ fn setup() -> SetupGuard {
let mut g = INIT_LOCK.lock().unwrap();
*g += 1;
if *g == 1 {
v8::V8::set_flags_from_string("--expose_gc");
v8::V8::initialize_platform(v8::new_default_platform().unwrap());
v8::V8::initialize();
}
@ -852,7 +853,9 @@ fn add_message_listener() {
let frame = stack_trace.get_frame(scope, 0).unwrap();
assert_eq!(1, frame.get_line_number());
assert_eq!(1, frame.get_column());
assert_eq!(3, frame.get_script_id());
// Note: V8 flags like --expose_externalize_string and --expose_gc install
// scripts of their own and therefore affect the script id that we get.
assert_eq!(4, frame.get_script_id());
assert!(frame.get_script_name(scope).is_none());
assert!(frame.get_script_name_or_source_url(scope).is_none());
assert!(frame.get_function_name(scope).is_none());
@ -4255,3 +4258,31 @@ fn value_serializer_not_implemented() {
"Uncaught Error: Deno serializer: get_shared_array_buffer_id not implemented"
);
}
#[test]
fn clear_kept_objects() {
let _setup_guard = setup();
let isolate = &mut v8::Isolate::new(Default::default());
isolate.set_microtasks_policy(v8::MicrotasksPolicy::Explicit);
let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
let step1 = r#"
var weakrefs = [];
for (let i = 0; i < 424242; i++) weakrefs.push(new WeakRef({ i }));
gc();
if (weakrefs.some(w => !w.deref())) throw "fail";
"#;
let step2 = r#"
gc();
if (weakrefs.every(w => w.deref())) throw "fail";
"#;
eval(scope, step1).unwrap();
scope.clear_kept_objects();
eval(scope, step2).unwrap();
}