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 "nsAutoPtr.h"
#include "nsScriptLoader.h"
#include "jsapi.h"
#include "jsxdrapi.h"

View File

@@ -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);

View File

@@ -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);

10
common/bootstrap.js vendored
View File

@@ -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");
}

View File

@@ -492,19 +492,25 @@ 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 {
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;
@@ -524,6 +530,7 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
delete context[EVAL_RESULT];
delete context[EVAL_STRING];
}
}
},
/**

View File

@@ -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;