Bug 1475228 - Make synchronous compile APIs take SourceBufferHolders exclusively r=jandem r=fitzgen

This commit is contained in:
Jon Coppeard 2018-07-17 14:30:22 +01:00
Родитель a6587cb874
Коммит ec2af16383
15 изменённых файлов: 78 добавлений и 137 удалений

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

@ -94,11 +94,12 @@ nsJSUtils::CompileFunction(AutoJSAPI& jsapi,
// Compile.
JS::Rooted<JSFunction*> fun(cx);
JS::SourceBufferHolder source(PromiseFlatString(aBody).get(), aBody.Length(),
JS::SourceBufferHolder::NoOwnership);
if (!JS::CompileFunction(cx, aScopeChain, aOptions,
PromiseFlatCString(aName).get(),
aArgCount, aArgArray,
PromiseFlatString(aBody).get(),
aBody.Length(), &fun))
source, &fun))
{
return NS_ERROR_FAILURE;
}

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

@ -4865,8 +4865,9 @@ WorkerPrivate::RunExpiredTimeouts(JSContext* aCx)
JS::Rooted<JS::Value> unused(aes.cx());
if (!JS::Evaluate(aes.cx(), options, script.BeginReading(),
script.Length(), &unused) &&
JS::SourceBufferHolder srcBuf(script.BeginReading(), script.Length(),
JS::SourceBufferHolder::NoOwnership);
if (!JS::Evaluate(aes.cx(), options, srcBuf, &unused) &&
!JS_IsExceptionPending(aCx)) {
retval = false;
break;

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

@ -487,8 +487,9 @@ XPCShellEnvironment::EvaluateString(const nsString& aString,
JS::CompileOptions options(cx);
options.setFileAndLine("typein", 0);
JS::Rooted<JSScript*> script(cx);
if (!JS_CompileUCScript(cx, aString.get(), aString.Length(), options,
&script))
JS::SourceBufferHolder srcBuf(aString.get(), aString.Length(),
JS::SourceBufferHolder::NoOwnership);
if (!JS_CompileUCScript(cx, srcBuf, options, &script))
{
return false;
}

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

@ -234,7 +234,12 @@ impl Runtime {
let _ac = AutoCompartment::with_obj(self.cx(), glob.get());
let options = CompileOptionsWrapper::new(self.cx(), filename_cstr.as_ptr(), line_num);
if !JS::Evaluate2(self.cx(), options.ptr, ptr as *const u16, len as _, rval) {
let mut srcBuf = JS::SourceBufferHolder {
data_: ptr,
length_: len as _,
ownsChars_: false
};
if !JS::Evaluate(self.cx(), options.ptr, &mut srcBuf, rval) {
debug!("...err!");
panic::maybe_resume_unwind();
Err(())

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

@ -5175,7 +5175,8 @@ js::TestingFunctionArgumentToScript(JSContext* cx,
RootedScript script(cx);
CompileOptions options(cx);
if (!JS::Compile(cx, options, chars, len, &script))
SourceBufferHolder source(chars, len, SourceBufferHolder::NoOwnership);
if (!JS::Compile(cx, options, source, &script))
return nullptr;
return script;
}

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

@ -84,8 +84,11 @@ testCompile(bool nonSyntactic)
CHECK(CompileForNonSyntacticScope(cx, options, src, length, &script));
CHECK_EQUAL(script->hasNonSyntacticScope(), true);
CHECK(CompileForNonSyntacticScope(cx, options, src_16, length, &script));
CHECK_EQUAL(script->hasNonSyntacticScope(), true);
{
JS::SourceBufferHolder srcBuf(src_16, length, JS::SourceBufferHolder::NoOwnership);
CHECK(CompileForNonSyntacticScope(cx, options, srcBuf, &script));
CHECK_EQUAL(script->hasNonSyntacticScope(), true);
}
CHECK(Compile(cx, options, buf, &script));
@ -94,8 +97,11 @@ testCompile(bool nonSyntactic)
CHECK(Compile(cx, options, src, length, &script));
CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic);
CHECK(Compile(cx, options, src_16, length, &script));
CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic);
{
JS::SourceBufferHolder srcBuf(src_16, length, JS::SourceBufferHolder::NoOwnership);
CHECK(Compile(cx, options, srcBuf, &script));
CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic);
}
options.forceAsync = true;

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

@ -40,7 +40,8 @@ eval(const char16_t* chars, size_t len, JS::MutableHandleValue rval)
JSAutoRealm ar(cx, global);
JS::CompileOptions options(cx);
return JS::Evaluate(cx, options, chars, len, rval);
JS::SourceBufferHolder srcBuf(chars, len, JS::SourceBufferHolder::NoOwnership);
return JS::Evaluate(cx, options, srcBuf, rval);
}
template<size_t N>

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

@ -17,8 +17,9 @@ BEGIN_TEST(testJSEvaluateScript)
JS::CompileOptions opts(cx);
JS::AutoObjectVector scopeChain(cx);
CHECK(scopeChain.append(obj));
JS::SourceBufferHolder srcBuf(src, ArrayLength(src) - 1, JS::SourceBufferHolder::NoOwnership);
CHECK(JS::Evaluate(cx, scopeChain, opts.setFileAndLine(__FILE__, __LINE__),
src, ArrayLength(src) - 1, &retval));
srcBuf, &retval));
bool hasProp = true;
CHECK(JS_AlreadyHasOwnProperty(cx, obj, "x", &hasProp));

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

