[javascriptlint-commit] SF.net SVN: javascriptlint:[280] trunk/javascriptlint/pyspidermonkey/ pyspi
Status: Beta
Brought to you by:
matthiasmiller
From: <mat...@us...> - 2009-10-22 06:55:31
|
Revision: 280 http://javascriptlint.svn.sourceforge.net/javascriptlint/?rev=280&view=rev Author: matthiasmiller Date: 2009-10-22 06:55:23 +0000 (Thu, 22 Oct 2009) Log Message: ----------- Refactor pyspidermonkey's code to load JS runtime and context. Modified Paths: -------------- trunk/javascriptlint/pyspidermonkey/pyspidermonkey.c Modified: trunk/javascriptlint/pyspidermonkey/pyspidermonkey.c =================================================================== --- trunk/javascriptlint/pyspidermonkey/pyspidermonkey.c 2009-10-22 05:09:12 UTC (rev 279) +++ trunk/javascriptlint/pyspidermonkey/pyspidermonkey.c 2009-10-22 06:55:23 UTC (rev 280) @@ -382,6 +382,36 @@ return NULL; } +/* Returns NULL on success. Otherwise, it returns an error. + * If the error is blank, an exception will be set. + */ +static const char* create_jscontext(void* ctx_data, + JSRuntime** runtime, JSContext** context, + JSObject** global) +{ + *runtime = JS_NewRuntime(8L * 1024L * 1024L); + if (*runtime == NULL) + return"cannot create runtime"; + + *context = JS_NewContext(*runtime, 8192); + if (*context == NULL) + return "cannot create context"; + + JS_SetErrorReporter(*context, error_reporter); + JS_SetContextPrivate(*context, ctx_data); + JS_ToggleOptions(*context, JSOPTION_STRICT); + + *global = JS_NewObject(*context, NULL, NULL, NULL); + if (*global == NULL) + return "cannot create global object"; + + if (!JS_InitStandardClasses(*context, *global)) + return "cannot initialize standard classes"; + + return NULL; +} + + static PyObject* module_parse(PyObject *self, PyObject *args) { struct { @@ -419,38 +449,16 @@ return NULL; } - m.runtime = JS_NewRuntime(8L * 1024L * 1024L); - if (m.runtime == NULL) { - error = "cannot create runtime"; + error = create_jscontext(&m.ctx_data, &m.runtime, &m.context, &m.global); + if (error) goto cleanup; - } - m.context = JS_NewContext(m.runtime, 8192); - if (m.context == NULL) { - error = "cannot create context"; - goto cleanup; - } - JS_SetErrorReporter(m.context, error_reporter); - JS_SetContextPrivate(m.context, &m.ctx_data); - JS_ToggleOptions(m.context, JSOPTION_STRICT); - m.contents = JS_NewStringCopyZ(m.context, m.script); if (m.contents == NULL) { error = "cannot create script contents"; goto cleanup; } - m.global = JS_NewObject(m.context, NULL, NULL, NULL); - if (m.global == NULL) { - error = "cannot create global object"; - goto cleanup; - } - - if (!JS_InitStandardClasses(m.context, m.global)) { - error = "cannot initialize standard classes"; - goto cleanup; - } - m.token_stream = js_NewBufferTokenStream(m.context, JS_GetStringChars(m.contents), JS_GetStringLength(m.contents)); if (!m.token_stream) { error = "cannot create token stream"; @@ -505,29 +513,10 @@ if (!PyArg_ParseTuple(args, "s", &m.script)) return NULL; - m.runtime = JS_NewRuntime(8L * 1024L * 1024L); - if (m.runtime == NULL) { - error = "cannot create runtime"; + error = create_jscontext(NULL, &m.runtime, &m.context, &m.global); + if (error) goto cleanup; - } - m.context = JS_NewContext(m.runtime, 8192); - if (m.context == NULL) { - error = "cannot create context"; - goto cleanup; - } - - m.global = JS_NewObject(m.context, NULL, NULL, NULL); - if (m.global == NULL) { - error = "cannot create global object"; - goto cleanup; - } - - if (!JS_InitStandardClasses(m.context, m.global)) { - error = "cannot initialize standard classes"; - goto cleanup; - } - m.is_compilable = JS_BufferIsCompilableUnit(m.context, m.global, m.script, strlen(m.script)); error = NULL; @@ -539,7 +528,8 @@ JS_DestroyRuntime(m.runtime); if (error) { - PyErr_SetString(PyExc_StandardError, error); + if (*error) + PyErr_SetString(PyExc_StandardError, error); return NULL; } if (m.is_compilable) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |