diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-06-15 02:40:55 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-06-15 18:31:52 +0100 |
commit | dac971b4aec2b36864b984929ab52be8f2015c85 (patch) | |
tree | 44cb967d809894153ec9dda913f9f2f087dfd543 /Userland/Libraries/LibJS | |
parent | 4aff4249aa570fe33b7520d6977391f3de4944fc (diff) | |
download | serenity-dac971b4aec2b36864b984929ab52be8f2015c85.zip |
LibJS: Store and return undefined Symbol description
Instead of the current incorrect behaviour of just defaulting to an
empty string.
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Symbol.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Symbol.h | 11 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp | 12 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/SymbolObject.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp | 5 |
5 files changed, 17 insertions, 19 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Symbol.cpp b/Userland/Libraries/LibJS/Runtime/Symbol.cpp index 98d3664511..c1f56036bc 100644 --- a/Userland/Libraries/LibJS/Runtime/Symbol.cpp +++ b/Userland/Libraries/LibJS/Runtime/Symbol.cpp @@ -10,7 +10,7 @@ namespace JS { -Symbol::Symbol(String description, bool is_global) +Symbol::Symbol(Optional<String> description, bool is_global) : m_description(move(description)) , m_is_global(is_global) { @@ -20,12 +20,12 @@ Symbol::~Symbol() { } -Symbol* js_symbol(Heap& heap, String description, bool is_global) +Symbol* js_symbol(Heap& heap, Optional<String> description, bool is_global) { return heap.allocate_without_global_object<Symbol>(move(description), is_global); } -Symbol* js_symbol(VM& vm, String description, bool is_global) +Symbol* js_symbol(VM& vm, Optional<String> description, bool is_global) { return js_symbol(vm.heap(), move(description), is_global); } diff --git a/Userland/Libraries/LibJS/Runtime/Symbol.h b/Userland/Libraries/LibJS/Runtime/Symbol.h index 8839447b57..a991f6fbdd 100644 --- a/Userland/Libraries/LibJS/Runtime/Symbol.h +++ b/Userland/Libraries/LibJS/Runtime/Symbol.h @@ -16,21 +16,22 @@ class Symbol final : public Cell { AK_MAKE_NONMOVABLE(Symbol); public: - Symbol(String, bool); + Symbol(Optional<String>, bool); virtual ~Symbol(); - const String& description() const { return m_description; } + String description() const { return m_description.value_or(""); } + const Optional<String>& raw_description() const { return m_description; } bool is_global() const { return m_is_global; } String to_string() const { return String::formatted("Symbol({})", description()); } private: virtual const char* class_name() const override { return "Symbol"; } - String m_description; + Optional<String> m_description; bool m_is_global; }; -Symbol* js_symbol(Heap&, String description, bool is_global); -Symbol* js_symbol(VM&, String description, bool is_global); +Symbol* js_symbol(Heap&, Optional<String> description, bool is_global); +Symbol* js_symbol(VM&, Optional<String> description, bool is_global); } diff --git a/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp b/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp index 4e0a029624..ca5a20d5eb 100644 --- a/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp @@ -42,8 +42,8 @@ SymbolConstructor::~SymbolConstructor() // 20.4.1.1 Symbol ( [ description ] ), https://tc39.es/ecma262/#sec-symbol-description Value SymbolConstructor::call() { - if (!vm().argument_count()) - return js_symbol(heap(), "", false); + if (vm().argument(0).is_undefined()) + return js_symbol(heap(), {}, false); return js_symbol(heap(), vm().argument(0).to_string(global_object()), false); } @@ -57,13 +57,7 @@ Value SymbolConstructor::construct(Function&) // 20.4.2.2 Symbol.for ( key ), https://tc39.es/ecma262/#sec-symbol.for JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::for_) { - String description; - if (!vm.argument_count()) { - description = "undefined"; - } else { - description = vm.argument(0).to_string(global_object); - } - + String description = vm.argument(0).to_string(global_object); return global_object.vm().get_global_symbol(description); } diff --git a/Userland/Libraries/LibJS/Runtime/SymbolObject.h b/Userland/Libraries/LibJS/Runtime/SymbolObject.h index f3db908164..a3d3adf5b4 100644 --- a/Userland/Libraries/LibJS/Runtime/SymbolObject.h +++ b/Userland/Libraries/LibJS/Runtime/SymbolObject.h @@ -23,7 +23,7 @@ public: Symbol& primitive_symbol() { return m_symbol; } const Symbol& primitive_symbol() const { return m_symbol; } - const String& description() const { return m_symbol.description(); } + String description() const { return m_symbol.description(); } bool is_global() const { return m_symbol.is_global(); } virtual Value value_of() const override diff --git a/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp b/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp index dbe839c26e..5780bba5c4 100644 --- a/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp @@ -58,7 +58,10 @@ JS_DEFINE_NATIVE_GETTER(SymbolPrototype::description_getter) auto symbol_value = this_symbol_value(global_object, vm.this_value(global_object)); if (vm.exception()) return {}; - return js_string(vm, symbol_value.as_symbol().description()); + auto& description = symbol_value.as_symbol().raw_description(); + if (!description.has_value()) + return js_undefined(); + return js_string(vm, *description); } // 20.4.3.3 Symbol.prototype.toString ( ), https://tc39.es/ecma262/#sec-symbol.prototype.tostring |