[gjs/wip/ptomato/mozjs45prep: 6/13] js: Fix use of mozilla::Maybe API



commit 056f2fc73580c0b30935e99d5999f319f95e5282
Author: Philip Chimento <philip chimento gmail com>
Date:   Mon Mar 27 19:24:45 2017 +0100

    js: Fix use of mozilla::Maybe API
    
    Creating a mozilla::Maybe from another mozilla::Maybe is fast, so we can
    dispense with the reference type, allowing a much more readable use of
    mozilla::Some() or mozilla::Nothing() inline in function calls.
    
    We still do have to specify the type of the mozilla::Maybe explicitly
    when it is JS::MutableHandle and we are creating one from a JS::Rooted or
    JS::Handle, otherwise the compiler will think it is JS::Rooted* or
    JS::Handle*.

 gi/function.cpp  |   23 ++++++++++-------------
 gi/gerror.cpp    |    9 ++++-----
 gjs/jsapi-util.h |    8 ++++----
 gjs/stack.cpp    |   14 ++++++--------
 4 files changed, 24 insertions(+), 30 deletions(-)
---
diff --git a/gi/function.cpp b/gi/function.cpp
index bcdf62c..e3a6c9f 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -697,12 +697,12 @@ format_function_name(Function *function,
  * providing a @r_value argument.
  */
 static bool
-gjs_invoke_c_function(JSContext                              *context,
-                      Function                               *function,
-                      JS::HandleObject                        obj, /* "this" object */
-                      const JS::HandleValueArray&             args,
-                      mozilla::Maybe<JS::MutableHandleValue>& js_rval,
-                      GIArgument                             *r_value)
+gjs_invoke_c_function(JSContext                             *context,
+                      Function                              *function,
+                      JS::HandleObject                       obj, /* "this" object */
+                      const JS::HandleValueArray&            args,
+                      mozilla::Maybe<JS::MutableHandleValue> js_rval,
+                      GIArgument                            *r_value)
 {
     /* These first four are arrays which hold argument pointers.
      * @in_arg_cvalues: C values which are passed on input (in or inout)
@@ -1340,9 +1340,8 @@ function_call(JSContext *context,
     if (priv == NULL)
         return true; /* we are the prototype, or have the wrong class */
 
-    /* COMPAT: mozilla::Maybe gains a much more usable API in future versions */
-    auto m_retval = mozilla::Some<JS::MutableHandleValue>(&retval);
-    success = gjs_invoke_c_function(context, priv, object, js_argv, m_retval,
+    success = gjs_invoke_c_function(context, priv, object, js_argv,
+                                    mozilla::Some<JS::MutableHandleValue>(&retval),
                                     NULL);
     if (success)
         js_argv.rval().set(retval);
@@ -1761,10 +1760,8 @@ gjs_invoke_c_function_uncached(JSContext                  *context,
   if (!init_cached_function_data (context, &function, 0, info))
       return false;
 
-  /* COMPAT: mozilla::Maybe gains a much more usable API in future versions */
-  mozilla::Maybe<JS::MutableHandleValue> m_rval;
-  m_rval.emplace(rval);
-  result = gjs_invoke_c_function(context, &function, obj, args, m_rval, NULL);
+  result = gjs_invoke_c_function(context, &function, obj, args,
+                                 mozilla::Some(rval), NULL);
   uninit_cached_function_data (&function);
   return result;
 }
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index c8c639c..b8035c0 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -396,12 +396,11 @@ define_error_properties(JSContext       *context,
                         JS::HandleObject obj)
 {
     JS::RootedValue stack(context), fileName(context), lineNumber(context);
-    /* COMPAT: mozilla::Maybe gains a much more usable API in future versions */
-    auto m_stack = mozilla::Some<JS::MutableHandleValue>(&stack);
-    auto m_file = mozilla::Some<JS::MutableHandleValue>(&fileName);
-    auto m_line = mozilla::Some<JS::MutableHandleValue>(&lineNumber);
 
-    if (!gjs_context_get_frame_info(context, m_stack, m_file, m_line))
+    if (!gjs_context_get_frame_info(context,
+                                    mozilla::Some<JS::MutableHandleValue>(&stack),
+                                    mozilla::Some<JS::MutableHandleValue>(&fileName),
+                                    mozilla::Some<JS::MutableHandleValue>(&lineNumber)))
         return;
 
     gjs_object_define_property(context, obj, GJS_STRING_STACK, stack,
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index d94f813..718a82f 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -228,10 +228,10 @@ const char* gjs_get_type_name                (JS::Value        value);
 
 void gjs_maybe_gc (JSContext *context);
 
-bool gjs_context_get_frame_info(JSContext                              *context,
-                                mozilla::Maybe<JS::MutableHandleValue>& stack,
-                                mozilla::Maybe<JS::MutableHandleValue>& fileName,
-                                mozilla::Maybe<JS::MutableHandleValue>& lineNumber);
+bool gjs_context_get_frame_info(JSContext                             *cx,
+                                mozilla::Maybe<JS::MutableHandleValue> stack,
+                                mozilla::Maybe<JS::MutableHandleValue> fileName,
+                                mozilla::Maybe<JS::MutableHandleValue> lineNumber);
 
 bool gjs_eval_with_scope(JSContext             *context,
                          JS::HandleObject       object,
diff --git a/gjs/stack.cpp b/gjs/stack.cpp
index f1aa8aa..e49a77f 100644
--- a/gjs/stack.cpp
+++ b/gjs/stack.cpp
@@ -48,10 +48,10 @@
 #include "jsapi-wrapper.h"
 
 bool
-gjs_context_get_frame_info(JSContext                              *context,
-                           mozilla::Maybe<JS::MutableHandleValue>& stack,
-                           mozilla::Maybe<JS::MutableHandleValue>& fileName,
-                           mozilla::Maybe<JS::MutableHandleValue>& lineNumber)
+gjs_context_get_frame_info(JSContext                             *context,
+                           mozilla::Maybe<JS::MutableHandleValue> stack,
+                           mozilla::Maybe<JS::MutableHandleValue> fileName,
+                           mozilla::Maybe<JS::MutableHandleValue> lineNumber)
 {
     JSAutoRequest ar(context);
     JS::RootedObject global(context, JS::CurrentGlobalOrNull(context)),
@@ -94,10 +94,8 @@ gjs_context_print_stack_stderr(GjsContext *context)
     g_printerr("== Stack trace for context %p ==\n", context);
 
     /* Stderr is locale encoding, so we use string_to_filename here */
-    /* COMPAT: mozilla::Maybe gains a much more usable API in future versions */
-    mozilla::Maybe<JS::MutableHandleValue> none,
-        m_stack = mozilla::Some<JS::MutableHandleValue>(&v_stack);
-    if (!gjs_context_get_frame_info(cx, m_stack, none, none) ||
+    if (!gjs_context_get_frame_info(cx, mozilla::Some<JS::MutableHandleValue>(&v_stack),
+                                    mozilla::Nothing(), mozilla::Nothing()) ||
         !gjs_string_to_filename(cx, v_stack, &stack)) {
         g_printerr("No stack trace available\n");
         return;


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