summaryrefslogtreecommitdiff
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
parenta00e432297d2ae119c8e1097aec74badc886f912 (diff)
fix(napi): date and unwrap handling (#17369)
-rw-r--r--cli/napi/js_native_api.rs9
-rw-r--r--test_napi/date_test.js17
-rw-r--r--test_napi/src/date.rs74
-rw-r--r--test_napi/src/lib.rs2
4 files changed, 101 insertions, 1 deletions
diff --git a/cli/napi/js_native_api.rs b/cli/napi/js_native_api.rs
index c72da9b02..f58bd3004 100644
--- a/cli/napi/js_native_api.rs
+++ b/cli/napi/js_native_api.rs
@@ -1484,6 +1484,11 @@ fn napi_get_date_value(
) -> Result {
let env: &mut Env = env.as_mut().ok_or(Error::InvalidArg)?;
let value = transmute::<napi_value, v8::Local<v8::Value>>(value);
+
+ if !value.is_date() {
+ return Err(Error::DateExpected);
+ }
+
let date = v8::Local::<v8::Date>::try_from(value).unwrap();
*result = date.number_value(&mut env.scope()).unwrap();
Ok(())
@@ -2287,7 +2292,9 @@ fn napi_unwrap(
let shared = &*(env.shared as *const EnvShared);
let napi_wrap = v8::Local::new(&mut env.scope(), &shared.napi_wrap);
let ext = obj.get_private(&mut env.scope(), napi_wrap).unwrap();
- let ext = v8::Local::<v8::External>::try_from(ext).unwrap();
+ let ext = v8::Local::<v8::External>::try_from(ext)
+ .ok()
+ .ok_or(Error::InvalidArg)?;
*result = ext.value();
Ok(())
}
diff --git a/test_napi/date_test.js b/test_napi/date_test.js
new file mode 100644
index 000000000..8bae30406
--- /dev/null
+++ b/test_napi/date_test.js
@@ -0,0 +1,17 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+
+import { assertEquals, loadTestLibrary } from "./common.js";
+
+const date = loadTestLibrary();
+
+Deno.test("napi date", function () {
+ const dateTypeTestDate = date.createDate(1549183351);
+ assertEquals(date.isDate(dateTypeTestDate), true);
+ assertEquals(date.isDate(new Date(1549183351)), true);
+ assertEquals(date.isDate(2.4), false);
+ assertEquals(date.isDate("not a date"), false);
+ assertEquals(date.isDate(undefined), false);
+ assertEquals(date.isDate(null), false);
+ assertEquals(date.isDate({}), false);
+ assertEquals(date.getDateValue(new Date(1549183351)), 1549183351);
+});
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);