forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-check-require-stack.c
More file actions
199 lines (156 loc) · 4.12 KB
/
test-check-require-stack.c
File metadata and controls
199 lines (156 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*===
*** test_1 (duk_safe_call)
==> rc=1, result='RangeError: cannot push beyond allocated stack'
*** check_1 (duk_safe_call)
rc=1
final top: 1000
==> rc=0, result='undefined'
*** check_2 (duk_safe_call)
final top: 1000
==> rc=0, result='undefined'
*** check_3 (duk_safe_call)
rc=0
final top: 0
==> rc=0, result='undefined'
*** check_4 (duk_safe_call)
rc=0
final top: 0
==> rc=0, result='undefined'
*** require_1 (duk_safe_call)
final top: 1000
==> rc=0, result='undefined'
*** require_2 (duk_safe_call)
final top: 1000
==> rc=0, result='undefined'
*** require_3 (duk_safe_call)
==> rc=1, result='RangeError: valstack limit'
*** require_4 (duk_safe_call)
==> rc=1, result='RangeError: valstack limit'
===*/
/* demonstrate how pushing too many elements causes an error */
static duk_ret_t test_1(duk_context *ctx, void *udata) {
int i;
(void) udata;
for (i = 0; i < 1000; i++) {
duk_push_int(ctx, 123);
}
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
/* demonstrate how using one large duk_check_stack() before such
* a loop works.
*/
static duk_ret_t check_1_inner(duk_context *ctx) {
int i;
duk_ret_t rc;
rc = duk_check_stack(ctx, 1000);
printf("rc=%d\n", (int) rc);
for (i = 0; i < 1000; i++) {
duk_push_int(ctx, 123);
}
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
static duk_ret_t check_1(duk_context *ctx, void *udata) {
int i;
(void) udata;
/* dummy filler */
for (i = 0; i < 10000; i++) {
duk_require_stack(ctx, 1);
duk_push_uint(ctx, 123);
}
duk_push_c_function(ctx, check_1_inner, 0);
duk_call(ctx, 0);
return 0;
}
/* same test but with one element checks, once per loop */
static duk_ret_t check_2(duk_context *ctx, void *udata) {
int i;
duk_ret_t rc;
(void) udata;
for (i = 0; i < 1000; i++) {
rc = duk_check_stack(ctx, 1);
if (rc == 0) {
printf("duk_check_stack failed\n");
}
duk_push_int(ctx, 123);
}
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
/* try to extend value stack too much with duk_check_stack */
static duk_ret_t check_3(duk_context *ctx, void *udata) {
duk_ret_t rc;
(void) udata;
rc = duk_check_stack(ctx, 1000*1000*1000);
printf("rc=%d\n", (int) rc); /* should print 0: fail */
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
/* try to extend value stack too much; value is high enough to wrap */
static duk_ret_t check_4(duk_context *ctx, void *udata) {
duk_ret_t rc;
(void) udata;
rc = duk_check_stack(ctx, DUK_IDX_MAX);
printf("rc=%d\n", (int) rc); /* should print 0: fail */
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
/* same as check_1 but with duk_require_stack() */
static duk_ret_t require_1_inner(duk_context *ctx) {
int i;
duk_require_stack(ctx, 1000);
for (i = 0; i < 1000; i++) {
duk_push_int(ctx, 123);
}
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
static duk_ret_t require_1(duk_context *ctx, void *udata) {
int i;
(void) udata;
/* dummy filler */
for (i = 0; i < 10000; i++) {
duk_require_stack(ctx, 1);
duk_push_uint(ctx, 123);
}
duk_push_c_function(ctx, require_1_inner, 0);
duk_call(ctx, 0);
return 0;
}
/* same as check_2 but with duk_require_stack() */
static duk_ret_t require_2(duk_context *ctx, void *udata) {
int i;
(void) udata;
for (i = 0; i < 1000; i++) {
duk_require_stack(ctx, 1);
duk_push_int(ctx, 123);
}
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
/* same as check_3 but with duk_require_stack */
static duk_ret_t require_3(duk_context *ctx, void *udata) {
(void) udata;
duk_require_stack(ctx, 1000*1000*1000);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
/* same as check_4 but with duk_require_stack */
static duk_ret_t require_4(duk_context *ctx, void *udata) {
(void) udata;
duk_require_stack(ctx, DUK_IDX_MAX);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
void test(duk_context *ctx) {
TEST_SAFE_CALL(test_1);
TEST_SAFE_CALL(check_1);
TEST_SAFE_CALL(check_2);
TEST_SAFE_CALL(check_3);
TEST_SAFE_CALL(check_4);
TEST_SAFE_CALL(require_1);
TEST_SAFE_CALL(require_2);
TEST_SAFE_CALL(require_3);
TEST_SAFE_CALL(require_4);
}