Mercurial > pymonkey
changeset 43:5727675b1bcb
JS-wrapped python functions now take a context object as their first parameter.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sun, 05 Jul 2009 23:52:14 -0700 |
parents | e62b1801f9af |
children | 0b9a316ce4ef |
files | context.c function.c test_pymonkey.py |
diffstat | 3 files changed, 30 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/context.c Fri Jul 03 21:15:18 2009 -0700 +++ b/context.c Sun Jul 05 23:52:14 2009 -0700 @@ -287,6 +287,7 @@ Py_INCREF(runtime); context->cx = cx; + JS_SetContextPrivate(cx, context); #ifdef JS_GC_ZEAL // TODO: Consider exposing JS_SetGCZeal() to Python instead of
--- a/function.c Fri Jul 03 21:15:18 2009 -0700 +++ b/function.c Sun Jul 05 23:52:14 2009 -0700 @@ -29,12 +29,17 @@ PyObject *callable = (PyObject *) JSVAL_TO_PRIVATE(jsCallable); // TODO: Convert args and 'this' parameter. - PyObject *args = PyTuple_New(0); + PyObject *args = PyTuple_New(1); if (args == NULL) { JS_ReportOutOfMemory(cx); return JS_FALSE; } + PYM_JSContextObject *context = (PYM_JSContextObject *) + JS_GetContextPrivate(cx); + Py_INCREF(context); + PyTuple_SET_ITEM(args, 0, (PyObject *) context); + PyObject *result = PyObject_Call(callable, args, NULL); if (result == NULL) { // TODO: Get the actual exception.
--- a/test_pymonkey.py Fri Jul 03 21:15:18 2009 -0700 +++ b/test_pymonkey.py Sun Jul 05 23:52:14 2009 -0700 @@ -17,44 +17,60 @@ cx.define_property(obj, func.__name__, jsfunc) return cx.evaluate_script(obj, code, '<string>', 1) + def testJsWrappedPythonFuncPassesContext(self): + contexts = [] + + def func(cx): + contexts.append(cx) + return True + + code = "func()" + cx = pymonkey.Runtime().new_context() + obj = cx.new_object() + cx.init_standard_classes(obj) + jsfunc = cx.new_function(func, func.__name__) + cx.define_property(obj, func.__name__, jsfunc) + cx.evaluate_script(obj, code, '<string>', 1) + self.assertEqual(contexts[0], cx) + def testJsWrappedPythonFunctionReturnsUnicode(self): - def hai2u(): + def hai2u(cx): return u"o hai" self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), u"o hai") def testJsWrappedPythonFunctionReturnsTrue(self): - def hai2u(): + def hai2u(cx): return True self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), True) def testJsWrappedPythonFunctionReturnsFalse(self): - def hai2u(): + def hai2u(cx): return False self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), False) def testJsWrappedPythonFunctionReturnsSmallInt(self): - def hai2u(): + def hai2u(cx): return 5 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), 5) def testJsWrappedPythonFunctionReturnsFloat(self): - def hai2u(): + def hai2u(cx): return 5.1 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), 5.1) def testJsWrappedPythonFunctionReturnsNegativeInt(self): - def hai2u(): + def hai2u(cx): return -5 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), -5) def testJsWrappedPythonFunctionReturnsBigInt(self): - def hai2u(): + def hai2u(cx): return 2147483647 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), 2147483647)