@ -54,7 +54,8 @@ eval(const char* asciiChars, bool mutedErrors, JS::MutableHandleValue rval)
options.setMutedErrors(mutedErrors)
.setFileAndLine("", 0);
return JS::Evaluate(cx, options, chars.get(), len, rval);
JS::SourceBufferHolder srcBuf(chars.get(), len, JS::SourceBufferHolder::NoOwnership);
return JS::Evaluate(cx, options, srcBuf, rval);
}
bool

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

@ -72,7 +72,8 @@ BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript)
JS::CompileOptions options(cx);
options.setFileAndLine(__FILE__, __LINE__);
JS::RootedScript script(cx);
CHECK(JS_CompileUCScript(cx, uc_code, code_size, options, &script));
JS::SourceBufferHolder srcBuf(uc_code, code_size, JS::SourceBufferHolder::NoOwnership);
CHECK(JS_CompileUCScript(cx, srcBuf, options, &script));
return tryScript(script);
}
END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript)
@ -82,7 +83,8 @@ BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript_empty)
JS::CompileOptions options(cx);
options.setFileAndLine(__FILE__, __LINE__);
JS::RootedScript script(cx);
CHECK(JS_CompileUCScript(cx, uc_code, 0, options, &script));
JS::SourceBufferHolder srcBuf(uc_code, 0, JS::SourceBufferHolder::NoOwnership);
CHECK(JS_CompileUCScript(cx, srcBuf, options, &script));
return tryScript(script);
}
END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript_empty)
@ -92,7 +94,8 @@ BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScriptForPrincipal
JS::CompileOptions options(cx);
options.setFileAndLine(__FILE__, __LINE__);
JS::RootedScript script(cx);
CHECK(JS_CompileUCScript(cx, uc_code, code_size, options, &script));
JS::SourceBufferHolder srcBuf(uc_code, code_size, JS::SourceBufferHolder::NoOwnership);
CHECK(JS_CompileUCScript(cx, srcBuf, options, &script));
return tryScript(script);
}
END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScriptForPrincipals)

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

