mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-11-21 15:04:33 -05:00
Add Isolate::RunMicrotasks and Isolate::EnqueueMicrotask (#164)
This commit is contained in:
parent
8d6ad51662
commit
53fd83a6fa
3 changed files with 55 additions and 0 deletions
|
@ -120,6 +120,15 @@ uint32_t v8__Isolate__GetNumberOfDataSlots(v8::Isolate* isolate) {
|
|||
return SLOT_NUM_EXTERNAL(isolate);
|
||||
}
|
||||
|
||||
void v8__Isolate__RunMicrotasks(v8::Isolate& isolate) {
|
||||
isolate.RunMicrotasks();
|
||||
}
|
||||
|
||||
void v8__Isolate__EnqueueMicrotask(v8::Isolate& isolate,
|
||||
v8::Function* function) {
|
||||
isolate.EnqueueMicrotask(ptr_to_local(function));
|
||||
}
|
||||
|
||||
void v8__Isolate__SetPromiseRejectCallback(v8::Isolate* isolate,
|
||||
v8::PromiseRejectCallback callback) {
|
||||
isolate->SetPromiseRejectCallback(callback);
|
||||
|
|
|
@ -7,6 +7,7 @@ use crate::support::Delete;
|
|||
use crate::support::Opaque;
|
||||
use crate::support::UniqueRef;
|
||||
use crate::Context;
|
||||
use crate::Function;
|
||||
use crate::Local;
|
||||
use crate::Message;
|
||||
use crate::Module;
|
||||
|
@ -97,6 +98,8 @@ extern "C" {
|
|||
fn v8__Isolate__TerminateExecution(isolate: &Isolate);
|
||||
fn v8__Isolate__IsExecutionTerminating(isolate: &Isolate) -> bool;
|
||||
fn v8__Isolate__CancelTerminateExecution(isolate: &Isolate);
|
||||
fn v8__Isolate__RunMicrotasks(isolate: &Isolate);
|
||||
fn v8__Isolate__EnqueueMicrotask(isolate: &Isolate, microtask: *mut Function);
|
||||
|
||||
fn v8__Isolate__CreateParams__NEW() -> *mut CreateParams;
|
||||
fn v8__Isolate__CreateParams__DELETE(this: &mut CreateParams);
|
||||
|
@ -289,6 +292,17 @@ impl Isolate {
|
|||
unsafe { v8__Isolate__CancelTerminateExecution(self) }
|
||||
}
|
||||
|
||||
/// Runs the default MicrotaskQueue until it gets empty.
|
||||
/// Any exceptions thrown by microtask callbacks are swallowed.
|
||||
pub fn run_microtasks(&self) {
|
||||
unsafe { v8__Isolate__RunMicrotasks(self) }
|
||||
}
|
||||
|
||||
/// Enqueues the callback to the default MicrotaskQueue
|
||||
pub fn enqueue_microtask(&self, mut microtask: Local<Function>) {
|
||||
unsafe { v8__Isolate__EnqueueMicrotask(self, &mut *microtask) }
|
||||
}
|
||||
|
||||
/// Disposes the isolate. The isolate must not be entered by any
|
||||
/// thread to be disposable.
|
||||
pub unsafe fn dispose(&mut self) {
|
||||
|
|
|
@ -201,6 +201,38 @@ fn escapable_handle_scope() {
|
|||
drop(g);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn microtasks() {
|
||||
setup();
|
||||
let mut params = v8::Isolate::create_params();
|
||||
params.set_array_buffer_allocator(v8::new_default_allocator());
|
||||
let isolate = v8::Isolate::new(params);
|
||||
|
||||
isolate.run_microtasks();
|
||||
|
||||
let mut locker = v8::Locker::new(&isolate);
|
||||
{
|
||||
let mut hs = v8::HandleScope::new(&mut locker);
|
||||
let scope = hs.enter();
|
||||
let mut context = v8::Context::new(scope);
|
||||
context.enter();
|
||||
|
||||
static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||
extern "C" fn cb(_info: &FunctionCallbackInfo) {
|
||||
CALL_COUNT.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
let function = v8::Function::new(scope, context, cb).unwrap();
|
||||
|
||||
assert_eq!(CALL_COUNT.load(Ordering::SeqCst), 0);
|
||||
|
||||
isolate.enqueue_microtask(function);
|
||||
isolate.run_microtasks();
|
||||
|
||||
assert_eq!(CALL_COUNT.load(Ordering::SeqCst), 1);
|
||||
context.exit();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn array_buffer() {
|
||||
setup();
|
||||
|
|
Loading…
Reference in a new issue