Mercurial > pymonkey
annotate pymonkey.c @ 14:9edcdb4ab12d
added an init_standard_classes() method to context objects.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sun, 28 Jun 2009 18:28:35 -0700 |
parents | ca17531e8c81 |
children | 532b7ddca616 |
rev | line source |
---|---|
10
29eaa1fceff1
Moved definition of undefined type into a separate module.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
1 #include "undefined.h" |
11
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
2 #include "runtime.h" |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
3 #include "context.h" |
13 | 4 #include "object.h" |
11
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
5 #include "utils.h" |
8
6647870380cc
Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
6 |
11
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
7 #include <jsapi.h> |
8
6647870380cc
Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
8 |
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
|
9 static JSClass PYM_jsGlobalClass = { |
0 | 10 "PymonkeyGlobal", JSCLASS_GLOBAL_FLAGS, |
11 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, | |
12 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, | |
13 JSCLASS_NO_OPTIONAL_MEMBERS | |
14 }; | |
15 | |
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
|
16 static PyObject * |
0 | 17 PYM_evaluate(PyObject *self, PyObject *args) |
18 { | |
19 const char *source; | |
20 int sourceLen; | |
21 const char *filename; | |
22 int lineNo; | |
23 | |
24 if (!PyArg_ParseTuple(args, "s#si", &source, &sourceLen, | |
25 &filename, &lineNo)) | |
26 return NULL; | |
27 | |
28 JSRuntime *rt = JS_NewRuntime(8L * 1024L * 1024L); | |
29 if (rt == NULL) { | |
30 PyErr_SetString(PYM_error, "JS_NewRuntime() failed"); | |
31 return NULL; | |
32 } | |
33 | |
34 JSContext *cx = JS_NewContext(rt, 8192); | |
35 if (cx == NULL) { | |
36 PyErr_SetString(PYM_error, "JS_NewContext() failed"); | |
37 JS_DestroyRuntime(rt); | |
38 } | |
39 | |
40 JS_SetOptions(cx, JSOPTION_VAROBJFIX); | |
41 JS_SetVersion(cx, JSVERSION_LATEST); | |
42 | |
43 JS_BeginRequest(cx); | |
44 | |
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
|
45 JSObject *global = JS_NewObject(cx, &PYM_jsGlobalClass, NULL, NULL); |
0 | 46 if (global == NULL) { |
47 PyErr_SetString(PYM_error, "JS_NewObject() failed"); | |
48 JS_EndRequest(cx); | |
49 JS_DestroyContext(cx); | |
50 JS_DestroyRuntime(rt); | |
51 return NULL; | |
52 } | |
53 | |
54 if (!JS_InitStandardClasses(cx, global)) { | |
55 PyErr_SetString(PYM_error, "JS_InitStandardClasses() failed"); | |
56 JS_EndRequest(cx); | |
57 JS_DestroyContext(cx); | |
58 JS_DestroyRuntime(rt); | |
59 return NULL; | |
60 } | |
61 | |
62 jsval rval; | |
63 if (!JS_EvaluateScript(cx, global, source, sourceLen, filename, | |
64 lineNo, &rval)) { | |
65 // TODO: Actually get the error that was raised. | |
66 PyErr_SetString(PYM_error, "Script failed"); | |
67 JS_EndRequest(cx); | |
68 JS_DestroyContext(cx); | |
69 JS_DestroyRuntime(rt); | |
70 return NULL; | |
71 } | |
72 | |
3
d6a0819ca6ca
Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
73 PyObject *pyRval = PYM_jsvalToPyObject(rval); |
d6a0819ca6ca
Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
74 |
0 | 75 JS_EndRequest(cx); |
76 JS_DestroyContext(cx); | |
77 JS_DestroyRuntime(rt); | |
78 | |
3
d6a0819ca6ca
Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
79 return pyRval; |
0 | 80 } |
81 | |
82 static PyMethodDef PYM_methods[] = { | |
83 {"evaluate", PYM_evaluate, METH_VARARGS, | |
84 "Evaluate the given JavaScript code, using the given filename " | |
85 "and line number information."}, | |
86 {NULL, NULL, 0, NULL} | |
87 }; | |
88 | |
89 PyMODINIT_FUNC | |
90 initpymonkey(void) | |
91 { | |
9
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
92 if (!JS_CStringsAreUTF8()) |
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
93 JS_SetCStringsAreUTF8(); |
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
94 |
0 | 95 PyObject *module; |
96 | |
97 module = Py_InitModule("pymonkey", PYM_methods); | |
98 if (module == NULL) | |
99 return; | |
100 | |
8
6647870380cc
Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
101 if (PyType_Ready(&PYM_undefinedType) < 0) |
6647870380cc
Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
102 return; |
6647870380cc
Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
103 |
6647870380cc
Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
104 Py_INCREF(PYM_undefined); |
6647870380cc
Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
105 PyModule_AddObject(module, "undefined", PYM_undefined); |
6647870380cc
Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
106 |
0 | 107 PYM_error = PyErr_NewException("pymonkey.error", NULL, NULL); |
108 Py_INCREF(PYM_error); | |
109 PyModule_AddObject(module, "error", PYM_error); | |
9
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
110 |
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
111 if (!PyType_Ready(&PYM_JSRuntimeType) < 0) |
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
112 return; |
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
113 |
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
114 Py_INCREF(&PYM_JSRuntimeType); |
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
115 PyModule_AddObject(module, "Runtime", (PyObject *) &PYM_JSRuntimeType); |
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
116 |
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
117 if (!PyType_Ready(&PYM_JSContextType) < 0) |
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
118 return; |
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
119 |
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
120 Py_INCREF(&PYM_JSContextType); |
032cfc448079
Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
121 PyModule_AddObject(module, "Context", (PyObject *) &PYM_JSContextType); |
13 | 122 |
123 if (!PyType_Ready(&PYM_JSObjectType) < 0) | |
124 return; | |
125 | |
126 Py_INCREF(&PYM_JSObjectType); | |
127 PyModule_AddObject(module, "Object", (PyObject *) &PYM_JSObjectType); | |
0 | 128 } |