[gjs/wip/js24: 23/29] compartments are compulsory now, we need to add AutoCompartment calls at all entry points



commit 1f1721390b621791a366837f48861c1edf83249c
Author: Tim Lunn <tim feathertop org>
Date:   Mon Sep 30 16:00:17 2013 +1000

    compartments are compulsory now, we need to add AutoCompartment calls at all entry points

 gi/closure.cpp     |    3 +++
 gi/function.cpp    |    3 +++
 gi/object.cpp      |    8 ++++++++
 gi/value.cpp       |    4 ++++
 gjs/context.cpp    |    3 +++
 gjs/jsapi-util.cpp |    5 +++++
 gjs/stack.cpp      |    4 ++++
 test/gjs-tests.cpp |   34 +++++++++++++++++++++++++++++++++-
 8 files changed, 63 insertions(+), 1 deletions(-)
---
diff --git a/gi/closure.cpp b/gi/closure.cpp
index 68b3ff5..4058c26 100644
--- a/gi/closure.cpp
+++ b/gi/closure.cpp
@@ -254,6 +254,7 @@ gjs_closure_invoke(GClosure *closure,
 {
     Closure *c;
     JSContext *context;
+    JSObject *global;
 
     c = (Closure*) closure;
 
@@ -267,6 +268,8 @@ gjs_closure_invoke(GClosure *closure,
 
     context = gjs_runtime_get_context(c->runtime);
     JS_BeginRequest(context);
+    global = JS_GetGlobalObject(context);
+    JSAutoCompartment ac(context, global);
 
     if (JS_IsExceptionPending(context)) {
         gjs_debug_closure("Exception was pending before invoking callback??? "
diff --git a/gi/function.cpp b/gi/function.cpp
index 6c5d2e0..87bd4da 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -169,6 +169,7 @@ gjs_callback_closure(ffi_cif *cif,
                      void *data)
 {
     JSContext *context;
+    JSObject *global;
     GjsCallbackTrampoline *trampoline;
     int i, n_args, n_jsargs, n_outargs;
     jsval *jsargs, rval;
@@ -183,6 +184,8 @@ gjs_callback_closure(ffi_cif *cif,
 
     context = gjs_runtime_get_context(trampoline->runtime);
     JS_BeginRequest(context);
+    global = JS_GetGlobalObject(context);
+    JSAutoCompartment ac(context, global);
 
     n_args = g_callable_info_get_n_args(trampoline->info);
 
diff --git a/gi/object.cpp b/gi/object.cpp
index b22358f..eb78b09 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1870,6 +1870,8 @@ gjs_define_object_class(JSContext      *context,
     JSObject *prototype;
     JSObject *constructor;
     JSObject *parent_proto;
+    JSObject *global;
+
     jsval value;
     ObjectInstance *priv;
     const char *ns;
@@ -1934,6 +1936,9 @@ gjs_define_object_class(JSContext      *context,
         constructor_name = g_type_name(gtype);
     }
 
+    global = JS_GetGlobalObject(context);
+    JSAutoCompartment ac(context, global);
+
     if (!gjs_init_class_dynamic(context, in_object,
                                 parent_proto,
                                 ns, constructor_name,
@@ -1993,6 +1998,7 @@ gjs_object_from_g_object(JSContext    *context,
                          GObject      *gobj)
 {
     JSObject *obj;
+    JSObject *global;
 
     if (gobj == NULL)
         return NULL;
@@ -2012,6 +2018,8 @@ gjs_object_from_g_object(JSContext    *context,
         proto = gjs_lookup_object_prototype(context, gtype);
 
         JS_BeginRequest(context);
+        global = JS_GetGlobalObject(context);
+        JSAutoCompartment ac(context, global);
 
         obj = JS_NewObjectWithGivenProto(context,
                                          JS_GetClass(proto), proto,
diff --git a/gi/value.cpp b/gi/value.cpp
index 4f2426f..803e5bb 100644
--- a/gi/value.cpp
+++ b/gi/value.cpp
@@ -58,6 +58,8 @@ closure_marshal(GClosure        *closure,
 {
     JSRuntime *runtime;
     JSContext *context;
+    JSObject *global;
+
     int argc;
     jsval *argv;
     jsval rval;
@@ -76,6 +78,8 @@ closure_marshal(GClosure        *closure,
     runtime = gjs_closure_get_runtime(closure);
     context = gjs_runtime_get_context(runtime);
     JS_BeginRequest(context);
+    global = JS_GetGlobalObject(context);
+    JSAutoCompartment ac(context, global);
 
     argc = n_param_values;
     rval = JSVAL_VOID;
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 95a788d..0d07e01 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -621,6 +621,7 @@ gjs_context_constructor (GType                  type,
     if (!gjs_init_context_standard(js_context->context))
         g_error("Failed to initialize context");
     js_context->global = JS_GetGlobalObject(js_context->context);
+    JSAutoCompartment ac(js_context->context, js_context->global);
 
     if (!JS_DefineProperty(js_context->context, js_context->global,
                            "window", OBJECT_TO_JSVAL(js_context->global),
@@ -1015,6 +1016,7 @@ gjs_context_eval(GjsContext *js_context,
     retval = JSVAL_VOID;
     if (script_len < 0)
         script_len = strlen(script);
+    JSAutoCompartment ac(js_context->context, js_context->global);
     if (!JS_EvaluateScript(js_context->context,
                            js_context->global,
                            script,
@@ -1096,6 +1098,7 @@ gjs_context_define_string_array(GjsContext  *js_context,
                                 const char   **array_values,
                                 GError       **error)
 {
+    JSAutoCompartment ac(js_context->context, js_context->global);
     if (!gjs_define_string_array(js_context->context,
                                  js_context->global,
                                  array_name, array_length, array_values,
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 98cf0a8..031e7f7 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -95,6 +95,11 @@ gjs_init_context_standard (JSContext       *context)
     global = JS_NewGlobalObject(context, &global_class, NULL);
     if (global == NULL)
         return FALSE;
+
+    /* Set the context's global */
+    JSAutoCompartment ac(context, global);
+    JS_SetGlobalObject(context, global);
+
     if (!JS_InitStandardClasses(context, global))
         return FALSE;
 
diff --git a/gjs/stack.cpp b/gjs/stack.cpp
index dcf4380..bba724b 100644
--- a/gjs/stack.cpp
+++ b/gjs/stack.cpp
@@ -55,9 +55,12 @@ gjs_context_get_frame_info (JSContext  *context,
 {
     jsval v_constructor;
     JSObject *err_obj;
+    JSObject *global;
     JSBool ret = JS_FALSE;
 
     JS_BeginRequest(context);
+    global = JS_GetGlobalObject(context);
+    JSAutoCompartment ac(context, global);
 
     if (!JS_GetProperty(context, JS_GetGlobalObject(context),
                         "Error", &v_constructor) ||
@@ -97,6 +100,7 @@ void
 gjs_context_print_stack_stderr(GjsContext *context)
 {
     JSContext *cx = (JSContext*) gjs_context_get_native_context(context);
+
     jsval v_stack;
     char *stack;
 
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index 26b5ece..1fda57a 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -90,6 +90,23 @@ gjstest_test_func_gjs_context_construct_eval(void)
     g_object_unref (context);
 }
 
+static void
+gjstest_test_func_gjs_context_fixture(void)
+{
+    GjsUnitTestFixture fixture;
+    JSContext *context;
+    JSObject *global;
+
+    _gjs_unit_test_fixture_begin(&fixture);
+    context = fixture.context;
+
+    global = JS_GetGlobalObject(context);
+    JSCompartment *oldCompartment = JS_EnterCompartment(context, global);
+
+    JS_LeaveCompartment(context, oldCompartment);
+    _gjs_unit_test_fixture_finish(&fixture);
+}
+
 #define N_ELEMS 15
 
 static void
@@ -97,6 +114,7 @@ gjstest_test_func_gjs_jsapi_util_array(void)
 {
     GjsUnitTestFixture fixture;
     JSContext *context;
+    JSObject *global;
     GjsRootedArray *array;
     int i;
     jsval value;
@@ -106,6 +124,9 @@ gjstest_test_func_gjs_jsapi_util_array(void)
 
     array = gjs_rooted_array_new();
 
+    global = JS_GetGlobalObject(context);
+    JSCompartment *oldCompartment = JS_EnterCompartment(context, global);
+
     for (i = 0; i < N_ELEMS; i++) {
         value = STRING_TO_JSVAL(JS_NewStringCopyZ(context, "abcdefghijk"));
         gjs_rooted_array_append(context, array, value);
@@ -125,6 +146,7 @@ gjstest_test_func_gjs_jsapi_util_array(void)
 
     gjs_rooted_array_free(context, array, TRUE);
 
+    JS_LeaveCompartment(context, oldCompartment);
     _gjs_unit_test_fixture_finish(&fixture);
 }
 
@@ -135,17 +157,22 @@ gjstest_test_func_gjs_jsapi_util_string_js_string_utf8(void)
 {
     GjsUnitTestFixture fixture;
     JSContext *context;
+    JSObject *global;
+
     const char *utf8_string = "\303\211\303\226 foobar \343\203\237";
     char *utf8_result;
     jsval js_string;
 
     _gjs_unit_test_fixture_begin(&fixture);
     context = fixture.context;
+    global = JS_GetGlobalObject(context);
+    JSCompartment *oldCompartment = JS_EnterCompartment(context, global);
 
     g_assert(gjs_string_from_utf8(context, utf8_string, -1, &js_string) == JS_TRUE);
     g_assert(JSVAL_IS_STRING(js_string));
     g_assert(gjs_string_to_utf8(context, js_string, &utf8_result) == JS_TRUE);
 
+    JS_LeaveCompartment(context, oldCompartment);
     _gjs_unit_test_fixture_finish(&fixture);
 
     g_assert(g_str_equal(utf8_string, utf8_result));
@@ -163,6 +190,7 @@ gjstest_test_func_gjs_stack_dump(void)
    * coverage.
    */
   context = gjs_context_new();
+
   gjs_dumpstack();
   g_object_unref(context);
   gjs_dumpstack();
@@ -173,13 +201,15 @@ gjstest_test_func_gjs_jsapi_util_error_throw(void)
 {
     GjsUnitTestFixture fixture;
     JSContext *context;
+    JSObject *global;
     jsval exc, value, previous;
     char *s = NULL;
     int strcmp_result;
 
     _gjs_unit_test_fixture_begin(&fixture);
     context = fixture.context;
-
+    global = JS_GetGlobalObject(context);
+    JSCompartment *oldCompartment = JS_EnterCompartment(context, global);
     /* Test that we can throw */
 
     gjs_throw(context, "This is an exception %d", 42);
@@ -227,6 +257,7 @@ gjstest_test_func_gjs_jsapi_util_error_throw(void)
 
     JS_RemoveValueRoot(context, &previous);
 
+    JS_LeaveCompartment(context, oldCompartment);
     _gjs_unit_test_fixture_finish(&fixture);
 }
 
@@ -278,6 +309,7 @@ main(int    argc,
 
     g_test_add_func("/gjs/context/construct/destroy", gjstest_test_func_gjs_context_construct_destroy);
     g_test_add_func("/gjs/context/construct/eval", gjstest_test_func_gjs_context_construct_eval);
+    g_test_add_func("/gjs/context/fixture", gjstest_test_func_gjs_context_fixture);
     g_test_add_func("/gjs/jsapi/util/array", gjstest_test_func_gjs_jsapi_util_array);
     g_test_add_func("/gjs/jsapi/util/error/throw", gjstest_test_func_gjs_jsapi_util_error_throw);
     g_test_add_func("/gjs/jsapi/util/string/js/string/utf8", 
gjstest_test_func_gjs_jsapi_util_string_js_string_utf8);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]