diff options
author | Matthew Olsson <matthewcolsson@gmail.com> | 2021-06-13 13:39:46 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-19 09:38:26 +0200 |
commit | 14fff5df06afb574e2a1a2df8c9f1c1ea5668f66 (patch) | |
tree | 257176b8e6b9b6f42c308385d93d9b7040fcac6a /Userland | |
parent | 1f8e643ef0dc1c61593e96c7dbd681cd26e7ebbf (diff) | |
download | serenity-14fff5df06afb574e2a1a2df8c9f1c1ea5668f66.zip |
LibJS: Implement more IteratorOperations and organize file
Implemented IteratorComplete and IteratorValue, and sorted functions
based on their spec ordering.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp | 32 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/IteratorOperations.h | 7 |
2 files changed, 25 insertions, 14 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp b/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp index 79d76212c0..dca658cee3 100644 --- a/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp @@ -69,6 +69,18 @@ Object* iterator_next(Object& iterator, Value value) return &result.as_object(); } +// 7.4.3 IteratorComplete ( iterResult ), https://tc39.es/ecma262/#sec-iteratorcomplete +bool iterator_complete(GlobalObject& global_object, Object& iterator_result) +{ + return iterator_result.get(global_object.vm().names.done).value_or(Value(false)).to_boolean(); +} + +// 7.4.4 IteratorValue ( iterResult ), https://tc39.es/ecma262/#sec-iteratorvalue +Value iterator_value(GlobalObject& global_object, Object& iterator_result) +{ + return iterator_result.get(global_object.vm().names.value).value_or(js_undefined()); +} + // 7.4.6 IteratorClose ( iteratorRecord, completion ), https://tc39.es/ecma262/#sec-iteratorclose void iterator_close(Object& iterator) { @@ -104,6 +116,16 @@ void iterator_close(Object& iterator) restore_completion(); // Return Completion(completion). } +// 7.4.8 CreateIterResultObject ( value, done ), https://tc39.es/ecma262/#sec-createiterresultobject +Value create_iterator_result_object(GlobalObject& global_object, Value value, bool done) +{ + auto& vm = global_object.vm(); + auto* object = Object::create(global_object, global_object.object_prototype()); + object->define_property(vm.names.value, value); + object->define_property(vm.names.done, Value(done)); + return object; +} + // 7.4.10 IterableToList ( items [ , method ] ), https://tc39.es/ecma262/#sec-iterabletolist MarkedValueList iterable_to_list(GlobalObject& global_object, Value iterable, Value method) { @@ -120,16 +142,6 @@ MarkedValueList iterable_to_list(GlobalObject& global_object, Value iterable, Va return values; } -// 7.4.8 CreateIterResultObject ( value, done ), https://tc39.es/ecma262/#sec-createiterresultobject -Value create_iterator_result_object(GlobalObject& global_object, Value value, bool done) -{ - auto& vm = global_object.vm(); - auto* object = Object::create(global_object, global_object.object_prototype()); - object->define_property(vm.names.value, value); - object->define_property(vm.names.done, Value(done)); - return object; -} - 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(); diff --git a/Userland/Libraries/LibJS/Runtime/IteratorOperations.h b/Userland/Libraries/LibJS/Runtime/IteratorOperations.h index 8671efd695..8afcbda067 100644 --- a/Userland/Libraries/LibJS/Runtime/IteratorOperations.h +++ b/Userland/Libraries/LibJS/Runtime/IteratorOperations.h @@ -19,12 +19,11 @@ enum class IteratorHint { }; Object* get_iterator(GlobalObject&, Value value, IteratorHint hint = IteratorHint::Sync, Value method = {}); -bool is_iterator_complete(Object& iterator_result); -Value create_iterator_result_object(GlobalObject&, Value value, bool done); - Object* iterator_next(Object& iterator, Value value = {}); +bool iterator_complete(GlobalObject&, Object& iterator_result); +Value iterator_value(GlobalObject&, Object& iterator_result); void iterator_close(Object& iterator); - +Value create_iterator_result_object(GlobalObject&, Value value, bool done); MarkedValueList iterable_to_list(GlobalObject&, Value iterable, Value method = {}); enum class CloseOnAbrupt { |