Title: [235024] trunk
Revision
235024
Author
[email protected]
Date
2018-08-20 00:00:36 -0700 (Mon, 20 Aug 2018)

Log Message

[GLIB] Add API to throw exceptions using printf formatted strings
https://bugs.webkit.org/show_bug.cgi?id=188698

Reviewed by Michael Catanzaro.

Source/_javascript_Core:

Add jsc_context_throw_printf() and jsc_context_throw_with_name_printf(). Also add new public constructors of
JSCException using printf formatted string.

* API/glib/JSCContext.cpp:
(jsc_context_throw_printf):
(jsc_context_throw_with_name_printf):
* API/glib/JSCContext.h:
* API/glib/JSCException.cpp:
(jsc_exception_new_printf):
(jsc_exception_new_vprintf):
(jsc_exception_new_with_name_printf):
(jsc_exception_new_with_name_vprintf):
* API/glib/JSCException.h:
* API/glib/docs/jsc-glib-4.0-sections.txt:

Tools:

Add cases to test the new API.

* TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp:
(createFormattedError):
(createCustomFormattedError):
(testJSCExceptions):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/API/glib/JSCContext.cpp (235023 => 235024)


--- trunk/Source/_javascript_Core/API/glib/JSCContext.cpp	2018-08-20 06:57:12 UTC (rev 235023)
+++ trunk/Source/_javascript_Core/API/glib/JSCContext.cpp	2018-08-20 07:00:36 UTC (rev 235024)
@@ -644,6 +644,25 @@
 }
 
 /**
+ * jsc_context_throw_printf:
+ * @context: a #JSCContext
+ * @format: the string format
+ * @...: the parameters to insert into the format string
+ *
+ * Throw an exception to @context using the given formatted string as error message.
+ * The created #JSCException can be retrieved with jsc_context_get_exception().
+ */
+void jsc_context_throw_printf(JSCContext* context, const char* format, ...)
+{
+    g_return_if_fail(JSC_IS_CONTEXT(context));
+
+    va_list args;
+    va_start(args, format);
+    context->priv->exception = adoptGRef(jsc_exception_new_vprintf(context, format, args));
+    va_end(args);
+}
+
+/**
  * jsc_context_throw_with_name:
  * @context: a #JSCContext
  * @error_name: the error name
@@ -661,6 +680,26 @@
 }
 
 /**
+ * jsc_context_throw_with_name_printf:
+ * @context: a #JSCContext
+ * @error_name: the error name
+ * @format: the string format
+ * @...: the parameters to insert into the format string
+ *
+ * Throw an exception to @context using the given error name and the formatted string as error message.
+ * The created #JSCException can be retrieved with jsc_context_get_exception().
+ */
+void jsc_context_throw_with_name_printf(JSCContext* context, const char* errorName, const char* format, ...)
+{
+    g_return_if_fail(JSC_IS_CONTEXT(context));
+
+    va_list args;
+    va_start(args, format);
+    context->priv->exception = adoptGRef(jsc_exception_new_with_name_vprintf(context, errorName, format, args));
+    va_end(args);
+}
+
+/**
  * jsc_context_throw_exception:
  * @context: a #JSCContext
  * @exception: a #JSCException

Modified: trunk/Source/_javascript_Core/API/glib/JSCContext.h (235023 => 235024)


--- trunk/Source/_javascript_Core/API/glib/JSCContext.h	2018-08-20 06:57:12 UTC (rev 235023)
+++ trunk/Source/_javascript_Core/API/glib/JSCContext.h	2018-08-20 07:00:36 UTC (rev 235024)
@@ -98,11 +98,22 @@
                                       const char         *error_message);
 
 JSC_API void
+jsc_context_throw_printf             (JSCContext         *context,
+                                      const char         *format,
+                                      ...) G_GNUC_PRINTF (2, 3);
+
+JSC_API void
 jsc_context_throw_with_name          (JSCContext         *context,
                                       const char         *error_name,
                                       const char         *error_message);
 
 JSC_API void
+jsc_context_throw_with_name_printf   (JSCContext         *context,
+                                      const char         *error_name,
+                                      const char         *format,
+                                      ...) G_GNUC_PRINTF (3, 4);
+
+JSC_API void
 jsc_context_throw_exception          (JSCContext         *context,
                                       JSCException       *exception);
 

Modified: trunk/Source/_javascript_Core/API/glib/JSCException.cpp (235023 => 235024)


--- trunk/Source/_javascript_Core/API/glib/JSCException.cpp	2018-08-20 06:57:12 UTC (rev 235023)
+++ trunk/Source/_javascript_Core/API/glib/JSCException.cpp	2018-08-20 07:00:36 UTC (rev 235024)
@@ -26,6 +26,7 @@
 #include "JSCInlines.h"
 #include "JSRetainPtr.h"
 #include "StrongInlines.h"
+#include <glib/gprintf.h>
 #include <wtf/glib/GUniquePtr.h>
 #include <wtf/glib/WTFGType.h>
 
@@ -133,6 +134,48 @@
 }
 
 /**
+ * jsc_exception_new_printf:
+ * @context: a #JSCContext
+ * @format: the string format
+ * @...: the parameters to insert into the format string
+ *
+ * Create a new #JSCException in @context using a formatted string
+ * for the message.
+ *
+ * Returns: (transfer full): a new #JSCException.
+ */
+JSCException* jsc_exception_new_printf(JSCContext* context, const char* format, ...)
+{
+    va_list args;
+    va_start(args, format);
+    auto* exception = jsc_exception_new_vprintf(context, format, args);
+    va_end(args);
+
+    return exception;
+}
+
+/**
+ * jsc_exception_new_vprintf:
+ * @context: a #JSCContext
+ * @format: the string format
+ * @args: the parameters to insert into the format string
+ *
+ * Create a new #JSCException in @context using a formatted string
+ * for the message. This is similar to jsc_exception_new_printf()
+ * except that the arguments to the format string are passed as a va_list.
+ *
+ * Returns: (transfer full): a new #JSCException.
+ */
+JSCException* jsc_exception_new_vprintf(JSCContext* context, const char* format, va_list args)
+{
+    g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
+
+    GUniqueOutPtr<char> buffer;
+    g_vasprintf(&buffer.outPtr(), format, args);
+    return jsc_exception_new(context, buffer.get());
+}
+
+/**
  * jsc_exception_new_with_name:
  * @context: a #JSCContext
  * @name: the error name
@@ -164,6 +207,50 @@
 }
 
 /**
+ * jsc_exception_new_with_name_printf:
+ * @context: a #JSCContext
+ * @name: the error name
+ * @format: the string format
+ * @...: the parameters to insert into the format string
+ *
+ * Create a new #JSCException in @context with @name and using a formatted string
+ * for the message.
+ *
+ * Returns: (transfer full): a new #JSCException.
+ */
+JSCException* jsc_exception_new_with_name_printf(JSCContext* context, const char* name, const char* format, ...)
+{
+    va_list args;
+    va_start(args, format);
+    auto* exception = jsc_exception_new_with_name_vprintf(context, name, format, args);
+    va_end(args);
+
+    return exception;
+}
+
+/**
+ * jsc_exception_new_with_name_vprintf:
+ * @context: a #JSCContext
+ * @name: the error name
+ * @format: the string format
+ * @args: the parameters to insert into the format string
+ *
+ * Create a new #JSCException in @context with @name and using a formatted string
+ * for the message. This is similar to jsc_exception_new_with_name_printf()
+ * except that the arguments to the format string are passed as a va_list.
+ *
+ * Returns: (transfer full): a new #JSCException.
+ */
+JSCException* jsc_exception_new_with_name_vprintf(JSCContext* context, const char* name, const char* format, va_list args)
+{
+    g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
+
+    GUniqueOutPtr<char> buffer;
+    g_vasprintf(&buffer.outPtr(), format, args);
+    return jsc_exception_new_with_name(context, name, buffer.get());
+}
+
+/**
  * jsc_exception_get_name:
  * @exception: a #JSCException
  *

Modified: trunk/Source/_javascript_Core/API/glib/JSCException.h (235023 => 235024)


--- trunk/Source/_javascript_Core/API/glib/JSCException.h	2018-08-20 06:57:12 UTC (rev 235023)
+++ trunk/Source/_javascript_Core/API/glib/JSCException.h	2018-08-20 07:00:36 UTC (rev 235024)
@@ -66,10 +66,32 @@
                                      const char   *message);
 
 JSC_API JSCException *
+jsc_exception_new_printf            (JSCContext   *context,
+                                     const char   *format,
+                                     ...) G_GNUC_PRINTF (2, 3);
+
+JSC_API JSCException *
+jsc_exception_new_vprintf           (JSCContext   *context,
+                                     const char   *format,
+                                     va_list       args) G_GNUC_PRINTF(2, 0);
+
+JSC_API JSCException *
 jsc_exception_new_with_name         (JSCContext   *context,
                                      const char   *name,
                                      const char   *message);
 
+JSC_API JSCException *
+jsc_exception_new_with_name_printf  (JSCContext   *context,
+                                     const char   *name,
+                                     const char   *format,
+                                     ...) G_GNUC_PRINTF (3, 4);
+
+JSC_API JSCException *
+jsc_exception_new_with_name_vprintf (JSCContext   *context,
+                                     const char   *name,
+                                     const char   *format,
+                                     va_list       args) G_GNUC_PRINTF (3, 0);
+
 JSC_API const char *
 jsc_exception_get_name              (JSCException *exception);
 

Modified: trunk/Source/_javascript_Core/API/glib/docs/jsc-glib-4.0-sections.txt (235023 => 235024)


--- trunk/Source/_javascript_Core/API/glib/docs/jsc-glib-4.0-sections.txt	2018-08-20 06:57:12 UTC (rev 235023)
+++ trunk/Source/_javascript_Core/API/glib/docs/jsc-glib-4.0-sections.txt	2018-08-20 07:00:36 UTC (rev 235024)
@@ -33,7 +33,9 @@
 jsc_context_get_virtual_machine
 jsc_context_get_exception
 jsc_context_throw
+jsc_context_throw_printf
 jsc_context_throw_with_name
+jsc_context_throw_with_name_printf
 jsc_context_throw_exception
 jsc_context_clear_exception
 jsc_context_push_exception_handler
@@ -152,7 +154,11 @@
 <TITLE>JSCException</TITLE>
 JSCException
 jsc_exception_new
+jsc_exception_new_printf
+jsc_exception_new_vprintf
 jsc_exception_new_with_name
+jsc_exception_new_with_name_printf
+jsc_exception_new_with_name_vprintf
 jsc_exception_get_name
 jsc_exception_get_message
 jsc_exception_get_line_number

Modified: trunk/Source/_javascript_Core/ChangeLog (235023 => 235024)


--- trunk/Source/_javascript_Core/ChangeLog	2018-08-20 06:57:12 UTC (rev 235023)
+++ trunk/Source/_javascript_Core/ChangeLog	2018-08-20 07:00:36 UTC (rev 235024)
@@ -1,5 +1,27 @@
 2018-08-19  Carlos Garcia Campos  <[email protected]>
 
+        [GLIB] Add API to throw exceptions using printf formatted strings
+        https://bugs.webkit.org/show_bug.cgi?id=188698
+
+        Reviewed by Michael Catanzaro.
+
+        Add jsc_context_throw_printf() and jsc_context_throw_with_name_printf(). Also add new public constructors of
+        JSCException using printf formatted string.
+
+        * API/glib/JSCContext.cpp:
+        (jsc_context_throw_printf):
+        (jsc_context_throw_with_name_printf):
+        * API/glib/JSCContext.h:
+        * API/glib/JSCException.cpp:
+        (jsc_exception_new_printf):
+        (jsc_exception_new_vprintf):
+        (jsc_exception_new_with_name_printf):
+        (jsc_exception_new_with_name_vprintf):
+        * API/glib/JSCException.h:
+        * API/glib/docs/jsc-glib-4.0-sections.txt:
+
+2018-08-19  Carlos Garcia Campos  <[email protected]>
+
         [GLIB] Complete the JSCException API
         https://bugs.webkit.org/show_bug.cgi?id=188695
 

Modified: trunk/Tools/ChangeLog (235023 => 235024)


--- trunk/Tools/ChangeLog	2018-08-20 06:57:12 UTC (rev 235023)
+++ trunk/Tools/ChangeLog	2018-08-20 07:00:36 UTC (rev 235024)
@@ -1,5 +1,19 @@
 2018-08-19  Carlos Garcia Campos  <[email protected]>
 
+        [GLIB] Add API to throw exceptions using printf formatted strings
+        https://bugs.webkit.org/show_bug.cgi?id=188698
+
+        Reviewed by Michael Catanzaro.
+
+        Add cases to test the new API.
+
+        * TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp:
+        (createFormattedError):
+        (createCustomFormattedError):
+        (testJSCExceptions):
+
+2018-08-19  Carlos Garcia Campos  <[email protected]>
+
         [GLIB] Complete the JSCException API
         https://bugs.webkit.org/show_bug.cgi?id=188695
 

Modified: trunk/Tools/TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp (235023 => 235024)


--- trunk/Tools/TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp	2018-08-20 06:57:12 UTC (rev 235023)
+++ trunk/Tools/TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp	2018-08-20 07:00:36 UTC (rev 235024)
@@ -2495,6 +2495,16 @@
     jsc_context_throw_with_name(jsc_context_get_current(), "CustomAPIError", "API custom exception");
 }
 
+static void createFormattedError(const char* details)
+{
+    jsc_context_throw_printf(jsc_context_get_current(), "API exception: %s", details);
+}
+
+static void createCustomFormattedError(const char* details)
+{
+    jsc_context_throw_with_name_printf(jsc_context_get_current(), "CustomFormattedAPIError", "API custom exception: %s", details);
+}
+
 static void testJSCExceptions()
 {
     {
@@ -2659,6 +2669,68 @@
         checker.watch(context.get());
         g_assert_false(jsc_context_get_exception(context.get()));
 
+        GRefPtr<JSCValue> function = adoptGRef(jsc_value_new_function(context.get(), "createFormattedError", G_CALLBACK(createFormattedError), nullptr, nullptr, G_TYPE_NONE, 1, G_TYPE_STRING));
+        checker.watch(function.get());
+        jsc_context_set_value(context.get(), "createFormattedError", function.get());
+
+        GRefPtr<JSCValue> result = adoptGRef(jsc_context_evaluate(context.get(), "var result; createFormattedError('error details'); result = 'No exception';", -1));
+        checker.watch(result.get());
+        g_assert_true(jsc_value_is_undefined(result.get()));
+        auto* exception = jsc_context_get_exception(context.get());
+        g_assert_true(JSC_IS_EXCEPTION(exception));
+        checker.watch(exception);
+        g_assert_cmpstr(jsc_exception_get_name(exception), ==, "Error");
+        g_assert_cmpstr(jsc_exception_get_message(exception), ==, "API exception: error details");
+        g_assert_cmpuint(jsc_exception_get_line_number(exception), ==, 1);
+        g_assert_cmpuint(jsc_exception_get_column_number(exception), ==, 33);
+        g_assert_null(jsc_exception_get_source_uri(exception));
+        g_assert_cmpstr(jsc_exception_get_backtrace_string(exception), ==, "createFormattedError@[native code]\nglobal code");
+        GUniquePtr<char> errorString(jsc_exception_to_string(exception));
+        g_assert_cmpstr(errorString.get(), ==, "Error: API exception: error details");
+        GUniquePtr<char> reportString(jsc_exception_report(exception));
+        g_assert_cmpstr(reportString.get(), ==, ":1:33 Error: API exception: error details\n  createFormattedError@[native code]\n  global code\n");
+
+        jsc_context_clear_exception(context.get());
+        g_assert_null(jsc_context_get_exception(context.get()));
+    }
+
+    {
+        LeakChecker checker;
+        GRefPtr<JSCContext> context = adoptGRef(jsc_context_new());
+        checker.watch(context.get());
+        g_assert_false(jsc_context_get_exception(context.get()));
+
+        GRefPtr<JSCValue> function = adoptGRef(jsc_value_new_function(context.get(), "createCustomFormattedError", G_CALLBACK(createCustomFormattedError), nullptr, nullptr, G_TYPE_NONE, 1, G_TYPE_STRING));
+        checker.watch(function.get());
+        jsc_context_set_value(context.get(), "createCustomFormattedError", function.get());
+
+        GRefPtr<JSCValue> result = adoptGRef(jsc_context_evaluate(context.get(), "var result; createCustomFormattedError('error details'); result = 'No exception';", -1));
+        checker.watch(result.get());
+        g_assert_true(jsc_value_is_undefined(result.get()));
+        auto* exception = jsc_context_get_exception(context.get());
+        g_assert_true(JSC_IS_EXCEPTION(exception));
+        checker.watch(exception);
+        g_assert_cmpstr(jsc_exception_get_name(exception), ==, "CustomFormattedAPIError");
+        g_assert_cmpstr(jsc_exception_get_message(exception), ==, "API custom exception: error details");
+        g_assert_cmpuint(jsc_exception_get_line_number(exception), ==, 1);
+        g_assert_cmpuint(jsc_exception_get_column_number(exception), ==, 39);
+        g_assert_null(jsc_exception_get_source_uri(exception));
+        g_assert_cmpstr(jsc_exception_get_backtrace_string(exception), ==, "createCustomFormattedError@[native code]\nglobal code");
+        GUniquePtr<char> errorString(jsc_exception_to_string(exception));
+        g_assert_cmpstr(errorString.get(), ==, "CustomFormattedAPIError: API custom exception: error details");
+        GUniquePtr<char> reportString(jsc_exception_report(exception));
+        g_assert_cmpstr(reportString.get(), ==, ":1:39 CustomFormattedAPIError: API custom exception: error details\n  createCustomFormattedError@[native code]\n  global code\n");
+
+        jsc_context_clear_exception(context.get());
+        g_assert_null(jsc_context_get_exception(context.get()));
+    }
+
+    {
+        LeakChecker checker;
+        GRefPtr<JSCContext> context = adoptGRef(jsc_context_new());
+        checker.watch(context.get());
+        g_assert_false(jsc_context_get_exception(context.get()));
+
         GRefPtr<JSCValue> result = adoptGRef(jsc_context_evaluate_with_source_uri(context.get(), "foo", -1, "file:///foo/script.js", 3));
         checker.watch(result.get());
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to