[gjs] ByteArray: don't call eval() just to retrieve a prototype



commit 5b7c1d3105514004f9e36852ea5c31497c5ce526
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Wed Jan 15 19:02:16 2014 +0100

    ByteArray: don't call eval() just to retrieve a prototype
    
    If using a global variable to store ByteArray.prototype is wrong,
    we can use a slot on the global object.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=722286

 gjs/byteArray.cpp |   35 ++++++++++++++++++++++-------------
 gjs/jsapi-util.h  |    1 +
 2 files changed, 23 insertions(+), 13 deletions(-)
---
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 42d7dc1..4aca8f1 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -543,10 +543,14 @@ byte_array_get_prototype(JSContext *context)
     jsval retval;
     JSObject *prototype;
 
-    if (!gjs_eval_with_scope(context, NULL,
-                             "imports.byteArray.ByteArray.prototype;", -1,
-                             "<internal>", &retval))
-        g_error ("Could not import byte array prototype\n");
+    retval = gjs_get_global_slot (context, GJS_GLOBAL_SLOT_BYTE_ARRAY_PROTOTYPE);
+
+    if (!JSVAL_IS_OBJECT (retval)) {
+        if (!gjs_eval_with_scope(context, NULL,
+                                 "imports.byteArray.ByteArray.prototype;", -1,
+                                 "<internal>", &retval))
+            g_error ("Could not import byte array prototype\n");
+    }
 
     return JSVAL_TO_OBJECT(retval);
 }
@@ -907,22 +911,27 @@ gjs_define_byte_array_stuff(JSContext  *context,
                             JSObject  **module_out)
 {
     JSObject *module;
+    JSObject *prototype;
 
     module = JS_NewObject (context, NULL, NULL, NULL);
 
-    JS_InitClass(context, module,
-                 NULL,
-                 &gjs_byte_array_class,
-                 gjs_byte_array_constructor,
-                 0,
-                 &gjs_byte_array_proto_props[0],
-                 &gjs_byte_array_proto_funcs[0],
-                 NULL,
-                 NULL);
+    prototype = JS_InitClass(context, module,
+                             NULL,
+                             &gjs_byte_array_class,
+                             gjs_byte_array_constructor,
+                             0,
+                             &gjs_byte_array_proto_props[0],
+                             &gjs_byte_array_proto_funcs[0],
+                             NULL,
+                             NULL);
 
     if (!JS_DefineFunctions(context, module, &gjs_byte_array_module_funcs[0]))
         return JS_FALSE;
 
+    g_assert(JSVAL_IS_VOID(gjs_get_global_slot(context, GJS_GLOBAL_SLOT_BYTE_ARRAY_PROTOTYPE)));
+    gjs_set_global_slot(context, GJS_GLOBAL_SLOT_BYTE_ARRAY_PROTOTYPE,
+                        OBJECT_TO_JSVAL(prototype));
+
     *module_out = module;
     return JS_TRUE;
 }
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index e3a1aac..e133b16 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -48,6 +48,7 @@ enum {
 typedef enum {
     GJS_GLOBAL_SLOT_IMPORTS,
     GJS_GLOBAL_SLOT_KEEP_ALIVE,
+    GJS_GLOBAL_SLOT_BYTE_ARRAY_PROTOTYPE,
     GJS_GLOBAL_SLOT_LAST,
 } GjsGlobalSlot;
 


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