Mercurial > pymonkey
changeset 31:d0a3f358072a
gcc now shows all warnings (-Wall).
JS-wrapped Python functions can now return integers.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Tue, 30 Jun 2009 22:28:04 -0700 |
parents | 3b2bdf2823bb |
children | abf14cba9ef5 |
files | manage.py test_pymonkey.py utils.c |
diffstat | 3 files changed, 46 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/manage.py Tue Jun 30 21:53:45 2009 -0700 +++ b/manage.py Tue Jun 30 22:28:04 2009 -0700 @@ -51,6 +51,7 @@ "-framework", "Python", "-I%s" % incdir, "-L%s" % libdir, + "-Wall", "-lmozjs", "-o", "pymonkey.so", "-dynamiclib",
--- a/test_pymonkey.py Tue Jun 30 21:53:45 2009 -0700 +++ b/test_pymonkey.py Tue Jun 30 22:28:04 2009 -0700 @@ -9,19 +9,36 @@ cx.init_standard_classes(obj) return cx.evaluate_script(obj, code, '<string>', 1) - def testJsWrappedPythonFunctionReturnsUnicode(self): + def _evalJsWrappedPyFunc(self, func, code): cx = pymonkey.Runtime().new_context() obj = cx.new_object() cx.init_standard_classes(obj) + cx.define_function(obj, func, func.__name__) + return cx.evaluate_script(obj, code, '<string>', 1) + def testJsWrappedPythonFunctionReturnsUnicode(self): def hai2u(): return u"o hai" + self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), + u"o hai") - cx.define_function(obj, hai2u, "hai2u") - self.assertEqual( - cx.evaluate_script(obj, 'hai2u()', '<string>', 1), - u"o hai" - ) + def testJsWrappedPythonFunctionReturnsSmallInt(self): + def hai2u(): + return 5 + self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), + 5) + + def testJsWrappedPythonFunctionReturnsNegativeInt(self): + def hai2u(): + return -5 + self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), + -5) + + def testJsWrappedPythonFunctionReturnsBigInt(self): + def hai2u(): + return 2147483647 + self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), + 2147483647) def testObjectIsIdentityPreserving(self): cx = pymonkey.Runtime().new_context() @@ -104,6 +121,13 @@ def testEvaluateReturnsIntegers(self): self.assertEqual(self._evaljs('1+3'), 4) + def testEvaluateReturnsNegativeIntegers(self): + self.assertEqual(self._evaljs('-5'), -5) + + def testEvaluateReturnsBigIntegers(self): + self.assertEqual(self._evaljs('2147483647*2'), + 2147483647*2) + def testEvaluateReturnsFloats(self): self.assertEqual(self._evaljs('1.1+3'), 4.1)
--- a/utils.c Tue Jun 30 21:53:45 2009 -0700 +++ b/utils.c Tue Jun 30 22:28:04 2009 -0700 @@ -24,6 +24,21 @@ } #endif + if (PyInt_Check(object)) { + long number = PyInt_AS_LONG(object); + if (INT_FITS_IN_JSVAL(number)) + *rval = INT_TO_JSVAL(number); + else { + jsdouble *numberAsJsdouble = JS_NewDouble(cx, number); + if (numberAsJsdouble == NULL) { + PyErr_SetString(PYM_error, "JS_NewDouble() failed"); + return NULL; + } + *rval = DOUBLE_TO_JSVAL(numberAsJsdouble); + } + Py_RETURN_NONE; + } + // TODO: Support more types. PyErr_SetString(PyExc_NotImplementedError, "Data type conversion not implemented.");