Skip to content

Commit 1726795

Browse files
committed
Add a get function for the context.
1 parent 513c47e commit 1726795

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

module.c

100644100755
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static PyObject *object_call(ObjectData *self, PyObject *args, PyObject *kwds) {
6868
PyObject *item = PyTuple_GetItem(args, i);
6969
if (PyLong_Check(item)) {
7070
} else if (PyUnicode_Check(item)) {
71-
} else if (PyObject_IsInstance(item, &Object)) {
71+
} else if (PyObject_IsInstance(item, (PyObject*)&Object)) {
7272
} else {
7373
PyErr_Format(PyExc_ValueError, "Unsupported type when calling quickjs object");
7474
return NULL;
@@ -81,7 +81,7 @@ static PyObject *object_call(ObjectData *self, PyObject *args, PyObject *kwds) {
8181
jsargs[i] = JS_MKVAL(JS_TAG_INT, PyLong_AsLong(item));
8282
} else if (PyUnicode_Check(item)) {
8383
jsargs[i] = JS_NewString(self->context, PyUnicode_AsUTF8(item));
84-
} else if (PyObject_IsInstance(item, &Object)) {
84+
} else if (PyObject_IsInstance(item, (PyObject*)&Object)) {
8585
jsargs[i] = JS_DupValue(self->context, ((ObjectData *)item)->object);
8686
}
8787
}
@@ -105,8 +105,6 @@ static PyObject *quickjs_to_python(JSContext *context, JSValue value) {
105105
return_value = Py_None;
106106
} else if (tag == JS_TAG_UNDEFINED) {
107107
return_value = Py_None;
108-
} else if (tag == JS_TAG_UNINITIALIZED) {
109-
return_value = Py_None;
110108
} else if (tag == JS_TAG_EXCEPTION) {
111109
JSValue exception = JS_GetException(context);
112110
JSValue error_string = JS_ToString(context, exception);
@@ -125,8 +123,7 @@ static PyObject *quickjs_to_python(JSContext *context, JSValue value) {
125123
return_value = PyObject_CallObject((PyObject *)&Object, NULL);
126124
ObjectData *object = (ObjectData *)return_value;
127125
object->context = context;
128-
object->object = value;
129-
return return_value;
126+
object->object = JS_DupValue(context, value);
130127
} else {
131128
PyErr_Format(PyExc_ValueError, "Unknown quickjs tag: %d", tag);
132129
}
@@ -177,8 +174,20 @@ static PyObject *context_eval(ContextData *self, PyObject *args) {
177174
return quickjs_to_python(self->context, value);
178175
}
179176

177+
static PyObject *context_get(ContextData *self, PyObject *args) {
178+
const char *name;
179+
if (!PyArg_ParseTuple(args, "s", &name)) {
180+
return NULL;
181+
}
182+
JSValue global = JS_GetGlobalObject(self->context);
183+
JSValue value = JS_GetPropertyStr(self->context, global, name);
184+
JS_FreeValue(self->context, global);
185+
return quickjs_to_python(self->context, value);
186+
}
187+
180188
static PyMethodDef context_methods[] = {
181189
{"eval", (PyCFunction)context_eval, METH_VARARGS, "Evaluates a Javascript string."},
190+
{"get", (PyCFunction)context_get, METH_VARARGS, "Gets a Javascript global variable."},
182191
{NULL} /* Sentinel */
183192
};
184193

test_quickjs.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ def test_function(self):
4747
""")
4848
self.assertEqual(self.context.eval("special(2)"), 42)
4949

50+
def test_get(self):
51+
self.context.eval("x = 42; y = 'foo';")
52+
self.assertEqual(self.context.get("x"), 42)
53+
self.assertEqual(self.context.get("y"), "foo")
54+
self.assertEqual(self.context.get("z"), None)
55+
5056
def test_error(self):
5157
with self.assertRaisesRegex(quickjs.JSException, "ReferenceError: missing is not defined"):
5258
self.context.eval("missing + missing")
@@ -80,6 +86,19 @@ def test_function_call_int_two_args(self):
8086
""")
8187
self.assertEqual(f(3, -1), 42)
8288

89+
90+
def test_function_call_many_times(self):
91+
n = 1000
92+
f = self.context.eval("""
93+
f = function(x, y) {
94+
return x + y;
95+
}
96+
""")
97+
s = 0
98+
for i in range(n):
99+
s += f(1, 1)
100+
self.assertEqual(s, 2 * n)
101+
83102
def test_function_call_str(self):
84103
f = self.context.eval("""
85104
f = function(a) {

0 commit comments

Comments
 (0)