diff 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
line wrap: on
line diff
--- a/context.c	Sun Jul 26 13:09:58 2009 -0700
+++ b/context.c	Sun Jul 26 15:06:19 2009 -0700
@@ -42,6 +42,7 @@
 static JSBool
 PYM_operationCallback(JSContext *cx)
 {
+  PYM_PyAutoEnsureGIL gil;
   PYM_JSContextObject *context = (PYM_JSContextObject *)
     JS_GetContextPrivate(cx);
 
@@ -66,6 +67,8 @@
 static void
 PYM_reportError(JSContext *cx, const char *message, JSErrorReport *report)
 {
+  PYM_PyAutoEnsureGIL gil;
+
   // Convert JS warnings into Python warnings.
   if (JSREPORT_IS_WARNING(report->flags)) {
     if (report->filename)
@@ -138,9 +141,14 @@
   }
 
   jsval val;
-  if (!JS_GetUCProperty(self->cx, object->obj,
-                        JS_GetStringChars(jsString),
-                        JS_GetStringLength(jsString), &val)) {
+  JSBool result;
+  Py_BEGIN_ALLOW_THREADS;
+  result = JS_GetUCProperty(self->cx, object->obj,
+                            JS_GetStringChars(jsString),
+                            JS_GetStringLength(jsString), &val);
+  Py_END_ALLOW_THREADS;
+
+  if (!result) {
     // TODO: Get the actual JS exception. Any exception that exists
     // here will probably still be pending on the JS context.
     PyErr_SetString(PYM_error, "Getting property failed.");
@@ -195,8 +203,13 @@
   JS_BeginRequest(self->cx);
 
   jsval rval;
-  if (!JS_EvaluateScript(self->cx, object->obj, source, sourceLen,
-                         filename, lineNo, &rval)) {
+  JSBool result;
+  Py_BEGIN_ALLOW_THREADS;
+  result = JS_EvaluateScript(self->cx, object->obj, source, sourceLen,
+                             filename, lineNo, &rval);
+  Py_END_ALLOW_THREADS;
+
+  if (!result) {
     PYM_jsExceptionToPython(self);
     JS_EndRequest(self->cx);
     return NULL;
@@ -266,8 +279,14 @@
   // TODO: This assumes that a JSFunction * is actually a subclass of
   // a JSObject *, which may or may not be regarded as an implementation
   // detail.
-  if (!JS_CallFunction(self->cx, obj->obj, (JSFunction *) fun->base.obj,
-                       argc, argv, &rval)) {
+  JSBool result;
+  Py_BEGIN_ALLOW_THREADS;
+  result = JS_CallFunction(self->cx, obj->obj,
+                           (JSFunction *) fun->base.obj,
+                           argc, argv, &rval);
+  Py_END_ALLOW_THREADS;
+
+  if (!result) {
     PYM_jsExceptionToPython(self);
     return NULL;
   }