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"
|
2013-08-27 06:05:20 +04:00
|
|
|
#include "js/OldDebugAPI.h"
|
2009-07-11 10:33:10 +04:00
|
|
|
|
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-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
|
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
|
|
|
|
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
|
|
|
}
|
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-11-16 16:31:36 +04:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
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
|
|
|
}
|
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
|
|
|
{
|
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
|
|
|
{
|
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
|
|
|
{
|
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
|
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("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
|
|
|
|
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,
|
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
|
|
|
|
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;
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
2013-11-19 15:41:42 +04:00
|
|
|
JS::CompileOptions options(cx);
|
|
|
|
options.setFileAndLine("typein", startline)
|
|
|
|
.setPrincipals(env->GetPrincipal());
|
|
|
|
script = JS_CompileScript(cx, obj, buffer, strlen(buffer), options);
|
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) {
|
2013-11-16 16:31:36 +04:00
|
|
|
/* Suppress error reports from JS::ToString(). */
|
2013-08-23 23:51:44 +04:00
|
|
|
older = JS_SetErrorReporter(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);
|
|
|
|
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;
|
|
|
|
|
|
|
|
// unbuffer stdout so that output is in the correct order; note that stderr
|
|
|
|
// is unbuffered by default
|
|
|
|
setbuf(stdout, 0);
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-11-19 15:41:42 +04:00
|
|
|
JS::CompileOptions options(cx);
|
|
|
|
options.setFileAndLine("typein", 0)
|
|
|
|
.setPrincipals(GetPrincipal());
|
|
|
|
JSScript* script = JS_CompileUCScript(cx, global, aString.get(),
|
|
|
|
aString.Length(), options);
|
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);
|
2013-08-09 02:53:04 +04:00
|
|
|
bool ok = JS_ExecuteScript(cx, global, script, result.address());
|
2013-07-17 07:38:45 +04:00
|
|
|
if (ok && result != JSVAL_VOID) {
|
2013-08-23 23:51:44 +04:00
|
|
|
JSErrorReporter old = JS_SetErrorReporter(cx, nullptr);
|
2013-11-16 16:31:36 +04:00
|
|
|
JSString* str = JS::ToString(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;
|
|
|
|
}
|