mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 10:07:40 +00:00
AK: Ban JsonValue from the kernel and remove ifdef guards
JsonValue can store JsonObject which uses DS for keys, so it is not safe to use it in the kernel even with the double/String guards.
This commit is contained in:
parent
d3b3e49e19
commit
c0ffff7e88
2 changed files with 9 additions and 47 deletions
|
@ -6,14 +6,15 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef KERNEL
|
||||||
|
# error "JsonValue does not propagate allocation failures, so it is not safe to use in the kernel."
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <AK/DeprecatedString.h>
|
||||||
#include <AK/Forward.h>
|
#include <AK/Forward.h>
|
||||||
#include <AK/Optional.h>
|
#include <AK/Optional.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
|
|
||||||
#ifndef KERNEL
|
|
||||||
# include <AK/DeprecatedString.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
class JsonValue {
|
class JsonValue {
|
||||||
|
@ -24,9 +25,7 @@ public:
|
||||||
UnsignedInt32,
|
UnsignedInt32,
|
||||||
Int64,
|
Int64,
|
||||||
UnsignedInt64,
|
UnsignedInt64,
|
||||||
#if !defined(KERNEL)
|
|
||||||
Double,
|
Double,
|
||||||
#endif
|
|
||||||
Bool,
|
Bool,
|
||||||
String,
|
String,
|
||||||
Array,
|
Array,
|
||||||
|
@ -51,13 +50,9 @@ public:
|
||||||
JsonValue(long long);
|
JsonValue(long long);
|
||||||
JsonValue(long long unsigned);
|
JsonValue(long long unsigned);
|
||||||
|
|
||||||
#if !defined(KERNEL)
|
|
||||||
JsonValue(double);
|
JsonValue(double);
|
||||||
#endif
|
|
||||||
JsonValue(char const*);
|
JsonValue(char const*);
|
||||||
#ifndef KERNEL
|
|
||||||
JsonValue(DeprecatedString const&);
|
JsonValue(DeprecatedString const&);
|
||||||
#endif
|
|
||||||
JsonValue(StringView);
|
JsonValue(StringView);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -83,7 +78,6 @@ public:
|
||||||
template<typename Builder>
|
template<typename Builder>
|
||||||
void serialize(Builder&) const;
|
void serialize(Builder&) const;
|
||||||
|
|
||||||
#ifndef KERNEL
|
|
||||||
DeprecatedString as_string_or(DeprecatedString const& alternative) const
|
DeprecatedString as_string_or(DeprecatedString const& alternative) const
|
||||||
{
|
{
|
||||||
if (is_string())
|
if (is_string())
|
||||||
|
@ -97,25 +91,16 @@ public:
|
||||||
return as_string();
|
return as_string();
|
||||||
return serialized<StringBuilder>();
|
return serialized<StringBuilder>();
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
int to_int(int default_value = 0) const
|
int to_int(int default_value = 0) const { return to_i32(default_value); }
|
||||||
{
|
|
||||||
return to_i32(default_value);
|
|
||||||
}
|
|
||||||
i32 to_i32(i32 default_value = 0) const { return to_number<i32>(default_value); }
|
i32 to_i32(i32 default_value = 0) const { return to_number<i32>(default_value); }
|
||||||
i64 to_i64(i64 default_value = 0) const { return to_number<i64>(default_value); }
|
i64 to_i64(i64 default_value = 0) const { return to_number<i64>(default_value); }
|
||||||
|
|
||||||
unsigned to_uint(unsigned default_value = 0) const { return to_u32(default_value); }
|
unsigned to_uint(unsigned default_value = 0) const { return to_u32(default_value); }
|
||||||
u32 to_u32(u32 default_value = 0) const { return to_number<u32>(default_value); }
|
u32 to_u32(u32 default_value = 0) const { return to_number<u32>(default_value); }
|
||||||
u64 to_u64(u64 default_value = 0) const { return to_number<u64>(default_value); }
|
u64 to_u64(u64 default_value = 0) const { return to_number<u64>(default_value); }
|
||||||
#if !defined(KERNEL)
|
float to_float(float default_value = 0) const { return to_number<float>(default_value); }
|
||||||
float to_float(float default_value = 0) const
|
|
||||||
{
|
|
||||||
return to_number<float>(default_value);
|
|
||||||
}
|
|
||||||
double to_double(double default_value = 0) const { return to_number<double>(default_value); }
|
double to_double(double default_value = 0) const { return to_number<double>(default_value); }
|
||||||
#endif
|
|
||||||
|
|
||||||
FlatPtr to_addr(FlatPtr default_value = 0) const
|
FlatPtr to_addr(FlatPtr default_value = 0) const
|
||||||
{
|
{
|
||||||
|
@ -163,13 +148,11 @@ public:
|
||||||
return m_value.as_bool;
|
return m_value.as_bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef KERNEL
|
|
||||||
DeprecatedString as_string() const
|
DeprecatedString as_string() const
|
||||||
{
|
{
|
||||||
VERIFY(is_string());
|
VERIFY(is_string());
|
||||||
return *m_value.as_string;
|
return *m_value.as_string;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
JsonObject& as_object()
|
JsonObject& as_object()
|
||||||
{
|
{
|
||||||
|
@ -195,13 +178,11 @@ public:
|
||||||
return *m_value.as_array;
|
return *m_value.as_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !defined(KERNEL)
|
|
||||||
double as_double() const
|
double as_double() const
|
||||||
{
|
{
|
||||||
VERIFY(is_double());
|
VERIFY(is_double());
|
||||||
return m_value.as_double;
|
return m_value.as_double;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
Type type() const
|
Type type() const
|
||||||
{
|
{
|
||||||
|
@ -215,16 +196,8 @@ public:
|
||||||
bool is_u32() const { return m_type == Type::UnsignedInt32; }
|
bool is_u32() const { return m_type == Type::UnsignedInt32; }
|
||||||
bool is_i64() const { return m_type == Type::Int64; }
|
bool is_i64() const { return m_type == Type::Int64; }
|
||||||
bool is_u64() const { return m_type == Type::UnsignedInt64; }
|
bool is_u64() const { return m_type == Type::UnsignedInt64; }
|
||||||
#if !defined(KERNEL)
|
bool is_double() const { return m_type == Type::Double; }
|
||||||
bool is_double() const
|
bool is_array() const { return m_type == Type::Array; }
|
||||||
{
|
|
||||||
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_object() const { return m_type == Type::Object; }
|
||||||
bool is_number() const
|
bool is_number() const
|
||||||
{
|
{
|
||||||
|
@ -233,9 +206,7 @@ public:
|
||||||
case Type::UnsignedInt32:
|
case Type::UnsignedInt32:
|
||||||
case Type::Int64:
|
case Type::Int64:
|
||||||
case Type::UnsignedInt64:
|
case Type::UnsignedInt64:
|
||||||
#if !defined(KERNEL)
|
|
||||||
case Type::Double:
|
case Type::Double:
|
||||||
#endif
|
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
@ -245,10 +216,8 @@ public:
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T to_number(T default_value = 0) const
|
T to_number(T default_value = 0) const
|
||||||
{
|
{
|
||||||
#if !defined(KERNEL)
|
|
||||||
if (is_double())
|
if (is_double())
|
||||||
return (T)as_double();
|
return (T)as_double();
|
||||||
#endif
|
|
||||||
if (type() == Type::Int32)
|
if (type() == Type::Int32)
|
||||||
return (T)as_i32();
|
return (T)as_i32();
|
||||||
if (type() == Type::UnsignedInt32)
|
if (type() == Type::UnsignedInt32)
|
||||||
|
@ -305,14 +274,10 @@ private:
|
||||||
Type m_type { Type::Null };
|
Type m_type { Type::Null };
|
||||||
|
|
||||||
union {
|
union {
|
||||||
#ifndef KERNEL
|
|
||||||
StringImpl* as_string { nullptr };
|
StringImpl* as_string { nullptr };
|
||||||
#endif
|
|
||||||
JsonArray* as_array;
|
JsonArray* as_array;
|
||||||
JsonObject* as_object;
|
JsonObject* as_object;
|
||||||
#if !defined(KERNEL)
|
|
||||||
double as_double;
|
double as_double;
|
||||||
#endif
|
|
||||||
i32 as_i32;
|
i32 as_i32;
|
||||||
u32 as_u32;
|
u32 as_u32;
|
||||||
i64 as_i64;
|
i64 as_i64;
|
||||||
|
@ -321,7 +286,6 @@ private:
|
||||||
} m_value;
|
} m_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifndef KERNEL
|
|
||||||
template<>
|
template<>
|
||||||
struct Formatter<JsonValue> : Formatter<StringView> {
|
struct Formatter<JsonValue> : Formatter<StringView> {
|
||||||
ErrorOr<void> format(FormatBuilder& builder, JsonValue const& value)
|
ErrorOr<void> format(FormatBuilder& builder, JsonValue const& value)
|
||||||
|
@ -329,7 +293,6 @@ struct Formatter<JsonValue> : Formatter<StringView> {
|
||||||
return Formatter<StringView>::format(builder, value.to_deprecated_string());
|
return Formatter<StringView>::format(builder, value.to_deprecated_string());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
|
|
||||||
#include <AK/JsonArraySerializer.h>
|
#include <AK/JsonArraySerializer.h>
|
||||||
#include <AK/JsonObjectSerializer.h>
|
#include <AK/JsonObjectSerializer.h>
|
||||||
#include <AK/JsonValue.h>
|
|
||||||
#include <Kernel/Devices/TTY/TTY.h>
|
#include <Kernel/Devices/TTY/TTY.h>
|
||||||
#include <Kernel/FileSystem/Custody.h>
|
#include <Kernel/FileSystem/Custody.h>
|
||||||
#include <Kernel/FileSystem/ProcFS/Inode.h>
|
#include <Kernel/FileSystem/ProcFS/Inode.h>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue