2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-10-05 10:06:44 -04:00
|
|
|
|
|
|
|
use deno_runtime::deno_napi::*;
|
|
|
|
|
2023-05-24 09:41:43 -04:00
|
|
|
use crate::check_env;
|
|
|
|
|
2022-10-05 10:06:44 -04:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct AsyncWork {
|
|
|
|
pub data: *mut c_void,
|
|
|
|
pub execute: napi_async_execute_callback,
|
|
|
|
pub complete: napi_async_complete_callback,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
fn napi_create_async_work(
|
|
|
|
_env: *mut Env,
|
|
|
|
_async_resource: napi_value,
|
|
|
|
_async_resource_name: napi_value,
|
|
|
|
execute: napi_async_execute_callback,
|
|
|
|
complete: napi_async_complete_callback,
|
|
|
|
data: *mut c_void,
|
|
|
|
result: *mut napi_async_work,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
2022-10-05 10:06:44 -04:00
|
|
|
let mut work = AsyncWork {
|
|
|
|
data,
|
|
|
|
execute,
|
|
|
|
complete,
|
|
|
|
};
|
2023-05-02 18:36:33 -04:00
|
|
|
let work_box = Box::new(work);
|
|
|
|
*result = transmute::<*mut AsyncWork, _>(Box::into_raw(work_box));
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
fn napi_cancel_async_work(
|
|
|
|
_env: &mut Env,
|
|
|
|
_async_work: napi_async_work,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Frees a previously allocated work object.
|
|
|
|
#[napi_sym::napi_sym]
|
2023-05-18 10:17:53 -04:00
|
|
|
fn napi_delete_async_work(
|
|
|
|
_env: &mut Env,
|
|
|
|
work: napi_async_work,
|
|
|
|
) -> napi_status {
|
2023-01-27 10:43:16 -05:00
|
|
|
let work = Box::from_raw(work as *mut AsyncWork);
|
2022-10-05 10:06:44 -04:00
|
|
|
drop(work);
|
|
|
|
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
2023-05-18 10:17:53 -04:00
|
|
|
fn napi_queue_async_work(
|
|
|
|
env_ptr: *mut Env,
|
|
|
|
work: napi_async_work,
|
|
|
|
) -> napi_status {
|
2022-10-05 10:06:44 -04:00
|
|
|
let work: &AsyncWork = &*(work as *const AsyncWork);
|
2023-05-18 10:17:53 -04:00
|
|
|
let Some(env) = env_ptr.as_mut() else {
|
|
|
|
return napi_invalid_arg;
|
|
|
|
};
|
2022-10-05 10:06:44 -04:00
|
|
|
|
|
|
|
let fut = Box::new(move || {
|
|
|
|
(work.execute)(env_ptr as napi_env, work.data);
|
|
|
|
// Note: Must be called from the loop thread.
|
|
|
|
(work.complete)(env_ptr as napi_env, napi_ok, work.data);
|
|
|
|
});
|
|
|
|
env.add_async_work(fut);
|
|
|
|
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
2023-05-24 09:41:43 -04:00
|
|
|
// NOTE: we don't support "async_hooks::AsyncContext" so these APIs are noops.
|
2022-10-05 10:06:44 -04:00
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
fn napi_async_init(
|
2023-05-24 09:41:43 -04:00
|
|
|
env: *mut Env,
|
2022-10-05 10:06:44 -04:00
|
|
|
_async_resource: napi_value,
|
|
|
|
_async_resource_name: napi_value,
|
2023-05-24 09:41:43 -04:00
|
|
|
result: *mut *mut (),
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
2023-05-24 09:41:43 -04:00
|
|
|
check_env!(env);
|
|
|
|
*result = ptr::null_mut();
|
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
2023-05-24 09:41:43 -04:00
|
|
|
fn napi_async_destroy(env: *mut Env, async_context: *mut ()) -> napi_status {
|
|
|
|
check_env!(env);
|
|
|
|
assert!(async_context.is_null());
|
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|