Title: [264201] trunk/Source/WebCore
Revision
264201
Author
[email protected]
Date
2020-07-09 15:41:21 -0700 (Thu, 09 Jul 2020)

Log Message

[Bindings] Add default value support for union types in dictionary members
https://bugs.webkit.org/show_bug.cgi?id=214160

Reviewed by Sam Weinig.

Add default value support for union types in dictionary members. This is needed for the WebAudio specification:
- https://www.w3.org/TR/webaudio/#AudioContextOptions
"""
(AudioContextLatencyCategory or double) latencyHint = "interactive";
"""

* bindings/scripts/CodeGeneratorJS.pm:
(GenerateDefaultValue):
* bindings/scripts/test/JS/JSTestObj.cpp:
(WebCore::jsTestObjPrototypeFunctionOverloadWithOptionalUnion1Body):
* bindings/scripts/test/JS/JSTestStandaloneDictionary.cpp:
(WebCore::convertDictionary<DictionaryImplName>):
(WebCore::convertDictionaryToJS):
* bindings/scripts/test/TestStandaloneDictionary.idl:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (264200 => 264201)


--- trunk/Source/WebCore/ChangeLog	2020-07-09 22:28:37 UTC (rev 264200)
+++ trunk/Source/WebCore/ChangeLog	2020-07-09 22:41:21 UTC (rev 264201)
@@ -1,3 +1,25 @@
+2020-07-09  Chris Dumez  <[email protected]>
+
+        [Bindings] Add default value support for union types in dictionary members
+        https://bugs.webkit.org/show_bug.cgi?id=214160
+
+        Reviewed by Sam Weinig.
+
+        Add default value support for union types in dictionary members. This is needed for the WebAudio specification:
+        - https://www.w3.org/TR/webaudio/#AudioContextOptions
+        """
+        (AudioContextLatencyCategory or double) latencyHint = "interactive";
+        """
+
+        * bindings/scripts/CodeGeneratorJS.pm:
+        (GenerateDefaultValue):
+        * bindings/scripts/test/JS/JSTestObj.cpp:
+        (WebCore::jsTestObjPrototypeFunctionOverloadWithOptionalUnion1Body):
+        * bindings/scripts/test/JS/JSTestStandaloneDictionary.cpp:
+        (WebCore::convertDictionary<DictionaryImplName>):
+        (WebCore::convertDictionaryToJS):
+        * bindings/scripts/test/TestStandaloneDictionary.idl:
+
 2020-07-09  Per Arne Vollan  <[email protected]>
 
         Move XPCEndpoint and XPCEndpointClient classes from WebCore to WebKit

Modified: trunk/Source/WebCore/bindings/scripts/CodeGeneratorJS.pm (264200 => 264201)


