summaryrefslogtreecommitdiff
path: root/source/compiler-core/slang-json-rpc.h
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2021-11-23 16:23:15 -0500
committerGitHub <noreply@github.com>2021-11-23 16:23:15 -0500
commit9e084ffab37c276d40931a58633041a2e10de623 (patch)
treed85aa490968cdd3fe4bbcf305b593c6b86587685 /source/compiler-core/slang-json-rpc.h
parentfd46034bf2de59b8ad51743e62b26359678432f7 (diff)
JSON-RPC test server (#2026)
* #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. * Fix some bugs in JSON parsing/handling. Make Rtti work copy/dtor/ctor struct types. * Testing JSON<->native with fixed array. Make makeArrayView explicit if it's just a single value. Added array type. * Fix getting arrayView. * Improve JSON diagnostic name. * First pass refactor using Rtti for JSON RPC. * First pass of test server using RTTI/JSON-RPC. * Added JSONRPCConnection. * Fix some naming issues. * First pass of test-server working. * Added unit test support for JSON-RPC test server. * Fix compilation issues on linux around template handling. * Typo fix. * Fix a bug around SourceLoc lookup with JSONContainer. * Set the console type to console for ISlangWriters. * Small improvements to test-server. * Small improvements in test-server. * Small fix.
Diffstat (limited to 'source/compiler-core/slang-json-rpc.h')
-rw-r--r--source/compiler-core/slang-json-rpc.h151
1 files changed, 80 insertions, 71 deletions
diff --git a/source/compiler-core/slang-json-rpc.h b/source/compiler-core/slang-json-rpc.h
index e85664ceb..f32513b02 100644
--- a/source/compiler-core/slang-json-rpc.h
+++ b/source/compiler-core/slang-json-rpc.h
@@ -12,115 +12,124 @@
namespace Slang {
+/// Struct to hold values associated with JSON-RPC
+struct JSONRPC
+{
+ enum class ErrorCode
+ {
+ ParseError = -32700, ///< Invalid JSON was received by the server.
+ InvalidRequest = -32600, ///< The JSON sent is not a valid Request object.
+ MethodNotFound = -32601, ///< The method does not exist / is not available.
+ InvalidParams = -32602, ///< Invalid method parameter(s).
+ InternalError = -32603, ///< Internal JSON - RPC error.
+
+ ServerImplStart = -32000, ///< Server implementation defined error range
+ ServerImplEnd = -32099,
+ };
+
+ static bool isIdOk(const JSONValue& value)
+ {
+ auto kind = value.getKind();
+ switch (kind)
+ {
+ case JSONValue::Kind::Integer:
+ case JSONValue::Kind::Invalid:
+ case JSONValue::Kind::String:
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ static const UnownedStringSlice jsonRpc;
+ static const UnownedStringSlice jsonRpcVersion;
+ static const UnownedStringSlice id;
+};
+
struct JSONRPCErrorResponse
{
struct Error
{
- Index code = 0; ///< Value from ErrorCode
- UnownedStringSlice message; ///< Error message
+ bool isValid() const { return code != 0; }
+ Int code = 0; ///< Value from ErrorCode
+ UnownedStringSlice message; ///< Error message
+
static const StructRttiInfo g_rttiInfo;
};
+ bool isValid() const { return jsonrpc == JSONRPC::jsonRpcVersion && error.isValid() && JSONRPC::isIdOk(id); }
+
+ UnownedStringSlice jsonrpc = JSONRPC::jsonRpcVersion;
Error error;
- JSONValue data;
- Int id = -1; ///< Id of initiating method or -1 if not set
+ JSONValue data; ///< Optional data describing the errro
+ JSONValue id; ///< Id associated with this request
static const StructRttiInfo g_rttiInfo;
};
struct JSONRPCCall
{
+ bool isValid() const { return method.getLength() > 0 && jsonrpc == JSONRPC::jsonRpcVersion && JSONRPC::isIdOk(id); }
+
+ UnownedStringSlice jsonrpc = JSONRPC::jsonRpcVersion;
UnownedStringSlice method; ///< The name of the method
JSONValue params; ///< Can be invalid/array/object
- Int id = -1; ///< Id associated with this request, or -1 if not set
+ JSONValue id; ///< Id associated with this request
static const StructRttiInfo g_rttiInfo;
};
struct JSONResultResponse
{
- JSONValue result; ///< The result value
- Int id = -1; ///< Id of initiating method or -1 if not set
+ bool isValid() const { return jsonrpc == JSONRPC::jsonRpcVersion && JSONRPC::isIdOk(id); }
+
+ UnownedStringSlice jsonrpc = JSONRPC::jsonRpcVersion;
+ JSONValue result; ///< The result value
+ JSONValue id; ///< Id associated with this request
static const StructRttiInfo g_rttiInfo;
};
+enum class JSONRPCMessageType
+{
+ Invalid,
+ Result,
+ Call,
+ Error,
+ CountOf,
+};
+
/// Send and receive messages as JSON
-///
-/// Strictly speaking should support ids, as strings or ids. Currently just supports with integer ids.
-/// One way of dealing with this would be to just use JSONValue for ids, would allow invalid/string/integer and
-/// a mechanism to compare/display etc.
class JSONRPCUtil
{
public:
- enum class ErrorCode
- {
- ParseError = -32700, ///< Invalid JSON was received by the server.
- InvalidRequest = -32600, ///< The JSON sent is not a valid Request object.
- MethodNotFound = -32601, ///< The method does not exist / is not available.
- InvalidParams = -32602, ///< Invalid method parameter(s).
- InternalError = -32603, ///< Internal JSON - RPC error.
-
- ServerImplStart = -32000, ///< Server implementation defined error range
- ServerImplEnd = -32099,
- };
-
- enum class ResponseType
- {
- Invalid,
- Error,
- Result
- };
-
- struct ErrorResponse
- {
- Index code = 0; ///< Value from ErrorCode
- UnownedStringSlice message; ///< Error message
- JSONValue data;
- Int id = -1; ///< Id of initiating method or -1 if not set
- };
-
- struct ResultResponse
- {
- JSONValue result; ///< The result value
- Int id = -1; ///< Id of initiating method or -1 if not set
- };
-
- struct Call
- {
- UnownedStringSlice method; ///< The name of the method
- JSONValue params; ///< Can be invalid/array/object
- Int id = -1; ///< Id associated with this request, or -1 if not set
- };
-
- /// Parameters can be either named or via index.
- static JSONValue createCall(JSONContainer* container, const UnownedStringSlice& method, JSONValue params, Int id = -1);
- /// Parameters can be either named or via index.
- static JSONValue createCall(JSONContainer* container, const UnownedStringSlice& method, Int id = -1);
-
- /// Create an error response
- /// Code should typically be something in the ErrorCode range
- static JSONValue createErrorResponse(JSONContainer* container, Index code, const UnownedStringSlice& message, const JSONValue& data = JSONValue(), Int id = -1);
- static JSONValue createErrorResponse(JSONContainer* container, ErrorCode code, const UnownedStringSlice& message, const JSONValue& data = JSONValue(), Int id = -1);
- /// Create a result response
- static JSONValue createResultResponse(JSONContainer* container, const JSONValue& resultValue, Int id = -1);
-
/// Determine the response type
- static ResponseType getResponseType(JSONContainer* container, const JSONValue& response);
+ static JSONRPCMessageType getMessageType(JSONContainer* container, const JSONValue& value);
- static SlangResult parseError(JSONContainer* container, const JSONValue& response, ErrorResponse& out);
+ /// Parse slice into JSONContainer. outValue is the root of the hierarchy.
+ /// NOTE! Uses and *assumes* there is a source manager on the sink. outValue is likely only usable whilst the sourceManger is in scope
+ /// The sourceLoc can only be interpretted with the sourceLoc anyway
+ static SlangResult parseJSON(const UnownedStringSlice& slice, JSONContainer* container, DiagnosticSink* sink, JSONValue& outValue);
- static SlangResult parseResult(JSONContainer* container, const JSONValue& response, ResultResponse& out);
+ /// Convert value into out
+ static SlangResult convertToNative(JSONContainer* container, const JSONValue& value, DiagnosticSink* sink, const RttiInfo* rttiInfo, void* out);
+ template <typename T>
+ static SlangResult convertToNative(JSONContainer* container, const JSONValue& value, DiagnosticSink* sink, T& out) { return convertToNative(container, value, sink, GetRttiInfo<T>::get(), (void*)&out); }
- static SlangResult parseCall(JSONContainer* container, const JSONValue& value, Call& out);
+ /// Convert to JSON
+ static SlangResult convertToJSON(const RttiInfo* rttiInfo, const void* in, DiagnosticSink* sink, StringBuilder& out);
- /// Parse slice into JSONContainer. outValue is the root of the hierarchy.
- static SlangResult parseJSON(const UnownedStringSlice& slice, JSONContainer* container, DiagnosticSink* sink, JSONValue& outValue);
+ template <typename T>
+ static SlangResult convertToJSON(const T* in, DiagnosticSink* sink, StringBuilder& out)
+ {
+ return convertToJSON(GetRttiInfo<T>::get(), (const void*)in, sink, out);
+ }
- /// Parse content from stream, and consume the packet
- static SlangResult parseJSONAndConsume(HTTPPacketConnection* connection, JSONContainer* container, DiagnosticSink* sink, JSONValue& outValue);
+ /// Get an id directly from root (assumed id: is in root object definition).
+ static JSONValue getId(JSONContainer* container, const JSONValue& root);
};
} // namespace Slang