Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ecd523

Browse files
committed
Support more argument types.
1 parent 9e698e8 commit 8ecd523

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

‎module.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,16 @@ static PyObject *object_call(ObjectData *self, PyObject *args, PyObject *kwds) {
9595
const int nargs = PyTuple_Size(args);
9696
for (int i = 0; i < nargs; ++i) {
9797
PyObject *item = PyTuple_GetItem(args, i);
98-
if (PyLong_Check(item)) {
98+
if (PyBool_Check(item)) {
99+
} else if (PyLong_Check(item)) {
100+
} else if (PyFloat_Check(item)) {
99101
} else if (PyUnicode_Check(item)) {
100102
} else if (PyObject_IsInstance(item, (PyObject *)&Object)) {
101103
} else {
102-
PyErr_Format(PyExc_ValueError, "Unsupported type when calling quickjs object");
104+
PyErr_Format(PyExc_ValueError,
105+
"Unsupported type of argument %d when calling quickjs object: %s.",
106+
i,
107+
Py_TYPE(item)->tp_name);
103108
return NULL;
104109
}
105110
}
@@ -108,8 +113,12 @@ static PyObject *object_call(ObjectData *self, PyObject *args, PyObject *kwds) {
108113
JSValueConst *jsargs = malloc(nargs * sizeof(JSValueConst));
109114
for (int i = 0; i < nargs; ++i) {
110115
PyObject *item = PyTuple_GetItem(args, i);
111-
if (PyLong_Check(item)) {
116+
if (PyBool_Check(item)) {
117+
jsargs[i] = JS_MKVAL(JS_TAG_BOOL, item == Py_True ? 1 : 0);
118+
} else if (PyLong_Check(item)) {
112119
jsargs[i] = JS_MKVAL(JS_TAG_INT, PyLong_AsLong(item));
120+
} else if (PyFloat_Check(item)) {
121+
jsargs[i] = JS_NewFloat64(self->context->context, PyFloat_AsDouble(item));
113122
} else if (PyUnicode_Check(item)) {
114123
jsargs[i] = JS_NewString(self->context->context, PyUnicode_AsUTF8(item));
115124
} else if (PyObject_IsInstance(item, (PyObject *)&Object)) {

‎quickjs/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __call__(self, *args):
2626

2727
def _call(self, *args):
2828
def convert_arg(arg):
29-
if isinstance(arg, (str, int)):
29+
if isinstance(arg, (str, bool, float, int)):
3030
return arg
3131
else:
3232
# More complex objects are passed through JSON.

‎test_quickjs.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def test_eval_str(self):
2424

2525
def test_eval_bool(self):
2626
self.assertEqual(self.context.eval("true || false"), True)
27+
self.assertEqual(self.context.eval("true && false"), False)
2728

2829
def test_eval_null(self):
2930
self.assertIsNone(self.context.eval("null"))
@@ -167,6 +168,26 @@ def test_adder(self):
167168
self.assertEqual(f(100, 200), 300)
168169
self.assertEqual(f("a", "b"), "ab")
169170

171+
def test_identity(self):
172+
identity = quickjs.Function(
173+
"identity", """
174+
function identity(x) {
175+
return x;
176+
}
177+
""")
178+
for x in [True, [1], {"a": 2}, 1, 1.5, "hej"]:
179+
self.assertEqual(identity(x), x)
180+
181+
def test_bool(self):
182+
f = quickjs.Function(
183+
"f", """
184+
function f(x) {
185+
return [typeof x ,!x];
186+
}
187+
""")
188+
self.assertEqual(f(False), ["boolean", True])
189+
self.assertEqual(f(True), ["boolean", False])
190+
170191
def test_empty(self):
171192
f = quickjs.Function("f", "function f() { }")
172193
self.assertEqual(f(), None)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /