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
433 lines (371 loc) · 15.2 KB
/
pythonmonkey.cc
File metadata and controls
433 lines (371 loc) · 15.2 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/**
* @file pythonmonkey.cc
* @author Caleb Aikens (caleb@distributive.network)
* @brief This file defines the pythonmonkey module, along with its various functions.
* @version 0.1
* @date 2023-03-29
*
* @copyright Copyright (c) 2023 Distributive Corp.
*
*/
#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/JSObjectProxy.hh"
#include "include/PyType.hh"
#include "include/pyTypeFactory.hh"
#include "include/StrType.hh"
#include "include/PyEventLoop.hh"
#include "include/internalBinding.hh"
#include <jsapi.h>
#include <jsfriendapi.h>
#include <js/friend/ErrorMessages.h>
#include <js/friend/DOMProxy.h>
#include <js/CompilationAndEvaluation.h>
#include <js/ContextOptions.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>
#include <unordered_map>
#include <vector>
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*/
static PyTypeObject NullType = {
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "pythonmonkey.null",
.tp_basicsize = sizeof(NullObject),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = PyDoc_STR("Javascript null object"),
};
static PyTypeObject BigIntType = {
.tp_name = "pythonmonkey.bigint",
.tp_flags = Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_LONG_SUBCLASS // https://docs.python.org/3/c-api/typeobj.html#Py_TPFLAGS_LONG_SUBCLASS
| Py_TPFLAGS_BASETYPE, // can be subclassed
.tp_doc = PyDoc_STR("Javascript BigInt object"),
.tp_base = &PyLong_Type, // extending the builtin int type
};
PyTypeObject JSObjectProxyType = {
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "pythonmonkey.JSObjectProxy",
.tp_basicsize = sizeof(JSObjectProxy),
.tp_dealloc = (destructor)JSObjectProxyMethodDefinitions::JSObjectProxy_dealloc,
.tp_repr = (reprfunc)JSObjectProxyMethodDefinitions::JSObjectProxy_repr,
.tp_as_mapping = &JSObjectProxy_mapping_methods,
.tp_getattro = (getattrofunc)JSObjectProxyMethodDefinitions::JSObjectProxy_get,
.tp_setattro = (setattrofunc)JSObjectProxyMethodDefinitions::JSObjectProxy_assign,
.tp_flags = Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_DICT_SUBCLASS, // https://docs.python.org/3/c-api/typeobj.html#Py_TPFLAGS_DICT_SUBCLASS
.tp_doc = PyDoc_STR("Javascript Object proxy dict"),
.tp_richcompare = (richcmpfunc)JSObjectProxyMethodDefinitions::JSObjectProxy_richcompare,
.tp_iter = (getiterfunc)JSObjectProxyMethodDefinitions::JSObjectProxy_iter,
.tp_base = &PyDict_Type,
.tp_init = (initproc)JSObjectProxyMethodDefinitions::JSObjectProxy_init,
.tp_new = JSObjectProxyMethodDefinitions::JSObjectProxy_new,
};
static void cleanup() {
delete autoRealm;
delete global;
delete JOB_QUEUE;
if (GLOBAL_CX) JS_DestroyContext(GLOBAL_CX);
JS_ShutDown();
}
void memoizePyTypeAndGCThing(PyType *pyType, JS::Handle<JS::Value> GCThing) {
JS::PersistentRooted<JS::Value> *RootedGCThing = new JS::PersistentRooted<JS::Value>(GLOBAL_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()) {
PyObject *pyObj = pyIt->first->getPyObject();
// 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 (_PyGC_FINALIZED(pyObj) || pyObj->ob_refcnt == 1) { // PyObject_GC_IsFinalized is only available in Python 3.9+
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(GLOBAL_CX);
Py_RETURN_NONE;
}
static bool getEvalOption(PyObject *evalOptions, const char *optionName, const char **s_p) {
PyObject *value;
value = PyDict_GetItemString(evalOptions, optionName);
if (value)
*s_p = PyUnicode_AsUTF8(value);
return value != NULL;
}
static bool getEvalOption(PyObject *evalOptions, const char *optionName, unsigned long *l_p) {
PyObject *value;
value = PyDict_GetItemString(evalOptions, optionName);
if (value)
*l_p = PyLong_AsUnsignedLong(value);
return value != NULL;
}
static bool getEvalOption(PyObject *evalOptions, const char *optionName, bool *b_p) {
PyObject *value;
value = PyDict_GetItemString(evalOptions, optionName);
if (value)
*b_p = PyObject_IsTrue(value) == 1 ? true : false;
return value != NULL;
}
static PyObject *eval(PyObject *self, PyObject *args) {
size_t argc = PyTuple_GET_SIZE(args);
StrType *code = new StrType(PyTuple_GetItem(args, 0));
PyObject *evalOptions = argc == 2 ? PyTuple_GetItem(args, 1) : NULL;
if (argc == 0 || !PyUnicode_Check(code->getPyObject())) {
PyErr_SetString(PyExc_TypeError, "pythonmonkey.eval expects a string as its first argument");
return NULL;
}
if (evalOptions && !PyDict_Check(evalOptions)) {
PyErr_SetString(PyExc_TypeError, "pythonmonkey.eval expects a dict as its (optional) second argument");
return NULL;
}
JSAutoRealm ar(GLOBAL_CX, *global);
JS::CompileOptions options (GLOBAL_CX);
options.setFileAndLine("evaluate", 1)
.setIsRunOnce(true)
.setNoScriptRval(false)
.setIntroductionType("pythonmonkey eval");
if (evalOptions) {
const char *s;
unsigned long l;
bool b;
if (getEvalOption(evalOptions, "filename", &s)) options.setFile(s);
if (getEvalOption(evalOptions, "lineno", &l)) options.setLine(l);
if (getEvalOption(evalOptions, "column", &l)) options.setColumn(l);
if (getEvalOption(evalOptions, "mutedErrors", &b)) options.setMutedErrors(b);
if (getEvalOption(evalOptions, "noScriptRval", &b)) options.setNoScriptRval(b);
if (getEvalOption(evalOptions, "selfHosting", &b)) options.setSelfHostingMode(b);
if (getEvalOption(evalOptions, "strict", &b)) if (b) options.setForceStrictMode();
if (getEvalOption(evalOptions, "module", &b)) if (b) options.setModule();
if (getEvalOption(evalOptions, "fromPythonFrame", &b) && b) {
#if PY_VERSION_HEX >= 0x03090000
PyFrameObject *frame = PyEval_GetFrame();
if (frame && !getEvalOption(evalOptions, "lineno", &l)) {
options.setLine(PyFrame_GetLineNumber(frame));
} /* lineno */
#endif
#if 0 && (PY_VERSION_HEX >= 0x030a0000) && (PY_VERSION_HEX < 0x030c0000)
PyObject *filename = PyDict_GetItemString(frame->f_builtins, "__file__");
#elif (PY_VERSION_HEX >= 0x030c0000)
PyObject *filename = PyDict_GetItemString(PyFrame_GetGlobals(frame), "__file__");
#else
PyObject *filename = NULL;
#endif
if (!getEvalOption(evalOptions, "filename", &s)) {
if (filename && PyUnicode_Check(filename)) {
options.setFile(PyUnicode_AsUTF8(filename));
}
} /* filename */
} /* fromPythonFrame */
} /* eval options */
// initialize JS context
JS::SourceText<mozilla::Utf8Unit> source;
if (!source.init(GLOBAL_CX, code->getValue(), strlen(code->getValue()), JS::SourceOwnership::Borrowed)) {
setSpiderMonkeyException(GLOBAL_CX);
return NULL;
}
delete code;
// evaluate source code
JS::Rooted<JS::Value> *rval = new JS::Rooted<JS::Value>(GLOBAL_CX);
if (!JS::Evaluate(GLOBAL_CX, options, source, rval)) {
setSpiderMonkeyException(GLOBAL_CX);
return NULL;
}
// translate to the proper python type
PyType *returnValue = pyTypeFactory(GLOBAL_CX, global, rval);
if (PyErr_Occurred()) {
return NULL;
}
// TODO: Find a better way to destroy the root when necessary (when the returned Python object is GCed).
js::ESClass cls = js::ESClass::Other; // placeholder if `rval` is not a JSObject
if (rval->isObject()) {
JS::GetBuiltinClass(GLOBAL_CX, JS::RootedObject(GLOBAL_CX, &rval->toObject()), &cls);
if (JS_ObjectIsBoundFunction(&rval->toObject())) {
cls = js::ESClass::Function; // In SpiderMonkey 115 ESR, bound function is no longer a JSFunction but a js::BoundFunctionObject.
}
}
bool rvalIsFunction = cls == js::ESClass::Function; // function object
bool rvalIsString = rval->isString() || cls == js::ESClass::String; // string primitive or boxed String object
if (!(rvalIsFunction || rvalIsString)) { // rval may be a JS function or string which must be kept alive.
delete rval;
}
if (returnValue) {
return returnValue->getPyObject();
}
else {
Py_RETURN_NONE;
}
}
static PyObject *waitForEventLoop(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(_)) {
PyObject *waiter = PyEventLoop::_locker->_queueIsEmpty; // instance of asyncio.Event
// Making sure it's attached to the current event-loop
PyEventLoop loop = PyEventLoop::getRunningLoop();
if (!loop.initialized()) return NULL;
PyObject_SetAttrString(waiter, "_loop", loop._loop);
return PyObject_CallMethod(waiter, "wait", NULL);
}
static PyObject *isCompilableUnit(PyObject *self, PyObject *args) {
StrType *buffer = new StrType(PyTuple_GetItem(args, 0));
const char *bufferUtf8;
bool compilable;
if (!PyUnicode_Check(buffer->getPyObject())) {
PyErr_SetString(PyExc_TypeError, "pythonmonkey.eval expects a string as its first argument");
return NULL;
}
bufferUtf8 = buffer->getValue();
compilable = JS_Utf8BufferIsCompilableUnit(GLOBAL_CX, *global, bufferUtf8, strlen(bufferUtf8));
if (compilable)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
PyMethodDef PythonMonkeyMethods[] = {
{"eval", eval, METH_VARARGS, "Javascript evaluator in Python"},
{"wait", waitForEventLoop, METH_NOARGS, "The event-loop shield. Blocks until all asynchronous jobs finish."},
{"isCompilableUnit", isCompilableUnit, METH_VARARGS, "Hint if a string might be compilable Javascript"},
{"collect", collect, METH_VARARGS, "Calls the spidermonkey garbage collector"},
{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)
{
if (!PyDateTimeAPI) { PyDateTime_IMPORT; }
SpiderMonkeyError = PyErr_NewException("pythonmonkey.SpiderMonkeyError", NULL, NULL);
if (!JS_Init()) {
PyErr_SetString(SpiderMonkeyError, "Spidermonkey could not be initialized.");
return NULL;
}
Py_AtExit(cleanup);
GLOBAL_CX = JS_NewContext(JS::DefaultHeapMaxBytes);
if (!GLOBAL_CX) {
PyErr_SetString(SpiderMonkeyError, "Spidermonkey could not create a JS context.");
return NULL;
}
JS::ContextOptionsRef(GLOBAL_CX)
.setWasm(true)
.setAsmJS(true)
.setAsyncStack(true)
.setSourcePragmas(true);
JOB_QUEUE = new JobQueue();
if (!JOB_QUEUE->init(GLOBAL_CX)) {
PyErr_SetString(SpiderMonkeyError, "Spidermonkey could not create the event-loop.");
return NULL;
}
if (!JS::InitSelfHostedCode(GLOBAL_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(GLOBAL_CX, JS_NewGlobalObject(GLOBAL_CX, &globalClass, nullptr, JS::FireOnNewGlobalHook, options));
if (!global) {
PyErr_SetString(SpiderMonkeyError, "Spidermonkey could not create a global object.");
return NULL;
}
JS::RootedObject debuggerGlobal(GLOBAL_CX, JS_NewGlobalObject(GLOBAL_CX, &globalClass, nullptr, JS::FireOnNewGlobalHook, options));
{
JSAutoRealm r(GLOBAL_CX, debuggerGlobal);
JS_DefineProperty(GLOBAL_CX, debuggerGlobal, "mainGlobal", *global, JSPROP_READONLY);
JS_DefineDebuggerObject(GLOBAL_CX, debuggerGlobal);
}
autoRealm = new JSAutoRealm(GLOBAL_CX, *global);
JS_SetGCCallback(GLOBAL_CX, handleSharedPythonMonkeyMemory, NULL);
JS_DefineProperty(GLOBAL_CX, *global, "debuggerGlobal", debuggerGlobal, JSPROP_READONLY);
// XXX: SpiderMonkey bug???
// In https://hg.mozilla.org/releases/mozilla-esr102/file/3b574e1/js/src/jit/CacheIR.cpp#l317, trying to use the callback returned by `js::GetDOMProxyShadowsCheck()` even it's unset (nullptr)
// Temporarily solved by explicitly setting the `domProxyShadowsCheck` callback here
JS::SetDOMProxyInformation(nullptr,
[](JSContext *, JS::HandleObject, JS::HandleId) { // domProxyShadowsCheck
return JS::DOMProxyShadowsResult::ShadowCheckFailed;
}, nullptr);
PyObject *pyModule;
if (PyType_Ready(&NullType) < 0)
return NULL;
if (PyType_Ready(&BigIntType) < 0)
return NULL;
if (PyType_Ready(&JSObjectProxyType) < 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;
}
Py_INCREF(&BigIntType);
if (PyModule_AddObject(pyModule, "bigint", (PyObject *)&BigIntType) < 0) {
Py_DECREF(&BigIntType);
Py_DECREF(pyModule);
return NULL;
}
Py_INCREF(&JSObjectProxyType);
if (PyModule_AddObject(pyModule, "JSObjectProxy", (PyObject *)&JSObjectProxyType) < 0) {
Py_DECREF(&JSObjectProxyType);
Py_DECREF(pyModule);
return NULL;
}
if (PyModule_AddObject(pyModule, "SpiderMonkeyError", SpiderMonkeyError)) {
Py_DECREF(pyModule);
return NULL;
}
// Initialize event-loop shield
PyEventLoop::_locker = new PyEventLoop::Lock();
PyObject *internalBindingPy = getInternalBindingPyFn(GLOBAL_CX);
if (PyModule_AddObject(pyModule, "internalBinding", internalBindingPy) < 0) {
Py_DECREF(internalBindingPy);
Py_DECREF(pyModule);
return NULL;
}
return pyModule;
}