mirror of
https://github.com/denoland/deno
synced 2024-08-25 02:57:22 +00:00
fix(napi): date and unwrap handling (#17369)
This commit is contained in:
parent
a00e432297
commit
291dcc31f7
|
@ -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(())
|
||||
}
|
||||
|
|
17
test_napi/date_test.js
Normal file
17
test_napi/date_test.js
Normal file
|
@ -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);
|
||||
});
|
74
test_napi/src/date.rs
Normal file
74
test_napi/src/date.rs
Normal file
|
@ -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()
|
||||
));
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue