0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-21 15:04:33 -05:00

feat: Add v8::MicrotaskQueue::new() (#1440)

This commit is contained in:
Bartek Iwańczuk 2024-03-16 04:00:57 +00:00 committed by GitHub
parent 91bec8216a
commit 943cbcb7c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 45 additions and 0 deletions

View file

@ -1923,6 +1923,15 @@ const v8::Value* v8__Context__GetContinuationPreservedEmbedderData(
return local_to_ptr(value);
}
v8::MicrotaskQueue* v8__MicrotaskQueue__New(
v8::Isolate* isolate, v8::MicrotasksPolicy policy) {
return v8::MicrotaskQueue::New(isolate, policy).release();
}
void v8__MicrotaskQueue__DESTRUCT(v8::MicrotaskQueue* self) {
self->~MicrotaskQueue();
}
void v8__MicrotaskQueue__PerformCheckpoint(v8::Isolate* isolate,
v8::MicrotaskQueue* self) {
self->PerformCheckpoint(isolate);

View file

@ -5,8 +5,15 @@ use crate::support::Opaque;
use crate::Function;
use crate::Isolate;
use crate::Local;
use crate::MicrotasksPolicy;
use crate::UniqueRef;
extern "C" {
fn v8__MicrotaskQueue__New(
isolate: *mut Isolate,
policy: MicrotasksPolicy,
) -> *mut MicrotaskQueue;
fn v8__MicrotaskQueue__DESTRUCT(queue: *mut MicrotaskQueue);
fn v8__MicrotaskQueue__PerformCheckpoint(
isolate: *mut Isolate,
queue: *const MicrotaskQueue,
@ -42,6 +49,13 @@ extern "C" {
pub struct MicrotaskQueue(Opaque);
impl MicrotaskQueue {
pub fn new(
isolate: &mut Isolate,
policy: MicrotasksPolicy,
) -> UniqueRef<Self> {
unsafe { UniqueRef::from_raw(v8__MicrotaskQueue__New(isolate, policy)) }
}
pub fn enqueue_microtask(
&self,
isolate: &mut Isolate,
@ -76,3 +90,9 @@ impl MicrotaskQueue {
unsafe { v8__MicrotaskQueue__GetMicrotasksScopeDepth(self) }
}
}
impl Drop for MicrotaskQueue {
fn drop(&mut self) {
unsafe { v8__MicrotaskQueue__DESTRUCT(self) }
}
}

View file

@ -11362,3 +11362,19 @@ fn microtask_queue() {
assert_eq!(CALL_COUNT.load(Ordering::SeqCst), 1);
}
#[test]
fn microtask_queue_new() {
let _setup_guard = setup::parallel_test();
let mut isolate = v8::Isolate::new(Default::default());
let mut scope = v8::HandleScope::new(&mut isolate);
let queue = v8::MicrotaskQueue::new(&mut scope, v8::MicrotasksPolicy::Auto);
let context = v8::Context::new(&mut scope);
context.set_microtask_queue(queue.as_ref());
assert!(std::ptr::eq(context.get_microtask_queue(), queue.as_ref()));
// TODO(bartlomieju): add more tests once we have Context::New() bindings
// https://github.com/denoland/rusty_v8/issues/1438
}