1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:47:34 +00:00

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().
This commit is contained in:
Linus Groh 2021-04-02 20:33:03 +02:00 committed by Andreas Kling
parent dc6db819f9
commit d6cffb82a2
3 changed files with 31 additions and 30 deletions

View file

@ -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;
}

View file

@ -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;
}
}

View file

@ -303,6 +303,8 @@ public:
return *this;
}
String typeof() const;
private:
Type m_type { Type::Empty };