Commit jscodelib (bug#238324) onto branch. Needed for XTF javascript

utilities.


git-svn-id: svn://10.0.0.236/branches/XTF_20040312_BRANCH@155114 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
alex.fritze%crocodile-clips.com
2004-04-19 10:53:08 +00:00
parent a90a90f72b
commit b599affc5a
20 changed files with 1414 additions and 197 deletions

View File

@@ -237,6 +237,8 @@ js/src/xpconnect/shell/Makefile
js/src/xpconnect/tools/Makefile
js/src/xpconnect/tools/idl/Makefile
js/src/xpconnect/tools/idl/Makefile
js/src/xpconnect/codelib/Makefile
js/src/xpconnect/shared/Makefile
"
MAKEFILES_jsdebugger="

View File

@@ -71,7 +71,12 @@ XTF_BRANCH_MODIFIED_FILES = \
config/rules.mk \
content/Makefile.in \
content/base/src/nsNameSpaceManager.cpp \
js/src/xpconnect/Makefile.in \
js/src/xpconnect/loader/Makefile.in \
js/src/xpconnect/loader/mozJSComponentLoader.cpp \
js/src/xpconnect/loader/mozJSComponentLoader.h \
js/src/xpconnect/src/Makefile.in \
js/src/xpconnect/src/xpcmodule.cpp \
layout/Makefile.in \
layout/build/Makefile.in \
layout/build/nsLayoutModule.cpp \
@@ -111,6 +116,16 @@ XTF_BRANCH_NEW_FILES = \
content/xtf/tests/smiley/Makefile.in \
content/xtf/tests/smiley/smiley.js \
content/xtf/tests/smiley/smiley.svg \
js/src/xpconnect/codelib/Makefile.in \
js/src/xpconnect/codelib/mozIJSCodeLib.idl \
js/src/xpconnect/codelib/mozJSCodeLib.cpp \
js/src/xpconnect/codelib/mozJSCodeLib.h \
js/src/xpconnect/shared/Makefile.in \
js/src/xpconnect/shared/JSAutoContext.cpp \
js/src/xpconnect/shared/JSAutoContext.h \
js/src/xpconnect/shared/JSBackstagePass.cpp \
js/src/xpconnect/shared/JSBackstagePass.h \
js/src/xpconnect/loader/JSComponentUtils.js \
layout/xtf/Makefile.in \
layout/xtf/src/Makefile.in \
layout/xtf/src/nsXTFSVGDisplayFrame.cpp

View File

@@ -94,6 +94,7 @@ ACCESSIBILITY = @ACCESSIBILITY@
MOZ_VIEW_SOURCE = @MOZ_VIEW_SOURCE@
MOZ_XPINSTALL = @MOZ_XPINSTALL@
MOZ_JSLOADER = @MOZ_JSLOADER@
MOZ_JSCODELIB = @MOZ_JSCODELIB@
MOZ_USE_NATIVE_UCONV = @MOZ_USE_NATIVE_UCONV@
MOZ_SINGLE_PROFILE = @MOZ_SINGLE_PROFILE@
MOZ_LDAP_XPCOM = @MOZ_LDAP_XPCOM@

View File

@@ -3661,6 +3661,18 @@ if test "$MOZ_JSLOADER"; then
AC_DEFINE(MOZ_JSLOADER)
fi
dnl ========================================================
dnl JSCodeLib
dnl ========================================================
MOZ_JSCODELIB=1
MOZ_ARG_DISABLE_BOOL(jscodelib,
[ --disable-jscodelib Disable js shared code library],
MOZ_JSCODELIB=,
MOZ_JSCODELIB=1 )
if test -n "$MOZ_JSCODELIB"; then
AC_DEFINE(MOZ_JSCODELIB)
fi
dnl ========================================================
dnl use native unicode converters
dnl ========================================================
@@ -5385,6 +5397,7 @@ AC_SUBST(MOZ_LEAKY)
AC_SUBST(MOZ_JPROF)
AC_SUBST(MOZ_XPCTOOLS)
AC_SUBST(MOZ_JSLOADER)
AC_SUBST(MOZ_JSCODELIB)
AC_SUBST(MOZ_USE_NATIVE_UCONV)
AC_SUBST(MOZ_INSURE)
AC_SUBST(MOZ_INSURE_DIRS)

View File

@@ -46,8 +46,18 @@ include $(DEPTH)/config/autoconf.mk
MODULE = xpconnect
DIRS = public idl
ifdef MOZ_JSCODELIB
DIRS += codelib
NEEDS_XPCONNECT_SHARED = 1
endif
ifdef MOZ_JSLOADER
DIRS += loader
NEEDS_XPCONNECT_SHARED = 1
endif
ifdef NEEDS_XPCONNECT_SHARED
DIRS += shared
endif
DIRS += src shell

View File

@@ -0,0 +1,68 @@
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Mozilla code.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 1999
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either of the GNU General Public License Version 2 or later (the "GPL"),
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
DEPTH = ../../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = xpconnect
XPIDL_MODULE = jscodelib
LIBRARY_NAME = jscodelib_s
FORCE_STATIC_LIB = 1
REQUIRES = xpcom \
string \
js \
caps \
necko \
dom \
$(NULL)
CPPSRCS = mozJSCodeLib.cpp
LOCAL_INCLUDES = \
-I$(srcdir)/../shared \
$(NULL)
XPIDLSRCS = mozIJSCodeLib.idl
include $(topsrcdir)/config/rules.mk
DEFINES += -DJSFILE -DJS_THREADSAFE

View File

@@ -0,0 +1,71 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ----- BEGIN LICENSE BLOCK -----
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Mozilla JSCodeLib.
*
* The Initial Developer of the Original Code is
* Alex Fritze.
* Portions created by the Initial Developer are Copyright (C) 2004
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Alex Fritze <alex@croczilla.com> (original author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ----- END LICENSE BLOCK ----- */
#include "nsISupports.idl"
%{C++
struct JSObject;
// {621E3F64-7060-4AE1-A338-276FC522B393}
#define MOZ_JSCODELIB_CID \
{ 0x621e3f64, 0x7060, 0x4ae1, { 0xa3, 0x38, 0x27, 0x6f, 0xc5, 0x22, 0xb3, 0x93 } }
#define MOZ_JSCODELIB_CONTRACTID "@mozilla.org/jscodelib;1"
%}
[ptr] native JSObjectPtr(JSObject);
[scriptable, uuid(a25ce5fb-5c45-4e63-94f5-73ee94ead0c4)]
interface mozIJSCodeLib : nsISupports
{
/**
* To be called from JS only.
*
* Imports the JS code module at 'moduleURL' into the calling JS
* context. If the optional argument 'obj' is given, the module will
* be installed on 'obj' instead of the calling context's global
* object.
*/
void importModule(in AUTF8String moduleURL /* , [optional] in JSObject obj */);
/**
* Imports the JS code module at 'moduleURL' to the JS object 'obj'.
*/
[noscript] void importModuleToJSObject(in AUTF8String moduleURL, in JSObjectPtr obj);
};

View File

@@ -0,0 +1,430 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ----- BEGIN LICENSE BLOCK -----
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Mozilla JSCodeLib.
*
* The Initial Developer of the Original Code is
* Alex Fritze.
* Portions created by the Initial Developer are Copyright (C) 2004
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Alex Fritze <alex@croczilla.com> (original author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ----- END LICENSE BLOCK ----- */
#include "mozJSCodeLib.h"
#include "nsIServiceManager.h"
#include "nsIXPConnect.h"
#include "jsapi.h"
#include "JSAutoContext.h"
#include "JSBackstagePass.h"
#include "nsIIOService.h"
#include "nsNetCID.h"
#include "nsIChannel.h"
#include "nsIInputStream.h"
#include "nsIJSContextStack.h"
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
const char kJSContextStackContractID[] = "@mozilla.org/js/xpc/ContextStack;1";
const char kJSRuntimeServiceContractID[] = "@mozilla.org/js/xpc/RuntimeService;1";
#ifndef XPCONNECT_STANDALONE
#include "nsIScriptSecurityManager.h"
#include "nsIScriptObjectPrincipal.h"
const char kScriptSecurityManagerContractID[] = NS_SCRIPTSECURITYMANAGER_CONTRACTID;
#endif
static JSFunctionSpec gGlobalFunctions[] = {
{"dump", JSDump, 1},
{"debug", JSDebug, 1 },
{"importModule", JSImportModule, 1 },
{0}
};
////////////////////////////////////////////////////////////////////////
// mozJSCodeLib implementation
mozJSCodeLib::mozJSCodeLib()
{
}
mozJSCodeLib::~mozJSCodeLib()
{
UnloadModules();
}
nsresult mozJSCodeLib::Init()
{
mXPConnect = do_GetService(nsIXPConnect::GetCID());
if (!mXPConnect) {
NS_ERROR("failed to get xpconnect service");
return NS_ERROR_FAILURE;
}
mRuntimeService = do_GetService(kJSRuntimeServiceContractID);
if (!mRuntimeService) {
NS_ERROR("could not get JSRuntimeService");
return NS_ERROR_FAILURE;
}
#ifndef XPCONNECT_STANDALONE
nsCOMPtr<nsIScriptSecurityManager> secman =
do_GetService(kScriptSecurityManagerContractID);
if (!secman) {
NS_ERROR("could not get script security manager");
return NS_ERROR_FAILURE;
}
secman->GetSystemPrincipal(getter_AddRefs(mSystemPrincipal));
if (!mSystemPrincipal) {
NS_ERROR("could not get system principal");
return NS_ERROR_FAILURE;
}
#endif
if (!mModules.Init(4))
return NS_ERROR_FAILURE;
return NS_OK;
}
//----------------------------------------------------------------------
// nsISupports methods
NS_IMPL_THREADSAFE_ADDREF(mozJSCodeLib)
NS_IMPL_THREADSAFE_RELEASE(mozJSCodeLib)
NS_INTERFACE_MAP_BEGIN(mozJSCodeLib)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_ENTRY(mozIJSCodeLib)
NS_INTERFACE_MAP_END
//----------------------------------------------------------------------
// mozIJSCodeLib methods
/* void importModule (in AUTF8String moduleURL, [optional] in JSObject obj); */
NS_IMETHODIMP
mozJSCodeLib::ImportModule(const nsACString & moduleName)
{
// This function should only be called from JS.
nsCOMPtr<nsIXPCNativeCallContext> cc;
mXPConnect->GetCurrentNativeCallContext(getter_AddRefs(cc));
if (!cc) {
NS_ERROR("could not get native call context");
return NS_ERROR_FAILURE;
}
JSContext *cx = nsnull;
cc->GetJSContext (&cx);
JSObject *targetObject = nsnull;
PRUint32 argc = 0;
cc->GetArgc(&argc);
if (argc>1) {
// The caller passed in the second optional argument. Get it.
jsval *argv = nsnull;
cc->GetArgvPtr (&argv);
if (!JS_ValueToObject(cx, argv[1], &targetObject)) {
cc->SetExceptionWasThrown(JS_TRUE);
return NS_OK;
}
if (!targetObject) {
// This happens when the caller passes in 'null' as targetObject.
return NS_ERROR_FAILURE;
}
}
else {
// Our targetObject is the caller's global object. Find it by
// walking the calling object's parent chain.
nsCOMPtr<nsIXPConnectWrappedNative> wn;
cc->GetCalleeWrapper (getter_AddRefs(wn));
if (!wn) {
NS_ERROR("null callee wrapper");
return NS_ERROR_FAILURE;
}
wn->GetJSObject (&targetObject);
if (!targetObject) {
NS_ERROR("null calling object");
return NS_ERROR_FAILURE;
}
JSObject *parent = nsnull;
while ((parent = JS_GetParent(cx, targetObject)))
targetObject = parent;
}
NS_ASSERTION(targetObject, "null targetObject");
return ImportModuleToJSObject(moduleName, targetObject);
}
/* [noscript] void importModuleToJSObject (in AUTF8String moduleURL, in JSObjectPtr obj); */
NS_IMETHODIMP
mozJSCodeLib::ImportModuleToJSObject(const nsACString & moduleName, JSObject * targetObj)
{
#ifdef DEBUG
printf("mozJSCodeLib::ImportModuleToJSObject(%s,%p)\n", PromiseFlatCString(moduleName).get(), targetObj);
#endif
if (!targetObj) {
NS_ERROR("null target object");
return NS_ERROR_FAILURE;
}
JSObject *moduleObj = nsnull;
if (!mModules.Get(moduleName, &moduleObj)) {
moduleObj = LoadModule(moduleName);
}
if (!moduleObj) {
return NS_ERROR_FAILURE;
}
// Retrieve the code module's MOZ_EXPORTED_SYMBOLS array:
// we need a context; any will do
nsCOMPtr<nsIThreadJSContextStack> cxstack =
do_GetService(kJSContextStackContractID);
JSContext*cx = nsnull;
cxstack->GetSafeJSContext(&cx);
jsval symbols;
if (!JS_GetProperty(cx, moduleObj, "MOZ_EXPORTED_SYMBOLS", &symbols)) {
NS_ERROR("error finding MOZ_EXPORTED_SYMBOLS");
return NS_ERROR_FAILURE;
}
JSObject *symbolsObj = nsnull;
if (!JSVAL_IS_OBJECT(symbols) ||
!(symbolsObj = JSVAL_TO_OBJECT(symbols)) ||
!JS_IsArrayObject(cx, symbolsObj)) {
NS_ERROR("MOZ_EXPORTED_SYMBOLS is not an array");
return NS_ERROR_FAILURE;
}
// Iterate over symbols array, installing symbols on targetObj:
jsuint symbolCount = 0;
if (!JS_GetArrayLength(cx, symbolsObj, &symbolCount)) {
NS_ERROR("Error getting array length");
return NS_ERROR_FAILURE;
}
#ifdef DEBUG
printf("Installing symbols [ ");
#endif
for (jsuint i=0; i<symbolCount; ++i) {
jsval val;
JSString *symbolName;
if (!JS_GetElement(cx, symbolsObj, i, &val) ||
!JSVAL_IS_STRING(val) ||
!(symbolName = JSVAL_TO_STRING(val))) {
NS_ERROR("Array element is not a string");
return NS_ERROR_FAILURE;
}
#ifdef DEBUG
printf("%s ", JS_GetStringBytes(symbolName));
#endif
if (!JS_GetProperty(cx, moduleObj, JS_GetStringBytes(symbolName), &val)) {
NS_ERROR("Could not get symbol");
return NS_ERROR_FAILURE;
}
if (!JS_SetProperty(cx, targetObj, JS_GetStringBytes(symbolName), &val)) {
NS_ERROR("Could not set property on target");
return NS_ERROR_FAILURE;
}
}
#ifdef DEBUG
printf("]\n");
#endif
return NS_OK;
}
//----------------------------------------------------------------------
// implementation helpers:
PRInt32
mozJSCodeLib::LoadURL(char **buf, const nsACString &url)
{
*buf = nsnull;
PRInt32 content_length = -1;
PRUint32 bytesRead = 0;
nsCOMPtr<nsIIOService> ioserv;
nsCOMPtr<nsIChannel> channel;
nsCOMPtr<nsIInputStream> instream;
ioserv = do_GetService(kIOServiceCID);
if (!ioserv) {
NS_ERROR("could not get io service");
goto error;
}
ioserv->NewChannel(url, nsnull, nsnull, getter_AddRefs(channel));
if (!channel) {
NS_ERROR("could not create channel");
goto error;
}
channel->Open(getter_AddRefs(instream));
if (!instream) {
NS_ERROR("could not open stream");
goto error;
}
if (NS_FAILED(channel->GetContentLength(&content_length))) {
NS_ERROR("could not get content length");
goto error;
}
*buf = new char[content_length+1];
if (!*buf) {
NS_ERROR("could not alloc buffer");
goto error;
}
instream->Read(*buf, content_length, &bytesRead);
if (bytesRead != content_length) {
NS_ERROR("stream read error");
goto error;
}
return content_length;
error:
if (*buf)
delete[] *buf;
return -1;
}
JSObject *
mozJSCodeLib::LoadModule(const nsACString &module)
{
JSObject *moduleObj = nsnull;
char *buf = nsnull;
PRInt32 content_length = LoadURL(&buf, module);
if (!buf) {
NS_ERROR("error loading url");
return nsnull;
}
jsval result;
JSAutoContext cx;
JSPrincipals* jsprincipals = nsnull;
#ifndef XPCONNECT_STANDALONE
nsCOMPtr<nsIScriptObjectPrincipal> backstagePass =
new JSBackstagePass(mSystemPrincipal);
#else
nsCOMPtr<nsISupports> backstagePass = new JSBackstagePass();
#endif
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
mXPConnect->InitClassesWithNewWrappedGlobal(cx, backstagePass,
NS_GET_IID(nsISupports),
PR_FALSE,
getter_AddRefs(holder));
if (!holder) {
NS_ERROR("global initialization failed");
goto done;
}
holder->GetJSObject(&moduleObj);
if (!moduleObj) {
NS_ERROR("bad global object");
goto done;
}
if (!JS_DefineFunctions(cx, moduleObj, gGlobalFunctions)) {
moduleObj = nsnull;
NS_ERROR("error setting functions");
goto done;
}
#ifndef XPCONNECT_STANDALONE
mSystemPrincipal->GetJSPrincipals(cx, &jsprincipals);
if (!jsprincipals) {
moduleObj = nsnull;
NS_ERROR("could not get principals");
goto done;
}
#endif
if (JS_EvaluateScriptForPrincipals(cx, moduleObj,
jsprincipals, buf,
content_length,
PromiseFlatCString(module).get(), 1, &result)) {
// Add obj to hash and protect from gc until unloaded in
// UnloadModules()
ModulesHash::EntryType *entry = mModules.PutEntry(module);
if (!entry) {
moduleObj = nsnull;
goto done;
}
entry->mData = moduleObj;
JS_AddNamedRoot(cx, &(entry->mData), "mozJSCodeLib");
}
else
moduleObj = nsnull;
JSPRINCIPALS_DROP(cx, jsprincipals);
done:
if (buf)
delete[] buf;
return moduleObj;
}
PR_STATIC_CALLBACK(PLDHashOperator)
UnrootModulesCallback(const nsACString& key, JSObject* &module, void* arg)
{
JS_RemoveRootRT((JSRuntime*)arg, &module);
return PL_DHASH_REMOVE;
}
void
mozJSCodeLib::UnloadModules()
{
#ifdef DEBUG
printf("mozJSCodeLib::UnloadModules()\n");
#endif
JSRuntime *rt = nsnull;
mRuntimeService->GetRuntime(&rt);
if (!rt) {
NS_ERROR("null js runtime");
return;
}
mModules.Enumerate(UnrootModulesCallback, rt);
}

View File

@@ -0,0 +1,89 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ----- BEGIN LICENSE BLOCK -----
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Mozilla JSCodeLib.
*
* The Initial Developer of the Original Code is
* Alex Fritze.
* Portions created by the Initial Developer are Copyright (C) 2004
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Alex Fritze <alex@croczilla.com> (original author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ----- END LICENSE BLOCK ----- */
#ifndef __MOZ_JSCODELIB_H__
#define __MOZ_JSCODELIB_H__
#include "mozIJSCodeLib.h"
#include "nsDataHashtable.h"
#include "jsapi.h"
#include "nsCOMPtr.h"
#include "nsIXPConnect.h"
#include "nsIJSRuntimeService.h"
#ifndef XPCONNECT_STANDALONE
#include "nsIPrincipal.h"
#endif
////////////////////////////////////////////////////////////////////////
// mozJSCodeLib class
class mozJSCodeLib : public mozIJSCodeLib
{
public:
mozJSCodeLib();
~mozJSCodeLib();
nsresult Init();
NS_DECL_ISUPPORTS
NS_DECL_MOZIJSCODELIB
private:
PRInt32 LoadURL(char **buf, const nsACString &url);
JSObject *LoadModule(const nsACString &module);
void UnloadModules();
class ModulesHash : public nsDataHashtable<nsCStringHashKey,JSObject*>
{
public:
// need this function to be public:
EntryType *PutEntry(KeyType aKey) {
return nsDataHashtable<nsCStringHashKey,JSObject*>::PutEntry(aKey);
}
};
ModulesHash mModules;
nsCOMPtr<nsIXPConnect> mXPConnect;
nsCOMPtr<nsIJSRuntimeService> mRuntimeService;
#ifndef XPCONNECT_STANDALONE
nsCOMPtr<nsIPrincipal> mSystemPrincipal;
#endif
};
#endif // __MOZ_JSCODELIB_H__

View File

@@ -0,0 +1,160 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2004
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Alex Fritze <alex@croczilla.com> (original author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/**
* Utilities for JavaScript components loaded by the JS component
* loader.
*
* Import into a JS component using
* 'importModule("res:/jscodelib/ComponentUtils.js");'
*
*/
MOZ_EXPORTED_SYMBOLS = [ "ComponentUtils" ];
debug("*** loading JSComponentUtils\n");
var ComponentUtils = {
/**
* Generate a factory object (implementing nsIFactory) for the given
* constructor.
*
* By default, calls to the factory's createInstance method will
* call the method 'QueryInterface' on the newly created object. If
* an optional list of interfaces is provided, createInstance will
* not call QueryInterface on newly created objects, but instead
* check the requested iid against the interface list.
*
* @param ctor : Constructor function. A call 'new ctor()' must
* return an instance of the class served by this factory.
* @param interfaces : Optional list of interfaces. If this parameter is not
* given, objects created by 'ctor' must implement a
* QueryInterface method. If this parameter is given,
* objects do not need to implement QueryInterface; the
* interface list will be consulted instead.
*/
generateFactory: function(ctor, interfaces) {
return {
createInstance: function(outer, iid) {
if (outer) throw Components.results.NS_ERROR_NO_AGGREGATION;
if (!interfaces)
return (new ctor()).QueryInterface(iid);
for (var i=interfaces.length; i>=0; --i) {
if (iid.equals(interfaces[i])) break;
}
if (i<0 && !iid.equals(Components.interfaces.nsISupports))
throw Components.results.NS_ERROR_NO_INTERFACE;
return new ctor();
}
}
},
/**
* Generate a NSGetModule function implementation.
*
* @param classArray : Array of class descriptors.
* A class descriptor is an
* object with the following members:
* {
* className : "xxx",
* cid : Components.ID("xxx"),
* contractID : "@mozilla/xxx;1",
* factory : object_implementing_nsIFactory
* }
* @param postRegister : optional post-registration function with
* signature 'postRegister(nsIComponentManager,nsIFile,classArray)'
* @param preUnregister : optional pre-unregistration function with
* signature 'preUnregister(nsIComponentManager,nsIFile,classArray)'
*/
generateNSGetModule: function(classArray, postRegister, preUnregister) {
var module = {
getClassObject: function(compMgr, cid, iid) {
if (!iid.equals(Components.interfaces.nsIFactory))
throw Components.results.NS_ERROR_NO_INTERFACE;
for (var i=0; i<classArray.length; ++i) {
if(cid.equals(classArray[i].cid))
return classArray[i].factory;
}
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
},
registerSelf: function(compMgr, fileSpec, location, type) {
debug("*** registering "+fileSpec.leafName+": [ ");
compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
for (var i=0; i<classArray.length; ++i) {
debug(classArray[i].className+" ");
compMgr.registerFactoryLocation(classArray[i].cid,
classArray[i].className,
classArray[i].contractID,
fileSpec,
location,
type);
}
if (postRegister)
postRegister(compMgr, fileSpec, classArray);
debug("]\n");
},
unregisterSelf: function(compMgr, fileSpec, location) {
debug("*** unregistering "+fileSpec.leafName+": [ ");
if (preUnregister)
preUnregister(compMgr, fileSpec, classArray);
compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
for (var i=0; i<classArray.length; ++i) {
debug(classArray[i].className+" ");
compMgr.unregisterFactoryLocation(classArray[i].cid, fileSpec);
}
debug("]\n");
},
canUnload: function(compMgr) {
return true;
}
};
return function(compMgr, fileSpec) {
return module;
}
},
/**
* Convenience access to category manager
*/
get categoryManager() {
return Components.classes["@mozilla.org/categorymanager;1"]
.getService(Components.interfaces.nsICategoryManager);
}
};

View File

@@ -55,8 +55,21 @@ REQUIRES = xpcom \
CPPSRCS = mozJSComponentLoader.cpp mozJSSubScriptLoader.cpp
LOCAL_INCLUDES = \
-I$(srcdir)/../shared \
$(NULL)
include $(topsrcdir)/config/rules.mk
DEFINES += -DJSFILE -DJS_THREADSAFE
JS_CODELIB_RESOURCES = \
JSComponentUtils.js \
$(NULL)
libs:: $(JS_CODELIB_RESOURCES)
$(INSTALL) $^ $(DIST)/bin/jscodelib
install:: $(JS_CODELIB_RESOURCES)
$(INSTALL) $^ $(DESTDIR)$(mozappdir)/bin/jscodelib

View File

@@ -73,6 +73,8 @@
// For reporting errors with the console service
#include "nsIScriptError.h"
#include "nsIConsoleService.h"
#include "JSBackstagePass.h"
#include "JSAutoContext.h"
const char mozJSComponentLoaderContractID[] = "@mozilla.org/moz/jsloader;1";
const char jsComponentTypeName[] = "text/javascript";
@@ -84,7 +86,6 @@ const char JSxpcomKeyName[] = "software/mozilla/XPCOM/components";
const char kJSRuntimeServiceContractID[] = "@mozilla.org/js/xpc/RuntimeService;1";
const char kXPConnectServiceContractID[] = "@mozilla.org/js/xpc/XPConnect;1";
const char kJSContextStackContractID[] = "@mozilla.org/js/xpc/ContextStack;1";
const char kConsoleServiceContractID[] = "@mozilla.org/consoleservice;1";
const char kScriptErrorContractID[] = "@mozilla.org/scripterror;1";
const char kObserverServiceContractID[] = "@mozilla.org/observer-service;1";
@@ -151,45 +152,11 @@ Reporter(JSContext *cx, const char *message, JSErrorReport *rep)
#endif
}
JS_STATIC_DLL_CALLBACK(JSBool)
Dump(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
JSString *str;
if (!argc)
return JS_TRUE;
str = JS_ValueToString(cx, argv[0]);
if (!str)
return JS_FALSE;
char *bytes = JS_GetStringBytes(str);
bytes = nsCRT::strdup(bytes);
#ifdef XP_MAC
for (char *c = bytes; *c; c++)
if (*c == '\r')
*c = '\n';
#endif
fputs(bytes, stdout);
nsMemory::Free(bytes);
return JS_TRUE;
}
JS_STATIC_DLL_CALLBACK(JSBool)
Debug(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
#ifdef DEBUG
return Dump(cx, obj, argc, argv, rval);
#else
return JS_TRUE;
#endif
}
#ifndef XPCONNECT_STANDALONE
static JSFunctionSpec gSandboxFun[] = {
{"dump", Dump, 1 },
{"debug", Debug, 1 },
{"dump", JSDump, 1 },
{"debug", JSDebug, 1 },
{0}
};
@@ -317,85 +284,18 @@ EvalInSandbox(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
#endif /* XPCONNECT_STANDALONE */
static JSFunctionSpec gGlobalFun[] = {
{"dump", Dump, 1 },
{"debug", Debug, 1 },
{"dump", JSDump, 1 },
{"debug", JSDebug, 1 },
#ifndef XPCONNECT_STANDALONE
{"Sandbox", NewSandbox, 0 },
{"evalInSandbox", EvalInSandbox, 3 },
#endif
#ifdef MOZ_JSCODELIB
{"importModule", JSImportModule, 1 },
#endif
{0}
};
#ifndef XPCONNECT_STANDALONE
class BackstagePass : public nsIScriptObjectPrincipal, public nsIXPCScriptable
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCSCRIPTABLE
virtual nsIPrincipal* GetPrincipal() {
return mPrincipal;
}
BackstagePass(nsIPrincipal *prin) :
mPrincipal(prin)
{
}
virtual ~BackstagePass() { }
private:
nsCOMPtr<nsIPrincipal> mPrincipal;
};
NS_IMPL_THREADSAFE_ISUPPORTS2(BackstagePass, nsIScriptObjectPrincipal, nsIXPCScriptable)
#else
class BackstagePass : public nsIXPCScriptable
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCSCRIPTABLE
BackstagePass()
{
}
virtual ~BackstagePass() { }
};
NS_IMPL_THREADSAFE_ISUPPORTS1(BackstagePass, nsIXPCScriptable)
#endif
// The nsIXPCScriptable map declaration that will generate stubs for us...
#define XPC_MAP_CLASSNAME BackstagePass
#define XPC_MAP_QUOTED_CLASSNAME "BackstagePass"
#define XPC_MAP_WANT_NEWRESOLVE
#define XPC_MAP_FLAGS nsIXPCScriptable::USE_JSSTUB_FOR_ADDPROPERTY | \
nsIXPCScriptable::USE_JSSTUB_FOR_DELPROPERTY | \
nsIXPCScriptable::USE_JSSTUB_FOR_SETPROPERTY | \
nsIXPCScriptable::DONT_ENUM_STATIC_PROPS | \
nsIXPCScriptable::DONT_ENUM_QUERY_INTERFACE | \
nsIXPCScriptable::DONT_REFLECT_INTERFACE_NAMES
#include "xpc_map_end.h" /* This will #undef the above */
/* PRBool newResolve (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal id, in PRUint32 flags, out JSObjectPtr objp); */
NS_IMETHODIMP
BackstagePass::NewResolve(nsIXPConnectWrappedNative *wrapper,
JSContext * cx, JSObject * obj,
jsval id, PRUint32 flags,
JSObject * *objp, PRBool *_retval)
{
JSBool resolved;
*_retval = JS_ResolveStandardClass(cx, obj, id, &resolved);
if (*_retval && resolved)
*objp = obj;
return NS_OK;
}
mozJSComponentLoader::mozJSComponentLoader()
: mRuntime(nsnull),
mModules(nsnull),
@@ -916,7 +816,7 @@ mozJSComponentLoader::ModuleForLocation(const char *registryLocation,
if (!xpc)
return nsnull;
JSCLAutoContext cx(mRuntime);
JSAutoContext cx;
if(NS_FAILED(cx.GetError()))
return nsnull;
@@ -942,7 +842,7 @@ mozJSComponentLoader::ModuleForLocation(const char *registryLocation,
return nsnull;
}
JSCLAutoErrorReporterSetter aers(cx, Reporter);
JSAutoErrorReporterSetter aers(cx, Reporter);
jsval argv[2], retval;
argv[0] = OBJECT_TO_JSVAL(cm_jsobj);
@@ -1008,23 +908,23 @@ mozJSComponentLoader::GlobalForLocation(const char *aLocation,
nsresult rv;
JSPrincipals* jsPrincipals = nsnull;
JSCLAutoContext cx(mRuntime);
JSAutoContext cx;
if (NS_FAILED(cx.GetError()))
return nsnull;
#ifndef XPCONNECT_STANDALONE
nsCOMPtr<nsIScriptObjectPrincipal> backstagePass =
new BackstagePass(mSystemPrincipal);
new JSBackstagePass(mSystemPrincipal);
rv = mSystemPrincipal->GetJSPrincipals(cx, &jsPrincipals);
if (NS_FAILED(rv) || !jsPrincipals)
return nsnull;
#else
nsCOMPtr<nsISupports> backstagePass = new BackstagePass();
nsCOMPtr<nsISupports> backstagePass = new JSBackstagePass();
#endif
JSCLAutoErrorReporterSetter aers(cx, Reporter);
JSAutoErrorReporterSetter aers(cx, Reporter);
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
nsCOMPtr<nsIXPConnect> xpc = do_GetService(kXPConnectServiceContractID);
@@ -1190,53 +1090,4 @@ mozJSComponentLoader::UnloadAll(PRInt32 aWhen)
}
//----------------------------------------------------------------------
JSCLAutoContext::JSCLAutoContext(JSRuntime* rt)
: mContext(nsnull), mError(NS_OK), mPopNeeded(JS_FALSE), mContextThread(0)
{
nsCOMPtr<nsIThreadJSContextStack> cxstack =
do_GetService(kJSContextStackContractID, &mError);
if (NS_SUCCEEDED(mError)) {
mError = cxstack->GetSafeJSContext(&mContext);
if (NS_SUCCEEDED(mError) && mContext) {
mError = cxstack->Push(mContext);
if (NS_SUCCEEDED(mError)) {
mPopNeeded = JS_TRUE;
}
}
}
if (mContext) {
mContextThread = JS_GetContextThread(mContext);
if (mContextThread) {
JS_BeginRequest(mContext);
}
} else {
if (NS_SUCCEEDED(mError)) {
mError = NS_ERROR_FAILURE;
}
}
}
JSCLAutoContext::~JSCLAutoContext()
{
if (mContext && mContextThread) {
JS_ClearNewbornRoots(mContext);
JS_EndRequest(mContext);
}
if (mPopNeeded) {
nsCOMPtr<nsIThreadJSContextStack> cxstack =
do_GetService(kJSContextStackContractID);
if (cxstack) {
JSContext* cx;
nsresult rv = cxstack->Pop(&cx);
NS_ASSERTION(NS_SUCCEEDED(rv) && cx == mContext, "push/pop mismatch");
}
}
}
//----------------------------------------------------------------------
/* XXX this should all be data-driven, via NS_IMPL_GETMODULE_WITH_CATEGORIES */

View File

@@ -93,36 +93,3 @@ public:
PRBool mInitialized;
nsSupportsArray mDeferredComponents;
};
class JSCLAutoContext
{
public:
JSCLAutoContext(JSRuntime* rt);
~JSCLAutoContext();
operator JSContext*() const {return mContext;}
JSContext* GetContext() const {return mContext;}
nsresult GetError() const {return mError;}
JSCLAutoContext(); // not implemnted
private:
JSContext* mContext;
nsresult mError;
JSBool mPopNeeded;
intN mContextThread;
};
class JSCLAutoErrorReporterSetter
{
public:
JSCLAutoErrorReporterSetter(JSContext* cx, JSErrorReporter reporter)
{mContext = cx; mOldReporter = JS_SetErrorReporter(cx, reporter);}
~JSCLAutoErrorReporterSetter()
{JS_SetErrorReporter(mContext, mOldReporter);}
JSCLAutoErrorReporterSetter(); // not implemented
private:
JSContext* mContext;
JSErrorReporter mOldReporter;
};

View File

@@ -0,0 +1,95 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1999
* the Initial Developer. All Rights Reserved.
*
* Contributors:
* Mike Shaver <shaver@zeroknowledge.com>
* John Bandhauer <jband@netscape.com>
* IBM Corp.
* Robert Ginda <rginda@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "JSAutoContext.h"
#include "nsCOMPtr.h"
#include "nsIJSContextStack.h"
#include "nsIServiceManager.h"
const char kJSContextStackContractID[] = "@mozilla.org/js/xpc/ContextStack;1";
JSAutoContext::JSAutoContext()
: mContext(nsnull), mError(NS_OK), mPopNeeded(JS_FALSE), mContextThread(0)
{
nsCOMPtr<nsIThreadJSContextStack> cxstack =
do_GetService(kJSContextStackContractID, &mError);
if (NS_SUCCEEDED(mError)) {
mError = cxstack->GetSafeJSContext(&mContext);
if (NS_SUCCEEDED(mError) && mContext) {
mError = cxstack->Push(mContext);
if (NS_SUCCEEDED(mError)) {
mPopNeeded = JS_TRUE;
}
}
}
if (mContext) {
mContextThread = JS_GetContextThread(mContext);
if (mContextThread) {
JS_BeginRequest(mContext);
}
} else {
if (NS_SUCCEEDED(mError)) {
mError = NS_ERROR_FAILURE;
}
}
}
JSAutoContext::~JSAutoContext()
{
if (mContext && mContextThread) {
JS_ClearNewbornRoots(mContext);
JS_EndRequest(mContext);
}
if (mPopNeeded) {
nsCOMPtr<nsIThreadJSContextStack> cxstack =
do_GetService(kJSContextStackContractID);
if (cxstack) {
JSContext* cx;
nsresult rv = cxstack->Pop(&cx);
NS_ASSERTION(NS_SUCCEEDED(rv) && cx == mContext, "push/pop mismatch");
}
}
}

View File

@@ -0,0 +1,77 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1999
* the Initial Developer. All Rights Reserved.
*
* Contributors:
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef __JS_AUTO_CONTEXT_H__
#define __JS_AUTO_CONTEXT_H__
#include "jsapi.h"
#include "nscore.h"
class JSAutoContext
{
public:
JSAutoContext();
~JSAutoContext();
operator JSContext*() const {return mContext;}
JSContext* GetContext() const {return mContext;}
nsresult GetError() const {return mError;}
private:
JSContext* mContext;
nsresult mError;
JSBool mPopNeeded;
intN mContextThread;
};
class JSAutoErrorReporterSetter
{
public:
JSAutoErrorReporterSetter(JSContext* cx, JSErrorReporter reporter)
{mContext = cx; mOldReporter = JS_SetErrorReporter(cx, reporter);}
~JSAutoErrorReporterSetter()
{JS_SetErrorReporter(mContext, mOldReporter);}
JSAutoErrorReporterSetter(); // not implemented
private:
JSContext* mContext;
JSErrorReporter mOldReporter;
};
#endif // __JS_AUTO_CONTEXT_H__

View File

@@ -0,0 +1,157 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1999
* the Initial Developer. All Rights Reserved.
*
* Contributors:
* Mike Shaver <shaver@zeroknowledge.com>
* John Bandhauer <jband@netscape.com>
* IBM Corp.
* Robert Ginda <rginda@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "JSBackstagePass.h"
#include "jsapi.h"
#include "nsMemory.h"
#include "nsCRT.h"
#ifdef MOZ_JSCODELIB
#include "mozIJSCodeLib.h"
#include "nsIServiceManager.h"
#include "nsString.h"
#endif
////////////////////////////////////////////////////////////////////////
// JSBackstagePass implementation
#ifndef XPCONNECT_STANDALONE
NS_IMPL_THREADSAFE_ISUPPORTS2(JSBackstagePass, nsIScriptObjectPrincipal, nsIXPCScriptable)
#else
NS_IMPL_THREADSAFE_ISUPPORTS1(JSBackstagePass, nsIXPCScriptable)
#endif
// The nsIXPCScriptable map declaration that will generate stubs for us...
#define XPC_MAP_CLASSNAME JSBackstagePass
#define XPC_MAP_QUOTED_CLASSNAME "JSBackstagePass"
#define XPC_MAP_WANT_NEWRESOLVE
#define XPC_MAP_FLAGS nsIXPCScriptable::USE_JSSTUB_FOR_ADDPROPERTY | \
nsIXPCScriptable::USE_JSSTUB_FOR_DELPROPERTY | \
nsIXPCScriptable::USE_JSSTUB_FOR_SETPROPERTY | \
nsIXPCScriptable::DONT_ENUM_STATIC_PROPS | \
nsIXPCScriptable::DONT_ENUM_QUERY_INTERFACE | \
nsIXPCScriptable::DONT_REFLECT_INTERFACE_NAMES
#include "xpc_map_end.h" /* This will #undef the above */
/* PRBool newResolve (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal id, in PRUint32 flags, out JSObjectPtr objp); */
NS_IMETHODIMP
JSBackstagePass::NewResolve(nsIXPConnectWrappedNative *wrapper,
JSContext * cx, JSObject * obj,
jsval id, PRUint32 flags,
JSObject * *objp, PRBool *_retval)
{
JSBool resolved;
*_retval = JS_ResolveStandardClass(cx, obj, id, &resolved);
if (*_retval && resolved)
*objp = obj;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////
// Standard JS functions
JSBool JS_DLL_CALLBACK
JSDump(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
JSString *str;
if (!argc)
return JS_TRUE;
str = JS_ValueToString(cx, argv[0]);
if (!str)
return JS_FALSE;
char *bytes = JS_GetStringBytes(str);
bytes = nsCRT::strdup(bytes);
#ifdef XP_MAC
for (char *c = bytes; *c; c++)
if (*c == '\r')
*c = '\n';
#endif
fputs(bytes, stderr);
nsMemory::Free(bytes);
return JS_TRUE;
}
JSBool JS_DLL_CALLBACK
JSDebug(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
#ifdef DEBUG
return JSDump(cx, obj, argc, argv, rval);
#else
return JS_TRUE;
#endif
}
#ifdef MOZ_JSCODELIB
JSBool JS_DLL_CALLBACK
JSImportModule(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
char *url;
JSObject *targetObj = nsnull;
if (!JS_ConvertArguments(cx, argc, argv, "s/o", &url, &targetObj)) {
return JS_FALSE;
}
if (!targetObj) {
// Our targetObject is the caller's global object. Find it by
// walking the calling object's parent chain.
targetObj = obj;
JSObject *parent = nsnull;
while ((parent = JS_GetParent(cx, targetObj)))
targetObj = parent;
}
nsCOMPtr<mozIJSCodeLib> codelib = do_GetService(MOZ_JSCODELIB_CONTRACTID);
if (!codelib)
return JS_FALSE;
return NS_SUCCEEDED(codelib->ImportModuleToJSObject(nsDependentCString(url), targetObj));
}
#endif // MOZ_JSCODELIB

View File

@@ -0,0 +1,111 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1999
* the Initial Developer. All Rights Reserved.
*
* Contributors:
* Mike Shaver <shaver@zeroknowledge.com>
* John Bandhauer <jband@netscape.com>
* IBM Corp.
* Robert Ginda <rginda@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef __JS_BACKSTAGE_PASS_H__
#define __JS_BACKSTAGE_PASS_H__
#include "nsIXPCScriptable.h"
#ifndef XPCONNECT_STANDALONE
#include "nsCOMPtr.h"
#include "nsIScriptObjectPrincipal.h"
#include "nsIPrincipal.h"
////////////////////////////////////////////////////////////////////////
// JSBackstagePass
class JSBackstagePass : public nsIScriptObjectPrincipal, public nsIXPCScriptable
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCSCRIPTABLE
virtual nsIPrincipal* GetPrincipal() {
return mPrincipal;
}
JSBackstagePass(nsIPrincipal *prin) :
mPrincipal(prin)
{
}
virtual ~JSBackstagePass() { }
private:
nsCOMPtr<nsIPrincipal> mPrincipal;
};
#else
class JSBackstagePass : public nsIXPCScriptable
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCSCRIPTABLE
JSBackstagePass()
{
}
virtual ~JSBackstagePass() { }
};
#endif
////////////////////////////////////////////////////////////////////////
// Standard JS functions
JSBool JS_DLL_CALLBACK
JSDump(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
JSBool JS_DLL_CALLBACK
JSDebug(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
#ifdef MOZ_JSCODELIB
JSBool JS_DLL_CALLBACK
JSImportModule(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
#endif // MOZ_JSCODELIB
#endif // __JS_BACKSTAGE_PASS_H__

View File

@@ -0,0 +1,62 @@
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Mozilla code.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 1999
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either of the GNU General Public License Version 2 or later (the "GPL"),
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
DEPTH = ../../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = xpconnect
LIBRARY_NAME = xpcshared_s
FORCE_STATIC_LIB = 1
REQUIRES = xpcom \
string \
xpconnect \
js \
caps \
dom \
necko \
$(NULL)
CPPSRCS = JSBackstagePass.cpp JSAutoContext.cpp
include $(topsrcdir)/config/rules.mk
DEFINES += -DJSFILE -DJS_THREADSAFE

View File

@@ -121,6 +121,23 @@ ifdef MOZ_JSLOADER
SHARED_LIBRARY_LIBS = \
$(DIST)/lib/$(LIB_PREFIX)jsloader_s.$(LIB_SUFFIX) \
$(NULL)
NEEDS_XPCONNECT_SHARED = 1
endif
ifdef MOZ_JSCODELIB
SHARED_LIBRARY_LIBS += \
$(DIST)/lib/$(LIB_PREFIX)jscodelib_s.$(LIB_SUFFIX) \
$(NULL)
LOCAL_INCLUDES += \
-I$(srcdir)/../codelib \
$(NULL)
NEEDS_XPCONNECT_SHARED = 1
endif
ifdef NEEDS_XPCONNECT_SHARED
SHARED_LIBRARY_LIBS += \
$(DIST)/lib/$(LIB_PREFIX)xpcshared_s.$(LIB_SUFFIX) \
$(NULL)
endif
include $(topsrcdir)/config/rules.mk

View File

@@ -44,6 +44,10 @@
#ifdef MOZ_JSLOADER
#include "mozJSLoaderConstructors.h"
#endif
#ifdef MOZ_JSCODELIB
#include "mozJSCodeLib.h"
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(mozJSCodeLib, Init)
#endif
/* Module implementation for the xpconnect library. */
@@ -103,6 +107,10 @@ static const nsModuleComponentInfo components[] = {
,{ nsnull, NS_IDISPATCH_SUPPORT_CID, NS_IDISPATCH_SUPPORT_CONTRACTID,
nsIDispatchSupportConstructor }
#endif
#ifdef MOZ_JSCODELIB
,{ "JS code library", MOZ_JSCODELIB_CID,
MOZ_JSCODELIB_CONTRACTID, mozJSCodeLibConstructor }
#endif
};
PR_STATIC_CALLBACK(void)