[gjs] context: Move const_strings to GjsContext



commit da0739b9ed761501a1a0e39ce3eeb395f505eed2
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Jan 15 09:51:29 2014 -0500

    context: Move const_strings to GjsContext
    
    It needs to be here, as const strings are context-dependent, and one
    JSRuntime needs to host multiple contexts.

 gi/arg.cpp                  |    6 ++----
 gi/boxed.cpp                |    9 +++------
 gi/gerror.cpp               |   18 ++++++------------
 gi/object.cpp               |    3 +--
 gi/repo.cpp                 |   21 +++++++--------------
 gi/value.cpp                |    3 +--
 gjs/context.cpp             |   37 +++++++++++++++++++++++++++++++++++++
 gjs/importer.cpp            |   15 +++++----------
 gjs/jsapi-dynamic-class.cpp |    3 +--
 gjs/jsapi-util.h            |    2 ++
 gjs/runtime.cpp             |   41 -----------------------------------------
 gjs/runtime.h               |    2 --
 12 files changed, 65 insertions(+), 95 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index 6a8cefe..fa9afe1 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -1093,8 +1093,7 @@ gjs_array_to_explicit_array_internal(JSContext       *context,
         goto out;
     }
 
-    length_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                               GJS_STRING_LENGTH);
+    length_name = gjs_context_get_const_string(context, GJS_STRING_LENGTH);
 
     if (JSVAL_IS_NULL(value)) {
         *contents = NULL;
@@ -1576,8 +1575,7 @@ gjs_value_to_g_argument(JSContext      *context,
         jsid length_name;
         JSBool found_length;
 
-        length_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                   GJS_STRING_LENGTH);
+        length_name = gjs_context_get_const_string(context, GJS_STRING_LENGTH);
 
         /* nullable_type=FALSE; while a list can be NULL in C, that
          * means empty array in JavaScript, it doesn't mean null in
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index 75ee461..8f7acf1 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -337,8 +337,7 @@ boxed_invoke_constructor(JSContext   *context,
     jsval js_constructor, js_constructor_func;
     jsid constructor_const;
 
-    constructor_const = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                     GJS_STRING_CONSTRUCTOR);
+    constructor_const = gjs_context_get_const_string(context, GJS_STRING_CONSTRUCTOR);
     if (!gjs_object_require_property(context, obj, NULL, constructor_const, &js_constructor))
         return JS_FALSE;
 
@@ -362,8 +361,7 @@ boxed_new(JSContext   *context,
 
         /* Short-circuit construction for GVariants by calling into the JS packing
            function */
-        constructor_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                        GJS_STRING_NEW_INTERNAL);
+        constructor_name = gjs_context_get_const_string(context, GJS_STRING_NEW_INTERNAL);
         return boxed_invoke_constructor (context, obj, constructor_name, argc, argv, rval);
     }
 
@@ -1115,8 +1113,7 @@ boxed_fill_prototype_info(JSContext *context,
                 if (priv->default_constructor < 0 &&
                     strcmp(g_base_info_get_name ((GIBaseInfo*) func_info), "new") == 0) {
                     priv->default_constructor = i;
-                    priv->default_constructor_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                                                  GJS_STRING_NEW);
+                    priv->default_constructor_name = gjs_context_get_const_string(context, GJS_STRING_NEW);
                 }
             }
 
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index 9d9e085..b27370c 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -103,10 +103,8 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(error)
     g_base_info_ref( (GIBaseInfo*) priv->info);
     priv->domain = proto_priv->domain;
 
-    message_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                GJS_STRING_MESSAGE);
-    code_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                             GJS_STRING_CODE);
+    message_name = gjs_context_get_const_string(context, GJS_STRING_MESSAGE);
+    code_name = gjs_context_get_const_string(context, GJS_STRING_CODE);
     if (!gjs_object_require_property(context, JSVAL_TO_OBJECT(argv[0]),
                                      "GError constructor", message_name, &v_message))
         return JS_FALSE;
@@ -274,8 +272,7 @@ error_constructor_value_of(JSContext *context, unsigned argc, jsval *vp)
         return JS_FALSE;
     }
 
-    prototype_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                  GJS_STRING_PROTOTYPE);
+    prototype_name = gjs_context_get_const_string(context, GJS_STRING_PROTOTYPE);
     if (!gjs_object_require_property(context,
                                      JSVAL_TO_OBJECT(v_self),
                                      "constructor",
@@ -452,12 +449,9 @@ define_error_properties(JSContext *context,
                                      &lineNumber))
         return;
 
-    stack_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                              GJS_STRING_STACK);
-    filename_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                 GJS_STRING_FILENAME);
-    linenumber_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                   GJS_STRING_LINE_NUMBER);
+    stack_name = gjs_context_get_const_string(context, GJS_STRING_STACK);
+    filename_name = gjs_context_get_const_string(context, GJS_STRING_FILENAME);
+    linenumber_name = gjs_context_get_const_string(context, GJS_STRING_LINE_NUMBER);
 
     JS_DefinePropertyById(context, obj, stack_name, stack,
                           NULL, NULL, JSPROP_ENUMERATE);
