1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 15:27:42 +00:00

LibJS: Use element index as key for array spread in object

This fixes spreading of arrays with holes in object literals where the
inserted keys are not consecutive numbers.

Fixes #3967.
This commit is contained in:
Linus Groh 2020-11-06 22:49:36 +00:00 committed by Andreas Kling
parent 06a3625545
commit 745ffca580
2 changed files with 6 additions and 1 deletions

View file

@ -1552,7 +1552,7 @@ Value ObjectExpression::execute(Interpreter& interpreter, GlobalObject& global_o
if (key.is_array()) { if (key.is_array()) {
auto& array_to_spread = static_cast<Array&>(key.as_object()); auto& array_to_spread = static_cast<Array&>(key.as_object());
for (auto& entry : array_to_spread.indexed_properties()) { for (auto& entry : array_to_spread.indexed_properties()) {
object->indexed_properties().append(entry.value_and_attributes(&array_to_spread).value); object->indexed_properties().put(object, entry.index(), entry.value_and_attributes(&array_to_spread).value);
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};
} }

View file

@ -53,6 +53,11 @@ test("spread array in object literal", () => {
testObjStrSpread(obj); testObjStrSpread(obj);
}); });
test("spread array with holes in object literal", () => {
const obj = { ...[, , "a", , , , "b", "c", , "d", , ,] };
expect(obj).toEqual({ 2: "a", 6: "b", 7: "c", 9: "d" });
});
test("spread string object in object literal", () => { test("spread string object in object literal", () => {
const obj = { ...String("abcd") }; const obj = { ...String("abcd") };
testObjStrSpread(obj); testObjStrSpread(obj);