forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-def-prop-virtual.c
More file actions
156 lines (138 loc) · 5.11 KB
/
test-def-prop-virtual.c
File metadata and controls
156 lines (138 loc) · 5.11 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
/*
* Test DUK_DEFPROP_FORCE for virtual properties.
*/
/*===
*** test_array_length_enumerable_noforce (duk_safe_call)
set array .length enumerable
==> rc=1, result='TypeError: not configurable'
*** test_array_length_enumerable_force (duk_safe_call)
set array .length enumerable
==> rc=1, result='TypeError: not configurable'
*** test_array_length_configurable_noforce (duk_safe_call)
set array .length configurable
==> rc=1, result='TypeError: not configurable'
*** test_array_length_configurable_force (duk_safe_call)
set array .length configurable
==> rc=1, result='TypeError: not configurable'
*** test_array_length_overwrite_same_noforce (duk_safe_call)
["foo","bar","quux"]
final top: 0
==> rc=0, result='undefined'
*** test_array_length_overwrite_same_force (duk_safe_call)
["foo","bar","quux"]
final top: 0
==> rc=0, result='undefined'
*** test_array_length_overwrite_bigger_noforce (duk_safe_call)
==> rc=1, result='TypeError: not configurable'
*** test_array_length_overwrite_bigger_force (duk_safe_call)
["foo","bar","quux",undefined,undefined]
final top: 0
==> rc=0, result='undefined'
*** test_array_length_overwrite_smaller_noforce (duk_safe_call)
==> rc=1, result='TypeError: not configurable'
*** test_array_length_overwrite_smaller_force (duk_safe_call)
["foo"]
final top: 0
==> rc=0, result='undefined'
===*/
static duk_ret_t test__array_length_enumerable(duk_context *ctx, int force) {
duk_push_array(ctx);
duk_push_string(ctx, "length");
printf("set array .length enumerable\n");
fflush(stdout);
duk_def_prop(ctx, -2, DUK_DEFPROP_SET_ENUMERABLE | (force ? DUK_DEFPROP_FORCE : 0));
duk_eval_string(ctx,
"(function (v) { print(Duktape.enc('jx', Object.getOwnPropertyDescriptor(v, 'length' ))); })");
duk_dup(ctx, -2);
duk_call(ctx, 1);
duk_pop_2(ctx);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
static duk_ret_t test_array_length_enumerable_noforce(duk_context *ctx, void *udata) {
(void) udata;
return test__array_length_enumerable(ctx, 0);
}
static duk_ret_t test_array_length_enumerable_force(duk_context *ctx, void *udata) {
(void) udata;
return test__array_length_enumerable(ctx, 1);
}
static duk_ret_t test__array_length_configurable(duk_context *ctx, int force) {
duk_push_array(ctx);
duk_push_string(ctx, "length");
printf("set array .length configurable\n");
fflush(stdout);
duk_def_prop(ctx, -2, DUK_DEFPROP_SET_CONFIGURABLE | (force ? DUK_DEFPROP_FORCE : 0));
duk_eval_string(ctx,
"(function (v) { print(Duktape.enc('jx', Object.getOwnPropertyDescriptor(v, 'length' ))); })");
duk_dup(ctx, -2);
duk_call(ctx, 1);
duk_pop_2(ctx);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
static duk_ret_t test_array_length_configurable_noforce(duk_context *ctx, void *udata) {
(void) udata;
return test__array_length_configurable(ctx, 0);
}
static duk_ret_t test_array_length_configurable_force(duk_context *ctx, void *udata) {
(void) udata;
return test__array_length_configurable(ctx, 1);
}
static duk_ret_t test__length_overwrite(duk_context *ctx, int force, int new_len) {
duk_eval_string(ctx,
"(function () {\n"
" var arr = [ 'foo', 'bar', 'quux' ];\n"
" Object.defineProperty(arr, 'length', { writable: false });\n"
" return arr;\n"
"})()\n");
/* Array .length is not writable; with DUK_DEFPROP_FORCE we can still
* write it.
*/
duk_push_string(ctx, "length");
duk_push_int(ctx, new_len);
duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | (force ? DUK_DEFPROP_FORCE : 0));
duk_eval_string(ctx,
"(function (v) { print(Duktape.enc('jx', v)); })");
duk_dup(ctx, -2);
duk_call(ctx, 1);
duk_pop_2(ctx);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
static duk_ret_t test_array_length_overwrite_same_noforce(duk_context *ctx, void *udata) {
(void) udata;
return test__length_overwrite(ctx, 0, 3);
}
static duk_ret_t test_array_length_overwrite_same_force(duk_context *ctx, void *udata) {
(void) udata;
return test__length_overwrite(ctx, 1, 3);
}
static duk_ret_t test_array_length_overwrite_bigger_noforce(duk_context *ctx, void *udata) {
(void) udata;
return test__length_overwrite(ctx, 0, 5);
}
static duk_ret_t test_array_length_overwrite_bigger_force(duk_context *ctx, void *udata) {
(void) udata;
return test__length_overwrite(ctx, 1, 5);
}
static duk_ret_t test_array_length_overwrite_smaller_noforce(duk_context *ctx, void *udata) {
(void) udata;
return test__length_overwrite(ctx, 0, 1);
}
static duk_ret_t test_array_length_overwrite_smaller_force(duk_context *ctx, void *udata) {
(void) udata;
return test__length_overwrite(ctx, 1, 1);
}
void test(duk_context *ctx) {
TEST_SAFE_CALL(test_array_length_enumerable_noforce);
TEST_SAFE_CALL(test_array_length_enumerable_force);
TEST_SAFE_CALL(test_array_length_configurable_noforce);
TEST_SAFE_CALL(test_array_length_configurable_force);
TEST_SAFE_CALL(test_array_length_overwrite_same_noforce);
TEST_SAFE_CALL(test_array_length_overwrite_same_force);
TEST_SAFE_CALL(test_array_length_overwrite_bigger_noforce);
TEST_SAFE_CALL(test_array_length_overwrite_bigger_force);
TEST_SAFE_CALL(test_array_length_overwrite_smaller_noforce);
TEST_SAFE_CALL(test_array_length_overwrite_smaller_force);
}