summaryrefslogtreecommitdiff
path: root/test_napi/src
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-01-13 12:53:45 +0100
committerGitHub <noreply@github.com>2023-01-13 12:53:45 +0100
commit291dcc31f778dc137f33b39d4750ed478a223ebd (patch)
tree997d9628338b3d9acb6ac67896ce0530e793c762 /test_napi/src
parenta00e432297d2ae119c8e1097aec74badc886f912 (diff)
fix(napi): date and unwrap handling (#17369)
Diffstat (limited to 'test_napi/src')
-rw-r--r--test_napi/src/date.rs74
-rw-r--r--test_napi/src/lib.rs2
2 files changed, 76 insertions, 0 deletions
diff --git a/test_napi/src/date.rs b/test_napi/src/date.rs
new file mode 100644
index 000000000..8926fe514
--- /dev/null
+++ b/test_napi/src/date.rs
@@ -0,0 +1,74 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+
+use crate::assert_napi_ok;
+use crate::napi_get_callback_info;
+use crate::napi_new_property;
+use napi_sys::ValueType::napi_number;
+use napi_sys::*;
+use std::ptr;
+
+extern "C" fn create_date(
+ env: napi_env,
+ info: napi_callback_info,
+) -> napi_value {
+ let (args, argc, _) = napi_get_callback_info!(env, info, 1);
+ assert_eq!(argc, 1);
+
+ let mut ty = -1;
+ assert_napi_ok!(napi_typeof(env, args[0], &mut ty));
+ assert_eq!(ty, napi_number);
+
+ let mut time = -1.0;
+ assert_napi_ok!(napi_get_value_double(env, args[0], &mut time));
+
+ let mut date: napi_value = ptr::null_mut();
+ assert_napi_ok!(napi_create_date(env, time, &mut date));
+
+ date
+}
+
+extern "C" fn is_date(env: napi_env, info: napi_callback_info) -> napi_value {
+ let (args, argc, _) = napi_get_callback_info!(env, info, 1);
+ assert_eq!(argc, 1);
+
+ let date: napi_value = args[0];
+ let mut result: napi_value = std::ptr::null_mut();
+ let mut is_date = false;
+
+ assert_napi_ok!(napi_is_date(env, date, &mut is_date));
+ assert_napi_ok!(napi_get_boolean(env, is_date, &mut result));
+
+ result
+}
+
+extern "C" fn get_date_value(
+ env: napi_env,
+ info: napi_callback_info,
+) -> napi_value {
+ let (args, argc, _) = napi_get_callback_info!(env, info, 1);
+ assert_eq!(argc, 1);
+
+ let date: napi_value = args[0];
+ let mut result: napi_value = std::ptr::null_mut();
+ let mut value = 0.0;
+
+ assert_napi_ok!(napi_get_date_value(env, date, &mut value));
+ assert_napi_ok!(napi_create_double(env, value, &mut result));
+
+ result
+}
+
+pub fn init(env: napi_env, exports: napi_value) {
+ let properties = &[
+ napi_new_property!(env, "createDate", create_date),
+ napi_new_property!(env, "isDate", is_date),
+ napi_new_property!(env, "getDateValue", get_date_value),
+ ];
+
+ assert_napi_ok!(napi_define_properties(
+ env,
+ exports,
+ properties.len(),
+ properties.as_ptr()
+ ));
+}
diff --git a/test_napi/src/lib.rs b/test_napi/src/lib.rs
index 025fbf5d2..2d43b0aea 100644
--- a/test_napi/src/lib.rs
+++ b/test_napi/src/lib.rs
@@ -11,6 +11,7 @@ pub mod arraybuffer;
pub mod r#async;
pub mod callback;
pub mod coerce;
+pub mod date;
pub mod numbers;
pub mod object_wrap;
pub mod primitives;
@@ -127,6 +128,7 @@ unsafe extern "C" fn napi_register_module_v1(
object_wrap::init(env, exports);
callback::init(env, exports);
r#async::init(env, exports);
+ date::init(env, exports);
tsfn::init(env, exports);
init_cleanup_hook(env, exports);