|
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"); |
81 | 5 | }
|
82 | 6 |
|
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; |
85 | 11 | }
|
86 | 12 |
|
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); |
0 commit comments