2015-03-14 08:36:16 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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-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 "nsIPrincipal.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsIXPCScriptable.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() { }
|
|
|
|
|
2014-10-10 23:06:50 +04:00
|
|
|
bool SetGREDirs(const char *dir);
|
|
|
|
void ClearGREDirs() { mGREDir = nullptr;
|
|
|
|
mGREBinDir = nullptr; }
|
2009-07-11 10:33:10 +04:00
|
|
|
|
|
|
|
private:
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> mGREDir;
|
2014-10-10 23:06:50 +04:00
|
|
|
nsCOMPtr<nsIFile> mGREBinDir;
|
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
|
2013-11-16 16:31:36 +04:00
|
|
|
Print(JSContext *cx, unsigned argc, JS::Value *vp)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2013-11-16 16:31:36 +04:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2013-11-16 16:31:36 +04:00
|
|
|
for (unsigned i = 0; i < args.length(); i++) {
|
|
|
|
JSString *str = JS::ToString(cx, args[i]);
|
2009-07-11 10:33:10 +04:00
|
|
|
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);
|
|
|
|
}
|
2013-11-16 16:31:36 +04:00
|
|
|
fputc('\n', stdout);
|
|
|
|
args.rval().setUndefined();
|
2013-08-02 11:41:57 +04:00
|
|
|
return true;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
static bool
|
2009-07-11 10:33:10 +04:00
|
|
|
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
|
2013-11-16 16:31:36 +04:00
|
|
|
Dump(JSContext *cx, unsigned argc, JS::Value *vp)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2013-11-16 16:31:36 +04:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2010-08-16 23:35:04 +04:00
|
|
|
|
2013-11-16 16:31:36 +04:00
|
|
|
if (!args.length())
|
2013-08-02 11:41:57 +04:00
|
|
|
return true;
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2013-11-16 16:31:36 +04:00
|
|
|
JSString *str = JS::ToString(cx, args[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-11-16 16:31:36 +04:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
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
|
|
|
|
2015-03-14 08:36:16 +03:00
|
|
|
if (!JS_IsGlobalObject(obj)) {
|
|
|
|
JS_ReportError(cx, "Trying to load() into a non-global object");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-16 16:31:36 +04:00
|
|
|
for (unsigned i = 0; i < args.length(); i++) {
|
|
|
|
JS::Rooted<JSString*> str(cx, JS::ToString(cx, args[i]));
|
2009-07-11 10:33:10 +04:00
|
|
|
if (!str)
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
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
|
|
|
}
|
2012-11-07 05:36:47 +04:00
|
|
|
JS::CompileOptions options(cx);
|
|
|
|
options.setUTF8(true)
|
2014-03-07 02:28:05 +04:00
|
|
|
.setFileAndLine(filename.ptr(), 1);
|
2014-06-26 02:35:35 +04:00
|
|
|
JS::Rooted<JSScript*> script(cx);
|
2015-03-19 17:13:34 +03:00
|
|
|
bool ok = JS::Compile(cx, options, file, &script);
|
2009-07-11 10:33:10 +04:00
|
|
|
fclose(file);
|
2014-06-26 02:35:35 +04:00
|
|
|
if (!ok)
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2015-03-14 08:36:16 +03:00
|
|
|
if (!JS_ExecuteScript(cx, script)) {
|
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
|
|
|
}
|
2013-11-16 16:31:36 +04:00
|
|
|
args.rval().setUndefined();
|
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
|
|
|
{
|
2014-03-25 22:42:33 +04:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
args.rval().setInt32(JS_GetVersion(cx));
|
|
|
|
if (args.get(0).isInt32())
|
2013-06-29 19:11:19 +04:00
|
|
|
JS_SetVersionForCompartment(js::GetContextCompartment(cx),
|
2014-03-25 22:42:33 +04:00
|
|
|
JSVersion(args[0].toInt32()));
|
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
|
|
|
{
|
2013-11-02 16:48:09 +04:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2013-11-02 16:48:09 +04:00
|
|
|
uint16_t depth = 2;
|
|
|
|
if (args.length() > 0) {
|
|
|
|
if (!JS::ToUint16(cx, args[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());
|
2013-11-02 16:48:09 +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));
|
2013-11-02 16:48:09 +04:00
|
|
|
args.rval().setUndefined();
|
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
|
|
|
{
|
2014-03-25 22:42:33 +04:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
|
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
|
2014-03-25 22:42:33 +04:00
|
|
|
args.rval().setUndefined();
|
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
|
2013-11-01 23:44:05 +04:00
|
|
|
GCZeal(JSContext *cx, unsigned argc, JS::Value *vp)
|
2010-08-11 21:45:00 +04:00
|
|
|
{
|
2013-11-01 23:44:05 +04:00
|
|
|
CallArgs args = CallArgsFromVp(argc, 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;
|
2013-11-01 23:44:05 +04:00
|
|
|
if (!ToUint32(cx, args.get(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
|
|
|
|
|
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("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
|
|
|
|
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,
|
|
|
|
const char *filename,
|
|
|
|
FILE *file,
|
2013-08-09 02:53:04 +04:00
|
|
|
bool 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
|
|
|
|
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;
|
2013-08-09 02:53:04 +04:00
|
|
|
bool ok, hitEOF;
|
2009-07-11 10:33:10 +04:00
|
|
|
char *bufp, buffer[4096];
|
|
|
|
JSString *str;
|
|
|
|
|
2015-03-14 08:36:16 +03:00
|
|
|
JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
2009-07-11 10:33:10 +04:00
|
|
|
if (forceTTY) {
|
|
|
|
file = stdin;
|
|
|
|
}
|
Bug 944935 - Cleanup some useless AC_CHECK_FUNCS tests in configure.in. r=gps
- HAVE_RANDOM is not checked at all.
- HAVE_STRERROR is not checked in code built using the defines from the main
configure.
- HAVE_LCHOWN is only checked in nsinstall.c, which means the test is also wrong
since it's checking for the target instead of the host. Also, lchown is only
used of the -o and -g options of nsinstall, which, as far as I know, we don't
use (and if we were, that would fail with nsinstall.py, which explicitly rejects
them).
- HAVE_FCHMOD is only checked in nsinstall.c, so same as above about the
correctness of the check. If it's not available, nsinstall.c falls back to
chmod, which is fine enough for our use.
- HAVE_SNPRINTF is not checked.
- HAVE_MEMMOVE is checked in parser/expat/lib/xmlparse.c, but it's also
unconditionally defined in expat_config.h which is included from that file.
- HAVE_SETBUF is checked in a couple files, but setbuf is C89 and C99, I think
it's safe to assume all compilers we support are C89 and C99. Interestingly,
windows does have it, but since we skip this check on windows, we don't use it.
- HAVE_ISATTY, same as HAVE_SETBUF, except it's POSIX instead of C89/C99.
- HAVE_FLOCKFILE is not checked at all.
- HAVE_STRTOK_R is not checked.
- HAVE_FT_SELECT_SIZE is not checked.
- HAVE_DLADDR is not checked under js/src.
- HAVE_GETPAGESIZE is not checked under js/src (it is in libffi, but ffi uses
its own configure)
- HAVE_LSTAT64, HAVE_STAT64, HAVE_STATVFS, HAVE_STATVFS64, HAVE_TRUNCATE64 are
not checked under js/src.
- HAVE_SBRK is not checked under js/src. Moreover,
js/src/assembler/wtf/Platform.h defines it depending on the platform.
- HAVE_SNPRINTF is not checked under js/src.
- HAVE_HYPOT is not checked under js/src.
- HAVE__UNWIND_BACKTRACE is not checked under js/src.
2013-12-11 05:57:53 +04:00
|
|
|
else if (!isatty(fileno(file)))
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
2012-11-07 05:36:47 +04:00
|
|
|
JS::CompileOptions options(cx);
|
|
|
|
options.setUTF8(true)
|
2014-03-07 02:28:05 +04:00
|
|
|
.setFileAndLine(filename, 1);
|
2014-06-26 02:35:35 +04:00
|
|
|
JS::Rooted<JSScript*> script(cx);
|
2015-03-19 17:13:34 +03:00
|
|
|
if (JS::Compile(cx, options, file, &script))
|
2015-03-14 08:36:16 +03:00
|
|
|
(void)JS_ExecuteScript(cx, script, &result);
|
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';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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++;
|
2015-03-14 08:36:16 +03:00
|
|
|
} while (!JS_BufferIsCompilableUnit(cx, global, buffer, strlen(buffer)));
|
2009-07-11 10:33:10 +04:00
|
|
|
|
|
|
|
/* Clear any pending exception from previous failed compiles. */
|
|
|
|
JS_ClearPendingException(cx);
|
2013-11-19 15:41:42 +04:00
|
|
|
JS::CompileOptions options(cx);
|
2014-03-07 02:28:05 +04:00
|
|
|
options.setFileAndLine("typein", startline);
|
2014-06-26 02:35:35 +04:00
|
|
|
JS::Rooted<JSScript*> script(cx);
|
2015-03-19 17:13:33 +03:00
|
|
|
if (JS_CompileScript(cx, buffer, strlen(buffer), options, &script)) {
|
2009-07-11 10:33:10 +04:00
|
|
|
JSErrorReporter older;
|
|
|
|
|
2015-03-14 08:36:16 +03:00
|
|
|
ok = JS_ExecuteScript(cx, script, &result);
|
2015-06-30 04:36:44 +03:00
|
|
|
if (ok && !result.isUndefined()) {
|
2013-11-16 16:31:36 +04:00
|
|
|
/* Suppress error reports from JS::ToString(). */
|
2014-09-10 03:02:10 +04:00
|
|
|
older = JS_SetErrorReporter(JS_GetRuntime(cx), nullptr);
|
2013-11-16 16:31:36 +04:00
|
|
|
str = JS::ToString(cx, result);
|
2013-07-17 07:38:45 +04:00
|
|
|
JSAutoByteString bytes;
|
|
|
|
if (str)
|
|
|
|
bytes.encodeLatin1(cx, str);
|
2014-09-10 03:02:10 +04:00
|
|
|
JS_SetErrorReporter(JS_GetRuntime(cx), older);
|
2013-07-17 07:38:45 +04:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2009-07-11 10:33:10 +04:00
|
|
|
XPCShellDirProvider::AddRef()
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2009-07-11 10:33:10 +04:00
|
|
|
XPCShellDirProvider::Release()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(XPCShellDirProvider, nsIDirectoryServiceProvider)
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-10-10 23:06:50 +04:00
|
|
|
XPCShellDirProvider::SetGREDirs(const char *dir)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
|
|
|
nsresult rv = XRE_GetFileFromPath(dir, getter_AddRefs(mGREDir));
|
2014-10-10 23:06:50 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mGREDir->Clone(getter_AddRefs(mGREBinDir));
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
mGREBinDir->SetNativeLeafName(NS_LITERAL_CSTRING("MacOS"));
|
|
|
|
#endif
|
|
|
|
}
|
2009-07-11 10:33:10 +04:00
|
|
|
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;
|
2014-10-10 23:06:50 +04:00
|
|
|
} else if (mGREBinDir && !strcmp(prop, NS_GRE_BIN_DIR)) {
|
|
|
|
*persistent = true;
|
|
|
|
NS_ADDREF(*result = mGREBinDir);
|
|
|
|
return NS_OK;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2015-01-23 13:23:57 +03:00
|
|
|
mGlobalHolder.reset();
|
2009-07-11 10:33:10 +04:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
// unbuffer stdout so that output is in the correct order; note that stderr
|
|
|
|
// is unbuffered by default
|
|
|
|
setbuf(stdout, 0);
|
|
|
|
|
2015-06-27 04:44:14 +03:00
|
|
|
JSRuntime *rt = xpc::GetJSRuntime();
|
|
|
|
if (!rt) {
|
2009-07-11 10:33:10 +04:00
|
|
|
NS_ERROR("failed to get JSRuntime from nsJSRuntimeService!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-23 13:23:57 +03:00
|
|
|
mGlobalHolder.init(rt);
|
2009-07-11 10:33:10 +04:00
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BackstagePass> backstagePass;
|
2013-04-04 13:27:38 +04:00
|
|
|
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;
|
2015-12-29 01:15:52 +03:00
|
|
|
options.creationOptions().setZone(JS::SystemZone);
|
|
|
|
options.behaviors().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
|
|
|
|
2014-01-22 06:11:17 +04:00
|
|
|
JS::Rooted<Value> privateVal(cx, PrivateValue(this));
|
2013-07-17 07:38:46 +04:00
|
|
|
if (!JS_DefineProperty(cx, globalObj, "__XPCShellEnvironment",
|
2014-10-29 22:06:31 +03:00
|
|
|
privateVal,
|
2014-10-29 22:06:32 +03:00
|
|
|
JSPROP_READONLY | JSPROP_PERMANENT,
|
2014-10-29 22:06:31 +03:00
|
|
|
JS_STUBGETTER, JS_STUBSETTER) ||
|
2013-07-17 07:38:46 +04:00
|
|
|
!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);
|
2015-03-14 08:36:16 +03:00
|
|
|
ProcessFile(cx, 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
|
|
|
|
2013-11-19 15:41:42 +04:00
|
|
|
JS::CompileOptions options(cx);
|
2014-03-07 02:28:05 +04:00
|
|
|
options.setFileAndLine("typein", 0);
|
2014-06-26 02:35:35 +04:00
|
|
|
JS::Rooted<JSScript*> script(cx);
|
2015-03-19 17:13:34 +03:00
|
|
|
if (!JS_CompileUCScript(cx, aString.get(), aString.Length(), options,
|
2014-06-26 02:35:35 +04:00
|
|
|
&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);
|
2015-03-14 08:36:16 +03:00
|
|
|
bool ok = JS_ExecuteScript(cx, script, &result);
|
2015-06-30 04:36:44 +03:00
|
|
|
if (ok && !result.isUndefined()) {
|
2014-09-10 03:02:10 +04:00
|
|
|
JSErrorReporter old = JS_SetErrorReporter(JS_GetRuntime(cx), nullptr);
|
2013-11-16 16:31:36 +04:00
|
|
|
JSString* str = JS::ToString(cx, result);
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoJSString autoStr;
|
2013-07-17 07:38:45 +04:00
|
|
|
if (str)
|
2014-07-05 19:30:54 +04:00
|
|
|
autoStr.init(cx, str);
|
2014-09-10 03:02:10 +04:00
|
|
|
JS_SetErrorReporter(JS_GetRuntime(cx), old);
|
2013-07-17 07:38:45 +04:00
|
|
|
|
2014-07-05 19:30:54 +04:00
|
|
|
if (!autoStr.IsEmpty() && aResult) {
|
|
|
|
aResult->Assign(autoStr);
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|