From 063d0caa78b53d6df27318b40d23e6e650af26a5 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Wed, 15 Feb 2017 00:01:39 -0500 Subject: [PATCH] Bug 1330699 part 12. Actually change the key type of a record, and its corresponding conversion behavior, depending on what the IDL says. r=qdot --- dom/bindings/Codegen.py | 48 ++++++++++++++++++++++++++++------- dom/bindings/Record.h | 2 +- dom/fetch/InternalHeaders.cpp | 2 +- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/dom/bindings/Codegen.py b/dom/bindings/Codegen.py index 1ee2aa4fc45e..625828a53c03 100644 --- a/dom/bindings/Codegen.py +++ b/dom/bindings/Codegen.py @@ -4473,11 +4473,15 @@ def handleDefaultStringValue(defaultValue, method): } -def recordKeyDeclType(recordType): +def recordKeyType(recordType): assert recordType.keyType.isString() if recordType.keyType.isByteString(): - return CGGeneric("nsCString") - return CGGeneric("nsString") + return "nsCString" + return "nsString" + + +def recordKeyDeclType(recordType): + return CGGeneric(recordKeyType(recordType)) # If this function is modified, modify CGNativeMember.getArg and @@ -4885,6 +4889,15 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None, "passedToJSImpl": "${passedToJSImpl}" }) + keyType = recordKeyType(recordType) + if recordType.keyType.isDOMString(): + keyConversionFunction = "ConvertJSValueToString" + elif recordType.keyType.isUSVString(): + keyConversionFunction = "ConvertJSValueToUSVString" + else: + assert recordType.keyType.isByteString() + keyConversionFunction = "ConvertJSValueToByteString" + templateBody = fill( """ auto& recordEntries = ${recordRef}.Entries(); @@ -4904,6 +4917,7 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None, JS::Rooted propNameValue(cx); JS::Rooted temp(cx); JS::Rooted curId(cx); + JS::Rooted idVal(cx); for (size_t i = 0; i < ids.length(); ++i) { curId = ids[i]; @@ -4920,12 +4934,11 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None, continue; } - binding_detail::FakeString propName; - bool isSymbol; - if (!ConvertIdToString(cx, curId, propName, isSymbol)) { + idVal = js::IdToValue(curId); + ${keyType} propName; + if (!${keyConversionFunction}(cx, idVal, propName)) { $*{exceptionCode} } - MOZ_ASSERT(!isSymbol, "We said, no symbols!"); if (!JS_GetPropertyById(cx, recordObj, curId, &temp)) { $*{exceptionCode} @@ -4941,6 +4954,8 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None, """, exceptionCode=exceptionCode, recordRef=recordRef, + keyType=keyType, + keyConversionFunction=keyConversionFunction, typeName=typeName, valueType=valueInfo.declType.define(), valueConversion=valueConversion) @@ -6558,6 +6573,18 @@ def getWrapTemplateForType(type, descriptorProvider, result, successCode, 'typedArraysAreStructs': typedArraysAreStructs }) recordWrapLevel -= 1 + if type.keyType.isByteString(): + # There is no length-taking JS_DefineProperty. So to keep + # things sane with embedded nulls, we want to byte-inflate + # to an nsAString. The only byte-inflation function we + # have around is AppendASCIItoUTF16, which luckily doesn't + # assert anything about the input being ASCII. + expandedKeyDecl = "NS_ConvertASCIItoUTF16 expandedKey(entry.mKey);\n" + keyName = "expandedKey" + else: + expandedKeyDecl = "" + keyName = "entry.mKey" + code = fill( """ @@ -6575,9 +6602,10 @@ def getWrapTemplateForType(type, descriptorProvider, result, successCode, do { $*{innerTemplate} } while (0); + $*{expandedKeyDecl} if (!JS_DefineUCProperty(cx, returnObj, - entry.mKey.BeginReading(), - entry.mKey.Length(), tmp, + ${keyName}.BeginReading(), + ${keyName}.Length(), tmp, JSPROP_ENUMERATE)) { $*{exceptionCode} } @@ -6589,6 +6617,8 @@ def getWrapTemplateForType(type, descriptorProvider, result, successCode, exceptionCode=exceptionCode, valueName=valueName, innerTemplate=innerTemplate, + expandedKeyDecl=expandedKeyDecl, + keyName=keyName, set=setObject("*returnObj")) return (code, False) diff --git a/dom/bindings/Record.h b/dom/bindings/Record.h index 2fe18be2b7b0..b7f7b01b0273 100644 --- a/dom/bindings/Record.h +++ b/dom/bindings/Record.h @@ -47,7 +47,7 @@ template class Record { public: - typedef typename binding_detail::RecordEntry EntryType; + typedef typename binding_detail::RecordEntry EntryType; typedef Record SelfType; Record() diff --git a/dom/fetch/InternalHeaders.cpp b/dom/fetch/InternalHeaders.cpp index 536308beba6c..bd643d13f79d 100644 --- a/dom/fetch/InternalHeaders.cpp +++ b/dom/fetch/InternalHeaders.cpp @@ -309,7 +309,7 @@ void InternalHeaders::Fill(const Record& aInit, ErrorResult& aRv) { for (auto& entry : aInit.Entries()) { - Append(NS_ConvertUTF16toUTF8(entry.mKey), entry.mValue, aRv); + Append(entry.mKey, entry.mValue, aRv); if (aRv.Failed()) { return; }