├── impy.py ├── index.js ├── README.md ├── package.json ├── CMakeLists.txt └── binding.cc /impy.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | return "I'm impy.main" 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const addon = require("bindings")("pythonMethod"); 2 | 3 | const str = addon.pythonMethod(); 4 | 5 | console.log(str); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pcn 2 | > Python to C/C++ to Node 3 | 4 | Ref: https://aerocode.net/341 5 | 6 | 7 | # How? 8 | 9 | ## Deps 10 | ```sh 11 | npm install -g node-gyp 12 | npm install -g cmake-js 13 | 14 | npm init -y 15 | npm install bindings 16 | npm install node-addon-api 17 | ``` 18 | 19 | ## Compile 20 | ```sh 21 | cmake-js compile 22 | ``` 23 | 24 | 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ncp", 3 | "version": "1.0.0", 4 | "description": "Node-C/C++-Python", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/SaidBySolo/ncp.git" 12 | }, 13 | "keywords": [], 14 | "author": "", 15 | "license": "ISC", 16 | "bugs": { 17 | "url": "https://github.com/SaidBySolo/ncp/issues" 18 | }, 19 | "homepage": "https://github.com/SaidBySolo/ncp#readme", 20 | "dependencies": { 21 | "bindings": "^1.5.0", 22 | "node-addon-api": "^4.0.0" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # https://aerocode.net/341 2 | 3 | cmake_minimum_required(VERSION 2.8.12.2) 4 | set (CMAKE_CXX_STANDARD 11) 5 | 6 | # 프로젝트 설정 7 | project (pythonMethod) 8 | include_directories(${CMAKE_JS_INC}) 9 | 10 | 11 | # C파일 등등 성정 12 | file(GLOB SOURCE_FILES "binding.cc") 13 | add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC}) 14 | set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node") 15 | target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB}) 16 | 17 | # 애드온 설정 18 | execute_process(COMMAND node -p "require('node-addon-api').include" 19 | WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} 20 | OUTPUT_VARIABLE NODE_ADDON_API_DIR 21 | ) 22 | string(REPLACE "\n" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR}) 23 | string(REPLACE "\"" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR}) 24 | target_include_directories(${PROJECT_NAME} PRIVATE ${NODE_ADDON_API_DIR}) 25 | 26 | # 파이썬 설정 27 | find_package(PythonLibs REQUIRED) 28 | include_directories(${PYTHON_INCLUDE_DIRS}) 29 | target_link_libraries(${PROJECT_NAME} ${PYTHON_LIBRARIES}) 30 | target_include_directories(${PROJECT_NAME} PRIVATE ${PYTHON_INCLUDE_DIRS}) 31 | 32 | # 애드온 버전? 33 | add_definitions(-DNAPI_VERSION=3) -------------------------------------------------------------------------------- /binding.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // 파이썬 바인딩 5 | std::string py() 6 | { 7 | 8 | PyObject *pName, *pModule, *pDict, *pFunc, *presult; 9 | 10 | Py_Initialize(); 11 | 12 | // c실행파일 기준 path append 13 | PyRun_SimpleString("import sys, os"); 14 | PyRun_SimpleString("sys.path.append(os.getcwd())"); 15 | // 모듈이름 16 | pName = PyUnicode_FromString("impy"); 17 | // 임포트 18 | pModule = PyImport_Import(pName); 19 | 20 | pDict = PyModule_GetDict(pModule); 21 | 22 | pFunc = PyObject_GetAttrString(pModule, (char *)"main"); 23 | 24 | presult = PyObject_CallObject(pFunc, NULL); 25 | 26 | Py_DECREF(pName); 27 | Py_DECREF(pModule); 28 | Py_FinalizeEx(); 29 | 30 | return PyUnicode_AsUTF8(presult); 31 | } 32 | 33 | //바인딩 34 | Napi::String pythonToJSString(const Napi::CallbackInfo &info) 35 | { 36 | Napi::Env env = info.Env(); 37 | return Napi::String::New(env, py()); 38 | } 39 | 40 | // export 지정 41 | Napi::Object init(Napi::Env env, Napi::Object exports) 42 | { 43 | exports.Set(Napi::String::New(env, "pythonMethod"), Napi::Function::New(env, pythonToJSString)); 44 | return exports; 45 | }; 46 | 47 | NODE_API_MODULE(pythonBinding, init); --------------------------------------------------------------------------------