Bug 1855074 - Avoid some string copies in WebIDL/XPCOM->JS string conversion. r=edgar

We can convert strings to jsval without mutating the string nowadays.
Make that efficient.

Differential Revision: https://phabricator.services.mozilla.com/D189171
This commit is contained in:
Emilio Cobos Álvarez 2023-10-05 17:16:28 +00:00
Родитель 3f9391b5f2
Коммит 3f833f02dd
3 изменённых файлов: 23 добавлений и 25 удалений

Просмотреть файл

@ -21360,15 +21360,7 @@ class CallbackMember(CGNativeMember):
if arg.canHaveMissingValue():
argval += ".Value()"
if arg.type.isDOMString():
# XPConnect string-to-JS conversion wants to mutate the string. So
# let's give it a string it can mutate
# XXXbz if we try to do a sequence of strings, this will kinda fail.
result = "mutableStr"
prepend = "nsString mutableStr(%s);\n" % argval
else:
result = argval
prepend = ""
prepend = ""
wrapScope = self.wrapScope
if arg.type.isUnion() and wrapScope is None:
@ -21381,7 +21373,7 @@ class CallbackMember(CGNativeMember):
arg.type,
self.descriptorProvider,
{
"result": result,
"result": argval,
"successCode": "continue;\n" if arg.variadic else "break;\n",
"jsvalRef": "argv[%s]" % jsvalIndex,
"jsvalHandle": "argv[%s]" % jsvalIndex,

Просмотреть файл

@ -112,4 +112,18 @@ bool NonVoidStringToJsval(JSContext* cx, nsAString& str,
return true;
}
bool NonVoidStringToJsval(JSContext* cx, const nsAString& str,
MutableHandleValue rval) {
nsStringBuffer* sharedBuffer;
if (!XPCStringConvert::ReadableToJSVal(cx, str, &sharedBuffer, rval)) {
return false;
}
if (sharedBuffer) {
// The string was shared but ReadableToJSVal didn't addref it.
sharedBuffer->AddRef();
}
return true;
}
} // namespace xpc

Просмотреть файл

@ -339,6 +339,8 @@ bool Base64Decode(JSContext* cx, JS::Handle<JS::Value> val,
*/
bool NonVoidStringToJsval(JSContext* cx, nsAString& str,
JS::MutableHandle<JS::Value> rval);
bool NonVoidStringToJsval(JSContext* cx, const nsAString& str,
JS::MutableHandle<JS::Value> rval);
inline bool StringToJsval(JSContext* cx, nsAString& str,
JS::MutableHandle<JS::Value> rval) {
// From the T_ASTRING case in XPCConvert::NativeData2JS.
@ -349,24 +351,14 @@ inline bool StringToJsval(JSContext* cx, nsAString& str,
return NonVoidStringToJsval(cx, str, rval);
}
inline bool NonVoidStringToJsval(JSContext* cx, const nsAString& str,
JS::MutableHandle<JS::Value> rval) {
nsString mutableCopy;
if (!mutableCopy.Assign(str, mozilla::fallible)) {
JS_ReportOutOfMemory(cx);
return false;
}
return NonVoidStringToJsval(cx, mutableCopy, rval);
}
inline bool StringToJsval(JSContext* cx, const nsAString& str,
JS::MutableHandle<JS::Value> rval) {
nsString mutableCopy;
if (!mutableCopy.Assign(str, mozilla::fallible)) {
JS_ReportOutOfMemory(cx);
return false;
// From the T_ASTRING case in XPCConvert::NativeData2JS.
if (str.IsVoid()) {
rval.setNull();
return true;
}
return StringToJsval(cx, mutableCopy, rval);
return NonVoidStringToJsval(cx, str, rval);
}
/**