|
|
|
@ -19,12 +19,18 @@ use crate::{
|
|
|
|
|
exec::Interpreter, |
|
|
|
|
BoaProfiler, |
|
|
|
|
}; |
|
|
|
|
use rand::random; |
|
|
|
|
use std::f64; |
|
|
|
|
|
|
|
|
|
#[cfg(test)] |
|
|
|
|
mod tests; |
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
|
|
|
|
pub(crate) struct Math; |
|
|
|
|
|
|
|
|
|
impl Math { |
|
|
|
|
/// The name of the object.
|
|
|
|
|
pub(crate) const NAME: &'static str = "Math"; |
|
|
|
|
|
|
|
|
|
/// Get the absolute value of a number.
|
|
|
|
|
///
|
|
|
|
|
/// More information:
|
|
|
|
@ -33,7 +39,7 @@ mod tests;
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.abs
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/abs
|
|
|
|
|
pub fn abs(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn abs(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -49,7 +55,7 @@ pub fn abs(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.acos
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/acos
|
|
|
|
|
pub fn acos(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn acos(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -65,7 +71,7 @@ pub fn acos(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.acosh
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/acosh
|
|
|
|
|
pub fn acosh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn acosh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -81,7 +87,7 @@ pub fn acosh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.asin
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/asin
|
|
|
|
|
pub fn asin(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn asin(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -97,7 +103,7 @@ pub fn asin(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.asinh
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/asinh
|
|
|
|
|
pub fn asinh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn asinh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -113,7 +119,7 @@ pub fn asinh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.atan
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan
|
|
|
|
|
pub fn atan(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn atan(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -129,7 +135,7 @@ pub fn atan(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.atanh
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atanh
|
|
|
|
|
pub fn atanh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn atanh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -145,7 +151,7 @@ pub fn atanh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.atan2
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan2
|
|
|
|
|
pub fn atan2(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn atan2(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -162,7 +168,7 @@ pub fn atan2(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.cbrt
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cbrt
|
|
|
|
|
pub fn cbrt(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn cbrt(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -178,7 +184,7 @@ pub fn cbrt(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.ceil
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil
|
|
|
|
|
pub fn ceil(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn ceil(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -194,7 +200,7 @@ pub fn ceil(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.cos
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cos
|
|
|
|
|
pub fn cos(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn cos(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -210,7 +216,7 @@ pub fn cos(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.cosh
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cosh
|
|
|
|
|
pub fn cosh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn cosh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -226,7 +232,7 @@ pub fn cosh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.exp
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/exp
|
|
|
|
|
pub fn exp(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn exp(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -242,7 +248,7 @@ pub fn exp(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.floor
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
|
|
|
|
|
pub fn floor(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn floor(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -258,7 +264,7 @@ pub fn floor(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.log
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log
|
|
|
|
|
pub fn log(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn log(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -280,7 +286,7 @@ pub fn log(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.log10
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10
|
|
|
|
|
pub fn log10(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn log10(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -302,7 +308,7 @@ pub fn log10(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.log2
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2
|
|
|
|
|
pub fn log2(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn log2(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -324,7 +330,7 @@ pub fn log2(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.max
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
|
|
|
|
|
pub fn max(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn max(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
let mut max = f64::NEG_INFINITY; |
|
|
|
|
for arg in args { |
|
|
|
|
let num = f64::from(arg); |
|
|
|
@ -341,7 +347,7 @@ pub fn max(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.min
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min
|
|
|
|
|
pub fn min(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn min(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
let mut max = f64::INFINITY; |
|
|
|
|
for arg in args { |
|
|
|
|
let num = f64::from(arg); |
|
|
|
@ -358,7 +364,7 @@ pub fn min(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.pow
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow
|
|
|
|
|
pub fn pow(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn pow(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.len() >= 2 { |
|
|
|
|
let num = f64::from(args.get(0).expect("Could not get argument")); |
|
|
|
|
let power = f64::from(args.get(1).expect("Could not get argument")); |
|
|
|
@ -376,8 +382,8 @@ pub fn pow(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.random
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
|
|
|
|
|
pub fn _random(_: &mut Value, _: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(random::<f64>())) |
|
|
|
|
pub(crate) fn random(_: &mut Value, _: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(rand::random::<f64>())) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Round a number to the nearest integer.
|
|
|
|
@ -388,7 +394,7 @@ pub fn _random(_: &mut Value, _: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.round
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
|
|
|
|
|
pub fn round(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn round(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -404,7 +410,7 @@ pub fn round(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.sign
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
|
|
|
|
|
pub fn sign(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn sign(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -426,7 +432,7 @@ pub fn sign(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.sin
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sin
|
|
|
|
|
pub fn sin(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn sin(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -442,7 +448,7 @@ pub fn sin(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.sinh
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sinh
|
|
|
|
|
pub fn sinh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn sinh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -458,7 +464,7 @@ pub fn sinh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.sqrt
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sqrt
|
|
|
|
|
pub fn sqrt(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn sqrt(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -466,7 +472,7 @@ pub fn sqrt(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
})) |
|
|
|
|
} |
|
|
|
|
/// Get the tangent of a number
|
|
|
|
|
pub fn tan(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn tan(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -482,7 +488,7 @@ pub fn tan(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.tanh
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/tanh
|
|
|
|
|
pub fn tanh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn tanh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -498,7 +504,7 @@ pub fn tanh(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
|
|
|
|
|
///
|
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-math.trunc
|
|
|
|
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc
|
|
|
|
|
pub fn trunc(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
pub(crate) fn trunc(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
|
|
|
|
Ok(Value::from(if args.is_empty() { |
|
|
|
|
f64::NAN |
|
|
|
|
} else { |
|
|
|
@ -507,54 +513,59 @@ pub fn trunc(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Create a new `Math` object
|
|
|
|
|
pub fn create(global: &Value) -> Value { |
|
|
|
|
pub(crate) fn create(global: &Value) -> Value { |
|
|
|
|
let _timer = BoaProfiler::global().start_event("math:create", "init"); |
|
|
|
|
let math = Value::new_object(Some(global)); |
|
|
|
|
|
|
|
|
|
math.set_field("E", Value::from(f64::consts::E)); |
|
|
|
|
math.set_field("LN2", Value::from(f64::consts::LN_2)); |
|
|
|
|
math.set_field("LN10", Value::from(f64::consts::LN_10)); |
|
|
|
|
math.set_field("LOG2E", Value::from(f64::consts::LOG2_E)); |
|
|
|
|
math.set_field("LOG10E", Value::from(f64::consts::LOG10_E)); |
|
|
|
|
math.set_field("SQRT1_2", Value::from(0.5_f64.sqrt())); |
|
|
|
|
math.set_field("SQRT2", Value::from(f64::consts::SQRT_2)); |
|
|
|
|
math.set_field("PI", Value::from(f64::consts::PI)); |
|
|
|
|
make_builtin_fn(abs, "abs", &math, 1); |
|
|
|
|
make_builtin_fn(acos, "acos", &math, 1); |
|
|
|
|
make_builtin_fn(acosh, "acosh", &math, 1); |
|
|
|
|
make_builtin_fn(asin, "asin", &math, 1); |
|
|
|
|
make_builtin_fn(asinh, "asinh", &math, 1); |
|
|
|
|
make_builtin_fn(atan, "atan", &math, 1); |
|
|
|
|
make_builtin_fn(atanh, "atanh", &math, 1); |
|
|
|
|
make_builtin_fn(atan2, "atan2", &math, 2); |
|
|
|
|
make_builtin_fn(cbrt, "cbrt", &math, 1); |
|
|
|
|
make_builtin_fn(ceil, "ceil", &math, 1); |
|
|
|
|
make_builtin_fn(cos, "cos", &math, 1); |
|
|
|
|
make_builtin_fn(cosh, "cosh", &math, 1); |
|
|
|
|
make_builtin_fn(exp, "exp", &math, 1); |
|
|
|
|
make_builtin_fn(floor, "floor", &math, 1); |
|
|
|
|
make_builtin_fn(log, "log", &math, 1); |
|
|
|
|
make_builtin_fn(log10, "log10", &math, 1); |
|
|
|
|
make_builtin_fn(log2, "log2", &math, 1); |
|
|
|
|
make_builtin_fn(max, "max", &math, 2); |
|
|
|
|
make_builtin_fn(min, "min", &math, 2); |
|
|
|
|
make_builtin_fn(pow, "pow", &math, 2); |
|
|
|
|
make_builtin_fn(_random, "random", &math, 0); |
|
|
|
|
make_builtin_fn(round, "round", &math, 1); |
|
|
|
|
make_builtin_fn(sign, "sign", &math, 1); |
|
|
|
|
make_builtin_fn(sin, "sin", &math, 1); |
|
|
|
|
make_builtin_fn(sinh, "sinh", &math, 1); |
|
|
|
|
make_builtin_fn(sqrt, "sqrt", &math, 1); |
|
|
|
|
make_builtin_fn(tan, "tan", &math, 1); |
|
|
|
|
make_builtin_fn(tanh, "tanh", &math, 1); |
|
|
|
|
make_builtin_fn(trunc, "trunc", &math, 1); |
|
|
|
|
{ |
|
|
|
|
let mut properties = math.as_object_mut().unwrap(); |
|
|
|
|
properties.insert_field("E", Value::from(f64::consts::E)); |
|
|
|
|
properties.insert_field("LN2", Value::from(f64::consts::LN_2)); |
|
|
|
|
properties.insert_field("LN10", Value::from(f64::consts::LN_10)); |
|
|
|
|
properties.insert_field("LOG2E", Value::from(f64::consts::LOG2_E)); |
|
|
|
|
properties.insert_field("LOG10E", Value::from(f64::consts::LOG10_E)); |
|
|
|
|
properties.insert_field("SQRT1_2", Value::from(0.5_f64.sqrt())); |
|
|
|
|
properties.insert_field("SQRT2", Value::from(f64::consts::SQRT_2)); |
|
|
|
|
properties.insert_field("PI", Value::from(f64::consts::PI)); |
|
|
|
|
} |
|
|
|
|
make_builtin_fn(Self::abs, "abs", &math, 1); |
|
|
|
|
make_builtin_fn(Self::acos, "acos", &math, 1); |
|
|
|
|
make_builtin_fn(Self::acosh, "acosh", &math, 1); |
|
|
|
|
make_builtin_fn(Self::asin, "asin", &math, 1); |
|
|
|
|
make_builtin_fn(Self::asinh, "asinh", &math, 1); |
|
|
|
|
make_builtin_fn(Self::atan, "atan", &math, 1); |
|
|
|
|
make_builtin_fn(Self::atanh, "atanh", &math, 1); |
|
|
|
|
make_builtin_fn(Self::atan2, "atan2", &math, 2); |
|
|
|
|
make_builtin_fn(Self::cbrt, "cbrt", &math, 1); |
|
|
|
|
make_builtin_fn(Self::ceil, "ceil", &math, 1); |
|
|
|
|
make_builtin_fn(Self::cos, "cos", &math, 1); |
|
|
|
|
make_builtin_fn(Self::cosh, "cosh", &math, 1); |
|
|
|
|
make_builtin_fn(Self::exp, "exp", &math, 1); |
|
|
|
|
make_builtin_fn(Self::floor, "floor", &math, 1); |
|
|
|
|
make_builtin_fn(Self::log, "log", &math, 1); |
|
|
|
|
make_builtin_fn(Self::log10, "log10", &math, 1); |
|
|
|
|
make_builtin_fn(Self::log2, "log2", &math, 1); |
|
|
|
|
make_builtin_fn(Self::max, "max", &math, 2); |
|
|
|
|
make_builtin_fn(Self::min, "min", &math, 2); |
|
|
|
|
make_builtin_fn(Self::pow, "pow", &math, 2); |
|
|
|
|
make_builtin_fn(Self::random, "random", &math, 0); |
|
|
|
|
make_builtin_fn(Self::round, "round", &math, 1); |
|
|
|
|
make_builtin_fn(Self::sign, "sign", &math, 1); |
|
|
|
|
make_builtin_fn(Self::sin, "sin", &math, 1); |
|
|
|
|
make_builtin_fn(Self::sinh, "sinh", &math, 1); |
|
|
|
|
make_builtin_fn(Self::sqrt, "sqrt", &math, 1); |
|
|
|
|
make_builtin_fn(Self::tan, "tan", &math, 1); |
|
|
|
|
make_builtin_fn(Self::tanh, "tanh", &math, 1); |
|
|
|
|
make_builtin_fn(Self::trunc, "trunc", &math, 1); |
|
|
|
|
|
|
|
|
|
math |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Initialise the `Math` object on the global object.
|
|
|
|
|
#[inline] |
|
|
|
|
pub fn init(global: &Value) { |
|
|
|
|
let _timer = BoaProfiler::global().start_event("math", "init"); |
|
|
|
|
global.set_field("Math", create(global)); |
|
|
|
|
pub(crate) fn init(global: &Value) -> (&str, Value) { |
|
|
|
|
let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); |
|
|
|
|
|
|
|
|
|
(Self::NAME, Self::create(global)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|