|
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/. */ |
|
9 |
|
10 #include "jsfriendapi.h" |
|
11 |
|
12 #include "js/OldDebugAPI.h" |
|
13 #include "jsapi-tests/tests.h" |
|
14 |
|
15 using namespace js; |
|
16 |
|
17 static JSScript *foundScript = nullptr; |
|
18 |
|
19 static bool |
|
20 CheckEnclosing(JSContext *cx, unsigned argc, Value *vp) |
|
21 { |
|
22 CallArgs args = CallArgsFromVp(argc, vp); |
|
23 |
|
24 foundScript = js::GetOutermostEnclosingFunctionOfScriptedCaller(cx); |
|
25 |
|
26 args.rval().set(UndefinedValue()); |
|
27 return true; |
|
28 } |
|
29 |
|
30 BEGIN_TEST(test_enclosingFunction) |
|
31 { |
|
32 CHECK(JS_DefineFunction(cx, global, "checkEnclosing", CheckEnclosing, 0, 0)); |
|
33 |
|
34 EXEC("checkEnclosing()"); |
|
35 CHECK(foundScript == nullptr); |
|
36 |
|
37 RootedFunction fun(cx); |
|
38 |
|
39 JS::CompileOptions options(cx); |
|
40 options.setFileAndLine(__FILE__, __LINE__); |
|
41 |
|
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)); |
|
48 |
|
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)); |
|
55 |
|
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)); |
|
62 |
|
63 return true; |
|
64 } |
|
65 END_TEST(test_enclosingFunction) |