WebKit Bugzilla
Attachment 347359 Details for
Bug 188698
: [GLIB] Add API to throw exceptions using printf formatted strings
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Patch
jsc-throw-printf.diff (text/plain), 16.47 KB, created by
Carlos Garcia Campos
on 2018-08-17 05:35:11 PDT
(
hide
)
Description:
Patch
Filename:
MIME Type:
Creator:
Carlos Garcia Campos
Created:
2018-08-17 05:35:11 PDT
Size:
16.47 KB
patch
obsolete
>diff --git a/Source/JavaScriptCore/API/glib/JSCContext.cpp b/Source/JavaScriptCore/API/glib/JSCContext.cpp >index f9def66a560..e21c5659288 100644 >--- a/Source/JavaScriptCore/API/glib/JSCContext.cpp >+++ b/Source/JavaScriptCore/API/glib/JSCContext.cpp >@@ -643,6 +643,25 @@ void jsc_context_throw(JSCContext* context, const char* errorMessage) > context->priv->exception = adoptGRef(jsc_exception_new(context, errorMessage)); > } > >+/** >+ * 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 >@@ -660,6 +679,26 @@ void jsc_context_throw_with_name(JSCContext* context, const char* errorName, con > context->priv->exception = adoptGRef(jsc_exception_new_with_name(context, errorName, errorMessage)); > } > >+/** >+ * 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 >diff --git a/Source/JavaScriptCore/API/glib/JSCContext.h b/Source/JavaScriptCore/API/glib/JSCContext.h >index 899ac26a477..e46494e3849 100644 >--- a/Source/JavaScriptCore/API/glib/JSCContext.h >+++ b/Source/JavaScriptCore/API/glib/JSCContext.h >@@ -97,11 +97,22 @@ JSC_API void > jsc_context_throw (JSCContext *context, > 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); >diff --git a/Source/JavaScriptCore/API/glib/JSCException.cpp b/Source/JavaScriptCore/API/glib/JSCException.cpp >index 2819f1addb2..b6888f03b6d 100644 >--- a/Source/JavaScriptCore/API/glib/JSCException.cpp >+++ b/Source/JavaScriptCore/API/glib/JSCException.cpp >@@ -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> > >@@ -132,6 +133,48 @@ JSCException* jsc_exception_new(JSCContext* context, const char* message) > return jsc_exception_new_with_name(context, nullptr, message); > } > >+/** >+ * 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 >@@ -163,6 +206,50 @@ JSCException* jsc_exception_new_with_name(JSCContext* context, const char* name, > return exception.leakRef(); > } > >+/** >+ * 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 >diff --git a/Source/JavaScriptCore/API/glib/JSCException.h b/Source/JavaScriptCore/API/glib/JSCException.h >index b71b207a998..6dab6407b65 100644 >--- a/Source/JavaScriptCore/API/glib/JSCException.h >+++ b/Source/JavaScriptCore/API/glib/JSCException.h >@@ -65,11 +65,33 @@ JSC_API JSCException * > jsc_exception_new (JSCContext *context, > const char *message); > >+JSC_API JSCException * >+jsc_exception_new_printf (JSCContext *context, >+ const gchar *format, >+ ...) G_GNUC_PRINTF (2, 3); >+ >+JSC_API JSCException * >+jsc_exception_new_vprintf (JSCContext *context, >+ const gchar *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 gchar *format, >+ ...) G_GNUC_PRINTF (3, 4); >+ >+JSC_API JSCException * >+jsc_exception_new_with_name_vprintf (JSCContext *context, >+ const char *name, >+ const gchar *format, >+ va_list args) G_GNUC_PRINTF (3, 0); >+ > JSC_API const char * > jsc_exception_get_name (JSCException *exception); > >diff --git a/Source/JavaScriptCore/API/glib/docs/jsc-glib-4.0-sections.txt b/Source/JavaScriptCore/API/glib/docs/jsc-glib-4.0-sections.txt >index fdff1f6cdc1..add682a481a 100644 >--- a/Source/JavaScriptCore/API/glib/docs/jsc-glib-4.0-sections.txt >+++ b/Source/JavaScriptCore/API/glib/docs/jsc-glib-4.0-sections.txt >@@ -33,7 +33,9 @@ jsc_context_new_with_virtual_machine > 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 @@ jsc_weak_value_get_type > <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 >diff --git a/Source/JavaScriptCore/ChangeLog b/Source/JavaScriptCore/ChangeLog >index 7e100fe09c2..ba11e332bc2 100644 >--- a/Source/JavaScriptCore/ChangeLog >+++ b/Source/JavaScriptCore/ChangeLog >@@ -1,3 +1,25 @@ >+2018-08-17 Carlos Garcia Campos <cgarcia@igalia.com> >+ >+ [GLIB] Add API to throw exceptions using printf formatted strings >+ https://bugs.webkit.org/show_bug.cgi?id=188698 >+ >+ Reviewed by NOBODY (OOPS!). >+ >+ 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-17 Carlos Garcia Campos <cgarcia@igalia.com> > > [GLIB] Complete the JSCException API >diff --git a/Tools/ChangeLog b/Tools/ChangeLog >index 9c02d227312..94165c6b846 100644 >--- a/Tools/ChangeLog >+++ b/Tools/ChangeLog >@@ -1,3 +1,17 @@ >+2018-08-17 Carlos Garcia Campos <cgarcia@igalia.com> >+ >+ [GLIB] Add API to throw exceptions using printf formatted strings >+ https://bugs.webkit.org/show_bug.cgi?id=188698 >+ >+ Reviewed by NOBODY (OOPS!). >+ >+ Add cases to test the new API. >+ >+ * TestWebKitAPI/Tests/JavaScriptCore/glib/TestJSC.cpp: >+ (createFormattedError): >+ (createCustomFormattedError): >+ (testJSCExceptions): >+ > 2018-08-17 Carlos Garcia Campos <cgarcia@igalia.com> > > [GLIB] Complete the JSCException API >diff --git a/Tools/TestWebKitAPI/Tests/JavaScriptCore/glib/TestJSC.cpp b/Tools/TestWebKitAPI/Tests/JavaScriptCore/glib/TestJSC.cpp >index 88acac13320..f7fa6aacdc3 100644 >--- a/Tools/TestWebKitAPI/Tests/JavaScriptCore/glib/TestJSC.cpp >+++ b/Tools/TestWebKitAPI/Tests/JavaScriptCore/glib/TestJSC.cpp >@@ -2495,6 +2495,16 @@ static void createCustomError() > 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() > { > { >@@ -2653,6 +2663,68 @@ static void testJSCExceptions() > 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(), "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());
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Flags:
mcatanzaro
:
review+
Actions:
View
|
Formatted Diff
|
Diff
Attachments on
bug 188698
:
347357
| 347359