[gjs/wip/ptomato/mozjs45prep: 38/38] WIP - js: Save prototypes of created classes



commit 8b1270a9716281ca506db8adfd853479214b0c68
Author: Philip Chimento <philip chimento gmail com>
Date:   Tue Apr 4 00:44:26 2017 -0700

    WIP - js: Save prototypes of created classes
    
    JS_NewObject() doesn't guess the prototypes of created classes anymore. So
    when we define a class with JS_InitClass() we have to use
    JS_NewObjectWithGivenProto() to create it. For that, we need to keep the
    prototype around somehow.
    
    This does that for GI Function, but probably inefficiently

 gi/function.cpp |   21 +++++++++++++++------
 1 files changed, 15 insertions(+), 6 deletions(-)
---
diff --git a/gi/function.cpp b/gi/function.cpp
index 5f7068a..035b347 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -1663,16 +1663,16 @@ function_new(JSContext      *context,
              GICallableInfo *info)
 {
     Function *priv;
-    bool found;
 
     /* put constructor for GIRepositoryFunction() in the global namespace */
     JS::RootedObject global(context, gjs_get_import_global(context));
+    JS::RootedObject prototype(context);
+    JS::RootedValue v_ctor(context);
 
-    if (!JS_HasProperty(context, global, gjs_function_class.name, &found))
+    if (!JS_GetProperty(context, global, gjs_function_class.name, &v_ctor))
         return NULL;
 
-    if (!found) {
-        JSObject *prototype;
+    if (v_ctor.isUndefined()) {
         JS::RootedObject function_proto(context,
                                         JS_GetFunctionPrototype(context, global));
 
@@ -1697,11 +1697,20 @@ function_new(JSContext      *context,
             g_error("Can't init class %s", gjs_function_class.name);
 
         gjs_debug(GJS_DEBUG_GFUNCTION, "Initialized class %s prototype %p",
-                  gjs_function_class.name, prototype);
+                  gjs_function_class.name, prototype.get());
+    } else {
+        JS::RootedValue v_prototype(context);
+        JS::RootedObject ctor(context, &v_ctor.toObject());
+        if (!gjs_object_get_property(context, ctor, GJS_STRING_PROTOTYPE,
+                                     &v_prototype)) {
+            gjs_debug(GJS_DEBUG_GFUNCTION, "Failed to get GI function prototype");
+            return NULL;
+        }
+        prototype = &v_prototype.toObject();
     }
 
     JS::RootedObject function(context,
-                              JS_NewObject(context, &gjs_function_class));
+        JS_NewObjectWithGivenProto(context, &gjs_function_class, prototype));
     if (function == NULL) {
         gjs_debug(GJS_DEBUG_GFUNCTION, "Failed to construct function");
         return NULL;


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