diff --git a/mozilla/allmakefiles.sh b/mozilla/allmakefiles.sh index 11fcea100ea..874b7d8c4a4 100755 --- a/mozilla/allmakefiles.sh +++ b/mozilla/allmakefiles.sh @@ -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=" diff --git a/mozilla/client.mk b/mozilla/client.mk index 1827d6c2f02..3524005ba8c 100644 --- a/mozilla/client.mk +++ b/mozilla/client.mk @@ -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 diff --git a/mozilla/config/autoconf.mk.in b/mozilla/config/autoconf.mk.in index 0cbf3f0c43c..9b64d93d100 100644 --- a/mozilla/config/autoconf.mk.in +++ b/mozilla/config/autoconf.mk.in @@ -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@ diff --git a/mozilla/configure.in b/mozilla/configure.in index 6102fa635e7..b166ab40a62 100644 --- a/mozilla/configure.in +++ b/mozilla/configure.in @@ -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) diff --git a/mozilla/js/src/xpconnect/Makefile.in b/mozilla/js/src/xpconnect/Makefile.in index b1f01d060a1..b5026cae7cd 100644 --- a/mozilla/js/src/xpconnect/Makefile.in +++ b/mozilla/js/src/xpconnect/Makefile.in @@ -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 diff --git a/mozilla/js/src/xpconnect/codelib/Makefile.in b/mozilla/js/src/xpconnect/codelib/Makefile.in new file mode 100644 index 00000000000..db18ba1e9f0 --- /dev/null +++ b/mozilla/js/src/xpconnect/codelib/Makefile.in @@ -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 + + diff --git a/mozilla/js/src/xpconnect/codelib/mozIJSCodeLib.idl b/mozilla/js/src/xpconnect/codelib/mozIJSCodeLib.idl new file mode 100644 index 00000000000..3345b1ff2b8 --- /dev/null +++ b/mozilla/js/src/xpconnect/codelib/mozIJSCodeLib.idl @@ -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 (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); +}; + diff --git a/mozilla/js/src/xpconnect/codelib/mozJSCodeLib.cpp b/mozilla/js/src/xpconnect/codelib/mozJSCodeLib.cpp new file mode 100644 index 00000000000..06af505895e --- /dev/null +++ b/mozilla/js/src/xpconnect/codelib/mozJSCodeLib.cpp @@ -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 (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 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 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 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 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 ioserv; + nsCOMPtr channel; + nsCOMPtr 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 backstagePass = + new JSBackstagePass(mSystemPrincipal); +#else + nsCOMPtr backstagePass = new JSBackstagePass(); +#endif + + nsCOMPtr 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); +} diff --git a/mozilla/js/src/xpconnect/codelib/mozJSCodeLib.h b/mozilla/js/src/xpconnect/codelib/mozJSCodeLib.h new file mode 100644 index 00000000000..7f7eba6e7ad --- /dev/null +++ b/mozilla/js/src/xpconnect/codelib/mozJSCodeLib.h @@ -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 (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 + { + public: + // need this function to be public: + EntryType *PutEntry(KeyType aKey) { + return nsDataHashtable::PutEntry(aKey); + } + }; + ModulesHash mModules; + + nsCOMPtr mXPConnect; + nsCOMPtr mRuntimeService; +#ifndef XPCONNECT_STANDALONE + nsCOMPtr mSystemPrincipal; +#endif + +}; + +#endif // __MOZ_JSCODELIB_H__ diff --git a/mozilla/js/src/xpconnect/loader/JSComponentUtils.js b/mozilla/js/src/xpconnect/loader/JSComponentUtils.js new file mode 100644 index 00000000000..0693ff34c7e --- /dev/null +++ b/mozilla/js/src/xpconnect/loader/JSComponentUtils.js @@ -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 (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 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 backstagePass = - new BackstagePass(mSystemPrincipal); + new JSBackstagePass(mSystemPrincipal); rv = mSystemPrincipal->GetJSPrincipals(cx, &jsPrincipals); if (NS_FAILED(rv) || !jsPrincipals) return nsnull; #else - nsCOMPtr backstagePass = new BackstagePass(); + nsCOMPtr backstagePass = new JSBackstagePass(); #endif - JSCLAutoErrorReporterSetter aers(cx, Reporter); + JSAutoErrorReporterSetter aers(cx, Reporter); nsCOMPtr holder; nsCOMPtr 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 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 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 */ diff --git a/mozilla/js/src/xpconnect/loader/mozJSComponentLoader.h b/mozilla/js/src/xpconnect/loader/mozJSComponentLoader.h index 4e486a26a3d..00e61d40f6f 100644 --- a/mozilla/js/src/xpconnect/loader/mozJSComponentLoader.h +++ b/mozilla/js/src/xpconnect/loader/mozJSComponentLoader.h @@ -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; -}; diff --git a/mozilla/js/src/xpconnect/shared/JSAutoContext.cpp b/mozilla/js/src/xpconnect/shared/JSAutoContext.cpp new file mode 100644 index 00000000000..2260f5e4e99 --- /dev/null +++ b/mozilla/js/src/xpconnect/shared/JSAutoContext.cpp @@ -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 + * John Bandhauer + * IBM Corp. + * Robert Ginda + * + * 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 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 cxstack = + do_GetService(kJSContextStackContractID); + if (cxstack) { + JSContext* cx; + nsresult rv = cxstack->Pop(&cx); + NS_ASSERTION(NS_SUCCEEDED(rv) && cx == mContext, "push/pop mismatch"); + } + } +} diff --git a/mozilla/js/src/xpconnect/shared/JSAutoContext.h b/mozilla/js/src/xpconnect/shared/JSAutoContext.h new file mode 100644 index 00000000000..26bbb162932 --- /dev/null +++ b/mozilla/js/src/xpconnect/shared/JSAutoContext.h @@ -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__ diff --git a/mozilla/js/src/xpconnect/shared/JSBackstagePass.cpp b/mozilla/js/src/xpconnect/shared/JSBackstagePass.cpp new file mode 100644 index 00000000000..20f3b9c2091 --- /dev/null +++ b/mozilla/js/src/xpconnect/shared/JSBackstagePass.cpp @@ -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 + * John Bandhauer + * IBM Corp. + * Robert Ginda + * + * 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 codelib = do_GetService(MOZ_JSCODELIB_CONTRACTID); + if (!codelib) + return JS_FALSE; + + return NS_SUCCEEDED(codelib->ImportModuleToJSObject(nsDependentCString(url), targetObj)); +} +#endif // MOZ_JSCODELIB diff --git a/mozilla/js/src/xpconnect/shared/JSBackstagePass.h b/mozilla/js/src/xpconnect/shared/JSBackstagePass.h new file mode 100644 index 00000000000..b92ac1769d4 --- /dev/null +++ b/mozilla/js/src/xpconnect/shared/JSBackstagePass.h @@ -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 + * John Bandhauer + * IBM Corp. + * Robert Ginda + * + * 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 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__ diff --git a/mozilla/js/src/xpconnect/shared/Makefile.in b/mozilla/js/src/xpconnect/shared/Makefile.in new file mode 100644 index 00000000000..94b3c9bf8a0 --- /dev/null +++ b/mozilla/js/src/xpconnect/shared/Makefile.in @@ -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 + + diff --git a/mozilla/js/src/xpconnect/src/Makefile.in b/mozilla/js/src/xpconnect/src/Makefile.in index 29c0fd95a4c..95aa37075a0 100644 --- a/mozilla/js/src/xpconnect/src/Makefile.in +++ b/mozilla/js/src/xpconnect/src/Makefile.in @@ -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 diff --git a/mozilla/js/src/xpconnect/src/xpcmodule.cpp b/mozilla/js/src/xpconnect/src/xpcmodule.cpp index 8669c0871ba..f0ed7ab9cf9 100644 --- a/mozilla/js/src/xpconnect/src/xpcmodule.cpp +++ b/mozilla/js/src/xpconnect/src/xpcmodule.cpp @@ -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)