diff --git a/Userland/Libraries/LibJS/CMakeLists.txt b/Userland/Libraries/LibJS/CMakeLists.txt index 07bce78cb7..f6ddf44313 100644 --- a/Userland/Libraries/LibJS/CMakeLists.txt +++ b/Userland/Libraries/LibJS/CMakeLists.txt @@ -43,6 +43,7 @@ set(SOURCES Runtime/AsyncFunctionPrototype.cpp Runtime/AsyncGeneratorFunctionConstructor.cpp Runtime/AsyncGeneratorFunctionPrototype.cpp + Runtime/AsyncIteratorPrototype.cpp Runtime/AtomicsObject.cpp Runtime/BigInt.cpp Runtime/BigIntConstructor.cpp diff --git a/Userland/Libraries/LibJS/Forward.h b/Userland/Libraries/LibJS/Forward.h index 84863febcf..fd0db9cccd 100644 --- a/Userland/Libraries/LibJS/Forward.h +++ b/Userland/Libraries/LibJS/Forward.h @@ -88,6 +88,7 @@ #define JS_ENUMERATE_ITERATOR_PROTOTYPES \ __JS_ENUMERATE(Iterator, iterator) \ __JS_ENUMERATE(ArrayIterator, array_iterator) \ + __JS_ENUMERATE(AsyncIterator, async_iterator) \ __JS_ENUMERATE(MapIterator, map_iterator) \ __JS_ENUMERATE(RegExpStringIterator, regexp_string_iterator) \ __JS_ENUMERATE(SetIterator, set_iterator) \ diff --git a/Userland/Libraries/LibJS/Runtime/AsyncIteratorPrototype.cpp b/Userland/Libraries/LibJS/Runtime/AsyncIteratorPrototype.cpp new file mode 100644 index 0000000000..cd623fd749 --- /dev/null +++ b/Userland/Libraries/LibJS/Runtime/AsyncIteratorPrototype.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021, David Tuin + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace JS { + +AsyncIteratorPrototype::AsyncIteratorPrototype(GlobalObject& global_object) + : Object(*global_object.object_prototype()) +{ +} + +void AsyncIteratorPrototype::initialize(GlobalObject& global_object) +{ + auto& vm = this->vm(); + Object::initialize(global_object); + u8 attr = Attribute::Writable | Attribute::Enumerable; + define_native_function(*vm.well_known_symbol_async_iterator(), symbol_async_iterator, 0, attr); +} + +// 27.1.3.1 %AsyncIteratorPrototype% [ @@asyncIterator ] ( ), https://tc39.es/ecma262/#sec-asynciteratorprototype-asynciterator +JS_DEFINE_NATIVE_FUNCTION(AsyncIteratorPrototype::symbol_async_iterator) +{ + // 1. Return the this value. + return vm.this_value(global_object); +} + +} diff --git a/Userland/Libraries/LibJS/Runtime/AsyncIteratorPrototype.h b/Userland/Libraries/LibJS/Runtime/AsyncIteratorPrototype.h new file mode 100644 index 0000000000..1f02d29505 --- /dev/null +++ b/Userland/Libraries/LibJS/Runtime/AsyncIteratorPrototype.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021, David Tuin + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace JS { + +class AsyncIteratorPrototype final : public Object { + JS_OBJECT(AsyncIteratorPrototype, Object) + +public: + explicit AsyncIteratorPrototype(GlobalObject&); + virtual void initialize(GlobalObject&) override; + virtual ~AsyncIteratorPrototype() override = default; + +private: + JS_DECLARE_NATIVE_FUNCTION(symbol_async_iterator); +}; + +} diff --git a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp index ee3ada697b..338e16ee9f 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include