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

Kernel: Change the format of /proc/all to JSON.

Update ProcessManager, top and WSCPUMonitor to handle the new format.

Since the kernel is not allowed to use floating-point math, we now compile
the JSON classes in AK without JsonValue::Type::Double support.
To accomodate large unsigned ints, I added a JsonValue::Type::UnsignedInt.
This commit is contained in:
Andreas Kling 2019-06-29 09:04:45 +02:00
parent 561bfd3ed6
commit 2bd8118843
9 changed files with 128 additions and 104 deletions

View file

@ -146,7 +146,9 @@ JsonValue JsonParser::parse_number()
{
auto number_string = extract_while([](char ch) { return ch == '-' || (ch >= '0' && ch <= '9'); });
bool ok;
auto value = JsonValue(number_string.to_int(ok));
auto value = JsonValue(number_string.to_uint(ok));
if (!ok)
value = JsonValue(number_string.to_int(ok));
ASSERT(ok);
return value;
}

View file

@ -68,15 +68,15 @@ JsonValue::JsonValue(int value)
m_value.as_int = value;
}
JsonValue::JsonValue(unsigned value)
JsonValue::JsonValue(long unsigned value)
: JsonValue((unsigned)value)
{
if (value > INT32_MAX) {
m_type = Type::Double;
m_value.as_double = value;
} else {
m_type = Type::Int;
m_value.as_int = (int)value;
}
}
JsonValue::JsonValue(unsigned value)
: m_type(Type::UnsignedInt)
{
m_value.as_uint = value;
}
JsonValue::JsonValue(const char* cstring)
@ -84,11 +84,13 @@ JsonValue::JsonValue(const char* cstring)
{
}
#ifndef KERNEL
JsonValue::JsonValue(double value)
: m_type(Type::Double)
{
m_value.as_double = value;
}
#endif
JsonValue::JsonValue(bool value)
: m_type(Type::Bool)
@ -153,12 +155,17 @@ void JsonValue::serialize(StringBuilder& builder) const
case Type::Bool:
builder.append(m_value.as_bool ? "true" : "false");
break;
#ifndef KERNEL
case Type::Double:
builder.appendf("%g", m_value.as_double);
break;
#endif
case Type::Int:
builder.appendf("%d", m_value.as_int);
break;
case Type::UnsignedInt:
builder.appendf("%u", m_value.as_uint);
break;
case Type::Undefined:
builder.append("undefined");
break;

View file

@ -14,7 +14,10 @@ public:
Undefined,
Null,
Int,
UnsignedInt,
#ifndef KERNEL
Double,
#endif
Bool,
String,
Array,
@ -34,7 +37,10 @@ public:
JsonValue(int);
JsonValue(unsigned);
JsonValue(long unsigned);
#ifndef KERNEL
JsonValue(double);
#endif
JsonValue(bool);
JsonValue(const char*);
JsonValue(const String&);
@ -75,18 +81,36 @@ public:
bool is_undefined() const { return m_type == Type::Undefined; }
bool is_string() const { return m_type == Type::String; }
bool is_int() const { return m_type == Type::Int; }
bool is_uint() const { return m_type == Type::UnsignedInt; }
#ifndef KERNEL
bool is_double() const { return m_type == Type::Double; }
#endif
bool is_array() const { return m_type == Type::Array; }
bool is_object() const { return m_type == Type::Object; }
bool is_number() const { return m_type == Type::Int || m_type == Type::Double; }
bool is_number() const
{
if (m_type == Type::Int || m_type == Type::UnsignedInt)
return true;
#ifdef KERNEL
return false;
#else
return m_type == Type::Double;
#endif
}
dword to_dword(dword default_value = 0) const
{
if (!is_number())
return default_value;
#ifdef KERNEL
return (dword)m_value.as_int;
#else
if (type() == Type::Int)
return (dword)m_value.as_int;
if (type() == Type::UnsignedInt)
return m_value.as_uint;
return (dword)m_value.as_double;
#endif
}
private:
@ -99,8 +123,11 @@ private:
StringImpl* as_string { nullptr };
JsonArray* as_array;
JsonObject* as_object;
#ifndef KERNEL
double as_double;
#endif
int as_int;
unsigned int as_uint;
bool as_bool;
} m_value;
};

View file

@ -13,7 +13,7 @@ typedef signed short signed_word;
typedef signed int signed_dword;
typedef signed long long int signed_qword;
typedef decltype(sizeof(void*)) size_t;
typedef __SIZE_TYPE__ size_t;
typedef signed_dword ssize_t;
static_assert(sizeof(size_t) == sizeof(dword));