1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsapi-tests/testEnclosingFunction.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,65 @@ 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 "jsfriendapi.h" 1.14 + 1.15 +#include "js/OldDebugAPI.h" 1.16 +#include "jsapi-tests/tests.h" 1.17 + 1.18 +using namespace js; 1.19 + 1.20 +static JSScript *foundScript = nullptr; 1.21 + 1.22 +static bool 1.23 +CheckEnclosing(JSContext *cx, unsigned argc, Value *vp) 1.24 +{ 1.25 + CallArgs args = CallArgsFromVp(argc, vp); 1.26 + 1.27 + foundScript = js::GetOutermostEnclosingFunctionOfScriptedCaller(cx); 1.28 + 1.29 + args.rval().set(UndefinedValue()); 1.30 + return true; 1.31 +} 1.32 + 1.33 +BEGIN_TEST(test_enclosingFunction) 1.34 +{ 1.35 + CHECK(JS_DefineFunction(cx, global, "checkEnclosing", CheckEnclosing, 0, 0)); 1.36 + 1.37 + EXEC("checkEnclosing()"); 1.38 + CHECK(foundScript == nullptr); 1.39 + 1.40 + RootedFunction fun(cx); 1.41 + 1.42 + JS::CompileOptions options(cx); 1.43 + options.setFileAndLine(__FILE__, __LINE__); 1.44 + 1.45 + const char s1chars[] = "checkEnclosing()"; 1.46 + fun = JS_CompileFunction(cx, global, "s1", 0, nullptr, s1chars, 1.47 + strlen(s1chars), options); 1.48 + CHECK(fun); 1.49 + EXEC("s1()"); 1.50 + CHECK(foundScript == JS_GetFunctionScript(cx, fun)); 1.51 + 1.52 + const char s2chars[] = "return function() { checkEnclosing() }"; 1.53 + fun = JS_CompileFunction(cx, global, "s2", 0, nullptr, s2chars, 1.54 + strlen(s2chars), options); 1.55 + CHECK(fun); 1.56 + EXEC("s2()()"); 1.57 + CHECK(foundScript == JS_GetFunctionScript(cx, fun)); 1.58 + 1.59 + const char s3chars[] = "return function() { let (x) { function g() { checkEnclosing() } return g() } }"; 1.60 + fun = JS_CompileFunction(cx, global, "s3", 0, nullptr, s3chars, 1.61 + strlen(s3chars), options); 1.62 + CHECK(fun); 1.63 + EXEC("s3()()"); 1.64 + CHECK(foundScript == JS_GetFunctionScript(cx, fun)); 1.65 + 1.66 + return true; 1.67 +} 1.68 +END_TEST(test_enclosingFunction)