diff --git a/gi/object.cpp b/gi/object.cpp
index ac2eb39..2d38e8a 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1267,8 +1267,7 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(object_instance)
 
     GJS_NATIVE_CONSTRUCTOR_PRELUDE(object_instance);
 
-    object_init_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                    GJS_STRING_GOBJECT_INIT);
+    object_init_name = gjs_context_get_const_string(context, GJS_STRING_GOBJECT_INIT);
     if (!gjs_object_require_property(context, object, "GObject instance", object_init_name, &initer))
         return JS_FALSE;
 
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 95ed577..30e398e 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -67,8 +67,7 @@ get_version_for_ns (JSContext *context,
     JSObject *versions;
     jsval version_val;
 
-    versions_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                 GJS_STRING_GI_VERSIONS);
+    versions_name = gjs_context_get_const_string(context, GJS_STRING_GI_VERSIONS);
     if (!gjs_object_require_property(context, repo_obj, "GI repository object", versions_name, 
&versions_val) ||
         !JSVAL_IS_OBJECT(versions_val)) {
         gjs_throw(context, "No 'versions' property in GI repository object");
@@ -316,8 +315,7 @@ repo_new(JSContext *context)
                         "repo constructor, obj %p priv %p", repo, priv);
 
     versions = JS_NewObject(context, NULL, NULL, global);
-    versions_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                 GJS_STRING_GI_VERSIONS);
+    versions_name = gjs_context_get_const_string(context, GJS_STRING_GI_VERSIONS);
     JS_DefinePropertyById(context, repo,
                           versions_name,
                           OBJECT_TO_JSVAL(versions),
@@ -325,8 +323,7 @@ repo_new(JSContext *context)
                           JSPROP_PERMANENT);
 
     private_ns = JS_NewObject(context, NULL, NULL, global);
-    private_ns_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                   GJS_STRING_PRIVATE_NS_MARKER);
+    private_ns_name = gjs_context_get_const_string(context, GJS_STRING_PRIVATE_NS_MARKER);
     JS_DefinePropertyById(context, repo,
                           private_ns_name,
                           OBJECT_TO_JSVAL(private_ns),
@@ -531,8 +528,7 @@ gjs_lookup_private_namespace(JSContext *context)
 {
     jsid ns_name;
 
-    ns_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                           GJS_STRING_PRIVATE_NS_MARKER);
+    ns_name = gjs_context_get_const_string(context, GJS_STRING_PRIVATE_NS_MARKER);
     return gjs_lookup_namespace_object_by_name(context, ns_name);
 }
 
@@ -573,8 +569,7 @@ lookup_override_function(JSContext  *context,
     g_assert(JSVAL_IS_OBJECT(importer));
 
     overridespkg = JSVAL_VOID;
-    overrides_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                  GJS_STRING_GI_OVERRIDES);
+    overrides_name = gjs_context_get_const_string(context, GJS_STRING_GI_OVERRIDES);
     if (!gjs_object_require_property(context, JSVAL_TO_OBJECT(importer), "importer",
                                      overrides_name, &overridespkg) ||
         !JSVAL_IS_OBJECT(overridespkg))
@@ -585,8 +580,7 @@ lookup_override_function(JSContext  *context,
         || !JSVAL_IS_OBJECT(module))
         goto fail;
 
-    object_init_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                    GJS_STRING_GOBJECT_INIT);
+    object_init_name = gjs_context_get_const_string(context, GJS_STRING_GOBJECT_INIT);
     if (!gjs_object_require_property(context, JSVAL_TO_OBJECT(module), "override module",
                                      object_init_name, &function) ||
         !JSVAL_IS_OBJECT(function))
