Bug 1803495 - Use AutoStableStringChars overload for SourceText::init. r=arai

Remove duplicate code and transfer string buffer ownership in more cases.

Depends on D163552

Differential Revision: https://phabricator.services.mozilla.com/D163553
This commit is contained in:
Ted Campbell 2022-12-07 21:52:37 +00:00
parent 9c73cd619c
commit e727a5089f
8 changed files with 36 additions and 79 deletions

View file

@ -330,12 +330,7 @@ static bool EvalKernel(JSContext* cx, HandleValue v, EvalType evalType,
}
SourceText<char16_t> srcBuf;
const char16_t* chars = linearChars.twoByteRange().begin().get();
SourceOwnership ownership = linearChars.maybeGiveOwnershipToCaller()
? SourceOwnership::TakeOwnership
: SourceOwnership::Borrowed;
if (!srcBuf.init(cx, chars, linearStr->length(), ownership)) {
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) {
return false;
}

View file

@ -222,12 +222,7 @@ static bool PerformShadowRealmEval(JSContext* cx, Handle<JSString*> sourceText,
return false;
}
SourceText<char16_t> srcBuf;
const char16_t* chars = linearChars.twoByteRange().begin().get();
SourceOwnership ownership = linearChars.maybeGiveOwnershipToCaller()
? SourceOwnership::TakeOwnership
: SourceOwnership::Borrowed;
if (!srcBuf.init(cx, chars, linearChars.length(), ownership)) {
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) {
return false;
}

View file

