Mercurial > pymonkey
diff utils.c @ 46:a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Mon, 06 Jul 2009 01:37:16 -0700 |
parents | 03aec8572461 |
children | 3f4982759e55 |
line wrap: on
line diff
--- a/utils.c Mon Jul 06 00:09:42 2009 -0700 +++ b/utils.c Mon Jul 06 01:37:16 2009 -0700 @@ -132,3 +132,47 @@ "Data type conversion not implemented."); return NULL; } + +void +PYM_pythonExceptionToJs(PYM_JSContextObject *context) +{ + PyObject *type; + PyObject *value; + PyObject *traceback; + + PyErr_Fetch(&type, &value, &traceback); + PyObject *str = PyObject_Unicode(value); + if (str == NULL) + JS_ReportError(context->cx, "Python exception occurred"); + else { + jsval val; + if (PYM_pyObjectToJsval(context, str, &val) == 0) + // TODO: Include filename/line information. + JS_SetPendingException(context->cx, val); + else + JS_ReportError(context->cx, "Python exception occurred"); + } + + Py_DECREF(type); + Py_DECREF(value); + Py_DECREF(traceback); +} + +void +PYM_jsExceptionToPython(PYM_JSContextObject *context) +{ + if (!JS_IsExceptionPending(context->cx) && + PyErr_Occurred()) + return; + + jsval val; + if (JS_GetPendingException(context->cx, &val)) { + JSString *str = JS_ValueToString(context->cx, val); + if (str != NULL) { + const char *chars = JS_GetStringBytes(str); + PyErr_SetString(PYM_error, chars); + } else + PyErr_SetString(PYM_error, "JS exception occurred"); + } else + PyErr_SetString(PYM_error, "JS_GetPendingException() failed"); +}