@@ -617,8 +611,7 @@ gjs_lookup_namespace_object_by_name(JSContext      *context,
     g_assert(JSVAL_IS_OBJECT(importer));
 
     girepository = JSVAL_VOID;
-    gi_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                           GJS_STRING_GI_MODULE);
+    gi_name = gjs_context_get_const_string(context, GJS_STRING_GI_MODULE);
     if (!gjs_object_require_property(context, JSVAL_TO_OBJECT(importer), "importer",
                                      gi_name, &girepository) ||
         !JSVAL_IS_OBJECT(girepository)) {
diff --git a/gi/value.cpp b/gi/value.cpp
index b9a603d..e783457 100644
--- a/gi/value.cpp
+++ b/gi/value.cpp
@@ -363,8 +363,7 @@ gjs_value_to_g_value_internal(JSContext    *context,
         jsid length_name;
         JSBool found_length;
 
-        length_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                   GJS_STRING_LENGTH);
+        length_name = gjs_context_get_const_string(context, GJS_STRING_LENGTH);
         if (JSVAL_IS_NULL(value)) {
             /* do nothing */
         } else if (JS_HasPropertyById(context, JSVAL_TO_OBJECT(value), length_name, &found_length) &&
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 5dc91cd..5949b10 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -65,8 +65,22 @@ struct _GjsContext {
     char *program_name;
 
     char **search_path;
+
+    jsid const_strings[GJS_STRING_LAST];
+};
+
+/* Keep this consistent with GjsConstString */
+static const char *const_strings[] = {
+    "constructor", "prototype", "length",
+    "imports", "__parentModule__", "__init__", "searchPath",
+    "__gjsKeepAlive", "__gjsPrivateNS",
+    "gi", "versions", "overrides",
+    "_init", "_new_internal", "new",
+    "message", "code", "stack", "fileName", "lineNumber"
 };
 
+G_STATIC_ASSERT(G_N_ELEMENTS(const_strings) == GJS_STRING_LAST);
+
 struct _GjsContextClass {
     GObjectClass parent;
 };
@@ -493,6 +507,7 @@ gjs_context_constructed(GObject *object)
 {
     GjsContext *js_context = GJS_CONTEXT(object);
     guint32 options_flags;
+    int i;
 
     G_OBJECT_CLASS(gjs_context_parent_class)->constructed(object);
 
@@ -508,6 +523,9 @@ gjs_context_constructed(GObject *object)
 
     gjs_runtime_init_for_context(js_context->runtime, js_context->context);
 
+    for (i = 0; i < GJS_STRING_LAST; i++)
+        js_context->const_strings[i] = gjs_intern_string_to_id(js_context->context, const_strings[i]);
+
     JS_BeginRequest(js_context->context);
 
     /* JSOPTION_DONT_REPORT_UNCAUGHT: Don't send exceptions to our
@@ -843,3 +861,22 @@ gjs_context_make_current (GjsContext *context)
 
     current_context = context;
 }
+
+jsid
+gjs_context_get_const_string(JSContext      *context,
+                             GjsConstString  name)
+{
+    GjsContext *gjs_context = (GjsContext *) JS_GetContextPrivate(context);
+    return gjs_context->const_strings[name];
+}
+
+gboolean
+gjs_object_get_property_const(JSContext      *context,
+                              JSObject       *obj,
+                              GjsConstString  property_name,
+                              jsval          *value_p)
+{
+    jsid pname;
+    pname = gjs_context_get_const_string(context, property_name);
+    return JS_GetPropertyById(context, obj, pname, value_p);
+}
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 00bea71..e8bcc65 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -309,8 +309,7 @@ load_module_init(JSContext  *context,
     GFile *file;
 
     /* First we check if js module has already been loaded  */
-    module_init_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                    GJS_STRING_MODULE_INIT);
+    module_init_name = gjs_context_get_const_string(context, GJS_STRING_MODULE_INIT);
     if (JS_HasPropertyById(context, in_object, module_init_name, &found) && found) {
         jsval module_obj_val;
 
@@ -433,8 +432,7 @@ do_import(JSContext  *context,
     GFile *gfile;
     gboolean exists;
 
-    search_path_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                    GJS_STRING_SEARCH_PATH);
+    search_path_name = gjs_context_get_const_string(context, GJS_STRING_SEARCH_PATH);
     if (!gjs_object_require_property(context, obj, "importer", search_path_name, &search_path_val)) {
         return JS_FALSE;
     }
@@ -694,8 +692,7 @@ importer_new_enumerate(JSContext  *context,
             /* we are enumerating the prototype properties */
             return JS_TRUE;
 
-        search_path_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                        GJS_STRING_SEARCH_PATH);
+        search_path_name = gjs_context_get_const_string(context, GJS_STRING_SEARCH_PATH);
         if (!gjs_object_require_property(context, *object, "importer", search_path_name, &search_path_val))
             return JS_FALSE;
 
@@ -871,8 +868,7 @@ importer_new_resolve(JSContext *context,
 
     *objp = NULL;
 
-    module_init_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                    GJS_STRING_MODULE_INIT);
+    module_init_name = gjs_context_get_const_string(context, GJS_STRING_MODULE_INIT);
     if (*id == module_init_name)
         return JS_TRUE;
 
@@ -1160,8 +1156,7 @@ gjs_define_root_importer(JSContext   *context,
     JS_BeginRequest(context);
 
     importer = gjs_get_global_slot(context, GJS_GLOBAL_SLOT_IMPORTS);
-    imports_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                GJS_STRING_IMPORTS);
+    imports_name = gjs_context_get_const_string(context, GJS_STRING_IMPORTS);
     if (!JS_DefinePropertyById(context, in_object,
                                imports_name, importer,
                                NULL, NULL,
diff --git a/gjs/jsapi-dynamic-class.cpp b/gjs/jsapi-dynamic-class.cpp
index 536701d..49381ba 100644
--- a/gjs/jsapi-dynamic-class.cpp
+++ b/gjs/jsapi-dynamic-class.cpp
@@ -210,8 +210,7 @@ gjs_construct_object_dynamic(JSContext      *context,
 
     JS_BeginRequest(context);
 
-    constructor_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                                    GJS_STRING_CONSTRUCTOR);
+    constructor_name = gjs_context_get_const_string(context, GJS_STRING_CONSTRUCTOR);
     if (!gjs_object_require_property(context, proto, "prototype",
                                      constructor_name, &value))
         goto out;
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index bb37da5..15fdd2d 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -390,6 +390,8 @@ JSBool            gjs_eval_with_scope        (JSContext    *context,
                                               const char   *filename,
                                               jsval        *retval_p,
                                               GError      **error);
+jsid              gjs_context_get_const_string (JSContext       *context,
+                                                GjsConstString   string);
 
 /**
  * gjs_strip_unix_shebang:
diff --git a/gjs/runtime.cpp b/gjs/runtime.cpp
index d9f658d..339b77b 100644
--- a/gjs/runtime.cpp
+++ b/gjs/runtime.cpp
@@ -37,21 +37,8 @@
 
 typedef struct {
     JSContext *context;
-    jsid const_strings[GJS_STRING_LAST];
 } GjsRuntimeData;
 
-/* Keep this consistent with GjsConstString */
-static const char *const_strings[] = {
-    "constructor", "prototype", "length",
-    "imports", "__parentModule__", "__init__", "searchPath",
-    "__gjsKeepAlive", "__gjsPrivateNS",
-    "gi", "versions", "overrides",
-    "_init", "_new_internal", "new",
-    "message", "code", "stack", "fileName", "lineNumber"
-};
-
-G_STATIC_ASSERT(G_N_ELEMENTS(const_strings) == GJS_STRING_LAST);
-
 static inline GjsRuntimeData *
 get_data(JSRuntime *runtime)
 {
@@ -73,42 +60,14 @@ gjs_runtime_get_context(JSRuntime *runtime)
     return get_data(runtime)->context;
 }
 
-jsid
-gjs_runtime_get_const_string(JSRuntime      *runtime,
-                             GjsConstString  name)
-{
-    /* Do not add prerequisite checks here, this is a very hot call! */
-
-    return get_data(runtime)->const_strings[name];
-}
-
-gboolean
-gjs_object_get_property_const(JSContext      *context,
-                              JSObject       *obj,
-                              GjsConstString  property_name,
-                              jsval          *value_p)
-{
-    jsid pname;
-
-    pname = gjs_runtime_get_const_string(JS_GetRuntime(context),
-                                         property_name);
-    return JS_GetPropertyById(context, obj,
-                              pname, value_p);
-}
-
 void
 gjs_runtime_init_for_context(JSRuntime *runtime,
                              JSContext *context)
 {
     GjsRuntimeData *data;
-    int i;
 
     data = g_new(GjsRuntimeData, 1);
-
     data->context = context;
-    for (i = 0; i < GJS_STRING_LAST; i++)
-        data->const_strings[i] = gjs_intern_string_to_id(context, const_strings[i]);
-
     JS_SetRuntimePrivate(runtime, data);
 }
 
diff --git a/gjs/runtime.h b/gjs/runtime.h
index 7a6a36e..7bbba32 100644
--- a/gjs/runtime.h
+++ b/gjs/runtime.h
@@ -53,7 +53,5 @@ void        gjs_runtime_init_for_context     (JSRuntime       *runtime,
 void        gjs_runtime_deinit               (JSRuntime       *runtime);
 
 JSContext*  gjs_runtime_get_context          (JSRuntime       *runtime);
-jsid        gjs_runtime_get_const_string     (JSRuntime       *runtime,
-                                              GjsConstString   string);
 
 #endif /* __GJS_RUNTIME_H__ */


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