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
107 lines (88 loc) · 2.5 KB
/
Copy pathtest-error.c
File metadata and controls
107 lines (88 loc) · 2.5 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
/*===
*** test_1 (duk_pcall)
==> rc=1
ToString(error): RangeError: range error: 123
name: RangeError
message: range error: 123
code: undefined
fileName is a string: 1
lineNumber: 34
isNative: undefined
*** test_2 (duk_pcall)
==> rc=1
ToString(error): Error: arbitrary error code
name: Error
message: arbitrary error code
code: undefined
fileName is a string: 1
lineNumber: 43
isNative: undefined
*** test_3 (duk_pcall)
==> rc=1
ToString(error): TypeError: 105
name: TypeError
message: 105
code: undefined
fileName is a string: 1
lineNumber: 53
isNative: undefined
===*/
static duk_ret_t test_1(duk_context *ctx) {
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_2(duk_context *ctx) {
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_3(duk_context *ctx) {
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;
}
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), 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_1);
TEST(test_2);
TEST(test_3);
}