mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 19:57:45 +00:00
LibJS: Add Object.prototype.toLocaleString()
This commit is contained in:
parent
9755f8d297
commit
70d2add22f
3 changed files with 42 additions and 0 deletions
|
@ -45,6 +45,7 @@ void ObjectPrototype::initialize()
|
||||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||||
define_native_function("hasOwnProperty", has_own_property, 1, attr);
|
define_native_function("hasOwnProperty", has_own_property, 1, attr);
|
||||||
define_native_function("toString", to_string, 0, attr);
|
define_native_function("toString", to_string, 0, attr);
|
||||||
|
define_native_function("toLocaleString", to_locale_string, 0, attr);
|
||||||
define_native_function("valueOf", value_of, 0, attr);
|
define_native_function("valueOf", value_of, 0, attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,6 +72,14 @@ Value ObjectPrototype::to_string(Interpreter& interpreter)
|
||||||
return js_string(interpreter, String::format("[object %s]", this_object->class_name()));
|
return js_string(interpreter, String::format("[object %s]", this_object->class_name()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value ObjectPrototype::to_locale_string(Interpreter& interpreter)
|
||||||
|
{
|
||||||
|
auto* this_object = interpreter.this_value().to_object(interpreter);
|
||||||
|
if (!this_object)
|
||||||
|
return {};
|
||||||
|
return this_object->invoke("toString");
|
||||||
|
}
|
||||||
|
|
||||||
Value ObjectPrototype::value_of(Interpreter& interpreter)
|
Value ObjectPrototype::value_of(Interpreter& interpreter)
|
||||||
{
|
{
|
||||||
auto* this_object = interpreter.this_value().to_object(interpreter);
|
auto* this_object = interpreter.this_value().to_object(interpreter);
|
||||||
|
|
|
@ -44,6 +44,7 @@ private:
|
||||||
virtual const char* class_name() const override { return "ObjectPrototype"; }
|
virtual const char* class_name() const override { return "ObjectPrototype"; }
|
||||||
|
|
||||||
static Value has_own_property(Interpreter&);
|
static Value has_own_property(Interpreter&);
|
||||||
|
static Value to_locale_string(Interpreter&);
|
||||||
static Value value_of(Interpreter&);
|
static Value value_of(Interpreter&);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
32
Libraries/LibJS/Tests/Object.prototype.toLocaleString.js
Normal file
32
Libraries/LibJS/Tests/Object.prototype.toLocaleString.js
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
load("test-common.js");
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert(Object.prototype.toLocaleString.length === 0);
|
||||||
|
|
||||||
|
var o;
|
||||||
|
|
||||||
|
o = {};
|
||||||
|
assert(o.toString() === o.toLocaleString());
|
||||||
|
|
||||||
|
o = { toString: () => 42 };
|
||||||
|
assert(o.toString() === 42);
|
||||||
|
|
||||||
|
o = { toString: () => { throw Error(); } };
|
||||||
|
assertThrowsError(() => {
|
||||||
|
o.toLocaleString();
|
||||||
|
}, {
|
||||||
|
error: Error
|
||||||
|
});
|
||||||
|
|
||||||
|
o = { toString: "foo" };
|
||||||
|
assertThrowsError(() => {
|
||||||
|
o.toLocaleString();
|
||||||
|
}, {
|
||||||
|
error: TypeError,
|
||||||
|
message: "foo is not a function"
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("PASS");
|
||||||
|
} catch (e) {
|
||||||
|
console.log("FAIL: " + e);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue