[gjs: 9/11] object: Move argument checking out of props_to_g_parameters()



commit 2c9c989d3679a372448295a0c6ad81cdb3bddbc6
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Oct 27 22:10:35 2019 -0700

    object: Move argument checking out of props_to_g_parameters()
    
    It makes more sense to check the CallArgs object right where the
    function is called, in init_impl(), and pass the resulting properties
    object, if it is valid, directly to props_to_g_parameters().

 gi/object.cpp | 28 +++++++++++++++-------------
 gi/object.h   |  2 +-
 2 files changed, 16 insertions(+), 14 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index 01af3363..9cd9c40a 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -951,20 +951,10 @@ bool ObjectPrototype::new_enumerate_impl(JSContext* cx, JS::HandleObject,
 /* Set properties from args to constructor (args[0] is supposed to be
  * a hash) */
 bool ObjectPrototype::props_to_g_parameters(JSContext* context,
-                                            const JS::HandleValueArray& args,
+                                            JS::HandleObject props,
                                             std::vector<const char*>* names,
                                             AutoGValueVector* values) {
     size_t ix, length;
-
-    if (args.length() == 0 || args[0].isUndefined())
-        return true;
-
-    if (!args[0].isObject()) {
-        gjs_throw(context, "argument should be a hash with props to set");
-        return false;
-    }
-
-    JS::RootedObject props(context, &args[0].toObject());
     JS::RootedId prop_id(context);
     JS::RootedValue value(context);
     JS::Rooted<JS::IdVector> ids(context, context);
@@ -1449,8 +1439,20 @@ ObjectInstance::init_impl(JSContext              *context,
 
     std::vector<const char *> names;
     AutoGValueVector values;
-    if (!m_proto->props_to_g_parameters(context, args, &names, &values))
-        return false;
+
+    if (args.length() > 0 && !args[0].isUndefined()) {
+        if (!args[0].isObject()) {
+            gjs_throw(context,
+                      "Argument to the constructor of %s should be an object "
+                      "with properties to set",
+                      name());
+            return false;
+        }
+
+        JS::RootedObject props(context, &args[0].toObject());
+        if (!m_proto->props_to_g_parameters(context, props, &names, &values))
+            return false;
+    }
 
     if (G_TYPE_IS_ABSTRACT(gtype())) {
         gjs_throw(context,
diff --git a/gi/object.h b/gi/object.h
index 10c383c9..3acffc83 100644
--- a/gi/object.h
+++ b/gi/object.h
@@ -241,7 +241,7 @@ class ObjectPrototype
     GJS_JSAPI_RETURN_CONVENTION
     GIFieldInfo* lookup_cached_field_info(JSContext* cx, JS::HandleString key);
     GJS_JSAPI_RETURN_CONVENTION
-    bool props_to_g_parameters(JSContext* cx, const JS::HandleValueArray& args,
+    bool props_to_g_parameters(JSContext* cx, JS::HandleObject props,
                                std::vector<const char*>* names,
                                AutoGValueVector* values);
 


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