mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-11-24 15:19:31 -05:00
feat: add UseCounterCallback (#1567)
This commit is contained in:
parent
70e18db2ff
commit
82301490d5
6 changed files with 51 additions and 0 deletions
1
build.rs
1
build.rs
|
@ -152,6 +152,7 @@ fn build_binding() {
|
|||
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
|
||||
.clang_args(["-x", "c++", "-std=c++20", "-Iv8/include", "-I."])
|
||||
.clang_args(args)
|
||||
.rustified_enum(".*UseCounterFeature")
|
||||
.allowlist_item("v8__.*")
|
||||
.allowlist_item("cppgc__.*")
|
||||
.generate()
|
||||
|
|
|
@ -274,6 +274,11 @@ void v8__Isolate__SetHostCreateShadowRealmContextCallback(
|
|||
isolate->SetHostCreateShadowRealmContextCallback(callback);
|
||||
}
|
||||
|
||||
void v8__Isolate__SetUseCounterCallback(
|
||||
v8::Isolate* isolate, v8::Isolate::UseCounterCallback callback) {
|
||||
isolate->SetUseCounterCallback(callback);
|
||||
}
|
||||
|
||||
bool v8__Isolate__AddMessageListener(v8::Isolate* isolate,
|
||||
v8::MessageCallback callback) {
|
||||
return isolate->AddMessageListener(callback);
|
||||
|
|
|
@ -32,3 +32,5 @@ EACH_TYPED_ARRAY(TYPED_ARRAY_MAX_LENGTH)
|
|||
|
||||
using v8__CFunction = v8::CFunction;
|
||||
using v8__CFunctionInfo = v8::CFunctionInfo;
|
||||
|
||||
using v8__Isolate__UseCounterFeature = v8::Isolate::UseCounterFeature;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
|
||||
use crate::binding::v8__Isolate__UseCounterFeature;
|
||||
use crate::cppgc::Heap;
|
||||
use crate::function::FunctionCallbackInfo;
|
||||
use crate::gc::GCCallbackFlags;
|
||||
|
@ -406,6 +407,9 @@ pub type PrepareStackTraceCallback<'s> =
|
|||
Local<'s, Array>,
|
||||
) -> PrepareStackTraceCallbackRet;
|
||||
|
||||
pub type UseCounterFeature = v8__Isolate__UseCounterFeature;
|
||||
pub type UseCounterCallback = extern "C" fn(&mut Isolate, UseCounterFeature);
|
||||
|
||||
extern "C" {
|
||||
static v8__internal__Internals__kIsolateEmbedderDataOffset: int;
|
||||
|
||||
|
@ -501,6 +505,10 @@ extern "C" {
|
|||
initiator_context: Local<Context>,
|
||||
) -> *mut *mut Context,
|
||||
);
|
||||
fn v8__Isolate__SetUseCounterCallback(
|
||||
isolate: *mut Isolate,
|
||||
callback: UseCounterCallback,
|
||||
);
|
||||
fn v8__Isolate__RequestInterrupt(
|
||||
isolate: *const Isolate,
|
||||
callback: InterruptCallback,
|
||||
|
@ -1126,6 +1134,14 @@ impl Isolate {
|
|||
}
|
||||
}
|
||||
|
||||
/// Sets a callback for counting the number of times a feature of V8 is used.
|
||||
#[inline(always)]
|
||||
pub fn set_use_counter_callback(&mut self, callback: UseCounterCallback) {
|
||||
unsafe {
|
||||
v8__Isolate__SetUseCounterCallback(self, callback);
|
||||
}
|
||||
}
|
||||
|
||||
/// Enables the host application to receive a notification before a
|
||||
/// garbage collection. Allocations are allowed in the callback function,
|
||||
/// but the callback is not re-entrant: if the allocation inside it will
|
||||
|
|
|
@ -120,6 +120,8 @@ pub use isolate::PromiseHook;
|
|||
pub use isolate::PromiseHookType;
|
||||
pub use isolate::PromiseRejectCallback;
|
||||
pub use isolate::TimeZoneDetection;
|
||||
pub use isolate::UseCounterCallback;
|
||||
pub use isolate::UseCounterFeature;
|
||||
pub use isolate::WasmAsyncSuccess;
|
||||
pub use isolate_create_params::CreateParams;
|
||||
pub use microtask::MicrotaskQueue;
|
||||
|
|
|
@ -12017,3 +12017,28 @@ fn host_defined_options() {
|
|||
.unwrap();
|
||||
script.run(scope).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn use_counter_callback() {
|
||||
static COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
extern "C" fn callback(
|
||||
_isolate: &mut v8::Isolate,
|
||||
feature: v8::UseCounterFeature,
|
||||
) {
|
||||
if feature == v8::UseCounterFeature::kStrictMode {
|
||||
COUNT.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
let _setup_guard = setup::parallel_test();
|
||||
let mut isolate = v8::Isolate::new(Default::default());
|
||||
isolate.set_use_counter_callback(callback);
|
||||
let mut scope = v8::HandleScope::new(&mut isolate);
|
||||
let context = v8::Context::new(&mut scope, Default::default());
|
||||
let scope = &mut v8::ContextScope::new(&mut scope, context);
|
||||
|
||||
eval(scope, "'use strict'; 1 + 1");
|
||||
|
||||
assert_eq!(COUNT.load(Ordering::Relaxed), 1);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue