-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmaster.h
250 lines (225 loc) · 6.42 KB
/
master.h
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* Python.h
*
* Class that is exported to PHP space
*/
#pragma once
#include <pybind11/pybind11.h>
#include <pybind11/eval.h>
#include <iostream>
#include <sstream>
namespace py = pybind11;
/**
* Class definition
*/
class Master : public Php::Base
{
private:
py::module _main;
py::module _json;
py::module _sys;
py::dict *_scope;
py::dict *_local;
public:
/**
* Constructor
*/
Master()
{
//PyEval_AcquireLock();
//PyThreadState_Swap(tstate);
_main = py::module::import("__main__");
_sys = py::module::import("sys");
_json = py::module::import("json");
_scope = new py::dict(_main.attr("__dict__"));
_local = _scope;
_main.attr("json") = _json;
_main.attr("sys") = _sys;
}
/**
* Destructor
*/
~Master() {
delete _scope;
//delete _local;
//PyEval_ReleaseLock();
}
/**
* php "constructor"
* @param params
*/
void __construct(Php::Parameters ¶ms)
{
if (!params.empty()) {
Php::Value argv = params[0];
_sys.attr("argv") = phpVal2PyObj(argv);
}
}
void evalFile(Php::Parameters ¶ms) {
std::string filePath = params[0];
try {
py::eval_file(filePath, *_scope);
} catch(py::error_already_set& ex) {
throw Php::Exception(ex.what());
}
}
void eval(Php::Parameters ¶ms) {
std::string state = params[0];
try {
py::eval<py::eval_statements>(state, *_scope);
} catch (py::error_already_set& ex) {
throw Php::Exception(ex.what());
}
}
void assign(Php::Parameters ¶ms) {
std::string name = params[0];
(*_local)[py::str(name)] = phpVal2PyObj(params[1]);
}
Php::Value call(Php::Parameters ¶ms) {
// Python函数执行结果
py::object pyRet;
py::object func;
// 第1个参数为被调用的函数名,形式为:
// 1) "print"
// 2) ["os", "rename"]
if (params[0].isString()) {
const char* funcStr = params[0];
if ((*_scope).contains(funcStr) && !(*_scope)[funcStr].is_none()) {
func = _main.attr(funcStr);
} else {
pyRet = py::eval(funcStr, *_scope);
return pyObj2PhpVal(pyRet);
}
} else if(params[0].isArray()){
Php::Value array = params[0];
auto moduleFunc = array.mapValue();
const char* mName = moduleFunc["0"];
const char* fName = moduleFunc["1"];
// 模块是否已加载
if ((*_scope).contains(mName) && !(*_scope)[mName].is_none()) {
// nothing
// std::cout << mName << " alread loaded in _main" << std::endl;
} else {
(*_scope)[mName] = py::module::import(mName);
}
func = (*_local)[mName].attr(fName);
}
// 构建Python参数
py::tuple pyParams;
if (params.size() > 1) {
pyParams = makeArgs(params[1]);
} else {
pyParams = py::tuple(0);
}
pyRet = func(*pyParams);
if(params.size() > 2 && params[2].isString()) {
std::string pyVar = params[2];
(*_local)[py::str(pyVar)] = pyRet;
return nullptr;
}
return pyObj2PhpVal(pyRet);
}
Php::Value __call(const char *name, Php::Parameters ¶ms)
{
std::string funcName = std::string(name);
size_t index = 0;
py::tuple pyArgs = py::tuple(params.size());
for (auto iter : params) {
if (iter.isString()) {
std::string pyLocalArgStr = iter;
if(pyLocalArgStr.rfind("py::", 0) == 0) {
std::string pyLocalArgName = pyLocalArgStr.substr(4);
py::object pyLocalArg = (*_local)[py::str(pyLocalArgName)];
pyArgs[index++] = pyLocalArg;
} else {
pyArgs[index++] = py::str(pyLocalArgStr);
}
} else {
pyArgs[index++] = phpVal2PyObj(iter);
}
}
auto func = (*_local)[py::str(funcName)];
return pyObj2PhpVal(func(*pyArgs));
}
py::tuple makeArgs(Php::Value phpArgs) {
size_t index = 0;
if(phpArgs.isArray()) {
auto pyParams = py::tuple(phpArgs.size());
for (auto &iter : phpArgs) {
if (iter.second.isString()) {
std::string pyLocalArgStr = iter.second;
if(pyLocalArgStr.rfind("py::", 0) == 0) {
std::string pyLocalArgName = pyLocalArgStr.substr(4);
py::object pyLocalArg = (*_local)[py::str(pyLocalArgName)];
pyParams[index++] = pyLocalArg;
} else {
pyParams[index++] = py::str(pyLocalArgStr);
}
} else {
pyParams[index++] = phpVal2PyObj(iter.second);
}
}
return pyParams;
} else if(phpArgs.isString()) {
auto pyParams = py::tuple(1);
std::string pyLocalArgStr = phpArgs;
if(pyLocalArgStr.rfind("py::", 0) == 0) {
std::string pyLocalArgName = pyLocalArgStr.substr(4);
py::object pyLocalArg = (*_local)[py::str(pyLocalArgName)];
pyParams[index++] = pyLocalArg;
} else {
pyParams[index++] = py::str(pyLocalArgStr);
}
return pyParams;
}
return py::tuple(0);
}
py::object phpVal2PyObj(Php::Value val) {
auto json_decode = this->_json.attr("loads");
std::string phpStr = Php::call("json_encode", val);
return json_decode(py::str(phpStr));
}
Php::Value pyObj2PhpVal(py::object obj) {
auto json_encode = this->_json.attr("dumps");
std::string pyStr = json_encode(obj).cast<std::string>();
return Php::call("json_decode", pyStr, true);
}
void def(Php::Parameters ¶ms) {
const char *name = params[0];
Php::Value func = params[1];
_main.def(name, [func, this](py::args args, py::kwargs kwargs) {
return phpVal2PyObj(func(pyObj2PhpVal(args)));
});
}
Php::Value extract(Php::Parameters ¶ms) {
if (params.size() != 1) throw Php::Exception("Invalid number of parameters supplied");
std::string name = params[0];
py::object val = (*_local)[py::str(name)];
return pyObj2PhpVal(val);
}
void dump(Php::Parameters ¶ms) {
py::print(*_local);
}
void print(Php::Parameters ¶ms) {
if(params.size() == 1) {
std::string v = params[0];
py::print((*_local)[py::str(v)]);
} else {
auto pyParams = py::tuple(params.size());
size_t index = 0;
for (auto iter : params) {
std::string v = iter;
pyParams[index++] = (*_local)[py::str(v)];
}
py::print(pyParams);
}
}
/**
* Cast to a string
* @return const char *
*/
const char *__toString() const
{
return "This is the PHPython main class";
}
};