1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 10:01:13 +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> #include <LibJS/Runtime/BooleanObject.h>
namespace JS { namespace JS {
BooleanObject::BooleanObject(bool value) BooleanObject::BooleanObject(bool value)
: m_value(value) : m_value(value)
{ {
@ -37,4 +38,5 @@ BooleanObject::BooleanObject(bool value)
BooleanObject::~BooleanObject() BooleanObject::~BooleanObject()
{ {
} }
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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