Skip to content

Commit 94e1f64

Browse files
committed
Allow strings in function call.
1 parent be3022b commit 94e1f64

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

module.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static PyObject *object_call(ObjectData *self, PyObject *args, PyObject *kwds) {
3939
for (int i = 0; i < nargs; ++i) {
4040
PyObject *item = PyTuple_GetItem(args, i);
4141
if (PyLong_Check(item)) {
42+
} else if (PyUnicode_Check(item)) {
4243
} else {
4344
PyErr_Format(PyExc_ValueError, "Unsupported type when calling quickjs object");
4445
return NULL;
@@ -49,6 +50,10 @@ static PyObject *object_call(ObjectData *self, PyObject *args, PyObject *kwds) {
4950
PyObject *item = PyTuple_GetItem(args, i);
5051
if (PyLong_Check(item)) {
5152
jsargs[i] = JS_MKVAL(JS_TAG_INT, PyLong_AsLong(item));
53+
} else if (PyUnicode_Check(item)) {
54+
const char *cstring = PyUnicode_AsUTF8(item);
55+
jsargs[i] = JS_NewString(self->context, cstring);
56+
PyMem_Free(cstring);
5257
}
5358
}
5459
JSValue value = JS_Call(self->context, self->object, JS_NULL, 1, jsargs);

test_quickjs.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,22 @@ def test_function_is_object(self):
5454
""")
5555
self.assertIsInstance(f, quickjs.Object)
5656

57-
def test_function_call(self):
57+
def test_function_call_int(self):
5858
f = self.context.eval("""
5959
f = function(x) {
6060
return 40 + x;
6161
}
6262
""")
6363
self.assertEqual(f(2), 42)
6464

65+
def test_function_call_str(self):
66+
f = self.context.eval("""
67+
f = function(a) {
68+
return a + " hej";
69+
}
70+
""")
71+
self.assertEqual(f("1"), "1 hej")
72+
6573
def test_function_call_unsupported_arg(self):
6674
f = self.context.eval("""
6775
f = function(x) {

0 commit comments

Comments
 (0)