From 0df3d046b5816a3f8482b3bd91ab8c8ec70f65d6 Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Fri, 12 Oct 2018 00:09:51 +0100 Subject: [PATCH] updating value --- src/lib/js/value.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/lib/js/value.rs b/src/lib/js/value.rs index 2f63ef8408..31efa2c766 100644 --- a/src/lib/js/value.rs +++ b/src/lib/js/value.rs @@ -106,4 +106,19 @@ impl Value { _ => false, } } + + /// Converts the value into a 64-bit floating point number + pub fn to_num(&self) -> f64 { + match *self.ptr { + ValueData::Object(_) | ValueData::Undefined | ValueData::Function(_) => std::f64::NAN, + ValueData::String(ref str) => match from_str(str) { + Some(num) => num, + None => std::f64::NAN, + }, + ValueData::Number(num) => num, + ValueData::Boolean(true) => 1.0, + ValueData::Boolean(false) | ValueData::Null => 0.0, + ValueData::Integer(num) => num as f64, + } + } }