ipc/testshell/XPCShellEnvironment.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/testshell/XPCShellEnvironment.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,609 @@
     1.4 +/* vim: set ts=8 sts=4 et sw=4 tw=80:
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include <stdlib.h>
    1.10 +#include <errno.h>
    1.11 +#ifdef HAVE_IO_H
    1.12 +#include <io.h>     /* for isatty() */
    1.13 +#endif
    1.14 +#ifdef HAVE_UNISTD_H
    1.15 +#include <unistd.h>     /* for isatty() */
    1.16 +#endif
    1.17 +
    1.18 +#include "base/basictypes.h"
    1.19 +
    1.20 +#include "jsapi.h"
    1.21 +#include "js/OldDebugAPI.h"
    1.22 +
    1.23 +#include "xpcpublic.h"
    1.24 +
    1.25 +#include "XPCShellEnvironment.h"
    1.26 +
    1.27 +#include "mozilla/XPCOM.h"
    1.28 +
    1.29 +#include "nsIChannel.h"
    1.30 +#include "nsIClassInfo.h"
    1.31 +#include "nsIDirectoryService.h"
    1.32 +#include "nsIJSRuntimeService.h"
    1.33 +#include "nsIPrincipal.h"
    1.34 +#include "nsIScriptSecurityManager.h"
    1.35 +#include "nsIURI.h"
    1.36 +#include "nsIXPConnect.h"
    1.37 +#include "nsIXPCScriptable.h"
    1.38 +
    1.39 +#include "nsCxPusher.h"
    1.40 +#include "nsJSUtils.h"
    1.41 +#include "nsJSPrincipals.h"
    1.42 +#include "nsThreadUtils.h"
    1.43 +#include "nsXULAppAPI.h"
    1.44 +
    1.45 +#include "BackstagePass.h"
    1.46 +
    1.47 +#include "TestShellChild.h"
    1.48 +#include "TestShellParent.h"
    1.49 +
    1.50 +using mozilla::ipc::XPCShellEnvironment;
    1.51 +using mozilla::ipc::TestShellChild;
    1.52 +using mozilla::ipc::TestShellParent;
    1.53 +using mozilla::AutoSafeJSContext;
    1.54 +using namespace JS;
    1.55 +
    1.56 +namespace {
    1.57 +
    1.58 +static const char kDefaultRuntimeScriptFilename[] = "xpcshell.js";
    1.59 +
    1.60 +class XPCShellDirProvider : public nsIDirectoryServiceProvider
    1.61 +{
    1.62 +public:
    1.63 +    NS_DECL_ISUPPORTS
    1.64 +    NS_DECL_NSIDIRECTORYSERVICEPROVIDER
    1.65 +
    1.66 +    XPCShellDirProvider() { }
    1.67 +    ~XPCShellDirProvider() { }
    1.68 +
    1.69 +    bool SetGREDir(const char *dir);
    1.70 +    void ClearGREDir() { mGREDir = nullptr; }
    1.71 +
    1.72 +private:
    1.73 +    nsCOMPtr<nsIFile> mGREDir;
    1.74 +};
    1.75 +
    1.76 +inline XPCShellEnvironment*
    1.77 +Environment(Handle<JSObject*> global)
    1.78 +{
    1.79 +    AutoSafeJSContext cx;
    1.80 +    JSAutoCompartment ac(cx, global);
    1.81 +    Rooted<Value> v(cx);
    1.82 +    if (!JS_GetProperty(cx, global, "__XPCShellEnvironment", &v) ||
    1.83 +        !v.get().isDouble())
    1.84 +    {
    1.85 +        return nullptr;
    1.86 +    }
    1.87 +    return static_cast<XPCShellEnvironment*>(v.get().toPrivate());
    1.88 +}
    1.89 +
    1.90 +static bool
    1.91 +Print(JSContext *cx, unsigned argc, JS::Value *vp)
    1.92 +{
    1.93 +    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
    1.94 +
    1.95 +    for (unsigned i = 0; i < args.length(); i++) {
    1.96 +        JSString *str = JS::ToString(cx, args[i]);
    1.97 +        if (!str)
    1.98 +            return false;
    1.99 +        JSAutoByteString bytes(cx, str);
   1.100 +        if (!bytes)
   1.101 +            return false;
   1.102 +        fprintf(stdout, "%s%s", i ? " " : "", bytes.ptr());
   1.103 +        fflush(stdout);
   1.104 +    }
   1.105 +    fputc('\n', stdout);
   1.106 +    args.rval().setUndefined();
   1.107 +    return true;
   1.108 +}
   1.109 +
   1.110 +static bool
   1.111 +GetLine(char *bufp,
   1.112 +        FILE *file,
   1.113 +        const char *prompt)
   1.114 +{
   1.115 +    char line[256];
   1.116 +    fputs(prompt, stdout);
   1.117 +    fflush(stdout);
   1.118 +    if (!fgets(line, sizeof line, file))
   1.119 +        return false;
   1.120 +    strcpy(bufp, line);
   1.121 +    return true;
   1.122 +}
   1.123 +
   1.124 +static bool
   1.125 +Dump(JSContext *cx, unsigned argc, JS::Value *vp)
   1.126 +{
   1.127 +    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
   1.128 +
   1.129 +    if (!args.length())
   1.130 +        return true;
   1.131 +
   1.132 +    JSString *str = JS::ToString(cx, args[0]);
   1.133 +    if (!str)
   1.134 +        return false;
   1.135 +    JSAutoByteString bytes(cx, str);
   1.136 +    if (!bytes)
   1.137 +      return false;
   1.138 +
   1.139 +    fputs(bytes.ptr(), stdout);
   1.140 +    fflush(stdout);
   1.141 +    return true;
   1.142 +}
   1.143 +
   1.144 +static bool
   1.145 +Load(JSContext *cx,
   1.146 +     unsigned argc,
   1.147 +     JS::Value *vp)
   1.148 +{
   1.149 +    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
   1.150 +
   1.151 +    JS::Rooted<JSObject*> obj(cx, JS_THIS_OBJECT(cx, vp));
   1.152 +    if (!obj)
   1.153 +        return false;
   1.154 +
   1.155 +    for (unsigned i = 0; i < args.length(); i++) {
   1.156 +        JS::Rooted<JSString*> str(cx, JS::ToString(cx, args[i]));
   1.157 +        if (!str)
   1.158 +            return false;
   1.159 +        JSAutoByteString filename(cx, str);
   1.160 +        if (!filename)
   1.161 +            return false;
   1.162 +        FILE *file = fopen(filename.ptr(), "r");
   1.163 +        if (!file) {
   1.164 +            JS_ReportError(cx, "cannot open file '%s' for reading", filename.ptr());
   1.165 +            return false;
   1.166 +        }
   1.167 +        Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
   1.168 +        JS::CompileOptions options(cx);
   1.169 +        options.setUTF8(true)
   1.170 +               .setFileAndLine(filename.ptr(), 1);
   1.171 +        JS::Rooted<JSScript*> script(cx, JS::Compile(cx, obj, options, file));
   1.172 +        fclose(file);
   1.173 +        if (!script)
   1.174 +            return false;
   1.175 +
   1.176 +        if (!JS_ExecuteScript(cx, obj, script)) {
   1.177 +            return false;
   1.178 +        }
   1.179 +    }
   1.180 +    args.rval().setUndefined();
   1.181 +    return true;
   1.182 +}
   1.183 +
   1.184 +static bool
   1.185 +Version(JSContext *cx,
   1.186 +        unsigned argc,
   1.187 +        JS::Value *vp)
   1.188 +{
   1.189 +    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
   1.190 +    args.rval().setInt32(JS_GetVersion(cx));
   1.191 +    if (args.get(0).isInt32())
   1.192 +        JS_SetVersionForCompartment(js::GetContextCompartment(cx),
   1.193 +                                    JSVersion(args[0].toInt32()));
   1.194 +    return true;
   1.195 +}
   1.196 +
   1.197 +static bool
   1.198 +BuildDate(JSContext *cx, unsigned argc, JS::Value *vp)
   1.199 +{
   1.200 +    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
   1.201 +    fprintf(stdout, "built on %s at %s\n", __DATE__, __TIME__);
   1.202 +    args.rval().setUndefined();
   1.203 +    return true;
   1.204 +}
   1.205 +
   1.206 +static bool
   1.207 +Quit(JSContext *cx,
   1.208 +     unsigned argc,
   1.209 +     JS::Value *vp)
   1.210 +{
   1.211 +    Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
   1.212 +    XPCShellEnvironment* env = Environment(global);
   1.213 +    env->SetIsQuitting();
   1.214 +
   1.215 +    return false;
   1.216 +}
   1.217 +
   1.218 +static bool
   1.219 +DumpXPC(JSContext *cx,
   1.220 +        unsigned argc,
   1.221 +        JS::Value *vp)
   1.222 +{
   1.223 +    JS::CallArgs args = CallArgsFromVp(argc, vp);
   1.224 +
   1.225 +    uint16_t depth = 2;
   1.226 +    if (args.length() > 0) {
   1.227 +        if (!JS::ToUint16(cx, args[0], &depth))
   1.228 +            return false;
   1.229 +    }
   1.230 +
   1.231 +    nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
   1.232 +    if (xpc)
   1.233 +        xpc->DebugDump(int16_t(depth));
   1.234 +    args.rval().setUndefined();
   1.235 +    return true;
   1.236 +}
   1.237 +
   1.238 +static bool
   1.239 +GC(JSContext *cx,
   1.240 +   unsigned argc,
   1.241 +   JS::Value *vp)
   1.242 +{
   1.243 +    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
   1.244 +
   1.245 +    JSRuntime *rt = JS_GetRuntime(cx);
   1.246 +    JS_GC(rt);
   1.247 +#ifdef JS_GCMETER
   1.248 +    js_DumpGCStats(rt, stdout);
   1.249 +#endif
   1.250 +    args.rval().setUndefined();
   1.251 +    return true;
   1.252 +}
   1.253 +
   1.254 +#ifdef JS_GC_ZEAL
   1.255 +static bool
   1.256 +GCZeal(JSContext *cx, unsigned argc, JS::Value *vp)
   1.257 +{
   1.258 +  CallArgs args = CallArgsFromVp(argc, vp);
   1.259 +
   1.260 +  uint32_t zeal;
   1.261 +  if (!ToUint32(cx, args.get(0), &zeal))
   1.262 +    return false;
   1.263 +
   1.264 +  JS_SetGCZeal(cx, uint8_t(zeal), JS_DEFAULT_ZEAL_FREQ);
   1.265 +  return true;
   1.266 +}
   1.267 +#endif
   1.268 +
   1.269 +const JSFunctionSpec gGlobalFunctions[] =
   1.270 +{
   1.271 +    JS_FS("print",           Print,          0,0),
   1.272 +    JS_FS("load",            Load,           1,0),
   1.273 +    JS_FS("quit",            Quit,           0,0),
   1.274 +    JS_FS("version",         Version,        1,0),
   1.275 +    JS_FS("build",           BuildDate,      0,0),
   1.276 +    JS_FS("dumpXPC",         DumpXPC,        1,0),
   1.277 +    JS_FS("dump",            Dump,           1,0),
   1.278 +    JS_FS("gc",              GC,             0,0),
   1.279 + #ifdef JS_GC_ZEAL
   1.280 +    JS_FS("gczeal",          GCZeal,         1,0),
   1.281 + #endif
   1.282 +    JS_FS_END
   1.283 +};
   1.284 +
   1.285 +typedef enum JSShellErrNum
   1.286 +{
   1.287 +#define MSG_DEF(name, number, count, exception, format) \
   1.288 +    name = number,
   1.289 +#include "jsshell.msg"
   1.290 +#undef MSG_DEF
   1.291 +    JSShellErr_Limit
   1.292 +#undef MSGDEF
   1.293 +} JSShellErrNum;
   1.294 +
   1.295 +} /* anonymous namespace */
   1.296 +
   1.297 +void
   1.298 +XPCShellEnvironment::ProcessFile(JSContext *cx,
   1.299 +                                 JS::Handle<JSObject*> obj,
   1.300 +                                 const char *filename,
   1.301 +                                 FILE *file,
   1.302 +                                 bool forceTTY)
   1.303 +{
   1.304 +    XPCShellEnvironment* env = this;
   1.305 +
   1.306 +    JS::Rooted<JS::Value> result(cx);
   1.307 +    int lineno, startline;
   1.308 +    bool ok, hitEOF;
   1.309 +    char *bufp, buffer[4096];
   1.310 +    JSString *str;
   1.311 +
   1.312 +    if (forceTTY) {
   1.313 +        file = stdin;
   1.314 +    }
   1.315 +    else if (!isatty(fileno(file)))
   1.316 +    {
   1.317 +        /*
   1.318 +         * It's not interactive - just execute it.
   1.319 +         *
   1.320 +         * Support the UNIX #! shell hack; gobble the first line if it starts
   1.321 +         * with '#'.  TODO - this isn't quite compatible with sharp variables,
   1.322 +         * as a legal js program (using sharp variables) might start with '#'.
   1.323 +         * But that would require multi-character lookahead.
   1.324 +         */
   1.325 +        int ch = fgetc(file);
   1.326 +        if (ch == '#') {
   1.327 +            while((ch = fgetc(file)) != EOF) {
   1.328 +                if(ch == '\n' || ch == '\r')
   1.329 +                    break;
   1.330 +            }
   1.331 +        }
   1.332 +        ungetc(ch, file);
   1.333 +
   1.334 +        JSAutoRequest ar(cx);
   1.335 +        JSAutoCompartment ac(cx, obj);
   1.336 +
   1.337 +        JS::CompileOptions options(cx);
   1.338 +        options.setUTF8(true)
   1.339 +               .setFileAndLine(filename, 1);
   1.340 +        JS::Rooted<JSScript*> script(cx, JS::Compile(cx, obj, options, file));
   1.341 +        if (script)
   1.342 +            (void)JS_ExecuteScript(cx, obj, script, &result);
   1.343 +
   1.344 +        return;
   1.345 +    }
   1.346 +
   1.347 +    /* It's an interactive filehandle; drop into read-eval-print loop. */
   1.348 +    lineno = 1;
   1.349 +    hitEOF = false;
   1.350 +    do {
   1.351 +        bufp = buffer;
   1.352 +        *bufp = '\0';
   1.353 +
   1.354 +        JSAutoRequest ar(cx);
   1.355 +        JSAutoCompartment ac(cx, obj);
   1.356 +
   1.357 +        /*
   1.358 +         * Accumulate lines until we get a 'compilable unit' - one that either
   1.359 +         * generates an error (before running out of source) or that compiles
   1.360 +         * cleanly.  This should be whenever we get a complete statement that
   1.361 +         * coincides with the end of a line.
   1.362 +         */
   1.363 +        startline = lineno;
   1.364 +        do {
   1.365 +            if (!GetLine(bufp, file, startline == lineno ? "js> " : "")) {
   1.366 +                hitEOF = true;
   1.367 +                break;
   1.368 +            }
   1.369 +            bufp += strlen(bufp);
   1.370 +            lineno++;
   1.371 +        } while (!JS_BufferIsCompilableUnit(cx, obj, buffer, strlen(buffer)));
   1.372 +
   1.373 +        /* Clear any pending exception from previous failed compiles.  */
   1.374 +        JS_ClearPendingException(cx);
   1.375 +        JS::CompileOptions options(cx);
   1.376 +        options.setFileAndLine("typein", startline);
   1.377 +        JS::Rooted<JSScript*> script(cx,
   1.378 +                                     JS_CompileScript(cx, obj, buffer, strlen(buffer), options));
   1.379 +        if (script) {
   1.380 +            JSErrorReporter older;
   1.381 +
   1.382 +            ok = JS_ExecuteScript(cx, obj, script, &result);
   1.383 +            if (ok && result != JSVAL_VOID) {
   1.384 +                /* Suppress error reports from JS::ToString(). */
   1.385 +                older = JS_SetErrorReporter(cx, nullptr);
   1.386 +                str = JS::ToString(cx, result);
   1.387 +                JSAutoByteString bytes;
   1.388 +                if (str)
   1.389 +                    bytes.encodeLatin1(cx, str);
   1.390 +                JS_SetErrorReporter(cx, older);
   1.391 +
   1.392 +                if (!!bytes)
   1.393 +                    fprintf(stdout, "%s\n", bytes.ptr());
   1.394 +                else
   1.395 +                    ok = false;
   1.396 +            }
   1.397 +        }
   1.398 +    } while (!hitEOF && !env->IsQuitting());
   1.399 +
   1.400 +    fprintf(stdout, "\n");
   1.401 +}
   1.402 +
   1.403 +NS_IMETHODIMP_(MozExternalRefCountType)
   1.404 +XPCShellDirProvider::AddRef()
   1.405 +{
   1.406 +    return 2;
   1.407 +}
   1.408 +
   1.409 +NS_IMETHODIMP_(MozExternalRefCountType)
   1.410 +XPCShellDirProvider::Release()
   1.411 +{
   1.412 +    return 1;
   1.413 +}
   1.414 +
   1.415 +NS_IMPL_QUERY_INTERFACE(XPCShellDirProvider, nsIDirectoryServiceProvider)
   1.416 +
   1.417 +bool
   1.418 +XPCShellDirProvider::SetGREDir(const char *dir)
   1.419 +{
   1.420 +    nsresult rv = XRE_GetFileFromPath(dir, getter_AddRefs(mGREDir));
   1.421 +    return NS_SUCCEEDED(rv);
   1.422 +}
   1.423 +
   1.424 +NS_IMETHODIMP
   1.425 +XPCShellDirProvider::GetFile(const char *prop,
   1.426 +                             bool *persistent,
   1.427 +                             nsIFile* *result)
   1.428 +{
   1.429 +    if (mGREDir && !strcmp(prop, NS_GRE_DIR)) {
   1.430 +        *persistent = true;
   1.431 +        NS_ADDREF(*result = mGREDir);
   1.432 +        return NS_OK;
   1.433 +    }
   1.434 +
   1.435 +    return NS_ERROR_FAILURE;
   1.436 +}
   1.437 +
   1.438 +// static
   1.439 +XPCShellEnvironment*
   1.440 +XPCShellEnvironment::CreateEnvironment()
   1.441 +{
   1.442 +    XPCShellEnvironment* env = new XPCShellEnvironment();
   1.443 +    if (env && !env->Init()) {
   1.444 +        delete env;
   1.445 +        env = nullptr;
   1.446 +    }
   1.447 +    return env;
   1.448 +}
   1.449 +
   1.450 +XPCShellEnvironment::XPCShellEnvironment()
   1.451 +:   mQuitting(false)
   1.452 +{
   1.453 +}
   1.454 +
   1.455 +XPCShellEnvironment::~XPCShellEnvironment()
   1.456 +{
   1.457 +
   1.458 +    AutoSafeJSContext cx;
   1.459 +    Rooted<JSObject*> global(cx, GetGlobalObject());
   1.460 +    if (global) {
   1.461 +        {
   1.462 +            JSAutoCompartment ac(cx, global);
   1.463 +            JS_SetAllNonReservedSlotsToUndefined(cx, global);
   1.464 +        }
   1.465 +        mGlobalHolder.Release();
   1.466 +
   1.467 +        JSRuntime *rt = JS_GetRuntime(cx);
   1.468 +        JS_GC(rt);
   1.469 +    }
   1.470 +}
   1.471 +
   1.472 +bool
   1.473 +XPCShellEnvironment::Init()
   1.474 +{
   1.475 +    nsresult rv;
   1.476 +
   1.477 +    // unbuffer stdout so that output is in the correct order; note that stderr
   1.478 +    // is unbuffered by default
   1.479 +    setbuf(stdout, 0);
   1.480 +
   1.481 +    nsCOMPtr<nsIJSRuntimeService> rtsvc =
   1.482 +        do_GetService("@mozilla.org/js/xpc/RuntimeService;1");
   1.483 +    if (!rtsvc) {
   1.484 +        NS_ERROR("failed to get nsJSRuntimeService!");
   1.485 +        return false;
   1.486 +    }
   1.487 +
   1.488 +    JSRuntime *rt;
   1.489 +    if (NS_FAILED(rtsvc->GetRuntime(&rt)) || !rt) {
   1.490 +        NS_ERROR("failed to get JSRuntime from nsJSRuntimeService!");
   1.491 +        return false;
   1.492 +    }
   1.493 +
   1.494 +    if (!mGlobalHolder.Hold(rt)) {
   1.495 +        NS_ERROR("Can't protect global object!");
   1.496 +        return false;
   1.497 +    }
   1.498 +
   1.499 +    AutoSafeJSContext cx;
   1.500 +
   1.501 +    JS_SetContextPrivate(cx, this);
   1.502 +
   1.503 +    nsCOMPtr<nsIXPConnect> xpc =
   1.504 +      do_GetService(nsIXPConnect::GetCID());
   1.505 +    if (!xpc) {
   1.506 +        NS_ERROR("failed to get nsXPConnect service!");
   1.507 +        return false;
   1.508 +    }
   1.509 +
   1.510 +    nsCOMPtr<nsIPrincipal> principal;
   1.511 +    nsCOMPtr<nsIScriptSecurityManager> securityManager =
   1.512 +        do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
   1.513 +    if (NS_SUCCEEDED(rv) && securityManager) {
   1.514 +        rv = securityManager->GetSystemPrincipal(getter_AddRefs(principal));
   1.515 +        if (NS_FAILED(rv)) {
   1.516 +            fprintf(stderr, "+++ Failed to obtain SystemPrincipal from ScriptSecurityManager service.\n");
   1.517 +        }
   1.518 +    } else {
   1.519 +        fprintf(stderr, "+++ Failed to get ScriptSecurityManager service, running without principals");
   1.520 +    }
   1.521 +
   1.522 +    nsRefPtr<BackstagePass> backstagePass;
   1.523 +    rv = NS_NewBackstagePass(getter_AddRefs(backstagePass));
   1.524 +    if (NS_FAILED(rv)) {
   1.525 +        NS_ERROR("Failed to create backstage pass!");
   1.526 +        return false;
   1.527 +    }
   1.528 +
   1.529 +    JS::CompartmentOptions options;
   1.530 +    options.setZone(JS::SystemZone)
   1.531 +           .setVersion(JSVERSION_LATEST);
   1.532 +    nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
   1.533 +    rv = xpc->InitClassesWithNewWrappedGlobal(cx,
   1.534 +                                              static_cast<nsIGlobalObject *>(backstagePass),
   1.535 +                                              principal, 0,
   1.536 +                                              options,
   1.537 +                                              getter_AddRefs(holder));
   1.538 +    if (NS_FAILED(rv)) {
   1.539 +        NS_ERROR("InitClassesWithNewWrappedGlobal failed!");
   1.540 +        return false;
   1.541 +    }
   1.542 +
   1.543 +    JS::Rooted<JSObject*> globalObj(cx, holder->GetJSObject());
   1.544 +    if (!globalObj) {
   1.545 +        NS_ERROR("Failed to get global JSObject!");
   1.546 +        return false;
   1.547 +    }
   1.548 +    JSAutoCompartment ac(cx, globalObj);
   1.549 +
   1.550 +    backstagePass->SetGlobalObject(globalObj);
   1.551 +
   1.552 +    JS::Rooted<Value> privateVal(cx, PrivateValue(this));
   1.553 +    if (!JS_DefineProperty(cx, globalObj, "__XPCShellEnvironment",
   1.554 +                           privateVal, JSPROP_READONLY | JSPROP_PERMANENT,
   1.555 +                           JS_PropertyStub, JS_StrictPropertyStub) ||
   1.556 +        !JS_DefineFunctions(cx, globalObj, gGlobalFunctions) ||
   1.557 +        !JS_DefineProfilingFunctions(cx, globalObj))
   1.558 +    {
   1.559 +        NS_ERROR("JS_DefineFunctions failed!");
   1.560 +        return false;
   1.561 +    }
   1.562 +
   1.563 +    mGlobalHolder = globalObj;
   1.564 +
   1.565 +    FILE* runtimeScriptFile = fopen(kDefaultRuntimeScriptFilename, "r");
   1.566 +    if (runtimeScriptFile) {
   1.567 +        fprintf(stdout, "[loading '%s'...]\n", kDefaultRuntimeScriptFilename);
   1.568 +        ProcessFile(cx, globalObj, kDefaultRuntimeScriptFilename,
   1.569 +                    runtimeScriptFile, false);
   1.570 +        fclose(runtimeScriptFile);
   1.571 +    }
   1.572 +
   1.573 +    return true;
   1.574 +}
   1.575 +
   1.576 +bool
   1.577 +XPCShellEnvironment::EvaluateString(const nsString& aString,
   1.578 +                                    nsString* aResult)
   1.579 +{
   1.580 +  AutoSafeJSContext cx;
   1.581 +  JS::Rooted<JSObject*> global(cx, GetGlobalObject());
   1.582 +  JSAutoCompartment ac(cx, global);
   1.583 +
   1.584 +  JS::CompileOptions options(cx);
   1.585 +  options.setFileAndLine("typein", 0);
   1.586 +  JS::Rooted<JSScript*> script(cx, JS_CompileUCScript(cx, global, aString.get(),
   1.587 +                                                      aString.Length(), options));
   1.588 +  if (!script) {
   1.589 +     return false;
   1.590 +  }
   1.591 +
   1.592 +  if (aResult) {
   1.593 +      aResult->Truncate();
   1.594 +  }
   1.595 +
   1.596 +  JS::Rooted<JS::Value> result(cx);
   1.597 +  bool ok = JS_ExecuteScript(cx, global, script, &result);
   1.598 +  if (ok && result != JSVAL_VOID) {
   1.599 +      JSErrorReporter old = JS_SetErrorReporter(cx, nullptr);
   1.600 +      JSString* str = JS::ToString(cx, result);
   1.601 +      nsDependentJSString depStr;
   1.602 +      if (str)
   1.603 +          depStr.init(cx, str);
   1.604 +      JS_SetErrorReporter(cx, old);
   1.605 +
   1.606 +      if (!depStr.IsEmpty() && aResult) {
   1.607 +          aResult->Assign(depStr);
   1.608 +      }
   1.609 +  }
   1.610 +
   1.611 +  return true;
   1.612 +}

mercurial