1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 01:51:03 +09:00

LibJS: Boolean, Number and String prototypes should have values too

It appears that calling .valueOf() on an objectified primitive's
prototype should return a value after all.

This matches what other engines are doing.
This commit is contained in:
Andreas Kling 2020-04-10 13:04:12 +02:00
parent c06d5ef114
commit 070a8f2689
Notes: sideshowbarker 2024-07-19 07:44:52 +09:00
11 changed files with 33 additions and 9 deletions

View file

@ -28,6 +28,7 @@
#include <LibJS/Runtime/BooleanObject.h>
namespace JS {
BooleanObject::BooleanObject(bool value)
: m_value(value)
{
@ -37,4 +38,5 @@ BooleanObject::BooleanObject(bool value)
BooleanObject::~BooleanObject()
{
}
}

View file

@ -26,14 +26,15 @@
#include <AK/Function.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/BooleanObject.h>
#include <LibJS/Runtime/BooleanPrototype.h>
#include <LibJS/Runtime/Error.h>
namespace JS {
BooleanPrototype::BooleanPrototype()
: BooleanObject(false)
{
set_prototype(interpreter().object_prototype());
put_native_function("toString", to_string);
put_native_function("valueOf", value_of);
}

View file

@ -26,11 +26,11 @@
#pragma once
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/BooleanObject.h>
namespace JS {
class BooleanPrototype final : public Object {
class BooleanPrototype final : public BooleanObject {
public:
BooleanPrototype();
virtual ~BooleanPrototype() override;

View file

@ -30,7 +30,7 @@
namespace JS {
class NumberObject final : public Object {
class NumberObject : public Object {
public:
explicit NumberObject(double);
virtual ~NumberObject() override;

View file

@ -24,12 +24,15 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/NumberPrototype.h>
namespace JS {
NumberPrototype::NumberPrototype()
: NumberObject(0)
{
set_prototype(interpreter().object_prototype());
}
NumberPrototype::~NumberPrototype()

View file

@ -26,11 +26,11 @@
#pragma once
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/NumberObject.h>
namespace JS {
class NumberPrototype final : public Object {
class NumberPrototype final : public NumberObject {
public:
NumberPrototype();
virtual ~NumberPrototype() override;

View file

@ -30,7 +30,7 @@
namespace JS {
class StringObject final : public Object {
class StringObject : public Object {
public:
explicit StringObject(PrimitiveString*);
virtual ~StringObject() override;

View file

@ -38,7 +38,9 @@
namespace JS {
StringPrototype::StringPrototype()
: StringObject(js_string(interpreter(), String::empty()))
{
set_prototype(interpreter().object_prototype());
put_native_property("length", length_getter, nullptr);
put_native_function("charAt", char_at, 1);
put_native_function("repeat", repeat, 1);

View file

@ -26,11 +26,11 @@
#pragma once
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/StringObject.h>
namespace JS {
class StringPrototype final : public Object {
class StringPrototype final : public StringObject {
public:
StringPrototype();
virtual ~StringPrototype() override;

View file

@ -0,0 +1,8 @@
try {
assert(typeof Number.prototype === "object");
assert(Number.prototype.valueOf() === 0);
console.log("PASS");
} catch (err) {
console.log("FAIL: " + err);
}

View file

@ -0,0 +1,8 @@
try {
assert(typeof Object.getPrototypeOf("") === "object");
assert(Object.getPrototypeOf("").valueOf() === '');
console.log("PASS");
} catch (err) {
console.log("FAIL: " + err);
}