comparison context.c @ 66:b49180c39d0a

Pymonkey now handles the GIL properly so that Python code can run while JS code does.
author Atul Varma <varmaa@toolness.com>
date Sun, 26 Jul 2009 15:06:19 -0700
parents fb7e11dec538
children b5160c82be65
comparison
equal deleted inserted replaced
65:f89a582c1276 66:b49180c39d0a
40 #include "utils.h" 40 #include "utils.h"
41 41
42 static JSBool 42 static JSBool
43 PYM_operationCallback(JSContext *cx) 43 PYM_operationCallback(JSContext *cx)
44 { 44 {
45 PYM_PyAutoEnsureGIL gil;
45 PYM_JSContextObject *context = (PYM_JSContextObject *) 46 PYM_JSContextObject *context = (PYM_JSContextObject *)
46 JS_GetContextPrivate(cx); 47 JS_GetContextPrivate(cx);
47 48
48 PyObject *callable = context->opCallback; 49 PyObject *callable = context->opCallback;
49 PyObject *args = PyTuple_Pack(1, (PyObject *) context); 50 PyObject *args = PyTuple_Pack(1, (PyObject *) context);
64 65
65 // This is the default JSErrorReporter for pymonkey-owned JS contexts. 66 // This is the default JSErrorReporter for pymonkey-owned JS contexts.
66 static void 67 static void
67 PYM_reportError(JSContext *cx, const char *message, JSErrorReport *report) 68 PYM_reportError(JSContext *cx, const char *message, JSErrorReport *report)
68 { 69 {
70 PYM_PyAutoEnsureGIL gil;
71
69 // Convert JS warnings into Python warnings. 72 // Convert JS warnings into Python warnings.
70 if (JSREPORT_IS_WARNING(report->flags)) { 73 if (JSREPORT_IS_WARNING(report->flags)) {
71 if (report->filename) 74 if (report->filename)
72 PyErr_WarnExplicit(NULL, message, report->filename, report->lineno, 75 PyErr_WarnExplicit(NULL, message, report->filename, report->lineno,
73 NULL, NULL); 76 NULL, NULL);
136 PyErr_SetString(PYM_error, "JS_NewStringCopyZ() failed"); 139 PyErr_SetString(PYM_error, "JS_NewStringCopyZ() failed");
137 return NULL; 140 return NULL;
138 } 141 }
139 142
140 jsval val; 143 jsval val;
141 if (!JS_GetUCProperty(self->cx, object->obj, 144 JSBool result;
142 JS_GetStringChars(jsString), 145 Py_BEGIN_ALLOW_THREADS;
143 JS_GetStringLength(jsString), &val)) { 146 result = JS_GetUCProperty(self->cx, object->obj,
147 JS_GetStringChars(jsString),
148 JS_GetStringLength(jsString), &val);
149 Py_END_ALLOW_THREADS;
150
151 if (!result) {
144 // TODO: Get the actual JS exception. Any exception that exists 152 // TODO: Get the actual JS exception. Any exception that exists
145 // here will probably still be pending on the JS context. 153 // here will probably still be pending on the JS context.
146 PyErr_SetString(PYM_error, "Getting property failed."); 154 PyErr_SetString(PYM_error, "Getting property failed.");
147 return NULL; 155 return NULL;
148 } 156 }
193 return NULL; 201 return NULL;
194 202
195 JS_BeginRequest(self->cx); 203 JS_BeginRequest(self->cx);
196 204
197 jsval rval; 205 jsval rval;
198 if (!JS_EvaluateScript(self->cx, object->obj, source, sourceLen, 206 JSBool result;
199 filename, lineNo, &rval)) { 207 Py_BEGIN_ALLOW_THREADS;
208 result = JS_EvaluateScript(self->cx, object->obj, source, sourceLen,
209 filename, lineNo, &rval);
210 Py_END_ALLOW_THREADS;
211
212 if (!result) {
200 PYM_jsExceptionToPython(self); 213 PYM_jsExceptionToPython(self);
201 JS_EndRequest(self->cx); 214 JS_EndRequest(self->cx);
202 return NULL; 215 return NULL;
203 } 216 }
204 217
264 jsval rval; 277 jsval rval;
265 278
266 // TODO: This assumes that a JSFunction * is actually a subclass of 279 // TODO: This assumes that a JSFunction * is actually a subclass of
267 // a JSObject *, which may or may not be regarded as an implementation 280 // a JSObject *, which may or may not be regarded as an implementation
268 // detail. 281 // detail.
269 if (!JS_CallFunction(self->cx, obj->obj, (JSFunction *) fun->base.obj, 282 JSBool result;
270 argc, argv, &rval)) { 283 Py_BEGIN_ALLOW_THREADS;
284 result = JS_CallFunction(self->cx, obj->obj,
285 (JSFunction *) fun->base.obj,
286 argc, argv, &rval);
287 Py_END_ALLOW_THREADS;
288
289 if (!result) {
271 PYM_jsExceptionToPython(self); 290 PYM_jsExceptionToPython(self);
272 return NULL; 291 return NULL;
273 } 292 }
274 293
275 return PYM_jsvalToPyObject(self, rval); 294 return PYM_jsvalToPyObject(self, rval);