From a61723ec599be984523e40030c05e3a3f3af7e67 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 10 Jul 2021 22:53:54 -0400 Subject: LibJS: Begin implementing Atomics This adds the Atomics object to the global object and sets up only its @@toStringTag property. --- Userland/Libraries/LibJS/CMakeLists.txt | 1 + Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp | 27 ++++++++++++++++++++++ Userland/Libraries/LibJS/Runtime/AtomicsObject.h | 22 ++++++++++++++++++ .../Libraries/LibJS/Runtime/CommonPropertyNames.h | 1 + Userland/Libraries/LibJS/Runtime/GlobalObject.cpp | 2 ++ .../builtins/Atomics/Atomics.@@toStringTag.js | 4 ++++ 6 files changed, 57 insertions(+) create mode 100644 Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp create mode 100644 Userland/Libraries/LibJS/Runtime/AtomicsObject.h create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.@@toStringTag.js (limited to 'Userland/Libraries') diff --git a/Userland/Libraries/LibJS/CMakeLists.txt b/Userland/Libraries/LibJS/CMakeLists.txt index 286ea7f7b6..921ce02bac 100644 --- a/Userland/Libraries/LibJS/CMakeLists.txt +++ b/Userland/Libraries/LibJS/CMakeLists.txt @@ -35,6 +35,7 @@ set(SOURCES Runtime/ArrayIterator.cpp Runtime/ArrayIteratorPrototype.cpp Runtime/ArrayPrototype.cpp + Runtime/AtomicsObject.cpp Runtime/BigInt.cpp Runtime/BigIntConstructor.cpp Runtime/BigIntObject.cpp diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp new file mode 100644 index 0000000000..a875043fe4 --- /dev/null +++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace JS { + +AtomicsObject::AtomicsObject(GlobalObject& global_object) + : Object(*global_object.object_prototype()) +{ +} + +void AtomicsObject::initialize(GlobalObject& global_object) +{ + Object::initialize(global_object); + + auto& vm = this->vm(); + + // 25.4.15 Atomics [ @@toStringTag ], https://tc39.es/ecma262/#sec-atomics-@@tostringtag + define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(global_object.heap(), "Atomics"), Attribute::Configurable); +} + +} diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.h b/Userland/Libraries/LibJS/Runtime/AtomicsObject.h new file mode 100644 index 0000000000..db393e1213 --- /dev/null +++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2021, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace JS { + +class AtomicsObject : public Object { + JS_OBJECT(AtomicsObject, Object); + +public: + explicit AtomicsObject(GlobalObject&); + virtual void initialize(GlobalObject&) override; + virtual ~AtomicsObject() override = default; +}; + +} diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 2c047727f8..530cca681e 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -17,6 +17,7 @@ namespace JS { P(__defineSetter__) \ P(__lookupGetter__) \ P(__lookupSetter__) \ + P(Atomics) \ P(BYTES_PER_ELEMENT) \ P(BigInt) \ P(Boolean) \ diff --git a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp index f64ebb9b45..2b729d4deb 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -184,6 +185,7 @@ void GlobalObject::initialize_global_object() define_direct_property(vm.names.globalThis, this, attr); define_direct_property(vm.names.console, heap().allocate(*this, *this), attr); + define_direct_property(vm.names.Atomics, heap().allocate(*this, *this), attr); define_direct_property(vm.names.Math, heap().allocate(*this, *this), attr); define_direct_property(vm.names.JSON, heap().allocate(*this, *this), attr); define_direct_property(vm.names.Reflect, heap().allocate(*this, *this), attr); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.@@toStringTag.js b/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.@@toStringTag.js new file mode 100644 index 0000000000..6cdf79b4de --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.@@toStringTag.js @@ -0,0 +1,4 @@ +test("basic functionality", () => { + expect(Atomics[Symbol.toStringTag]).toBe("Atomics"); + expect(Atomics.toString()).toBe("[object Atomics]"); +}); -- cgit v1.2.3