summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-03-15 15:00:18 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-15 15:00:18 +0100
commitd02c37f3e35f9f00939c674c79998919e93894dc (patch)
tree981e79e555bcd31362f6d0ddc2bdd8907c165d66 /Libraries
parent57646e362f91ec8a5a514efc8616fd005a09e613 (diff)
downloadserenity-d02c37f3e35f9f00939c674c79998919e93894dc.zip
LibJS: Add Value::to_i32() helper function
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/Value.cpp14
-rw-r--r--Libraries/LibJS/Value.h1
2 files changed, 13 insertions, 2 deletions
diff --git a/Libraries/LibJS/Value.cpp b/Libraries/LibJS/Value.cpp
index c4c25b2e1e..6ba182f6f5 100644
--- a/Libraries/LibJS/Value.cpp
+++ b/Libraries/LibJS/Value.cpp
@@ -89,6 +89,18 @@ Value Value::to_object(Heap& heap) const
ASSERT_NOT_REACHED();
}
+i32 Value::to_i32() const
+{
+ switch (m_type) {
+ case Type::Boolean:
+ return m_value.as_bool;
+ case Type::Number:
+ return static_cast<i32>(m_value.as_double);
+ default:
+ ASSERT_NOT_REACHED();
+ }
+}
+
Value greater_than(Value lhs, Value rhs)
{
ASSERT(lhs.is_number());
@@ -209,8 +221,6 @@ Value typed_eq(Value lhs, Value rhs)
ASSERT_NOT_REACHED();
}
-
-
const LogStream& operator<<(const LogStream& stream, const Value& value)
{
return stream << value.to_string();
diff --git a/Libraries/LibJS/Value.h b/Libraries/LibJS/Value.h
index 95db26df83..d98522ced3 100644
--- a/Libraries/LibJS/Value.h
+++ b/Libraries/LibJS/Value.h
@@ -133,6 +133,7 @@ public:
String to_string() const;
bool to_boolean() const;
+ i32 to_i32() const;
Value to_object(Heap&) const;