Mercurial > pymonkey
changeset 29:608d086d12e3
Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Tue, 30 Jun 2009 21:23:04 -0700 |
parents | bd30f5c02fc3 |
children | 3b2bdf2823bb |
files | context.c test_pymonkey.py utils.c utils.h |
diffstat | 4 files changed, 46 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/context.c Mon Jun 29 14:09:01 2009 -0700 +++ b/context.c Tue Jun 30 21:23:04 2009 -0700 @@ -40,9 +40,7 @@ static PyObject * PYM_getProperty(PYM_JSContextObject *self, PyObject *args) { - // TODO: We're making a lot of copies of the string here, which - // can't be very efficient. - +#ifndef Py_UNICODE_WIDE PYM_JSObject *object; Py_UNICODE *string; @@ -68,6 +66,12 @@ } return PYM_jsvalToPyObject(self, val); +#else + PyErr_SetString(PyExc_NotImplementedError, + "This function is not yet implemented for wide " + "unicode builds of Python."); + return NULL; +#endif } static PyObject * @@ -145,10 +149,9 @@ return JS_FALSE; } - // TODO: Convert result to JS value. + JSBool success = PYM_pyObjectToJsval(cx, result, rval); Py_DECREF(result); - - return JS_TRUE; + return success; } static PyObject *
--- a/test_pymonkey.py Mon Jun 29 14:09:01 2009 -0700 +++ b/test_pymonkey.py Tue Jun 30 21:23:04 2009 -0700 @@ -9,19 +9,19 @@ cx.init_standard_classes(obj) return cx.evaluate_script(obj, code, '<string>', 1) - def testDefineFunctionWorks(self): + def testJsWrappedPythonFunctionReturnsUnicode(self): cx = pymonkey.Runtime().new_context() obj = cx.new_object() cx.init_standard_classes(obj) - result = {'wasCalled': False} - def hai2u(): - result['wasCalled'] = True + return u"o hai" cx.define_function(obj, hai2u, "hai2u") - cx.evaluate_script(obj, 'hai2u()', '<string>', 1) - self.assertTrue(result['wasCalled']) + self.assertEqual( + cx.evaluate_script(obj, 'hai2u()', '<string>', 1), + u"o hai" + ) def testObjectIsIdentityPreserving(self): cx = pymonkey.Runtime().new_context()
--- a/utils.c Mon Jun 29 14:09:01 2009 -0700 +++ b/utils.c Tue Jun 30 21:23:04 2009 -0700 @@ -4,6 +4,31 @@ PyObject *PYM_error; +JSBool +PYM_pyObjectToJsval(JSContext *cx, + PyObject *object, + jsval *rval) +{ +#ifndef Py_UNICODE_WIDE + if (PyUnicode_Check(object)) { + Py_UNICODE *string = PyUnicode_AsUnicode(object); + JSString *jsString = JS_NewUCStringCopyZ(cx, + (const jschar *) string); + if (jsString == NULL) { + JS_ReportError(cx, "JS_NewUCStringCopyZ() failed"); + return JS_FALSE; + } + + *rval = STRING_TO_JSVAL(jsString); + return JS_TRUE; + } +#endif + + // TODO: Support more types. + JS_ReportError(cx, "Data type conversion not implemented."); + return JS_FALSE; +} + PyObject * PYM_jsvalToPyObject(PYM_JSContextObject *context, jsval value) { @@ -49,4 +74,5 @@ // TODO: Support more types. PyErr_SetString(PyExc_NotImplementedError, "Data type conversion not implemented."); + return NULL; }
--- a/utils.h Mon Jun 29 14:09:01 2009 -0700 +++ b/utils.h Tue Jun 30 21:23:04 2009 -0700 @@ -14,6 +14,11 @@ extern PyObject *PYM_error; +extern JSBool +PYM_pyObjectToJsval(JSContext *cx, + PyObject *object, + jsval *rval); + extern PyObject * PYM_jsvalToPyObject(PYM_JSContextObject *context, jsval value);