Skip to content

Commit

Permalink
feat: impl new_lossy
Browse files Browse the repository at this point in the history
  • Loading branch information
amilajack committed Jan 7, 2021
1 parent 1d91bda commit 5818563
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1,065 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ neon-runtime = { version = "=0.7.0", path = "crates/neon-runtime" }
neon-macros = { version = "=0.7.0", path = "crates/neon-macros", optional = true }

[features]
default = ["napi-runtime"]
# default = ["legacy-runtime"]
# default = ["napi-runtime"]
default = ["legacy-runtime"]

# Enable static tests. These can be fragile because of variations in Rust compiler
# error message formatting from version to version, so they're disabled by default.
Expand Down
6 changes: 4 additions & 2 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use context::internal::Env;
use handle::{Managed, Handle};
#[cfg(all(feature = "napi-4", feature = "event-queue-api"))]
use event::EventQueue;
use types::{JsValue, Value, JsObject, JsArray, JsFunction, JsBoolean, JsNumber, JsString, StringResult, JsNull, JsUndefined, JsDate};
use types::{JsValue, Value, JsObject, JsArray, JsFunction, JsBoolean, JsNumber, JsString, StringResult, JsNull, JsUndefined};
#[cfg(feature = "napi-1")]
use types::JsDate;
#[cfg(feature = "napi-1")]
use types::boxed::{Finalize, JsBox};
use types::binary::{JsArrayBuffer, JsBuffer};
Expand Down Expand Up @@ -313,7 +315,7 @@ pub trait Context<'a>: ContextInternal<'a> {
JsBuffer::new(self, size)
}

#[cfg(feature = "napi-runtime")]
#[cfg(feature = "napi-1")]
fn date(&mut self, value: impl Into<f64>) -> Handle<'a, JsDate> {
JsDate::new(self, value)
}
Expand Down
76 changes: 65 additions & 11 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use std;
use std::fmt;
use std::os::raw::c_void;
use std::marker::PhantomData;
use std::fmt::Debug;
use std::error::Error;
use neon_runtime;
use neon_runtime::raw;
use context::{Context, FunctionContext};
Expand Down Expand Up @@ -427,26 +429,72 @@ impl ValueInternal for JsNumber {
/// A JavaScript Date object.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
#[cfg(feature = "napi-runtime")]
#[cfg(feature = "napi-1")]
pub struct JsDate(raw::Local);

#[cfg(feature = "napi-runtime")]
#[cfg(feature = "napi-1")]
impl Value for JsDate { }

#[cfg(feature = "napi-runtime")]
#[cfg(feature = "napi-1")]
impl Managed for JsDate {
fn to_raw(self) -> raw::Local { self.0 }

fn from_raw(_: Env, h: raw::Local) -> Self { JsDate(h) }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DateError {
#[cfg(feature = "napi-1")]
#[derive(PartialEq, PartialOrd, Clone, Debug)]
pub struct DateError(f64, DateErrorKind);

#[cfg(feature = "napi-1")]
impl DateError {
pub fn kind(&self) -> DateErrorKind {
self.1
}
}

#[cfg(feature = "napi-1")]
impl fmt::Display for DateError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.1.as_str())
}
}

#[cfg(feature = "napi-1")]
impl Error for DateError {}

#[cfg(feature = "napi-1")]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
pub enum DateErrorKind {
Overflow,
Underflow,
}

#[cfg(feature = "napi-runtime")]
#[cfg(feature = "napi-1")]
impl DateErrorKind {
pub fn as_str(&self) -> &'static str {
match *self {
DateErrorKind::Overflow => "underflow",
DateErrorKind::Underflow => "overflow",
}
}
}

#[cfg(feature = "napi-1")]
pub type DateResult<'a> = Result<Handle<'a, JsDate>, DateError>;

#[cfg(feature = "napi-1")]
impl<'a> JsResultExt<'a, JsDate> for DateResult<'a> {
/// Creates a `Error` on error
fn or_throw<'b, C: Context<'b>>(self, cx: &mut C) -> JsResult<'a, JsDate> {
match self {
Ok(v) => Ok(v),
Err(e) => cx.throw_error(&e.to_string())
}
}
}

#[cfg(feature = "napi-1")]
impl JsDate {
pub const MIN_VALUE: f64 = -8.64e15;
pub const MAX_VALUE: f64 = 8.64e15;
Expand All @@ -459,14 +507,20 @@ impl JsDate {
Handle::new_internal(JsDate(local))
}

pub fn new_lossy<'a, C: Context<'a>, V: Into<f64> + std::cmp::PartialOrd>(cx: &mut C, value: V) -> Result<Handle<'a, JsDate>, DateError> {
pub fn new_lossy<'a, C: Context<'a>, V: Into<f64> + std::cmp::PartialOrd>(cx: &mut C, value: V) -> DateResult<'a> {
let env = cx.env().to_raw();
let time = value.into();
let local = unsafe {
neon_runtime::date::new_date(env, value.into())
neon_runtime::date::new_date(env, time.clone())
};
let date = Handle::new_internal(JsDate(local));
if date.is_valid(cx) { return Ok(date); }
if date.value(cx) > JsDate::MAX_VALUE { Err(DateError::Overflow) } else { Err(DateError::Underflow) }

if date.value(cx) > JsDate::MAX_VALUE {
Err(DateError(time, DateErrorKind::Overflow))
} else {
Err(DateError(time, DateErrorKind::Underflow))
}
}

pub fn value<'a, C: Context<'a>>(self, cx: &mut C) -> f64 {
Expand All @@ -482,7 +536,7 @@ impl JsDate {
}
}

#[cfg(feature = "napi-runtime")]
#[cfg(feature = "napi-1")]
impl ValueInternal for JsDate {
fn name() -> String { "object".to_string() }

Expand All @@ -491,7 +545,7 @@ impl ValueInternal for JsDate {
}
}

#[cfg(feature = "napi-runtime")]
#[cfg(feature = "napi-1")]
impl Object for JsDate { }

/// A JavaScript object.
Expand Down
Loading

0 comments on commit 5818563

Please sign in to comment.