@@ -25,18 +25,42 @@ static PyObject *object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2525}
2626
2727static void object_dealloc (ObjectData * self ) {
28+ if (self -> context ) {
29+ JS_FreeValue (self -> context , self -> object );
30+ }
2831Py_TYPE (self )-> tp_free ((PyObject * )self );
2932}
3033
31- static PyObject * object_call (ObjectData * self , PyObject * args ) {
32- if (self -> context ) {
33- JS_FreeValue ( self -> context , self -> object ) ;
34+ static PyObject * object_call (ObjectData * self , PyObject * args , PyObject * kwds ) {
35+ if (self -> context == NULL ) {
36+ Py_RETURN_NONE ;
3437}
35- Py_RETURN_NONE ;
38+ const int nargs = PyTuple_Size (args );
39+ for (int i = 0 ; i < nargs ; ++ i ) {
40+ PyObject * item = PyTuple_GetItem (args , i );
41+ if (PyLong_Check (item )) {
42+ } else {
43+ PyErr_Format (PyExc_ValueError , "Unsupported type when calling quickjs object" );
44+ return NULL ;
45+ }
46+ }
47+ JSValueConst * jsargs = malloc (nargs * sizeof (JSValueConst ));
48+ for (int i = 0 ; i < nargs ; ++ i ) {
49+ PyObject * item = PyTuple_GetItem (args , i );
50+ if (PyLong_Check (item )) {
51+ jsargs [i ] = JS_MKVAL (JS_TAG_INT , PyLong_AsLong (item ));
52+ }
53+ }
54+ JSValue value = JS_Call (self -> context , self -> object , JS_NULL , 1 , jsargs );
55+ for (int i = 0 ; i < nargs ; ++ i ) {
56+ JS_FreeValue (self -> context , jsargs [i ]);
57+ }
58+ free (jsargs );
59+ return quickjs_to_python (self -> context , value );
3660}
3761
3862static PyMethodDef object_methods [] = {
39- {"call" , ( PyCFunction ) object_call , METH_VARARGS , "Calls a JS function." }, { NULL } /* Sentinel */
63+ {NULL } /* Sentinel */
4064};
4165
4266static PyTypeObject Object = {PyVarObject_HEAD_INIT (NULL , 0 ).tp_name = "_quickjs.Object" ,
@@ -46,6 +70,7 @@ static PyTypeObject Object = {PyVarObject_HEAD_INIT(NULL, 0).tp_name = "_quickjs
4670 .tp_flags = Py_TPFLAGS_DEFAULT ,
4771 .tp_new = object_new ,
4872 .tp_dealloc = (destructor )object_dealloc ,
73+ .tp_call = object_call ,
4974 .tp_methods = object_methods };
5075
5176static PyObject * quickjs_to_python (JSContext * context , JSValue value ) {
0 commit comments