diff options
author | Linus Groh <mail@linusgroh.de> | 2021-04-02 20:33:03 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-02 22:24:30 +0200 |
commit | d6cffb82a25973d59312afccf63e958660b30449 (patch) | |
tree | 36e5106962feecb3c77ff8a2eb049d1068728ccd | |
parent | dc6db819f91f5270a7f270a12fad4fc1f469f432 (diff) | |
download | serenity-d6cffb82a25973d59312afccf63e958660b30449.zip |
LibJS: Move 'typeof' string functionality from AST to Value
We should be able to get the 'typeof' string for any value directly, so
this is now a standalone Value::typeof() method instead of being part of
UnaryExpression::execute().
-rw-r--r-- | Userland/Libraries/LibJS/AST.cpp | 31 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Value.cpp | 28 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Value.h | 2 |
3 files changed, 31 insertions, 30 deletions
diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index 09b6c7df54..be1b71d3b4 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -719,34 +719,7 @@ Value UnaryExpression::execute(Interpreter& interpreter, GlobalObject& global_ob case UnaryOp::Minus: return unary_minus(global_object, lhs_result); case UnaryOp::Typeof: - switch (lhs_result.type()) { - case Value::Type::Empty: - VERIFY_NOT_REACHED(); - return {}; - case Value::Type::Undefined: - return js_string(vm, "undefined"); - case Value::Type::Null: - // yes, this is on purpose. yes, this is how javascript works. - // yes, it's silly. - return js_string(vm, "object"); - case Value::Type::Int32: - case Value::Type::Double: - return js_string(vm, "number"); - case Value::Type::String: - return js_string(vm, "string"); - case Value::Type::Object: - if (lhs_result.is_function()) - return js_string(vm, "function"); - return js_string(vm, "object"); - case Value::Type::Boolean: - return js_string(vm, "boolean"); - case Value::Type::Symbol: - return js_string(vm, "symbol"); - case Value::Type::BigInt: - return js_string(vm, "bigint"); - default: - VERIFY_NOT_REACHED(); - } + return js_string(vm, lhs_result.typeof()); case UnaryOp::Void: return js_undefined(); case UnaryOp::Delete: @@ -1430,9 +1403,9 @@ Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& glob } reference.put(global_object, rhs_result); - if (interpreter.exception()) return {}; + return rhs_result; } diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index be8375115b..9f498f0c59 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -264,6 +264,33 @@ bool Value::is_regexp(GlobalObject& global_object) const return is<RegExpObject>(as_object()); } +String Value::typeof() const +{ + switch (m_type) { + case Value::Type::Undefined: + return "undefined"; + case Value::Type::Null: + return "object"; + case Value::Type::Int32: + case Value::Type::Double: + return "number"; + case Value::Type::String: + return "string"; + case Value::Type::Object: + if (is_function()) + return "function"; + return "object"; + case Value::Type::Boolean: + return "boolean"; + case Value::Type::Symbol: + return "symbol"; + case Value::Type::BigInt: + return "bigint"; + default: + VERIFY_NOT_REACHED(); + } +} + String Value::to_string_without_side_effects() const { switch (m_type) { @@ -1364,5 +1391,4 @@ Object* species_constructor(GlobalObject& global_object, const Object& object, O vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, species.to_string_without_side_effects()); return nullptr; } - } diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index 4b8d42431e..6346909f62 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -303,6 +303,8 @@ public: return *this; } + String typeof() const; + private: Type m_type { Type::Empty }; |