Debian Codegraph @

rhino/testsrc/tests/js1_5/extensions/regress-348986.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 var gTestfile = 'regress-348986.js';
7 //-----------------------------------------------------------------------------
8 var BUGNUMBER = 348986;
9 var summary = 'Recursion check of nested functions';
10 var actual = 'No Crash';
11 var expect = 'No Crash';
12
13
14 //-----------------------------------------------------------------------------
15 test();
16 //-----------------------------------------------------------------------------
17
18 function test()
19 {
20 enterFunc ('test');
21 printBugNumber(BUGNUMBER);
22 printStatus (summary);
23
24 // Construct f(){function f(){function f(){...}}} with maximum
25 // nested function declaration still does not hit recursion limit.
26
27 var deepestFunction;
28
29 var n = findActionMax(function(n) {
30 var prefix="function f(){";
31 var suffix="}";
32 var source = Array(n+1).join(prefix) + Array(n+1).join(suffix);
33 try {
34 deepestFunction = Function(source);
35 return true;
36 } catch (e) {
37 if (!(e instanceof InternalError))
38 throw e;
39 return false;
40 }
41
42 });
43
44 if (n == 0)
45 throw "unexpected";
46
47 print("Max nested function leveles:"+n);
48
49 n = findActionMax(function(n) {
50 try {
51 callAfterConsumingCStack(n, function() {});
52 return true;
53 } catch (e) {
54 if (!(e instanceof InternalError))
55 throw e;
56 return false;
57 }
58 });
59
60 print("Max callAfterConsumingCStack levels:"+n);
61
62 // Here n is max possible value when callAfterConsumingCStack(n, emptyFunction)
63 // does not trigger stackOverflow. Decrease it slightly to give some C stack
64 // space for deepestFunction.toSource()
65
66 n = Math.max(0, n - 10);
67 try {
68 var src = callAfterConsumingCStack(n, function() {
69 return deepestFunction.toSource();
70 });
71 throw "Test failed to hit the recursion limit.";
72 } catch (e) {
73 if (!(e instanceof InternalError))
74 throw e;
75 }
76
77 print('Done');
78 expect = true;
79 actual = true;
80 reportCompare(expect, true, summary);
81
82 exitFunc ('test');
83 }
84
85 function callAfterConsumingCStack(n, action)
86 {
87 var testObj = {
88 get propertyWithGetter() {
89 if (n == 0)
90 return action();
91 n--;
92 return this.propertyWithGetter;
93 }
94 };
95 return testObj.propertyWithGetter;
96 }
97
98
99 // Return the maximum positive value of N where action(N) still returns true
100 // or 0 if no such value exists.
101 function findActionMax(action)
102 {
103 var N, next, increase;
104
105 n = 0;
106 for (;;) {
107 var next = (n == 0 ? 1 : n * 2);
108 if (!isFinite(next) || !action(next))
109 break;
110 n = next;
111 }
112 if (n == 0)
113 return 0;
114
115 var increase = n / 2;
116 for (;;) {
117 var next = n + increase;
118 if (next == n)
119 break;
120 if (isFinite(next) && action(next)) {
121 n = next;
122 } else if (increase == 1) {
123 break;
124 } else {
125 increase = increase / 2;
126 }
127 }
128 return n;
129 }