From 6bdf021b0cf4f9509abcdec255a3d0a842ceae33 Mon Sep 17 00:00:00 2001 From: davidot Date: Mon, 28 Nov 2022 12:00:09 +0100 Subject: [PATCH] LibJS: Add spec comments and check for edge cases in Math.asin --- Userland/Libraries/LibJS/Runtime/MathObject.cpp | 9 +++++++++ .../Libraries/LibJS/Tests/builtins/Math/Math.asin.js | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/MathObject.cpp b/Userland/Libraries/LibJS/Runtime/MathObject.cpp index 7e09f933109..da51b95897c 100644 --- a/Userland/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/MathObject.cpp @@ -319,9 +319,18 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::acosh) // 21.3.2.4 Math.asin ( x ), https://tc39.es/ecma262/#sec-math.asin JS_DEFINE_NATIVE_FUNCTION(MathObject::asin) { + // 1. Let n be ? ToNumber(x). auto number = TRY(vm.argument(0).to_number(vm)); + + // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n. if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero()) return number; + + // 3. If n > 1𝔽 or n < -1𝔽, return NaN. + if (number.as_double() > 1 || number.as_double() < -1) + return js_nan(); + + // 4. Return an implementation-approximated Number value representing the result of the inverse sine of ℝ(n). return Value(::asin(number.as_double())); } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Math/Math.asin.js b/Userland/Libraries/LibJS/Tests/builtins/Math/Math.asin.js index 928ce7be5c3..a18210ea4e9 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Math/Math.asin.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Math/Math.asin.js @@ -12,4 +12,8 @@ test("basic functionality", () => { expect(Math.asin([1, 2, 3])).toBeNaN(); expect(Math.asin({})).toBeNaN(); expect(Math.asin("foo")).toBeNaN(); + expect(Math.asin(NaN)).toBe(NaN); + expect(Math.asin(-0.0)).toBe(-0.0); + expect(Math.asin(1.1)).toBe(NaN); + expect(Math.asin(-1.1)).toBe(NaN); });