mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:47:36 +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:
parent
dc6db819f9
commit
d6cffb82a2
3 changed files with 31 additions and 30 deletions
|
@ -719,34 +719,7 @@ Value UnaryExpression::execute(Interpreter& interpreter, GlobalObject& global_ob
|
||||||
case UnaryOp::Minus:
|
case UnaryOp::Minus:
|
||||||
return unary_minus(global_object, lhs_result);
|
return unary_minus(global_object, lhs_result);
|
||||||
case UnaryOp::Typeof:
|
case UnaryOp::Typeof:
|
||||||
switch (lhs_result.type()) {
|
return js_string(vm, lhs_result.typeof());
|
||||||
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();
|
|
||||||
}
|
|
||||||
case UnaryOp::Void:
|
case UnaryOp::Void:
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
case UnaryOp::Delete:
|
case UnaryOp::Delete:
|
||||||
|
@ -1430,9 +1403,9 @@ Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& glob
|
||||||
}
|
}
|
||||||
|
|
||||||
reference.put(global_object, rhs_result);
|
reference.put(global_object, rhs_result);
|
||||||
|
|
||||||
if (interpreter.exception())
|
if (interpreter.exception())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
return rhs_result;
|
return rhs_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -264,6 +264,33 @@ bool Value::is_regexp(GlobalObject& global_object) const
|
||||||
return is<RegExpObject>(as_object());
|
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
|
String Value::to_string_without_side_effects() const
|
||||||
{
|
{
|
||||||
switch (m_type) {
|
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());
|
vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, species.to_string_without_side_effects());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -303,6 +303,8 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String typeof() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Type m_type { Type::Empty };
|
Type m_type { Type::Empty };
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue