Changeset 264201 in webkit


Ignore:
Timestamp:
Jul 9, 2020, 3:41:21 PM (5 years ago)
Author:
Chris Dumez
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:

"""
(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:
Location:
trunk/Source/WebCore
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r264199 r264201  
     12020-07-09  Chris Dumez  <cdumez@apple.com>
     2
     3        [Bindings] Add default value support for union types in dictionary members
     4        https://bugs.webkit.org/show_bug.cgi?id=214160
     5
     6        Reviewed by Sam Weinig.
     7
     8        Add default value support for union types in dictionary members. This is needed for the WebAudio specification:
     9        - https://www.w3.org/TR/webaudio/#AudioContextOptions
     10        """
     11        (AudioContextLatencyCategory or double) latencyHint = "interactive";
     12        """
     13
     14        * bindings/scripts/CodeGeneratorJS.pm:
     15        (GenerateDefaultValue):
     16        * bindings/scripts/test/JS/JSTestObj.cpp:
     17        (WebCore::jsTestObjPrototypeFunctionOverloadWithOptionalUnion1Body):
     18        * bindings/scripts/test/JS/JSTestStandaloneDictionary.cpp:
     19        (WebCore::convertDictionary<DictionaryImplName>):
     20        (WebCore::convertDictionaryToJS):
     21        * bindings/scripts/test/TestStandaloneDictionary.idl:
     22
    1232020-07-09  Per Arne Vollan  <pvollan@apple.com>
    224
  • trunk/Source/WebCore/bindings/scripts/CodeGeneratorJS.pm

    r263638 r264201  
    22112211    my ($typeScope, $context, $type, $defaultValue) = @_;
    22122212
    2213     if ($codeGenerator->IsStringType($type)) {
    2214         my $useAtomString = $type->extendedAttributes->{AtomString};
    2215         if ($defaultValue eq "null") {
    2216             return $useAtomString ? "nullAtom()" : "String()";
    2217         } elsif ($defaultValue eq "\"\"") {
    2218             return $useAtomString ? "emptyAtom()" : "emptyString()";
    2219         } else {
    2220             return $useAtomString ? "AtomString(${defaultValue}, AtomString::ConstructFromLiteral)" : "${defaultValue}_s";
    2221         }
    2222     }
    2223 
    2224     if ($codeGenerator->IsEnumType($type)) {
    2225         # FIXME: Would be nice to report an error if the value does not have quote marks around it.
    2226         # FIXME: Would be nice to report an error if the value is not one of the enumeration values.
    2227         if ($defaultValue eq "null") {
    2228             die if !$type->isNullable;
    2229             return "WTF::nullopt";
    2230         }
    2231         my $className = GetEnumerationClassName($type, $typeScope);
    2232         my $enumerationValueName = GetEnumerationValueName(substr($defaultValue, 1, -1));
    2233         return $className . "::" . $enumerationValueName;
    2234     }
    22352213    if ($defaultValue eq "null") {
    22362214        if ($type->isUnion) {
     
    22432221        return "jsNull()" if $type->name eq "any";
    22442222        return "nullptr" if $codeGenerator->IsWrapperType($type) || $codeGenerator->IsBufferSourceType($type);
    2245         return "String()" if $codeGenerator->IsStringType($type);
     2223        if ($codeGenerator->IsStringType($type)) {
     2224            my $useAtomString = $type->extendedAttributes->{AtomString};
     2225            return $useAtomString ? "nullAtom()" : "String()";
     2226        }
    22462227        return "WTF::nullopt";
    22472228    }
     
    22542235    return "jsUndefined()" if $defaultValue eq "undefined";
    22552236    return "PNaN" if $defaultValue eq "NaN";
     2237
     2238    if (substr($defaultValue, 0, 1) eq "\"") {
     2239        # Default value is a quoted string so the type should be a DOMString or an enumeration.
     2240        if ($type->isUnion) {
     2241            foreach my $memberType (GetFlattenedMemberTypes($type)) {
     2242                if ($codeGenerator->IsStringType($memberType) || $codeGenerator->IsEnumType($memberType)) {
     2243                    $type = $memberType;
     2244                    last;
     2245                }
     2246            }
     2247        }
     2248        if ($codeGenerator->IsStringType($type)) {
     2249            my $useAtomString = $type->extendedAttributes->{AtomString};
     2250            if ($defaultValue eq "\"\"") {
     2251                return $useAtomString ? "emptyAtom()" : "emptyString()";
     2252            } else {
     2253                return $useAtomString ? "AtomString(${defaultValue}, AtomString::ConstructFromLiteral)" : "${defaultValue}_s";
     2254            }
     2255        }
     2256
     2257        if ($codeGenerator->IsEnumType($type)) {
     2258            my $className = GetEnumerationClassName($type, $typeScope);
     2259            my $enumerationValueName = GetEnumerationValueName(substr($defaultValue, 1, -1));
     2260            return $className . "::" . $enumerationValueName;
     2261        }
     2262    }
    22562263
    22572264    return $defaultValue;
  • trunk/Source/WebCore/bindings/scripts/test/JS/JSTestCallTracer.cpp

    r260992 r264201  
    513513    auto& impl = castedThis->wrapped();
    514514    EnsureStillAliveScope argument0 = callFrame->argument(0);
    515     auto variantDefaultArg = argument0.value().isUndefined() ? "" : convert<IDLUnion<IDLBoolean, IDLFloat, IDLDOMString>>(*lexicalGlobalObject, argument0.value());
     515    auto variantDefaultArg = argument0.value().isUndefined() ? emptyString() : convert<IDLUnion<IDLBoolean, IDLFloat, IDLDOMString>>(*lexicalGlobalObject, argument0.value());
    516516    RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
    517517    if (UNLIKELY(impl.callTracingActive()))
  • trunk/Source/WebCore/bindings/scripts/test/JS/JSTestStandaloneDictionary.cpp

    r251425 r264201  
    2828#include "JSDOMConvertBoolean.h"
    2929#include "JSDOMConvertCallbacks.h"
     30#include "JSDOMConvertNullable.h"
    3031#include "JSDOMConvertNumbers.h"
    3132#include "JSDOMConvertStrings.h"
     33#include "JSDOMConvertUnion.h"
    3234#include "JSDOMGlobalObject.h"
    3335#include "JSVoidCallback.h"
     
    3739#include <JavaScriptCore/ObjectConstructor.h>
    3840#include <wtf/NeverDestroyed.h>
     41#include <wtf/Variant.h>
    3942
    4043
     
    8891        RETURN_IF_EXCEPTION(throwScope, { });
    8992    }
     93    JSValue nullableUnionWithNullDefaultValueValue;
     94    if (isNullOrUndefined)
     95        nullableUnionWithNullDefaultValueValue = jsUndefined();
     96    else {
     97        nullableUnionWithNullDefaultValueValue = object->get(&lexicalGlobalObject, Identifier::fromString(vm, "nullableUnionWithNullDefaultValue"));
     98        RETURN_IF_EXCEPTION(throwScope, { });
     99    }
     100    if (!nullableUnionWithNullDefaultValueValue.isUndefined()) {
     101        result.nullableUnionWithNullDefaultValue = convert<IDLNullable<IDLUnion<IDLDOMString, IDLBoolean>>>(lexicalGlobalObject, nullableUnionWithNullDefaultValueValue);
     102        RETURN_IF_EXCEPTION(throwScope, { });
     103    } else
     104        result.nullableUnionWithNullDefaultValue = WTF::nullopt;
    90105#if ENABLE(Conditional13) || ENABLE(Conditional14)
    91106    JSValue partialBooleanMemberValue;
     
    206221        RETURN_IF_EXCEPTION(throwScope, { });
    207222    }
     223    JSValue unionMemberWithDefaultValueValue;
     224    if (isNullOrUndefined)
     225        unionMemberWithDefaultValueValue = jsUndefined();
     226    else {
     227        unionMemberWithDefaultValueValue = object->get(&lexicalGlobalObject, Identifier::fromString(vm, "unionMemberWithDefaultValue"));
     228        RETURN_IF_EXCEPTION(throwScope, { });
     229    }
     230    if (!unionMemberWithDefaultValueValue.isUndefined()) {
     231        result.unionMemberWithDefaultValue = convert<IDLUnion<IDLEnumeration<TestStandaloneDictionary::EnumInStandaloneDictionaryFile>, IDLDouble>>(lexicalGlobalObject, unionMemberWithDefaultValueValue);
     232        RETURN_IF_EXCEPTION(throwScope, { });
     233    } else
     234        result.unionMemberWithDefaultValue = TestStandaloneDictionary::EnumInStandaloneDictionaryFile::EnumValue1;
    208235    return result;
    209236}
     
    227254        result->putDirect(vm, JSC::Identifier::fromString(vm, "enumMember"), enumMemberValue);
    228255    }
     256    auto nullableUnionWithNullDefaultValueValue = toJS<IDLNullable<IDLUnion<IDLDOMString, IDLBoolean>>>(lexicalGlobalObject, globalObject, dictionary.nullableUnionWithNullDefaultValue);
     257    result->putDirect(vm, JSC::Identifier::fromString(vm, "nullableUnionWithNullDefaultValue"), nullableUnionWithNullDefaultValueValue);
    229258#if ENABLE(Conditional13) || ENABLE(Conditional14)
    230259    if (!IDLBoolean::isNullValue(dictionary.partialBooleanMember)) {
     
    279308        result->putDirect(vm, JSC::Identifier::fromString(vm, "stringMember"), stringMemberValue);
    280309    }
     310    auto unionMemberWithDefaultValueValue = toJS<IDLUnion<IDLEnumeration<TestStandaloneDictionary::EnumInStandaloneDictionaryFile>, IDLDouble>>(lexicalGlobalObject, globalObject, dictionary.unionMemberWithDefaultValue);
     311    result->putDirect(vm, JSC::Identifier::fromString(vm, "unionMemberWithDefaultValue"), unionMemberWithDefaultValueValue);
    281312    return result;
    282313}
  • trunk/Source/WebCore/bindings/scripts/test/TestStandaloneDictionary.idl

    r248275 r264201  
    3939    TestEnumInStandaloneDictionaryFile enumMember;
    4040    VoidCallback callbackMember;
     41    (TestEnumInStandaloneDictionaryFile or double) unionMemberWithDefaultValue = "enumValue1";
     42    (DOMString or boolean)? nullableUnionWithNullDefaultValue = null;
    4143};
Note: See TracChangeset for help on using the changeset viewer.