diff --git a/3rd/cjson/CMakeLists.txt b/3rd/cjson/CMakeLists.txt new file mode 100644 index 00000000..2cadd692 --- /dev/null +++ b/3rd/cjson/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.10) + +project(cjson) + +set(CMAKE_CXX_STANDARD 17) + +include_directories(${CMAKE_CURRENT_LIST_DIR}/include) +include_directories(${CMAKE_CURRENT_LIST_DIR}/../pocketpy/include) + +set(CMAKE_CXX_FLAGS_RELEASE "-O2") + +add_library( + cjson + ${CMAKE_CURRENT_LIST_DIR}/src/cJSON.cpp +) diff --git a/3rd/cjson/LICENSE b/3rd/cjson/LICENSE new file mode 100644 index 00000000..8b84267d --- /dev/null +++ b/3rd/cjson/LICENSE @@ -0,0 +1,10 @@ +MIT License + +Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Sofware without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/3rd/cjson/include/cjson.hpp b/3rd/cjson/include/cjson.hpp new file mode 100644 index 00000000..e6f87e9d --- /dev/null +++ b/3rd/cjson/include/cjson.hpp @@ -0,0 +1,41 @@ +#include "cjson/cJSON.h" +#include "pocketpy/pocketpy.h" + + +namespace pkpy{ + +inline void add_module_cjson(VM* vm){ + PyObject* mod = vm->new_module("cjson"); + vm->bind_func<1>(mod, "loads", [](VM* vm, ArgsView args) { + + const Str& expr = CAST(Str&, args[0]); + + cJSON *json = cJSON_Parse(expr.c_str()); + /* + Plan: + Create dictionary from cJSON object + Return dictionary + */ + + //Following is an example dictionary that we will return + Dict d(vm); + d.set(py_var(vm, "x"), py_var(vm, 1)); + d.set(py_var(vm, "y"), py_var(vm, "123")); + return VAR(d); + }); + + vm->bind_func<1>(mod, "dumps", [](VM* vm, ArgsView args) { + /* + Plan: + Create cJSON object from dictionary + Return string + */ + const Dict& dict = CAST(Dict&, args[0]); + + char* out = "This will be created from the dictionary"; + return VAR(Str(out)); + + }); +} + +} // namespace pkpy diff --git a/include/pocketpy/cJSON.h b/3rd/cjson/include/cjson/cJSON.h similarity index 100% rename from include/pocketpy/cJSON.h rename to 3rd/cjson/include/cjson/cJSON.h diff --git a/include/pocketpy/cJSON_c.h b/3rd/cjson/src/cJSON.cpp similarity index 99% rename from include/pocketpy/cJSON_c.h rename to 3rd/cjson/src/cJSON.cpp index f6dd11c5..ce271649 100644 --- a/include/pocketpy/cJSON_c.h +++ b/3rd/cjson/src/cJSON.cpp @@ -56,7 +56,7 @@ #pragma GCC visibility pop #endif -#include "cJSON.h" +#include "cjson.hpp" /* define our own boolean type */ #ifdef true diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f05b5b5..dde0843e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,9 @@ if(PK_USE_BOX2D) add_definitions(-DPK_USE_BOX2D) endif() +add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/3rd/cjson) +include_directories(${CMAKE_CURRENT_LIST_DIR}/3rd/cjson/include) + # PK_IS_MAIN determines whether the project is being used from root # or if it is added as a dependency (through add_subdirectory for example). if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") @@ -84,6 +87,8 @@ if(PK_USE_BOX2D) target_link_libraries(${PROJECT_NAME} box2d) endif() +target_link_libraries(${PROJECT_NAME} cjson) + if(PK_EXPORT_CXX_SYMBOLS AND MSVC) set_target_properties(${PROJECT_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) endif() \ No newline at end of file diff --git a/compile_flags.txt b/compile_flags.txt index aa762e54..b1325542 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -4,4 +4,5 @@ -std=c++17 -stdlib=libc++ -Iinclude/ --I3rd/box2d/include/ \ No newline at end of file +-I3rd/box2d/include/ +-I3rd/cjson/include/ diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index addefe20..81498d21 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -27,5 +27,4 @@ void add_module_math(VM* vm); void add_module_dis(VM* vm); void add_module_traceback(VM* vm); void add_module_gc(VM* vm); -void add_module_cjson(VM* vm); } // namespace pkpy \ No newline at end of file diff --git a/src/cjson.cpp b/src/cjson.cpp deleted file mode 100644 index 7b2c6617..00000000 --- a/src/cjson.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "pocketpy/cffi.h" -#include "pocketpy/cJSON_c.h" - - -namespace pkpy{ - -void add_module_cjson(VM* vm){ - PyObject* mod = vm->new_module("cjson"); - vm->bind_func<1>(mod, "loads", [](VM* vm, ArgsView args) { - const Str& expr = CAST(Str&, args[0]); - - cJSON *json = cJSON_Parse(expr.c_str()); - char *json_str = cJSON_Print(json); - - return vm->eval(json_str); - }); - - vm->bind_func<1>(mod, "dumps", [](VM* vm, ArgsView args) { - return vm->py_json(args[0]); - - }); -} - -} // namespace pkpy diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 8625af26..4be2a2e9 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -8,6 +8,7 @@ #define WIN32_LEAN_AND_MEAN #include #endif +#include "cjson.hpp" namespace pkpy{