[gjs] GjsContext: add the concept of "current context"



commit 639e145b7f9b9445479f5f74b55bf89a3ad92b4e
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Tue May 14 22:43:28 2013 +0200

    GjsContext: add the concept of "current context"
    
    Just a static global variable holding the GjsContext. There should
    be at most one context at any time, and this is enforced by
    make_current() checking the current context.
    For backward compatibility, any new GjsContext is immediately made current,
    which means that code that uses multiple context without handling
    the current one manually will assert now. You can still use multiple
    contexts if you take care to clear the current one.
    
    This will be used to avoid passing a JSContext where we can't easily.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=699817

 gjs/context.c |   21 +++++++++++++++++++++
 gjs/context.h |    4 ++++
 2 files changed, 25 insertions(+), 0 deletions(-)
---
diff --git a/gjs/context.c b/gjs/context.c
index 52b2dbf..b7c01cb 100644
--- a/gjs/context.c
+++ b/gjs/context.c
@@ -285,6 +285,8 @@ static void
 gjs_context_init(GjsContext *js_context)
 {
     js_context->jsversion_string = g_strdup(_GJS_JS_VERSION_DEFAULT);
+
+    gjs_context_make_current(js_context);
 }
 
 static void
@@ -425,6 +427,9 @@ gjs_context_finalize(GObject *object)
 
     g_free(js_context->jsversion_string);
 
+    if (gjs_context_get_current() == (GjsContext*)object)
+        gjs_context_make_current(NULL);
+
     g_mutex_lock(&contexts_lock);
     all_contexts = g_list_remove(all_contexts, object);
     g_mutex_unlock(&contexts_lock);
@@ -1108,3 +1113,19 @@ gjs_context_define_string_array(GjsContext  *js_context,
 
     return TRUE;
 }
+
+static GjsContext *current_context;
+
+GjsContext *
+gjs_context_get_current (void)
+{
+    return current_context;
+}
+
+void
+gjs_context_make_current (GjsContext *context)
+{
+    g_assert (context == NULL || current_context == NULL);
+
+    current_context = context;
+}
diff --git a/gjs/context.h b/gjs/context.h
index cb3f7a5..dc2d037 100644
--- a/gjs/context.h
+++ b/gjs/context.h
@@ -66,6 +66,10 @@ gboolean        gjs_context_define_string_array  (GjsContext  *js_context,
                                                   GError       **error);
 
 GList*          gjs_context_get_all              (void);
+
+GjsContext     *gjs_context_get_current          (void);
+void            gjs_context_make_current         (GjsContext *js_context);
+
 void*           gjs_context_get_native_context   (GjsContext *js_context);
 
 void            gjs_context_print_stack_stderr    (GjsContext *js_context);


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