# HG changeset patch # User Alain Mazy # Date 1558026638 -7200 # Node ID f185cfcb72a04f5bce49c8841491a8609b4bc4ac # Parent 8c0b073efda8b81d7d49a05d0e22b0ba4ead7e80 CodeGen: tests improvements diff -r 8c0b073efda8 -r f185cfcb72a0 Resources/CodeGeneration/template.in.h.j2 --- a/Resources/CodeGeneration/template.in.h.j2 Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/template.in.h.j2 Thu May 16 19:10:38 2019 +0200 @@ -182,6 +182,17 @@ return out; } + inline std::string ToString(const std::string& str) + { + return str; + } + + inline void FromString(std::string& value, std::string strValue) + { + value = strValue; + } + + /** Throws in case of problem */ template void _StoneDeserializeValue( @@ -195,8 +206,10 @@ itr != jsonValue.end(); itr++) { + std::string strKey; + _StoneDeserializeValue(strKey, itr.key()); TK key; - _StoneDeserializeValue(key, itr.key()); + FromString(key, strKey); // if you have a compile error here, it means that your type is not suitable to be the key of a map (or you should overwrite the FromString/ToString in template.in.h.j2) TV innerDestValue; _StoneDeserializeValue(innerDestValue, *itr); @@ -205,6 +218,7 @@ } } } + template Json::Value _StoneSerializeValue(const std::map& value) { @@ -214,7 +228,7 @@ it != value.cend(); ++it) { // it->first it->second - result[it->first] = _StoneSerializeValue(it->second); + result[ToString(it->first)] = _StoneSerializeValue(it->second); } return result; } @@ -239,7 +253,7 @@ void _StoneDeserializeValue( std::vector& destValue, const Json::Value& jsonValue) { - if (!jsonValue.isNull()) + if (!jsonValue.isNull() && jsonValue.isArray()) { destValue.clear(); destValue.reserve(jsonValue.size()); @@ -281,7 +295,7 @@ void _StoneDeserializeValue( std::set& destValue, const Json::Value& jsonValue) { - if (!jsonValue.isNull()) + if (!jsonValue.isNull() && jsonValue.isArray()) { destValue.clear(); for (Json::Value::ArrayIndex i = 0; i != jsonValue.size(); i++) diff -r 8c0b073efda8 -r f185cfcb72a0 Resources/CodeGeneration/testWasmIntegrated/CMakeLists.txt --- a/Resources/CodeGeneration/testWasmIntegrated/CMakeLists.txt Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/CMakeLists.txt Thu May 16 19:10:38 2019 +0200 @@ -21,14 +21,14 @@ set(jsoncppRootDir ${CMAKE_CURRENT_LIST_DIR}/jsoncpp-1.8.4) add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/testWasmIntegratedCpp_generated.hpp ${CMAKE_CURRENT_BINARY_DIR}/testWasmIntegratedCpp_generated.ts + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/TestStoneCodeGen_generated.hpp ${CMAKE_CURRENT_BINARY_DIR}/TestStoneCodeGen_generated.ts COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/../stonegentool.py -o ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/../test_data/testTestStoneCodeGen.yaml DEPENDS ${testCppHandler_Codegen_Deps} ${CMAKE_CURRENT_LIST_DIR}/../test_data/testTestStoneCodeGen.yaml ) add_executable(testWasmIntegratedCpp main.cpp - ${CMAKE_CURRENT_BINARY_DIR}/testWasmIntegratedCpp_generated.hpp + ${CMAKE_CURRENT_BINARY_DIR}/TestStoneCodeGen_generated.hpp ${jsoncppRootDir}/jsoncpp.cpp ${testCppHandler_Codegen_Deps}) diff -r 8c0b073efda8 -r f185cfcb72a0 Resources/CodeGeneration/testWasmIntegrated/DefaultLibrary.js --- a/Resources/CodeGeneration/testWasmIntegrated/DefaultLibrary.js Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/DefaultLibrary.js Thu May 16 19:10:38 2019 +0200 @@ -9,7 +9,7 @@ }, SendMessageFromCppJS: function(statusUpdateMessage) { var statusUpdateMessage_ = UTF8ToString(statusUpdateMessage); - SendMessageFromCpp(statusUpdateMessage_); + window.SendMessageFromCpp(statusUpdateMessage_); } }); diff -r 8c0b073efda8 -r f185cfcb72a0 Resources/CodeGeneration/testWasmIntegrated/build-web.sh --- a/Resources/CodeGeneration/testWasmIntegrated/build-web.sh Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/build-web.sh Thu May 16 19:10:38 2019 +0200 @@ -4,7 +4,7 @@ mkdir -p build-final # compile TS to JS -tsc --module commonjs --sourceMap -t ES2015 --outDir "build-tsc/" build-wasm/testWasmIntegratedCpp_generated.ts testWasmIntegrated.ts +tsc --module commonjs --sourceMap -t ES2015 --outDir "build-tsc/" build-wasm/TestStoneCodeGen_generated.ts testWasmIntegrated.ts # bundle JS files to final build dir browserify "build-tsc/build-wasm/testWasmIntegratedCpp_generated.js" "build-tsc/testWasmIntegrated.js" -o "build-final/testWasmIntegratedApp.js" diff -r 8c0b073efda8 -r f185cfcb72a0 Resources/CodeGeneration/testWasmIntegrated/main.cpp --- a/Resources/CodeGeneration/testWasmIntegrated/main.cpp Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/main.cpp Thu May 16 19:10:38 2019 +0200 @@ -59,6 +59,15 @@ SendFreeTextFromCppJS(ss.str().c_str()); \ return true; +#define ECHO_MESSAGE(Type,value) \ + stringstream ss; \ + ss << "Received an instance of:\n" #Type "\n. Here's the dump:\n"; \ + TestStoneCodeGen::StoneDumpValue(ss, value, 0); \ + SendFreeTextFromCppJS(ss.str().c_str()); \ + std::string serializedInCpp = StoneSerialize(value); \ + SendMessageFromCppJS(serializedInCpp.c_str()); \ + return true; + class MyHandler : public TestStoneCodeGen::IHandler { public: @@ -87,6 +96,34 @@ } }; +class MyEchoHandler : public TestStoneCodeGen::IHandler +{ + public: + virtual bool Handle(const TestStoneCodeGen::A& value) override + { + ECHO_MESSAGE(TestStoneCodeGen::A,value) + } + virtual bool Handle(const TestStoneCodeGen::B& value) override + { + ECHO_MESSAGE(TestStoneCodeGen::B,value) + } + + virtual bool Handle(const TestStoneCodeGen::Message1& value) override + { + ECHO_MESSAGE(TestStoneCodeGen::Message1,value) + } + + virtual bool Handle(const TestStoneCodeGen::Message2& value) override + { + ECHO_MESSAGE(TestStoneCodeGen::Message2,value) + } + + virtual bool Handle(const TestStoneCodeGen::C& value) override + { + ECHO_MESSAGE(TestStoneCodeGen::C,value) + } +}; + extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCpp(const char* message) { MyHandler handler; @@ -106,6 +143,25 @@ } } +extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCppForEcho(const char* message) +{ + MyEchoHandler echoHandler; + try + { + bool handled = TestStoneCodeGen::StoneDispatchToHandler(message,&echoHandler); + if(!handled) + { + SendFreeTextFromCppJS("This message is valid JSON, but was not handled by the echo handler!"); + } + } + catch(std::exception& e) + { + stringstream ss; + ss << "Error while parsing message: " << e.what() << "\n"; + SendFreeTextFromCppJS(ss.str().c_str()); + } +} + void EMSCRIPTEN_KEEPALIVE StartWasmApplication(const char* baseUri) { printf("Hello! (this is sent from C++)\n"); diff -r 8c0b073efda8 -r f185cfcb72a0 Resources/CodeGeneration/testWasmIntegrated/styles.css --- a/Resources/CodeGeneration/testWasmIntegrated/styles.css Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/styles.css Thu May 16 19:10:38 2019 +0200 @@ -9,7 +9,7 @@ "Test1 Test2 Test3 Test4 . . ." ". . . . . . ." "Test5 Test6 Test7 Test8 . . ." - ". . . . . . ." + "TestTsCppTs . . . . . ." ". . . . . . ." ; height: 480px; @@ -59,6 +59,8 @@ .TestWasm-Test8 { grid-area: Test8; } +.TestWasm-ts-cpp-ts { grid-area: TestTsCppTs; } + .TestWasm-button { width:80px; } diff -r 8c0b073efda8 -r f185cfcb72a0 Resources/CodeGeneration/testWasmIntegrated/testWasmIntegrated.html --- a/Resources/CodeGeneration/testWasmIntegrated/testWasmIntegrated.html Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/testWasmIntegrated.html Thu May 16 19:10:38 2019 +0200 @@ -54,6 +54,9 @@
+
+ +