Mercurial > pymonkey
annotate pymonkey.c @ 4:2711b636f8e6
Added support for NULL return values.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sun, 28 Jun 2009 12:43:02 -0700 |
parents | d6a0819ca6ca |
children | aae78eac86d6 |
rev | line source |
---|---|
0 | 1 #include "jsapi.h" |
2 #include <Python/Python.h> | |
3 | |
2
2f6bdc9f7245
pymonkey.evaluate() now returns a python-ized version of the JS return value, though it only supports integers at the moment.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
4 static JSClass PYM_jsGlobalClass = { |
0 | 5 "PymonkeyGlobal", JSCLASS_GLOBAL_FLAGS, |
6 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, | |
7 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, | |
8 JSCLASS_NO_OPTIONAL_MEMBERS | |
9 }; | |
10 | |
11 static PyObject *PYM_error; | |
12 | |
13 static PyObject * | |
2
2f6bdc9f7245
pymonkey.evaluate() now returns a python-ized version of the JS return value, though it only supports integers at the moment.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
14 PYM_jsvalToPyObject(jsval value) { |
2f6bdc9f7245
pymonkey.evaluate() now returns a python-ized version of the JS return value, though it only supports integers at the moment.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
15 if (JSVAL_IS_INT(value)) |
2f6bdc9f7245
pymonkey.evaluate() now returns a python-ized version of the JS return value, though it only supports integers at the moment.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
16 return PyInt_FromLong(JSVAL_TO_INT(value)); |
2f6bdc9f7245
pymonkey.evaluate() now returns a python-ized version of the JS return value, though it only supports integers at the moment.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
17 |
3
d6a0819ca6ca
Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
18 if (JSVAL_IS_DOUBLE(value)) { |
d6a0819ca6ca
Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
19 jsdouble *doubleRef = JSVAL_TO_DOUBLE(value); |
d6a0819ca6ca
Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
20 return PyFloat_FromDouble(*doubleRef); |
d6a0819ca6ca
Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
21 } |
d6a0819ca6ca
Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
22 |
4
2711b636f8e6
Added support for NULL return values.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
23 if (JSVAL_IS_NULL(value)) { |
2711b636f8e6
Added support for NULL return values.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
24 Py_INCREF(Py_None); |
2711b636f8e6
Added support for NULL return values.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
25 return Py_None; |
2711b636f8e6
Added support for NULL return values.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
26 } |
2
2f6bdc9f7245
pymonkey.evaluate() now returns a python-ized version of the JS return value, though it only supports integers at the moment.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
27 |
4
2711b636f8e6
Added support for NULL return values.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
28 // TODO: Support more types. |
2711b636f8e6
Added support for NULL return values.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
29 PyErr_SetString(PyExc_NotImplementedError, |
2711b636f8e6
Added support for NULL return values.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
30 "Data type conversion not implemented."); |
2
2f6bdc9f7245
pymonkey.evaluate() now returns a python-ized version of the JS return value, though it only supports integers at the moment.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
31 } |
2f6bdc9f7245
pymonkey.evaluate() now returns a python-ized version of the JS return value, though it only supports integers at the moment.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
32 |
2f6bdc9f7245
pymonkey.evaluate() now returns a python-ized version of the JS return value, though it only supports integers at the moment.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
33 static PyObject * |
0 | 34 PYM_evaluate(PyObject *self, PyObject *args) |
35 { | |
36 const char *source; | |
37 int sourceLen; | |
38 const char *filename; | |
39 int lineNo; | |
40 | |
41 if (!PyArg_ParseTuple(args, "s#si", &source, &sourceLen, | |
42 &filename, &lineNo)) | |
43 return NULL; | |
44 | |
45 JSRuntime *rt = JS_NewRuntime(8L * 1024L * 1024L); | |
46 if (rt == NULL) { | |
47 PyErr_SetString(PYM_error, "JS_NewRuntime() failed"); | |
48 return NULL; | |
49 } | |
50 | |
51 JSContext *cx = JS_NewContext(rt, 8192); | |
52 if (cx == NULL) { | |
53 PyErr_SetString(PYM_error, "JS_NewContext() failed"); | |
54 JS_DestroyRuntime(rt); | |
55 } | |
56 | |
57 JS_SetOptions(cx, JSOPTION_VAROBJFIX); | |
58 JS_SetVersion(cx, JSVERSION_LATEST); | |
59 | |
60 JS_BeginRequest(cx); | |
61 | |
2
2f6bdc9f7245
pymonkey.evaluate() now returns a python-ized version of the JS return value, though it only supports integers at the moment.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
62 JSObject *global = JS_NewObject(cx, &PYM_jsGlobalClass, NULL, NULL); |
0 | 63 if (global == NULL) { |
64 PyErr_SetString(PYM_error, "JS_NewObject() failed"); | |
65 JS_EndRequest(cx); | |
66 JS_DestroyContext(cx); | |
67 JS_DestroyRuntime(rt); | |
68 return NULL; | |
69 } | |
70 | |
71 if (!JS_InitStandardClasses(cx, global)) { | |
72 PyErr_SetString(PYM_error, "JS_InitStandardClasses() failed"); | |
73 JS_EndRequest(cx); | |
74 JS_DestroyContext(cx); | |
75 JS_DestroyRuntime(rt); | |
76 return NULL; | |
77 } | |
78 | |
79 jsval rval; | |
80 if (!JS_EvaluateScript(cx, global, source, sourceLen, filename, | |
81 lineNo, &rval)) { | |
82 // TODO: Actually get the error that was raised. | |
83 PyErr_SetString(PYM_error, "Script failed"); | |
84 JS_EndRequest(cx); | |
85 JS_DestroyContext(cx); | |
86 JS_DestroyRuntime(rt); | |
87 return NULL; | |
88 } | |
89 | |
3
d6a0819ca6ca
Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
90 PyObject *pyRval = PYM_jsvalToPyObject(rval); |
d6a0819ca6ca
Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
91 |
0 | 92 JS_EndRequest(cx); |
93 JS_DestroyContext(cx); | |
94 JS_DestroyRuntime(rt); | |
95 | |
3
d6a0819ca6ca
Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
96 return pyRval; |
0 | 97 } |
98 | |
99 static PyMethodDef PYM_methods[] = { | |
100 {"evaluate", PYM_evaluate, METH_VARARGS, | |
101 "Evaluate the given JavaScript code, using the given filename " | |
102 "and line number information."}, | |
103 {NULL, NULL, 0, NULL} | |
104 }; | |
105 | |
106 PyMODINIT_FUNC | |
107 initpymonkey(void) | |
108 { | |
109 PyObject *module; | |
110 | |
111 module = Py_InitModule("pymonkey", PYM_methods); | |
112 if (module == NULL) | |
113 return; | |
114 | |
115 PYM_error = PyErr_NewException("pymonkey.error", NULL, NULL); | |
116 Py_INCREF(PYM_error); | |
117 PyModule_AddObject(module, "error", PYM_error); | |
118 } |