Title: [288795] trunk
Revision
288795
Author
[email protected]
Date
2022-01-29 19:26:20 -0800 (Sat, 29 Jan 2022)

Log Message

[WebIDL] Add a sensible error message for a primitive non-null EventListener
https://bugs.webkit.org/show_bug.cgi?id=235863

Patch by Alexey Shvayka <[email protected]> on 2022-01-29
Reviewed by Darin Adler.

Source/WebCore:

This patch moves isObject() check to JSEventListener converter, aligning it with
callback interface converter and removing now unused create() overload, and makes
code generator pass an exception thrower with a user-friendly TypeError message.

Since JSEventListener is quite different from other callback interfaces, we can't
make IsCallbackInterface() recognize it just yet, thus the type name has to be
hard-coded if we want the error message to be consistent with other generated ones.

Test: fast/events/event-listener-not-an-object.html

* bindings/js/JSDOMConvertEventListener.h:
(WebCore::Converter<IDLEventListener<T>>::convert):
* bindings/js/JSEventListener.cpp:
* bindings/js/JSEventListener.h:
* bindings/scripts/CodeGeneratorJS.pm:
(GetArgumentExceptionFunction):
* bindings/scripts/test/JS/*: Updated.
* dom/EventTarget.idl:
Rename EventListener parameter to "listener" to match MDN / Chrome DevTools / matchMedia().

LayoutTests:

* fast/events/event-listener-not-an-object-expected.txt: Added.
* fast/events/event-listener-not-an-object.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (288794 => 288795)


--- trunk/LayoutTests/ChangeLog	2022-01-29 23:15:01 UTC (rev 288794)
+++ trunk/LayoutTests/ChangeLog	2022-01-30 03:26:20 UTC (rev 288795)
@@ -1,3 +1,13 @@
+2022-01-29  Alexey Shvayka  <[email protected]>
+
+        [WebIDL] Add a sensible error message for a primitive non-null EventListener
+        https://bugs.webkit.org/show_bug.cgi?id=235863
+
+        Reviewed by Darin Adler.
+
+        * fast/events/event-listener-not-an-object-expected.txt: Added.
+        * fast/events/event-listener-not-an-object.html: Added.
+
 2022-01-29  Patrick Griffis  <[email protected]>
 
         CSP: Properly block image content in object elements

Added: trunk/LayoutTests/fast/events/event-listener-not-an-object-expected.txt (0 => 288795)


--- trunk/LayoutTests/fast/events/event-listener-not-an-object-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/events/event-listener-not-an-object-expected.txt	2022-01-30 03:26:20 UTC (rev 288795)
@@ -0,0 +1,15 @@
+A TypeError with sensible message should be thrown for a primitive EventListener that is neither null nor undefined
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS () => { (new EventTarget).addEventListener("foo", null); } did not throw exception.
+PASS () => { matchMedia("").removeListener(undefined); } did not throw exception.
+PASS () => { (new EventTarget).addEventListener("foo", 1); } threw exception TypeError: Argument 2 ('listener') to EventTarget.addEventListener must be an object.
+PASS () => { (new EventTarget).removeEventListener("foo", false); } threw exception TypeError: Argument 2 ('listener') to EventTarget.removeEventListener must be an object.
+PASS () => { matchMedia("").addListener(Symbol()); } threw exception TypeError: Argument 1 ('listener') to MediaQueryList.addListener must be an object.
+PASS () => { matchMedia("").removeListener("bar"); } threw exception TypeError: Argument 1 ('listener') to MediaQueryList.removeListener must be an object.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/fast/events/event-listener-not-an-object.html (0 => 288795)


--- trunk/LayoutTests/fast/events/event-listener-not-an-object.html	                        (rev 0)
+++ trunk/LayoutTests/fast/events/event-listener-not-an-object.html	2022-01-30 03:26:20 UTC (rev 288795)
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<body>
+<script src=""
+<script>
+description("A TypeError with sensible message should be thrown for a primitive EventListener that is neither null nor undefined");
+
+shouldNotThrow(() => { (new EventTarget).addEventListener("foo", null); });
+shouldNotThrow(() => { matchMedia("").removeListener(undefined); });
+
+shouldThrowErrorName(() => { (new EventTarget).addEventListener("foo", 1); }, "TypeError");
+shouldThrowErrorName(() => { (new EventTarget).removeEventListener("foo", false); }, "TypeError");
+shouldThrowErrorName(() => { matchMedia("").addListener(Symbol()); }, "TypeError");
+shouldThrowErrorName(() => { matchMedia("").removeListener("bar"); }, "TypeError");
+</script>
+<script src=""
+</body>

Modified: trunk/Source/WebCore/ChangeLog (288794 => 288795)


--- trunk/Source/WebCore/ChangeLog	2022-01-29 23:15:01 UTC (rev 288794)
+++ trunk/Source/WebCore/ChangeLog	2022-01-30 03:26:20 UTC (rev 288795)
@@ -1,3 +1,30 @@
+2022-01-29  Alexey Shvayka  <[email protected]>
+
+        [WebIDL] Add a sensible error message for a primitive non-null EventListener
+        https://bugs.webkit.org/show_bug.cgi?id=235863
+
+        Reviewed by Darin Adler.
+
+        This patch moves isObject() check to JSEventListener converter, aligning it with
+        callback interface converter and removing now unused create() overload, and makes
+        code generator pass an exception thrower with a user-friendly TypeError message.
+
+        Since JSEventListener is quite different from other callback interfaces, we can't
+        make IsCallbackInterface() recognize it just yet, thus the type name has to be
+        hard-coded if we want the error message to be consistent with other generated ones.
+
+        Test: fast/events/event-listener-not-an-object.html
+
+        * bindings/js/JSDOMConvertEventListener.h:
+        (WebCore::Converter<IDLEventListener<T>>::convert):
+        * bindings/js/JSEventListener.cpp:
+        * bindings/js/JSEventListener.h:
+        * bindings/scripts/CodeGeneratorJS.pm:
+        (GetArgumentExceptionFunction):
+        * bindings/scripts/test/JS/*: Updated.
+        * dom/EventTarget.idl:
+        Rename EventListener parameter to "listener" to match MDN / Chrome DevTools / matchMedia().
+
 2022-01-29  Ryosuke Niwa  <[email protected]>
 
         Delete EventQueue.h

Modified: trunk/Source/WebCore/bindings/js/JSDOMConvertEventListener.h (288794 => 288795)


--- trunk/Source/WebCore/bindings/js/JSDOMConvertEventListener.h	2022-01-29 23:15:01 UTC (rev 288794)
+++ trunk/Source/WebCore/bindings/js/JSDOMConvertEventListener.h	2022-01-30 03:26:20 UTC (rev 288795)
@@ -33,15 +33,18 @@
 template<typename T> struct Converter<IDLEventListener<T>> : DefaultConverter<IDLEventListener<T>> {
     using ReturnType = RefPtr<T>;
 
-    static ReturnType convert(JSC::JSGlobalObject& lexicalGlobalObject, JSC::JSValue value, JSC::JSObject& thisObject)
+    template<typename ExceptionThrower = DefaultExceptionThrower>
+    static ReturnType convert(JSC::JSGlobalObject& lexicalGlobalObject, JSC::JSValue value, JSC::JSObject& thisObject, ExceptionThrower&& exceptionThrower = ExceptionThrower())
     {
         auto scope = DECLARE_THROW_SCOPE(JSC::getVM(&lexicalGlobalObject));
 
-        auto listener = T::create(value, thisObject, false, currentWorld(lexicalGlobalObject));
-        if (!listener)
-            throwTypeError(&lexicalGlobalObject, scope);
-    
-        return listener;
+        if (UNLIKELY(!value.isObject())) {
+            exceptionThrower(lexicalGlobalObject, scope);
+            return nullptr;
+        }
+
+        bool isAttribute = false;
+        return T::create(*asObject(value), thisObject, isAttribute, currentWorld(lexicalGlobalObject));
     }
 };
 

Modified: trunk/Source/WebCore/bindings/js/JSEventListener.cpp (288794 => 288795)


--- trunk/Source/WebCore/bindings/js/JSEventListener.cpp	2022-01-29 23:15:01 UTC (rev 288794)
+++ trunk/Source/WebCore/bindings/js/JSEventListener.cpp	2022-01-30 03:26:20 UTC (rev 288795)
@@ -66,14 +66,6 @@
     return adoptRef(*new JSEventListener(&listener, &wrapper, isAttribute, world));
 }
 
-RefPtr<JSEventListener> JSEventListener::create(JSC::JSValue listener, JSC::JSObject& wrapper, bool isAttribute, DOMWrapperWorld& world)
-{
-    if (UNLIKELY(!listener.isObject()))
-        return nullptr;
-
-    return adoptRef(*new JSEventListener(asObject(listener), &wrapper, isAttribute, world));
-}
-
 JSObject* JSEventListener::initializeJSFunction(ScriptExecutionContext&) const
 {
     return nullptr;

Modified: trunk/Source/WebCore/bindings/js/JSEventListener.h (288794 => 288795)


--- trunk/Source/WebCore/bindings/js/JSEventListener.h	2022-01-29 23:15:01 UTC (rev 288794)
+++ trunk/Source/WebCore/bindings/js/JSEventListener.h	2022-01-30 03:26:20 UTC (rev 288795)
@@ -37,7 +37,6 @@
 class JSEventListener : public EventListener {
 public:
     WEBCORE_EXPORT static Ref<JSEventListener> create(JSC::JSObject& listener, JSC::JSObject& wrapper, bool isAttribute, DOMWrapperWorld&);
-    WEBCORE_EXPORT static RefPtr<JSEventListener> create(JSC::JSValue listener, JSC::JSObject& wrapper, bool isAttribute, DOMWrapperWorld&);
 
     virtual ~JSEventListener();
 

Modified: trunk/Source/WebCore/bindings/scripts/CodeGeneratorJS.pm (288794 => 288795)


--- trunk/Source/WebCore/bindings/scripts/CodeGeneratorJS.pm	2022-01-29 23:15:01 UTC (rev 288794)
+++ trunk/Source/WebCore/bindings/scripts/CodeGeneratorJS.pm	2022-01-30 03:26:20 UTC (rev 288795)
@@ -1745,7 +1745,7 @@
     my $visibleInterfaceName = $codeGenerator->GetVisibleInterfaceName($interface);
     my $typeName = GetTypeNameForDisplayInException($argument->type);
 
-    if ($codeGenerator->IsCallbackInterface($argument->type)) {
+    if ($codeGenerator->IsCallbackInterface($argument->type) || $argument->type->name eq "EventListener") {
         return "throwArgumentMustBeObjectError(lexicalGlobalObject, scope, ${argumentIndex}, \"${name}\", \"${visibleInterfaceName}\", ${quotedFunctionName});";
     }
 

Modified: trunk/Source/WebCore/bindings/scripts/test/JS/JSTestObj.cpp (288794 => 288795)


--- trunk/Source/WebCore/bindings/scripts/test/JS/JSTestObj.cpp	2022-01-29 23:15:01 UTC (rev 288794)
+++ trunk/Source/WebCore/bindings/scripts/test/JS/JSTestObj.cpp	2022-01-30 03:26:20 UTC (rev 288795)
@@ -6168,7 +6168,7 @@
     auto type = convert<IDLDOMString>(*lexicalGlobalObject, argument0.value());
     RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
     EnsureStillAliveScope argument1 = callFrame->uncheckedArgument(1);
-    auto listener = convert<IDLEventListener<JSEventListener>>(*lexicalGlobalObject, argument1.value(), *castedThis);
+    auto listener = convert<IDLEventListener<JSEventListener>>(*lexicalGlobalObject, argument1.value(), *castedThis, [](JSC::JSGlobalObject& lexicalGlobalObject, JSC::ThrowScope& scope) { throwArgumentMustBeObjectError(lexicalGlobalObject, scope, 1, "listener", "TestObject", "addEventListener"); });
     RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
     EnsureStillAliveScope argument2 = callFrame->argument(2);
     auto useCapture = convert<IDLBoolean>(*lexicalGlobalObject, argument2.value());
@@ -6197,7 +6197,7 @@
     auto type = convert<IDLDOMString>(*lexicalGlobalObject, argument0.value());
     RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
     EnsureStillAliveScope argument1 = callFrame->uncheckedArgument(1);
-    auto listener = convert<IDLEventListener<JSEventListener>>(*lexicalGlobalObject, argument1.value(), *castedThis);
+    auto listener = convert<IDLEventListener<JSEventListener>>(*lexicalGlobalObject, argument1.value(), *castedThis, [](JSC::JSGlobalObject& lexicalGlobalObject, JSC::ThrowScope& scope) { throwArgumentMustBeObjectError(lexicalGlobalObject, scope, 1, "listener", "TestObject", "removeEventListener"); });
     RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
     EnsureStillAliveScope argument2 = callFrame->argument(2);
     auto useCapture = convert<IDLBoolean>(*lexicalGlobalObject, argument2.value());

Modified: trunk/Source/WebCore/dom/EventTarget.idl (288794 => 288795)


--- trunk/Source/WebCore/dom/EventTarget.idl	2022-01-29 23:15:01 UTC (rev 288794)
+++ trunk/Source/WebCore/dom/EventTarget.idl	2022-01-30 03:26:20 UTC (rev 288795)
@@ -28,7 +28,7 @@
 ] interface EventTarget {
     [CallWith=ScriptExecutionContext] constructor();
 
-    [ImplementedAs=addEventListenerForBindings] undefined addEventListener([AtomString] DOMString type, EventListener? callback, optional (AddEventListenerOptions or boolean) options = false);
-    [ImplementedAs=removeEventListenerForBindings] undefined removeEventListener([AtomString] DOMString type, EventListener? callback, optional (EventListenerOptions or boolean) options = false);
+    [ImplementedAs=addEventListenerForBindings] undefined addEventListener([AtomString] DOMString type, EventListener? listener, optional (AddEventListenerOptions or boolean) options = false);
+    [ImplementedAs=removeEventListenerForBindings] undefined removeEventListener([AtomString] DOMString type, EventListener? listener, optional (EventListenerOptions or boolean) options = false);
     [ImplementedAs=dispatchEventForBindings] boolean dispatchEvent(Event event);
 };
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to