@ -6131,15 +6131,6 @@ static bool EvalReturningScope(JSContext* cx, unsigned argc, Value* vp) {
}
}
AutoStableStringChars strChars(cx);
if (!strChars.initTwoByte(cx, str)) {
return false;
}
mozilla::Range<const char16_t> chars = strChars.twoByteRange();
size_t srclen = chars.length();
const char16_t* src = chars.begin().get();
JS::AutoFilename filename;
unsigned lineno;
@ -6150,8 +6141,12 @@ static bool EvalReturningScope(JSContext* cx, unsigned argc, Value* vp) {
options.setNoScriptRval(true);
options.setNonSyntacticScope(true);
AutoStableStringChars linearChars(cx);
if (!linearChars.initTwoByte(cx, str)) {
return false;
}
JS::SourceText<char16_t> srcBuf;
if (!srcBuf.init(cx, src, srclen, SourceOwnership::Borrowed)) {
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) {
return false;
}
@ -6362,8 +6357,7 @@ static bool CompileToStencil(JSContext* cx, uint32_t argc, Value* vp) {
return false;
}
JS::SourceText<char16_t> srcBuf;
if (!srcBuf.init(cx, linearChars.twoByteChars(), src->length(),
JS::SourceOwnership::Borrowed)) {
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) {
return false;
}
@ -6508,8 +6502,7 @@ static bool CompileToStencilXDR(JSContext* cx, uint32_t argc, Value* vp) {
return false;
}
JS::SourceText<char16_t> srcBuf;
if (!srcBuf.init(cx, linearChars.twoByteChars(), src->length(),
JS::SourceOwnership::Borrowed)) {
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) {
return false;
}
@ -7694,20 +7687,13 @@ JSScript* js::TestingFunctionArgumentToScript(
JSContext* cx, HandleValue v, JSFunction** funp /* = nullptr */) {
if (v.isString()) {
// To convert a string to a script, compile it. Parse it as an ES6 Program.
Rooted<JSLinearString*> linearStr(
cx, JS::StringToLinearString(cx, v.toString()));
if (!linearStr) {
return nullptr;
}
size_t len = JS::GetLinearStringLength(linearStr);
Rooted<JSString*> str(cx, v.toString());
AutoStableStringChars linearChars(cx);
if (!linearChars.initTwoByte(cx, linearStr)) {
if (!linearChars.initTwoByte(cx, str)) {
return nullptr;
}
const char16_t* chars = linearChars.twoByteRange().begin().get();
SourceText<char16_t> source;
if (!source.init(cx, chars, len, SourceOwnership::Borrowed)) {
if (!source.initMaybeBorrowed(cx, linearChars)) {
return nullptr;
}

View file

@ -1287,14 +1287,12 @@ bool DebuggerObject::CallData::createSource() {
compileOptions.setIntroductionType("inlineScript");
}
Vector<char16_t> textChars(cx);
if (!CopyStringToVector(cx, text, textChars)) {
AutoStableStringChars linearChars(cx);
if (!linearChars.initTwoByte(cx, text)) {
return false;
}
JS::SourceText<char16_t> srcBuf;
if (!srcBuf.init(cx, textChars.begin(), text->length(),
JS::SourceOwnership::Borrowed)) {
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) {
return false;
}

View file

@ -469,15 +469,13 @@ JSObject* ModuleLoader::loadAndParse(JSContext* cx, HandleString pathArg) {
return nullptr;
}
JS::AutoStableStringChars stableChars(cx);
if (!stableChars.initTwoByte(cx, source)) {
JS::AutoStableStringChars linearChars(cx);
if (!linearChars.initTwoByte(cx, source)) {
return nullptr;
}
const char16_t* chars = stableChars.twoByteRange().begin().get();
JS::SourceText<char16_t> srcBuf;
if (!srcBuf.init(cx, chars, source->length(),
JS::SourceOwnership::Borrowed)) {
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) {
return nullptr;
}

View file

@ -2409,15 +2409,13 @@ static bool Evaluate(JSContext* cx, unsigned argc, Value* vp) {
return false;
}
} else {
AutoStableStringChars codeChars(cx);
if (!codeChars.initTwoByte(cx, code)) {
AutoStableStringChars linearChars(cx);
if (!linearChars.initTwoByte(cx, code)) {
return false;
}
mozilla::Range<const char16_t> chars = codeChars.twoByteRange();
JS::SourceText<char16_t> srcBuf;
if (!srcBuf.init(cx, chars.begin().get(), chars.length(),
JS::SourceOwnership::Borrowed)) {
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) {
return false;
}
@ -2629,14 +2627,13 @@ static bool Run(JSContext* cx, unsigned argc, Value* vp) {
return false;
}
AutoStableStringChars chars(cx);
if (!chars.initTwoByte(cx, str)) {
AutoStableStringChars linearChars(cx);
if (!linearChars.initTwoByte(cx, str)) {
return false;
}
JS::SourceText<char16_t> srcBuf;
if (!srcBuf.init(cx, chars.twoByteRange().begin().get(), str->length(),
JS::SourceOwnership::Borrowed)) {
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) {
return false;
}
@ -4892,15 +4889,13 @@ static bool ParseModule(JSContext* cx, unsigned argc, Value* vp) {
}
options.setModule();
AutoStableStringChars stableChars(cx);
if (!stableChars.initTwoByte(cx, scriptContents)) {
AutoStableStringChars linearChars(cx);
if (!linearChars.initTwoByte(cx, scriptContents)) {
return false;
}
const char16_t* chars = stableChars.twoByteRange().begin().get();
JS::SourceText<char16_t> srcBuf;
if (!srcBuf.init(cx, chars, scriptContents->length(),
JS::SourceOwnership::Borrowed)) {
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) {
return false;
}
@ -8195,13 +8190,12 @@ static bool EntryPoints(JSContext* cx, unsigned argc, Value* vp) {
return false;
}
AutoStableStringChars stableChars(cx);
if (!stableChars.initTwoByte(cx, codeString)) {
AutoStableStringChars linearChars(cx);
if (!linearChars.initTwoByte(cx, codeString)) {
return false;
}
JS::SourceText<char16_t> srcBuf;
if (!srcBuf.init(cx, stableChars.twoByteRange().begin().get(),
codeString->length(), JS::SourceOwnership::Borrowed)) {
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) {
return false;
}

View file

@ -1675,17 +1675,13 @@ static bool CreateDynamicFunction(JSContext* cx, const CallArgs& args,
}
// Steps 7.a-b, 8.a-b, 9.a-b, 16-28.
AutoStableStringChars stableChars(cx);
if (!stableChars.initTwoByte(cx, functionText)) {
AutoStableStringChars linearChars(cx);
if (!linearChars.initTwoByte(cx, functionText)) {
return false;
}
mozilla::Range<const char16_t> chars = stableChars.twoByteRange();
SourceOwnership ownership = stableChars.maybeGiveOwnershipToCaller()
? SourceOwnership::TakeOwnership
: SourceOwnership::Borrowed;
SourceText<char16_t> srcBuf;
if (!srcBuf.init(cx, chars.begin().get(), chars.length(), ownership)) {
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) {
return false;
}

View file

@ -7009,18 +7009,13 @@ static bool HandleInstantiationFailure(JSContext* cx, CallArgs args,
options.setForceStrictMode();
}
AutoStableStringChars stableChars(cx);
if (!stableChars.initTwoByte(cx, src)) {
AutoStableStringChars linearChars(cx);
if (!linearChars.initTwoByte(cx, src)) {
return false;
}
SourceText<char16_t> srcBuf;
const char16_t* chars = stableChars.twoByteRange().begin().get();
SourceOwnership ownership = stableChars.maybeGiveOwnershipToCaller()
? SourceOwnership::TakeOwnership
: SourceOwnership::Borrowed;
if (!srcBuf.init(cx, chars, end - begin, ownership)) {
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) {
return false;
}