@@ -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.
395408static 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 ,
0 commit comments