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:
parent
c06d5ef114
commit
070a8f2689
Notes:
sideshowbarker
2024-07-19 07:44:52 +09:00
Author: https://github.com/awesomekling
Commit: 070a8f2689
11 changed files with 33 additions and 9 deletions
|
@ -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()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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;
|
||||||
|
|
8
Libraries/LibJS/Tests/Number.prototype.js
Normal file
8
Libraries/LibJS/Tests/Number.prototype.js
Normal 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);
|
||||||
|
}
|
8
Libraries/LibJS/Tests/String.prototype.js
Normal file
8
Libraries/LibJS/Tests/String.prototype.js
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
try {
|
||||||
|
assert(typeof Object.getPrototypeOf("") === "object");
|
||||||
|
assert(Object.getPrototypeOf("").valueOf() === '');
|
||||||
|
|
||||||
|
console.log("PASS");
|
||||||
|
} catch (err) {
|
||||||
|
console.log("FAIL: " + err);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue