Skip to content

Commit 4bdcf8e

Browse files
committed
Refactor to allow modules in the future.
1 parent 67bc242 commit 4bdcf8e

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

module.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ static PyObject *quickjs_to_python(ContextData *context_obj, JSValue value) {
220220
const char *cstring = JS_ToCString(context, value);
221221
return_value = Py_BuildValue("s", cstring);
222222
JS_FreeCString(context, cstring);
223-
} else if (tag == JS_TAG_OBJECT) {
223+
} else if (tag == JS_TAG_OBJECT || tag == JS_TAG_MODULE) {
224224
// This is a Javascript object or function. We wrap it in a _quickjs.Object.
225225
return_value = PyObject_CallObject((PyObject *)&Object, NULL);
226226
ObjectData *object = (ObjectData *)return_value;
@@ -270,11 +270,9 @@ static void context_dealloc(ContextData *self) {
270270
Py_TYPE(self)->tp_free((PyObject *)self);
271271
}
272272

273-
// _quickjs.Context.eval
274-
//
275273
// Evaluates a Python string as JS and returns the result as a Python object. Will return
276274
// _quickjs.Object for complex types (other than e.g. str, int).
277-
static PyObject *context_eval(ContextData *self, PyObject *args) {
275+
static PyObject *context_eval_internal(ContextData *self, PyObject *args, int eval_type) {
278276
const char *code;
279277
if (!PyArg_ParseTuple(args, "s", &code)) {
280278
return NULL;
@@ -287,12 +285,27 @@ static PyObject *context_eval(ContextData *self, PyObject *args) {
287285
Py_BEGIN_ALLOW_THREADS;
288286
InterruptData interrupt_data;
289287
setup_time_limit(self, &interrupt_data);
290-
value = JS_Eval(self->context, code, strlen(code), "<input>", JS_EVAL_TYPE_GLOBAL);
288+
value = JS_Eval(self->context, code, strlen(code), "<input>", eval_type);
291289
teardown_time_limit(self);
292290
Py_END_ALLOW_THREADS;
293291
return quickjs_to_python(self, value);
294292
}
295293

294+
// _quickjs.Context.eval
295+
//
296+
// Evaluates a Python string as JS and returns the result as a Python object. Will return
297+
// _quickjs.Object for complex types (other than e.g. str, int).
298+
static PyObject *context_eval(ContextData *self, PyObject *args) {
299+
return context_eval_internal(self, args, JS_EVAL_TYPE_GLOBAL);
300+
}
301+
302+
// _quickjs.Context.module
303+
//
304+
// Evaluates a Python string as JS module. Otherwise identical to eval.
305+
static PyObject *context_module(ContextData *self, PyObject *args) {
306+
return context_eval_internal(self, args, JS_EVAL_TYPE_MODULE);
307+
}
308+
296309
// _quickjs.Context.get
297310
//
298311
// Retrieves a global variable from the JS context.
@@ -394,6 +407,10 @@ static PyObject *context_gc(ContextData *self) {
394407
// All methods of the _quickjs.Context class.
395408
static PyMethodDef context_methods[] = {
396409
{"eval", (PyCFunction)context_eval, METH_VARARGS, "Evaluates a Javascript string."},
410+
{"module",
411+
(PyCFunction)context_module,
412+
METH_VARARGS,
413+
"Evaluates a Javascript string as a module."},
397414
{"get", (PyCFunction)context_get, METH_VARARGS, "Gets a Javascript global variable."},
398415
{"set_memory_limit",
399416
(PyCFunction)context_set_memory_limit,

test_quickjs.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ def test_get(self):
5454
self.assertEqual(self.context.get("y"), "foo")
5555
self.assertEqual(self.context.get("z"), None)
5656

57+
def test_module(self):
58+
self.context.module("""
59+
export function test() {
60+
return 42;
61+
}
62+
""")
63+
5764
def test_error(self):
5865
with self.assertRaisesRegex(quickjs.JSException, "ReferenceError: missing is not defined"):
5966
self.context.eval("missing + missing")
@@ -310,5 +317,7 @@ def test_unicode(self):
310317
return x;
311318
}
312319
""")
320+
context = quickjs.Context()
313321
for x in ["äpple", "≤≥", "☺"]:
314322
self.assertEqual(identity(x), x)
323+
self.assertEqual(context.eval('(function(){ return "' + x + '";})()'), x)

0 commit comments

Comments
 (0)