summaryrefslogtreecommitdiff
path: root/Libraries/LibJS
diff options
context:
space:
mode:
Diffstat (limited to 'Libraries/LibJS')
-rw-r--r--Libraries/LibJS/Runtime/MathObject.cpp18
-rw-r--r--Libraries/LibJS/Runtime/MathObject.h1
-rw-r--r--Libraries/LibJS/Tests/Math.sqrt.js7
3 files changed, 23 insertions, 3 deletions
diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp
index c35d254177..b1755144af 100644
--- a/Libraries/LibJS/Runtime/MathObject.cpp
+++ b/Libraries/LibJS/Runtime/MathObject.cpp
@@ -28,7 +28,7 @@
#include <AK/Function.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/MathObject.h>
-#include <LibM/math.h>
+#include <math.h>
namespace JS {
@@ -36,6 +36,7 @@ MathObject::MathObject()
{
put_native_function("abs", abs, 1);
put_native_function("random", random);
+ put_native_function("sqrt", sqrt);
put("E", Value(M_E));
put("LN2", Value(M_LN2));
@@ -43,8 +44,8 @@ MathObject::MathObject()
put("LOG2E", Value(log2(M_E)));
put("LOG10E", Value(log10(M_E)));
put("PI", Value(M_PI));
- put("SQRT1_2", Value(sqrt(1 / 2)));
- put("SQRT2", Value(sqrt(2)));
+ put("SQRT1_2", Value(::sqrt(1 / 2)));
+ put("SQRT2", Value(::sqrt(2)));
}
MathObject::~MathObject()
@@ -72,4 +73,15 @@ Value MathObject::random(Interpreter&)
return Value(r);
}
+Value MathObject::sqrt(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(::sqrt(number.as_double()));
+}
+
}
diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h
index 1953eeb3a2..21f6130e75 100644
--- a/Libraries/LibJS/Runtime/MathObject.h
+++ b/Libraries/LibJS/Runtime/MathObject.h
@@ -40,6 +40,7 @@ private:
static Value abs(Interpreter&);
static Value random(Interpreter&);
+ static Value sqrt(Interpreter&);
};
}
diff --git a/Libraries/LibJS/Tests/Math.sqrt.js b/Libraries/LibJS/Tests/Math.sqrt.js
new file mode 100644
index 0000000000..c8574d71cf
--- /dev/null
+++ b/Libraries/LibJS/Tests/Math.sqrt.js
@@ -0,0 +1,7 @@
+function assert(x) { if (!x) throw 1; }
+
+try {
+ assert(Math.sqrt(9) === 3);
+ console.log("PASS");
+} catch {
+}