summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Olsson <matthewcolsson@gmail.com>2020-07-11 10:27:00 -0700
committerAndreas Kling <kling@serenityos.org>2020-07-11 23:13:29 +0200
commit43d955014d3f0f4f3931e28c6007ad2c3a7dd98f (patch)
tree1fd19b1a29be50a9667d4ac28fe52266ea6aa4a0
parent5ecd504f4e158b1c95b10c88b019b5a2ad232c10 (diff)
downloadserenity-43d955014d3f0f4f3931e28c6007ad2c3a7dd98f.zip
LibJS: Implement Symbol.toStringTag
-rw-r--r--Libraries/LibJS/Runtime/ArrayIteratorPrototype.cpp2
-rw-r--r--Libraries/LibJS/Runtime/BigIntPrototype.cpp2
-rw-r--r--Libraries/LibJS/Runtime/JSONObject.cpp2
-rw-r--r--Libraries/LibJS/Runtime/MathObject.cpp2
-rw-r--r--Libraries/LibJS/Runtime/SymbolPrototype.cpp2
-rw-r--r--Libraries/LibJS/Tests/builtins/BigInt/BigInt.prototype.@@toStringTag.js3
-rw-r--r--Libraries/LibJS/Tests/builtins/JSON/JSON.@@toStringTag.js4
-rw-r--r--Libraries/LibJS/Tests/builtins/Math/Math.@@toStringTag.js4
-rw-r--r--Libraries/LibJS/Tests/builtins/Symbol/Symbol.prototype.@@toStringTag.js3
-rw-r--r--Libraries/LibJS/Tests/custom-@@toStringTag.js26
-rw-r--r--Libraries/LibJS/Tests/iterators/array-iterator.js5
11 files changed, 55 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/ArrayIteratorPrototype.cpp b/Libraries/LibJS/Runtime/ArrayIteratorPrototype.cpp
index e5e381d34d..f6f2c263a7 100644
--- a/Libraries/LibJS/Runtime/ArrayIteratorPrototype.cpp
+++ b/Libraries/LibJS/Runtime/ArrayIteratorPrototype.cpp
@@ -42,6 +42,8 @@ void ArrayIteratorPrototype::initialize(Interpreter& interpreter, GlobalObject&
{
Object::initialize(interpreter, global_object);
define_native_function("next", next, 0, Attribute::Writable | Attribute::Configurable);
+
+ define_property(interpreter.well_known_symbol_to_string_tag(), js_string(interpreter, "Array Iterator"), Attribute::Configurable);
}
ArrayIteratorPrototype::~ArrayIteratorPrototype()
diff --git a/Libraries/LibJS/Runtime/BigIntPrototype.cpp b/Libraries/LibJS/Runtime/BigIntPrototype.cpp
index 0a0cac411b..6543e0e2ce 100644
--- a/Libraries/LibJS/Runtime/BigIntPrototype.cpp
+++ b/Libraries/LibJS/Runtime/BigIntPrototype.cpp
@@ -44,6 +44,8 @@ void BigIntPrototype::initialize(Interpreter& interpreter, GlobalObject& global_
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_property(interpreter.well_known_symbol_to_string_tag(), js_string(interpreter, "BigInt"), Attribute::Configurable);
}
BigIntPrototype::~BigIntPrototype()
diff --git a/Libraries/LibJS/Runtime/JSONObject.cpp b/Libraries/LibJS/Runtime/JSONObject.cpp
index b333e6a984..60d1fe7618 100644
--- a/Libraries/LibJS/Runtime/JSONObject.cpp
+++ b/Libraries/LibJS/Runtime/JSONObject.cpp
@@ -48,6 +48,8 @@ void JSONObject::initialize(Interpreter& interpreter, GlobalObject& global_objec
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function("stringify", stringify, 3, attr);
define_native_function("parse", parse, 2, attr);
+
+ define_property(interpreter.well_known_symbol_to_string_tag(), js_string(interpreter, "JSON"), Attribute::Configurable);
}
JSONObject::~JSONObject()
diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp
index ac104dedd0..75db4eaa89 100644
--- a/Libraries/LibJS/Runtime/MathObject.cpp
+++ b/Libraries/LibJS/Runtime/MathObject.cpp
@@ -74,6 +74,8 @@ void MathObject::initialize(Interpreter& interpreter, GlobalObject& global_objec
define_property("PI", Value(M_PI), 0);
define_property("SQRT1_2", Value(M_SQRT1_2), 0);
define_property("SQRT2", Value(M_SQRT2), 0);
+
+ define_property(interpreter.well_known_symbol_to_string_tag(), js_string(interpreter, "Math"), Attribute::Configurable);
}
MathObject::~MathObject()
diff --git a/Libraries/LibJS/Runtime/SymbolPrototype.cpp b/Libraries/LibJS/Runtime/SymbolPrototype.cpp
index 5531c221f8..12d08adfe7 100644
--- a/Libraries/LibJS/Runtime/SymbolPrototype.cpp
+++ b/Libraries/LibJS/Runtime/SymbolPrototype.cpp
@@ -50,6 +50,8 @@ void SymbolPrototype::initialize(Interpreter& interpreter, GlobalObject& global_
define_native_property("description", description_getter, nullptr, Attribute::Configurable);
define_native_function("toString", to_string, 0, Attribute::Writable | Attribute::Configurable);
define_native_function("valueOf", value_of, 0, Attribute::Writable | Attribute::Configurable);
+
+ define_property(interpreter.well_known_symbol_to_string_tag(), js_string(interpreter, "Symbol"), Attribute::Configurable);
}
SymbolPrototype::~SymbolPrototype()
diff --git a/Libraries/LibJS/Tests/builtins/BigInt/BigInt.prototype.@@toStringTag.js b/Libraries/LibJS/Tests/builtins/BigInt/BigInt.prototype.@@toStringTag.js
new file mode 100644
index 0000000000..44812778a4
--- /dev/null
+++ b/Libraries/LibJS/Tests/builtins/BigInt/BigInt.prototype.@@toStringTag.js
@@ -0,0 +1,3 @@
+test("basic functionality", () => {
+ expect(BigInt.prototype[Symbol.toStringTag]).toBe("BigInt");
+});
diff --git a/Libraries/LibJS/Tests/builtins/JSON/JSON.@@toStringTag.js b/Libraries/LibJS/Tests/builtins/JSON/JSON.@@toStringTag.js
new file mode 100644
index 0000000000..5a1e51f7c6
--- /dev/null
+++ b/Libraries/LibJS/Tests/builtins/JSON/JSON.@@toStringTag.js
@@ -0,0 +1,4 @@
+test("basic functionality", () => {
+ expect(JSON[Symbol.toStringTag]).toBe("JSON");
+ expect(JSON.toString()).toBe("[object JSON]");
+});
diff --git a/Libraries/LibJS/Tests/builtins/Math/Math.@@toStringTag.js b/Libraries/LibJS/Tests/builtins/Math/Math.@@toStringTag.js
new file mode 100644
index 0000000000..69815a1499
--- /dev/null
+++ b/Libraries/LibJS/Tests/builtins/Math/Math.@@toStringTag.js
@@ -0,0 +1,4 @@
+test("basic functionality", () => {
+ expect(Math[Symbol.toStringTag]).toBe("Math");
+ expect(Math.toString()).toBe("[object Math]");
+});
diff --git a/Libraries/LibJS/Tests/builtins/Symbol/Symbol.prototype.@@toStringTag.js b/Libraries/LibJS/Tests/builtins/Symbol/Symbol.prototype.@@toStringTag.js
new file mode 100644
index 0000000000..c73e637fc4
--- /dev/null
+++ b/Libraries/LibJS/Tests/builtins/Symbol/Symbol.prototype.@@toStringTag.js
@@ -0,0 +1,3 @@
+test("basic functionality", () => {
+ expect(Symbol.prototype[Symbol.toStringTag]).toBe("Symbol");
+});
diff --git a/Libraries/LibJS/Tests/custom-@@toStringTag.js b/Libraries/LibJS/Tests/custom-@@toStringTag.js
new file mode 100644
index 0000000000..e297961e55
--- /dev/null
+++ b/Libraries/LibJS/Tests/custom-@@toStringTag.js
@@ -0,0 +1,26 @@
+test("inside objects", () => {
+ const o = {
+ [Symbol.toStringTag]: "hello friends",
+ };
+
+ expect(o.toString()).toBe("[object hello friends]");
+});
+
+test("inside classes", () => {
+ class A {
+ constructor() {
+ this[Symbol.toStringTag] = "hello friends";
+ }
+ }
+
+ const a = new A();
+ expect(a.toString()).toBe("[object hello friends]");
+});
+
+test("non-string values are ignored", () => {
+ const o = {
+ [Symbol.toStringTag]: [1, 2, 3],
+ };
+
+ expect(o.toString()).toBe("[object Object]");
+});
diff --git a/Libraries/LibJS/Tests/iterators/array-iterator.js b/Libraries/LibJS/Tests/iterators/array-iterator.js
index 117030ebdf..8f02845ebe 100644
--- a/Libraries/LibJS/Tests/iterators/array-iterator.js
+++ b/Libraries/LibJS/Tests/iterators/array-iterator.js
@@ -2,6 +2,11 @@ test("length", () => {
expect(Array.prototype[Symbol.iterator].length).toBe(0);
});
+test("@@toStringTag", () => {
+ expect([].values()[Symbol.toStringTag]).toBe("Array Iterator");
+ expect([].values().toString()).toBe("[object Array Iterator]");
+});
+
test("same function as Array.prototype.values", () => {
expect(Array.prototype[Symbol.iterator]).toBe(Array.prototype.values);
});