diff options
author | Kris Maglione <maglione.k@gmail.com> | 2011-09-22 21:55:58 -0400 |
---|---|---|
committer | Kris Maglione <maglione.k@gmail.com> | 2011-09-22 21:55:58 -0400 |
commit | e1db34990b848ce5f5aa900f3d8d895ef0e262eb (patch) | |
tree | 7479c587f526f6a2435f4f6f5622347717348cbc | |
parent | 6af256bc53e5c7eba63e3df64638ad5f1da99711 (diff) | |
download | pentadactyl-e1db34990b848ce5f5aa900f3d8d895ef0e262eb.tar.gz |
Use services.dactyl when available.
-rw-r--r-- | binary/src/mozJSLoaderUtils.cpp | 1 | ||||
-rw-r--r-- | binary/src/mozJSLoaderUtils.h | 10 | ||||
-rw-r--r-- | binary/src/subscriptLoader.cpp | 98 | ||||
-rwxr-xr-x | common/bootstrap.js | 10 | ||||
-rw-r--r-- | common/content/dactyl.js | 53 | ||||
-rw-r--r-- | common/modules/main.jsm | 8 |
6 files changed, 148 insertions, 32 deletions
diff --git a/binary/src/mozJSLoaderUtils.cpp b/binary/src/mozJSLoaderUtils.cpp index c9cfb91c..8a4c517c 100644 --- a/binary/src/mozJSLoaderUtils.cpp +++ b/binary/src/mozJSLoaderUtils.cpp @@ -39,7 +39,6 @@ #include "mozJSLoaderUtils.h" #include "nsAutoPtr.h" -#include "nsScriptLoader.h" #include "jsapi.h" #include "jsxdrapi.h" diff --git a/binary/src/mozJSLoaderUtils.h b/binary/src/mozJSLoaderUtils.h index 0b47b947..b56e7296 100644 --- a/binary/src/mozJSLoaderUtils.h +++ b/binary/src/mozJSLoaderUtils.h @@ -40,6 +40,12 @@ #ifndef mozJSLoaderUtils_h #define mozJSLoaderUtils_h +/* + * This is evil. Very evil. +#define nsString_h___ +#include "nsStringGlue.h" + */ + #include "nsIStartupCache.h" #include "nsStringAPI.h" #include "jsapi.h" @@ -52,10 +58,6 @@ class StartupCache; } } -#include "nsIWeakReferenceUtils.h" -typedef nsString nsAFlatString; -#include "nsScriptLoader.h" - nsresult ReadCachedScript(nsIStartupCache* cache, nsACString &uri, JSContext *cx, JSScript **scriptObj); diff --git a/binary/src/subscriptLoader.cpp b/binary/src/subscriptLoader.cpp index 3b552a4a..ac62177a 100644 --- a/binary/src/subscriptLoader.cpp +++ b/binary/src/subscriptLoader.cpp @@ -64,6 +64,16 @@ #include "nsIScriptSecurityManager.h" #include "nsIFileURL.h" +// ConvertToUTF16 +#include "nsICharsetConverterManager.h" +#include "nsIUnicodeDecoder.h" +template<class T> +inline PRBool EnsureStringLength(T& aStr, PRUint32 aLen) +{ + aStr.SetLength(aLen); + return (aStr.Length() == aLen); +} + #include "jsapi.h" #include "jscntxt.h" #include "jsdbgapi.h" @@ -74,7 +84,6 @@ #include "mozilla/scache/StartupCacheUtils.h" using namespace mozilla::scache; - class nsACString_internal : nsACString {}; namespace mozilla { @@ -96,6 +105,89 @@ namespace mozilla { #define LOAD_ERROR_NOPRINCIPALS "Failed to get principals." #define LOAD_ERROR_NOSPEC "Failed to get URI spec. This is bad." +/* Internal linkage, bloody hell... */ +static nsresult +ConvertToUTF16(nsIChannel* aChannel, const PRUint8* aData, + PRUint32 aLength, const nsString& aHintCharset, + nsString& aString) +{ + if (!aLength) { + aString.Truncate(); + return NS_OK; + } + + nsCAutoString characterSet; + + nsresult rv = NS_OK; + if (aChannel) { + rv = aChannel->GetContentCharset(characterSet); + } + + if (!aHintCharset.IsEmpty() && (NS_FAILED(rv) || characterSet.IsEmpty())) { + // charset name is always ASCII. + LossyCopyUTF16toASCII(aHintCharset, characterSet); + } + + if (characterSet.IsEmpty()) { + // fall back to ISO-8859-1, see bug 118404 + characterSet.AssignLiteral("ISO-8859-1"); + } + + nsCOMPtr<nsICharsetConverterManager> charsetConv = + do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv); + + nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder; + + if (NS_SUCCEEDED(rv) && charsetConv) { + rv = charsetConv->GetUnicodeDecoder(characterSet.get(), + getter_AddRefs(unicodeDecoder)); + if (NS_FAILED(rv)) { + // fall back to ISO-8859-1 if charset is not supported. (bug 230104) + rv = charsetConv->GetUnicodeDecoderRaw("ISO-8859-1", + getter_AddRefs(unicodeDecoder)); + } + } + + // converts from the charset to unicode + if (NS_SUCCEEDED(rv)) { + PRInt32 unicodeLength = 0; + + rv = unicodeDecoder->GetMaxLength(reinterpret_cast<const char*>(aData), + aLength, &unicodeLength); + if (NS_SUCCEEDED(rv)) { + if (!EnsureStringLength(aString, unicodeLength)) + return NS_ERROR_OUT_OF_MEMORY; + + PRUnichar *ustr = aString.BeginWriting(); + + PRInt32 consumedLength = 0; + PRInt32 originalLength = aLength; + PRInt32 convertedLength = 0; + PRInt32 bufferLength = unicodeLength; + do { + rv = unicodeDecoder->Convert(reinterpret_cast<const char*>(aData), + (PRInt32 *) &aLength, ustr, + &unicodeLength); + if (NS_FAILED(rv)) { + // if we failed, we consume one byte, replace it with U+FFFD + // and try the conversion again. + ustr[unicodeLength++] = (PRUnichar)0xFFFD; + ustr += unicodeLength; + + unicodeDecoder->Reset(); + } + aData += ++aLength; + consumedLength += aLength; + aLength = originalLength - consumedLength; + convertedLength += unicodeLength; + unicodeLength = bufferLength - convertedLength; + } while (NS_FAILED(rv) && (originalLength > consumedLength) && (bufferLength > convertedLength)); + aString.SetLength(convertedLength); + } + } + return rv; +} + // We just use the same reporter as the component loader static void mozJSLoaderErrorReporter(JSContext *cx, const char *message, JSErrorReport *rep) @@ -226,9 +318,9 @@ ReadScript(nsIURI *uri, JSContext *cx, JSObject *target_obj, if (charset) { nsString script; - rv = nsScriptLoader::ConvertToUTF16( + rv = ConvertToUTF16( nsnull, reinterpret_cast<const PRUint8*>(buf.get()), len, - nsDependentString(reinterpret_cast<PRUnichar*>(charset)), nsnull, script); + nsDependentString(reinterpret_cast<PRUnichar*>(charset)), script); if (NS_FAILED(rv)) { JSPRINCIPALS_DROP(cx, jsPrincipals); diff --git a/common/bootstrap.js b/common/bootstrap.js index 30a5a673..93f4d658 100755 --- a/common/bootstrap.js +++ b/common/bootstrap.js @@ -58,6 +58,13 @@ let components = {}; let resources = []; let getURI = null; +function updateLoader() { + try { + JSMLoader.loader = Cc["@dactyl.googlecode.com/extra/utils"].getService(Ci.dactylIUtils); + } + catch (e) {}; +} + /** * Performs necessary migrations after a version change. */ @@ -100,6 +107,8 @@ function startup(data, reason) { name = data.id.replace(/@.*/, ""); AddonManager.getAddonByID(addon.id, function (a) { addon = a; + + updateLoader(); updateVersion(); if (typeof require !== "undefined") require(global, "main"); @@ -259,6 +268,7 @@ function init() { Services.obs.notifyObservers(null, "dactyl-rehash", null); updateVersion(); + updateLoader(); if (addon !== addonData) require(global, "main"); } diff --git a/common/content/dactyl.js b/common/content/dactyl.js index 53305344..4fbb5507 100644 --- a/common/content/dactyl.js +++ b/common/content/dactyl.js @@ -492,38 +492,45 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), { let info = contexts.context; if (fileName == null) - if (info && info.file[0] !== "[") + if (info) ({ file: fileName, line: lineNumber, context: ctxt }) = info; + if (fileName && fileName[0] == "[") + fileName = "dactyl://command-line/"; + if (!context && fileName && fileName[0] !== "[") context = ctxt || _userContext; if (isinstance(context, ["Sandbox"])) return Cu.evalInSandbox(str, context, "1.8", fileName, lineNumber); - else - try { - if (!context) - context = userContext || ctxt; - - context[EVAL_ERROR] = null; - context[EVAL_STRING] = str; - context[EVAL_RESULT] = null; - this.loadScript("resource://dactyl-content/eval.js", context); - if (context[EVAL_ERROR]) { - try { - context[EVAL_ERROR].fileName = info.file; - context[EVAL_ERROR].lineNumber += info.line; + else { + if (!context) + context = userContext || ctxt; + + if (services.has("dactyl") && services.dactyl.evalInContext) + return services.dactyl.evalInContext(str, context, fileName, lineNumber); + else + try { + context[EVAL_ERROR] = null; + context[EVAL_STRING] = str; + context[EVAL_RESULT] = null; + this.loadScript("resource://dactyl-content/eval.js", context); + if (context[EVAL_ERROR]) { + try { + context[EVAL_ERROR].fileName = info.file; + context[EVAL_ERROR].lineNumber += info.line; + } + catch (e) {} + throw context[EVAL_ERROR]; } - catch (e) {} - throw context[EVAL_ERROR]; + return context[EVAL_RESULT]; } - return context[EVAL_RESULT]; - } - finally { - delete context[EVAL_ERROR]; - delete context[EVAL_RESULT]; - delete context[EVAL_STRING]; - } + finally { + delete context[EVAL_ERROR]; + delete context[EVAL_RESULT]; + delete context[EVAL_STRING]; + } + } }, /** diff --git a/common/modules/main.jsm b/common/modules/main.jsm index f61557c2..d6f18597 100644 --- a/common/modules/main.jsm +++ b/common/modules/main.jsm @@ -139,7 +139,13 @@ var Modules = function Modules(window) { newContext: function newContext(proto, normal) { if (normal) return create(proto); - let sandbox = Components.utils.Sandbox(window, { sandboxPrototype: proto || modules, wantXrays: false }); + + if (services.has("dactyl") && services.dactyl.createGlobal) + var sandbox = services.dactyl.createGlobal(); + else + sandbox = Components.utils.Sandbox(window, { sandboxPrototype: proto || modules, + wantXrays: false }); + // Hack: sandbox.Object = jsmodules.Object; sandbox.File = jsmodules.File; |