Permalink
Browse files

Allow setting the filename and lineno in execute()

This should not only make tracebacks better, but also allow Python
to open the JavaScript file so as to show the correct line (of
course, given the filename can be found.)
  • Loading branch information...
lericson committed Oct 5, 2010
1 parent a2084f2 commit b498a981063596b85e4d9e05c5d5354e691d8445
Showing with 16 additions and 9 deletions.
  1. +11 −4 spidermonkey/context.c
  2. +5 −5 spidermonkey/error.c
@@ -546,11 +546,18 @@ Context_execute(Context* self, PyObject* args, PyObject* kwargs)
JSString* script = NULL;
jschar* schars = NULL;
JSBool started_counter = JS_FALSE;
char *fname = "<anonymous JavaScript>";
unsigned int lineno = 1;
size_t slen;
jsval rval;

char *keywords[] = {"code", "filename", "lineno", NULL};

if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O|sI", keywords,
&obj, &fname, &lineno))
goto error;

JS_BeginRequest(self->cx);
if(!PyArg_ParseTuple(args, "O", &obj)) goto error;

script = py2js_string_obj(self, obj);
if(script == NULL) goto error;
@@ -568,11 +575,11 @@ Context_execute(Context* self, PyObject* args, PyObject* kwargs)
self->start_time = time(NULL);
}

if(!JS_EvaluateUCScript(cx, root, schars, slen, "<JavaScript>", 1, &rval))
if(!JS_EvaluateUCScript(cx, root, schars, slen, fname, lineno, &rval))
{
if(!PyErr_Occurred())
{
PyErr_SetString(PyExc_RuntimeError, "Failed to execute script.");
PyErr_SetString(PyExc_RuntimeError, "Script execution failed and no exception was set");
}
goto error;
}
@@ -666,7 +673,7 @@ static PyMethodDef Context_methods[] = {
{
"execute",
(PyCFunction)Context_execute,
METH_VARARGS,
METH_VARARGS | METH_KEYWORDS,
"Execute JavaScript source code."
},
{
@@ -78,13 +78,13 @@ report_error_cb(JSContext* cx, const char* message, JSErrorReport* report)
const char* srcfile = report->filename;
const char* mesg = message;

if(srcfile == NULL) srcfile = "<JavaScript>";
if(mesg == NULL) mesg = "Unknown JavaScript execution error";

if(!PyErr_Occurred())
{
PyErr_SetString(JSError, "Error executing JavaScript.");
PyErr_SetString(JSError, message);
}

if(srcfile == NULL) srcfile = "<JavaScript>";
if(mesg == NULL) mesg = "<Unknown Error>";

add_frame(srcfile, mesg, report->lineno);
add_frame(srcfile, "JavaScript code", report->lineno);
}

0 comments on commit b498a98

Please sign in to comment.