@ -4007,27 +4007,20 @@ Compile(JSContext* cx, const ReadOnlyCompileOptions& options,
return !!script;
}
static bool
Compile(JSContext* cx, const ReadOnlyCompileOptions& options,
const char16_t* chars, size_t length, MutableHandleScript script)
{
SourceBufferHolder srcBuf(chars, length, SourceBufferHolder::NoOwnership);
return ::Compile(cx, options, srcBuf, script);
}
static bool
Compile(JSContext* cx, const ReadOnlyCompileOptions& options,
const char* bytes, size_t length, MutableHandleScript script)
{
UniqueTwoByteChars chars;
char16_t* chars;
if (options.utf8)
chars.reset(UTF8CharsToNewTwoByteCharsZ(cx, UTF8Chars(bytes, length), &length).get());
chars = UTF8CharsToNewTwoByteCharsZ(cx, UTF8Chars(bytes, length), &length).get();
else
chars.reset(InflateString(cx, bytes, length));
chars = InflateString(cx, bytes, length);
if (!chars)
return false;
return ::Compile(cx, options, chars.get(), length, script);
SourceBufferHolder source(chars, length, SourceBufferHolder::GiveOwnership);
return ::Compile(cx, options, source, script);
}
static bool
@ -4067,13 +4060,6 @@ JS::Compile(JSContext* cx, const ReadOnlyCompileOptions& options,
return ::Compile(cx, options, bytes, length, script);
}
bool
JS::Compile(JSContext* cx, const ReadOnlyCompileOptions& options,
const char16_t* chars, size_t length, JS::MutableHandleScript script)
{
return ::Compile(cx, options, chars, length, script);
}
bool
JS::Compile(JSContext* cx, const ReadOnlyCompileOptions& options,
FILE* file, JS::MutableHandleScript script)
@ -4106,16 +4092,6 @@ JS::CompileForNonSyntacticScope(JSContext* cx, const ReadOnlyCompileOptions& opt
return ::Compile(cx, options, bytes, length, script);
}
bool
JS::CompileForNonSyntacticScope(JSContext* cx, const ReadOnlyCompileOptions& optionsArg,
const char16_t* chars, size_t length,
JS::MutableHandleScript script)
{
CompileOptions options(cx, optionsArg);
options.setNonSyntacticScope(true);
return ::Compile(cx, options, chars, length, script);
}
bool
JS::CompileForNonSyntacticScope(JSContext* cx, const ReadOnlyCompileOptions& optionsArg,
FILE* file, JS::MutableHandleScript script)
@ -4361,10 +4337,10 @@ JS_CompileScript(JSContext* cx, const char* ascii, size_t length,
}
JS_PUBLIC_API(bool)
JS_CompileUCScript(JSContext* cx, const char16_t* chars, size_t length,
JS_CompileUCScript(JSContext* cx, JS::SourceBufferHolder& srcBuf,
const JS::CompileOptions& options, MutableHandleScript script)
{
return ::Compile(cx, options, chars, length, script);
return ::Compile(cx, options, srcBuf, script);
}
JS_PUBLIC_API(bool)
@ -4581,33 +4557,23 @@ JS::CompileFunction(JSContext* cx, AutoObjectVector& envChain,
scope, fun);
}
JS_PUBLIC_API(bool)
JS::CompileFunction(JSContext* cx, AutoObjectVector& envChain,
const ReadOnlyCompileOptions& options,
const char* name, unsigned nargs, const char* const* argnames,
const char16_t* chars, size_t length, MutableHandleFunction fun)
{
SourceBufferHolder srcBuf(chars, length, SourceBufferHolder::NoOwnership);
return CompileFunction(cx, envChain, options, name, nargs, argnames,
srcBuf, fun);
}
JS_PUBLIC_API(bool)
JS::CompileFunction(JSContext* cx, AutoObjectVector& envChain,
const ReadOnlyCompileOptions& options,
const char* name, unsigned nargs, const char* const* argnames,
const char* bytes, size_t length, MutableHandleFunction fun)
{
UniqueTwoByteChars chars;
char16_t* chars;
if (options.utf8)
chars.reset(UTF8CharsToNewTwoByteCharsZ(cx, UTF8Chars(bytes, length), &length).get());
chars = UTF8CharsToNewTwoByteCharsZ(cx, UTF8Chars(bytes, length), &length).get();
else
chars.reset(InflateString(cx, bytes, length));
chars = InflateString(cx, bytes, length);
if (!chars)
return false;
SourceBufferHolder source(chars, length, SourceBufferHolder::GiveOwnership);
return CompileFunction(cx, envChain, options, name, nargs, argnames,
chars.get(), length, fun);
source, fun);
}
JS_PUBLIC_API(bool)
@ -4786,15 +4752,6 @@ Evaluate(JSContext* cx, AutoObjectVector& envChain, const ReadOnlyCompileOptions
return ::Evaluate(cx, scope->kind(), env, optionsArg, srcBuf, rval);
}
static bool
Evaluate(JSContext* cx, const ReadOnlyCompileOptions& optionsArg,
const char16_t* chars, size_t length, MutableHandleValue rval)
{
SourceBufferHolder srcBuf(chars, length, SourceBufferHolder::NoOwnership);
RootedObject globalLexical(cx, &cx->global()->lexicalEnvironment());
return ::Evaluate(cx, ScopeKind::Global, globalLexical, optionsArg, srcBuf, rval);
}
extern JS_PUBLIC_API(bool)
JS::Evaluate(JSContext* cx, const ReadOnlyCompileOptions& options,
const char* bytes, size_t length, MutableHandleValue rval)
@ -4844,21 +4801,6 @@ JS::Evaluate(JSContext* cx, AutoObjectVector& envChain, const ReadOnlyCompileOpt
return ::Evaluate(cx, envChain, optionsArg, srcBuf, rval);
}
JS_PUBLIC_API(bool)
JS::Evaluate(JSContext* cx, const ReadOnlyCompileOptions& optionsArg,
const char16_t* chars, size_t length, MutableHandleValue rval)
{
return ::Evaluate(cx, optionsArg, chars, length, rval);
}
JS_PUBLIC_API(bool)
JS::Evaluate(JSContext* cx, AutoObjectVector& envChain, const ReadOnlyCompileOptions& optionsArg,
const char16_t* chars, size_t length, MutableHandleValue rval)
{
SourceBufferHolder srcBuf(chars, length, SourceBufferHolder::NoOwnership);
return ::Evaluate(cx, envChain, optionsArg, srcBuf, rval);
}
JS_PUBLIC_API(bool)
JS::Evaluate(JSContext* cx, const ReadOnlyCompileOptions& optionsArg,
const char* filename, MutableHandleValue rval)

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

