From 1d5f815b3964edee8a2d701e1a6cc078c89d677f Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Thu, 18 Nov 2021 15:58:12 -0500 Subject: RTTI/JSON (#2021) * #include an absolute path didn't work - because paths were taken to always be relative. * Use 'Process' to communicate with an command line tool. * Remove slang-win-stream * Tidy up windows ProcessUtil. * First version of BufferedReadStream. * Windows working IPC for steams. * Test proxy count option. * Split Process/ProcessUtil. Process is platform dependant. ProcessUtil are functions that are platform independent. * First implementation of Unix Process interface. * Unix process compiles on cygwin. * Fix typo in unix process. * Separate unix pipe stream error of invalid access, from pipe availability. * Fix in standard line extraction. * Make fd non blocking. * Fix issues with Windows Process streams. * Added UnixPipe. * Some fixes around UnixPipeStream. * Make a unix stream closed explicit. * Hack to debug linux process/stream. * Revert to old linux pipe handling. * Pass executable path for unit tests. Split out CommandLine into own source. * Small improvements in process/command line. * Check process behavior with crash. * Make stderr and stdout unbuffered for crash testing. * Only turn disable buffering in crash test. * Disable crash test on CI. * Fix crash on clang/linux. * Enable crash test. Remove _appendBuffer as can use StreamUtil functionality. * Added inital processing for http headers. * Small improvements to HttpHeader. * First pass HTTPPacketConnection working on windows. * Enable other Process communication tests. * Update comments. * WIP JSON RPC. * Add terminate to Process. Made JSONRPC a Util. * Small tidy up around HTTPPacketConnection. * Improve process termination options. * WIP for test-server. * Add diagnostics error handling to test-server. * Improved JSON support. Parsing/creating JSON-RPC messages. * WIP JSONRPC parsing. * First pass RttiInfo support. * WIP converting between JSON/native types. * Project files. * Split out RttiUtil. Made RttiInfo constuction thread safe. * WIP RTTI<->JSON. * Add diagnostics to JSON<->native conversions. * Make RttiInfo for structs globals. Avoids problem around derived types (like pointers), being able to cause an abort. * Add pointer support to RTTI. Fixed some compilation issues on linux. * Add fixed array support. * Added Rtti unit test. * Add rtti unit test. * Split out quoted/unquoted key handling. Fix bugs in JSON value/container. Added JSON native test. * Make default array allocator use malloc/free. Remove the new[] handler (doesn't work on visuals studio). * Fix for linux warning. * Remove some test code. * Fix issues on x86 win. * Fix warning on aarch64. --- source/core/slang-rtti-info.cpp | 184 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 source/core/slang-rtti-info.cpp (limited to 'source/core/slang-rtti-info.cpp') diff --git a/source/core/slang-rtti-info.cpp b/source/core/slang-rtti-info.cpp new file mode 100644 index 000000000..f53cf742f --- /dev/null +++ b/source/core/slang-rtti-info.cpp @@ -0,0 +1,184 @@ +#include "slang-rtti-info.h" + +#include "../../slang-com-helper.h" + +#include + +namespace Slang { + +#define SLANG_RTTI_INFO_INVALID(name) RttiInfo{RttiInfo::Kind::Invalid, 0, 0} +#define SLANG_RTTI_INFO_BASIC(name, type) \ + RttiInfo{RttiInfo::Kind::name, RttiInfo::AlignmentType(SLANG_ALIGN_OF(type)), RttiInfo::SizeType(sizeof(type))} + +/* static */const RttiInfo RttiInfo::g_basicTypes[Index(Kind::CountOf)] = +{ + SLANG_RTTI_INFO_INVALID(Invalid), + SLANG_RTTI_INFO_BASIC(I32, int32_t), + SLANG_RTTI_INFO_BASIC(U32, uint32_t), + SLANG_RTTI_INFO_BASIC(I64, int64_t), + SLANG_RTTI_INFO_BASIC(U64, uint64_t), + SLANG_RTTI_INFO_BASIC(F32, float), + SLANG_RTTI_INFO_BASIC(F64, double), + SLANG_RTTI_INFO_BASIC(Bool, bool), + SLANG_RTTI_INFO_BASIC(String, String), + SLANG_RTTI_INFO_BASIC(UnownedStringSlice, UnownedStringSlice), + SLANG_RTTI_INFO_BASIC(Ptr, void*), + SLANG_RTTI_INFO_BASIC(RefPtr, RefPtr), + SLANG_RTTI_INFO_INVALID(FixedArray), + SLANG_RTTI_INFO_INVALID(Struct), + SLANG_RTTI_INFO_INVALID(Other), + SLANG_RTTI_INFO_INVALID(Enum), + SLANG_RTTI_INFO_INVALID(List), + SLANG_RTTI_INFO_INVALID(Dictionary), +}; + +struct RttiInfoManager +{ + void* allocate(size_t size) + { + std::lock_guard guard(m_mutex); + return m_arena.allocate(size); + } + + static RttiInfoManager& getSingleton() + { + static RttiInfoManager g_manager; + return g_manager; + } + +protected: + RttiInfoManager() : + m_arena(1024) + { + } + + std::recursive_mutex m_mutex; ///< We need a mutex to guard access to m_arena + MemoryArena m_arena; +}; + +/* static */void* RttiInfo::allocate(size_t size) +{ + return RttiInfoManager::getSingleton().allocate(size); +} + +/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! StructRttiBuilder !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ + +static void _appendFixedArray(const FixedArrayRttiInfo* inFixedArray, StringBuilder& out) +{ + List fixedArrays; + fixedArrays.add(inFixedArray); + + const RttiInfo* cur = inFixedArray->m_elementType; + while (cur->m_kind == RttiInfo::Kind::FixedArray) + { + const FixedArrayRttiInfo* curArray = static_cast(cur); + fixedArrays.add(curArray); + cur = curArray->m_elementType; + } + + // Append the 'target' which is in cur + RttiInfo::append(cur, out); + // Now all the fixed array values, in order + for (auto fixedArray : fixedArrays) + { + out << "[" << int32_t(fixedArray->m_elementCount) << "]"; + } +} + +/* static */void RttiInfo::append(const RttiInfo* info, StringBuilder& out) +{ + switch (info->m_kind) + { + case RttiInfo::Kind::I32: out << "int32_t"; break; + case RttiInfo::Kind::U32: out << "uint32_t"; break; + case RttiInfo::Kind::I64: out << "int64_t"; break; + case RttiInfo::Kind::U64: out << "uint64_t"; break; + case RttiInfo::Kind::F32: out << "float"; break; + case RttiInfo::Kind::F64: out << "double"; break; + case RttiInfo::Kind::Bool: out << "bool"; break; + case RttiInfo::Kind::String: out << "String"; break; + case RttiInfo::Kind::UnownedStringSlice: out << "UnownedStringSlice"; break; + case RttiInfo::Kind::Ptr: + { + const PtrRttiInfo* ptrRttiInfo = static_cast(info); + append(ptrRttiInfo->m_targetType, out); + out << "*"; + break; + } + case RttiInfo::Kind::RefPtr: + { + const RefPtrRttiInfo* ptrRttiInfo = static_cast(info); + out << "RefPtr<"; + append(ptrRttiInfo->m_targetType, out); + out << ">"; + break; + } + case RttiInfo::Kind::FixedArray: + { + const FixedArrayRttiInfo* arrayRttiInfo = static_cast(info); + _appendFixedArray(arrayRttiInfo, out); + break; + } + case RttiInfo::Kind::List: + { + const ListRttiInfo* listRttiInfo = static_cast(info); + out << "List<"; + append(listRttiInfo->m_elementType, out); + out << ">"; + break; + } + case RttiInfo::Kind::Dictionary: + { + const DictionaryRttiInfo* dictionaryRttiInfo = static_cast(info); + + out << "Dictionary<"; + append(dictionaryRttiInfo->m_keyType, out); + out << ","; + append(dictionaryRttiInfo->m_valueType, out); + out << ">"; + break; + } + default: + { + if (info->isNamed()) + { + const NamedRttiInfo* namedRttiInfo = static_cast(info); + out << namedRttiInfo->m_name; + break; + } + + out << "%Unknown%"; + break; + } + } +} + +/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! StructRttiBuilder !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ + +void StructRttiBuilder::_init(const char* name, const StructRttiInfo* super, const Byte* base) +{ + m_rttiInfo.m_name = name; + m_rttiInfo.m_super = super; + m_base = base; + + m_rttiInfo.m_fieldCount = 0; + m_rttiInfo.m_fields = nullptr; +} + +StructRttiInfo StructRttiBuilder::make() +{ + const Index fieldCount = m_fields.getCount(); + + if (fieldCount) + { + StructRttiInfo::Field* dstFields = (StructRttiInfo::Field*)RttiInfo::allocate(sizeof(StructRttiInfo::Field) * fieldCount); + ::memcpy(dstFields, m_fields.getBuffer(), sizeof(StructRttiInfo::Field) * fieldCount); + + m_rttiInfo.m_fields = dstFields; + m_rttiInfo.m_fieldCount = fieldCount; + } + + return m_rttiInfo; +} + +} // namespace Slang -- cgit v1.2.3