forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(napi): date and unwrap handling (denoland#17369)
- Loading branch information
1 parent
a00e432
commit 291dcc3
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters