Skip to content

Commit d82e8e9

Browse files
committed
napi wip
1 parent 18a3de3 commit d82e8e9

File tree

7 files changed

+336
-94
lines changed

7 files changed

+336
-94
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ cmake_install.cmake
55
CMakeFiles
66
vader_sentiment_cpython
77
node_modules
8-
build
8+
build
9+
prebuilds

binding.cc

+98-83
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,102 @@
1-
#include <node.h>
2-
#include <v8.h>
3-
4-
// TODO: handle errors, add Py_DECREFs
5-
6-
#include <Python.h>
7-
#include <string>
8-
#include <boost/dll.hpp>
9-
10-
const char* executableFolder() {
11-
return (new std::string(boost::dll::program_location().parent_path().string()))->c_str();
12-
}
13-
14-
double MainFunc(const char *arg) {
15-
PyObject *pName, *importlib, *importlib__import_module, *vaderSentiment, *pFunc;
16-
PyObject *pArgs, *pArgs2, *pModule2, *importlib__import_module__args, *analyser, *result;
17-
18-
Py_Initialize();
19-
pName = PyUnicode_DecodeFSDefault("importlib");
20-
21-
importlib = PyImport_Import(pName);
22-
// Py_DECREF(pName);
23-
24-
pModule2 = PyImport_Import(PyUnicode_DecodeFSDefault("sys"));
25-
26-
pArgs2 = PyTuple_New(2);
27-
PyTuple_SET_ITEM(pArgs2, 0, PyLong_FromLong(0));
28-
PyTuple_SET_ITEM(pArgs2, 1, PyUnicode_DecodeFSDefault(executableFolder()));
29-
30-
PyObject_CallObject(PyObject_GetAttrString(PyObject_GetAttrString(pModule2, "path"), "insert"), pArgs2);
31-
32-
if (importlib != NULL) {
33-
importlib__import_module = PyObject_GetAttrString(importlib, "import_module");
34-
35-
importlib__import_module__args = PyTuple_New(1);
36-
PyTuple_SET_ITEM(importlib__import_module__args, 0, PyUnicode_DecodeFSDefault("vaderSentiment-master.vaderSentiment.vaderSentiment"));
37-
vaderSentiment = PyObject_CallObject(importlib__import_module, importlib__import_module__args);
38-
39-
pFunc = PyObject_GetAttrString(vaderSentiment, "SentimentIntensityAnalyzer");
40-
// /* pFunc is a new reference */
41-
42-
if (pFunc && PyCallable_Check(pFunc)) {
43-
pArgs = PyTuple_New(0);
44-
analyser = PyObject_CallObject(pFunc, pArgs);
45-
result = PyObject_CallMethod(analyser, "polarity_scores", "(s)", arg);
46-
// Py_DECREF(pArgs);
47-
if (result != NULL) {
48-
printf("Result of call: %f\n", PyFloat_AsDouble(PyDict_GetItemString(result, "compound")));
49-
// Py_DECREF(result);
50-
return PyFloat_AsDouble(PyDict_GetItemString(result, "compound"));
51-
} else {
52-
// Py_DECREF(pFunc);
53-
// Py_DECREF(vaderSentiment);
54-
PyErr_Print();
55-
fprintf(stderr, "Call failed\n");
56-
return 1;
57-
}
58-
} else {
59-
if (PyErr_Occurred())
60-
PyErr_Print();
61-
fprintf(stderr, "Cannot find function \"%s\"\n", "SentimentIntensityAnalyzer");
62-
}
63-
Py_XDECREF(pFunc);
64-
Py_DECREF(vaderSentiment);
65-
} else {
66-
PyErr_Print();
67-
fprintf(stderr, "Failed to load \"%s\"\n", "importlib");
68-
return 1;
69-
}
70-
if (Py_FinalizeEx() < 0) {
71-
return 120;
72-
}
73-
return 0;
74-
}
75-
76-
void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
77-
v8::Isolate* isolate = args.GetIsolate();
78-
const char* data = "Woohooo 😍 ✌️";
79-
args.GetReturnValue().Set(v8::String::NewFromUtf8(
80-
isolate, std::to_string(MainFunc(data)).c_str()).ToLocalChecked());
1+
#include "napi.h"
2+
Napi::String Method(const Napi::CallbackInfo& info) {
3+
Napi::Env env = info.Env();
4+
return Napi::String::New(env, "world");
815
}
826

83-
void init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
84-
NODE_SET_METHOD(module, "exports", Method);
7+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
8+
exports.Set(Napi::String::New(env, "hello"),
9+
Napi::Function::New(env, Method));
10+
return exports;
8511
}
8612

87-
NODE_MODULE(NODE_GYP_MODULE_NAME, init);
13+
NODE_API_MODULE(hello, Init)
14+
15+
16+
// #include <node.h>
17+
// #include <v8.h>
18+
19+
// // TODO: handle errors, add Py_DECREFs
20+
21+
// #include <Python.h>
22+
// #include <string>
23+
// #include <boost/dll.hpp>
24+
25+
// const char* executableFolder() {
26+
// return (new std::string(boost::dll::program_location().parent_path().string()))->c_str();
27+
// }
28+
29+
// double MainFunc(const char *arg) {
30+
// PyObject *pName, *importlib, *importlib__import_module, *vaderSentiment, *pFunc;
31+
// PyObject *pArgs, *pArgs2, *pModule2, *importlib__import_module__args, *analyser, *result;
32+
33+
// Py_Initialize();
34+
// pName = PyUnicode_DecodeFSDefault("importlib");
35+
36+
// importlib = PyImport_Import(pName);
37+
// // Py_DECREF(pName);
38+
39+
// pModule2 = PyImport_Import(PyUnicode_DecodeFSDefault("sys"));
40+
41+
// pArgs2 = PyTuple_New(2);
42+
// PyTuple_SET_ITEM(pArgs2, 0, PyLong_FromLong(0));
43+
// PyTuple_SET_ITEM(pArgs2, 1, PyUnicode_DecodeFSDefault(executableFolder()));
44+
45+
// PyObject_CallObject(PyObject_GetAttrString(PyObject_GetAttrString(pModule2, "path"), "insert"), pArgs2);
46+
47+
// if (importlib != NULL) {
48+
// importlib__import_module = PyObject_GetAttrString(importlib, "import_module");
49+
50+
// importlib__import_module__args = PyTuple_New(1);
51+
// PyTuple_SET_ITEM(importlib__import_module__args, 0, PyUnicode_DecodeFSDefault("vaderSentiment-master.vaderSentiment.vaderSentiment"));
52+
// vaderSentiment = PyObject_CallObject(importlib__import_module, importlib__import_module__args);
53+
54+
// pFunc = PyObject_GetAttrString(vaderSentiment, "SentimentIntensityAnalyzer");
55+
// // /* pFunc is a new reference */
56+
57+
// if (pFunc && PyCallable_Check(pFunc)) {
58+
// pArgs = PyTuple_New(0);
59+
// analyser = PyObject_CallObject(pFunc, pArgs);
60+
// result = PyObject_CallMethod(analyser, "polarity_scores", "(s)", arg);
61+
// // Py_DECREF(pArgs);
62+
// if (result != NULL) {
63+
// printf("Result of call: %f\n", PyFloat_AsDouble(PyDict_GetItemString(result, "compound")));
64+
// // Py_DECREF(result);
65+
// return PyFloat_AsDouble(PyDict_GetItemString(result, "compound"));
66+
// } else {
67+
// // Py_DECREF(pFunc);
68+
// // Py_DECREF(vaderSentiment);
69+
// PyErr_Print();
70+
// fprintf(stderr, "Call failed\n");
71+
// return 1;
72+
// }
73+
// } else {
74+
// if (PyErr_Occurred())
75+
// PyErr_Print();
76+
// fprintf(stderr, "Cannot find function \"%s\"\n", "SentimentIntensityAnalyzer");
77+
// }
78+
// Py_XDECREF(pFunc);
79+
// Py_DECREF(vaderSentiment);
80+
// } else {
81+
// PyErr_Print();
82+
// fprintf(stderr, "Failed to load \"%s\"\n", "importlib");
83+
// return 1;
84+
// }
85+
// if (Py_FinalizeEx() < 0) {
86+
// return 120;
87+
// }
88+
// return 0;
89+
// }
90+
91+
// void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
92+
// v8::Isolate* isolate = args.GetIsolate();
93+
// const char* data = "Woohooo 😍 ✌️";
94+
// args.GetReturnValue().Set(v8::String::NewFromUtf8(
95+
// isolate, std::to_string(MainFunc(data)).c_str()).ToLocalChecked());
96+
// }
97+
98+
// void init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
99+
// NODE_SET_METHOD(module, "exports", Method);
100+
// }
101+
102+
// NODE_MODULE(NODE_GYP_MODULE_NAME, init);

binding.gyp

+21-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,28 @@
33
{
44
'target_name': 'binding',
55
'sources': ['binding.cc'],
6-
'include_dirs': ["/usr/include/python3.8"],
7-
"cflags!": [ "-fno-exceptions" ],
8-
"cflags": [ "-fPIC", "-DBOOST_ALL_NO_LIB" ],
9-
"cflags_cc": [ "-fPIC", "-DBOOST_ALL_NO_LIB" ],
10-
"cflags_cc!": [ "-fno-exceptions" ],
6+
'include_dirs': ["/usr/include/python3.8", "<!(node -p \"require('node-addon-api').include_dir\")"],
7+
'cflags!': ['-fno-exceptions'],
8+
'cflags_cc!': ['-fno-exceptions'],
9+
"cflags": ["-fPIC", "-DBOOST_ALL_NO_LIB"],
10+
"cflags_cc": ["-fPIC", "-DBOOST_ALL_NO_LIB"],
11+
'xcode_settings': {
12+
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
13+
'CLANG_CXX_LIBRARY': 'libc++',
14+
'MACOSX_DEPLOYMENT_TARGET': '10.7',
15+
},
16+
'msvs_settings': {
17+
'VCCLCompilerTool': {'ExceptionHandling': 1},
18+
},
1119
"libraries": ["/usr/lib/x86_64-linux-gnu/libpython3.8.so", "/usr/lib/x86_64-linux-gnu/libboost_filesystem.so", "/usr/lib/x86_64-linux-gnu/libboost_system.so"]
1220
}
21+
],
22+
'conditions': [
23+
['OS=="mac"', {
24+
'cflags+': ['-fvisibility=hidden'],
25+
'xcode_settings': {
26+
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
27+
}
28+
}]
1329
]
1430
}

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
module.exports = require("node-gyp-build")(__dirname);

0 commit comments

Comments
 (0)