This repository was archived by the owner on Sep 8, 2023. It is now read-only.
forked from hmenyus/node-calls-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyinterpreter.h
More file actions
57 lines (45 loc) · 1.34 KB
/
pyinterpreter.h
File metadata and controls
57 lines (45 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once
#include <node_api.h>
#include "cpyobject.h"
#include <vector>
#include <string>
#include <mutex>
#include <unordered_map>
#include <iostream>
namespace nodecallspython
{
class GIL
{
PyGILState_STATE m_gstate;
static std::mutex m_mutex;
public:
GIL()
{
m_mutex.lock();
m_gstate = PyGILState_Ensure();
}
~GIL()
{
PyGILState_Release(m_gstate);
m_mutex.unlock();
}
};
class PyInterpreter
{
PyThreadState* m_state;
std::unordered_map<std::string, CPyObject> m_objs;
static std::mutex m_mutex;
static bool m_inited;
public:
PyInterpreter();
~PyInterpreter();
static CPyObject convert(napi_env env, const std::vector<napi_value>& args);
static napi_value convert(napi_env env, PyObject* obj);
std::string import(const std::string& modulename);
std::string create(const std::string& handler, const std::string& name, CPyObject& args);
void release(const std::string& handler);
CPyObject call(const std::string& handler, const std::string& func, CPyObject& args);
CPyObject exec(const std::string& handler, const std::string& code, bool eval);
void addImportPath(const std::string& path);
};
}