michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * michael@0: * Test script cloning. michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "jsfriendapi.h" michael@0: michael@0: #include "js/OldDebugAPI.h" michael@0: #include "jsapi-tests/tests.h" michael@0: michael@0: using namespace js; michael@0: michael@0: static JSScript *foundScript = nullptr; michael@0: michael@0: static bool michael@0: CheckEnclosing(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: foundScript = js::GetOutermostEnclosingFunctionOfScriptedCaller(cx); michael@0: michael@0: args.rval().set(UndefinedValue()); michael@0: return true; michael@0: } michael@0: michael@0: BEGIN_TEST(test_enclosingFunction) michael@0: { michael@0: CHECK(JS_DefineFunction(cx, global, "checkEnclosing", CheckEnclosing, 0, 0)); michael@0: michael@0: EXEC("checkEnclosing()"); michael@0: CHECK(foundScript == nullptr); michael@0: michael@0: RootedFunction fun(cx); michael@0: michael@0: JS::CompileOptions options(cx); michael@0: options.setFileAndLine(__FILE__, __LINE__); michael@0: michael@0: const char s1chars[] = "checkEnclosing()"; michael@0: fun = JS_CompileFunction(cx, global, "s1", 0, nullptr, s1chars, michael@0: strlen(s1chars), options); michael@0: CHECK(fun); michael@0: EXEC("s1()"); michael@0: CHECK(foundScript == JS_GetFunctionScript(cx, fun)); michael@0: michael@0: const char s2chars[] = "return function() { checkEnclosing() }"; michael@0: fun = JS_CompileFunction(cx, global, "s2", 0, nullptr, s2chars, michael@0: strlen(s2chars), options); michael@0: CHECK(fun); michael@0: EXEC("s2()()"); michael@0: CHECK(foundScript == JS_GetFunctionScript(cx, fun)); michael@0: michael@0: const char s3chars[] = "return function() { let (x) { function g() { checkEnclosing() } return g() } }"; michael@0: fun = JS_CompileFunction(cx, global, "s3", 0, nullptr, s3chars, michael@0: strlen(s3chars), options); michael@0: CHECK(fun); michael@0: EXEC("s3()()"); michael@0: CHECK(foundScript == JS_GetFunctionScript(cx, fun)); michael@0: michael@0: return true; michael@0: } michael@0: END_TEST(test_enclosingFunction)