summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-05-28 19:19:20 +0100
committerAndreas Kling <kling@serenityos.org>2020-05-29 08:00:02 +0200
commit70d2add22f7ac66a2dbce65613af094bbe72a834 (patch)
tree8f731c03143af5d36cac41af14fc2664c9e471f1 /Libraries
parent9755f8d29755362b306d8d2ac90a42a7f855100e (diff)
downloadserenity-70d2add22f7ac66a2dbce65613af094bbe72a834.zip
LibJS: Add Object.prototype.toLocaleString()
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/Runtime/ObjectPrototype.cpp9
-rw-r--r--Libraries/LibJS/Runtime/ObjectPrototype.h1
-rw-r--r--Libraries/LibJS/Tests/Object.prototype.toLocaleString.js32
3 files changed, 42 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/ObjectPrototype.cpp b/Libraries/LibJS/Runtime/ObjectPrototype.cpp
index 2b3624dc56..6a4b4a7efb 100644
--- a/Libraries/LibJS/Runtime/ObjectPrototype.cpp
+++ b/Libraries/LibJS/Runtime/ObjectPrototype.cpp
@@ -45,6 +45,7 @@ void ObjectPrototype::initialize()
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function("hasOwnProperty", has_own_property, 1, 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);
}
@@ -71,6 +72,14 @@ Value ObjectPrototype::to_string(Interpreter& interpreter)
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)
{
auto* this_object = interpreter.this_value().to_object(interpreter);
diff --git a/Libraries/LibJS/Runtime/ObjectPrototype.h b/Libraries/LibJS/Runtime/ObjectPrototype.h
index 8e18b5bd3b..708ddc7245 100644
--- a/Libraries/LibJS/Runtime/ObjectPrototype.h
+++ b/Libraries/LibJS/Runtime/ObjectPrototype.h
@@ -44,6 +44,7 @@ private:
virtual const char* class_name() const override { return "ObjectPrototype"; }
static Value has_own_property(Interpreter&);
+ static Value to_locale_string(Interpreter&);
static Value value_of(Interpreter&);
};
diff --git a/Libraries/LibJS/Tests/Object.prototype.toLocaleString.js b/Libraries/LibJS/Tests/Object.prototype.toLocaleString.js
new file mode 100644
index 0000000000..2868548376
--- /dev/null
+++ b/Libraries/LibJS/Tests/Object.prototype.toLocaleString.js
@@ -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);
+}