-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
132 lines (110 loc) · 3.85 KB
/
main.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <phpcpp.h>
#include "master.h"
PyThreadState *mainThreadState;
PyThreadState *tstate;
/**
* tell the compiler that the get_module is a pure C function
*/
extern "C" {
/**
* Function that is called by PHP right after the PHP process
* has started, and that returns an address of an internal PHP
* strucure with all the details and features of your extension
*
* @return void* a pointer to an address that is understood by PHP
*/
PHPCPP_EXPORT void *get_module()
{
// static(!) Php::Extension object that should stay in memory
// for the entire duration of the process (that's why it's static)
static Php::Extension extension("php-python++", "1.0");
// we have to class - master
Php::Class<Master> master("python");
master.method<&Master::__construct>
("__construct",
{
Php::ByRef("argv", Php::Type::Array, false)
});
master.method<&Master::print>
("print",
{
Php::ByVal("name", Php::Type::String, true)
});
master.method<&Master::dump>
("dump", {});
master.method<&Master::eval>
("eval",
{
Php::ByVal("statements", Php::Type::String, true)
});
master.method<&Master::evalFile>
("evalFile",
{
Php::ByVal("filePath", Php::Type::String, true)
});
master.method<&Master::extract>
("extract",
{
Php::ByVal("name", Php::Type::String, true)
});
master.method<&Master::assign>
("assign",
{
Php::ByVal("name", Php::Type::String, true),
Php::ByVal("value", Php::Type::Null, true),
});
master.method<&Master::def>
("def",
{
Php::ByVal("name", Php::Type::String, true),
Php::ByVal("func", Php::Type::Callable, true),
});
master.method<&Master::call>
("call",
{
Php::ByVal("func", Php::Type::Null, true),
Php::ByVal("params", Php::Type::Null, false),
Php::ByVal("result", Php::Type::String, false)
});
// add all classes to the extension
extension.add(master);
extension.onStartup([]() {
Py_Initialize();
PyEval_InitThreads(); // Will Lock Interpreter
mainThreadState = PyThreadState_Get();
PyEval_ReleaseLock(); // Will Release Lock
});
extension.onShutdown([]() {
PyEval_AcquireLock();
Py_Finalize() ;
});
extension.onRequest([]() {
// Creating a New Thread of Execution
PyEval_AcquireLock();
PyInterpreterState * mainInterpreterState = mainThreadState->interp;
tstate = PyThreadState_New(mainInterpreterState);
PyEval_ReleaseLock();
if (!tstate) {
throw Php::Exception("Python: Failed to create new interpreter");
}
// Prepare to execute PythonCode
PyEval_AcquireLock();
PyThreadState_Swap(tstate);
// Thread Ready!!!
});
extension.onIdle([]() {
//PyEval_ReleaseLock();
//PyEval_AcquireLock();
// Thread Completed!!!
// Swith to main thread
PyThreadState_Swap(mainThreadState);
// Clear thread
PyThreadState_Clear(tstate);
PyEval_ReleaseLock();
// Delete ...
PyThreadState_Delete(tstate);
});
// return the extension
return extension;
}
}