diff --git a/Userland/Libraries/LibJS/Runtime/Symbol.cpp b/Userland/Libraries/LibJS/Runtime/Symbol.cpp index 61126cabef..9dd6b3f237 100644 --- a/Userland/Libraries/LibJS/Runtime/Symbol.cpp +++ b/Userland/Libraries/LibJS/Runtime/Symbol.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Matthew Olsson + * Copyright (c) 2022, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -16,14 +17,9 @@ Symbol::Symbol(Optional description, bool is_global) { } -Symbol* js_symbol(Heap& heap, Optional description, bool is_global) +NonnullGCPtr Symbol::create(VM& vm, Optional description, bool is_global) { - return heap.allocate_without_realm(move(description), is_global); -} - -Symbol* js_symbol(VM& vm, Optional description, bool is_global) -{ - return js_symbol(vm.heap(), move(description), is_global); + return *vm.heap().allocate_without_realm(move(description), is_global); } } diff --git a/Userland/Libraries/LibJS/Runtime/Symbol.h b/Userland/Libraries/LibJS/Runtime/Symbol.h index 4dd99bd619..f323489f27 100644 --- a/Userland/Libraries/LibJS/Runtime/Symbol.h +++ b/Userland/Libraries/LibJS/Runtime/Symbol.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Matthew Olsson + * Copyright (c) 2022, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -16,6 +17,8 @@ class Symbol final : public Cell { JS_CELL(Symbol, Cell); public: + [[nodiscard]] static NonnullGCPtr create(VM&, Optional description, bool is_global); + virtual ~Symbol() = default; DeprecatedString description() const { return m_description.value_or(""); } @@ -30,7 +33,4 @@ private: bool m_is_global; }; -Symbol* js_symbol(Heap&, Optional description, bool is_global); -Symbol* js_symbol(VM&, Optional description, bool is_global); - } diff --git a/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp b/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp index 1bffcc0826..e255300764 100644 --- a/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Matthew Olsson + * Copyright (c) 2022, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -40,8 +41,8 @@ ThrowCompletionOr SymbolConstructor::call() { auto& vm = this->vm(); if (vm.argument(0).is_undefined()) - return js_symbol(vm, {}, false); - return js_symbol(vm, TRY(vm.argument(0).to_string(vm)), false); + return Symbol::create(vm, {}, false); + return Symbol::create(vm, TRY(vm.argument(0).to_string(vm)), false); } // 20.4.1.1 Symbol ( [ description ] ), https://tc39.es/ecma262/#sec-symbol-description @@ -67,10 +68,10 @@ JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::for_) VERIFY(!result.has_value()); // 4. Let newSymbol be a new unique Symbol value whose [[Description]] value is stringKey. - auto* new_symbol = js_symbol(vm, string_key, true); + auto new_symbol = Symbol::create(vm, string_key, true); // 5. Append the Record { [[Key]]: stringKey, [[Symbol]]: newSymbol } to the GlobalSymbolRegistry List. - vm.global_symbol_registry().set(string_key, *new_symbol); + vm.global_symbol_registry().set(string_key, new_symbol); // 6. Return newSymbol. return new_symbol; diff --git a/Userland/Libraries/LibJS/Runtime/VM.cpp b/Userland/Libraries/LibJS/Runtime/VM.cpp index be416b5933..626e6f5219 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.cpp +++ b/Userland/Libraries/LibJS/Runtime/VM.cpp @@ -140,7 +140,7 @@ VM::VM(OwnPtr custom_data) }; #define __JS_ENUMERATE(SymbolName, snake_name) \ - m_well_known_symbol_##snake_name = js_symbol(*this, "Symbol." #SymbolName, false); + m_well_known_symbol_##snake_name = Symbol::create(*this, "Symbol." #SymbolName, false); JS_ENUMERATE_WELL_KNOWN_SYMBOLS #undef __JS_ENUMERATE }