[javascriptlint-commit] SF.net SVN: javascriptlint: [164] trunk
Status: Beta
Brought to you by:
matthiasmiller
|
From: <mat...@us...> - 2008-03-04 03:44:14
|
Revision: 164
http://javascriptlint.svn.sourceforge.net/javascriptlint/?rev=164&view=rev
Author: matthiasmiller
Date: 2008-03-03 19:42:17 -0800 (Mon, 03 Mar 2008)
Log Message:
-----------
provide an interface to SpiderMonkey to determine if a script is a compilable unit
Modified Paths:
--------------
trunk/pyjsl/jsparse.py
trunk/pyspidermonkey/pyspidermonkey.c
Modified: trunk/pyjsl/jsparse.py
===================================================================
--- trunk/pyjsl/jsparse.py 2008-03-04 03:37:22 UTC (rev 163)
+++ trunk/pyjsl/jsparse.py 2008-03-04 03:42:17 UTC (rev 164)
@@ -306,14 +306,29 @@
r = NodeRanges()
r.add(5, 10)
r.add(15, 15)
- assert not r.has(4)
- assert r.has(5)
- assert r.has(6)
- assert r.has(9)
- assert r.has(10)
- assert not r.has(14)
- assert r.has(15)
- assert not r.has(16)
+ assert not r.has(4)
+ assert r.has(5)
+ assert r.has(6)
+ assert r.has(9)
+ assert r.has(10)
+ assert not r.has(14)
+ assert r.has(15)
+ assert not r.has(16)
+
+class TestCompilableUnit(unittest.TestCase):
+ def test(self):
+ tests = (
+ ('var s = "', False),
+ ('bogon()', True),
+ ('int syntax_error;', True),
+ ('a /* b', False),
+ ('re = /.*', False),
+ ('{ // missing curly', False)
+ )
+ for text, is_compilable_unit in tests:
+ self.assertEquals(pyspidermonkey.is_compilable_unit(text),
+ is_compilable_unit)
+
if __name__ == '__main__':
unittest.main()
Modified: trunk/pyspidermonkey/pyspidermonkey.c
===================================================================
--- trunk/pyspidermonkey/pyspidermonkey.c 2008-03-04 03:37:22 UTC (rev 163)
+++ trunk/pyspidermonkey/pyspidermonkey.c 2008-03-04 03:42:17 UTC (rev 164)
@@ -52,10 +52,16 @@
static PyObject*
module_traverse(PyObject *self, PyObject *args);
+static PyObject*
+is_compilable_unit(PyObject *self, PyObject *args);
+
static PyMethodDef module_methods[] = {
{"traverse", module_traverse, METH_VARARGS,
"Parses \"script\" and calls \"push\" and \"pop\" for each node."},
+ {"is_compilable_unit", is_compilable_unit, METH_VARARGS,
+ "Returns True if \"script\" is a compilable unit."},
+
{NULL, NULL, 0, NULL} /* Sentinel */
};
@@ -432,3 +438,67 @@
return m.kids;
}
+static PyObject*
+is_compilable_unit(PyObject *self, PyObject *args) {
+ struct {
+ const char* script;
+ JSRuntime* runtime;
+ JSContext* context;
+ JSObject* global;
+ JSBool is_compilable;
+ } m;
+ const char* error;
+
+ memset(&m, 0, sizeof(m));
+ error = "encountered an unknown error";
+
+ if (!PyArg_ParseTuple(args, "s", &m.script))
+ return NULL;
+
+ m.runtime = JS_NewRuntime(8L * 1024L * 1024L);
+ if (m.runtime == NULL) {
+ error = "cannot create runtime";
+ 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;
+
+cleanup:
+ if (m.context)
+ JS_DestroyContext(m.context);
+ if (m.runtime)
+ JS_DestroyRuntime(m.runtime);
+
+ if (error) {
+ PyErr_SetString(PyExc_StandardError, error);
+ return NULL;
+ }
+ if (m.is_compilable) {
+ Py_INCREF(Py_True);
+ return Py_True;
+ }
+ else {
+ Py_INCREF(Py_False);
+ return Py_False;
+ }
+}
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|