Skip to content

Commit

Permalink
feat: Add v8::MicrotaskQueue::new() (#1440)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored Mar 16, 2024
1 parent 91bec82 commit 943cbcb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions src/microtask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -76,3 +90,9 @@ impl MicrotaskQueue {
unsafe { v8__MicrotaskQueue__GetMicrotasksScopeDepth(self) }
}
}

impl Drop for MicrotaskQueue {
fn drop(&mut self) {
unsafe { v8__MicrotaskQueue__DESTRUCT(self) }
}
}
16 changes: 16 additions & 0 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 943cbcb

Please sign in to comment.