mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:48:11 +00:00
LibJS: Make Array.prototype.{join,toString}() generic
This commit is contained in:
parent
e9ee06b19e
commit
040c75a3cc
3 changed files with 48 additions and 27 deletions
|
@ -35,6 +35,7 @@
|
|||
#include <LibJS/Runtime/Function.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/MarkedValueList.h>
|
||||
#include <LibJS/Runtime/ObjectPrototype.h>
|
||||
#include <LibJS/Runtime/Value.h>
|
||||
|
||||
namespace JS {
|
||||
|
@ -240,46 +241,48 @@ Value ArrayPrototype::shift(Interpreter& interpreter)
|
|||
return array->elements().take_first().value_or(js_undefined());
|
||||
}
|
||||
|
||||
static Value join_array_with_separator(Interpreter& interpreter, const Array& array, StringView separator)
|
||||
{
|
||||
StringBuilder builder;
|
||||
for (size_t i = 0; i < array.elements().size(); ++i) {
|
||||
if (i != 0)
|
||||
builder.append(separator);
|
||||
auto value = array.elements()[i];
|
||||
if (!value.is_empty() && !value.is_undefined() && !value.is_null()) {
|
||||
auto string = value.to_string(interpreter);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
builder.append(string);
|
||||
}
|
||||
}
|
||||
return js_string(interpreter, builder.to_string());
|
||||
}
|
||||
|
||||
Value ArrayPrototype::to_string(Interpreter& interpreter)
|
||||
{
|
||||
auto* array = array_from(interpreter);
|
||||
if (!array)
|
||||
auto* this_object = interpreter.this_value().to_object(interpreter);
|
||||
if (!this_object)
|
||||
return {};
|
||||
|
||||
return join_array_with_separator(interpreter, *array, ",");
|
||||
auto join_function = this_object->get("join");
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
if (!join_function.is_function())
|
||||
return ObjectPrototype::to_string(interpreter);
|
||||
return interpreter.call(join_function.as_function(), this_object);
|
||||
}
|
||||
|
||||
Value ArrayPrototype::join(Interpreter& interpreter)
|
||||
{
|
||||
auto* array = array_from(interpreter);
|
||||
if (!array)
|
||||
auto* this_object = interpreter.this_value().to_object(interpreter);
|
||||
if (!this_object)
|
||||
return {};
|
||||
|
||||
String separator = ",";
|
||||
if (interpreter.argument_count()) {
|
||||
separator = interpreter.argument(0).to_string(interpreter);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
}
|
||||
|
||||
return join_array_with_separator(interpreter, *array, separator);
|
||||
auto length = get_length(interpreter, *this_object);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
StringBuilder builder;
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
if (i > 0)
|
||||
builder.append(separator);
|
||||
auto value = this_object->get_by_index(i).value_or(js_undefined());
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
if (value.is_undefined() || value.is_null())
|
||||
continue;
|
||||
auto string = value.to_string(interpreter);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
builder.append(string);
|
||||
}
|
||||
return js_string(interpreter, builder.to_string());
|
||||
}
|
||||
|
||||
Value ArrayPrototype::concat(Interpreter& interpreter)
|
||||
|
|
|
@ -37,12 +37,14 @@ public:
|
|||
|
||||
virtual ~ObjectPrototype() override;
|
||||
|
||||
// public to serve as intrinsic function %Object.prototype.toString%
|
||||
static Value to_string(Interpreter&);
|
||||
|
||||
private:
|
||||
virtual const char* class_name() const override { return "ObjectPrototype"; }
|
||||
|
||||
static Value has_own_property(Interpreter&);
|
||||
static Value value_of(Interpreter&);
|
||||
static Value to_string(Interpreter&);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -27,6 +27,22 @@ try {
|
|||
assert(o.length === 0);
|
||||
});
|
||||
|
||||
{
|
||||
assert(Array.prototype.join.call({}) === "");
|
||||
assert(Array.prototype.join.call({ length: "foo" }) === "");
|
||||
assert(Array.prototype.join.call({ length: 3 }) === ",,");
|
||||
assert(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar" }) === "foo,bar");
|
||||
assert(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar", 2: "baz" }) === "foo,bar");
|
||||
assert(Array.prototype.join.call({ length: 3, 1: "bar" }, "~") === "~bar~");
|
||||
assert(Array.prototype.join.call({ length: 3, 0: "foo", 1: "bar", 2: "baz" }, "~") === "foo~bar~baz");
|
||||
}
|
||||
|
||||
{
|
||||
assert(Array.prototype.toString.call({}) === "[object Object]");
|
||||
assert(Array.prototype.toString.call({ join: "foo" }) === "[object Object]");
|
||||
assert(Array.prototype.toString.call({ join: () => "foo" }) === "foo");
|
||||
}
|
||||
|
||||
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
|
||||
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue