[gjs/wip/gcampax/70-arg-cache: 4/6] function: don't root the out argument locations



commit 86f9e8e242a1b133df8aa567bb34940f278f31ab
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Sun Apr 21 20:21:40 2013 +0200

    function: don't root the out argument locations
    
    Instead, build the array object eagerly if it's know that it will
    be needed, and rely on the stack scanner to find the array itself.

 gi/function.cpp | 45 +++++++++++++--------------------------------
 1 file changed, 13 insertions(+), 32 deletions(-)
---
diff --git a/gi/function.cpp b/gi/function.cpp
index 8fa6a42..9c46574 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -723,7 +723,6 @@ gjs_invoke_c_function(JSContext                             *context,
     bool is_method;
     bool is_object_method = FALSE;
     GITypeTag return_tag;
-    JS::AutoValueVector return_values(context);
     GSList *iter;
 
     /* Because we can't free a closure while we're in it, we defer
@@ -888,11 +887,8 @@ gjs_invoke_c_function(JSContext                             *context,
                                               &state.out_arg_cvalues[-1]);
     }
 
-    if (function->js_out_argc > 0) {
-        for (size_t i = 0; i < function->js_out_argc; i++)
-            if (!return_values.append(JS::UndefinedValue()))
-                g_error("Unable to append to vector");
-    }
+    if (function->js_out_argc > 1)
+        js_rval->setObject(*JS_NewArrayObject(context, 0));
 
     /* Process out arguments and return values
      * This loop is skipped if we fail the type conversion above, or
@@ -902,15 +898,24 @@ gjs_invoke_c_function(JSContext                             *context,
         GjsArgumentCache *cache = &function->arguments[gi_arg_pos];
         GIArgument *out_value = &state.out_arg_cvalues[gi_arg_pos];
 
+        JS::RootedValue value(context);
         if (!cache->marshal_out(context, cache,
                                 &state, out_value,
-                                return_values[js_arg_pos])) {
+                                &value)) {
             failed = true;
             break;
         }
 
-        if (!gjs_arg_cache_is_skip_out(cache))
+        if (!gjs_arg_cache_is_skip_out(cache)) {
+            if (function->js_out_argc == 1) {
+                js_rval->set(value);
+            } else {
+                JS::RootedObject js_rval_array(context, &js_rval->toObject());
+                JS_SetElement(context, js_rval_array, js_arg_pos, value);
+            }
+
             js_arg_pos++;
+        }
     }
 
     g_assert(failed || did_throw_gerror || js_arg_pos == uint8_t(function->js_out_argc));
@@ -942,30 +947,6 @@ release:
 
     g_assert_cmpuint(ffi_arg_pos, ==, processed_c_args + 1);
 
-    if (function->js_out_argc > 0 && (!failed && !did_throw_gerror)) {
-        /* if we have 1 return value or out arg, return that item
-         * on its own, otherwise return a JavaScript array with
-         * [return value, out arg 1, out arg 2, ...]
-         */
-        if (js_rval) {
-            if (function->js_out_argc == 1) {
-                js_rval.ref().set(return_values[0]);
-            } else {
-                JSObject *array;
-                array = JS_NewArrayObject(context, return_values);
-                if (array == NULL) {
-                    failed = true;
-                } else {
-                    js_rval.ref().setObject(*array);
-                }
-            }
-        }
-
-        if (r_value) {
-            *r_value = return_gargument;
-        }
-    }
-
     if (!failed && did_throw_gerror) {
         gjs_throw_g_error(context, local_error);
         return false;


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