[gjs/wip/js24] compartments are compulsory now



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

    compartments are compulsory now

 gjs/context.cpp    |    2 ++
 gjs/jsapi-util.cpp |    5 +++++
 gjs/stack.cpp      |    4 ++++
 test/gjs-tests.cpp |   34 +++++++++++++++++++++++++++++++++-
 4 files changed, 44 insertions(+), 1 deletions(-)
---
diff --git a/gjs/context.cpp b/gjs/context.cpp
index eaba028..c2a6cf1 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,
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 6007694..b987185 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -94,6 +94,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]