forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-error.c
More file actions
185 lines (151 loc) · 4.37 KB
/
test-error.c
File metadata and controls
185 lines (151 loc) · 4.37 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
/*===
*** test_range_error (duk_pcall)
==> rc=1
ToString(error): RangeError: range error: 123
name: RangeError
message: range error: 123
code: undefined
fileName is a string: 1
lineNumber: 63
isNative: undefined
*** test_arbitrary_code (duk_pcall)
==> rc=1
ToString(error): Error: arbitrary error code
name: Error
message: arbitrary error code
code: undefined
fileName is a string: 1
lineNumber: 74
isNative: undefined
*** test_null_message (duk_pcall)
==> rc=1
ToString(error): TypeError: 6
name: TypeError
message: 6
code: undefined
fileName is a string: 1
lineNumber: 86
isNative: undefined
*** test_vararg (duk_pcall)
==> rc=1
ToString(error): RangeError: my error 123 234 foobar
name: RangeError
message: my error 123 234 foobar
code: undefined
fileName is a string: 1
lineNumber: 97
isNative: undefined
*** test_error_return (duk_pcall)
==> rc=1
ToString(error): URIError: invalid argument uri
name: URIError
message: invalid argument uri
code: undefined
fileName is a string: 1
lineNumber: 124
isNative: undefined
*** test_error_va_return (duk_pcall)
==> rc=1
ToString(error): RangeError: my error 123 234 foobar
name: RangeError
message: my error 123 234 foobar
code: undefined
fileName is a string: 1
lineNumber: 104
isNative: undefined
===*/
static duk_ret_t test_range_error(duk_context *ctx, void *udata) {
(void) udata;
duk_set_top(ctx, 0);
duk_error(ctx, DUK_ERR_RANGE_ERROR, "range error: %d", 123);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
static duk_ret_t test_arbitrary_code(duk_context *ctx, void *udata) {
(void) udata;
duk_set_top(ctx, 0);
duk_error(ctx, 1234567, "arbitrary error code");
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
static duk_ret_t test_null_message(duk_context *ctx, void *udata) {
(void) udata;
duk_set_top(ctx, 0);
/* error code replaces message automatically now if message is NULL */
duk_error(ctx, DUK_ERR_TYPE_ERROR, NULL);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
/* Vararg */
static void my_error_1(duk_context *ctx, duk_errcode_t errcode, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
duk_error_va(ctx, errcode, fmt, ap);
va_end(ap);
}
static duk_ret_t my_error_2(duk_context *ctx, duk_errcode_t errcode, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
return duk_error_va(ctx, errcode, fmt, ap);
va_end(ap);
}
static duk_ret_t test_vararg(duk_context *ctx, void *udata) {
(void) udata;
duk_set_top(ctx, 0);
my_error_1(ctx, DUK_ERR_RANGE_ERROR, "my error %d %d %s", 123, 234, "foobar");
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
static duk_ret_t test_error_return(duk_context *ctx, void *udata) {
(void) udata;
duk_set_top(ctx, 0);
return duk_error(ctx, DUK_ERR_URI_ERROR, "invalid argument uri");
}
static duk_ret_t test_error_va_return(duk_context *ctx, void *udata) {
(void) udata;
duk_set_top(ctx, 0);
return my_error_2(ctx, DUK_ERR_RANGE_ERROR, "my error %d %d %s", 123, 234, "foobar");
}
void dump_error(duk_context *ctx) {
duk_dup(ctx, -1);
printf("ToString(error): %s\n", duk_to_string(ctx, -1));
duk_pop(ctx);
duk_get_prop_string(ctx, -1, "name");
printf("name: %s\n", duk_to_string(ctx, -1));
duk_pop(ctx);
duk_get_prop_string(ctx, -1, "message");
printf("message: %s\n", duk_to_string(ctx, -1));
duk_pop(ctx);
/* 'code' is no longer set, test that it reads back as 'undefined' */
duk_get_prop_string(ctx, -1, "code");
printf("code: %s\n", duk_to_string(ctx, -1));
duk_pop(ctx);
duk_get_prop_string(ctx, -1, "fileName");
printf("fileName is a string: %d\n", (int) duk_is_string(ctx, -1));
duk_pop(ctx);
duk_get_prop_string(ctx, -1, "lineNumber");
printf("lineNumber: %ld\n", (long) duk_get_int(ctx, -1));
duk_pop(ctx);
/* 'isNative' has also been removed, check that it reads back as 'undefined' */
duk_get_prop_string(ctx, -1, "isNative");
printf("isNative: %s\n", duk_to_string(ctx, -1));
duk_pop(ctx);
}
/* use custom helper because of dump_error() */
#define TEST(func) do { \
printf("*** %s (duk_pcall)\n", #func); \
rc = duk_safe_call(ctx, (func), NULL, 0, 1); \
printf("==> rc=%d\n", (int) rc); \
dump_error(ctx); \
duk_pop(ctx); \
} while (0)
void test(duk_context *ctx) {
duk_int_t rc;
/* special test macro because of dump_error() */
TEST(test_range_error);
TEST(test_arbitrary_code);
TEST(test_null_message);
TEST(test_vararg);
TEST(test_error_return);
TEST(test_error_va_return);
}