forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-get-string.c
More file actions
126 lines (111 loc) · 2.5 KB
/
Copy pathtest-get-string.c
File metadata and controls
126 lines (111 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
static void dump(const unsigned char *buf) {
const unsigned char *p = buf;
int first = 1;
if (!buf) {
printf("null\n");
return;
}
printf("[");
while (*p) {
if (first) {
first = 0;
} else {
printf(" ");
}
printf("%02x", (int) (*p));
p++;
}
printf("]\n");
}
/*===
*** test_get_string (duk_safe_call)
top: 10
index 0: null
index 1: null
index 2: null
index 3: null
index 4: []
index 5: [66 6f 6f]
index 6: [66 6f 6f]
index 7: [e1 88 b4 78 79 7a]
index 8: null
index 9: null
==> rc=0, result='undefined'
===*/
static duk_ret_t test_get_string(duk_context *ctx) {
duk_idx_t i, n;
duk_push_undefined(ctx);
duk_push_null(ctx);
duk_push_true(ctx);
duk_push_false(ctx);
duk_push_string(ctx, "");
duk_push_string(ctx, "foo");
duk_push_lstring(ctx, "foo\0bar", 7);
duk_push_string(ctx, "\xe1\x88\xb4xyz"); /* 4 chars, first char utf-8 encoded U+1234 */
duk_push_nan(ctx);
duk_push_object(ctx);
n = duk_get_top(ctx);
printf("top: %ld\n", (long) n);
for (i = 0; i < n; i++) {
printf("index %ld: ", (long) i);
dump((const unsigned char *) duk_get_string(ctx, i));
}
return 0;
}
/*===
*** test_get_lstring (duk_safe_call)
top: 10
index 0: length 0: null
index 0: null
index 1: length 0: null
index 1: null
index 2: length 0: null
index 2: null
index 3: length 0: null
index 3: null
index 4: length 0: []
index 4: []
index 5: length 3: [66 6f 6f]
index 5: [66 6f 6f]
index 6: length 7: [66 6f 6f]
index 6: [66 6f 6f]
index 7: length 6: [e1 88 b4 78 79 7a]
index 7: [e1 88 b4 78 79 7a]
index 8: length 0: null
index 8: null
index 9: length 0: null
index 9: null
==> rc=0, result='undefined'
===*/
static duk_ret_t test_get_lstring(duk_context *ctx) {
duk_idx_t i, n;
duk_push_undefined(ctx);
duk_push_null(ctx);
duk_push_true(ctx);
duk_push_false(ctx);
duk_push_string(ctx, "");
duk_push_string(ctx, "foo");
duk_push_lstring(ctx, "foo\0bar", 7);
duk_push_string(ctx, "\xe1\x88\xb4xyz"); /* 4 chars, first char utf-8 encoded U+1234 */
duk_push_nan(ctx);
duk_push_object(ctx);
n = duk_get_top(ctx);
printf("top: %ld\n", (long) n);
for (i = 0; i < n; i++) {
const char *buf;
size_t len;
len = (size_t) 0xdeadbeef;
buf = duk_get_lstring(ctx, i, &len);
printf("index %ld: length %lu: ",
(long) i, (unsigned long) len);
dump((const unsigned char *) buf);
buf = duk_get_lstring(ctx, i, NULL);
printf("index %ld: ", (long) i);
dump((const unsigned char *) buf);
}
return 0;
}
void test(duk_context *ctx) {
TEST_SAFE_CALL(test_get_string);
TEST_SAFE_CALL(test_get_lstring);
}