@ -3186,7 +3186,7 @@ JS_CompileScript(JSContext* cx, const char* ascii, size_t length,
* |script| will always be set. On failure, it will be set to nullptr.
*/
extern JS_PUBLIC_API(bool)
JS_CompileUCScript(JSContext* cx, const char16_t* chars, size_t length,
JS_CompileUCScript(JSContext* cx, JS::SourceBufferHolder& srcBuf,
const JS::CompileOptions& options,
JS::MutableHandleScript script);
@ -3599,10 +3599,6 @@ extern JS_PUBLIC_API(bool)
Compile(JSContext* cx, const ReadOnlyCompileOptions& options,
const char* bytes, size_t length, JS::MutableHandleScript script);
extern JS_PUBLIC_API(bool)
Compile(JSContext* cx, const ReadOnlyCompileOptions& options,
const char16_t* chars, size_t length, JS::MutableHandleScript script);
extern JS_PUBLIC_API(bool)
Compile(JSContext* cx, const ReadOnlyCompileOptions& options,
FILE* file, JS::MutableHandleScript script);
@ -3619,10 +3615,6 @@ extern JS_PUBLIC_API(bool)
CompileForNonSyntacticScope(JSContext* cx, const ReadOnlyCompileOptions& options,
const char* bytes, size_t length, JS::MutableHandleScript script);
extern JS_PUBLIC_API(bool)
CompileForNonSyntacticScope(JSContext* cx, const ReadOnlyCompileOptions& options,
const char16_t* chars, size_t length, JS::MutableHandleScript script);
extern JS_PUBLIC_API(bool)
CompileForNonSyntacticScope(JSContext* cx, const ReadOnlyCompileOptions& options,
FILE* file, JS::MutableHandleScript script);
@ -3712,15 +3704,6 @@ CancelMultiOffThreadScriptsDecoder(JSContext* cx, OffThreadToken* token);
* global must not be explicitly included in the scope chain.
*/
extern JS_PUBLIC_API(bool)
CompileFunction(JSContext* cx, AutoObjectVector& envChain,
const ReadOnlyCompileOptions& options,
const char* name, unsigned nargs, const char* const* argnames,
const char16_t* chars, size_t length, JS::MutableHandleFunction fun);
/**
* Same as above, but taking a SourceBufferHolder for the function body.
*/
extern JS_PUBLIC_API(bool)
CompileFunction(JSContext* cx, AutoObjectVector& envChain,
const ReadOnlyCompileOptions& options,
const char* name, unsigned nargs, const char* const* argnames,
@ -3837,22 +3820,6 @@ extern JS_PUBLIC_API(bool)
Evaluate(JSContext* cx, AutoObjectVector& envChain, const ReadOnlyCompileOptions& options,
SourceBufferHolder& srcBuf, JS::MutableHandleValue rval);
/**
* Evaluate the given character buffer in the scope of the current global of cx.
*/
extern JS_PUBLIC_API(bool)
Evaluate(JSContext* cx, const ReadOnlyCompileOptions& options,
const char16_t* chars, size_t length, JS::MutableHandleValue rval);
/**
* As above, but providing an explicit scope chain. envChain must not include
* the global object on it; that's implicit. It needs to contain the other
* objects that should end up on the script's scope chain.
*/
extern JS_PUBLIC_API(bool)
Evaluate(JSContext* cx, AutoObjectVector& envChain, const ReadOnlyCompileOptions& options,
const char16_t* chars, size_t length, JS::MutableHandleValue rval);
/**
* Evaluate the given byte buffer in the scope of the current global of cx.
*/

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

@ -1985,10 +1985,12 @@ Evaluate(JSContext* cx, unsigned argc, Value* vp)
return false;
} else {
mozilla::Range<const char16_t> chars = codeChars.twoByteRange();
JS::SourceBufferHolder srcBuf(chars.begin().get(), chars.length(),
JS::SourceBufferHolder::NoOwnership);
if (envChain.length() == 0) {
(void) JS::Compile(cx, options, chars.begin().get(), chars.length(), &script);
(void) JS::Compile(cx, options, srcBuf, &script);
} else {
(void) JS::CompileForNonSyntacticScope(cx, options, chars.begin().get(), chars.length(), &script);
(void) JS::CompileForNonSyntacticScope(cx, options, srcBuf, &script);
}
}
@ -2187,8 +2189,8 @@ Run(JSContext* cx, unsigned argc, Value* vp)
if (!chars.initTwoByte(cx, str))
return false;
const char16_t* ucbuf = chars.twoByteRange().begin().get();
size_t buflen = str->length();
JS::SourceBufferHolder srcBuf(chars.twoByteRange().begin().get(), str->length(),
JS::SourceBufferHolder::NoOwnership);
RootedScript script(cx);
int64_t startClock = PRMJ_Now();
@ -2203,7 +2205,7 @@ Run(JSContext* cx, unsigned argc, Value* vp)
.setFileAndLine(filename.ptr(), 1)
.setIsRunOnce(true)
.setNoScriptRval(true);
if (!JS_CompileUCScript(cx, ucbuf, buflen, options, &script))
if (!JS_CompileUCScript(cx, srcBuf, options, &script))
return false;
}
@ -3549,7 +3551,8 @@ EvalInContext(JSContext* cx, unsigned argc, Value* vp)
}
JS::CompileOptions opts(cx);
opts.setFileAndLine(filename.get(), lineno);
if (!JS::Evaluate(cx, opts, src, srclen, args.rval())) {
JS::SourceBufferHolder srcBuf(src, srclen, JS::SourceBufferHolder::NoOwnership);
if (!JS::Evaluate(cx, opts, srcBuf, args.rval())) {
return false;
}
}
@ -3652,7 +3655,9 @@ WorkerMain(WorkerInput* input)
AutoReportException are(cx);
RootedScript script(cx);
if (!JS::Compile(cx, options, input->chars.get(), input->length, &script))
JS::SourceBufferHolder srcBuf(input->chars.get(), input->length,
JS::SourceBufferHolder::NoOwnership);
if (!JS::Compile(cx, options, srcBuf, &script))
break;
RootedValue result(cx);
JS_ExecuteScript(cx, script, &result);
@ -4238,8 +4243,10 @@ Compile(JSContext* cx, unsigned argc, Value* vp)
.setIsRunOnce(true)
.setNoScriptRval(true);
RootedScript script(cx);
const char16_t* chars = stableChars.twoByteRange().begin().get();
bool ok = JS_CompileUCScript(cx, chars, scriptContents->length(), options, &script);
JS::SourceBufferHolder srcBuf(stableChars.twoByteRange().begin().get(),
scriptContents->length(),
JS::SourceBufferHolder::NoOwnership);
bool ok = JS_CompileUCScript(cx, srcBuf, options, &script);
args.rval().setUndefined();
return ok;
}
@ -6686,15 +6693,16 @@ EntryPoints(JSContext* cx, unsigned argc, Value* vp)
AutoStableStringChars stableChars(cx);
if (!stableChars.initTwoByte(cx, codeString))
return false;
const char16_t* chars = stableChars.twoByteRange().begin().get();
size_t length = codeString->length();
JS::SourceBufferHolder srcBuf(stableChars.twoByteRange().begin().get(),
codeString->length(),
JS::SourceBufferHolder::NoOwnership);
CompileOptions options(cx);
options.setIntroductionType("entryPoint eval")
.setFileAndLine("entryPoint eval", 1);
js::shell::ShellAutoEntryMonitor sarep(cx);
if (!JS::Evaluate(cx, options, chars, length, &dummy))
if (!JS::Evaluate(cx, options, srcBuf, &dummy))
return false;
return sarep.buildResult(cx, args.rval());
}

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

@ -143,7 +143,9 @@ AsyncScriptCompiler::StartCompile(JSContext* aCx)
}
Rooted<JSScript*> script(aCx);
if (!JS::Compile(aCx, mOptions, mScriptText.get(), mScriptLength, &script)) {
JS::SourceBufferHolder srcBuf(mScriptText.get(), mScriptLength,
JS::SourceBufferHolder::NoOwnership);
if (!JS::Compile(aCx, mOptions, srcBuf, &script)) {
return false;
}

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

@ -1849,8 +1849,9 @@ xpc::EvalInSandbox(JSContext* cx, HandleObject sandboxArg, const nsAString& sour
JS::CompileOptions options(sandcx);
options.setFileAndLine(filenameBuf.get(), lineNo);
MOZ_ASSERT(JS_IsGlobalObject(sandbox));
ok = JS::Evaluate(sandcx, options,
PromiseFlatString(source).get(), source.Length(), &v);
JS::SourceBufferHolder buffer(PromiseFlatString(source).get(), source.Length(),
JS::SourceBufferHolder::NoOwnership);
ok = JS::Evaluate(sandcx, options, buffer, &v);
// If the sandbox threw an exception, grab it off the context.
if (aes.HasException()) {