--- trunk/Source/WebCore/bindings/scripts/CodeGeneratorJS.pm	2020-07-09 22:28:37 UTC (rev 264200)
+++ trunk/Source/WebCore/bindings/scripts/CodeGeneratorJS.pm	2020-07-09 22:41:21 UTC (rev 264201)
@@ -2210,28 +2210,6 @@
 {
     my ($typeScope, $context, $type, $defaultValue) = @_;
 
-    if ($codeGenerator->IsStringType($type)) {
-        my $useAtomString = $type->extendedAttributes->{AtomString};
-        if ($defaultValue eq "null") {
-            return $useAtomString ? "nullAtom()" : "String()";
-        } elsif ($defaultValue eq "\"\"") {
-            return $useAtomString ? "emptyAtom()" : "emptyString()";
-        } else {
-            return $useAtomString ? "AtomString(${defaultValue}, AtomString::ConstructFromLiteral)" : "${defaultValue}_s";
-        }
-    }
-
-    if ($codeGenerator->IsEnumType($type)) {
-        # FIXME: Would be nice to report an error if the value does not have quote marks around it.
-        # FIXME: Would be nice to report an error if the value is not one of the enumeration values.
-        if ($defaultValue eq "null") {
-            die if !$type->isNullable;
-            return "WTF::nullopt";
-        }
-        my $className = GetEnumerationClassName($type, $typeScope);
-        my $enumerationValueName = GetEnumerationValueName(substr($defaultValue, 1, -1));
-        return $className . "::" . $enumerationValueName;
-    }
     if ($defaultValue eq "null") {
         if ($type->isUnion) {
             return "WTF::nullopt" if $type->isNullable;
@@ -2242,7 +2220,10 @@
 
         return "jsNull()" if $type->name eq "any";
         return "nullptr" if $codeGenerator->IsWrapperType($type) || $codeGenerator->IsBufferSourceType($type);
-        return "String()" if $codeGenerator->IsStringType($type);
+        if ($codeGenerator->IsStringType($type)) {
+            my $useAtomString = $type->extendedAttributes->{AtomString};
+            return $useAtomString ? "nullAtom()" : "String()";
+        }
         return "WTF::nullopt";
     }
 
@@ -2254,6 +2235,32 @@
     return "jsUndefined()" if $defaultValue eq "undefined";
     return "PNaN" if $defaultValue eq "NaN";
 
+    if (substr($defaultValue, 0, 1) eq "\"") {
+        # Default value is a quoted string so the type should be a DOMString or an enumeration.
+        if ($type->isUnion) {
+            foreach my $memberType (GetFlattenedMemberTypes($type)) {
+                if ($codeGenerator->IsStringType($memberType) || $codeGenerator->IsEnumType($memberType)) {
+                    $type = $memberType;
+                    last;
+                }
+            }
+        }
+        if ($codeGenerator->IsStringType($type)) {
+            my $useAtomString = $type->extendedAttributes->{AtomString};
+            if ($defaultValue eq "\"\"") {
+                return $useAtomString ? "emptyAtom()" : "emptyString()";
+            } else {
+                return $useAtomString ? "AtomString(${defaultValue}, AtomString::ConstructFromLiteral)" : "${defaultValue}_s";
+            }
+        }
+
+        if ($codeGenerator->IsEnumType($type)) {
+            my $className = GetEnumerationClassName($type, $typeScope);
+            my $enumerationValueName = GetEnumerationValueName(substr($defaultValue, 1, -1));
+            return $className . "::" . $enumerationValueName;
+        }
+    }
+
     return $defaultValue;
 }
 

Modified: trunk/Source/WebCore/bindings/scripts/test/JS/JSTestCallTracer.cpp (264200 => 264201)


--- trunk/Source/WebCore/bindings/scripts/test/JS/JSTestCallTracer.cpp	2020-07-09 22:28:37 UTC (rev 264200)
+++ trunk/Source/WebCore/bindings/scripts/test/JS/JSTestCallTracer.cpp	2020-07-09 22:41:21 UTC (rev 264201)
@@ -512,7 +512,7 @@
     UNUSED_PARAM(throwScope);
     auto& impl = castedThis->wrapped();
     EnsureStillAliveScope argument0 = callFrame->argument(0);
-    auto variantDefaultArg = argument0.value().isUndefined() ? "" : convert<IDLUnion<IDLBoolean, IDLFloat, IDLDOMString>>(*lexicalGlobalObject, argument0.value());
+    auto variantDefaultArg = argument0.value().isUndefined() ? emptyString() : convert<IDLUnion<IDLBoolean, IDLFloat, IDLDOMString>>(*lexicalGlobalObject, argument0.value());
     RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
     if (UNLIKELY(impl.callTracingActive()))
         CallTracer::testCallTracerInterface(impl, "testOperationWithDefaultVariantArgument"_s, { variantDefaultArg });

Modified: trunk/Source/WebCore/bindings/scripts/test/JS/JSTestStandaloneDictionary.cpp (264200 => 264201)


--- trunk/Source/WebCore/bindings/scripts/test/JS/JSTestStandaloneDictionary.cpp	2020-07-09 22:28:37 UTC (rev 264200)
+++ trunk/Source/WebCore/bindings/scripts/test/JS/JSTestStandaloneDictionary.cpp	2020-07-09 22:41:21 UTC (rev 264201)
@@ -27,8 +27,10 @@
 #include "Document.h"
 #include "JSDOMConvertBoolean.h"
 #include "JSDOMConvertCallbacks.h"
+#include "JSDOMConvertNullable.h"
 #include "JSDOMConvertNumbers.h"
 #include "JSDOMConvertStrings.h"
+#include "JSDOMConvertUnion.h"
 #include "JSDOMGlobalObject.h"
 #include "JSVoidCallback.h"
 #include "Settings.h"
@@ -36,6 +38,7 @@
 #include <_javascript_Core/JSString.h>
 #include <_javascript_Core/ObjectConstructor.h>
 #include <wtf/NeverDestroyed.h>
+#include <wtf/Variant.h>
 
 
 namespace WebCore {
@@ -87,6 +90,18 @@
         result.enumMember = convert<IDLEnumeration<TestStandaloneDictionary::EnumInStandaloneDictionaryFile>>(lexicalGlobalObject, enumMemberValue);
         RETURN_IF_EXCEPTION(throwScope, { });
     }
+    JSValue nullableUnionWithNullDefaultValueValue;
+    if (isNullOrUndefined)
+        nullableUnionWithNullDefaultValueValue = jsUndefined();
+    else {
+        nullableUnionWithNullDefaultValueValue = object->get(&lexicalGlobalObject, Identifier::fromString(vm, "nullableUnionWithNullDefaultValue"));
+        RETURN_IF_EXCEPTION(throwScope, { });
+    }
+    if (!nullableUnionWithNullDefaultValueValue.isUndefined()) {
+        result.nullableUnionWithNullDefaultValue = convert<IDLNullable<IDLUnion<IDLDOMString, IDLBoolean>>>(lexicalGlobalObject, nullableUnionWithNullDefaultValueValue);
+        RETURN_IF_EXCEPTION(throwScope, { });
+    } else
+        result.nullableUnionWithNullDefaultValue = WTF::nullopt;
 #if ENABLE(Conditional13) || ENABLE(Conditional14)
     JSValue partialBooleanMemberValue;
     if (isNullOrUndefined)
@@ -205,6 +220,18 @@
         result.stringMember = convert<IDLDOMString>(lexicalGlobalObject, stringMemberValue);
         RETURN_IF_EXCEPTION(throwScope, { });
     }
