diff --git a/Libraries/LibJS/Bytecode/Builtins.h b/Libraries/LibJS/Bytecode/Builtins.h index a49f4802db7..270b68e7c50 100644 --- a/Libraries/LibJS/Bytecode/Builtins.h +++ b/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 { diff --git a/Libraries/LibJS/Bytecode/Interpreter.cpp b/Libraries/LibJS/Bytecode/Interpreter.cpp index cefb789d7d9..e868d4cd5c5 100644 --- a/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -2599,6 +2599,8 @@ static ThrowCompletionOr 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: diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp index f9065bf7e6e..88d846c3888 100644 --- a/Libraries/LibJS/Runtime/MathObject.cpp +++ b/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 diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h index 9a938aa5412..19d5efd84ad 100644 --- a/Libraries/LibJS/Runtime/MathObject.h +++ b/Libraries/LibJS/Runtime/MathObject.h @@ -28,6 +28,7 @@ public: static ThrowCompletionOr abs_impl(VM&, Value); static ThrowCompletionOr sum_precise_impl(VM&, Value); static ThrowCompletionOr imul_impl(VM&, Value, Value); + static Value random_impl(); private: explicit MathObject(Realm&);