Skip to content

Commit

Permalink
fix: address more cr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
amilajack committed Nov 22, 2020
1 parent 712af84 commit 853a345
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 25 deletions.
10 changes: 1 addition & 9 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,5 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: install g++-4.8
run: sudo apt-get install -y g++-4.8
- name: Start Xvfb
run: |
sudo apt-get install libwoff1 libopus0 libwebp6 libwebpdemux2 libenchant1c2a libgudev-1.0-0 libsecret-1-0 libhyphen0 libgdk-pixbuf2.0-0
sudo apt-get install xvfb
export DISPLAY=:99.0
Xvfb -ac :99 -screen 0 1280x1024x16 > /dev/null 2>&1 &
- name: run cargo test
run: xvfb-run --auto-servernum cargo test --release -- --nocapture
run: cargo test --release -- --nocapture
9 changes: 6 additions & 3 deletions crates/neon-runtime/src/napi/date.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use std::mem::MaybeUninit;
use nodejs_sys as napi;
use raw::{Env, Local};

pub unsafe extern "C" fn new_date(env: Env, out: *mut Local, value: f64) {
let status = napi::napi_create_date(env, value, out);
pub unsafe extern "C" fn new_date(env: Env, value: f64) -> Local {
let mut local = MaybeUninit::zeroed();
let status = napi::napi_create_date(env, value, local.as_mut_ptr());
assert_eq!(status, napi::napi_status::napi_ok);
local.assume_init()
}

pub unsafe extern "C" fn value(env: Env, p: Local) -> f64 {
let mut value = 0.0;
let status = napi::napi_get_date_value(env, p, &mut value as *mut f64);
let status = napi::napi_get_date_value(env, p, &mut value as *mut _);
assert_eq!(status, napi::napi_status::napi_ok);
return value;
}
2 changes: 1 addition & 1 deletion src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ pub trait Context<'a>: ContextInternal<'a> {
}

#[cfg(feature = "napi-runtime")]
fn date<V: Into<f64>>(&mut self, value: V) -> Handle<'a, JsDate> {
fn date(&mut self, value: impl Into<f64>) -> Handle<'a, JsDate> {
JsDate::new(self, value)
}

Expand Down
16 changes: 6 additions & 10 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,21 +442,17 @@ impl Managed for JsDate {

#[cfg(feature = "napi-runtime")]
impl JsDate {
pub const MIN_VALID_VALUE: f64 = -8640000000000000.0;
pub const MAX_VALID_VALUE: f64 = 8640000000000000.0;
pub const MIN_VALUE: f64 = -8.64e15;
pub const MAX_VALUE: f64 = 8.64e15;

pub(crate) fn new_internal<'a>(env: Env, value: f64) -> Handle<'a, JsDate> {
pub fn new<'a, C: Context<'a>, T: Into<f64>>(cx: &mut C, time: T) -> Handle<'a, JsDate> {
let env = cx.env().to_raw();
unsafe {
let mut local: raw::Local = std::mem::zeroed();
neon_runtime::date::new_date( env.to_raw(), &mut local, value);
let local = neon_runtime::date::new_date(env, time.into());
Handle::new_internal(JsDate(local))
}
}

pub fn new<'a, C: Context<'a>, T: Into<f64>>(cx: &mut C, time: T) -> Handle<'a, JsDate> {
JsDate::new_internal(cx.env(), time.into())
}

pub fn value<'a, C: Context<'a>>(self, cx: &mut C) -> f64 {
let env = cx.env().to_raw();
unsafe {
Expand All @@ -466,7 +462,7 @@ impl JsDate {

pub fn is_valid<'a, C: Context<'a>>(self, cx: &mut C) -> bool {
let value = self.value(cx);
return value <= JsDate::MAX_VALID_VALUE && value >= JsDate::MIN_VALID_VALUE;
return value <= JsDate::MAX_VALUE && value >= JsDate::MIN_VALUE;
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/napi/lib/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ describe('JsDate', function() {
});

it('should check if date is invalid', function () {
const dateIsValid = addon.check_date_is_invalid();
assert.isFalse(dateIsValid);
const date = addon.create_and_get_invalid_date();
assert.isNaN(date);
});

it('should get date value', function () {
Expand Down
9 changes: 9 additions & 0 deletions test/napi/native/src/js/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@ pub fn check_date_is_invalid(mut cx: FunctionContext) -> JsResult<JsBoolean> {
let time = 2f64.powf(64.0);
let date = cx.date(time);
let is_valid = date.is_valid(&mut cx);
let val = date.value(&mut cx);
Ok(cx.boolean(is_valid))
}

pub fn create_and_get_invalid_date(mut cx: FunctionContext) -> JsResult<JsNumber> {
let time = 2f64.powf(64.0);
let date = cx.date(time).value(&mut cx);
assert!(!cx.date(time).is_valid(&mut cx));
assert!(cx.date(time).value(&mut cx).is_nan());
Ok(cx.number(date))
}

pub fn get_date_value(mut cx: FunctionContext) -> JsResult<JsNumber> {
let date = cx.date(31415);
let value = date.value(&mut cx);
Expand Down
1 change: 1 addition & 0 deletions test/napi/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ register_module!(|mut cx| {
cx.export_function("check_date_is_invalid", check_date_is_invalid)?;
cx.export_function("check_date_is_valid", check_date_is_valid)?;
cx.export_function("create_date_from_value", create_date_from_value)?;
cx.export_function("create_and_get_invalid_date", create_and_get_invalid_date)?;

cx.export_function("is_array", is_array)?;
cx.export_function("is_array_buffer", is_array_buffer)?;
Expand Down

0 comments on commit 853a345

Please sign in to comment.