mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 04:28:13 +00:00
LibJS: Implement Symbol.toStringTag
This commit is contained in:
parent
5ecd504f4e
commit
43d955014d
11 changed files with 55 additions and 0 deletions
|
@ -42,6 +42,8 @@ void ArrayIteratorPrototype::initialize(Interpreter& interpreter, GlobalObject&
|
||||||
{
|
{
|
||||||
Object::initialize(interpreter, global_object);
|
Object::initialize(interpreter, global_object);
|
||||||
define_native_function("next", next, 0, Attribute::Writable | Attribute::Configurable);
|
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()
|
ArrayIteratorPrototype::~ArrayIteratorPrototype()
|
||||||
|
|
|
@ -44,6 +44,8 @@ void BigIntPrototype::initialize(Interpreter& interpreter, GlobalObject& global_
|
||||||
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("toLocaleString", to_locale_string, 0, attr);
|
||||||
define_native_function("valueOf", value_of, 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()
|
BigIntPrototype::~BigIntPrototype()
|
||||||
|
|
|
@ -48,6 +48,8 @@ void JSONObject::initialize(Interpreter& interpreter, GlobalObject& global_objec
|
||||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||||
define_native_function("stringify", stringify, 3, attr);
|
define_native_function("stringify", stringify, 3, attr);
|
||||||
define_native_function("parse", parse, 2, 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()
|
JSONObject::~JSONObject()
|
||||||
|
|
|
@ -74,6 +74,8 @@ void MathObject::initialize(Interpreter& interpreter, GlobalObject& global_objec
|
||||||
define_property("PI", Value(M_PI), 0);
|
define_property("PI", Value(M_PI), 0);
|
||||||
define_property("SQRT1_2", Value(M_SQRT1_2), 0);
|
define_property("SQRT1_2", Value(M_SQRT1_2), 0);
|
||||||
define_property("SQRT2", Value(M_SQRT2), 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()
|
MathObject::~MathObject()
|
||||||
|
|
|
@ -50,6 +50,8 @@ void SymbolPrototype::initialize(Interpreter& interpreter, GlobalObject& global_
|
||||||
define_native_property("description", description_getter, nullptr, Attribute::Configurable);
|
define_native_property("description", description_getter, nullptr, Attribute::Configurable);
|
||||||
define_native_function("toString", to_string, 0, Attribute::Writable | 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_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()
|
SymbolPrototype::~SymbolPrototype()
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
test("basic functionality", () => {
|
||||||
|
expect(BigInt.prototype[Symbol.toStringTag]).toBe("BigInt");
|
||||||
|
});
|
|
@ -0,0 +1,4 @@
|
||||||
|
test("basic functionality", () => {
|
||||||
|
expect(JSON[Symbol.toStringTag]).toBe("JSON");
|
||||||
|
expect(JSON.toString()).toBe("[object JSON]");
|
||||||
|
});
|
|
@ -0,0 +1,4 @@
|
||||||
|
test("basic functionality", () => {
|
||||||
|
expect(Math[Symbol.toStringTag]).toBe("Math");
|
||||||
|
expect(Math.toString()).toBe("[object Math]");
|
||||||
|
});
|
|
@ -0,0 +1,3 @@
|
||||||
|
test("basic functionality", () => {
|
||||||
|
expect(Symbol.prototype[Symbol.toStringTag]).toBe("Symbol");
|
||||||
|
});
|
26
Libraries/LibJS/Tests/custom-@@toStringTag.js
Normal file
26
Libraries/LibJS/Tests/custom-@@toStringTag.js
Normal file
|
@ -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]");
|
||||||
|
});
|
|
@ -2,6 +2,11 @@ test("length", () => {
|
||||||
expect(Array.prototype[Symbol.iterator].length).toBe(0);
|
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", () => {
|
test("same function as Array.prototype.values", () => {
|
||||||
expect(Array.prototype[Symbol.iterator]).toBe(Array.prototype.values);
|
expect(Array.prototype[Symbol.iterator]).toBe(Array.prototype.values);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue