Browse Source

LibJS: Add builtin for Math.random()

Andreas Kling 1 week ago
parent
commit
ec590ef3e6

+ 10 - 9
Libraries/LibJS/Bytecode/Builtins.h

@@ -12,15 +12,16 @@
 namespace JS::Bytecode {
 
 // TitleCaseName, snake_case_name, base, property, argument_count
-#define JS_ENUMERATE_BUILTINS(O)             \
-    O(MathAbs, math_abs, Math, abs, 1)       \
-    O(MathLog, math_log, Math, log, 1)       \
-    O(MathPow, math_pow, Math, pow, 2)       \
-    O(MathExp, math_exp, Math, exp, 1)       \
-    O(MathCeil, math_ceil, Math, ceil, 1)    \
-    O(MathFloor, math_floor, Math, floor, 1) \
-    O(MathImul, math_imul, Math, imul, 2)    \
-    O(MathRound, math_round, Math, round, 1) \
+#define JS_ENUMERATE_BUILTINS(O)                \
+    O(MathAbs, math_abs, Math, abs, 1)          \
+    O(MathLog, math_log, Math, log, 1)          \
+    O(MathPow, math_pow, Math, pow, 2)          \
+    O(MathExp, math_exp, Math, exp, 1)          \
+    O(MathCeil, math_ceil, Math, ceil, 1)       \
+    O(MathFloor, math_floor, Math, floor, 1)    \
+    O(MathImul, math_imul, Math, imul, 2)       \
+    O(MathRandom, math_random, Math, random, 0) \
+    O(MathRound, math_round, Math, round, 1)    \
     O(MathSqrt, math_sqrt, Math, sqrt, 1)
 
 enum class Builtin : u8 {

+ 2 - 0
Libraries/LibJS/Bytecode/Interpreter.cpp

@@ -2599,6 +2599,8 @@ static ThrowCompletionOr<Value> dispatch_builtin_call(Bytecode::Interpreter& int
         return TRY(MathObject::floor_impl(interpreter.vm(), interpreter.get(arguments[0])));
     case Builtin::MathImul:
         return TRY(MathObject::imul_impl(interpreter.vm(), interpreter.get(arguments[0]), interpreter.get(arguments[1])));
+    case Builtin::MathRandom:
+        return MathObject::random_impl();
     case Builtin::MathRound:
         return TRY(MathObject::round_impl(interpreter.vm(), interpreter.get(arguments[0])));
     case Builtin::MathSqrt:

+ 9 - 4
Libraries/LibJS/Runtime/MathObject.cpp

@@ -32,7 +32,7 @@ void MathObject::initialize(Realm& realm)
     Base::initialize(realm);
     u8 attr = Attribute::Writable | Attribute::Configurable;
     define_native_function(realm, vm.names.abs, abs, 1, attr, Bytecode::Builtin::MathAbs);
-    define_native_function(realm, vm.names.random, random, 0, attr);
+    define_native_function(realm, vm.names.random, random, 0, attr, Bytecode::Builtin::MathRandom);
     define_native_function(realm, vm.names.sqrt, sqrt, 1, attr, Bytecode::Builtin::MathSqrt);
     define_native_function(realm, vm.names.floor, floor, 1, attr, Bytecode::Builtin::MathFloor);
     define_native_function(realm, vm.names.ceil, ceil, 1, attr, Bytecode::Builtin::MathCeil);
@@ -840,14 +840,19 @@ private:
     u64 m_high { 0 };
 };
 
-// 21.3.2.27 Math.random ( ), https://tc39.es/ecma262/#sec-math.random
-JS_DEFINE_NATIVE_FUNCTION(MathObject::random)
+Value MathObject::random_impl()
 {
     // This function returns a Number value with positive sign, greater than or equal to +0𝔽 but strictly less than 1𝔽,
     // chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an
     // implementation-defined algorithm or strategy.
     static XorShift128PlusPlusRNG rng;
-    return rng.get();
+    return Value(rng.get());
+}
+
+// 21.3.2.27 Math.random ( ), https://tc39.es/ecma262/#sec-math.random
+JS_DEFINE_NATIVE_FUNCTION(MathObject::random)
+{
+    return random_impl();
 }
 
 // 21.3.2.28 Math.round ( x ), https://tc39.es/ecma262/#sec-math.round

+ 1 - 0
Libraries/LibJS/Runtime/MathObject.h

@@ -28,6 +28,7 @@ public:
     static ThrowCompletionOr<Value> abs_impl(VM&, Value);
     static ThrowCompletionOr<Value> sum_precise_impl(VM&, Value);
     static ThrowCompletionOr<Value> imul_impl(VM&, Value, Value);
+    static Value random_impl();
 
 private:
     explicit MathObject(Realm&);