Bug 1515801 - Rip out vestigial handling of non-UTF-8 character sets from the subscript loader. r=kmag

This commit is contained in:
Jeff Walden
2018-12-14 14:25:56 -08:00
parent e3fa7b988c
commit 1972c8e797
2 changed files with 18 additions and 50 deletions

View File

@@ -50,7 +50,6 @@ class MOZ_STACK_CLASS LoadSubScriptOptions : public OptionsBase {
JSObject* options = nullptr)
: OptionsBase(cx, options),
target(cx),
charset(VoidString()),
ignoreCache(false),
async(false),
wantReturnValue(false) {}
@@ -63,7 +62,6 @@ class MOZ_STACK_CLASS LoadSubScriptOptions : public OptionsBase {
}
RootedObject target;
nsString charset;
bool ignoreCache;
bool async;
bool wantReturnValue;
@@ -129,40 +127,15 @@ static void ReportError(JSContext* cx, const char* origMsg, nsIURI* uri) {
}
static bool PrepareScript(nsIURI* uri, JSContext* cx, bool wantGlobalScript,
const char* uriStr, const nsAString& charset,
const char* buf, int64_t len, bool wantReturnValue,
MutableHandleScript script) {
const char* uriStr, const char* buf, int64_t len,
bool wantReturnValue, MutableHandleScript script) {
JS::CompileOptions options(cx);
options.setFileAndLine(uriStr, 1).setNoScriptRval(!wantReturnValue);
if (!charset.IsVoid()) {
char16_t* scriptBuf = nullptr;
size_t scriptLength = 0;
nsresult rv = ScriptLoader::ConvertToUTF16(
nullptr, reinterpret_cast<const uint8_t*>(buf), len, charset, nullptr,
scriptBuf, scriptLength);
if (NS_FAILED(rv)) {
ReportError(cx, LOAD_ERROR_BADCHARSET, uri);
return false;
}
JS::SourceText<char16_t> srcBuf;
if (!srcBuf.init(cx, JS::UniqueTwoByteChars(scriptBuf), scriptLength)) {
return false;
}
if (wantGlobalScript) {
return JS::Compile(cx, options, srcBuf, script);
}
return JS::CompileForNonSyntacticScope(cx, options, srcBuf, script);
}
// We only use lazy source when no special encoding is specified because
// the lazy source loader doesn't know the encoding.
options.setSourceIsLazy(true);
if (wantGlobalScript) {
return JS::CompileLatin1(cx, options, buf, len, script);
return JS::CompileUtf8(cx, options, buf, len, script);
}
return JS::CompileLatin1ForNonSyntacticScope(cx, options, buf, len, script);
return JS::CompileUtf8ForNonSyntacticScope(cx, options, buf, len, script);
}
static bool EvalScript(JSContext* cx, HandleObject targetObj,
@@ -268,13 +241,12 @@ class AsyncScriptLoader : public nsIIncrementalStreamLoaderObserver {
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(AsyncScriptLoader)
AsyncScriptLoader(nsIChannel* aChannel, bool aWantReturnValue,
JSObject* aTargetObj, JSObject* aLoadScope,
const nsAString& aCharset, bool aCache, Promise* aPromise)
JSObject* aTargetObj, JSObject* aLoadScope, bool aCache,
Promise* aPromise)
: mChannel(aChannel),
mTargetObj(aTargetObj),
mLoadScope(aLoadScope),
mPromise(aPromise),
mCharset(aCharset),
mWantReturnValue(aWantReturnValue),
mCache(aCache) {
// Needed for the cycle collector to manage mTargetObj.
@@ -288,7 +260,6 @@ class AsyncScriptLoader : public nsIIncrementalStreamLoaderObserver {
Heap<JSObject*> mTargetObj;
Heap<JSObject*> mLoadScope;
RefPtr<Promise> mPromise;
nsString mCharset;
bool mWantReturnValue;
bool mCache;
};
@@ -392,7 +363,7 @@ AsyncScriptLoader::OnStreamComplete(nsIIncrementalStreamLoader* aLoader,
RootedObject loadScope(cx, mLoadScope);
if (!PrepareScript(uri, cx, JS_IsGlobalObject(targetObj), spec.get(),
mCharset, reinterpret_cast<const char*>(aBuf), aLength,
reinterpret_cast<const char*>(aBuf), aLength,
mWantReturnValue, &script)) {
return NS_OK;
}
@@ -408,8 +379,8 @@ AsyncScriptLoader::OnStreamComplete(nsIIncrementalStreamLoader* aLoader,
nsresult mozJSSubScriptLoader::ReadScriptAsync(
nsIURI* uri, HandleObject targetObj, HandleObject loadScope,
const nsAString& charset, nsIIOService* serv, bool wantReturnValue,
bool cache, MutableHandleValue retval) {
nsIIOService* serv, bool wantReturnValue, bool cache,
MutableHandleValue retval) {
nsCOMPtr<nsIGlobalObject> globalObject = xpc::NativeGlobal(targetObj);
ErrorResult result;
@@ -446,7 +417,7 @@ nsresult mozJSSubScriptLoader::ReadScriptAsync(
channel->SetContentType(NS_LITERAL_CSTRING("application/javascript"));
RefPtr<AsyncScriptLoader> loadObserver = new AsyncScriptLoader(
channel, wantReturnValue, targetObj, loadScope, charset, cache, promise);
channel, wantReturnValue, targetObj, loadScope, cache, promise);
nsCOMPtr<nsIIncrementalStreamLoader> loader;
rv = NS_NewIncrementalStreamLoader(getter_AddRefs(loader), loadObserver);
@@ -458,7 +429,6 @@ nsresult mozJSSubScriptLoader::ReadScriptAsync(
bool mozJSSubScriptLoader::ReadScript(nsIURI* uri, JSContext* cx,
HandleObject targetObj,
const nsAString& charset,
const char* uriStr, nsIIOService* serv,
bool wantReturnValue,
bool useCompilationScope,
@@ -520,8 +490,8 @@ bool mozJSSubScriptLoader::ReadScript(nsIURI* uri, JSContext* cx,
ar.emplace(cx, xpc::CompilationScope());
}
return PrepareScript(uri, cx, JS_IsGlobalObject(targetObj), uriStr, charset,
buf.get(), len, wantReturnValue, script);
return PrepareScript(uri, cx, JS_IsGlobalObject(targetObj), uriStr, buf.get(),
len, wantReturnValue, script);
}
NS_IMETHODIMP
@@ -538,7 +508,6 @@ mozJSSubScriptLoader::LoadSubScript(const nsAString& url, HandleValue target,
* Should ONLY (O N L Y !) be called from JavaScript code.
*/
LoadSubScriptOptions options(cx);
options.charset.AssignLiteral("UTF-8");
options.target = target.isObject() ? &target.toObject() : nullptr;
return DoLoadSubScriptWithOptions(url, options, cx, retval);
}
@@ -551,12 +520,12 @@ mozJSSubScriptLoader::LoadSubScriptWithOptions(const nsAString& url,
if (!optionsVal.isObject()) {
return NS_ERROR_INVALID_ARG;
}
LoadSubScriptOptions options(cx, &optionsVal.toObject());
if (!options.Parse()) {
return NS_ERROR_INVALID_ARG;
}
options.charset.AssignLiteral("UTF-8");
return DoLoadSubScriptWithOptions(url, options, cx, retval);
}
@@ -679,7 +648,7 @@ nsresult mozJSSubScriptLoader::DoLoadSubScriptWithOptions(
// If we are doing an async load, trigger it and bail out.
if (!script && options.async) {
return ReadScriptAsync(uri, targetObj, loadScope, options.charset, serv,
return ReadScriptAsync(uri, targetObj, loadScope, serv,
options.wantReturnValue, !!cache, retval);
}
@@ -687,7 +656,7 @@ nsresult mozJSSubScriptLoader::DoLoadSubScriptWithOptions(
// |script| came from the cache, so don't bother writing it
// |back there.
cache = nullptr;
} else if (!ReadScript(uri, cx, targetObj, options.charset,
} else if (!ReadScript(uri, cx, targetObj,
static_cast<const char*>(uriStr.get()), serv,
options.wantReturnValue, useCompilationScope,
&script)) {