js/src/jsapi-tests/testCloneScript.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jsapi-tests/testCloneScript.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,152 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + *
     1.7 + * Test script cloning.
     1.8 + */
     1.9 +/* This Source Code Form is subject to the terms of the Mozilla Public
    1.10 + * License, v. 2.0. If a copy of the MPL was not distributed with this
    1.11 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.12 +
    1.13 +#include "js/OldDebugAPI.h"
    1.14 +#include "jsapi-tests/tests.h"
    1.15 +
    1.16 +BEGIN_TEST(test_cloneScript)
    1.17 +{
    1.18 +    JS::RootedObject A(cx, createGlobal());
    1.19 +    JS::RootedObject B(cx, createGlobal());
    1.20 +
    1.21 +    CHECK(A);
    1.22 +    CHECK(B);
    1.23 +
    1.24 +    const char *source =
    1.25 +        "var i = 0;\n"
    1.26 +        "var sum = 0;\n"
    1.27 +        "while (i < 10) {\n"
    1.28 +        "    sum += i;\n"
    1.29 +        "    ++i;\n"
    1.30 +        "}\n"
    1.31 +        "(sum);\n";
    1.32 +
    1.33 +    JS::RootedObject obj(cx);
    1.34 +
    1.35 +    // compile for A
    1.36 +    {
    1.37 +        JSAutoCompartment a(cx, A);
    1.38 +        JSFunction *fun;
    1.39 +        JS::CompileOptions options(cx);
    1.40 +        options.setFileAndLine(__FILE__, 1);
    1.41 +        CHECK(fun = JS_CompileFunction(cx, A, "f", 0, nullptr, source,
    1.42 +                                       strlen(source), options));
    1.43 +        CHECK(obj = JS_GetFunctionObject(fun));
    1.44 +    }
    1.45 +
    1.46 +    // clone into B
    1.47 +    {
    1.48 +        JSAutoCompartment b(cx, B);
    1.49 +        CHECK(JS_CloneFunctionObject(cx, obj, B));
    1.50 +    }
    1.51 +
    1.52 +    return true;
    1.53 +}
    1.54 +END_TEST(test_cloneScript)
    1.55 +
    1.56 +static void
    1.57 +DestroyPrincipals(JSPrincipals *principals)
    1.58 +{
    1.59 +    delete principals;
    1.60 +}
    1.61 +
    1.62 +struct Principals : public JSPrincipals
    1.63 +{
    1.64 +  public:
    1.65 +    Principals()
    1.66 +    {
    1.67 +        refcount = 0;
    1.68 +    }
    1.69 +};
    1.70 +
    1.71 +class AutoDropPrincipals
    1.72 +{
    1.73 +    JSRuntime *rt;
    1.74 +    JSPrincipals *principals;
    1.75 +
    1.76 +  public:
    1.77 +    AutoDropPrincipals(JSRuntime *rt, JSPrincipals *principals)
    1.78 +      : rt(rt), principals(principals)
    1.79 +    {
    1.80 +        JS_HoldPrincipals(principals);
    1.81 +    }
    1.82 +
    1.83 +    ~AutoDropPrincipals()
    1.84 +    {
    1.85 +        JS_DropPrincipals(rt, principals);
    1.86 +    }
    1.87 +};
    1.88 +
    1.89 +BEGIN_TEST(test_cloneScriptWithPrincipals)
    1.90 +{
    1.91 +    JS_InitDestroyPrincipalsCallback(rt, DestroyPrincipals);
    1.92 +
    1.93 +    JSPrincipals *principalsA = new Principals();
    1.94 +    AutoDropPrincipals dropA(rt, principalsA);
    1.95 +    JSPrincipals *principalsB = new Principals();
    1.96 +    AutoDropPrincipals dropB(rt, principalsB);
    1.97 +
    1.98 +    JS::RootedObject A(cx, createGlobal(principalsA));
    1.99 +    JS::RootedObject B(cx, createGlobal(principalsB));
   1.100 +
   1.101 +    CHECK(A);
   1.102 +    CHECK(B);
   1.103 +
   1.104 +    const char *argnames[] = { "arg" };
   1.105 +    const char *source = "return function() { return arg; }";
   1.106 +
   1.107 +    JS::RootedObject obj(cx);
   1.108 +
   1.109 +    // Compile in A
   1.110 +    {
   1.111 +        JSAutoCompartment a(cx, A);
   1.112 +        JS::CompileOptions options(cx);
   1.113 +        options.setFileAndLine(__FILE__, 1);
   1.114 +        JS::RootedFunction fun(cx, JS_CompileFunction(cx, A, "f",
   1.115 +                mozilla::ArrayLength(argnames), argnames, source,
   1.116 +                strlen(source), options));
   1.117 +        CHECK(fun);
   1.118 +
   1.119 +        JSScript *script;
   1.120 +        CHECK(script = JS_GetFunctionScript(cx, fun));
   1.121 +
   1.122 +        CHECK(JS_GetScriptPrincipals(script) == principalsA);
   1.123 +        CHECK(obj = JS_GetFunctionObject(fun));
   1.124 +    }
   1.125 +
   1.126 +    // Clone into B
   1.127 +    {
   1.128 +        JSAutoCompartment b(cx, B);
   1.129 +        JS::RootedObject cloned(cx);
   1.130 +        CHECK(cloned = JS_CloneFunctionObject(cx, obj, B));
   1.131 +
   1.132 +        JS::RootedFunction fun(cx);
   1.133 +        JS::RootedValue clonedValue(cx, JS::ObjectValue(*cloned));
   1.134 +        CHECK(fun = JS_ValueToFunction(cx, clonedValue));
   1.135 +
   1.136 +        JSScript *script;
   1.137 +        CHECK(script = JS_GetFunctionScript(cx, fun));
   1.138 +
   1.139 +        CHECK(JS_GetScriptPrincipals(script) == principalsB);
   1.140 +
   1.141 +        JS::RootedValue v(cx);
   1.142 +        JS::RootedValue arg(cx, JS::Int32Value(1));
   1.143 +        CHECK(JS_CallFunctionValue(cx, B, clonedValue, arg, &v));
   1.144 +        CHECK(v.isObject());
   1.145 +
   1.146 +        JSObject *funobj = &v.toObject();
   1.147 +        CHECK(JS_ObjectIsFunction(cx, funobj));
   1.148 +        CHECK(fun = JS_ValueToFunction(cx, v));
   1.149 +        CHECK(script = JS_GetFunctionScript(cx, fun));
   1.150 +        CHECK(JS_GetScriptPrincipals(script) == principalsB);
   1.151 +    }
   1.152 +
   1.153 +    return true;
   1.154 +}
   1.155 +END_TEST(test_cloneScriptWithPrincipals)

mercurial