[gjs: 8/25] js: Use new warnings API



commit cec9a2348f4216c9b12d6b9fc12cebadbb32d196
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat May 18 23:54:33 2019 -0700

    js: Use new warnings API
    
    The new warnings API is JS::WarnUTF8(), JS::WarnLatin1(), and
    JS::WarnASCII(). It can now fail, in the case where the warning was
    upgraded into an error.
    
    There was one instance where we used the old ASCII API which is now
    replaced by the UTF8 API. It is possible, though extremely unlikely,
    that a boxed union type might have a non-ASCII name, so just change it
    to UTF8 here.

 gi/function.cpp     | 10 +++++++---
 gi/object.cpp       | 12 ++++++------
 gi/repo.cpp         | 14 +++++++-------
 gi/union.cpp        |  8 +++++---
 gjs/engine.cpp      |  1 +
 modules/console.cpp |  1 +
 6 files changed, 27 insertions(+), 19 deletions(-)
---
diff --git a/gi/function.cpp b/gi/function.cpp
index 722b243a..4b70b71b 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -34,6 +34,7 @@
 #include <glib.h>
 
 #include "gjs/jsapi-wrapper.h"
+#include "js/Warnings.h"
 #include "mozilla/Maybe.h"
 
 #include "gi/arg.h"
@@ -826,9 +827,12 @@ gjs_invoke_c_function(JSContext                             *context,
      */
     if (args.length() > function->expected_js_argc) {
         GjsAutoChar name = format_function_name(function, is_method);
-        JS_ReportWarningUTF8(context, "Too many arguments to %s: expected %d, "
-                             "got %" G_GSIZE_FORMAT, name.get(),
-                             function->expected_js_argc, args.length());
+        if (!JS::WarnUTF8(context,
+                          "Too many arguments to %s: expected %d, "
+                          "got %" G_GSIZE_FORMAT,
+                          name.get(), function->expected_js_argc,
+                          args.length()))
+            return false;
     } else if (args.length() < function->expected_js_argc) {
         GjsAutoChar name = format_function_name(function, is_method);
         gjs_throw(context, "Too few arguments to %s: "
diff --git a/gi/object.cpp b/gi/object.cpp
index c8560205..affac701 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1434,12 +1434,12 @@ ObjectInstance::init_impl(JSContext              *context,
 
     g_assert(gtype() != G_TYPE_NONE);
 
-    if (args.length() > 1) {
-        JS_ReportWarningUTF8(context,
-                             "Too many arguments to the constructor of %s: "
-                             "expected 1, got %u",
-                             name(), args.length());
-    }
+    if (args.length() > 1 &&
+        !JS::WarnUTF8(context,
+                      "Too many arguments to the constructor of %s: expected "
+                      "1, got %u",
+                      name(), args.length()))
+        return false;
 
     std::vector<const char *> names;
     AutoGValueVector values;
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 061b6f68..4c83e926 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -29,6 +29,7 @@
 #include <glib.h>
 
 #include "gjs/jsapi-wrapper.h"
+#include "js/Warnings.h"
 
 #include "gi/arg.h"
 #include "gi/boxed.h"
@@ -104,13 +105,12 @@ static bool resolve_namespace_object(JSContext* context,
     GList* versions = g_irepository_enumerate_versions(nullptr, ns_name.get());
     unsigned nversions = g_list_length(versions);
     if (nversions > 1 && !version &&
-        !g_irepository_is_registered(nullptr, ns_name.get(), nullptr)) {
-        GjsAutoChar warn_text = g_strdup_printf(
-            "Requiring %s but it has %u versions available; use "
-            "imports.gi.versions to pick one",
-            ns_name.get(), nversions);
-        JS_ReportWarningUTF8(context, "%s", warn_text.get());
-    }
+        !g_irepository_is_registered(nullptr, ns_name.get(), nullptr) &&
+        !JS::WarnUTF8(context,
+                      "Requiring %s but it has %u versions available; use "
+                      "imports.gi.versions to pick one",
+                      ns_name.get(), nversions))
+        return false;
     g_list_free_full(versions, g_free);
 
     error = NULL;
diff --git a/gi/union.cpp b/gi/union.cpp
index 56540599..3e8ab58e 100644
--- a/gi/union.cpp
+++ b/gi/union.cpp
@@ -24,6 +24,7 @@
 #include <girepository.h>
 
 #include "gjs/jsapi-wrapper.h"
+#include "js/Warnings.h"
 
 #include "gi/function.h"
 #include "gi/repo.h"
@@ -142,9 +143,10 @@ union_new(JSContext       *context,
 bool UnionInstance::constructor_impl(JSContext* context,
                                      JS::HandleObject object,
                                      const JS::CallArgs& args) {
-    if (args.length() > 0)
-        JS_ReportWarningASCII(context, "Arguments to constructor of %s ignored",
-                              name());
+    if (args.length() > 0 &&
+        !JS::WarnUTF8(context, "Arguments to constructor of %s ignored",
+                      name()))
+        return false;
 
     /* union_new happens to be implemented by calling
      * gjs_invoke_c_function(), which returns a JS::Value.
diff --git a/gjs/engine.cpp b/gjs/engine.cpp
index b9307639..0bded2a1 100644
--- a/gjs/engine.cpp
+++ b/gjs/engine.cpp
@@ -35,6 +35,7 @@
 
 #include "gjs/jsapi-wrapper.h"
 #include "js/Initialization.h"  // for JS_Init, JS_ShutDown
+#include "js/Warnings.h"
 #include "mozilla/UniquePtr.h"
 
 #include "gi/object.h"
diff --git a/modules/console.cpp b/modules/console.cpp
index 3426c99f..723954a5 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -52,6 +52,7 @@
 #include <glib/gprintf.h>  // for g_fprintf
 
 #include "gjs/jsapi-wrapper.h"
+#include "js/Warnings.h"
 #include "mozilla/UniquePtr.h"
 #include "mozilla/Unused.h"
 


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