2013-07-17 07:38:45 +04:00
|
|
|
/* vim: set ts=8 sts=4 et sw=4 tw=80:
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2009-07-11 10:33:10 +04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
#include <io.h> /* for isatty() */
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h> /* for isatty() */
|
|
|
|
#endif
|
|
|
|
|
2010-03-08 23:16:41 +03:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
2009-07-11 10:33:10 +04:00
|
|
|
#include "jsapi.h"
|
2011-10-04 18:06:54 +04:00
|
|
|
#include "jsdbgapi.h"
|
2009-07-11 10:33:10 +04:00
|
|
|
#include "jsprf.h"
|
|
|
|
|
2011-01-15 01:03:21 +03:00
|
|
|
#include "xpcpublic.h"
|
|
|
|
|
2010-03-08 23:16:41 +03:00
|
|
|
#include "XPCShellEnvironment.h"
|
|
|
|
|
2009-07-11 10:33:10 +04:00
|
|
|
#include "mozilla/XPCOM.h"
|
|
|
|
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIClassInfo.h"
|
|
|
|
#include "nsIDirectoryService.h"
|
|
|
|
#include "nsIJSRuntimeService.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsIXPCScriptable.h"
|
|
|
|
|
2013-04-18 19:36:05 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-22 20:05:26 +04:00
|
|
|
#include "nsCxPusher.h"
|
2009-07-30 01:12:15 +04:00
|
|
|
#include "nsJSUtils.h"
|
2012-03-09 13:48:50 +04:00
|
|
|
#include "nsJSPrincipals.h"
|
2009-08-26 03:07:22 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2009-07-11 10:33:10 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
2013-04-04 13:27:38 +04:00
|
|
|
#include "BackstagePass.h"
|
|
|
|
|
2009-07-24 02:27:17 +04:00
|
|
|
#include "TestShellChild.h"
|
2009-07-11 10:33:10 +04:00
|
|
|
#include "TestShellParent.h"
|
|
|
|
|
|
|
|
using mozilla::ipc::XPCShellEnvironment;
|
2009-07-24 02:27:17 +04:00
|
|
|
using mozilla::ipc::TestShellChild;
|
2009-07-11 10:33:10 +04:00
|
|
|
using mozilla::ipc::TestShellParent;
|
2013-07-17 07:38:45 +04:00
|
|
|
using mozilla::AutoSafeJSContext;
|
|
|
|
using namespace JS;
|
2009-07-11 10:33:10 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-08-29 03:16:19 +04:00
|
|
|
static const char kDefaultRuntimeScriptFilename[] = "xpcshell.js";
|
2009-07-11 10:33:10 +04:00
|
|
|
|
|
|
|
class XPCShellDirProvider : public nsIDirectoryServiceProvider
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIDIRECTORYSERVICEPROVIDER
|
|
|
|
|
|
|
|
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; }
|
2009-07-11 10:33:10 +04:00
|
|
|
|
|
|
|
private:
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> mGREDir;
|
2009-07-11 10:33:10 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
inline XPCShellEnvironment*
|
2013-08-08 03:13:34 +04:00
|
|
|
Environment(Handle<JSObject*> global)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2013-07-17 07:38:45 +04:00
|
|
|
AutoSafeJSContext cx;
|
|
|
|
JSAutoCompartment ac(cx, global);
|
|
|
|
Rooted<Value> v(cx);
|
2013-07-26 13:00:38 +04:00
|
|
|
if (!JS_GetProperty(cx, global, "__XPCShellEnvironment", &v) ||
|
2013-07-17 07:38:45 +04:00
|
|
|
!v.get().isDouble())
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return static_cast<XPCShellEnvironment*>(v.get().toPrivate());
|
2013-07-17 05:16:31 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
static bool
|
2009-07-11 10:33:10 +04:00
|
|
|
Print(JSContext *cx,
|
2012-02-29 03:11:11 +04:00
|
|
|
unsigned argc,
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value *vp)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2012-02-29 03:11:11 +04:00
|
|
|
unsigned i, n;
|
2009-07-11 10:33:10 +04:00
|
|
|
JSString *str;
|
|
|
|
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value *argv = JS_ARGV(cx, vp);
|
2009-07-11 10:33:10 +04:00
|
|
|
for (i = n = 0; i < argc; i++) {
|
|
|
|
str = JS_ValueToString(cx, argv[i]);
|
|
|
|
if (!str)
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2010-11-11 23:40:29 +03:00
|
|
|
JSAutoByteString bytes(cx, str);
|
|
|
|
if (!bytes)
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2010-11-11 23:40:29 +03:00
|
|
|
fprintf(stdout, "%s%s", i ? " " : "", bytes.ptr());
|
2009-07-11 10:33:10 +04:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
n++;
|
|
|
|
if (n)
|
|
|
|
fputc('\n', stdout);
|
2010-08-16 23:35:04 +04:00
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_VOID);
|
2013-08-02 11:41:57 +04:00
|
|
|
return true;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
GetLine(char *bufp,
|
|
|
|
FILE *file,
|
|
|
|
const char *prompt)
|
|
|
|
{
|
|
|
|
char line[256];
|
2010-07-20 00:36:49 +04:00
|
|
|
fputs(prompt, stdout);
|
2009-07-11 10:33:10 +04:00
|
|
|
fflush(stdout);
|
|
|
|
if (!fgets(line, sizeof line, file))
|
2013-08-07 10:59:54 +04:00
|
|
|
return false;
|
2009-07-11 10:33:10 +04:00
|
|
|
strcpy(bufp, line);
|
2013-08-07 10:59:54 +04:00
|
|
|
return true;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
static bool
|
2009-07-11 10:33:10 +04:00
|
|
|
Dump(JSContext *cx,
|
2012-02-29 03:11:11 +04:00
|
|
|
unsigned argc,
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value *vp)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2010-08-16 23:35:04 +04:00
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_VOID);
|
|
|
|
|
2009-07-11 10:33:10 +04:00
|
|
|
JSString *str;
|
|
|
|
if (!argc)
|
2013-08-02 11:41:57 +04:00
|
|
|
return true;
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2010-08-16 23:35:04 +04:00
|
|
|
str = JS_ValueToString(cx, JS_ARGV(cx, vp)[0]);
|
2009-07-11 10:33:10 +04:00
|
|
|
if (!str)
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2011-06-02 15:11:54 +04:00
|
|
|
JSAutoByteString bytes(cx, str);
|
|
|
|
if (!bytes)
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2011-06-02 15:11:54 +04:00
|
|
|
|
|
|
|
fputs(bytes.ptr(), stdout);
|
2009-07-11 10:33:10 +04:00
|
|
|
fflush(stdout);
|
2013-08-02 11:41:57 +04:00
|
|
|
return true;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
static bool
|
2009-07-11 10:33:10 +04:00
|
|
|
Load(JSContext *cx,
|
2012-02-29 03:11:11 +04:00
|
|
|
unsigned argc,
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value *vp)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2013-05-04 01:49:47 +04:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2013-05-04 01:49:47 +04:00
|
|
|
JS::Rooted<JSObject*> obj(cx, JS_THIS_OBJECT(cx, vp));
|
2010-08-16 23:35:04 +04:00
|
|
|
if (!obj)
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2010-08-16 23:35:04 +04:00
|
|
|
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value *argv = JS_ARGV(cx, vp);
|
2012-12-02 12:54:55 +04:00
|
|
|
for (unsigned i = 0; i < argc; i++) {
|
|
|
|
JSString *str = JS_ValueToString(cx, argv[i]);
|
2009-07-11 10:33:10 +04:00
|
|
|
if (!str)
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2009-07-11 10:33:10 +04:00
|
|
|
argv[i] = STRING_TO_JSVAL(str);
|
2010-11-11 23:40:29 +03:00
|
|
|
JSAutoByteString filename(cx, str);
|
|
|
|
if (!filename)
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2012-12-02 12:54:55 +04:00
|
|
|
FILE *file = fopen(filename.ptr(), "r");
|
2009-07-11 10:33:10 +04:00
|
|
|
if (!file) {
|
2010-11-11 23:40:29 +03:00
|
|
|
JS_ReportError(cx, "cannot open file '%s' for reading", filename.ptr());
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
2013-08-08 03:13:34 +04:00
|
|
|
Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
|
2012-11-07 05:36:47 +04:00
|
|
|
JS::CompileOptions options(cx);
|
|
|
|
options.setUTF8(true)
|
|
|
|
.setFileAndLine(filename.ptr(), 1)
|
2013-08-08 03:13:34 +04:00
|
|
|
.setPrincipals(Environment(global)->GetPrincipal());
|
2013-03-06 20:41:43 +04:00
|
|
|
JS::RootedObject rootedObj(cx, obj);
|
2012-11-07 05:36:47 +04:00
|
|
|
JSScript *script = JS::Compile(cx, rootedObj, options, file);
|
2009-07-11 10:33:10 +04:00
|
|
|
fclose(file);
|
2011-09-02 10:46:00 +04:00
|
|
|
if (!script)
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2013-07-17 07:38:45 +04:00
|
|
|
if (!JS_ExecuteScript(cx, obj, script, result.address())) {
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2011-02-03 16:06:21 +03:00
|
|
|
}
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
2010-08-16 23:35:04 +04:00
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_VOID);
|
2013-08-02 11:41:57 +04:00
|
|
|
return true;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
static bool
|
2009-07-11 10:33:10 +04:00
|
|
|
Version(JSContext *cx,
|
2012-02-29 03:11:11 +04:00
|
|
|
unsigned argc,
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value *vp)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value *argv = JS_ARGV(cx, vp);
|
2013-06-29 19:11:19 +04:00
|
|
|
JS_SET_RVAL(cx, vp, INT_TO_JSVAL(JS_GetVersion(cx)));
|
2009-07-11 10:33:10 +04:00
|
|
|
if (argc > 0 && JSVAL_IS_INT(argv[0]))
|
2013-06-29 19:11:19 +04:00
|
|
|
JS_SetVersionForCompartment(js::GetContextCompartment(cx),
|
|
|
|
JSVersion(JSVAL_TO_INT(argv[0])));
|
2013-08-02 11:41:57 +04:00
|
|
|
return true;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
static bool
|
2013-03-16 09:22:01 +04:00
|
|
|
BuildDate(JSContext *cx, unsigned argc, JS::Value *vp)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
|
|
|
fprintf(stdout, "built on %s at %s\n", __DATE__, __TIME__);
|
2013-08-02 11:41:57 +04:00
|
|
|
return true;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
static bool
|
2009-07-11 10:33:10 +04:00
|
|
|
Quit(JSContext *cx,
|
2012-02-29 03:11:11 +04:00
|
|
|
unsigned argc,
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value *vp)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2013-08-08 03:13:34 +04:00
|
|
|
Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
|
|
|
|
XPCShellEnvironment* env = Environment(global);
|
2009-07-11 10:33:10 +04:00
|
|
|
env->SetIsQuitting();
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
static bool
|
2009-07-11 10:33:10 +04:00
|
|
|
DumpXPC(JSContext *cx,
|
2012-02-29 03:11:11 +04:00
|
|
|
unsigned argc,
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value *vp)
|
2009-07-11 10:33:10 +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;
|
2009-07-11 10:33:10 +04:00
|
|
|
|
|
|
|
if (argc > 0) {
|
2010-08-16 23:35:04 +04:00
|
|
|
if (!JS_ValueToInt32(cx, JS_ARGV(cx, vp)[0], &depth))
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
|
|
|
|
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);
|
2013-08-02 11:41:57 +04:00
|
|
|
return true;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
static bool
|
2009-07-11 10:33:10 +04:00
|
|
|
GC(JSContext *cx,
|
2012-02-29 03:11:11 +04:00
|
|
|
unsigned argc,
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value *vp)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2011-09-28 00:58:14 +04:00
|
|
|
JSRuntime *rt = JS_GetRuntime(cx);
|
2012-03-28 14:13:30 +04:00
|
|
|
JS_GC(rt);
|
|
|
|
#ifdef JS_GCMETER
|
2009-07-11 10:33:10 +04:00
|
|
|
js_DumpGCStats(rt, stdout);
|
|
|
|
#endif
|
2010-08-16 23:35:04 +04:00
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_VOID);
|
2013-08-02 11:41:57 +04:00
|
|
|
return true;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
2010-08-11 21:45:00 +04:00
|
|
|
#ifdef JS_GC_ZEAL
|
2013-08-02 11:41:57 +04:00
|
|
|
static bool
|
2010-08-11 21:45:00 +04:00
|
|
|
GCZeal(JSContext *cx,
|
2012-02-29 03:11:11 +04:00
|
|
|
unsigned argc,
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value *vp)
|
2010-08-11 21:45:00 +04:00
|
|
|
{
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value* argv = JS_ARGV(cx, vp);
|
2010-08-11 21:45:00 +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 zeal;
|
2010-08-11 21:45:00 +04:00
|
|
|
if (!JS_ValueToECMAUint32(cx, argv[0], &zeal))
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2010-08-11 21:45:00 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
JS_SetGCZeal(cx, uint8_t(zeal), JS_DEFAULT_ZEAL_FREQ);
|
2013-08-02 11:41:57 +04:00
|
|
|
return true;
|
2010-08-11 21:45:00 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-07-11 10:33:10 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
static bool
|
2009-07-11 10:33:10 +04:00
|
|
|
DumpHeap(JSContext *cx,
|
2012-02-29 03:11:11 +04:00
|
|
|
unsigned argc,
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value *vp)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2010-11-11 23:40:29 +03:00
|
|
|
JSAutoByteString fileName;
|
2009-07-11 10:33:10 +04:00
|
|
|
void* startThing = NULL;
|
2011-09-01 22:43:46 +04:00
|
|
|
JSGCTraceKind startTraceKind = JSTRACE_OBJECT;
|
2009-07-11 10:33:10 +04:00
|
|
|
void *thingToFind = NULL;
|
|
|
|
size_t maxDepth = (size_t)-1;
|
|
|
|
void *thingToIgnore = NULL;
|
|
|
|
FILE *dumpFile;
|
|
|
|
JSBool ok;
|
|
|
|
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value *argv = JS_ARGV(cx, vp);
|
2010-08-16 23:35:04 +04:00
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_VOID);
|
|
|
|
|
|
|
|
vp = argv + 0;
|
|
|
|
if (argc > 0 && *vp != JSVAL_NULL && *vp != JSVAL_VOID) {
|
2009-07-11 10:33:10 +04:00
|
|
|
JSString *str;
|
|
|
|
|
|
|
|
str = JS_ValueToString(cx, *vp);
|
|
|
|
if (!str)
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2009-07-11 10:33:10 +04:00
|
|
|
*vp = STRING_TO_JSVAL(str);
|
2013-02-21 13:48:17 +04:00
|
|
|
if (!fileName.encodeLatin1(cx, str))
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
2010-08-16 23:35:04 +04:00
|
|
|
vp = argv + 1;
|
|
|
|
if (argc > 1 && *vp != JSVAL_NULL && *vp != JSVAL_VOID) {
|
2009-07-11 10:33:10 +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) {
|
2009-07-11 10:33:10 +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;
|
2009-07-11 10:33:10 +04:00
|
|
|
|
|
|
|
if (!JS_ValueToECMAUint32(cx, *vp, &depth))
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2009-07-11 10:33:10 +04:00
|
|
|
maxDepth = depth;
|
|
|
|
}
|
|
|
|
|
2010-08-16 23:35:04 +04:00
|
|
|
vp = argv + 4;
|
|
|
|
if (argc > 4 && *vp != JSVAL_NULL && *vp != JSVAL_VOID) {
|
2009-07-11 10:33:10 +04:00
|
|
|
if (!JSVAL_IS_TRACEABLE(*vp))
|
|
|
|
goto not_traceable_arg;
|
|
|
|
thingToIgnore = JSVAL_TO_TRACEABLE(*vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fileName) {
|
|
|
|
dumpFile = stdout;
|
|
|
|
} else {
|
2010-11-11 23:40:29 +03:00
|
|
|
dumpFile = fopen(fileName.ptr(), "w");
|
2009-07-11 10:33:10 +04:00
|
|
|
if (!dumpFile) {
|
|
|
|
fprintf(stderr, "dumpHeap: can't open %s: %s\n",
|
2010-11-11 23:40:29 +03:00
|
|
|
fileName.ptr(), strerror(errno));
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-01 00:23:38 +04:00
|
|
|
ok = JS_DumpHeap(JS_GetRuntime(cx), dumpFile, startThing, startTraceKind, thingToFind,
|
2009-07-11 10:33:10 +04:00
|
|
|
maxDepth, thingToIgnore);
|
|
|
|
if (dumpFile != stdout)
|
|
|
|
fclose(dumpFile);
|
2012-03-01 00:23:38 +04:00
|
|
|
if (!ok)
|
|
|
|
JS_ReportOutOfMemory(cx);
|
2009-07-11 10:33:10 +04:00
|
|
|
return ok;
|
|
|
|
|
|
|
|
not_traceable_arg:
|
|
|
|
fprintf(stderr,
|
|
|
|
"dumpHeap: argument %u is not null or a heap-allocated thing\n",
|
|
|
|
(unsigned)(vp - argv));
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2013-04-23 01:15:49 +04:00
|
|
|
const JSFunctionSpec gGlobalFunctions[] =
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2012-08-08 09:26:19 +04:00
|
|
|
JS_FS("print", Print, 0,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),
|
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
JS_FS("gczeal", GCZeal, 1,0),
|
|
|
|
#endif
|
|
|
|
#ifdef DEBUG
|
|
|
|
JS_FS("dumpHeap", DumpHeap, 5,0),
|
|
|
|
#endif
|
|
|
|
JS_FS_END
|
2009-07-11 10:33:10 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum JSShellErrNum
|
|
|
|
{
|
|
|
|
#define MSG_DEF(name, number, count, exception, format) \
|
|
|
|
name = number,
|
|
|
|
#include "jsshell.msg"
|
|
|
|
#undef MSG_DEF
|
|
|
|
JSShellErr_Limit
|
|
|
|
#undef MSGDEF
|
|
|
|
} JSShellErrNum;
|
|
|
|
|
2013-07-17 07:38:45 +04:00
|
|
|
} /* anonymous namespace */
|
|
|
|
|
|
|
|
void
|
|
|
|
XPCShellEnvironment::ProcessFile(JSContext *cx,
|
|
|
|
JS::Handle<JSObject*> obj,
|
|
|
|
const char *filename,
|
|
|
|
FILE *file,
|
|
|
|
JSBool forceTTY)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2013-07-17 07:38:45 +04:00
|
|
|
XPCShellEnvironment* env = this;
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2011-09-02 10:46:00 +04:00
|
|
|
JSScript *script;
|
2013-05-04 01:49:47 +04:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2009-07-11 10:33:10 +04:00
|
|
|
int lineno, startline;
|
|
|
|
JSBool ok, hitEOF;
|
|
|
|
char *bufp, buffer[4096];
|
|
|
|
JSString *str;
|
|
|
|
|
|
|
|
if (forceTTY) {
|
|
|
|
file = stdin;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#ifdef HAVE_ISATTY
|
|
|
|
if (!isatty(fileno(file)))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* It's not interactive - just execute it.
|
|
|
|
*
|
|
|
|
* Support the UNIX #! shell hack; gobble the first line if it starts
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
int ch = fgetc(file);
|
|
|
|
if (ch == '#') {
|
|
|
|
while((ch = fgetc(file)) != EOF) {
|
|
|
|
if(ch == '\n' || ch == '\r')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ungetc(ch, file);
|
|
|
|
|
|
|
|
JSAutoRequest ar(cx);
|
2012-08-22 05:42:53 +04:00
|
|
|
JSAutoCompartment ac(cx, obj);
|
2010-09-30 11:14:37 +04:00
|
|
|
|
2012-11-07 05:36:47 +04:00
|
|
|
JS::CompileOptions options(cx);
|
|
|
|
options.setUTF8(true)
|
|
|
|
.setFileAndLine(filename, 1)
|
|
|
|
.setPrincipals(env->GetPrincipal());
|
2013-05-04 01:49:47 +04:00
|
|
|
JSScript* script = JS::Compile(cx, obj, options, file);
|
2013-07-17 07:38:45 +04:00
|
|
|
if (script)
|
2013-05-04 01:49:47 +04:00
|
|
|
(void)JS_ExecuteScript(cx, obj, script, result.address());
|
2009-07-11 10:33:10 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It's an interactive filehandle; drop into read-eval-print loop. */
|
|
|
|
lineno = 1;
|
2013-08-07 10:59:54 +04:00
|
|
|
hitEOF = false;
|
2009-07-11 10:33:10 +04:00
|
|
|
do {
|
|
|
|
bufp = buffer;
|
|
|
|
*bufp = '\0';
|
|
|
|
|
|
|
|
JSAutoRequest ar(cx);
|
2012-08-22 05:42:53 +04:00
|
|
|
JSAutoCompartment ac(cx, obj);
|
2010-09-30 11:14:37 +04:00
|
|
|
|
2009-07-11 10:33:10 +04:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
do {
|
|
|
|
if (!GetLine(bufp, file, startline == lineno ? "js> " : "")) {
|
2013-08-07 10:59:54 +04:00
|
|
|
hitEOF = true;
|
2009-07-11 10:33:10 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
bufp += strlen(bufp);
|
|
|
|
lineno++;
|
2012-10-30 00:55:17 +04:00
|
|
|
} while (!JS_BufferIsCompilableUnit(cx, obj, buffer, strlen(buffer)));
|
2009-07-11 10:33:10 +04:00
|
|
|
|
|
|
|
/* Clear any pending exception from previous failed compiles. */
|
|
|
|
JS_ClearPendingException(cx);
|
2011-09-02 10:46:00 +04:00
|
|
|
script =
|
2009-07-11 10:33:10 +04:00
|
|
|
JS_CompileScriptForPrincipals(cx, obj, env->GetPrincipal(), buffer,
|
|
|
|
strlen(buffer), "typein", startline);
|
2011-09-02 10:46:00 +04:00
|
|
|
if (script) {
|
2009-07-11 10:33:10 +04:00
|
|
|
JSErrorReporter older;
|
|
|
|
|
2013-07-17 07:38:45 +04:00
|
|
|
ok = JS_ExecuteScript(cx, obj, script, result.address());
|
|
|
|
if (ok && result != JSVAL_VOID) {
|
|
|
|
/* Suppress error reports from JS_ValueToString(). */
|
|
|
|
older = JS_SetErrorReporter(cx, NULL);
|
|
|
|
str = JS_ValueToString(cx, result);
|
|
|
|
JSAutoByteString bytes;
|
|
|
|
if (str)
|
|
|
|
bytes.encodeLatin1(cx, str);
|
|
|
|
JS_SetErrorReporter(cx, older);
|
|
|
|
|
|
|
|
if (!!bytes)
|
|
|
|
fprintf(stdout, "%s\n", bytes.ptr());
|
|
|
|
else
|
2013-08-07 10:59:54 +04:00
|
|
|
ok = false;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (!hitEOF && !env->IsQuitting());
|
|
|
|
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
XPCShellDirProvider::AddRef()
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
XPCShellDirProvider::Release()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_QUERY_INTERFACE1(XPCShellDirProvider, nsIDirectoryServiceProvider)
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2009-07-11 10:33:10 +04:00
|
|
|
XPCShellDirProvider::SetGREDir(const char *dir)
|
|
|
|
{
|
|
|
|
nsresult rv = XRE_GetFileFromPath(dir, getter_AddRefs(mGREDir));
|
|
|
|
return NS_SUCCEEDED(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
XPCShellDirProvider::GetFile(const char *prop,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *persistent,
|
2009-07-11 10:33:10 +04:00
|
|
|
nsIFile* *result)
|
|
|
|
{
|
|
|
|
if (mGREDir && !strcmp(prop, NS_GRE_DIR)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*persistent = true;
|
2009-07-11 10:33:10 +04:00
|
|
|
NS_ADDREF(*result = mGREDir);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
XPCShellEnvironment*
|
|
|
|
XPCShellEnvironment::CreateEnvironment()
|
|
|
|
{
|
|
|
|
XPCShellEnvironment* env = new XPCShellEnvironment();
|
|
|
|
if (env && !env->Init()) {
|
|
|
|
delete env;
|
2012-07-30 18:20:58 +04:00
|
|
|
env = nullptr;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCShellEnvironment::XPCShellEnvironment()
|
2013-08-07 10:59:54 +04:00
|
|
|
: mQuitting(false)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCShellEnvironment::~XPCShellEnvironment()
|
|
|
|
{
|
|
|
|
|
2013-07-17 07:38:46 +04:00
|
|
|
AutoSafeJSContext cx;
|
2013-08-08 03:14:20 +04:00
|
|
|
Rooted<JSObject*> global(cx, GetGlobalObject());
|
2013-07-17 07:38:46 +04:00
|
|
|
if (global) {
|
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, global);
|
|
|
|
JS_SetAllNonReservedSlotsToUndefined(cx, global);
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
mGlobalHolder.Release();
|
|
|
|
|
2013-07-17 07:38:46 +04:00
|
|
|
JSRuntime *rt = JS_GetRuntime(cx);
|
2012-03-28 14:13:30 +04:00
|
|
|
JS_GC(rt);
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
XPCShellEnvironment::Init()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
#ifdef HAVE_SETBUF
|
|
|
|
// unbuffer stdout so that output is in the correct order; note that stderr
|
|
|
|
// is unbuffered by default
|
|
|
|
setbuf(stdout, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsCOMPtr<nsIJSRuntimeService> rtsvc =
|
|
|
|
do_GetService("@mozilla.org/js/xpc/RuntimeService;1");
|
|
|
|
if (!rtsvc) {
|
|
|
|
NS_ERROR("failed to get nsJSRuntimeService!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSRuntime *rt;
|
|
|
|
if (NS_FAILED(rtsvc->GetRuntime(&rt)) || !rt) {
|
|
|
|
NS_ERROR("failed to get JSRuntime from nsJSRuntimeService!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mGlobalHolder.Hold(rt)) {
|
|
|
|
NS_ERROR("Can't protect global object!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-17 07:38:46 +04:00
|
|
|
AutoSafeJSContext cx;
|
2009-07-11 10:33:10 +04:00
|
|
|
|
|
|
|
JS_SetContextPrivate(cx, this);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXPConnect> xpc =
|
|
|
|
do_GetService(nsIXPConnect::GetCID());
|
|
|
|
if (!xpc) {
|
|
|
|
NS_ERROR("failed to get nsXPConnect service!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
nsCOMPtr<nsIScriptSecurityManager> securityManager =
|
|
|
|
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv) && securityManager) {
|
|
|
|
rv = securityManager->GetSystemPrincipal(getter_AddRefs(principal));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
fprintf(stderr, "+++ Failed to obtain SystemPrincipal from ScriptSecurityManager service.\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "+++ Failed to get ScriptSecurityManager service, running without principals");
|
|
|
|
}
|
|
|
|
|
2013-04-04 13:27:38 +04:00
|
|
|
nsRefPtr<BackstagePass> backstagePass;
|
|
|
|
rv = NS_NewBackstagePass(getter_AddRefs(backstagePass));
|
2009-07-11 10:33:10 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-04-04 13:27:38 +04:00
|
|
|
NS_ERROR("Failed to create backstage pass!");
|
2009-07-11 10:33:10 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-29 19:11:17 +04:00
|
|
|
JS::CompartmentOptions options;
|
2013-06-29 19:11:18 +04:00
|
|
|
options.setZone(JS::SystemZone)
|
|
|
|
.setVersion(JSVERSION_LATEST);
|
2009-07-11 10:33:10 +04:00
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
|
2013-04-04 13:27:38 +04:00
|
|
|
rv = xpc->InitClassesWithNewWrappedGlobal(cx,
|
|
|
|
static_cast<nsIGlobalObject *>(backstagePass),
|
2012-09-05 22:32:07 +04:00
|
|
|
principal, 0,
|
2013-06-29 19:11:17 +04:00
|
|
|
options,
|
2009-07-11 10:33:10 +04:00
|
|
|
getter_AddRefs(holder));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("InitClassesWithNewWrappedGlobal failed!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-20 16:44:18 +04:00
|
|
|
JS::Rooted<JSObject*> globalObj(cx, holder->GetJSObject());
|
|
|
|
if (!globalObj) {
|
2009-07-11 10:33:10 +04:00
|
|
|
NS_ERROR("Failed to get global JSObject!");
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-17 07:38:46 +04:00
|
|
|
JSAutoCompartment ac(cx, globalObj);
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2013-04-04 13:27:38 +04:00
|
|
|
backstagePass->SetGlobalObject(globalObj);
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2013-07-17 07:38:46 +04:00
|
|
|
if (!JS_DefineProperty(cx, globalObj, "__XPCShellEnvironment",
|
|
|
|
PRIVATE_TO_JSVAL(this), JS_PropertyStub,
|
|
|
|
JS_StrictPropertyStub,
|
|
|
|
JSPROP_READONLY | JSPROP_PERMANENT) ||
|
|
|
|
!JS_DefineFunctions(cx, globalObj, gGlobalFunctions) ||
|
|
|
|
!JS_DefineProfilingFunctions(cx, globalObj))
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2013-07-17 07:38:46 +04:00
|
|
|
NS_ERROR("JS_DefineFunctions failed!");
|
|
|
|
return false;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mGlobalHolder = globalObj;
|
|
|
|
|
|
|
|
FILE* runtimeScriptFile = fopen(kDefaultRuntimeScriptFilename, "r");
|
|
|
|
if (runtimeScriptFile) {
|
|
|
|
fprintf(stdout, "[loading '%s'...]\n", kDefaultRuntimeScriptFilename);
|
|
|
|
ProcessFile(cx, globalObj, kDefaultRuntimeScriptFilename,
|
2013-08-07 10:59:54 +04:00
|
|
|
runtimeScriptFile, false);
|
2009-07-11 10:33:10 +04:00
|
|
|
fclose(runtimeScriptFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2009-07-30 01:12:15 +04:00
|
|
|
XPCShellEnvironment::EvaluateString(const nsString& aString,
|
|
|
|
nsString* aResult)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2013-07-17 07:38:46 +04:00
|
|
|
AutoSafeJSContext cx;
|
|
|
|
JS::Rooted<JSObject*> global(cx, GetGlobalObject());
|
|
|
|
JSAutoCompartment ac(cx, global);
|
2010-09-30 11:14:37 +04:00
|
|
|
|
2011-09-02 10:46:00 +04:00
|
|
|
JSScript* script =
|
2013-07-17 07:38:46 +04:00
|
|
|
JS_CompileUCScriptForPrincipals(cx, global, GetPrincipal(),
|
2009-07-30 01:12:15 +04:00
|
|
|
aString.get(), aString.Length(),
|
|
|
|
"typein", 0);
|
2011-09-02 10:46:00 +04:00
|
|
|
if (!script) {
|
2009-07-11 10:33:10 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-17 07:38:45 +04:00
|
|
|
if (aResult) {
|
|
|
|
aResult->Truncate();
|
|
|
|
}
|
2009-07-24 02:27:17 +04:00
|
|
|
|
2013-07-17 07:38:46 +04:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
|
|
|
JSBool ok = JS_ExecuteScript(cx, global, script, result.address());
|
2013-07-17 07:38:45 +04:00
|
|
|
if (ok && result != JSVAL_VOID) {
|
2013-07-17 07:38:46 +04:00
|
|
|
JSErrorReporter old = JS_SetErrorReporter(cx, NULL);
|
|
|
|
JSString* str = JS_ValueToString(cx, result);
|
2013-07-17 07:38:45 +04:00
|
|
|
nsDependentJSString depStr;
|
|
|
|
if (str)
|
2013-07-17 07:38:46 +04:00
|
|
|
depStr.init(cx, str);
|
|
|
|
JS_SetErrorReporter(cx, old);
|
2013-07-17 07:38:45 +04:00
|
|
|
|
|
|
|
if (!depStr.IsEmpty() && aResult) {
|
|
|
|
aResult->Assign(depStr);
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|