2001-09-20 04:02:59 +04:00
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2005-09-02 03:17:27 +04:00
* vim : set ts = 2 sw = 4 et tw = 80 :
1999-04-10 10:51:01 +04:00
*
2012-05-21 15:12:37 +04:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http : //mozilla.org/MPL/2.0/. */
1999-04-10 10:51:01 +04:00
/* XPConnect JavaScript interactive shell. */
# include <stdio.h>
2011-10-11 10:00:28 +04:00
# include "mozilla/Util.h"
2010-03-04 08:02:55 +03:00
# include "jsapi.h"
# include "jsdbgapi.h"
2011-05-31 00:28:01 +04:00
# include "jsfriendapi.h"
2010-03-04 08:02:55 +03:00
# include "jsprf.h"
2009-01-20 22:56:44 +03:00
# include "nsXULAppAPI.h"
2005-12-12 18:08:16 +03:00
# include "nsServiceManagerUtils.h"
# include "nsComponentManagerUtils.h"
2009-01-20 22:56:44 +03:00
# include "nsStringAPI.h"
1999-04-10 10:51:01 +04:00
# include "nsIXPConnect.h"
# include "nsIXPCScriptable.h"
# include "nsIInterfaceInfo.h"
# include "nsIInterfaceInfoManager.h"
# include "nsIXPCScriptable.h"
# include "nsIServiceManager.h"
# include "nsIComponentManager.h"
2002-01-30 00:22:13 +03:00
# include "nsIComponentRegistrar.h"
2012-06-06 06:08:30 +04:00
# include "nsIFile.h"
2009-01-18 20:01:15 +03:00
# include "nsStringAPI.h"
2009-01-20 22:56:44 +03:00
# include "nsIDirectoryService.h"
# include "nsDirectoryServiceDefs.h"
2009-07-17 18:41:26 +04:00
# include "nsAppDirectoryServiceDefs.h"
1999-04-10 10:51:01 +04:00
# include "nscore.h"
2009-07-17 18:41:26 +04:00
# include "nsArrayEnumerator.h"
# include "nsCOMArray.h"
# include "nsDirectoryServiceUtils.h"
2000-06-03 13:46:12 +04:00
# include "nsMemory.h"
2009-01-20 22:56:44 +03:00
# include "nsISupportsImpl.h"
1999-10-15 07:05:48 +04:00
# include "nsIJSRuntimeService.h"
2000-02-19 03:14:44 +03:00
# include "nsCOMPtr.h"
2007-06-21 04:10:48 +04:00
# include "nsAutoPtr.h"
2000-02-19 03:14:44 +03:00
# include "nsIXPCSecurityManager.h"
2012-03-09 13:48:50 +04:00
# include "nsJSPrincipals.h"
2010-12-06 23:45:00 +03:00
# include "xpcpublic.h"
2012-05-22 18:50:04 +04:00
# include "nsXULAppAPI.h"
2008-06-03 14:56:09 +04:00
# ifdef XP_MACOSX
# include "xpcshellMacUtils.h"
# endif
2009-01-18 20:01:15 +03:00
# ifdef XP_WIN
# include <windows.h>
2012-05-22 18:50:04 +04:00
# include <shlobj.h>
2009-01-18 20:01:15 +03:00
# endif
1999-04-10 10:51:01 +04:00
2011-11-10 04:27:08 +04:00
# ifdef ANDROID
# include <android/log.h>
# endif
2004-11-23 08:38:59 +03:00
# include "nsIScriptSecurityManager.h"
# include "nsIPrincipal.h"
1999-04-10 10:51:01 +04:00
// all this crap is needed to do the interactive shell stuff
# include <stdlib.h>
# include <errno.h>
2009-01-17 01:10:48 +03:00
# ifdef HAVE_IO_H
1999-04-10 10:51:01 +04:00
# include <io.h> /* for isatty() */
2009-01-17 01:10:48 +03:00
# endif
# ifdef HAVE_UNISTD_H
1999-04-13 22:10:24 +04:00
# include <unistd.h> /* for isatty() */
1999-04-10 10:51:01 +04:00
# endif
1999-07-15 09:42:53 +04:00
# include "nsIJSContextStack.h"
1999-04-10 10:51:01 +04:00
2009-08-20 19:35:30 +04:00
# ifdef MOZ_CRASHREPORTER
# include "nsICrashReporter.h"
# endif
2011-10-11 10:00:28 +04:00
using namespace mozilla ;
2009-07-17 18:41:26 +04:00
class XPCShellDirProvider : public nsIDirectoryServiceProvider2
2009-01-20 22:56:44 +03:00
{
public :
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIDIRECTORYSERVICEPROVIDER
2009-07-17 18:41:26 +04:00
NS_DECL_NSIDIRECTORYSERVICEPROVIDER2
2009-01-20 22:56:44 +03:00
XPCShellDirProvider ( ) { }
~ XPCShellDirProvider ( ) { }
2011-09-29 10:19:26 +04:00
bool SetGREDir ( const char * dir ) ;
2012-07-30 18:20:58 +04:00
void ClearGREDir ( ) { mGREDir = nullptr ; }
2012-06-06 06:08:30 +04:00
void SetAppFile ( nsIFile * appFile ) ;
2012-07-30 18:20:58 +04:00
void ClearAppFile ( ) { mAppFile = nullptr ; }
2009-01-20 22:56:44 +03:00
private :
2012-06-06 06:08:30 +04:00
nsCOMPtr < nsIFile > mGREDir ;
nsCOMPtr < nsIFile > mAppFile ;
2009-01-20 22:56:44 +03:00
} ;
1999-04-10 10:51:01 +04:00
/***************************************************************************/
2000-11-30 09:58:37 +03:00
# ifdef JS_THREADSAFE
# define DoBeginRequest(cx) JS_BeginRequest((cx))
# define DoEndRequest(cx) JS_EndRequest((cx))
# else
# define DoBeginRequest(cx) ((void)0)
# define DoEndRequest(cx) ((void)0)
# endif
/***************************************************************************/
2009-01-18 20:01:15 +03:00
static const char kXPConnectServiceContractID [ ] = " @mozilla.org/js/xpc/XPConnect;1 " ;
1999-11-23 02:28:58 +03:00
# define EXITCODE_RUNTIME_ERROR 3
# define EXITCODE_FILE_NOT_FOUND 4
1999-04-10 10:51:01 +04:00
FILE * gOutFile = NULL ;
FILE * gErrFile = NULL ;
2008-11-28 05:25:35 +03:00
FILE * gInFile = NULL ;
1999-04-10 10:51:01 +04:00
1999-11-23 02:28:58 +03:00
int gExitCode = 0 ;
2011-11-26 14:05:59 +04:00
JSBool gQuitting = false ;
static JSBool reportWarnings = true ;
static JSBool compileOnly = false ;
1999-10-09 10:01:57 +04:00
2012-07-30 18:20:58 +04:00
JSPrincipals * gJSPrincipals = nullptr ;
nsAutoString * gWorkingDirectory = nullptr ;
2009-01-18 20:01:15 +03:00
static JSBool
2012-07-30 15:19:09 +04:00
GetLocationProperty ( JSContext * cx , JSHandleObject obj , JSHandleId id , JSMutableHandleValue vp )
2009-01-18 20:01:15 +03:00
{
2011-04-09 23:17:53 +04:00
# if !defined(XP_WIN) && !defined(XP_UNIX)
2009-01-18 20:01:15 +03:00
//XXX: your platform should really implement this
2011-11-26 14:05:59 +04:00
return false ;
2009-01-30 12:19:43 +03:00
# else
2012-03-07 03:33:12 +04:00
JSScript * script ;
JS_DescribeScriptedCaller ( cx , & script , NULL ) ;
2009-01-18 20:01:15 +03:00
const char * filename = JS_GetScriptFilename ( cx , script ) ;
if ( filename ) {
nsresult rv ;
nsCOMPtr < nsIXPConnect > xpc =
do_GetService ( kXPConnectServiceContractID , & rv ) ;
# if defined(XP_WIN)
// convert from the system codepage to UTF-16
int bufferSize = MultiByteToWideChar ( CP_ACP , 0 , filename ,
- 1 , NULL , 0 ) ;
nsAutoString filenameString ;
filenameString . SetLength ( bufferSize ) ;
MultiByteToWideChar ( CP_ACP , 0 , filename ,
- 1 , ( LPWSTR ) filenameString . BeginWriting ( ) ,
filenameString . Length ( ) ) ;
// remove the null terminator
filenameString . SetLength ( bufferSize - 1 ) ;
// replace forward slashes with backslashes,
// since nsLocalFileWin chokes on them
PRUnichar * start , * end ;
filenameString . BeginWriting ( & start , & end ) ;
2009-06-29 03:01:42 +04:00
2009-01-18 20:01:15 +03:00
while ( start ! = end ) {
if ( * start = = L ' / ' )
* start = L ' \\ ' ;
start + + ;
}
# elif defined(XP_UNIX)
NS_ConvertUTF8toUTF16 filenameString ( filename ) ;
# endif
2012-06-06 06:08:30 +04:00
nsCOMPtr < nsIFile > location ;
2009-01-18 20:01:15 +03:00
if ( NS_SUCCEEDED ( rv ) ) {
rv = NS_NewLocalFile ( filenameString ,
2011-10-17 18:59:28 +04:00
false , getter_AddRefs ( location ) ) ;
2009-01-18 20:01:15 +03:00
}
if ( ! location & & gWorkingDirectory ) {
// could be a relative path, try appending it to the cwd
// and then normalize
nsAutoString absolutePath ( * gWorkingDirectory ) ;
absolutePath . Append ( filenameString ) ;
rv = NS_NewLocalFile ( absolutePath ,
2011-10-17 18:59:28 +04:00
false , getter_AddRefs ( location ) ) ;
2009-01-18 20:01:15 +03:00
}
if ( location ) {
nsCOMPtr < nsIXPConnectJSObjectHolder > locationHolder ;
JSObject * locationObj = NULL ;
2011-09-29 10:19:26 +04:00
bool symlink ;
2009-02-02 16:53:23 +03:00
// don't normalize symlinks, because that's kind of confusing
if ( NS_SUCCEEDED ( location - > IsSymlink ( & symlink ) ) & &
! symlink )
location - > Normalize ( ) ;
2009-01-18 20:01:15 +03:00
rv = xpc - > WrapNative ( cx , obj , location ,
2012-06-06 06:08:30 +04:00
NS_GET_IID ( nsIFile ) ,
2009-01-18 20:01:15 +03:00
getter_AddRefs ( locationHolder ) ) ;
if ( NS_SUCCEEDED ( rv ) & &
NS_SUCCEEDED ( locationHolder - > GetJSObject ( & locationObj ) ) ) {
2012-07-30 15:19:09 +04:00
vp . set ( OBJECT_TO_JSVAL ( locationObj ) ) ;
2009-01-18 20:01:15 +03:00
}
}
}
2011-11-26 14:05:59 +04:00
return true ;
2009-01-30 12:19:43 +03:00
# endif
2009-01-18 20:01:15 +03:00
}
2004-11-23 08:38:59 +03:00
2009-06-30 03:10:54 +04:00
# ifdef EDITLINE
extern " C " {
2011-12-13 23:26:58 +04:00
extern JS_EXPORT_API ( char * ) readline ( const char * prompt ) ;
2010-05-13 11:06:13 +04:00
extern JS_EXPORT_API ( void ) add_history ( char * line ) ;
2009-06-30 03:10:54 +04:00
}
# endif
2008-11-28 05:25:35 +03:00
static JSBool
GetLine ( JSContext * cx , char * bufp , FILE * file , const char * prompt ) {
# ifdef EDITLINE
/*
* Use readline only if file is stdin , because there ' s no way to specify
* another handle . Are other filehandles interactive ?
*/
if ( file = = stdin ) {
char * linep = readline ( prompt ) ;
if ( ! linep )
2011-11-26 14:05:59 +04:00
return false ;
2008-11-28 05:25:35 +03:00
if ( * linep )
add_history ( linep ) ;
strcpy ( bufp , linep ) ;
JS_free ( cx , linep ) ;
bufp + = strlen ( bufp ) ;
* bufp + + = ' \n ' ;
* bufp = ' \0 ' ;
} else
# endif
{
2010-07-16 05:13:00 +04:00
char line [ 256 ] = { ' \0 ' } ;
2009-10-19 19:03:00 +04:00
fputs ( prompt , gOutFile ) ;
2008-11-28 05:25:35 +03:00
fflush ( gOutFile ) ;
2011-09-29 04:57:27 +04:00
if ( ( ! fgets ( line , sizeof line , file ) & & errno ! = EINTR ) | | feof ( file ) )
2011-11-26 14:05:59 +04:00
return false ;
2008-11-28 05:25:35 +03:00
strcpy ( bufp , line ) ;
}
2011-11-26 14:05:59 +04:00
return true ;
2008-11-28 05:25:35 +03:00
}
2008-09-07 02:21:43 +04:00
static void
1999-07-31 04:19:50 +04:00
my_ErrorReporter ( JSContext * cx , const char * message , JSErrorReport * report )
{
int i , j , k , n ;
char * prefix = NULL , * tmp ;
1999-10-19 03:45:39 +04:00
const char * ctmp ;
2008-10-12 22:32:34 +04:00
nsCOMPtr < nsIXPConnect > xpc ;
// Don't report an exception from inner JS frames as the callers may intend
// to handle it.
2012-07-30 18:20:58 +04:00
if ( JS_DescribeScriptedCaller ( cx , nullptr , nullptr ) ) {
2012-04-16 23:25:28 +04:00
return ;
2008-10-12 22:32:34 +04:00
}
// In some cases cx->fp is null here so use XPConnect to tell us about inner
// frames.
if ( ( xpc = do_GetService ( nsIXPConnect : : GetCID ( ) ) ) ) {
2012-07-30 18:20:58 +04:00
nsAXPCNativeCallContext * cc = nullptr ;
2008-10-12 22:32:34 +04:00
xpc - > GetCurrentNativeCallContext ( & cc ) ;
if ( cc ) {
nsAXPCNativeCallContext * prev = cc ;
while ( NS_SUCCEEDED ( prev - > GetPreviousCallContext ( & prev ) ) & & prev ) {
2012-08-22 19:56:38 +04:00
uint16_t lang ;
2008-10-12 22:32:34 +04:00
if ( NS_SUCCEEDED ( prev - > GetLanguage ( & lang ) ) & &
lang = = nsAXPCNativeCallContext : : LANG_JS ) {
return ;
}
}
}
}
1999-07-31 04:19:50 +04:00
if ( ! report ) {
fprintf ( gErrFile , " %s \n " , message ) ;
return ;
}
2000-08-19 07:28:01 +04:00
/* Conditionally ignore reported warnings. */
if ( JSREPORT_IS_WARNING ( report - > flags ) & & ! reportWarnings )
return ;
1999-07-31 04:19:50 +04:00
if ( report - > filename )
prefix = JS_smprintf ( " %s: " , report - > filename ) ;
if ( report - > lineno ) {
tmp = prefix ;
prefix = JS_smprintf ( " %s%u: " , tmp ? tmp : " " , report - > lineno ) ;
JS_free ( cx , tmp ) ;
}
if ( JSREPORT_IS_WARNING ( report - > flags ) ) {
tmp = prefix ;
2000-08-19 07:28:01 +04:00
prefix = JS_smprintf ( " %s%swarning: " ,
tmp ? tmp : " " ,
JSREPORT_IS_STRICT ( report - > flags ) ? " strict " : " " ) ;
1999-07-31 04:19:50 +04:00
JS_free ( cx , tmp ) ;
}
/* embedded newlines -- argh! */
1999-10-19 03:45:39 +04:00
while ( ( ctmp = strchr ( message , ' \n ' ) ) ! = 0 ) {
ctmp + + ;
1999-07-31 04:19:50 +04:00
if ( prefix ) fputs ( prefix , gErrFile ) ;
1999-10-19 03:45:39 +04:00
fwrite ( message , 1 , ctmp - message , gErrFile ) ;
message = ctmp ;
1999-07-31 04:19:50 +04:00
}
/* If there were no filename or lineno, the prefix might be empty */
if ( prefix )
fputs ( prefix , gErrFile ) ;
fputs ( message , gErrFile ) ;
if ( ! report - > linebuf ) {
fputc ( ' \n ' , gErrFile ) ;
goto out ;
}
fprintf ( gErrFile , " : \n %s%s \n %s " , prefix , report - > linebuf , prefix ) ;
n = report - > tokenptr - report - > linebuf ;
for ( i = j = 0 ; i < n ; i + + ) {
if ( report - > linebuf [ i ] = = ' \t ' ) {
for ( k = ( j + 8 ) & ~ 7 ; j < k ; j + + ) {
fputc ( ' . ' , gErrFile ) ;
}
continue ;
}
fputc ( ' . ' , gErrFile ) ;
j + + ;
}
fputs ( " ^ \n " , gErrFile ) ;
out :
2000-10-31 01:42:48 +03:00
if ( ! JSREPORT_IS_WARNING ( report - > flags ) )
2000-10-27 03:45:43 +04:00
gExitCode = EXITCODE_RUNTIME_ERROR ;
1999-07-31 04:19:50 +04:00
JS_free ( cx , prefix ) ;
}
1999-04-10 10:51:01 +04:00
2008-11-28 05:25:35 +03:00
static JSBool
2012-02-29 03:11:11 +04:00
ReadLine ( JSContext * cx , unsigned argc , jsval * vp )
2008-11-28 05:25:35 +03:00
{
// While 4096 might be quite arbitrary, this is something to be fixed in
// bug 105707. It is also the same limit as in ProcessFile.
char buf [ 4096 ] ;
JSString * str ;
/* If a prompt was specified, construct the string */
if ( argc > 0 ) {
2010-08-16 23:35:04 +04:00
str = JS_ValueToString ( cx , JS_ARGV ( cx , vp ) [ 0 ] ) ;
2008-11-28 05:25:35 +03:00
if ( ! str )
2011-11-26 14:05:59 +04:00
return false ;
2008-11-28 05:25:35 +03:00
} else {
str = JSVAL_TO_STRING ( JS_GetEmptyStringValue ( cx ) ) ;
}
/* Get a line from the infile */
2010-11-11 23:40:29 +03:00
JSAutoByteString strBytes ( cx , str ) ;
if ( ! strBytes | | ! GetLine ( cx , buf , gInFile , strBytes . ptr ( ) ) )
2011-11-26 14:05:59 +04:00
return false ;
2008-11-28 05:25:35 +03:00
/* Strip newline character added by GetLine() */
unsigned int buflen = strlen ( buf ) ;
if ( buflen = = 0 ) {
if ( feof ( gInFile ) ) {
2010-08-16 23:35:04 +04:00
JS_SET_RVAL ( cx , vp , JSVAL_NULL ) ;
2011-11-26 14:05:59 +04:00
return true ;
2008-11-28 05:25:35 +03:00
}
} else if ( buf [ buflen - 1 ] = = ' \n ' ) {
- - buflen ;
}
/* Turn buf into a JSString */
str = JS_NewStringCopyN ( cx , buf , buflen ) ;
if ( ! str )
2011-11-26 14:05:59 +04:00
return false ;
2008-11-28 05:25:35 +03:00
2010-08-16 23:35:04 +04:00
JS_SET_RVAL ( cx , vp , STRING_TO_JSVAL ( str ) ) ;
2011-11-26 14:05:59 +04:00
return true ;
2008-11-28 05:25:35 +03:00
}
2008-09-07 02:21:43 +04:00
static JSBool
2012-02-29 03:11:11 +04:00
Print ( JSContext * cx , unsigned argc , jsval * vp )
1999-04-10 10:51:01 +04:00
{
2012-02-29 03:11:11 +04:00
unsigned i , n ;
1999-04-10 10:51:01 +04:00
JSString * str ;
2010-08-16 23:35:04 +04:00
jsval * argv = JS_ARGV ( cx , vp ) ;
1999-04-10 10:51:01 +04:00
for ( i = n = 0 ; i < argc ; i + + ) {
str = JS_ValueToString ( cx , argv [ i ] ) ;
if ( ! str )
2011-11-26 14:05:59 +04:00
return false ;
2010-11-11 23:40:29 +03:00
JSAutoByteString strBytes ( cx , str ) ;
if ( ! strBytes )
2011-11-26 14:05:59 +04:00
return false ;
2010-11-11 23:40:29 +03:00
fprintf ( gOutFile , " %s%s " , i ? " " : " " , strBytes . ptr ( ) ) ;
2009-06-29 03:01:42 +04:00
fflush ( gOutFile ) ;
1999-04-10 10:51:01 +04:00
}
n + + ;
if ( n )
fputc ( ' \n ' , gOutFile ) ;
2010-08-16 23:35:04 +04:00
JS_SET_RVAL ( cx , vp , JSVAL_VOID ) ;
2011-11-26 14:05:59 +04:00
return true ;
1999-04-10 10:51:01 +04:00
}
2008-09-07 02:21:43 +04:00
static JSBool
2012-02-29 03:11:11 +04:00
Dump ( JSContext * cx , unsigned argc , jsval * vp )
1999-11-02 08:55:29 +03:00
{
2010-08-16 23:35:04 +04:00
JS_SET_RVAL ( cx , vp , JSVAL_VOID ) ;
1999-11-02 08:55:29 +03:00
JSString * str ;
if ( ! argc )
2011-11-26 14:05:59 +04:00
return true ;
2009-06-29 03:01:42 +04:00
2010-08-16 23:35:04 +04:00
str = JS_ValueToString ( cx , JS_ARGV ( cx , vp ) [ 0 ] ) ;
1999-11-02 08:55:29 +03:00
if ( ! str )
2011-11-26 14:05:59 +04:00
return false ;
1999-11-02 08:55:29 +03:00
2010-11-23 16:40:55 +03:00
JSAutoByteString bytes ( cx , str ) ;
if ( ! bytes )
2011-11-26 14:05:59 +04:00
return false ;
2011-10-14 21:52:47 +04:00
2011-11-10 04:27:08 +04:00
# ifdef ANDROID
2012-06-28 18:14:11 +04:00
__android_log_print ( ANDROID_LOG_INFO , " Gecko " , " %s " , bytes . ptr ( ) ) ;
2011-11-10 04:27:08 +04:00
# endif
2010-11-23 16:40:55 +03:00
fputs ( bytes . ptr ( ) , gOutFile ) ;
2009-06-29 03:01:42 +04:00
fflush ( gOutFile ) ;
2011-11-26 14:05:59 +04:00
return true ;
1999-11-02 08:55:29 +03:00
}
2008-09-07 02:21:43 +04:00
static JSBool
2012-02-29 03:11:11 +04:00
Load ( JSContext * cx , unsigned argc , jsval * vp )
1999-04-10 10:51:01 +04:00
{
2010-08-16 23:35:04 +04:00
JSObject * obj = JS_THIS_OBJECT ( cx , vp ) ;
if ( ! obj )
2011-02-03 16:06:21 +03:00
return false ;
2010-08-16 23:35:04 +04:00
jsval * argv = JS_ARGV ( cx , vp ) ;
2012-02-29 03:11:11 +04:00
for ( unsigned i = 0 ; i < argc ; i + + ) {
2011-02-03 16:06:21 +03:00
JSString * str = JS_ValueToString ( cx , argv [ i ] ) ;
1999-04-10 10:51:01 +04:00
if ( ! str )
2011-02-03 16:06:21 +03:00
return false ;
1999-04-10 10:51:01 +04:00
argv [ i ] = STRING_TO_JSVAL ( str ) ;
2010-11-11 23:40:29 +03:00
JSAutoByteString filename ( cx , str ) ;
if ( ! filename )
2011-02-03 16:06:21 +03:00
return false ;
FILE * file = fopen ( filename . ptr ( ) , " r " ) ;
2009-03-08 12:38:55 +03:00
if ( ! file ) {
2010-11-11 23:40:29 +03:00
JS_ReportError ( cx , " cannot open file '%s' for reading " ,
filename . ptr ( ) ) ;
2011-02-03 16:06:21 +03:00
return false ;
2009-03-08 12:38:55 +03:00
}
2011-12-16 23:08:59 +04:00
JSScript * script = JS_CompileUTF8FileHandleForPrincipals ( cx , obj , filename . ptr ( ) ,
file , gJSPrincipals ) ;
2009-03-08 12:38:55 +03:00
fclose ( file ) ;
2011-09-02 10:46:00 +04:00
if ( ! script )
2011-02-03 16:06:21 +03:00
return false ;
2009-03-08 12:38:55 +03:00
2011-02-03 16:06:21 +03:00
jsval result ;
2011-09-02 10:46:00 +04:00
if ( ! compileOnly & & ! JS_ExecuteScript ( cx , obj , script , & result ) )
2011-02-03 16:06:21 +03:00
return false ;
1999-04-10 10:51:01 +04:00
}
2010-08-16 23:35:04 +04:00
JS_SET_RVAL ( cx , vp , JSVAL_VOID ) ;
2011-02-03 16:06:21 +03:00
return true ;
1999-04-10 10:51:01 +04:00
}
2008-09-07 02:21:43 +04:00
static JSBool
2012-02-29 03:11:11 +04:00
Version ( JSContext * cx , unsigned argc , jsval * vp )
1999-04-10 10:51:01 +04:00
{
2010-08-16 23:35:04 +04:00
if ( argc > 0 & & JSVAL_IS_INT ( JS_ARGV ( cx , vp ) [ 0 ] ) )
JS_SET_RVAL ( cx , vp , INT_TO_JSVAL ( JS_SetVersion ( cx , JSVersion ( JSVAL_TO_INT ( JS_ARGV ( cx , vp ) [ 0 ] ) ) ) ) ) ;
1999-04-10 10:51:01 +04:00
else
2010-08-16 23:35:04 +04:00
JS_SET_RVAL ( cx , vp , INT_TO_JSVAL ( JS_GetVersion ( cx ) ) ) ;
2011-11-26 14:05:59 +04:00
return true ;
1999-04-10 10:51:01 +04:00
}
2008-09-07 02:21:43 +04:00
static JSBool
2012-02-29 03:11:11 +04:00
BuildDate ( JSContext * cx , unsigned argc , jsval * vp )
1999-04-10 10:51:01 +04:00
{
fprintf ( gOutFile , " built on %s at %s \n " , __DATE__ , __TIME__ ) ;
2010-08-16 23:35:04 +04:00
JS_SET_RVAL ( cx , vp , JSVAL_VOID ) ;
2011-11-26 14:05:59 +04:00
return true ;
1999-04-10 10:51:01 +04:00
}
2008-09-07 02:21:43 +04:00
static JSBool
2012-02-29 03:11:11 +04:00
Quit ( JSContext * cx , unsigned argc , jsval * vp )
1999-04-10 10:51:01 +04:00
{
1999-11-23 02:28:58 +03:00
gExitCode = 0 ;
2010-08-16 23:35:04 +04:00
JS_ConvertArguments ( cx , argc , JS_ARGV ( cx , vp ) , " / i " , & gExitCode ) ;
1999-11-23 02:28:58 +03:00
2011-11-26 14:05:59 +04:00
gQuitting = true ;
1999-10-09 10:01:57 +04:00
// exit(0);
2011-11-26 14:05:59 +04:00
return false ;
1999-04-10 10:51:01 +04:00
}
2008-09-07 02:21:43 +04:00
static JSBool
2012-02-29 03:11:11 +04:00
DumpXPC ( JSContext * cx , unsigned argc , jsval * vp )
1999-04-13 08:15:36 +04:00
{
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
int32_t depth = 2 ;
1999-07-31 04:19:50 +04:00
1999-04-13 08:15:36 +04:00
if ( argc > 0 ) {
2010-08-16 23:35:04 +04:00
if ( ! JS_ValueToInt32 ( cx , JS_ARGV ( cx , vp ) [ 0 ] , & depth ) )
2011-11-26 14:05:59 +04:00
return false ;
1999-04-13 08:15:36 +04:00
}
1999-04-24 12:10:16 +04:00
2000-03-28 07:45:12 +04:00
nsCOMPtr < nsIXPConnect > xpc = do_GetService ( nsIXPConnect : : GetCID ( ) ) ;
2011-10-14 21:52:48 +04:00
if ( xpc )
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
xpc - > DebugDump ( int16_t ( depth ) ) ;
2010-08-16 23:35:04 +04:00
JS_SET_RVAL ( cx , vp , JSVAL_VOID ) ;
2011-11-26 14:05:59 +04:00
return true ;
1999-04-13 08:15:36 +04:00
}
2008-09-07 02:21:43 +04:00
static JSBool
2012-02-29 03:11:11 +04:00
GC ( JSContext * cx , unsigned argc , jsval * vp )
1999-04-13 08:15:36 +04:00
{
2012-03-28 14:13:30 +04:00
JSRuntime * rt = JS_GetRuntime ( cx ) ;
JS_GC ( rt ) ;
1999-04-13 08:15:36 +04:00
# ifdef JS_GCMETER
2012-03-28 14:13:30 +04:00
js_DumpGCStats ( rt , stdout ) ;
1999-04-13 08:15:36 +04:00
# endif
2010-08-16 23:35:04 +04:00
JS_SET_RVAL ( cx , vp , JSVAL_VOID ) ;
2011-11-26 14:05:59 +04:00
return true ;
1999-04-13 08:15:36 +04:00
}
2010-03-18 03:05:22 +03:00
# ifdef JS_GC_ZEAL
static JSBool
2012-02-29 03:11:11 +04:00
GCZeal ( JSContext * cx , unsigned argc , jsval * vp )
2010-03-18 03:05:22 +03:00
{
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
uint32_t zeal ;
2010-08-16 23:35:04 +04:00
if ( ! JS_ValueToECMAUint32 ( cx , argc ? JS_ARGV ( cx , vp ) [ 0 ] : JSVAL_VOID , & zeal ) )
2011-11-26 14:05:59 +04:00
return false ;
2010-03-18 03:05:22 +03:00
2012-04-03 22:41:56 +04:00
JS_SetGCZeal ( cx , uint8_t ( zeal ) , JS_DEFAULT_ZEAL_FREQ ) ;
2010-08-16 23:35:04 +04:00
JS_SET_RVAL ( cx , vp , JSVAL_VOID ) ;
2011-11-26 14:05:59 +04:00
return true ;
2010-03-18 03:05:22 +03:00
}
# endif
2007-04-25 17:43:18 +04:00
# ifdef DEBUG
static JSBool
2012-02-29 03:11:11 +04:00
DumpHeap ( JSContext * cx , unsigned argc , jsval * vp )
2007-04-25 17:43:18 +04:00
{
void * startThing = NULL ;
2011-09-01 22:43:46 +04:00
JSGCTraceKind startTraceKind = JSTRACE_OBJECT ;
2007-04-25 17:43:18 +04:00
void * thingToFind = NULL ;
size_t maxDepth = ( size_t ) - 1 ;
void * thingToIgnore = NULL ;
FILE * dumpFile ;
JSBool ok ;
2010-08-16 23:35:04 +04:00
jsval * argv = JS_ARGV ( cx , vp ) ;
JS_SET_RVAL ( cx , vp , JSVAL_VOID ) ;
vp = argv + 0 ;
2010-11-11 23:40:29 +03:00
JSAutoByteString fileName ;
2010-08-16 23:35:04 +04:00
if ( argc > 0 & & * vp ! = JSVAL_NULL & & * vp ! = JSVAL_VOID ) {
2007-04-25 17:43:18 +04:00
JSString * str ;
str = JS_ValueToString ( cx , * vp ) ;
if ( ! str )
2011-11-26 14:05:59 +04:00
return false ;
2007-04-25 17:43:18 +04:00
* vp = STRING_TO_JSVAL ( str ) ;
2010-11-11 23:40:29 +03:00
if ( ! fileName . encode ( cx , str ) )
2011-11-26 14:05:59 +04:00
return false ;
2007-04-25 17:43:18 +04:00
}
2010-08-16 23:35:04 +04:00
vp = argv + 1 ;
if ( argc > 1 & & * vp ! = JSVAL_NULL & & * vp ! = JSVAL_VOID ) {
2007-04-25 17:43:18 +04:00
if ( ! JSVAL_IS_TRACEABLE ( * vp ) )
goto not_traceable_arg ;
startThing = JSVAL_TO_TRACEABLE ( * vp ) ;
startTraceKind = JSVAL_TRACE_KIND ( * vp ) ;
}
2010-08-16 23:35:04 +04:00
vp = argv + 2 ;
if ( argc > 2 & & * vp ! = JSVAL_NULL & & * vp ! = JSVAL_VOID ) {
2007-04-25 17:43:18 +04:00
if ( ! JSVAL_IS_TRACEABLE ( * vp ) )
goto not_traceable_arg ;
thingToFind = JSVAL_TO_TRACEABLE ( * vp ) ;
}
2010-08-16 23:35:04 +04:00
vp = argv + 3 ;
if ( argc > 3 & & * vp ! = JSVAL_NULL & & * vp ! = JSVAL_VOID ) {
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
uint32_t depth ;
2007-04-25 17:43:18 +04:00
if ( ! JS_ValueToECMAUint32 ( cx , * vp , & depth ) )
2011-11-26 14:05:59 +04:00
return false ;
2007-04-25 17:43:18 +04:00
maxDepth = depth ;
}
2010-08-16 23:35:04 +04:00
vp = argv + 4 ;
if ( argc > 4 & & * vp ! = JSVAL_NULL & & * vp ! = JSVAL_VOID ) {
2007-04-25 17:43:18 +04:00
if ( ! JSVAL_IS_TRACEABLE ( * vp ) )
goto not_traceable_arg ;
thingToIgnore = JSVAL_TO_TRACEABLE ( * vp ) ;
}
if ( ! fileName ) {
dumpFile = gOutFile ;
} else {
2010-11-11 23:40:29 +03:00
dumpFile = fopen ( fileName . ptr ( ) , " w " ) ;
2007-04-25 17:43:18 +04:00
if ( ! dumpFile ) {
fprintf ( gErrFile , " dumpHeap: can't open %s: %s \n " ,
2010-11-11 23:40:29 +03:00
fileName . ptr ( ) , strerror ( errno ) ) ;
2011-11-26 14:05:59 +04:00
return false ;
2007-04-25 17:43:18 +04:00
}
}
2012-03-01 00:23:38 +04:00
ok = JS_DumpHeap ( JS_GetRuntime ( cx ) , dumpFile , startThing , startTraceKind , thingToFind ,
2007-04-30 01:49:00 +04:00
maxDepth , thingToIgnore ) ;
2007-04-25 17:43:18 +04:00
if ( dumpFile ! = gOutFile )
fclose ( dumpFile ) ;
2012-03-01 00:23:38 +04:00
if ( ! ok )
JS_ReportOutOfMemory ( cx ) ;
2007-04-25 17:43:18 +04:00
return ok ;
not_traceable_arg :
fprintf ( gErrFile ,
" dumpHeap: argument %u is not null or a heap-allocated thing \n " ,
( unsigned ) ( vp - argv ) ) ;
2011-11-26 14:05:59 +04:00
return false ;
2007-04-25 17:43:18 +04:00
}
# endif /* DEBUG */
2009-08-29 03:16:19 +04:00
static JSBool
SendCommand ( JSContext * cx ,
2012-02-29 03:11:11 +04:00
unsigned argc ,
2010-08-16 23:35:04 +04:00
jsval * vp )
2009-08-29 03:16:19 +04:00
{
if ( argc = = 0 ) {
JS_ReportError ( cx , " Function takes at least one argument! " ) ;
2011-11-26 14:05:59 +04:00
return false ;
2009-08-29 03:16:19 +04:00
}
2010-08-16 23:35:04 +04:00
jsval * argv = JS_ARGV ( cx , vp ) ;
2009-08-29 03:16:19 +04:00
JSString * str = JS_ValueToString ( cx , argv [ 0 ] ) ;
if ( ! str ) {
JS_ReportError ( cx , " Could not convert argument 1 to string! " ) ;
2011-11-26 14:05:59 +04:00
return false ;
2009-08-29 03:16:19 +04:00
}
if ( argc > 1 & & JS_TypeOfValue ( cx , argv [ 1 ] ) ! = JSTYPE_FUNCTION ) {
JS_ReportError ( cx , " Could not convert argument 2 to function! " ) ;
2011-11-26 14:05:59 +04:00
return false ;
2009-08-29 03:16:19 +04:00
}
2012-07-30 18:20:58 +04:00
if ( ! XRE_SendTestShellCommand ( cx , str , argc > 1 ? & argv [ 1 ] : nullptr ) ) {
2009-08-29 03:16:19 +04:00
JS_ReportError ( cx , " Couldn't send command! " ) ;
2011-11-26 14:05:59 +04:00
return false ;
2009-08-29 03:16:19 +04:00
}
2010-08-16 23:35:04 +04:00
JS_SET_RVAL ( cx , vp , JSVAL_VOID ) ;
2011-11-26 14:05:59 +04:00
return true ;
2009-08-29 03:16:19 +04:00
}
2009-11-06 23:43:39 +03:00
static JSBool
GetChildGlobalObject ( JSContext * cx ,
2012-02-29 03:11:11 +04:00
unsigned ,
2010-08-16 23:35:04 +04:00
jsval * vp )
2009-11-06 23:43:39 +03:00
{
JSObject * global ;
if ( XRE_GetChildGlobalObject ( cx , & global ) ) {
2010-08-16 23:35:04 +04:00
JS_SET_RVAL ( cx , vp , OBJECT_TO_JSVAL ( global ) ) ;
2011-11-26 14:05:59 +04:00
return true ;
2009-11-06 23:43:39 +03:00
}
2011-11-26 14:05:59 +04:00
return false ;
2009-11-06 23:43:39 +03:00
}
2009-09-28 22:34:57 +04:00
/*
* JSContext option name to flag map . The option names are in alphabetical
* order for better reporting .
*/
2011-10-11 10:00:28 +04:00
static const struct JSOption {
2009-09-28 22:34:57 +04:00
const char * name ;
2012-01-02 22:05:19 +04:00
uint32_t flag ;
2009-09-28 22:34:57 +04:00
} js_options [ ] = {
{ " atline " , JSOPTION_ATLINE } ,
{ " relimit " , JSOPTION_RELIMIT } ,
{ " strict " , JSOPTION_STRICT } ,
{ " werror " , JSOPTION_WERROR } ,
2012-05-31 00:05:59 +04:00
{ " allow_xml " , JSOPTION_ALLOW_XML } ,
2012-05-31 00:05:58 +04:00
{ " moar_xml " , JSOPTION_MOAR_XML } ,
2012-05-05 23:26:16 +04:00
{ " strict_mode " , JSOPTION_STRICT_MODE } ,
2009-09-28 22:34:57 +04:00
} ;
2012-01-02 22:05:19 +04:00
static uint32_t
2009-09-28 22:34:57 +04:00
MapContextOptionNameToFlag ( JSContext * cx , const char * name )
{
2011-10-11 10:00:28 +04:00
for ( size_t i = 0 ; i < ArrayLength ( js_options ) ; + + i ) {
2009-09-28 22:34:57 +04:00
if ( strcmp ( name , js_options [ i ] . name ) = = 0 )
return js_options [ i ] . flag ;
}
char * msg = JS_sprintf_append ( NULL ,
" unknown option name '%s'. "
" The valid names are " , name ) ;
2011-10-11 10:00:28 +04:00
for ( size_t i = 0 ; i < ArrayLength ( js_options ) ; + + i ) {
2009-09-28 22:34:57 +04:00
if ( ! msg )
break ;
msg = JS_sprintf_append ( msg , " %s%s " , js_options [ i ] . name ,
2011-10-11 10:00:28 +04:00
( i + 2 < ArrayLength ( js_options )
2009-09-28 22:34:57 +04:00
? " , "
2011-10-11 10:00:28 +04:00
: i + 2 = = ArrayLength ( js_options )
2009-09-28 22:34:57 +04:00
? " and "
: " . " ) ) ;
}
if ( ! msg ) {
JS_ReportOutOfMemory ( cx ) ;
} else {
JS_ReportError ( cx , msg ) ;
free ( msg ) ;
}
return 0 ;
}
static JSBool
2012-02-29 03:11:11 +04:00
Options ( JSContext * cx , unsigned argc , jsval * vp )
2009-09-28 22:34:57 +04:00
{
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
uint32_t optset , flag ;
2009-09-28 22:34:57 +04:00
JSString * str ;
char * names ;
JSBool found ;
optset = 0 ;
2010-08-16 23:35:04 +04:00
jsval * argv = JS_ARGV ( cx , vp ) ;
2012-02-29 03:11:11 +04:00
for ( unsigned i = 0 ; i < argc ; i + + ) {
2009-09-28 22:34:57 +04:00
str = JS_ValueToString ( cx , argv [ i ] ) ;
if ( ! str )
2011-11-26 14:05:59 +04:00
return false ;
2009-09-28 22:34:57 +04:00
argv [ i ] = STRING_TO_JSVAL ( str ) ;
2010-11-11 23:40:29 +03:00
JSAutoByteString opt ( cx , str ) ;
2009-09-28 22:34:57 +04:00
if ( ! opt )
2011-11-26 14:05:59 +04:00
return false ;
2010-11-11 23:40:29 +03:00
flag = MapContextOptionNameToFlag ( cx , opt . ptr ( ) ) ;
2009-09-28 22:34:57 +04:00
if ( ! flag )
2011-11-26 14:05:59 +04:00
return false ;
2009-09-28 22:34:57 +04:00
optset | = flag ;
}
optset = JS_ToggleOptions ( cx , optset ) ;
names = NULL ;
2011-11-26 14:05:59 +04:00
found = false ;
2011-10-11 10:00:28 +04:00
for ( size_t i = 0 ; i < ArrayLength ( js_options ) ; i + + ) {
2009-09-28 22:34:57 +04:00
if ( js_options [ i ] . flag & optset ) {
2011-11-26 14:05:59 +04:00
found = true ;
2009-09-28 22:34:57 +04:00
names = JS_sprintf_append ( names , " %s%s " ,
names ? " , " : " " , js_options [ i ] . name ) ;
if ( ! names )
break ;
}
}
if ( ! found )
names = strdup ( " " ) ;
if ( ! names ) {
JS_ReportOutOfMemory ( cx ) ;
2011-11-26 14:05:59 +04:00
return false ;
2009-09-28 22:34:57 +04:00
}
2010-12-09 13:22:15 +03:00
str = JS_NewStringCopyZ ( cx , names ) ;
free ( names ) ;
if ( ! str )
2011-11-26 14:05:59 +04:00
return false ;
2010-08-16 23:35:04 +04:00
JS_SET_RVAL ( cx , vp , STRING_TO_JSVAL ( str ) ) ;
2011-11-26 14:05:59 +04:00
return true ;
2009-09-28 22:34:57 +04:00
}
2010-04-12 00:51:00 +04:00
static JSBool
2012-02-29 03:11:11 +04:00
Parent ( JSContext * cx , unsigned argc , jsval * vp )
2010-04-12 00:51:00 +04:00
{
if ( argc ! = 1 ) {
JS_ReportError ( cx , " Wrong number of arguments " ) ;
2011-11-26 14:05:59 +04:00
return false ;
2010-04-12 00:51:00 +04:00
}
jsval v = JS_ARGV ( cx , vp ) [ 0 ] ;
if ( JSVAL_IS_PRIMITIVE ( v ) ) {
JS_ReportError ( cx , " Only objects have parents! " ) ;
2011-11-26 14:05:59 +04:00
return false ;
2010-04-12 00:51:00 +04:00
}
2012-02-06 00:07:23 +04:00
* vp = OBJECT_TO_JSVAL ( JS_GetParent ( JSVAL_TO_OBJECT ( v ) ) ) ;
2011-11-26 14:05:59 +04:00
return true ;
2010-04-12 00:51:00 +04:00
}
1999-04-10 10:51:01 +04:00
static JSFunctionSpec glob_functions [ ] = {
2012-08-08 09:26:19 +04:00
JS_FS ( " print " , Print , 0 , 0 ) ,
JS_FS ( " readline " , ReadLine , 1 , 0 ) ,
JS_FS ( " load " , Load , 1 , 0 ) ,
JS_FS ( " quit " , Quit , 0 , 0 ) ,
JS_FS ( " version " , Version , 1 , 0 ) ,
JS_FS ( " build " , BuildDate , 0 , 0 ) ,
JS_FS ( " dumpXPC " , DumpXPC , 1 , 0 ) ,
JS_FS ( " dump " , Dump , 1 , 0 ) ,
JS_FS ( " gc " , GC , 0 , 0 ) ,
2010-03-18 03:05:22 +03:00
# ifdef JS_GC_ZEAL
2012-08-08 09:26:19 +04:00
JS_FS ( " gczeal " , GCZeal , 1 , 0 ) ,
2010-03-18 03:05:22 +03:00
# endif
2012-08-08 09:26:19 +04:00
JS_FS ( " options " , Options , 0 , 0 ) ,
2010-04-12 00:51:00 +04:00
JS_FN ( " parent " , Parent , 1 , 0 ) ,
2007-04-25 17:43:18 +04:00
# ifdef DEBUG
2012-08-08 09:26:19 +04:00
JS_FS ( " dumpHeap " , DumpHeap , 5 , 0 ) ,
2008-01-16 23:42:50 +03:00
# endif
2012-08-08 09:26:19 +04:00
JS_FS ( " sendCommand " , SendCommand , 1 , 0 ) ,
JS_FS ( " getChildGlobalObject " , GetChildGlobalObject , 0 , 0 ) ,
JS_FS_END
1999-04-10 10:51:01 +04:00
} ;
2003-11-11 04:28:17 +03:00
JSClass global_class = {
1999-04-10 10:51:01 +04:00
" global " , 0 ,
2011-02-09 22:31:40 +03:00
JS_PropertyStub , JS_PropertyStub , JS_PropertyStub , JS_StrictPropertyStub ,
2012-07-30 18:20:58 +04:00
JS_EnumerateStub , JS_ResolveStub , JS_ConvertStub , nullptr
1999-04-10 10:51:01 +04:00
} ;
2003-06-14 01:25:10 +04:00
static JSBool
2012-07-30 15:19:09 +04:00
env_setProperty ( JSContext * cx , JSHandleObject obj , JSHandleId id , JSBool strict , JSMutableHandleValue vp )
2003-06-14 01:25:10 +04:00
{
2003-06-14 10:10:04 +04:00
/* XXX porting may be easy, but these don't seem to supply setenv by default */
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 22:10:24 +03:00
# if !defined XP_OS2 && !defined SOLARIS
2003-06-14 01:25:10 +04:00
JSString * idstr , * valstr ;
int rv ;
2010-07-15 10:19:36 +04:00
jsval idval ;
if ( ! JS_IdToValue ( cx , id , & idval ) )
2011-11-26 14:05:59 +04:00
return false ;
2011-10-14 21:52:47 +04:00
2010-07-15 10:19:36 +04:00
idstr = JS_ValueToString ( cx , idval ) ;
2012-07-30 15:19:09 +04:00
valstr = JS_ValueToString ( cx , vp ) ;
2003-06-14 01:25:10 +04:00
if ( ! idstr | | ! valstr )
2011-11-26 14:05:59 +04:00
return false ;
2010-11-11 23:40:29 +03:00
JSAutoByteString name ( cx , idstr ) ;
if ( ! name )
2011-11-26 14:05:59 +04:00
return false ;
2010-11-11 23:40:29 +03:00
JSAutoByteString value ( cx , valstr ) ;
if ( ! value )
2011-11-26 14:05:59 +04:00
return false ;
2011-09-02 18:41:45 +04:00
# if defined XP_WIN || defined HPUX || defined OSF1 || defined SCO
2003-06-14 01:25:10 +04:00
{
2010-11-11 23:40:29 +03:00
char * waste = JS_smprintf ( " %s=%s " , name . ptr ( ) , value . ptr ( ) ) ;
2003-06-14 01:25:10 +04:00
if ( ! waste ) {
JS_ReportOutOfMemory ( cx ) ;
2011-11-26 14:05:59 +04:00
return false ;
2003-06-14 01:25:10 +04:00
}
rv = putenv ( waste ) ;
2003-06-28 04:55:12 +04:00
# ifdef XP_WIN
2003-06-19 08:43:23 +04:00
/*
* HPUX9 at least still has the bad old non - copying putenv .
*
* Per mail from < s . shanmuganathan @ digital . com > , OSF1 also has a putenv
* that will crash if you pass it an auto char array ( so it must place
* its argument directly in the char * environ [ ] array ) .
*/
2003-06-14 01:25:10 +04:00
free ( waste ) ;
2003-06-14 04:10:09 +04:00
# endif
2003-06-14 01:25:10 +04:00
}
# else
2010-11-11 23:40:29 +03:00
rv = setenv ( name . ptr ( ) , value . ptr ( ) , 1 ) ;
2003-06-14 01:25:10 +04:00
# endif
if ( rv < 0 ) {
2010-11-11 23:40:29 +03:00
JS_ReportError ( cx , " can't set envariable %s to %s " , name . ptr ( ) , value . ptr ( ) ) ;
2011-11-26 14:05:59 +04:00
return false ;
2003-06-14 01:25:10 +04:00
}
2012-07-30 15:19:09 +04:00
vp . set ( STRING_TO_JSVAL ( valstr ) ) ;
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 22:10:24 +03:00
# endif /* !defined XP_OS2 && !defined SOLARIS */
2011-11-26 14:05:59 +04:00
return true ;
2003-06-14 01:25:10 +04:00
}
static JSBool
2012-05-20 02:03:45 +04:00
env_enumerate ( JSContext * cx , JSHandleObject obj )
2003-06-14 01:25:10 +04:00
{
static JSBool reflected ;
char * * evp , * name , * value ;
JSString * valstr ;
JSBool ok ;
if ( reflected )
2011-11-26 14:05:59 +04:00
return true ;
2003-06-14 01:25:10 +04:00
2012-02-06 00:07:23 +04:00
for ( evp = ( char * * ) JS_GetPrivate ( obj ) ; ( name = * evp ) ! = NULL ; evp + + ) {
2003-06-14 01:25:10 +04:00
value = strchr ( name , ' = ' ) ;
if ( ! value )
continue ;
* value + + = ' \0 ' ;
valstr = JS_NewStringCopyZ ( cx , value ) ;
if ( ! valstr ) {
2011-11-26 14:05:59 +04:00
ok = false ;
2003-06-14 01:25:10 +04:00
} else {
ok = JS_DefineProperty ( cx , obj , name , STRING_TO_JSVAL ( valstr ) ,
NULL , NULL , JSPROP_ENUMERATE ) ;
}
value [ - 1 ] = ' = ' ;
if ( ! ok )
2011-11-26 14:05:59 +04:00
return false ;
2003-06-14 01:25:10 +04:00
}
2011-11-26 14:05:59 +04:00
reflected = true ;
return true ;
2003-06-14 01:25:10 +04:00
}
static JSBool
2012-05-20 02:03:45 +04:00
env_resolve ( JSContext * cx , JSHandleObject obj , JSHandleId id , unsigned flags ,
2012-07-04 22:12:16 +04:00
JSMutableHandleObject objp )
2003-06-14 01:25:10 +04:00
{
JSString * idstr , * valstr ;
if ( flags & JSRESOLVE_ASSIGNING )
2011-11-26 14:05:59 +04:00
return true ;
2003-06-14 01:25:10 +04:00
2010-07-15 10:19:36 +04:00
jsval idval ;
if ( ! JS_IdToValue ( cx , id , & idval ) )
2011-11-26 14:05:59 +04:00
return false ;
2010-07-15 10:19:36 +04:00
idstr = JS_ValueToString ( cx , idval ) ;
2003-06-14 01:25:10 +04:00
if ( ! idstr )
2011-11-26 14:05:59 +04:00
return false ;
2010-11-11 23:40:29 +03:00
JSAutoByteString name ( cx , idstr ) ;
if ( ! name )
2011-11-26 14:05:59 +04:00
return false ;
2010-11-11 23:40:29 +03:00
const char * value = getenv ( name . ptr ( ) ) ;
2003-06-14 01:25:10 +04:00
if ( value ) {
valstr = JS_NewStringCopyZ ( cx , value ) ;
if ( ! valstr )
2011-11-26 14:05:59 +04:00
return false ;
2010-11-11 23:40:29 +03:00
if ( ! JS_DefinePropertyById ( cx , obj , id , STRING_TO_JSVAL ( valstr ) ,
NULL , NULL , JSPROP_ENUMERATE ) ) {
2011-11-26 14:05:59 +04:00
return false ;
2003-06-14 01:25:10 +04:00
}
2012-07-04 22:12:16 +04:00
objp . set ( obj ) ;
2003-06-14 01:25:10 +04:00
}
2011-11-26 14:05:59 +04:00
return true ;
2003-06-14 01:25:10 +04:00
}
static JSClass env_class = {
" environment " , JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE ,
JS_PropertyStub , JS_PropertyStub ,
JS_PropertyStub , env_setProperty ,
env_enumerate , ( JSResolveOp ) env_resolve ,
2012-07-30 18:20:58 +04:00
JS_ConvertStub , nullptr
2003-06-14 01:25:10 +04:00
} ;
1999-04-10 10:51:01 +04:00
/***************************************************************************/
1999-04-28 01:09:34 +04:00
typedef enum JSShellErrNum {
# define MSG_DEF(name, number, count, exception, format) \
name = number ,
# include "jsshell.msg"
# undef MSG_DEF
JSShellErr_Limit
} JSShellErrNum ;
2011-12-28 12:13:37 +04:00
JSErrorFormatString jsShell_ErrorFormatString [ JSShellErr_Limit ] = {
1999-04-28 01:09:34 +04:00
# define MSG_DEF(name, number, count, exception, format) \
{ format , count } ,
# include "jsshell.msg"
# undef MSG_DEF
} ;
2008-09-07 02:21:43 +04:00
static const JSErrorFormatString *
2012-02-29 03:11:11 +04:00
my_GetErrorMessage ( void * userRef , const char * locale , const unsigned errorNumber )
1999-04-28 01:09:34 +04:00
{
2011-12-28 12:13:37 +04:00
if ( errorNumber = = 0 | | errorNumber > = JSShellErr_Limit )
return NULL ;
return & jsShell_ErrorFormatString [ errorNumber ] ;
1999-04-28 01:09:34 +04:00
}
1999-04-10 10:51:01 +04:00
static void
2005-11-30 02:52:28 +03:00
ProcessFile ( JSContext * cx , JSObject * obj , const char * filename , FILE * file ,
JSBool forceTTY )
1999-04-10 10:51:01 +04:00
{
2011-09-02 10:46:00 +04:00
JSScript * script ;
1999-04-10 10:51:01 +04:00
jsval result ;
2003-06-14 01:25:10 +04:00
int lineno , startline ;
JSBool ok , hitEOF ;
char * bufp , buffer [ 4096 ] ;
1999-04-10 10:51:01 +04:00
JSString * str ;
1999-07-31 04:19:50 +04:00
2005-11-30 02:52:28 +03:00
if ( forceTTY ) {
file = stdin ;
2011-10-14 21:52:48 +04:00
} else
2009-01-17 01:10:48 +03:00
# ifdef HAVE_ISATTY
2009-02-16 18:27:13 +03:00
if ( ! isatty ( fileno ( file ) ) )
# endif
{
1999-07-31 04:19:50 +04:00
/*
1999-04-28 01:09:34 +04:00
* It ' s not interactive - just execute it .
*
* Support the UNIX # ! shell hack ; gobble the first line if it starts
1999-07-31 04:19:50 +04:00
* with ' # ' . TODO - this isn ' t quite compatible with sharp variables ,
* as a legal js program ( using sharp variables ) might start with ' # ' .
* But that would require multi - character lookahead .
*/
2003-06-14 01:25:10 +04:00
int ch = fgetc ( file ) ;
1999-07-31 04:19:50 +04:00
if ( ch = = ' # ' ) {
2011-10-14 21:52:48 +04:00
while ( ( ch = fgetc ( file ) ) ! = EOF ) {
if ( ch = = ' \n ' | | ch = = ' \r ' )
1999-07-31 04:19:50 +04:00
break ;
}
}
2003-06-14 01:25:10 +04:00
ungetc ( ch , file ) ;
2000-11-30 09:58:37 +03:00
DoBeginRequest ( cx ) ;
2004-11-23 08:38:59 +03:00
2011-12-16 23:08:59 +04:00
script = JS_CompileUTF8FileHandleForPrincipals ( cx , obj , filename , file ,
gJSPrincipals ) ;
2004-11-23 08:38:59 +03:00
2011-09-02 10:46:00 +04:00
if ( script & & ! compileOnly )
( void ) JS_ExecuteScript ( cx , obj , script , & result ) ;
2000-11-30 09:58:37 +03:00
DoEndRequest ( cx ) ;
2005-11-30 02:52:28 +03:00
1999-04-28 01:09:34 +04:00
return ;
1999-04-10 10:51:01 +04:00
}
1999-04-28 01:09:34 +04:00
/* It's an interactive filehandle; drop into read-eval-print loop. */
lineno = 1 ;
2011-11-26 14:05:59 +04:00
hitEOF = false ;
1999-04-10 10:51:01 +04:00
do {
1999-04-28 01:09:34 +04:00
bufp = buffer ;
* bufp = ' \0 ' ;
/*
* Accumulate lines until we get a ' compilable unit ' - one that either
* generates an error ( before running out of source ) or that compiles
* cleanly . This should be whenever we get a complete statement that
* coincides with the end of a line .
*/
startline = lineno ;
1999-04-10 10:51:01 +04:00
do {
2003-06-14 01:25:10 +04:00
if ( ! GetLine ( cx , bufp , file , startline = = lineno ? " js> " : " " ) ) {
2011-11-26 14:05:59 +04:00
hitEOF = true ;
1999-04-28 01:09:34 +04:00
break ;
1999-04-10 10:51:01 +04:00
}
1999-04-28 01:09:34 +04:00
bufp + = strlen ( bufp ) ;
lineno + + ;
2011-11-26 14:05:59 +04:00
} while ( ! JS_BufferIsCompilableUnit ( cx , false , obj , buffer , strlen ( buffer ) ) ) ;
2009-06-29 03:01:42 +04:00
2000-11-30 09:58:37 +03:00
DoBeginRequest ( cx ) ;
1999-04-28 01:09:34 +04:00
/* Clear any pending exception from previous failed compiles. */
JS_ClearPendingException ( cx ) ;
2011-09-02 10:46:00 +04:00
script = JS_CompileScriptForPrincipals ( cx , obj , gJSPrincipals , buffer ,
strlen ( buffer ) , " typein " , startline ) ;
if ( script ) {
1999-04-28 01:09:34 +04:00
JSErrorReporter older ;
1999-07-31 04:19:50 +04:00
2005-09-02 03:17:27 +04:00
if ( ! compileOnly ) {
2011-09-02 10:46:00 +04:00
ok = JS_ExecuteScript ( cx , obj , script , & result ) ;
2005-09-02 03:17:27 +04:00
if ( ok & & result ! = JSVAL_VOID ) {
/* Suppress error reports from JS_ValueToString(). */
older = JS_SetErrorReporter ( cx , NULL ) ;
str = JS_ValueToString ( cx , result ) ;
JS_SetErrorReporter ( cx , older ) ;
2010-11-11 23:40:29 +03:00
JSAutoByteString bytes ;
if ( str & & bytes . encode ( cx , str ) )
fprintf ( gOutFile , " %s \n " , bytes . ptr ( ) ) ;
2005-09-02 03:17:27 +04:00
else
2011-11-26 14:05:59 +04:00
ok = false ;
2005-09-02 03:17:27 +04:00
}
}
1999-04-10 10:51:01 +04:00
}
2000-11-30 09:58:37 +03:00
DoEndRequest ( cx ) ;
1999-10-09 10:01:57 +04:00
} while ( ! hitEOF & & ! gQuitting ) ;
2005-11-30 02:52:28 +03:00
1999-04-28 01:09:34 +04:00
fprintf ( gOutFile , " \n " ) ;
1999-04-10 10:51:01 +04:00
}
2003-06-14 01:25:10 +04:00
static void
2005-11-30 02:52:28 +03:00
Process ( JSContext * cx , JSObject * obj , const char * filename , JSBool forceTTY )
2003-06-14 01:25:10 +04:00
{
FILE * file ;
2005-11-30 02:52:28 +03:00
if ( forceTTY | | ! filename | | strcmp ( filename , " - " ) = = 0 ) {
2003-06-14 01:25:10 +04:00
file = stdin ;
} else {
file = fopen ( filename , " r " ) ;
if ( ! file ) {
JS_ReportErrorNumber ( cx , my_GetErrorMessage , NULL ,
JSSMSG_CANT_OPEN ,
filename , strerror ( errno ) ) ;
gExitCode = EXITCODE_FILE_NOT_FOUND ;
return ;
}
}
2005-11-30 02:52:28 +03:00
ProcessFile ( cx , obj , filename , file , forceTTY ) ;
2009-02-15 19:23:50 +03:00
if ( file ! = stdin )
fclose ( file ) ;
2003-06-14 01:25:10 +04:00
}
1999-04-10 10:51:01 +04:00
static int
usage ( void )
{
fprintf ( gErrFile , " %s \n " , JS_GetImplementationVersion ( ) ) ;
2011-05-06 12:40:51 +04:00
fprintf ( gErrFile , " usage: xpcshell [-g gredir] [-a appdir] [-r manifest]... [-PsSwWxCij] [-v version] [-f scriptfile] [-e script] [scriptfile] [scriptarg...] \n " ) ;
1999-04-10 10:51:01 +04:00
return 2 ;
}
2003-11-11 04:28:17 +03:00
extern JSClass global_class ;
1999-04-10 10:51:01 +04:00
static int
ProcessArgs ( JSContext * cx , JSObject * obj , char * * argv , int argc )
{
2003-06-14 01:25:10 +04:00
const char rcfilename [ ] = " xpcshell.js " ;
FILE * rcfile ;
2011-08-05 06:39:12 +04:00
int i ;
1999-04-10 10:51:01 +04:00
JSObject * argsObj ;
2003-06-14 01:25:10 +04:00
char * filename = NULL ;
2011-11-26 14:05:59 +04:00
JSBool isInteractive = true ;
JSBool forceTTY = false ;
1999-04-10 10:51:01 +04:00
2003-06-14 01:25:10 +04:00
rcfile = fopen ( rcfilename , " r " ) ;
if ( rcfile ) {
printf ( " [loading '%s'...] \n " , rcfilename ) ;
2011-11-26 14:05:59 +04:00
ProcessFile ( cx , obj , rcfilename , rcfile , false ) ;
2009-02-15 19:23:50 +03:00
fclose ( rcfile ) ;
2001-03-03 04:01:20 +03:00
}
2003-06-14 01:25:10 +04:00
/*
* Scan past all optional arguments so we can create the arguments object
* before processing any - f options , which must interleave properly with
* - v and - w options . This requires two passes , and without getopt , we ' ll
* have to keep the option logic here and in the second for loop in sync .
*/
for ( i = 0 ; i < argc ; i + + ) {
if ( argv [ i ] [ 0 ] ! = ' - ' | | argv [ i ] [ 1 ] = = ' \0 ' ) {
+ + i ;
break ;
}
switch ( argv [ i ] [ 1 ] ) {
case ' v ' :
case ' f ' :
2005-07-06 09:51:07 +04:00
case ' e ' :
2003-06-14 01:25:10 +04:00
+ + i ;
1999-04-10 10:51:01 +04:00
break ;
2005-07-06 09:51:07 +04:00
default : ;
1999-04-10 10:51:01 +04:00
}
}
2003-06-14 01:25:10 +04:00
/*
* Create arguments early and define it to root it , so it ' s safe from any
* GC calls nested below , and so it is available to - f < file > arguments .
*/
argsObj = JS_NewArrayObject ( cx , 0 , NULL ) ;
if ( ! argsObj )
return 1 ;
if ( ! JS_DefineProperty ( cx , obj , " arguments " , OBJECT_TO_JSVAL ( argsObj ) ,
NULL , NULL , 0 ) ) {
return 1 ;
2000-05-19 04:34:46 +04:00
}
1999-04-10 10:51:01 +04:00
2011-08-05 06:39:12 +04:00
for ( size_t j = 0 , length = argc - i ; j < length ; j + + ) {
2003-06-14 01:25:10 +04:00
JSString * str = JS_NewStringCopyZ ( cx , argv [ i + + ] ) ;
if ( ! str )
1999-04-10 10:51:01 +04:00
return 1 ;
2003-06-14 01:25:10 +04:00
if ( ! JS_DefineElement ( cx , argsObj , j , STRING_TO_JSVAL ( str ) ,
NULL , NULL , JSPROP_ENUMERATE ) ) {
return 1 ;
}
2000-05-19 04:34:46 +04:00
}
1999-04-10 10:51:01 +04:00
2003-06-14 01:25:10 +04:00
for ( i = 0 ; i < argc ; i + + ) {
if ( argv [ i ] [ 0 ] ! = ' - ' | | argv [ i ] [ 1 ] = = ' \0 ' ) {
filename = argv [ i + + ] ;
2011-11-26 14:05:59 +04:00
isInteractive = false ;
2003-06-14 01:25:10 +04:00
break ;
}
switch ( argv [ i ] [ 1 ] ) {
case ' v ' :
if ( + + i = = argc ) {
return usage ( ) ;
}
JS_SetVersion ( cx , JSVersion ( atoi ( argv [ i ] ) ) ) ;
break ;
case ' W ' :
2011-11-26 14:05:59 +04:00
reportWarnings = false ;
2003-06-14 01:25:10 +04:00
break ;
case ' w ' :
2011-11-26 14:05:59 +04:00
reportWarnings = true ;
2003-06-14 01:25:10 +04:00
break ;
2009-09-28 22:34:57 +04:00
case ' S ' :
JS_ToggleOptions ( cx , JSOPTION_WERROR ) ;
2003-06-14 01:25:10 +04:00
case ' s ' :
JS_ToggleOptions ( cx , JSOPTION_STRICT ) ;
break ;
2005-07-06 09:51:07 +04:00
case ' x ' :
2012-05-31 00:05:58 +04:00
JS_ToggleOptions ( cx , JSOPTION_MOAR_XML ) ;
2005-07-06 09:51:07 +04:00
break ;
2011-10-07 10:34:21 +04:00
case ' d ' :
xpc_ActivateDebugMode ( ) ;
break ;
2003-06-14 01:25:10 +04:00
case ' f ' :
if ( + + i = = argc ) {
return usage ( ) ;
}
2011-11-26 14:05:59 +04:00
Process ( cx , obj , argv [ i ] , false ) ;
2003-06-14 01:25:10 +04:00
/*
* XXX : js - f foo . js should interpret foo . js and then
* drop into interactive mode , but that breaks test
* harness . Just execute foo . js for now .
*/
2011-11-26 14:05:59 +04:00
isInteractive = false ;
2003-06-14 01:25:10 +04:00
break ;
2005-11-30 02:52:28 +03:00
case ' i ' :
2011-11-26 14:05:59 +04:00
isInteractive = forceTTY = true ;
2005-11-30 02:52:28 +03:00
break ;
2005-07-06 09:51:07 +04:00
case ' e ' :
{
jsval rval ;
if ( + + i = = argc ) {
return usage ( ) ;
}
2009-03-12 02:13:01 +03:00
JS_EvaluateScriptForPrincipals ( cx , obj , gJSPrincipals , argv [ i ] ,
strlen ( argv [ i ] ) , " -e " , 1 , & rval ) ;
2005-07-06 09:51:07 +04:00
2011-11-26 14:05:59 +04:00
isInteractive = false ;
2005-07-06 09:51:07 +04:00
break ;
}
2005-09-02 03:17:27 +04:00
case ' C ' :
2011-11-26 14:05:59 +04:00
compileOnly = true ;
isInteractive = false ;
2005-09-02 03:17:27 +04:00
break ;
2010-10-13 04:25:37 +04:00
case ' m ' :
JS_ToggleOptions ( cx , JSOPTION_METHODJIT ) ;
break ;
2011-03-04 01:07:48 +03:00
case ' n ' :
JS_ToggleOptions ( cx , JSOPTION_TYPE_INFERENCE ) ;
break ;
2003-06-14 01:25:10 +04:00
default :
return usage ( ) ;
}
}
1999-04-10 10:51:01 +04:00
if ( filename | | isInteractive )
2005-11-30 02:52:28 +03:00
Process ( cx , obj , filename , forceTTY ) ;
1999-11-23 02:28:58 +03:00
return gExitCode ;
1999-04-10 10:51:01 +04:00
}
/***************************************************************************/
2007-06-21 04:10:48 +04:00
class FullTrustSecMan
: public nsIScriptSecurityManager
2000-02-19 03:14:44 +03:00
{
public :
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCSECURITYMANAGER
2007-06-21 04:10:48 +04:00
NS_DECL_NSISCRIPTSECURITYMANAGER
2000-02-19 03:14:44 +03:00
FullTrustSecMan ( ) ;
2007-06-21 04:10:48 +04:00
virtual ~ FullTrustSecMan ( ) ;
void SetSystemPrincipal ( nsIPrincipal * aPrincipal ) {
mSystemPrincipal = aPrincipal ;
}
private :
nsCOMPtr < nsIPrincipal > mSystemPrincipal ;
2000-02-19 03:14:44 +03:00
} ;
2007-06-21 04:10:48 +04:00
NS_INTERFACE_MAP_BEGIN ( FullTrustSecMan )
NS_INTERFACE_MAP_ENTRY ( nsIXPCSecurityManager )
NS_INTERFACE_MAP_ENTRY ( nsIScriptSecurityManager )
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS ( nsISupports , nsIXPCSecurityManager )
NS_INTERFACE_MAP_END
NS_IMPL_ADDREF ( FullTrustSecMan )
NS_IMPL_RELEASE ( FullTrustSecMan )
2000-02-19 03:14:44 +03:00
FullTrustSecMan : : FullTrustSecMan ( )
{
2012-07-30 18:20:58 +04:00
mSystemPrincipal = nullptr ;
2007-06-21 04:10:48 +04:00
}
FullTrustSecMan : : ~ FullTrustSecMan ( )
{
2000-02-19 03:14:44 +03:00
}
NS_IMETHODIMP
2007-06-21 04:10:48 +04:00
FullTrustSecMan : : CanCreateWrapper ( JSContext * aJSContext , const nsIID & aIID ,
nsISupports * aObj , nsIClassInfo * aClassInfo ,
void * * aPolicy )
2000-02-19 03:14:44 +03:00
{
return NS_OK ;
}
NS_IMETHODIMP
FullTrustSecMan : : CanCreateInstance ( JSContext * aJSContext , const nsCID & aCID )
{
return NS_OK ;
}
NS_IMETHODIMP
FullTrustSecMan : : CanGetService ( JSContext * aJSContext , const nsCID & aCID )
{
return NS_OK ;
}
2012-08-22 19:56:38 +04:00
/* void CanAccess (in uint32_t aAction, in nsIXPCNativeCallContext aCallContext, in JSContextPtr aJSContext, in JSObjectPtr aJSObject, in nsISupports aObj, in nsIClassInfo aClassInfo, in jsval aName, inout voidPtr aPolicy); */
2007-06-21 04:10:48 +04:00
NS_IMETHODIMP
2012-08-22 19:56:38 +04:00
FullTrustSecMan : : CanAccess ( uint32_t aAction ,
2008-01-15 18:50:57 +03:00
nsAXPCNativeCallContext * aCallContext ,
2007-06-21 04:10:48 +04:00
JSContext * aJSContext , JSObject * aJSObject ,
nsISupports * aObj , nsIClassInfo * aClassInfo ,
2010-07-15 10:19:36 +04:00
jsid aName , void * * aPolicy )
2007-06-21 04:10:48 +04:00
{
return NS_OK ;
}
2012-08-22 19:56:38 +04:00
/* [noscript] void checkPropertyAccess (in JSContextPtr aJSContext, in JSObjectPtr aJSObject, in string aClassName, in jsid aProperty, in uint32_t aAction); */
2007-06-21 04:10:48 +04:00
NS_IMETHODIMP
FullTrustSecMan : : CheckPropertyAccess ( JSContext * aJSContext ,
JSObject * aJSObject ,
const char * aClassName ,
2012-08-22 19:56:38 +04:00
jsid aProperty , uint32_t aAction )
2000-02-19 03:14:44 +03:00
{
return NS_OK ;
}
2007-06-21 04:10:48 +04:00
/* [noscript] void checkLoadURIFromScript (in JSContextPtr cx, in nsIURI uri); */
NS_IMETHODIMP
FullTrustSecMan : : CheckLoadURIFromScript ( JSContext * cx , nsIURI * uri )
{
return NS_OK ;
}
/* void checkLoadURIWithPrincipal (in nsIPrincipal aPrincipal, in nsIURI uri, in unsigned long flags); */
NS_IMETHODIMP
FullTrustSecMan : : CheckLoadURIWithPrincipal ( nsIPrincipal * aPrincipal ,
2012-08-22 19:56:38 +04:00
nsIURI * uri , uint32_t flags )
2007-06-21 04:10:48 +04:00
{
return NS_OK ;
}
/* void checkLoadURIStrWithPrincipal (in nsIPrincipal aPrincipal, in AUTF8String uri, in unsigned long flags); */
NS_IMETHODIMP
FullTrustSecMan : : CheckLoadURIStrWithPrincipal ( nsIPrincipal * aPrincipal ,
const nsACString & uri ,
2012-08-22 19:56:38 +04:00
uint32_t flags )
2007-06-21 04:10:48 +04:00
{
return NS_OK ;
}
/* [noscript] void checkFunctionAccess (in JSContextPtr cx, in voidPtr funObj, in voidPtr targetObj); */
NS_IMETHODIMP
FullTrustSecMan : : CheckFunctionAccess ( JSContext * cx , void * funObj ,
void * targetObj )
{
return NS_OK ;
}
/* [noscript] boolean canExecuteScripts (in JSContextPtr cx, in nsIPrincipal principal); */
NS_IMETHODIMP
FullTrustSecMan : : CanExecuteScripts ( JSContext * cx , nsIPrincipal * principal ,
2011-09-29 10:19:26 +04:00
bool * _retval )
2007-06-21 04:10:48 +04:00
{
2011-10-17 18:59:28 +04:00
* _retval = true ;
2007-06-21 04:10:48 +04:00
return NS_OK ;
}
/* [noscript] nsIPrincipal getSubjectPrincipal (); */
NS_IMETHODIMP
FullTrustSecMan : : GetSubjectPrincipal ( nsIPrincipal * * _retval )
{
NS_IF_ADDREF ( * _retval = mSystemPrincipal ) ;
return * _retval ? NS_OK : NS_ERROR_FAILURE ;
}
/* [noscript] nsIPrincipal getSystemPrincipal (); */
NS_IMETHODIMP
FullTrustSecMan : : GetSystemPrincipal ( nsIPrincipal * * _retval )
{
NS_IF_ADDREF ( * _retval = mSystemPrincipal ) ;
return * _retval ? NS_OK : NS_ERROR_FAILURE ;
}
/* [noscript] nsIPrincipal getCertificatePrincipal (in AUTF8String aCertFingerprint, in AUTF8String aSubjectName, in AUTF8String aPrettyName, in nsISupports aCert, in nsIURI aURI); */
NS_IMETHODIMP
FullTrustSecMan : : GetCertificatePrincipal ( const nsACString & aCertFingerprint ,
const nsACString & aSubjectName ,
const nsACString & aPrettyName ,
nsISupports * aCert , nsIURI * aURI ,
nsIPrincipal * * _retval )
{
NS_IF_ADDREF ( * _retval = mSystemPrincipal ) ;
return * _retval ? NS_OK : NS_ERROR_FAILURE ;
}
2012-07-21 11:29:40 +04:00
/* [noscript] nsIPrincipal getSimpleCodebasePrincipal (in nsIURI aURI); */
2007-06-21 04:10:48 +04:00
NS_IMETHODIMP
2012-07-21 11:29:40 +04:00
FullTrustSecMan : : GetSimpleCodebasePrincipal ( nsIURI * aURI , nsIPrincipal * * _retval )
2007-06-21 04:10:48 +04:00
{
NS_IF_ADDREF ( * _retval = mSystemPrincipal ) ;
return * _retval ? NS_OK : NS_ERROR_FAILURE ;
}
2012-07-20 07:28:08 +04:00
/* [noscript] nsIPrincipal getNoAppCodebasePrincipal (in nsIURI aURI); */
NS_IMETHODIMP
FullTrustSecMan : : GetNoAppCodebasePrincipal ( nsIURI * aURI , nsIPrincipal * * _retval )
{
2012-07-21 11:29:40 +04:00
return GetSimpleCodebasePrincipal ( aURI , _retval ) ;
2012-07-20 07:28:08 +04:00
}
/* [noscript] nsIPrincipal getAppCodebasePrincipal (in nsIURI aURI, unsigned long appid, bool inMozBrowser); */
NS_IMETHODIMP
2012-08-22 19:56:38 +04:00
FullTrustSecMan : : GetAppCodebasePrincipal ( nsIURI * aURI , uint32_t aAppId , bool aInMozBrowser , nsIPrincipal * * _retval )
2012-07-20 07:28:08 +04:00
{
2012-07-21 11:29:40 +04:00
return GetSimpleCodebasePrincipal ( aURI , _retval ) ;
2012-07-20 07:28:08 +04:00
}
2012-07-20 11:06:24 +04:00
/* [noscript] nsIPrincipal getDocShellCodebasePrincipal (in nsIURI aURI, nsIDocShell docShell); */
NS_IMETHODIMP
FullTrustSecMan : : GetDocShellCodebasePrincipal ( nsIURI * aURI , nsIDocShell * aDocShell , nsIPrincipal * * _retval )
{
2012-07-21 11:29:40 +04:00
return GetSimpleCodebasePrincipal ( aURI , _retval ) ;
2012-07-20 11:06:24 +04:00
}
2007-06-21 04:10:48 +04:00
/* [noscript] short requestCapability (in nsIPrincipal principal, in string capability); */
NS_IMETHODIMP
FullTrustSecMan : : RequestCapability ( nsIPrincipal * principal ,
2012-08-22 19:56:38 +04:00
const char * capability , int16_t * _retval )
2007-06-21 04:10:48 +04:00
{
* _retval = nsIPrincipal : : ENABLE_GRANTED ;
return NS_OK ;
}
/* boolean isCapabilityEnabled (in string capability); */
NS_IMETHODIMP
2011-09-29 10:19:26 +04:00
FullTrustSecMan : : IsCapabilityEnabled ( const char * capability , bool * _retval )
2007-06-21 04:10:48 +04:00
{
2011-10-17 18:59:28 +04:00
* _retval = true ;
2007-06-21 04:10:48 +04:00
return NS_OK ;
}
/* void enableCapability (in string capability); */
NS_IMETHODIMP
FullTrustSecMan : : EnableCapability ( const char * capability )
{
return NS_OK ; ;
}
/* [noscript] nsIPrincipal getObjectPrincipal (in JSContextPtr cx, in JSObjectPtr obj); */
NS_IMETHODIMP
FullTrustSecMan : : GetObjectPrincipal ( JSContext * cx , JSObject * obj ,
nsIPrincipal * * _retval )
{
NS_IF_ADDREF ( * _retval = mSystemPrincipal ) ;
return * _retval ? NS_OK : NS_ERROR_FAILURE ;
}
/* [noscript] boolean subjectPrincipalIsSystem (); */
NS_IMETHODIMP
2011-09-29 10:19:26 +04:00
FullTrustSecMan : : SubjectPrincipalIsSystem ( bool * _retval )
2007-06-21 04:10:48 +04:00
{
2011-10-17 18:59:28 +04:00
* _retval = true ;
2007-06-21 04:10:48 +04:00
return NS_OK ;
}
/* [noscript] void checkSameOrigin (in JSContextPtr aJSContext, in nsIURI aTargetURI); */
NS_IMETHODIMP
FullTrustSecMan : : CheckSameOrigin ( JSContext * aJSContext , nsIURI * aTargetURI )
{
return NS_OK ;
}
/* void checkSameOriginURI (in nsIURI aSourceURI, in nsIURI aTargetURI); */
NS_IMETHODIMP
2007-10-27 05:46:09 +04:00
FullTrustSecMan : : CheckSameOriginURI ( nsIURI * aSourceURI , nsIURI * aTargetURI ,
2011-09-29 10:19:26 +04:00
bool reportError )
2007-06-21 04:10:48 +04:00
{
return NS_OK ;
}
/* [noscript] nsIPrincipal getPrincipalFromContext (in JSContextPtr cx); */
NS_IMETHODIMP
FullTrustSecMan : : GetPrincipalFromContext ( JSContext * cx , nsIPrincipal * * _retval )
{
NS_IF_ADDREF ( * _retval = mSystemPrincipal ) ;
return * _retval ? NS_OK : NS_ERROR_FAILURE ;
}
/* [noscript] nsIPrincipal getChannelPrincipal (in nsIChannel aChannel); */
NS_IMETHODIMP
FullTrustSecMan : : GetChannelPrincipal ( nsIChannel * aChannel , nsIPrincipal * * _retval )
{
NS_IF_ADDREF ( * _retval = mSystemPrincipal ) ;
return * _retval ? NS_OK : NS_ERROR_FAILURE ;
}
/* boolean isSystemPrincipal (in nsIPrincipal aPrincipal); */
NS_IMETHODIMP
2011-09-29 10:19:26 +04:00
FullTrustSecMan : : IsSystemPrincipal ( nsIPrincipal * aPrincipal , bool * _retval )
2007-06-21 04:10:48 +04:00
{
* _retval = aPrincipal = = mSystemPrincipal ;
return NS_OK ;
}
2008-01-05 02:59:12 +03:00
NS_IMETHODIMP_ ( nsIPrincipal * )
FullTrustSecMan : : GetCxSubjectPrincipal ( JSContext * cx )
{
return mSystemPrincipal ;
}
2008-10-23 00:15:22 +04:00
NS_IMETHODIMP_ ( nsIPrincipal * )
FullTrustSecMan : : GetCxSubjectPrincipalAndFrame ( JSContext * cx , JSStackFrame * * fp )
{
2012-07-30 18:20:58 +04:00
* fp = nullptr ;
2008-10-23 00:15:22 +04:00
return mSystemPrincipal ;
}
2012-07-19 08:25:19 +04:00
NS_IMETHODIMP
2012-08-22 19:56:38 +04:00
FullTrustSecMan : : GetExtendedOrigin ( nsIURI * aURI , uint32_t aAppId ,
2012-07-19 08:25:19 +04:00
bool aInMozBrowser ,
nsACString & aExtendedOrigin )
{
aExtendedOrigin . Truncate ( ) ;
return NS_OK ;
}
2000-02-19 03:14:44 +03:00
/***************************************************************************/
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
// #define TEST_InitClassesWithNewWrappedGlobal
# ifdef TEST_InitClassesWithNewWrappedGlobal
// XXX hacky test code...
# include "xpctest.h"
class TestGlobal : public nsIXPCTestNoisy , public nsIXPCScriptable
{
public :
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCTESTNOISY
NS_DECL_NSIXPCSCRIPTABLE
2003-01-09 00:07:51 +03:00
TestGlobal ( ) { }
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
} ;
NS_IMPL_ISUPPORTS2 ( TestGlobal , nsIXPCTestNoisy , nsIXPCScriptable )
// The nsIXPCScriptable map declaration that will generate stubs for us...
# define XPC_MAP_CLASSNAME TestGlobal
# define XPC_MAP_QUOTED_CLASSNAME "TestGlobal"
# define XPC_MAP_FLAGS nsIXPCScriptable::USE_JSSTUB_FOR_ADDPROPERTY |\
nsIXPCScriptable : : USE_JSSTUB_FOR_DELPROPERTY | \
nsIXPCScriptable : : USE_JSSTUB_FOR_SETPROPERTY
# include "xpc_map_end.h" /* This will #undef the above */
NS_IMETHODIMP TestGlobal : : Squawk ( ) { return NS_OK ; }
# endif
// uncomment to install the test 'this' translator
// #define TEST_TranslateThis
# ifdef TEST_TranslateThis
# include "xpctest.h"
class nsXPCFunctionThisTranslator : public nsIXPCFunctionThisTranslator
{
public :
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCFUNCTIONTHISTRANSLATOR
nsXPCFunctionThisTranslator ( ) ;
virtual ~ nsXPCFunctionThisTranslator ( ) ;
/* additional members */
} ;
/* Implementation file */
NS_IMPL_ISUPPORTS1 ( nsXPCFunctionThisTranslator , nsIXPCFunctionThisTranslator )
nsXPCFunctionThisTranslator : : nsXPCFunctionThisTranslator ( )
{
/* member initializers and constructor code */
}
nsXPCFunctionThisTranslator : : ~ nsXPCFunctionThisTranslator ( )
{
/* destructor code */
# ifdef DEBUG_jband
printf ( " destroying nsXPCFunctionThisTranslator \n " ) ;
# endif
}
2012-08-22 19:56:38 +04:00
/* nsISupports TranslateThis (in nsISupports aInitialThis, in nsIInterfaceInfo aInterfaceInfo, in uint16_t aMethodIndex, out bool aHideFirstParamFromJS, out nsIIDPtr aIIDOfResult); */
2009-06-29 03:01:42 +04:00
NS_IMETHODIMP
nsXPCFunctionThisTranslator : : TranslateThis ( nsISupports * aInitialThis ,
nsIInterfaceInfo * aInterfaceInfo ,
2012-08-22 19:56:38 +04:00
uint16_t aMethodIndex ,
2011-09-29 10:19:26 +04:00
bool * aHideFirstParamFromJS ,
2009-06-29 03:01:42 +04:00
nsIID * * aIIDOfResult ,
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
nsISupports * * _retval )
{
NS_IF_ADDREF ( aInitialThis ) ;
* _retval = aInitialThis ;
2011-11-26 14:05:59 +04:00
* aHideFirstParamFromJS = false ;
2012-07-30 18:20:58 +04:00
* aIIDOfResult = nullptr ;
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
return NS_OK ;
}
# endif
2008-10-11 21:35:39 +04:00
// ContextCallback calls are chained
static JSContextCallback gOldJSContextCallback ;
2008-09-07 02:21:43 +04:00
static JSBool
2012-02-29 03:11:11 +04:00
ContextCallback ( JSContext * cx , unsigned contextOp )
2008-04-09 11:27:16 +04:00
{
2008-10-11 21:35:39 +04:00
if ( gOldJSContextCallback & & ! gOldJSContextCallback ( cx , contextOp ) )
2011-11-26 14:05:59 +04:00
return false ;
2008-10-11 21:35:39 +04:00
2008-04-09 11:27:16 +04:00
if ( contextOp = = JSCONTEXT_NEW ) {
JS_SetErrorReporter ( cx , my_ErrorReporter ) ;
JS_SetVersion ( cx , JSVERSION_LATEST ) ;
}
2011-11-26 14:05:59 +04:00
return true ;
2008-04-09 11:27:16 +04:00
}
2009-01-18 20:01:15 +03:00
static bool
GetCurrentWorkingDirectory ( nsAString & workingDirectory )
{
2011-04-09 23:17:53 +04:00
# if !defined(XP_WIN) && !defined(XP_UNIX)
2009-01-18 20:01:15 +03:00
//XXX: your platform should really implement this
return false ;
2009-01-22 18:17:22 +03:00
# elif XP_WIN
2009-01-18 20:01:15 +03:00
DWORD requiredLength = GetCurrentDirectoryW ( 0 , NULL ) ;
workingDirectory . SetLength ( requiredLength ) ;
GetCurrentDirectoryW ( workingDirectory . Length ( ) ,
( LPWSTR ) workingDirectory . BeginWriting ( ) ) ;
// we got a trailing null there
workingDirectory . SetLength ( requiredLength ) ;
workingDirectory . Replace ( workingDirectory . Length ( ) - 1 , 1 , L ' \\ ' ) ;
# elif defined(XP_UNIX)
2012-09-02 06:35:17 +04:00
nsAutoCString cwd ;
2009-01-18 20:01:15 +03:00
// 1024 is just a guess at a sane starting value
size_t bufsize = 1024 ;
2012-07-30 18:20:58 +04:00
char * result = nullptr ;
while ( result = = nullptr ) {
2009-01-18 20:01:15 +03:00
if ( ! cwd . SetLength ( bufsize ) )
return false ;
result = getcwd ( cwd . BeginWriting ( ) , cwd . Length ( ) ) ;
if ( ! result ) {
if ( errno ! = ERANGE )
return false ;
// need to make the buffer bigger
bufsize * = 2 ;
}
}
// size back down to the actual string length
cwd . SetLength ( strlen ( result ) + 1 ) ;
cwd . Replace ( cwd . Length ( ) - 1 , 1 , ' / ' ) ;
workingDirectory = NS_ConvertUTF8toUTF16 ( cwd ) ;
# endif
return true ;
}
2012-03-09 13:48:50 +04:00
static JSSecurityCallbacks shellSecurityCallbacks ;
1999-04-10 10:51:01 +04:00
int
2003-06-14 01:25:10 +04:00
main ( int argc , char * * argv , char * * envp )
1999-04-10 10:51:01 +04:00
{
2008-06-03 14:56:09 +04:00
# ifdef XP_MACOSX
InitAutoreleasePool ( ) ;
# endif
1999-04-10 10:51:01 +04:00
JSRuntime * rt ;
2003-06-14 01:25:10 +04:00
JSContext * cx ;
JSObject * glob , * envobj ;
1999-04-10 10:51:01 +04:00
int result ;
1999-10-09 10:01:57 +04:00
nsresult rv ;
1999-04-10 10:51:01 +04:00
2009-01-17 01:10:48 +03:00
# ifdef HAVE_SETBUF
2007-02-14 00:16:09 +03:00
// unbuffer stdout so that output is in the correct order; note that stderr
// is unbuffered by default
setbuf ( stdout , 0 ) ;
2009-01-17 01:10:48 +03:00
# endif
2007-02-14 00:16:09 +03:00
2012-02-23 03:05:28 +04:00
# ifdef XRE_HAS_DLL_BLOCKLIST
XRE_SetupDllBlocklist ( ) ;
# endif
1999-04-10 10:51:01 +04:00
gErrFile = stderr ;
gOutFile = stdout ;
2008-11-28 05:25:35 +03:00
gInFile = stdin ;
2009-01-20 22:56:44 +03:00
NS_LogInit ( ) ;
2012-06-06 06:08:30 +04:00
nsCOMPtr < nsIFile > appFile ;
2009-01-20 22:56:44 +03:00
rv = XRE_GetBinaryPath ( argv [ 0 ] , getter_AddRefs ( appFile ) ) ;
if ( NS_FAILED ( rv ) ) {
2009-05-12 05:38:31 +04:00
printf ( " Couldn't find application file. \n " ) ;
2009-01-20 22:56:44 +03:00
return 1 ;
}
nsCOMPtr < nsIFile > appDir ;
rv = appFile - > GetParent ( getter_AddRefs ( appDir ) ) ;
if ( NS_FAILED ( rv ) ) {
printf ( " Couldn't get application directory. \n " ) ;
return 1 ;
}
XPCShellDirProvider dirprovider ;
2012-05-22 18:50:04 +04:00
dirprovider . SetAppFile ( appFile ) ;
2009-01-20 22:56:44 +03:00
if ( argc > 1 & & ! strcmp ( argv [ 1 ] , " -g " ) ) {
if ( argc < 3 )
return usage ( ) ;
if ( ! dirprovider . SetGREDir ( argv [ 2 ] ) ) {
printf ( " SetGREDir failed. \n " ) ;
return 1 ;
}
argc - = 2 ;
argv + = 2 ;
}
2011-05-06 12:40:51 +04:00
if ( argc > 1 & & ! strcmp ( argv [ 1 ] , " -a " ) ) {
if ( argc < 3 )
return usage ( ) ;
2012-06-06 06:08:30 +04:00
nsCOMPtr < nsIFile > dir ;
2011-05-06 12:40:51 +04:00
rv = XRE_GetFileFromPath ( argv [ 2 ] , getter_AddRefs ( dir ) ) ;
if ( NS_SUCCEEDED ( rv ) ) {
appDir = do_QueryInterface ( dir , & rv ) ;
}
if ( NS_FAILED ( rv ) ) {
printf ( " Couldn't use given appdir. \n " ) ;
return 1 ;
}
argc - = 2 ;
argv + = 2 ;
}
2010-07-22 18:42:43 +04:00
while ( argc > 1 & & ! strcmp ( argv [ 1 ] , " -r " ) ) {
if ( argc < 3 )
return usage ( ) ;
2012-06-06 06:08:30 +04:00
nsCOMPtr < nsIFile > lf ;
2010-07-22 18:42:43 +04:00
rv = XRE_GetFileFromPath ( argv [ 2 ] , getter_AddRefs ( lf ) ) ;
if ( NS_FAILED ( rv ) ) {
printf ( " Couldn't get manifest file. \n " ) ;
return 1 ;
}
XRE_AddManifestLocation ( NS_COMPONENT_LOCATION , lf ) ;
argc - = 2 ;
argv + = 2 ;
}
2002-07-04 18:29:25 +04:00
{
2011-06-27 06:05:51 +04:00
if ( argc > 1 & & ! strcmp ( argv [ 1 ] , " --greomni " ) ) {
2012-06-06 06:08:30 +04:00
nsCOMPtr < nsIFile > greOmni ;
nsCOMPtr < nsIFile > appOmni ;
2011-06-27 06:05:51 +04:00
XRE_GetFileFromPath ( argv [ 2 ] , getter_AddRefs ( greOmni ) ) ;
if ( argc > 3 & & ! strcmp ( argv [ 3 ] , " --appomni " ) ) {
XRE_GetFileFromPath ( argv [ 4 ] , getter_AddRefs ( appOmni ) ) ;
argc - = 2 ;
argv + = 2 ;
2011-08-22 12:00:34 +04:00
} else {
appOmni = greOmni ;
}
2011-10-14 21:52:47 +04:00
2011-06-27 06:05:51 +04:00
XRE_InitOmnijar ( greOmni , appOmni ) ;
argc - = 2 ;
argv + = 2 ;
}
2002-07-04 18:29:25 +04:00
nsCOMPtr < nsIServiceManager > servMan ;
2009-01-20 22:56:44 +03:00
rv = NS_InitXPCOM2 ( getter_AddRefs ( servMan ) , appDir , & dirprovider ) ;
2002-07-04 18:29:25 +04:00
if ( NS_FAILED ( rv ) ) {
2009-08-20 19:35:30 +04:00
printf ( " NS_InitXPCOM2 failed! \n " ) ;
2002-07-04 18:29:25 +04:00
return 1 ;
}
1999-04-10 10:51:01 +04:00
2002-07-04 18:29:25 +04:00
nsCOMPtr < nsIJSRuntimeService > rtsvc = do_GetService ( " @mozilla.org/js/xpc/RuntimeService;1 " ) ;
// get the JSRuntime from the runtime svc
if ( ! rtsvc ) {
printf ( " failed to get nsJSRuntimeService! \n " ) ;
return 1 ;
}
2009-06-29 03:01:42 +04:00
2002-07-04 18:29:25 +04:00
if ( NS_FAILED ( rtsvc - > GetRuntime ( & rt ) ) | | ! rt ) {
printf ( " failed to get JSRuntime from nsJSRuntimeService! \n " ) ;
return 1 ;
}
1999-10-15 07:05:48 +04:00
2008-10-11 21:35:39 +04:00
gOldJSContextCallback = JS_SetContextCallback ( rt , ContextCallback ) ;
2008-04-09 11:27:16 +04:00
2003-06-14 01:25:10 +04:00
cx = JS_NewContext ( rt , 8192 ) ;
if ( ! cx ) {
2002-07-04 18:29:25 +04:00
printf ( " JS_NewContext failed! \n " ) ;
return 1 ;
}
1999-04-10 10:51:01 +04:00
2012-05-31 00:05:59 +04:00
JS_SetOptions ( cx , JS_GetOptions ( cx ) | JSOPTION_ALLOW_XML ) ;
2010-12-06 23:45:00 +03:00
xpc_LocalizeContext ( cx ) ;
2002-07-04 18:29:25 +04:00
nsCOMPtr < nsIXPConnect > xpc = do_GetService ( nsIXPConnect : : GetCID ( ) ) ;
if ( ! xpc ) {
printf ( " failed to get nsXPConnect service! \n " ) ;
return 1 ;
}
1999-04-10 10:51:01 +04:00
2002-07-04 18:29:25 +04:00
// Since the caps security system might set a default security manager
// we will be sure that the secman on this context gives full trust.
2007-06-21 04:10:48 +04:00
nsRefPtr < FullTrustSecMan > secman = new FullTrustSecMan ( ) ;
xpc - > SetSecurityManagerForJSContext ( cx , secman , 0xFFFF ) ;
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
2010-07-20 00:36:49 +04:00
nsCOMPtr < nsIPrincipal > systemprincipal ;
2004-11-23 08:38:59 +03:00
// Fetch the system principal and store it away in a global, to use for
// script compilation in Load() and ProcessFile() (including interactive
// eval loop)
{
nsCOMPtr < nsIScriptSecurityManager > securityManager =
do_GetService ( NS_SCRIPTSECURITYMANAGER_CONTRACTID , & rv ) ;
if ( NS_SUCCEEDED ( rv ) & & securityManager ) {
2010-07-20 00:36:49 +04:00
rv = securityManager - > GetSystemPrincipal ( getter_AddRefs ( systemprincipal ) ) ;
2004-11-23 08:38:59 +03:00
if ( NS_FAILED ( rv ) ) {
fprintf ( gErrFile , " +++ Failed to obtain SystemPrincipal from ScriptSecurityManager service. \n " ) ;
} else {
// fetch the JS principals and stick in a global
2012-03-09 13:48:50 +04:00
gJSPrincipals = nsJSPrincipals : : get ( systemprincipal ) ;
JS_HoldPrincipals ( gJSPrincipals ) ;
2010-07-20 00:36:49 +04:00
secman - > SetSystemPrincipal ( systemprincipal ) ;
2004-11-23 08:38:59 +03:00
}
} else {
fprintf ( gErrFile , " +++ Failed to get ScriptSecurityManager service, running without principals " ) ;
}
}
2012-03-09 13:48:50 +04:00
const JSSecurityCallbacks * scb = JS_GetSecurityCallbacks ( rt ) ;
NS_ASSERTION ( scb , " We are assuming that nsScriptSecurityManager::Init() has been run " ) ;
shellSecurityCallbacks = * scb ;
JS_SetSecurityCallbacks ( rt , & shellSecurityCallbacks ) ;
2011-05-12 23:12:50 +04:00
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
# ifdef TEST_TranslateThis
2002-07-04 18:29:25 +04:00
nsCOMPtr < nsIXPCFunctionThisTranslator >
translator ( new nsXPCFunctionThisTranslator ) ;
2012-07-30 18:20:58 +04:00
xpc - > SetFunctionThisTranslator ( NS_GET_IID ( nsITestXPCFunctionCallback ) , translator , nullptr ) ;
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
# endif
2009-06-29 03:01:42 +04:00
2002-07-04 18:29:25 +04:00
nsCOMPtr < nsIJSContextStack > cxstack = do_GetService ( " @mozilla.org/js/xpc/ContextStack;1 " ) ;
if ( ! cxstack ) {
printf ( " failed to get the nsThreadJSContextStack service! \n " ) ;
return 1 ;
}
2000-03-28 07:45:12 +04:00
2011-10-14 21:52:48 +04:00
if ( NS_FAILED ( cxstack - > Push ( cx ) ) ) {
2002-07-04 18:29:25 +04:00
printf ( " failed to push the current JSContext on the nsThreadJSContextStack! \n " ) ;
return 1 ;
}
1999-07-15 09:42:53 +04:00
2004-12-07 21:13:02 +03:00
nsCOMPtr < nsIXPCScriptable > backstagePass ;
nsresult rv = rtsvc - > GetBackstagePass ( getter_AddRefs ( backstagePass ) ) ;
if ( NS_FAILED ( rv ) ) {
fprintf ( gErrFile , " +++ Failed to get backstage pass from rtsvc: %8x \n " ,
rv ) ;
2002-07-04 18:29:25 +04:00
return 1 ;
2004-12-07 21:13:02 +03:00
}
nsCOMPtr < nsIXPConnectJSObjectHolder > holder ;
rv = xpc - > InitClassesWithNewWrappedGlobal ( cx , backstagePass ,
2010-07-20 00:36:49 +04:00
systemprincipal ,
2012-09-05 22:32:07 +04:00
0 ,
2004-12-07 21:13:02 +03:00
getter_AddRefs ( holder ) ) ;
if ( NS_FAILED ( rv ) )
2003-06-14 01:25:10 +04:00
return 1 ;
2009-06-29 03:01:42 +04:00
2004-12-07 21:13:02 +03:00
rv = holder - > GetJSObject ( & glob ) ;
if ( NS_FAILED ( rv ) ) {
2012-07-30 18:20:58 +04:00
NS_ASSERTION ( glob = = nullptr , " bad GetJSObject? " ) ;
2002-07-04 18:29:25 +04:00
return 1 ;
2004-12-07 21:13:02 +03:00
}
2006-06-13 02:39:55 +04:00
JS_BeginRequest ( cx ) ;
2010-09-09 03:09:23 +04:00
{
2012-08-22 05:42:53 +04:00
JSAutoCompartment ac ( cx , glob ) ;
2011-09-10 02:11:00 +04:00
if ( ! JS_InitReflect ( cx , glob ) ) {
JS_EndRequest ( cx ) ;
2010-09-09 03:09:23 +04:00
return 1 ;
}
2006-06-13 02:39:55 +04:00
2011-01-15 04:58:40 +03:00
if ( ! JS_DefineFunctions ( cx , glob , glob_functions ) | |
! JS_DefineProfilingFunctions ( cx , glob ) ) {
2010-09-09 03:09:23 +04:00
JS_EndRequest ( cx ) ;
return 1 ;
}
2003-06-14 01:25:10 +04:00
2010-09-09 03:09:23 +04:00
envobj = JS_DefineObject ( cx , glob , " environment " , & env_class , NULL , 0 ) ;
2012-02-06 00:07:23 +04:00
if ( ! envobj ) {
2010-09-09 03:09:23 +04:00
JS_EndRequest ( cx ) ;
return 1 ;
}
1999-04-10 10:51:01 +04:00
2012-02-06 00:07:23 +04:00
JS_SetPrivate ( envobj , envp ) ;
2010-09-09 03:09:23 +04:00
nsAutoString workingDirectory ;
if ( GetCurrentWorkingDirectory ( workingDirectory ) )
gWorkingDirectory = & workingDirectory ;
2009-01-18 20:01:15 +03:00
2010-09-09 03:09:23 +04:00
JS_DefineProperty ( cx , glob , " __LOCATION__ " , JSVAL_VOID ,
GetLocationProperty , NULL , 0 ) ;
2009-01-18 20:01:15 +03:00
2010-09-09 03:09:23 +04:00
argc - - ;
argv + + ;
1999-04-10 10:51:01 +04:00
2010-09-09 03:09:23 +04:00
result = ProcessArgs ( cx , glob , argv , argc ) ;
1999-04-10 10:51:01 +04:00
2000-07-19 10:15:32 +04:00
//#define TEST_CALL_ON_WRAPPED_JS_AFTER_SHUTDOWN 1
# ifdef TEST_CALL_ON_WRAPPED_JS_AFTER_SHUTDOWN
2010-09-09 03:09:23 +04:00
// test of late call and release (see below)
nsCOMPtr < nsIJSContextStack > bogus ;
xpc - > WrapJS ( cx , glob , NS_GET_IID ( nsIJSContextStack ) ,
( void * * ) getter_AddRefs ( bogus ) ) ;
2000-07-19 10:15:32 +04:00
# endif
2012-03-09 13:48:50 +04:00
JS_DropPrincipals ( rt , gJSPrincipals ) ;
2012-08-23 22:23:28 +04:00
JS_SetAllNonReservedSlotsToUndefined ( cx , glob ) ;
2012-03-28 14:13:30 +04:00
JS_GC ( rt ) ;
2010-09-09 03:09:23 +04:00
JSContext * oldcx ;
cxstack - > Pop ( & oldcx ) ;
NS_ASSERTION ( oldcx = = cx , " JS thread context push/pop mismatch " ) ;
2012-07-30 18:20:58 +04:00
cxstack = nullptr ;
2012-03-28 14:13:30 +04:00
JS_GC ( rt ) ;
2010-09-09 03:09:23 +04:00
} //this scopes the JSAutoCrossCompartmentCall
2012-01-30 14:15:13 +04:00
JS_EndRequest ( cx ) ;
2003-06-14 01:25:10 +04:00
JS_DestroyContext ( cx ) ;
2002-07-04 18:29:25 +04:00
} // this scopes the nsCOMPtrs
2009-08-20 19:35:30 +04:00
2009-11-11 11:34:08 +03:00
if ( ! XRE_ShutdownTestShell ( ) )
NS_ERROR ( " problem shutting down testshell " ) ;
2009-08-20 19:35:30 +04:00
# ifdef MOZ_CRASHREPORTER
// Get the crashreporter service while XPCOM is still active.
// This is a special exception: it will remain usable after NS_ShutdownXPCOM().
nsCOMPtr < nsICrashReporter > crashReporter =
do_GetService ( " @mozilla.org/toolkit/crash-reporter;1 " ) ;
# endif
2002-07-04 18:29:25 +04:00
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
1999-10-09 10:01:57 +04:00
rv = NS_ShutdownXPCOM ( NULL ) ;
NS_ASSERTION ( NS_SUCCEEDED ( rv ) , " NS_ShutdownXPCOM failed " ) ;
2000-07-19 10:15:32 +04:00
# ifdef TEST_CALL_ON_WRAPPED_JS_AFTER_SHUTDOWN
// test of late call and release (see above)
JSContext * bogusCX ;
bogus - > Peek ( & bogusCX ) ;
2012-07-30 18:20:58 +04:00
bogus = nullptr ;
2000-07-19 10:15:32 +04:00
# endif
2012-07-30 18:20:58 +04:00
appDir = nullptr ;
appFile = nullptr ;
2009-01-20 22:56:44 +03:00
dirprovider . ClearGREDir ( ) ;
2012-05-22 18:50:04 +04:00
dirprovider . ClearAppFile ( ) ;
2009-01-20 22:56:44 +03:00
2009-08-20 19:35:30 +04:00
# ifdef MOZ_CRASHREPORTER
// Shut down the crashreporter service to prevent leaking some strings it holds.
if ( crashReporter ) {
2011-10-17 18:59:28 +04:00
crashReporter - > SetEnabled ( false ) ;
2012-07-30 18:20:58 +04:00
crashReporter = nullptr ;
2009-08-20 19:35:30 +04:00
}
# endif
2009-01-20 22:56:44 +03:00
NS_LogTerm ( ) ;
2008-06-03 14:56:09 +04:00
# ifdef XP_MACOSX
FinishAutoreleasePool ( ) ;
# endif
1999-04-10 10:51:01 +04:00
return result ;
}
2009-01-20 22:56:44 +03:00
2011-09-29 10:19:26 +04:00
bool
2009-01-20 22:56:44 +03:00
XPCShellDirProvider : : SetGREDir ( const char * dir )
{
nsresult rv = XRE_GetFileFromPath ( dir , getter_AddRefs ( mGREDir ) ) ;
return NS_SUCCEEDED ( rv ) ;
}
2012-05-22 18:50:04 +04:00
void
2012-06-06 06:08:30 +04:00
XPCShellDirProvider : : SetAppFile ( nsIFile * appFile )
2012-05-22 18:50:04 +04:00
{
mAppFile = appFile ;
}
2009-01-20 22:56:44 +03:00
NS_IMETHODIMP_ ( nsrefcnt )
XPCShellDirProvider : : AddRef ( )
{
return 2 ;
}
NS_IMETHODIMP_ ( nsrefcnt )
XPCShellDirProvider : : Release ( )
{
return 1 ;
}
2009-07-17 18:41:26 +04:00
NS_IMPL_QUERY_INTERFACE2 ( XPCShellDirProvider ,
nsIDirectoryServiceProvider ,
nsIDirectoryServiceProvider2 )
2009-01-20 22:56:44 +03:00
NS_IMETHODIMP
2011-09-29 10:19:26 +04:00
XPCShellDirProvider : : GetFile ( const char * prop , bool * persistent ,
2009-01-20 22:56:44 +03:00
nsIFile * * result )
{
if ( mGREDir & & ! strcmp ( prop , NS_GRE_DIR ) ) {
2011-10-17 18:59:28 +04:00
* persistent = true ;
2011-07-21 11:57:07 +04:00
return mGREDir - > Clone ( result ) ;
2012-05-22 18:50:04 +04:00
} else if ( mAppFile & & ! strcmp ( prop , XRE_EXECUTABLE_FILE ) ) {
* persistent = true ;
return mAppFile - > Clone ( result ) ;
2011-07-21 11:57:07 +04:00
} else if ( mGREDir & & ! strcmp ( prop , NS_APP_PREF_DEFAULTS_50_DIR ) ) {
nsCOMPtr < nsIFile > file ;
2011-10-17 18:59:28 +04:00
* persistent = true ;
2011-07-21 11:57:07 +04:00
if ( NS_FAILED ( mGREDir - > Clone ( getter_AddRefs ( file ) ) ) | |
NS_FAILED ( file - > AppendNative ( NS_LITERAL_CSTRING ( " defaults " ) ) ) | |
NS_FAILED ( file - > AppendNative ( NS_LITERAL_CSTRING ( " pref " ) ) ) )
return NS_ERROR_FAILURE ;
NS_ADDREF ( * result = file ) ;
2009-01-20 22:56:44 +03:00
return NS_OK ;
2012-05-22 18:50:04 +04:00
} else if ( mAppFile & & ! strcmp ( prop , XRE_UPDATE_ROOT_DIR ) ) {
// For xpcshell, we pretend that the update root directory is always
// the same as the GRE directory, except for Windows, where we immitate
// the algorithm defined in nsXREDirProvider::GetUpdateRootDir.
* persistent = true ;
# ifdef XP_WIN
char appData [ MAX_PATH ] = { ' \0 ' } ;
char path [ MAX_PATH ] = { ' \0 ' } ;
LPITEMIDLIST pItemIDList ;
if ( FAILED ( SHGetSpecialFolderLocation ( NULL , CSIDL_LOCAL_APPDATA , & pItemIDList ) ) | |
FAILED ( SHGetPathFromIDListA ( pItemIDList , appData ) ) ) {
return NS_ERROR_FAILURE ;
}
nsAutoString pathName ;
2012-06-29 10:24:33 +04:00
pathName . AssignASCII ( appData ) ;
2012-06-06 06:08:30 +04:00
nsCOMPtr < nsIFile > localFile ;
2012-05-22 18:50:04 +04:00
nsresult rv = NS_NewLocalFile ( pathName , true , getter_AddRefs ( localFile ) ) ;
if ( NS_FAILED ( rv ) ) {
return rv ;
}
2012-06-29 10:24:33 +04:00
# ifdef MOZ_APP_PROFILE
localFile - > AppendNative ( NS_LITERAL_CSTRING ( MOZ_APP_PROFILE ) ) ;
# else
// MOZ_APP_VENDOR and MOZ_APP_BASENAME are optional.
# ifdef MOZ_APP_VENDOR
localFile - > AppendNative ( NS_LITERAL_CSTRING ( MOZ_APP_VENDOR ) ) ;
# endif
# ifdef MOZ_APP_BASENAME
localFile - > AppendNative ( NS_LITERAL_CSTRING ( MOZ_APP_BASENAME ) ) ;
# endif
// However app name is always appended.
localFile - > AppendNative ( NS_LITERAL_CSTRING ( MOZ_APP_NAME ) ) ;
# endif
2012-05-22 18:50:04 +04:00
return localFile - > Clone ( result ) ;
# else
// Fail on non-Windows platforms, the caller is supposed to fal back on
// the app dir.
return NS_ERROR_FAILURE ;
# endif
2009-01-20 22:56:44 +03:00
}
return NS_ERROR_FAILURE ;
}
2009-07-17 18:41:26 +04:00
NS_IMETHODIMP
XPCShellDirProvider : : GetFiles ( const char * prop , nsISimpleEnumerator * * result )
{
if ( mGREDir & & ! strcmp ( prop , " ChromeML " ) ) {
nsCOMArray < nsIFile > dirs ;
nsCOMPtr < nsIFile > file ;
mGREDir - > Clone ( getter_AddRefs ( file ) ) ;
file - > AppendNative ( NS_LITERAL_CSTRING ( " chrome " ) ) ;
dirs . AppendObject ( file ) ;
nsresult rv = NS_GetSpecialDirectory ( NS_APP_CHROME_DIR ,
getter_AddRefs ( file ) ) ;
if ( NS_SUCCEEDED ( rv ) )
dirs . AppendObject ( file ) ;
return NS_NewArrayEnumerator ( result , dirs ) ;
2011-07-21 11:57:07 +04:00
} else if ( ! strcmp ( prop , NS_APP_PREFS_DEFAULTS_DIR_LIST ) ) {
nsCOMArray < nsIFile > dirs ;
nsCOMPtr < nsIFile > file ;
if ( NS_FAILED ( NS_GetSpecialDirectory ( NS_XPCOM_CURRENT_PROCESS_DIR ,
getter_AddRefs ( file ) ) ) | |
NS_FAILED ( file - > AppendNative ( NS_LITERAL_CSTRING ( " defaults " ) ) ) | |
NS_FAILED ( file - > AppendNative ( NS_LITERAL_CSTRING ( " preferences " ) ) ) )
return NS_ERROR_FAILURE ;
dirs . AppendObject ( file ) ;
return NS_NewArrayEnumerator ( result , dirs ) ;
2009-07-17 18:41:26 +04:00
}
return NS_ERROR_FAILURE ;
}