1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-22 00:37:58 +01:00

Use services.dactyl when available.

This commit is contained in:
Kris Maglione
2011-09-22 21:55:58 -04:00
parent 6af256bc53
commit e1db34990b
6 changed files with 147 additions and 31 deletions

View File

@@ -39,7 +39,6 @@
#include "mozJSLoaderUtils.h" #include "mozJSLoaderUtils.h"
#include "nsAutoPtr.h" #include "nsAutoPtr.h"
#include "nsScriptLoader.h"
#include "jsapi.h" #include "jsapi.h"
#include "jsxdrapi.h" #include "jsxdrapi.h"

View File

@@ -40,6 +40,12 @@
#ifndef mozJSLoaderUtils_h #ifndef mozJSLoaderUtils_h
#define mozJSLoaderUtils_h #define mozJSLoaderUtils_h
/*
* This is evil. Very evil.
#define nsString_h___
#include "nsStringGlue.h"
*/
#include "nsIStartupCache.h" #include "nsIStartupCache.h"
#include "nsStringAPI.h" #include "nsStringAPI.h"
#include "jsapi.h" #include "jsapi.h"
@@ -52,10 +58,6 @@ class StartupCache;
} }
} }
#include "nsIWeakReferenceUtils.h"
typedef nsString nsAFlatString;
#include "nsScriptLoader.h"
nsresult nsresult
ReadCachedScript(nsIStartupCache* cache, nsACString &uri, ReadCachedScript(nsIStartupCache* cache, nsACString &uri,
JSContext *cx, JSScript **scriptObj); JSContext *cx, JSScript **scriptObj);

View File

@@ -64,6 +64,16 @@
#include "nsIScriptSecurityManager.h" #include "nsIScriptSecurityManager.h"
#include "nsIFileURL.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 "jsapi.h"
#include "jscntxt.h" #include "jscntxt.h"
#include "jsdbgapi.h" #include "jsdbgapi.h"
@@ -74,7 +84,6 @@
#include "mozilla/scache/StartupCacheUtils.h" #include "mozilla/scache/StartupCacheUtils.h"
using namespace mozilla::scache; using namespace mozilla::scache;
class nsACString_internal : nsACString {}; class nsACString_internal : nsACString {};
namespace mozilla { namespace mozilla {
@@ -96,6 +105,89 @@ namespace mozilla {
#define LOAD_ERROR_NOPRINCIPALS "Failed to get principals." #define LOAD_ERROR_NOPRINCIPALS "Failed to get principals."
#define LOAD_ERROR_NOSPEC "Failed to get URI spec. This is bad." #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 // We just use the same reporter as the component loader
static void static void
mozJSLoaderErrorReporter(JSContext *cx, const char *message, JSErrorReport *rep) mozJSLoaderErrorReporter(JSContext *cx, const char *message, JSErrorReport *rep)
@@ -226,9 +318,9 @@ ReadScript(nsIURI *uri, JSContext *cx, JSObject *target_obj,
if (charset) { if (charset) {
nsString script; nsString script;
rv = nsScriptLoader::ConvertToUTF16( rv = ConvertToUTF16(
nsnull, reinterpret_cast<const PRUint8*>(buf.get()), len, 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)) { if (NS_FAILED(rv)) {
JSPRINCIPALS_DROP(cx, jsPrincipals); JSPRINCIPALS_DROP(cx, jsPrincipals);

10
common/bootstrap.js vendored
View File

@@ -58,6 +58,13 @@ let components = {};
let resources = []; let resources = [];
let getURI = null; 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. * Performs necessary migrations after a version change.
*/ */
@@ -100,6 +107,8 @@ function startup(data, reason) {
name = data.id.replace(/@.*/, ""); name = data.id.replace(/@.*/, "");
AddonManager.getAddonByID(addon.id, function (a) { AddonManager.getAddonByID(addon.id, function (a) {
addon = a; addon = a;
updateLoader();
updateVersion(); updateVersion();
if (typeof require !== "undefined") if (typeof require !== "undefined")
require(global, "main"); require(global, "main");
@@ -259,6 +268,7 @@ function init() {
Services.obs.notifyObservers(null, "dactyl-rehash", null); Services.obs.notifyObservers(null, "dactyl-rehash", null);
updateVersion(); updateVersion();
updateLoader();
if (addon !== addonData) if (addon !== addonData)
require(global, "main"); require(global, "main");
} }

View File

@@ -492,38 +492,45 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
let info = contexts.context; let info = contexts.context;
if (fileName == null) if (fileName == null)
if (info && info.file[0] !== "[") if (info)
({ file: fileName, line: lineNumber, context: ctxt }) = info; ({ file: fileName, line: lineNumber, context: ctxt }) = info;
if (fileName && fileName[0] == "[")
fileName = "dactyl://command-line/";
if (!context && fileName && fileName[0] !== "[") if (!context && fileName && fileName[0] !== "[")
context = ctxt || _userContext; context = ctxt || _userContext;
if (isinstance(context, ["Sandbox"])) if (isinstance(context, ["Sandbox"]))
return Cu.evalInSandbox(str, context, "1.8", fileName, lineNumber); return Cu.evalInSandbox(str, context, "1.8", fileName, lineNumber);
else else {
try { if (!context)
if (!context) context = userContext || ctxt;
context = userContext || ctxt;
context[EVAL_ERROR] = null; if (services.has("dactyl") && services.dactyl.evalInContext)
context[EVAL_STRING] = str; return services.dactyl.evalInContext(str, context, fileName, lineNumber);
context[EVAL_RESULT] = null; else
this.loadScript("resource://dactyl-content/eval.js", context); try {
if (context[EVAL_ERROR]) { context[EVAL_ERROR] = null;
try { context[EVAL_STRING] = str;
context[EVAL_ERROR].fileName = info.file; context[EVAL_RESULT] = null;
context[EVAL_ERROR].lineNumber += info.line; 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) {} return context[EVAL_RESULT];
throw context[EVAL_ERROR];
} }
return context[EVAL_RESULT]; finally {
} delete context[EVAL_ERROR];
finally { delete context[EVAL_RESULT];
delete context[EVAL_ERROR]; delete context[EVAL_STRING];
delete context[EVAL_RESULT]; }
delete context[EVAL_STRING]; }
}
}, },
/** /**

View File

@@ -139,7 +139,13 @@ var Modules = function Modules(window) {
newContext: function newContext(proto, normal) { newContext: function newContext(proto, normal) {
if (normal) if (normal)
return create(proto); 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: // Hack:
sandbox.Object = jsmodules.Object; sandbox.Object = jsmodules.Object;
sandbox.File = jsmodules.File; sandbox.File = jsmodules.File;