From afff22830862d8812880ec92d37031a56ef799d8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 5 Apr 2020 00:26:56 +0200 Subject: [PATCH] LibJS: Add Math.floor() --- Libraries/LibJS/Runtime/MathObject.cpp | 12 ++++++++++++ Libraries/LibJS/Runtime/MathObject.h | 1 + 2 files changed, 13 insertions(+) diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp index 2871feef99..00425bf4c4 100644 --- a/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Libraries/LibJS/Runtime/MathObject.cpp @@ -37,6 +37,7 @@ MathObject::MathObject() put_native_function("abs", abs, 1); put_native_function("random", random); put_native_function("sqrt", sqrt, 1); + put_native_function("floor", floor, 1); put("E", Value(M_E)); put("LN2", Value(M_LN2)); @@ -84,4 +85,15 @@ Value MathObject::sqrt(Interpreter& interpreter) return Value(::sqrt(number.as_double())); } +Value MathObject::floor(Interpreter& interpreter) +{ + if (!interpreter.argument_count()) + return js_nan(); + + auto number = interpreter.argument(0).to_number(); + if (number.is_nan()) + return js_nan(); + return Value(::floor(number.as_double())); +} + } diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h index 21f6130e75..6e11daf627 100644 --- a/Libraries/LibJS/Runtime/MathObject.h +++ b/Libraries/LibJS/Runtime/MathObject.h @@ -41,6 +41,7 @@ private: static Value abs(Interpreter&); static Value random(Interpreter&); static Value sqrt(Interpreter&); + static Value floor(Interpreter&); }; }