summaryrefslogtreecommitdiff
path: root/cli/napi/async.rs
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2022-10-05 07:06:44 -0700
committerGitHub <noreply@github.com>2022-10-05 19:36:44 +0530
commit0b016a7fb8639ce49603c8c339539174b191a4b1 (patch)
treec511060d701db60ede0214b7280e89c5749bbe62 /cli/napi/async.rs
parent3a3a8484069c9c6955fcb83ea761f9f74638175a (diff)
feat(npm): implement Node API (#13633)
This PR implements the NAPI for loading native modules into Deno. Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com> Co-authored-by: DjDeveloper <43033058+DjDeveloperr@users.noreply.github.com> Co-authored-by: Ryan Dahl <ry@tinyclouds.org>
Diffstat (limited to 'cli/napi/async.rs')
-rw-r--r--cli/napi/async.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/cli/napi/async.rs b/cli/napi/async.rs
new file mode 100644
index 000000000..b84d22272
--- /dev/null
+++ b/cli/napi/async.rs
@@ -0,0 +1,78 @@
+// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+
+use deno_runtime::deno_napi::*;
+
+#[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,
+) -> Result {
+ let mut work = AsyncWork {
+ data,
+ execute,
+ complete,
+ };
+ *result = transmute::<Box<AsyncWork>, _>(Box::new(work));
+ Ok(())
+}
+
+#[napi_sym::napi_sym]
+fn napi_cancel_async_work(
+ _env: &mut Env,
+ _async_work: napi_async_work,
+) -> Result {
+ Ok(())
+}
+
+/// Frees a previously allocated work object.
+#[napi_sym::napi_sym]
+fn napi_delete_async_work(_env: &mut Env, work: napi_async_work) -> Result {
+ let work = Box::from_raw(work);
+ drop(work);
+
+ Ok(())
+}
+
+#[napi_sym::napi_sym]
+fn napi_queue_async_work(env_ptr: *mut Env, work: napi_async_work) -> Result {
+ let work: &AsyncWork = &*(work as *const AsyncWork);
+ let env: &mut Env = env_ptr.as_mut().ok_or(Error::InvalidArg)?;
+
+ 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);
+
+ Ok(())
+}
+
+// TODO: Custom async operations.
+
+#[napi_sym::napi_sym]
+fn napi_async_init(
+ _env: *mut Env,
+ _async_resource: napi_value,
+ _async_resource_name: napi_value,
+ _result: *mut *mut (),
+) -> Result {
+ todo!()
+}
+
+#[napi_sym::napi_sym]
+fn napi_async_destroy(_env: *mut Env, _async_context: *mut ()) -> Result {
+ todo!()
+}