Create structure for cjson module

This commit is contained in:
Mahbub Alam 2023-10-06 17:07:54 -04:00
parent 072de45ae3
commit 6159165a2e
10 changed files with 75 additions and 27 deletions

15
3rd/cjson/CMakeLists.txt Normal file
View File

@ -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
)

10
3rd/cjson/LICENSE Normal file
View File

@ -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.

View File

@ -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

View File

@ -56,7 +56,7 @@
#pragma GCC visibility pop #pragma GCC visibility pop
#endif #endif
#include "cJSON.h" #include "cjson.hpp"
/* define our own boolean type */ /* define our own boolean type */
#ifdef true #ifdef true

View File

@ -49,6 +49,9 @@ if(PK_USE_BOX2D)
add_definitions(-DPK_USE_BOX2D) add_definitions(-DPK_USE_BOX2D)
endif() 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 # 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). # or if it is added as a dependency (through add_subdirectory for example).
if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
@ -84,6 +87,8 @@ if(PK_USE_BOX2D)
target_link_libraries(${PROJECT_NAME} box2d) target_link_libraries(${PROJECT_NAME} box2d)
endif() endif()
target_link_libraries(${PROJECT_NAME} cjson)
if(PK_EXPORT_CXX_SYMBOLS AND MSVC) if(PK_EXPORT_CXX_SYMBOLS AND MSVC)
set_target_properties(${PROJECT_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) set_target_properties(${PROJECT_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif() endif()

View File

@ -5,3 +5,4 @@
-stdlib=libc++ -stdlib=libc++
-Iinclude/ -Iinclude/
-I3rd/box2d/include/ -I3rd/box2d/include/
-I3rd/cjson/include/

View File

@ -27,5 +27,4 @@ void add_module_math(VM* vm);
void add_module_dis(VM* vm); void add_module_dis(VM* vm);
void add_module_traceback(VM* vm); void add_module_traceback(VM* vm);
void add_module_gc(VM* vm); void add_module_gc(VM* vm);
void add_module_cjson(VM* vm);
} // namespace pkpy } // namespace pkpy

View File

@ -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

View File

@ -8,6 +8,7 @@
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
#endif #endif
#include "cjson.hpp"
namespace pkpy{ namespace pkpy{