|
@@ -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);
|
|
@@ -60,7 +60,7 @@ void MathObject::initialize(Realm& realm)
|
|
|
define_native_function(realm, vm.names.fround, fround, 1, attr);
|
|
|
define_native_function(realm, vm.names.f16round, f16round, 1, attr);
|
|
|
define_native_function(realm, vm.names.hypot, hypot, 2, attr);
|
|
|
- define_native_function(realm, vm.names.imul, imul, 2, attr);
|
|
|
+ define_native_function(realm, vm.names.imul, imul, 2, attr, Bytecode::Builtin::MathImul);
|
|
|
define_native_function(realm, vm.names.log, log, 1, attr, Bytecode::Builtin::MathLog);
|
|
|
define_native_function(realm, vm.names.log2, log2, 1, attr);
|
|
|
define_native_function(realm, vm.names.log10, log10, 1, attr);
|
|
@@ -587,19 +587,25 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::hypot)
|
|
|
}
|
|
|
|
|
|
// 21.3.2.19 Math.imul ( x, y ), https://tc39.es/ecma262/#sec-math.imul
|
|
|
-JS_DEFINE_NATIVE_FUNCTION(MathObject::imul)
|
|
|
+ThrowCompletionOr<Value> MathObject::imul_impl(VM& vm, Value arg_a, Value arg_b)
|
|
|
{
|
|
|
// 1. Let a be ℝ(? ToUint32(x)).
|
|
|
- auto a = TRY(vm.argument(0).to_u32(vm));
|
|
|
+ auto const a = TRY(arg_a.to_u32(vm));
|
|
|
|
|
|
// 2. Let b be ℝ(? ToUint32(y)).
|
|
|
- auto b = TRY(vm.argument(1).to_u32(vm));
|
|
|
+ auto const b = TRY(arg_b.to_u32(vm));
|
|
|
|
|
|
// 3. Let product be (a × b) modulo 2^32.
|
|
|
// 4. If product ≥ 2^31, return 𝔽(product - 2^32); otherwise return 𝔽(product).
|
|
|
return Value(static_cast<i32>(a * b));
|
|
|
}
|
|
|
|
|
|
+// 21.3.2.19 Math.imul ( x, y ), https://tc39.es/ecma262/#sec-math.imul
|
|
|
+JS_DEFINE_NATIVE_FUNCTION(MathObject::imul)
|
|
|
+{
|
|
|
+ return imul_impl(vm, vm.argument(0), vm.argument(1));
|
|
|
+}
|
|
|
+
|
|
|
// 21.3.2.20 Math.log ( x ), https://tc39.es/ecma262/#sec-math.log
|
|
|
ThrowCompletionOr<Value> MathObject::log_impl(VM& vm, Value x)
|
|
|
{
|
|
@@ -834,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
|