diff --git a/core/libs/CMakeLists.txt b/core/libs/CMakeLists.txt index b0572b5bf..d7553bc0f 100644 --- a/core/libs/CMakeLists.txt +++ b/core/libs/CMakeLists.txt @@ -310,6 +310,14 @@ if(BUILD_TESTING) set_tests_properties(guest_rtc PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_executable(guest_json_tests tests/GuestJson.cpp) + target_include_directories(guest_json_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_link_libraries(guest_json_tests PRIVATE libSceJson2) + add_test(NAME guest_json COMMAND guest_json_tests) + if(WIN32) + set_tests_properties(guest_json PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_executable(mspace_tests tests/Mspace.cpp) target_include_directories(mspace_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(mspace_tests PRIVATE libc) diff --git a/core/libs/prx/libSceJson2/Export.cpp b/core/libs/prx/libSceJson2/Export.cpp index 848de74a4..cd5ac7f29 100644 --- a/core/libs/prx/libSceJson2/Export.cpp +++ b/core/libs/prx/libSceJson2/Export.cpp @@ -1,19 +1,462 @@ +#include +#include #include #include +#include +#include +#include +#include +#include +#include #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" +namespace { + +enum ValueType : std::int32_t { + TypeNull = 0, + TypeBoolean = 1, + TypeInteger = 2, + TypeUInteger = 3, + TypeReal = 4, + TypeString = 5, + TypeArray = 6, + TypeObject = 7, +}; + +constexpr int JsonErrorParse = static_cast(0x80920101); + +struct Node; +struct Value { Node* node; }; +struct String { std::string* text; }; +struct Array { std::list* items; }; +struct Pair; +struct Object { std::list* items; }; +struct ArrayIterator { std::list::iterator it; }; +struct ObjectIterator { std::list::iterator it; }; +struct Pair { + String key; + std::uint64_t reserved; + Value value; +}; +static_assert(sizeof(Value) == 8 && sizeof(String) == 8 && sizeof(Array) == 8 && sizeof(Object) == 8); +static_assert(sizeof(ArrayIterator) == 8 && sizeof(ObjectIterator) == 8); +static_assert(offsetof(Pair, value) == 0x10); + +struct Node { + ValueType type = TypeNull; + bool boolean = false; + std::int64_t integer = 0; + std::uint64_t uinteger = 0; + double real = 0; + String string{}; + Array array{}; + Object object{}; +}; + +using NullAccessCallback = const Value& (APS5_VABI*)(ValueType, const Value*, void*); + +struct Globals { + std::mutex mutex; + NullAccessCallback callback = nullptr; + void* context = nullptr; +}; + +Globals& State() { + static Globals globals; + return globals; +} + +void Construct(String& s, const std::string& text) { s.text = new std::string(text); } +void Construct(Array& a) { a.items = new std::list(); } +void Construct(Object& o) { o.items = new std::list(); } +void Destroy(Value& v); +void CopyInto(Value& destination, const Value& source); + +void Destroy(String& s) { delete s.text; s.text = nullptr; } +void Destroy(Array& a) { + if (!a.items) return; + for (auto& item : *a.items) Destroy(item); + delete a.items; + a.items = nullptr; +} +void Destroy(Object& o) { + if (!o.items) return; + for (auto& pair : *o.items) { + Destroy(pair.key); + Destroy(pair.value); + } + delete o.items; + o.items = nullptr; +} + +void Clear(Node& n) { + if (n.type == TypeString) Destroy(n.string); + if (n.type == TypeArray) Destroy(n.array); + if (n.type == TypeObject) Destroy(n.object); + n = Node{}; +} + +void Destroy(Value& v) { + if (!v.node) return; + Clear(*v.node); + delete v.node; + v.node = nullptr; +} + +Node& NodeOf(const Value& v) { + if (!v.node) throw std::runtime_error("sce::Json::Value used before construction"); + return *v.node; +} + +void Construct(Value& v) { v.node = new Node(); } + +void CopyArray(Array& destination, const Array& source) { + Construct(destination); + for (const auto& item : *source.items) { + Value copy{}; + CopyInto(copy, item); + destination.items->push_back(copy); + } +} + +void CopyObject(Object& destination, const Object& source) { + Construct(destination); + for (const auto& pair : *source.items) { + Pair copy{}; + Construct(copy.key, *pair.key.text); + CopyInto(copy.value, pair.value); + destination.items->push_back(copy); + } +} + +void AssignNode(Node& destination, const Node& source) { + Node copy{}; + copy.type = source.type; + copy.boolean = source.boolean; + copy.integer = source.integer; + copy.uinteger = source.uinteger; + copy.real = source.real; + if (source.type == TypeString) Construct(copy.string, *source.string.text); + if (source.type == TypeArray) CopyArray(copy.array, source.array); + if (source.type == TypeObject) CopyObject(copy.object, source.object); + Clear(destination); + destination = copy; +} + +void CopyInto(Value& destination, const Value& source) { + Construct(destination); + AssignNode(*destination.node, NodeOf(source)); +} + +void SetType(Node& n, ValueType type) { + Clear(n); + n.type = type; + if (type == TypeString) Construct(n.string, std::string()); + if (type == TypeArray) Construct(n.array); + if (type == TypeObject) Construct(n.object); + if (type < TypeNull || type > TypeObject) throw std::invalid_argument("sce::Json::Value::set: invalid value type"); +} + +const Value& Defaults(ValueType type) { + static Value values[8]{}; + static std::once_flag once; + std::call_once(once, [] { + for (int i = 0; i < 8; ++i) { + Construct(values[i]); + SetType(*values[i].node, static_cast(i)); + } + }); + return values[type]; +} + +const Value& NullAccess(ValueType requested, const Value* parent) { + auto& state = State(); + NullAccessCallback callback; + void* context; + { + std::lock_guard lock(state.mutex); + callback = state.callback; + context = state.context; + } + if (callback) { + const Value& result = callback(requested, parent, context); + if (result.node && (requested == TypeNull || result.node->type == requested)) return result; + } + return Defaults(requested); +} + +const Node& Typed(const Value* self, ValueType requested) { + const Node& n = NodeOf(*self); + if (n.type == requested) return n; + return NodeOf(NullAccess(requested, self)); +} + +void AppendEscaped(std::string& out, const std::string& text) { + out += '"'; + for (const unsigned char c : text) { + switch (c) { + case '"': out += "\\\""; break; + case '\\': out += "\\\\"; break; + case '\b': out += "\\b"; break; + case '\f': out += "\\f"; break; + case '\n': out += "\\n"; break; + case '\r': out += "\\r"; break; + case '\t': out += "\\t"; break; + default: + if (c < 0x20) { + char buffer[8]; + std::snprintf(buffer, sizeof(buffer), "\\u%04x", c); + out += buffer; + } else out += static_cast(c); + } + } + out += '"'; +} + +std::string RealText(double value) { + if (!std::isfinite(value)) throw std::runtime_error("sce::Json: cannot serialize a non-finite number"); + char buffer[32]; + std::snprintf(buffer, sizeof(buffer), "%.17g", value); + return buffer; +} + +void Serialize(std::string& out, const Node& n) { + switch (n.type) { + case TypeNull: out += "null"; return; + case TypeBoolean: out += n.boolean ? "true" : "false"; return; + case TypeInteger: out += std::to_string(n.integer); return; + case TypeUInteger: out += std::to_string(n.uinteger); return; + case TypeReal: out += RealText(n.real); return; + case TypeString: AppendEscaped(out, *n.string.text); return; + case TypeArray: { + out += '['; + bool first = true; + for (const auto& item : *n.array.items) { + if (!first) out += ','; + first = false; + Serialize(out, NodeOf(item)); + } + out += ']'; + return; + } + case TypeObject: { + out += '{'; + bool first = true; + for (const auto& pair : *n.object.items) { + if (!first) out += ','; + first = false; + AppendEscaped(out, *pair.key.text); + out += ':'; + Serialize(out, NodeOf(pair.value)); + } + out += '}'; + return; + } + } + throw std::runtime_error("sce::Json: invalid value type"); +} + +class Parser { +public: + Parser(const char* text, std::size_t size) : _text(text), _end(text + size) {} + + bool Parse(Node& out) { + skip(); + if (!value(out)) return false; + skip(); + return _text == _end || *_text == '\0'; + } + +private: + const char* _text; + const char* _end; + + void skip() { while (_text < _end && (*_text == ' ' || *_text == '\t' || *_text == '\n' || *_text == '\r')) ++_text; } + bool literal(const char* word) { + const auto length = std::strlen(word); + if (static_cast(_end - _text) < length || std::memcmp(_text, word, length) != 0) return false; + _text += length; + return true; + } + static void appendUtf8(std::string& out, std::uint32_t code) { + if (code < 0x80) out += static_cast(code); + else if (code < 0x800) { out += static_cast(0xc0 | (code >> 6)); out += static_cast(0x80 | (code & 0x3f)); } + else if (code < 0x10000) { out += static_cast(0xe0 | (code >> 12)); out += static_cast(0x80 | ((code >> 6) & 0x3f)); out += static_cast(0x80 | (code & 0x3f)); } + else { out += static_cast(0xf0 | (code >> 18)); out += static_cast(0x80 | ((code >> 12) & 0x3f)); out += static_cast(0x80 | ((code >> 6) & 0x3f)); out += static_cast(0x80 | (code & 0x3f)); } + } + bool hex4(std::uint32_t& code) { + if (_end - _text < 4) return false; + code = 0; + for (int i = 0; i < 4; ++i) { + const char c = *_text++; + code <<= 4; + if (c >= '0' && c <= '9') code |= static_cast(c - '0'); + else if (c >= 'a' && c <= 'f') code |= static_cast(c - 'a' + 10); + else if (c >= 'A' && c <= 'F') code |= static_cast(c - 'A' + 10); + else return false; + } + return true; + } + bool string(std::string& out) { + if (_text >= _end || *_text != '"') return false; + ++_text; + while (_text < _end && *_text != '"') { + const char c = *_text++; + if (static_cast(c) < 0x20) return false; + if (c != '\\') { out += c; continue; } + if (_text >= _end) return false; + switch (*_text++) { + case '"': out += '"'; break; + case '\\': out += '\\'; break; + case '/': out += '/'; break; + case 'b': out += '\b'; break; + case 'f': out += '\f'; break; + case 'n': out += '\n'; break; + case 'r': out += '\r'; break; + case 't': out += '\t'; break; + case 'u': { + std::uint32_t code = 0; + if (!hex4(code)) return false; + if (code >= 0xd800 && code < 0xdc00) { + std::uint32_t low = 0; + if (!literal("\\u") || !hex4(low) || low < 0xdc00 || low >= 0xe000) return false; + code = 0x10000 + ((code - 0xd800) << 10) + (low - 0xdc00); + } else if (code >= 0xdc00 && code < 0xe000) return false; + appendUtf8(out, code); + break; + } + default: return false; + } + } + if (_text >= _end) return false; + ++_text; + return true; + } + bool number(Node& out) { + const char* start = _text; + if (_text < _end && *_text == '-') ++_text; + if (_text >= _end || *_text < '0' || *_text > '9') return false; + if (*_text == '0') ++_text; + else while (_text < _end && *_text >= '0' && *_text <= '9') ++_text; + bool integral = true; + if (_text < _end && *_text == '.') { + integral = false; + ++_text; + if (_text >= _end || *_text < '0' || *_text > '9') return false; + while (_text < _end && *_text >= '0' && *_text <= '9') ++_text; + } + if (_text < _end && (*_text == 'e' || *_text == 'E')) { + integral = false; + ++_text; + if (_text < _end && (*_text == '+' || *_text == '-')) ++_text; + if (_text >= _end || *_text < '0' || *_text > '9') return false; + while (_text < _end && *_text >= '0' && *_text <= '9') ++_text; + } + const std::string text(start, _text); + if (integral) { + errno = 0; + if (text[0] == '-') { + const long long value = std::strtoll(text.c_str(), nullptr, 10); + if (errno == 0) { out.type = TypeInteger; out.integer = value; return true; } + } else { + const unsigned long long value = std::strtoull(text.c_str(), nullptr, 10); + if (errno == 0) { + if (value <= static_cast(INT64_MAX)) { out.type = TypeInteger; out.integer = static_cast(value); } + else { out.type = TypeUInteger; out.uinteger = value; } + return true; + } + } + } + out.type = TypeReal; + out.real = std::strtod(text.c_str(), nullptr); + return true; + } + bool value(Node& out) { + if (_text >= _end) return false; + switch (*_text) { + case 'n': if (!literal("null")) return false; SetType(out, TypeNull); return true; + case 't': if (!literal("true")) return false; SetType(out, TypeBoolean); out.boolean = true; return true; + case 'f': if (!literal("false")) return false; SetType(out, TypeBoolean); return true; + case '"': { + std::string text; + if (!string(text)) return false; + SetType(out, TypeString); + *out.string.text = std::move(text); + return true; + } + case '[': { + ++_text; + SetType(out, TypeArray); + skip(); + if (_text < _end && *_text == ']') { ++_text; return true; } + for (;;) { + Value item{}; + Construct(item); + out.array.items->push_back(item); + skip(); + if (!value(*item.node)) return false; + skip(); + if (_text < _end && *_text == ',') { ++_text; continue; } + if (_text < _end && *_text == ']') { ++_text; return true; } + return false; + } + } + case '{': { + ++_text; + SetType(out, TypeObject); + skip(); + if (_text < _end && *_text == '}') { ++_text; return true; } + for (;;) { + skip(); + std::string key; + if (!string(key)) return false; + skip(); + if (_text >= _end || *_text != ':') return false; + ++_text; + skip(); + Pair pair{}; + Construct(pair.key, key); + Construct(pair.value); + out.object.items->push_back(pair); + if (!value(*pair.value.node)) return false; + skip(); + if (_text < _end && *_text == ',') { ++_text; continue; } + if (_text < _end && *_text == '}') { ++_text; return true; } + return false; + } + } + default: + return number(out); + } + } +}; + +[[noreturn]] void APS5_VABI JsonMemAllocatorPureVirtual() { + throw std::runtime_error("sce::Json::MemAllocator: pure virtual function called"); +} + +void* const JsonMemAllocatorVtable[] = { + nullptr, nullptr, + reinterpret_cast(&JsonMemAllocatorPureVirtual), reinterpret_cast(&JsonMemAllocatorPureVirtual), + reinterpret_cast(&JsonMemAllocatorPureVirtual), reinterpret_cast(&JsonMemAllocatorPureVirtual), + reinterpret_cast(&JsonMemAllocatorPureVirtual), reinterpret_cast(&JsonMemAllocatorPureVirtual), +}; + +Value& ObjectEntry(Object& object, const std::string& key) { + for (auto& pair : *object.items) + if (*pair.key.text == key) return pair.value; + Pair pair{}; + Construct(pair.key, key); + Construct(pair.value); + object.items->push_back(pair); + return object.items->back().value; +} + +} extern "C" { -// Initialization succeeds as on an offline console: the title tears its whole web API layer down on -// an init failure and later dereferences it anyway. Requests made through it fail as offline. -int APS5_VABI _ZN3sce4Json11Initializer10initializeEPKNS0_13InitParameterE(void* self, const void* parameter) { - (void)self; - (void)parameter; - return 0; -} - int APS5_VABI _ZN3sce4Json11InitializerC1Ev(void* self) { (void)self; return 0; @@ -24,13 +467,261 @@ int APS5_VABI _ZN3sce4Json11InitializerD1Ev(void* self) { return 0; } -int APS5_VABI _ZN3sce4Json12MemAllocatorC2Ev(void* self) { +int APS5_VABI _ZN3sce4Json11Initializer10initializeEPKNS0_13InitParameterE(void* self, const void* parameter) { (void)self; + if (parameter == nullptr) throw std::invalid_argument("sce::Json::Initializer::initialize: null parameter"); return 0; } -int APS5_VABI _ZN3sce4Json12MemAllocatorD2Ev(void* self) { +int APS5_VABI _ZN3sce4Json11Initializer9terminateEv(void* self) { (void)self; + auto& state = State(); + std::lock_guard lock(state.mutex); + state.callback = nullptr; + state.context = nullptr; + return 0; +} + +int APS5_VABI _ZN3sce4Json11Initializer27setGlobalNullAccessCallBackEPFRKNS0_5ValueENS0_9ValueTypeEPS3_PvES7_(void* self, NullAccessCallback callback, void* context) { + (void)self; + auto& state = State(); + std::lock_guard lock(state.mutex); + state.callback = callback; + state.context = context; + return 0; +} + +void APS5_VABI _ZN3sce4Json12MemAllocatorC2Ev(void* self) { + *static_cast(self) = &JsonMemAllocatorVtable[2]; +} + +void APS5_VABI _ZN3sce4Json12MemAllocatorD2Ev(void* self) { + *static_cast(self) = &JsonMemAllocatorVtable[2]; +} + +void APS5_VABI _ZN3sce4Json6StringC1Ev(String* self) { Construct(*self, std::string()); } +void APS5_VABI _ZN3sce4Json6StringC1EPKc(String* self, const char* text) { + if (text == nullptr) throw std::invalid_argument("sce::Json::String: null text"); + Construct(*self, text); +} +void APS5_VABI _ZN3sce4Json6StringC1ERKS1_(String* self, const String* other) { Construct(*self, *other->text); } +void APS5_VABI _ZN3sce4Json6StringD1Ev(String* self) { Destroy(*self); } +String* APS5_VABI _ZN3sce4Json6StringaSERKS1_(String* self, const String* other) { + if (self != other) *self->text = *other->text; + return self; +} +const char* APS5_VABI _ZNK3sce4Json6String5c_strEv(const String* self) { return self->text->c_str(); } +bool APS5_VABI _ZNK3sce4Json6String5emptyEv(const String* self) { return self->text->empty(); } +std::size_t APS5_VABI _ZNK3sce4Json6String6lengthEv(const String* self) { return self->text->size(); } +bool APS5_VABI _ZNK3sce4Json6StringeqEPKc(const String* self, const char* text) { return text != nullptr && *self->text == text; } + +void APS5_VABI _ZN3sce4Json5ArrayC1Ev(Array* self) { Construct(*self); } +void APS5_VABI _ZN3sce4Json5ArrayD1Ev(Array* self) { Destroy(*self); } +void APS5_VABI _ZN3sce4Json5Array9push_backERKNS0_5ValueE(Array* self, const Value* value) { + Value copy{}; + CopyInto(copy, *value); + self->items->push_back(copy); +} +ArrayIterator* APS5_VABI _ZNK3sce4Json5Array5beginEv(ArrayIterator* result, const Array* self) { result->it = self->items->begin(); return result; } +ArrayIterator* APS5_VABI _ZNK3sce4Json5Array3endEv(ArrayIterator* result, const Array* self) { result->it = self->items->end(); return result; } +bool APS5_VABI _ZNK3sce4Json5Array5emptyEv(const Array* self) { return self->items->empty(); } +std::size_t APS5_VABI _ZNK3sce4Json5Array4sizeEv(const Array* self) { return self->items->size(); } +const Value* APS5_VABI _ZNK3sce4Json5Array4backEv(const Array* self) { + if (self->items->empty()) throw std::out_of_range("sce::Json::Array::back: empty array"); + return &self->items->back(); +} +void APS5_VABI _ZN3sce4Json5Array8iteratorD1Ev(ArrayIterator* self) { (void)self; } +ArrayIterator* APS5_VABI _ZN3sce4Json5Array8iteratorppEv(ArrayIterator* self) { ++self->it; return self; } +Value* APS5_VABI _ZNK3sce4Json5Array8iteratordeEv(const ArrayIterator* self) { return &*self->it; } +bool APS5_VABI _ZNK3sce4Json5Array8iteratorneERKS2_(const ArrayIterator* self, const ArrayIterator* other) { return self->it != other->it; } + +void APS5_VABI _ZN3sce4Json6ObjectC1Ev(Object* self) { Construct(*self); } +void APS5_VABI _ZN3sce4Json6ObjectC1ERKS1_(Object* self, const Object* other) { CopyObject(*self, *other); } +void APS5_VABI _ZN3sce4Json6ObjectD1Ev(Object* self) { Destroy(*self); } +Object* APS5_VABI _ZN3sce4Json6ObjectaSERKS1_(Object* self, const Object* other) { + if (self == other) return self; + Object copy{}; + CopyObject(copy, *other); + Destroy(*self); + *self = copy; + return self; +} +void APS5_VABI _ZN3sce4Json6Object5clearEv(Object* self) { + for (auto& pair : *self->items) { + Destroy(pair.key); + Destroy(pair.value); + } + self->items->clear(); +} +Value* APS5_VABI _ZN3sce4Json6ObjectixERKNS0_6StringE(Object* self, const String* key) { return &ObjectEntry(*self, *key->text); } +ObjectIterator* APS5_VABI _ZNK3sce4Json6Object5beginEv(ObjectIterator* result, const Object* self) { result->it = self->items->begin(); return result; } +ObjectIterator* APS5_VABI _ZNK3sce4Json6Object3endEv(ObjectIterator* result, const Object* self) { result->it = self->items->end(); return result; } +void APS5_VABI _ZN3sce4Json6Object8iteratorD1Ev(ObjectIterator* self) { (void)self; } +ObjectIterator* APS5_VABI _ZN3sce4Json6Object8iteratorppEv(ObjectIterator* self) { ++self->it; return self; } +Pair* APS5_VABI _ZNK3sce4Json6Object8iteratordeEv(const ObjectIterator* self) { return &*self->it; } +bool APS5_VABI _ZNK3sce4Json6Object8iteratorneERKS2_(const ObjectIterator* self, const ObjectIterator* other) { return self->it != other->it; } + +void APS5_VABI _ZN3sce4Json5ValueC1Ev(Value* self) { Construct(*self); } +void APS5_VABI _ZN3sce4Json5ValueC1Eb(Value* self, bool value) { Construct(*self); self->node->type = TypeBoolean; self->node->boolean = value; } +void APS5_VABI _ZN3sce4Json5ValueC1El(Value* self, std::int64_t value) { Construct(*self); self->node->type = TypeInteger; self->node->integer = value; } +void APS5_VABI _ZN3sce4Json5ValueC1Em(Value* self, std::uint64_t value) { Construct(*self); self->node->type = TypeUInteger; self->node->uinteger = value; } +void APS5_VABI _ZN3sce4Json5ValueC1Ed(Value* self, double value) { Construct(*self); self->node->type = TypeReal; self->node->real = value; } +void APS5_VABI _ZN3sce4Json5ValueC1EPKc(Value* self, const char* text) { + if (text == nullptr) throw std::invalid_argument("sce::Json::Value: null text"); + Construct(*self); + SetType(*self->node, TypeString); + *self->node->string.text = text; +} +void APS5_VABI _ZN3sce4Json5ValueC1ERKNS0_6StringE(Value* self, const String* text) { + Construct(*self); + SetType(*self->node, TypeString); + *self->node->string.text = *text->text; +} +void APS5_VABI _ZN3sce4Json5ValueC1ERKNS0_5ArrayE(Value* self, const Array* array) { + Construct(*self); + self->node->type = TypeArray; + CopyArray(self->node->array, *array); +} +void APS5_VABI _ZN3sce4Json5ValueC1ERKNS0_6ObjectE(Value* self, const Object* object) { + Construct(*self); + self->node->type = TypeObject; + CopyObject(self->node->object, *object); +} +void APS5_VABI _ZN3sce4Json5ValueC1ERKS1_(Value* self, const Value* other) { CopyInto(*self, *other); } +void APS5_VABI _ZN3sce4Json5ValueD1Ev(Value* self) { Destroy(*self); } +Value* APS5_VABI _ZN3sce4Json5ValueaSERKS1_(Value* self, const Value* other) { + if (self != other) AssignNode(NodeOf(*self), NodeOf(*other)); + return self; +} + +int APS5_VABI _ZN3sce4Json5Value3setENS0_9ValueTypeE(Value* self, ValueType type) { SetType(NodeOf(*self), type); return 0; } +int APS5_VABI _ZN3sce4Json5Value3setEb(Value* self, bool value) { SetType(NodeOf(*self), TypeBoolean); self->node->boolean = value; return 0; } +int APS5_VABI _ZN3sce4Json5Value3setEl(Value* self, std::int64_t value) { SetType(NodeOf(*self), TypeInteger); self->node->integer = value; return 0; } +int APS5_VABI _ZN3sce4Json5Value3setEm(Value* self, std::uint64_t value) { SetType(NodeOf(*self), TypeUInteger); self->node->uinteger = value; return 0; } +int APS5_VABI _ZN3sce4Json5Value3setEd(Value* self, double value) { SetType(NodeOf(*self), TypeReal); self->node->real = value; return 0; } +int APS5_VABI _ZN3sce4Json5Value3setEPKc(Value* self, const char* text) { + if (text == nullptr) throw std::invalid_argument("sce::Json::Value::set: null text"); + const std::string copy(text); + SetType(NodeOf(*self), TypeString); + *self->node->string.text = copy; + return 0; +} +int APS5_VABI _ZN3sce4Json5Value3setERKNS0_6StringE(Value* self, const String* text) { + const std::string copy(*text->text); + SetType(NodeOf(*self), TypeString); + *self->node->string.text = copy; + return 0; +} +int APS5_VABI _ZN3sce4Json5Value3setERKNS0_5ArrayE(Value* self, const Array* array) { + Array copy{}; + CopyArray(copy, *array); + Clear(NodeOf(*self)); + self->node->type = TypeArray; + self->node->array = copy; + return 0; +} +int APS5_VABI _ZN3sce4Json5Value3setERKNS0_6ObjectE(Value* self, const Object* object) { + Object copy{}; + CopyObject(copy, *object); + Clear(NodeOf(*self)); + self->node->type = TypeObject; + self->node->object = copy; + return 0; +} +int APS5_VABI _ZN3sce4Json5Value3setERKS1_(Value* self, const Value* other) { + if (self != other) AssignNode(NodeOf(*self), NodeOf(*other)); + return 0; +} + +ValueType APS5_VABI _ZNK3sce4Json5Value7getTypeEv(const Value* self) { return NodeOf(*self).type; } +bool APS5_VABI _ZNK3sce4Json5Value10getBooleanEv(const Value* self) { return Typed(self, TypeBoolean).boolean; } +std::int64_t APS5_VABI _ZNK3sce4Json5Value10getIntegerEv(const Value* self) { + const Node& n = NodeOf(*self); + if (n.type == TypeUInteger && n.uinteger <= static_cast(INT64_MAX)) return static_cast(n.uinteger); + return Typed(self, TypeInteger).integer; +} +std::uint64_t APS5_VABI _ZNK3sce4Json5Value11getUIntegerEv(const Value* self) { + const Node& n = NodeOf(*self); + if (n.type == TypeInteger && n.integer >= 0) return static_cast(n.integer); + return Typed(self, TypeUInteger).uinteger; +} +double APS5_VABI _ZNK3sce4Json5Value7getRealEv(const Value* self) { + const Node& n = NodeOf(*self); + if (n.type == TypeInteger) return static_cast(n.integer); + if (n.type == TypeUInteger) return static_cast(n.uinteger); + return Typed(self, TypeReal).real; +} +const String* APS5_VABI _ZNK3sce4Json5Value9getStringEv(const Value* self) { return &Typed(self, TypeString).string; } +const Array* APS5_VABI _ZNK3sce4Json5Value8getArrayEv(const Value* self) { return &Typed(self, TypeArray).array; } +const Object* APS5_VABI _ZNK3sce4Json5Value9getObjectEv(const Value* self) { return &Typed(self, TypeObject).object; } +std::size_t APS5_VABI _ZNK3sce4Json5Value5countEv(const Value* self) { + const Node& n = NodeOf(*self); + if (n.type == TypeArray) return n.array.items->size(); + if (n.type == TypeObject) return n.object.items->size(); + return 0; +} +const Value* APS5_VABI _ZNK3sce4Json5ValueixEPKc(const Value* self, const char* key) { + const Node& n = NodeOf(*self); + if (key != nullptr && n.type == TypeObject) + for (const auto& pair : *n.object.items) + if (*pair.key.text == key) return &pair.value; + return &NullAccess(TypeNull, self); +} +const Value* APS5_VABI _ZNK3sce4Json5ValueixEm(const Value* self, std::size_t index) { + const Node& n = NodeOf(*self); + if (n.type == TypeArray && index < n.array.items->size()) { + auto it = n.array.items->begin(); + std::advance(it, static_cast(index)); + return &*it; + } + return &NullAccess(TypeNull, self); +} +Value* APS5_VABI _ZN3sce4Json5Value10referValueERKNS0_6StringE(Value* self, const String* key) { + Node& n = NodeOf(*self); + if (n.type == TypeNull) SetType(n, TypeObject); + if (n.type != TypeObject) throw std::runtime_error("sce::Json::Value::referValue: value is not an object"); + return &ObjectEntry(n.object, *key->text); +} +Array* APS5_VABI _ZN3sce4Json5Value10referArrayEv(Value* self) { + Node& n = NodeOf(*self); + if (n.type == TypeNull) SetType(n, TypeArray); + if (n.type != TypeArray) throw std::runtime_error("sce::Json::Value::referArray: value is not an array"); + return &n.array; +} +Object* APS5_VABI _ZN3sce4Json5Value11referObjectEv(Value* self) { + Node& n = NodeOf(*self); + if (n.type == TypeNull) SetType(n, TypeObject); + if (n.type != TypeObject) throw std::runtime_error("sce::Json::Value::referObject: value is not an object"); + return &n.object; +} +int APS5_VABI _ZN3sce4Json5Value9serializeERNS0_6StringE(Value* self, String* out) { + std::string text; + Serialize(text, NodeOf(*self)); + *out->text = std::move(text); + return 0; +} +int APS5_VABI _ZNK3sce4Json5Value8toStringERNS0_6StringE(const Value* self, String* out) { + const Node& n = NodeOf(*self); + if (n.type == TypeString) { + *out->text = *n.string.text; + return 0; + } + std::string text; + Serialize(text, n); + *out->text = std::move(text); + return 0; +} + +int APS5_VABI _ZN3sce4Json6Parser5parseERNS0_5ValueEPKcm(Value* out, const char* text, std::size_t size) { + if (text == nullptr) throw std::invalid_argument("sce::Json::Parser::parse: null text"); + Node parsed{}; + Parser parser(text, size); + if (!parser.Parse(parsed)) { + Clear(parsed); + return JsonErrorParse; + } + Clear(NodeOf(*out)); + *out->node = parsed; return 0; } diff --git a/core/libs/tests/GuestJson.cpp b/core/libs/tests/GuestJson.cpp new file mode 100644 index 000000000..2dfbdb93c --- /dev/null +++ b/core/libs/tests/GuestJson.cpp @@ -0,0 +1,250 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +struct Value { void* node; }; +struct String { void* text; }; +struct Array { void* items; }; +struct Object { void* items; }; +struct Iterator { void* position; }; +struct Pair { + String key; + std::uint64_t reserved; + Value value; +}; +using NullAccessCallback = const Value& (APS5_VABI*)(std::int32_t, const Value*, void*); + +extern "C" { +int APS5_VABI _ZN3sce4Json11InitializerC1Ev(void*); +int APS5_VABI _ZN3sce4Json11InitializerD1Ev(void*); +int APS5_VABI _ZN3sce4Json11Initializer10initializeEPKNS0_13InitParameterE(void*, const void*); +int APS5_VABI _ZN3sce4Json11Initializer9terminateEv(void*); +int APS5_VABI _ZN3sce4Json11Initializer27setGlobalNullAccessCallBackEPFRKNS0_5ValueENS0_9ValueTypeEPS3_PvES7_(void*, NullAccessCallback, void*); +void APS5_VABI _ZN3sce4Json6StringC1Ev(String*); +void APS5_VABI _ZN3sce4Json6StringC1EPKc(String*, const char*); +void APS5_VABI _ZN3sce4Json6StringD1Ev(String*); +const char* APS5_VABI _ZNK3sce4Json6String5c_strEv(const String*); +std::size_t APS5_VABI _ZNK3sce4Json6String6lengthEv(const String*); +void APS5_VABI _ZN3sce4Json5ArrayC1Ev(Array*); +void APS5_VABI _ZN3sce4Json5ArrayD1Ev(Array*); +void APS5_VABI _ZN3sce4Json5Array9push_backERKNS0_5ValueE(Array*, const Value*); +Iterator* APS5_VABI _ZNK3sce4Json5Array5beginEv(Iterator*, const Array*); +Iterator* APS5_VABI _ZNK3sce4Json5Array3endEv(Iterator*, const Array*); +std::size_t APS5_VABI _ZNK3sce4Json5Array4sizeEv(const Array*); +const Value* APS5_VABI _ZNK3sce4Json5Array4backEv(const Array*); +void APS5_VABI _ZN3sce4Json5Array8iteratorD1Ev(Iterator*); +Iterator* APS5_VABI _ZN3sce4Json5Array8iteratorppEv(Iterator*); +Value* APS5_VABI _ZNK3sce4Json5Array8iteratordeEv(const Iterator*); +bool APS5_VABI _ZNK3sce4Json5Array8iteratorneERKS2_(const Iterator*, const Iterator*); +void APS5_VABI _ZN3sce4Json6ObjectC1Ev(Object*); +void APS5_VABI _ZN3sce4Json6ObjectD1Ev(Object*); +Value* APS5_VABI _ZN3sce4Json6ObjectixERKNS0_6StringE(Object*, const String*); +Iterator* APS5_VABI _ZNK3sce4Json6Object5beginEv(Iterator*, const Object*); +Iterator* APS5_VABI _ZNK3sce4Json6Object3endEv(Iterator*, const Object*); +void APS5_VABI _ZN3sce4Json6Object8iteratorD1Ev(Iterator*); +Iterator* APS5_VABI _ZN3sce4Json6Object8iteratorppEv(Iterator*); +Pair* APS5_VABI _ZNK3sce4Json6Object8iteratordeEv(const Iterator*); +bool APS5_VABI _ZNK3sce4Json6Object8iteratorneERKS2_(const Iterator*, const Iterator*); +void APS5_VABI _ZN3sce4Json5ValueC1Ev(Value*); +void APS5_VABI _ZN3sce4Json5ValueC1Eb(Value*, bool); +void APS5_VABI _ZN3sce4Json5ValueC1Ed(Value*, double); +void APS5_VABI _ZN3sce4Json5ValueC1ERKNS0_6ObjectE(Value*, const Object*); +void APS5_VABI _ZN3sce4Json5ValueD1Ev(Value*); +int APS5_VABI _ZN3sce4Json5Value3setEl(Value*, std::int64_t); +int APS5_VABI _ZN3sce4Json5Value3setEPKc(Value*, const char*); +std::int32_t APS5_VABI _ZNK3sce4Json5Value7getTypeEv(const Value*); +bool APS5_VABI _ZNK3sce4Json5Value10getBooleanEv(const Value*); +std::int64_t APS5_VABI _ZNK3sce4Json5Value10getIntegerEv(const Value*); +std::uint64_t APS5_VABI _ZNK3sce4Json5Value11getUIntegerEv(const Value*); +double APS5_VABI _ZNK3sce4Json5Value7getRealEv(const Value*); +const String* APS5_VABI _ZNK3sce4Json5Value9getStringEv(const Value*); +const Array* APS5_VABI _ZNK3sce4Json5Value8getArrayEv(const Value*); +std::size_t APS5_VABI _ZNK3sce4Json5Value5countEv(const Value*); +const Value* APS5_VABI _ZNK3sce4Json5ValueixEPKc(const Value*, const char*); +const Value* APS5_VABI _ZNK3sce4Json5ValueixEm(const Value*, std::size_t); +int APS5_VABI _ZN3sce4Json5Value9serializeERNS0_6StringE(Value*, String*); +int APS5_VABI _ZN3sce4Json6Parser5parseERNS0_5ValueEPKcm(Value*, const char*, std::size_t); +} + +static void Check(bool value, int line) { + if (!value) { + std::fprintf(stderr, "Json check failed at line %d\n", line); + std::abort(); + } +} +#define Require(value) Check((value), __LINE__) + +enum : std::int32_t { TypeNull, TypeBoolean, TypeInteger, TypeUInteger, TypeReal, TypeString, TypeArray, TypeObject }; + +static Value fallback{}; +static int callbackCalls = 0; +static std::int32_t lastRequested = -1; +static const Value* lastParent = nullptr; +static void* lastContext = nullptr; + +static const Value& APS5_VABI OnNullAccess(std::int32_t requested, const Value* parent, void* context) { + ++callbackCalls; + lastRequested = requested; + lastParent = parent; + lastContext = context; + return fallback; +} + +static std::string Serialize(Value& value) { + String out{}; + _ZN3sce4Json6StringC1Ev(&out); + Require(_ZN3sce4Json5Value9serializeERNS0_6StringE(&value, &out) == 0); + std::string text(_ZNK3sce4Json6String5c_strEv(&out), _ZNK3sce4Json6String6lengthEv(&out)); + _ZN3sce4Json6StringD1Ev(&out); + return text; +} + +static const Value& Member(const Value& value, const char* key) { + return *_ZNK3sce4Json5ValueixEPKc(&value, key); +} + +static void ParseAndRoundTrip() { + const std::string text = + "{ \"name\" : \"x\\u00e9\\n\", \"count\": 3, \"big\": 18446744073709551615, \"neg\": -5," + " \"pi\": 1.5, \"ok\": true, \"none\": null, \"list\": [1, \"two\", [3]] }"; + Value root{}; + _ZN3sce4Json5ValueC1Ev(&root); + Require(_ZN3sce4Json6Parser5parseERNS0_5ValueEPKcm(&root, text.c_str(), text.size()) == 0); + Require(_ZNK3sce4Json5Value7getTypeEv(&root) == TypeObject); + Require(_ZNK3sce4Json5Value5countEv(&root) == 8); + Require(std::strcmp(_ZNK3sce4Json6String5c_strEv(_ZNK3sce4Json5Value9getStringEv(&Member(root, "name"))), "x\xc3\xa9\n") == 0); + Require(_ZNK3sce4Json5Value7getTypeEv(&Member(root, "count")) == TypeInteger); + Require(_ZNK3sce4Json5Value10getIntegerEv(&Member(root, "count")) == 3); + Require(_ZNK3sce4Json5Value11getUIntegerEv(&Member(root, "count")) == 3); + Require(_ZNK3sce4Json5Value7getRealEv(&Member(root, "count")) == 3.0); + Require(_ZNK3sce4Json5Value7getTypeEv(&Member(root, "big")) == TypeUInteger); + Require(_ZNK3sce4Json5Value11getUIntegerEv(&Member(root, "big")) == std::numeric_limits::max()); + Require(_ZNK3sce4Json5Value10getIntegerEv(&Member(root, "neg")) == -5); + Require(_ZNK3sce4Json5Value7getRealEv(&Member(root, "pi")) == 1.5); + Require(_ZNK3sce4Json5Value10getBooleanEv(&Member(root, "ok"))); + Require(_ZNK3sce4Json5Value7getTypeEv(&Member(root, "none")) == TypeNull); + + const Value& list = Member(root, "list"); + const Array* array = _ZNK3sce4Json5Value8getArrayEv(&list); + Require(_ZNK3sce4Json5Array4sizeEv(array) == 3); + std::vector types; + Iterator it{}, end{}; + _ZNK3sce4Json5Array5beginEv(&it, array); + _ZNK3sce4Json5Array3endEv(&end, array); + for (; _ZNK3sce4Json5Array8iteratorneERKS2_(&it, &end); _ZN3sce4Json5Array8iteratorppEv(&it)) + types.push_back(_ZNK3sce4Json5Value7getTypeEv(_ZNK3sce4Json5Array8iteratordeEv(&it))); + _ZN3sce4Json5Array8iteratorD1Ev(&it); + _ZN3sce4Json5Array8iteratorD1Ev(&end); + Require((types == std::vector{TypeInteger, TypeString, TypeArray})); + Require(_ZNK3sce4Json5Value10getIntegerEv(_ZNK3sce4Json5ValueixEm(_ZNK3sce4Json5ValueixEm(&list, 2), 0)) == 3); + + const std::string serialized = Serialize(root); + Require(serialized == "{\"name\":\"x\xc3\xa9\\n\",\"count\":3,\"big\":18446744073709551615,\"neg\":-5," + "\"pi\":1.5,\"ok\":true,\"none\":null,\"list\":[1,\"two\",[3]]}"); + Value reparsed{}; + _ZN3sce4Json5ValueC1Ev(&reparsed); + Require(_ZN3sce4Json6Parser5parseERNS0_5ValueEPKcm(&reparsed, serialized.c_str(), serialized.size()) == 0); + Require(Serialize(reparsed) == serialized); + _ZN3sce4Json5ValueD1Ev(&reparsed); + + for (const char* invalid : {"{\"a\":}", "[1,]", "\"open", "tru", "{} x", "01"}) { + Require(_ZN3sce4Json6Parser5parseERNS0_5ValueEPKcm(&root, invalid, std::strlen(invalid)) < 0); + Require(_ZNK3sce4Json5Value7getTypeEv(&root) == TypeObject && _ZNK3sce4Json5Value5countEv(&root) == 8); + } + _ZN3sce4Json5ValueD1Ev(&root); +} + +static void ObjectsAndArrays() { + Object object{}; + _ZN3sce4Json6ObjectC1Ev(&object); + String alpha{}, beta{}; + _ZN3sce4Json6StringC1EPKc(&alpha, "alpha"); + _ZN3sce4Json6StringC1EPKc(&beta, "beta"); + Value* first = _ZN3sce4Json6ObjectixERKNS0_6StringE(&object, &alpha); + Require(_ZNK3sce4Json5Value7getTypeEv(first) == TypeNull); + Require(_ZN3sce4Json5Value3setEl(first, 1) == 0); + Require(_ZN3sce4Json5Value3setEPKc(_ZN3sce4Json6ObjectixERKNS0_6StringE(&object, &beta), "b") == 0); + Require(_ZN3sce4Json6ObjectixERKNS0_6StringE(&object, &alpha) == first); + + std::vector keys; + Iterator it{}, end{}; + _ZNK3sce4Json6Object5beginEv(&it, &object); + _ZNK3sce4Json6Object3endEv(&end, &object); + for (; _ZNK3sce4Json6Object8iteratorneERKS2_(&it, &end); _ZN3sce4Json6Object8iteratorppEv(&it)) { + const Pair* pair = _ZNK3sce4Json6Object8iteratordeEv(&it); + keys.emplace_back(_ZNK3sce4Json6String5c_strEv(&pair->key)); + if (keys.size() == 1) Require(&pair->value == first); + } + _ZN3sce4Json6Object8iteratorD1Ev(&it); + _ZN3sce4Json6Object8iteratorD1Ev(&end); + Require((keys == std::vector{"alpha", "beta"})); + + Value wrapped{}; + _ZN3sce4Json5ValueC1ERKNS0_6ObjectE(&wrapped, &object); + Require(Serialize(wrapped) == "{\"alpha\":1,\"beta\":\"b\"}"); + _ZN3sce4Json5ValueD1Ev(&wrapped); + _ZN3sce4Json6StringD1Ev(&alpha); + _ZN3sce4Json6StringD1Ev(&beta); + _ZN3sce4Json6ObjectD1Ev(&object); + + Array array{}; + _ZN3sce4Json5ArrayC1Ev(&array); + Value flag{}, real{}; + _ZN3sce4Json5ValueC1Eb(&flag, true); + _ZN3sce4Json5ValueC1Ed(&real, 2.5); + _ZN3sce4Json5Array9push_backERKNS0_5ValueE(&array, &flag); + _ZN3sce4Json5Array9push_backERKNS0_5ValueE(&array, &real); + _ZN3sce4Json5ValueD1Ev(&flag); + _ZN3sce4Json5ValueD1Ev(&real); + Require(_ZNK3sce4Json5Array4sizeEv(&array) == 2); + Require(_ZNK3sce4Json5Value7getRealEv(_ZNK3sce4Json5Array4backEv(&array)) == 2.5); + int visited = 0; + _ZNK3sce4Json5Array5beginEv(&it, &array); + _ZNK3sce4Json5Array3endEv(&end, &array); + for (; _ZNK3sce4Json5Array8iteratorneERKS2_(&it, &end); _ZN3sce4Json5Array8iteratorppEv(&it)) ++visited; + Require(visited == 2); + _ZN3sce4Json5ArrayD1Ev(&array); +} + +static void NullAccess() { + alignas(16) std::uint8_t initializer[16]{}; + alignas(16) std::uint8_t parameter[64]{}; + Require(_ZN3sce4Json11InitializerC1Ev(initializer) == 0); + Require(_ZN3sce4Json11Initializer10initializeEPKNS0_13InitParameterE(initializer, parameter) == 0); + int context = 0; + Require(_ZN3sce4Json11Initializer27setGlobalNullAccessCallBackEPFRKNS0_5ValueENS0_9ValueTypeEPS3_PvES7_(initializer, OnNullAccess, &context) == 0); + _ZN3sce4Json5ValueC1Ev(&fallback); + Require(_ZN3sce4Json5Value3setEl(&fallback, 99) == 0); + + Value root{}; + _ZN3sce4Json5ValueC1Ev(&root); + const char text[] = "{\"present\":\"text\"}"; + Require(_ZN3sce4Json6Parser5parseERNS0_5ValueEPKcm(&root, text, sizeof(text) - 1) == 0); + Require(&Member(root, "missing") == &fallback); + Require(callbackCalls == 1 && lastRequested == TypeNull && lastParent == &root && lastContext == &context); + Require(_ZNK3sce4Json5Value10getIntegerEv(&Member(root, "present")) == 99); + Require(callbackCalls == 2 && lastRequested == TypeInteger && lastParent == &Member(root, "present")); + Require(!_ZNK3sce4Json5Value10getBooleanEv(&Member(root, "present"))); + Require(callbackCalls == 3 && lastRequested == TypeBoolean); + Require(_ZNK3sce4Json5Value7getTypeEv(_ZNK3sce4Json5ValueixEm(&root, 0)) == TypeInteger); + + Require(_ZN3sce4Json11Initializer9terminateEv(initializer) == 0); + const Value& unhandled = Member(root, "missing"); + Require(&unhandled != &fallback && _ZNK3sce4Json5Value7getTypeEv(&unhandled) == TypeNull); + Require(callbackCalls == 4); + _ZN3sce4Json5ValueD1Ev(&root); + _ZN3sce4Json5ValueD1Ev(&fallback); + Require(_ZN3sce4Json11InitializerD1Ev(initializer) == 0); +} + +int main() { + ParseAndRoundTrip(); + ObjectsAndArrays(); + NullAccess(); +}