+    JSValue unionMemberWithDefaultValueValue;
+    if (isNullOrUndefined)
+        unionMemberWithDefaultValueValue = jsUndefined();
+    else {
+        unionMemberWithDefaultValueValue = object->get(&lexicalGlobalObject, Identifier::fromString(vm, "unionMemberWithDefaultValue"));
+        RETURN_IF_EXCEPTION(throwScope, { });
+    }
+    if (!unionMemberWithDefaultValueValue.isUndefined()) {
+        result.unionMemberWithDefaultValue = convert<IDLUnion<IDLEnumeration<TestStandaloneDictionary::EnumInStandaloneDictionaryFile>, IDLDouble>>(lexicalGlobalObject, unionMemberWithDefaultValueValue);
+        RETURN_IF_EXCEPTION(throwScope, { });
+    } else
+        result.unionMemberWithDefaultValue = TestStandaloneDictionary::EnumInStandaloneDictionaryFile::EnumValue1;
     return result;
 }
 
@@ -226,6 +253,8 @@
         auto enumMemberValue = toJS<IDLEnumeration<TestStandaloneDictionary::EnumInStandaloneDictionaryFile>>(lexicalGlobalObject, IDLEnumeration<TestStandaloneDictionary::EnumInStandaloneDictionaryFile>::extractValueFromNullable(dictionary.enumMember));
         result->putDirect(vm, JSC::Identifier::fromString(vm, "enumMember"), enumMemberValue);
     }
+    auto nullableUnionWithNullDefaultValueValue = toJS<IDLNullable<IDLUnion<IDLDOMString, IDLBoolean>>>(lexicalGlobalObject, globalObject, dictionary.nullableUnionWithNullDefaultValue);
+    result->putDirect(vm, JSC::Identifier::fromString(vm, "nullableUnionWithNullDefaultValue"), nullableUnionWithNullDefaultValueValue);
 #if ENABLE(Conditional13) || ENABLE(Conditional14)
     if (!IDLBoolean::isNullValue(dictionary.partialBooleanMember)) {
         auto partialBooleanMemberValue = toJS<IDLBoolean>(IDLBoolean::extractValueFromNullable(dictionary.partialBooleanMember));
@@ -278,6 +307,8 @@
         auto stringMemberValue = toJS<IDLDOMString>(lexicalGlobalObject, IDLDOMString::extractValueFromNullable(dictionary.stringMember));
         result->putDirect(vm, JSC::Identifier::fromString(vm, "stringMember"), stringMemberValue);
     }
+    auto unionMemberWithDefaultValueValue = toJS<IDLUnion<IDLEnumeration<TestStandaloneDictionary::EnumInStandaloneDictionaryFile>, IDLDouble>>(lexicalGlobalObject, globalObject, dictionary.unionMemberWithDefaultValue);
+    result->putDirect(vm, JSC::Identifier::fromString(vm, "unionMemberWithDefaultValue"), unionMemberWithDefaultValueValue);
     return result;
 }
 

Modified: trunk/Source/WebCore/bindings/scripts/test/TestStandaloneDictionary.idl (264200 => 264201)


--- trunk/Source/WebCore/bindings/scripts/test/TestStandaloneDictionary.idl	2020-07-09 22:28:37 UTC (rev 264200)
+++ trunk/Source/WebCore/bindings/scripts/test/TestStandaloneDictionary.idl	2020-07-09 22:41:21 UTC (rev 264201)
@@ -38,4 +38,6 @@
     DOMString stringMember;
     TestEnumInStandaloneDictionaryFile enumMember;
     VoidCallback callbackMember;
+    (TestEnumInStandaloneDictionaryFile or double) unionMemberWithDefaultValue = "enumValue1";
+    (DOMString or boolean)? nullableUnionWithNullDefaultValue = null;
 };
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to