forked from Distributive-Network/PythonMonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonmonkey.cc
More file actions
258 lines (223 loc) · 7.8 KB
/
pythonmonkey.cc
File metadata and controls
258 lines (223 loc) · 7.8 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
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
250
251
252
253
254
255
256
257
258
#include "include/modules/pythonmonkey/pythonmonkey.hh"
#include "include/BoolType.hh"
#include "include/setSpiderMonkeyException.hh"
#include "include/DateType.hh"
#include "include/FloatType.hh"
#include "include/FuncType.hh"
#include "include/PyType.hh"
#include "include/pyTypeFactory.hh"
#include "include/StrType.hh"
#include <jsapi.h>
#include <js/CompilationAndEvaluation.h>
#include <js/Class.h>
#include <js/Date.h>
#include <js/Initialization.h>
#include <js/Object.h>
#include <js/Proxy.h>
#include <js/SourceText.h>
#include <js/Symbol.h>
#include <Python.h>
#include <datetime.h>
typedef std::unordered_map<PyType *, std::vector<JS::PersistentRooted<JS::Value> *>>::iterator PyToGCIterator;
typedef struct {
PyObject_HEAD
} NullObject;
std::unordered_map<PyType *, std::vector<JS::PersistentRooted<JS::Value> *>> PyTypeToGCThing; /**< data structure to hold memoized PyObject & GCThing data for handling GC*/
// @TODO (Caleb Aikens) figure out how to use C99-style designated initializers with a modern C++ compiler
static PyTypeObject NullType = {
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "pythonmonkey.Null",
.tp_basicsize = sizeof(NullObject),
.tp_itemsize = 0,
.tp_dealloc = NULL,
.tp_vectorcall_offset = NULL,
.tp_getattr = NULL,
.tp_setattr = NULL,
.tp_as_async = NULL,
.tp_repr = NULL,
.tp_as_number = NULL,
.tp_as_sequence = NULL,
.tp_as_mapping = NULL,
.tp_hash = NULL,
.tp_call = NULL,
.tp_str = NULL,
.tp_getattro = NULL,
.tp_setattro = NULL,
.tp_as_buffer = NULL,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = PyDoc_STR("Javascript null object"),
.tp_traverse = NULL,
.tp_clear = NULL,
.tp_richcompare = NULL,
.tp_weaklistoffset = NULL,
.tp_iter = NULL,
.tp_iternext = NULL,
.tp_methods = NULL,
.tp_members = NULL,
.tp_getset = NULL,
.tp_base = NULL,
.tp_dict = NULL,
.tp_descr_get = NULL,
.tp_descr_set = NULL,
.tp_dictoffset = NULL,
.tp_init = NULL,
.tp_alloc = NULL,
.tp_new = PyType_GenericNew,
.tp_free = NULL,
.tp_is_gc = NULL,
.tp_bases = NULL,
.tp_mro = NULL,
.tp_cache = NULL,
.tp_subclasses = NULL,
.tp_weaklist = NULL,
.tp_del = NULL,
.tp_version_tag = NULL,
.tp_finalize = NULL,
.tp_vectorcall = NULL,
};
static void cleanup() {
JS_DestroyContext(cx);
JS_ShutDown();
delete global;
}
void memoizePyTypeAndGCThing(PyType *pyType, JS::Handle<JS::Value> GCThing) {
JS::PersistentRooted<JS::Value> *RootedGCThing = new JS::PersistentRooted<JS::Value>(cx, GCThing);
PyToGCIterator pyIt = PyTypeToGCThing.find(pyType);
if (pyIt == PyTypeToGCThing.end()) { // if the PythonObject is not memoized
std::vector<JS::PersistentRooted<JS::Value> *> gcVector(
{{RootedGCThing}});
PyTypeToGCThing.insert({{pyType, gcVector}});
}
else {
pyIt->second.push_back(RootedGCThing);
}
}
void handleSharedPythonMonkeyMemory(JSContext *cx, JSGCStatus status, JS::GCReason reason, void *data) {
if (status == JSGCStatus::JSGC_BEGIN) {
PyToGCIterator pyIt = PyTypeToGCThing.begin();
while (pyIt != PyTypeToGCThing.end()) {
// If the PyObject reference count is exactly 1, then the only reference to the object is the one
// we are holding, which means the object is ready to be free'd.
if (PyObject_GC_IsFinalized(pyIt->first->getPyObject()) || pyIt->first->getPyObject()->ob_refcnt == 1) {
for (JS::PersistentRooted<JS::Value> *rval: pyIt->second) { // for each related GCThing
bool found = false;
for (PyToGCIterator innerPyIt = PyTypeToGCThing.begin(); innerPyIt != PyTypeToGCThing.end(); innerPyIt++) { // for each other PyType pointer
if (innerPyIt != pyIt && std::find(innerPyIt->second.begin(), innerPyIt->second.end(), rval) != innerPyIt->second.end()) { // if the PyType is also related to the GCThing
found = true;
break;
}
}
// if this PyObject is the last PyObject that references this GCThing, then the GCThing can also be free'd
if (!found) {
delete rval;
}
}
pyIt = PyTypeToGCThing.erase(pyIt);
}
else {
pyIt++;
}
}
}
};
static PyObject *collect(PyObject *self, PyObject *args) {
JS_GC(cx);
Py_RETURN_NONE;
}
static PyObject *asUCS4(PyObject *self, PyObject *args) {
StrType *str = new StrType(PyTuple_GetItem(args, 0));
if (!PyUnicode_Check(str->getPyObject())) {
PyErr_SetString(PyExc_TypeError, "pythonmonkey.asUCS4 expects a string as its first argument");
return NULL;
}
return str->asUCS4();
}
static PyObject *eval(PyObject *self, PyObject *args) {
StrType *code = new StrType(PyTuple_GetItem(args, 0));
if (!PyUnicode_Check(code->getPyObject())) {
PyErr_SetString(PyExc_TypeError, "pythonmonkey.eval expects a string as its first argument");
return NULL;
}
JSAutoRealm ar(cx, *global);
JS::CompileOptions options (cx);
options.setFileAndLine("noname", 1);
// initialize JS context
JS::SourceText<mozilla::Utf8Unit> source;
if (!source.init(cx, code->getValue(), strlen(code->getValue()), JS::SourceOwnership::Borrowed)) {
setSpiderMonkeyException(cx);
return NULL;
}
// evaluate source code
JS::Rooted<JS::Value> *rval = new JS::Rooted<JS::Value>(cx);
if (!JS::Evaluate(cx, options, source, rval)) {
setSpiderMonkeyException(cx);
return NULL;
}
// translate to the proper python type
PyType *returnValue = pyTypeFactory(cx, global, rval);
if (returnValue) {
return returnValue->getPyObject();
}
else {
Py_RETURN_NONE;
}
}
PyMethodDef PythonMonkeyMethods[] = {
{"eval", eval, METH_VARARGS, "Javascript evaluator in Python"},
{"collect", collect, METH_VARARGS, "Calls the spidermonkey garbage collector"},
{"asUCS4", asUCS4, METH_VARARGS, "Expects a python string in UTF16 encoding, and returns a new equivalent string in UCS4. Undefined behaviour if the string is not in UTF16."},
{NULL, NULL, 0, NULL}
};
struct PyModuleDef pythonmonkey =
{
PyModuleDef_HEAD_INIT,
"pythonmonkey", /* name of module */
"A module for python to JS interoperability", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
PythonMonkeyMethods
};
PyObject *SpiderMonkeyError = NULL;
PyMODINIT_FUNC PyInit_pythonmonkey(void)
{
PyDateTime_IMPORT;
if (!JS_Init()) {
PyErr_SetString(SpiderMonkeyError, "Spidermonkey could not be initialized.");
return NULL;
}
cx = JS_NewContext(JS::DefaultHeapMaxBytes);
if (!cx) {
PyErr_SetString(SpiderMonkeyError, "Spidermonkey could not create a JS context.");
return NULL;
}
if (!JS::InitSelfHostedCode(cx)) {
PyErr_SetString(SpiderMonkeyError, "Spidermonkey could not initialize self-hosted code.");
return NULL;
}
JS::RealmOptions options;
static JSClass globalClass = {"global", JSCLASS_GLOBAL_FLAGS, &JS::DefaultGlobalClassOps};
global = new JS::RootedObject(cx, JS_NewGlobalObject(cx, &globalClass, nullptr, JS::FireOnNewGlobalHook, options));
if (!global) {
PyErr_SetString(SpiderMonkeyError, "Spidermonkey could not create a global object.");
return NULL;
}
Py_AtExit(cleanup);
JS_SetGCCallback(cx, handleSharedPythonMonkeyMemory, NULL);
PyObject *pyModule;
if (PyType_Ready(&NullType) < 0)
return NULL;
pyModule = PyModule_Create(&pythonmonkey);
if (pyModule == NULL)
return NULL;
Py_INCREF(&NullType);
if (PyModule_AddObject(pyModule, "null", (PyObject *)&NullType) < 0) {
Py_DECREF(&NullType);
Py_DECREF(pyModule);
return NULL;
}
SpiderMonkeyError = PyErr_NewException("pythonmonkey.SpiderMonkeyError", NULL, NULL);
if (PyModule_AddObject(pyModule, "SpiderMonkeyError", SpiderMonkeyError)) {
Py_DECREF(pyModule);
return NULL;
}
return pyModule;
}