js/src/jsapi-tests/testEnclosingFunction.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     2  * vim: set ts=8 sts=4 et sw=4 tw=99:
     3  *
     4  * Test script cloning.
     5  */
     6 /* This Source Code Form is subject to the terms of the Mozilla Public
     7  * License, v. 2.0. If a copy of the MPL was not distributed with this
     8  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    10 #include "jsfriendapi.h"
    12 #include "js/OldDebugAPI.h"
    13 #include "jsapi-tests/tests.h"
    15 using namespace js;
    17 static JSScript *foundScript = nullptr;
    19 static bool
    20 CheckEnclosing(JSContext *cx, unsigned argc, Value *vp)
    21 {
    22     CallArgs args = CallArgsFromVp(argc, vp);
    24     foundScript = js::GetOutermostEnclosingFunctionOfScriptedCaller(cx);
    26     args.rval().set(UndefinedValue());
    27     return true;
    28 }
    30 BEGIN_TEST(test_enclosingFunction)
    31 {
    32     CHECK(JS_DefineFunction(cx, global, "checkEnclosing", CheckEnclosing, 0, 0));
    34     EXEC("checkEnclosing()");
    35     CHECK(foundScript == nullptr);
    37     RootedFunction fun(cx);
    39     JS::CompileOptions options(cx);
    40     options.setFileAndLine(__FILE__, __LINE__);
    42     const char s1chars[] = "checkEnclosing()";
    43     fun = JS_CompileFunction(cx, global, "s1", 0, nullptr, s1chars,
    44                              strlen(s1chars), options);
    45     CHECK(fun);
    46     EXEC("s1()");
    47     CHECK(foundScript == JS_GetFunctionScript(cx, fun));
    49     const char s2chars[] = "return function() { checkEnclosing() }";
    50     fun = JS_CompileFunction(cx, global, "s2", 0, nullptr, s2chars,
    51                              strlen(s2chars), options);
    52     CHECK(fun);
    53     EXEC("s2()()");
    54     CHECK(foundScript == JS_GetFunctionScript(cx, fun));
    56     const char s3chars[] = "return function() { let (x) { function g() { checkEnclosing() } return g() } }";
    57     fun = JS_CompileFunction(cx, global, "s3", 0, nullptr, s3chars,
    58                              strlen(s3chars), options);
    59     CHECK(fun);
    60     EXEC("s3()()");
    61     CHECK(foundScript == JS_GetFunctionScript(cx, fun));
    63     return true;
    64 }
    65 END_TEST(test_enclosingFunction)

mercurial