mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 05:57:44 +00:00
LibJS: Implicitly break for..in loop if the RHS result is nullish
This implements the missing step 6a of 14.7.5.6 ForIn/OfHeadEvaluation: a. If exprValue is undefined or null, then i. Return Completion { [[Type]]: break, [[Value]]: empty, [[Target]]: empty }. In other words, this should just do nothing instead of throwing during the undefined to object coercion: for (const x in undefined);
This commit is contained in:
parent
9cd010167a
commit
2172e51246
2 changed files with 8 additions and 0 deletions
|
@ -477,6 +477,8 @@ Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_obj
|
||||||
auto rhs_result = m_rhs->execute(interpreter, global_object);
|
auto rhs_result = m_rhs->execute(interpreter, global_object);
|
||||||
if (interpreter.exception())
|
if (interpreter.exception())
|
||||||
return {};
|
return {};
|
||||||
|
if (rhs_result.is_nullish())
|
||||||
|
return {};
|
||||||
auto* object = rhs_result.to_object(global_object);
|
auto* object = rhs_result.to_object(global_object);
|
||||||
while (object) {
|
while (object) {
|
||||||
auto property_names = object->get_enumerable_own_property_names(Object::PropertyKind::Key);
|
auto property_names = object->get_enumerable_own_property_names(Object::PropertyKind::Key);
|
||||||
|
|
|
@ -38,6 +38,12 @@ test("iterate through object", () => {
|
||||||
expect(a).toEqual(["a", "b", "c"]);
|
expect(a).toEqual(["a", "b", "c"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("iterate through undefined", () => {
|
||||||
|
for (const property in undefined) {
|
||||||
|
expect.fail();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
test("use already-declared variable", () => {
|
test("use already-declared variable", () => {
|
||||||
var property;
|
var property;
|
||||||
for (property in "abc");
|
for (property in "abc");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue