mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:07:35 +00:00
LibJS: Implement the IteratorClose abstract op and use it where needed
This commit is contained in:
parent
3e4b48f68e
commit
2100b46d02
4 changed files with 46 additions and 8 deletions
|
@ -40,8 +40,8 @@
|
||||||
M(IsNotAn, "{} is not an {}") \
|
M(IsNotAn, "{} is not an {}") \
|
||||||
M(IsNotAEvaluatedFrom, "{} is not a {} (evaluated from '{}')") \
|
M(IsNotAEvaluatedFrom, "{} is not a {} (evaluated from '{}')") \
|
||||||
M(IterableNextBadReturn, "iterator.next() returned a non-object value") \
|
M(IterableNextBadReturn, "iterator.next() returned a non-object value") \
|
||||||
M(IterableNextNotAFunction, "'next' property on returned object from Symbol.iterator method is " \
|
M(IterableNextNotAFunction, "'next' property on returned object from Symbol.iterator method is not a function") \
|
||||||
"not a function") \
|
M(IterableReturnBadReturn, "iterator.return() returned a non-object value") \
|
||||||
M(JsonBigInt, "Cannot serialize BigInt value to JSON") \
|
M(JsonBigInt, "Cannot serialize BigInt value to JSON") \
|
||||||
M(JsonCircular, "Cannot stringify circular object") \
|
M(JsonCircular, "Cannot stringify circular object") \
|
||||||
M(JsonMalformed, "Malformed JSON string") \
|
M(JsonMalformed, "Malformed JSON string") \
|
||||||
|
|
|
@ -43,6 +43,7 @@ Object* iterator_next(Object& iterator, Value value)
|
||||||
{
|
{
|
||||||
auto& vm = iterator.vm();
|
auto& vm = iterator.vm();
|
||||||
auto& global_object = iterator.global_object();
|
auto& global_object = iterator.global_object();
|
||||||
|
|
||||||
auto next_method = iterator.get(vm.names.next);
|
auto next_method = iterator.get(vm.names.next);
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
|
@ -69,9 +70,38 @@ Object* iterator_next(Object& iterator, Value value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7.4.6 IteratorClose ( iteratorRecord, completion ), https://tc39.es/ecma262/#sec-iteratorclose
|
// 7.4.6 IteratorClose ( iteratorRecord, completion ), https://tc39.es/ecma262/#sec-iteratorclose
|
||||||
void iterator_close([[maybe_unused]] Object& iterator)
|
void iterator_close(Object& iterator)
|
||||||
{
|
{
|
||||||
TODO();
|
auto& vm = iterator.vm();
|
||||||
|
auto& global_object = iterator.global_object();
|
||||||
|
|
||||||
|
// Emulates `completion` behaviour
|
||||||
|
auto* completion_exception = vm.exception();
|
||||||
|
vm.clear_exception();
|
||||||
|
auto unwind_until = vm.unwind_until();
|
||||||
|
auto unwind_until_label = vm.unwind_until_label();
|
||||||
|
vm.stop_unwind();
|
||||||
|
auto restore_completion = [&]() {
|
||||||
|
if (completion_exception)
|
||||||
|
vm.set_exception(*completion_exception);
|
||||||
|
if (unwind_until != ScopeType::None)
|
||||||
|
vm.unwind(unwind_until, unwind_until_label);
|
||||||
|
};
|
||||||
|
|
||||||
|
auto return_method = get_method(global_object, &iterator, vm.names.return_);
|
||||||
|
if (!return_method)
|
||||||
|
return restore_completion(); // If return is undefined, return Completion(completion).
|
||||||
|
|
||||||
|
auto result = vm.call(*return_method, &iterator);
|
||||||
|
if (completion_exception)
|
||||||
|
return restore_completion(); // If completion.[[Type]] is throw, return Completion(completion).
|
||||||
|
if (vm.exception())
|
||||||
|
return; // If innerResult.[[Type]] is throw, return Completion(innerResult).
|
||||||
|
if (!result.is_object()) {
|
||||||
|
vm.throw_exception<TypeError>(global_object, ErrorType::IterableReturnBadReturn);
|
||||||
|
return; // If Type(innerResult.[[Value]]) is not Object, throw a TypeError exception.
|
||||||
|
}
|
||||||
|
restore_completion(); // Return Completion(completion).
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7.4.10 IterableToList ( items [ , method ] ), https://tc39.es/ecma262/#sec-iterabletolist
|
// 7.4.10 IterableToList ( items [ , method ] ), https://tc39.es/ecma262/#sec-iterabletolist
|
||||||
|
@ -86,7 +116,7 @@ MarkedValueList iterable_to_list(GlobalObject& global_object, Value iterable, Va
|
||||||
values.append(value);
|
values.append(value);
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
},
|
},
|
||||||
method);
|
method, CloseOnAbrupt::No);
|
||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,7 +130,7 @@ Value create_iterator_result_object(GlobalObject& global_object, Value value, bo
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_iterator_values(GlobalObject& global_object, Value value, AK::Function<IterationDecision(Value)> callback, Value method)
|
void get_iterator_values(GlobalObject& global_object, Value value, AK::Function<IterationDecision(Value)> callback, Value method, CloseOnAbrupt close_on_abrupt)
|
||||||
{
|
{
|
||||||
auto& vm = global_object.vm();
|
auto& vm = global_object.vm();
|
||||||
|
|
||||||
|
@ -125,8 +155,11 @@ void get_iterator_values(GlobalObject& global_object, Value value, AK::Function<
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto result = callback(next_value);
|
auto result = callback(next_value);
|
||||||
if (result == IterationDecision::Break)
|
if (result == IterationDecision::Break) {
|
||||||
|
if (close_on_abrupt == CloseOnAbrupt::Yes)
|
||||||
|
iterator_close(*iterator);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
VERIFY(result == IterationDecision::Continue);
|
VERIFY(result == IterationDecision::Continue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,10 @@ void iterator_close(Object& iterator);
|
||||||
|
|
||||||
MarkedValueList iterable_to_list(GlobalObject&, Value iterable, Value method = {});
|
MarkedValueList iterable_to_list(GlobalObject&, Value iterable, Value method = {});
|
||||||
|
|
||||||
void get_iterator_values(GlobalObject&, Value value, AK::Function<IterationDecision(Value)> callback, Value method = {});
|
enum class CloseOnAbrupt {
|
||||||
|
No,
|
||||||
|
Yes
|
||||||
|
};
|
||||||
|
void get_iterator_values(GlobalObject&, Value value, AK::Function<IterationDecision(Value)> callback, Value method = {}, CloseOnAbrupt close_on_abrupt = CloseOnAbrupt::Yes);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -188,6 +188,7 @@ public:
|
||||||
bool should_unwind() const { return m_unwind_until != ScopeType::None; }
|
bool should_unwind() const { return m_unwind_until != ScopeType::None; }
|
||||||
|
|
||||||
ScopeType unwind_until() const { return m_unwind_until; }
|
ScopeType unwind_until() const { return m_unwind_until; }
|
||||||
|
FlyString unwind_until_label() const { return m_unwind_until_label; }
|
||||||
|
|
||||||
Value get_variable(const FlyString& name, GlobalObject&);
|
Value get_variable(const FlyString& name, GlobalObject&);
|
||||||
void set_variable(const FlyString& name, Value, GlobalObject&, bool first_assignment = false, ScopeObject* specific_scope = nullptr);
|
void set_variable(const FlyString& name, Value, GlobalObject&, bool first_assignment = false, ScopeObject* specific_scope = nullptr);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue