forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-compile-string.c
More file actions
122 lines (105 loc) · 3.45 KB
/
test-compile-string.c
File metadata and controls
122 lines (105 loc) · 3.45 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
/*===
*** test_string (duk_safe_call)
program code
return value is: '123'
myFile.js
return value is: '234'
compile rc=0
program code
return value is: '123'
compile rc=0
myFile.js
return value is: '234'
compile rc=1 -> SyntaxError: invalid object literal (line 1, end of input)
top: 0
==> rc=0, result='undefined'
*** test_lstring (duk_safe_call)
program code
return value is: '123'
myFile.js
return value is: '234'
compile rc=0
program code
return value is: '123'
compile rc=0
myFile.js
return value is: '234'
compile rc=1 -> SyntaxError: invalid object literal (line 1, end of input)
top: 0
==> rc=0, result='undefined'
===*/
static duk_ret_t test_string(duk_context *ctx, void *udata) {
duk_int_t rc;
(void) udata;
/* Normal compile */
duk_compile_string(ctx, 0, "print('program code'); 123");
duk_call(ctx, 0);
printf("return value is: '%s'\n", duk_to_string(ctx, -1));
duk_pop(ctx);
/* Normal compile with explicit filename */
duk_push_string(ctx, "myFile.js");
duk_compile_string_filename(ctx, 0, "print(Duktape.act(-2).function.fileName); 234");
duk_call(ctx, 0);
printf("return value is: '%s'\n", duk_to_string(ctx, -1));
duk_pop(ctx);
/* Protected compile, success */
rc = duk_pcompile_string(ctx, 0, "print('program code'); 123");
printf("compile rc=%ld\n", (long) rc);
duk_call(ctx, 0);
printf("return value is: '%s'\n", duk_to_string(ctx, -1));
duk_pop(ctx);
/* Protected compile with explicit filename, success */
duk_push_string(ctx, "myFile.js");
rc = duk_pcompile_string_filename(ctx, 0, "print(Duktape.act(-2).function.fileName); 234");
printf("compile rc=%ld\n", (long) rc);
duk_call(ctx, 0);
printf("return value is: '%s'\n", duk_to_string(ctx, -1));
duk_pop(ctx);
/* Protected compile, syntax error */
rc = duk_pcompile_string(ctx, 0, "print('program code'); 123; obj={");
printf("compile rc=%ld -> %s\n", (long) rc, duk_safe_to_string(ctx, -1));
duk_pop(ctx);
printf("top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
static duk_ret_t test_lstring(duk_context *ctx, void *udata) {
duk_int_t rc;
const char *src1 = "print('program code'); 123@";
const char *src2 = "print(Duktape.act(-2).function.fileName); 234@";
const char *src3 = "print('program code'); 123; obj={@";
(void) udata;
/* Normal compile */
duk_compile_lstring(ctx, 0, src1, strlen(src1) - 1);
duk_call(ctx, 0);
printf("return value is: '%s'\n", duk_to_string(ctx, -1));
duk_pop(ctx);
/* Normal compile with explicit filename */
duk_push_string(ctx, "myFile.js");
duk_compile_lstring_filename(ctx, 0, src2, strlen(src2) - 1);
duk_call(ctx, 0);
printf("return value is: '%s'\n", duk_to_string(ctx, -1));
duk_pop(ctx);
/* Protected compile, success */
rc = duk_pcompile_lstring(ctx, 0, src1, strlen(src1) - 1);
printf("compile rc=%ld\n", (long) rc);
duk_call(ctx, 0);
printf("return value is: '%s'\n", duk_to_string(ctx, -1));
duk_pop(ctx);
/* Protected compile with explicit filename, success */
duk_push_string(ctx, "myFile.js");
rc = duk_pcompile_lstring_filename(ctx, 0, src2, strlen(src2) - 1);
printf("compile rc=%ld\n", (long) rc);
duk_call(ctx, 0);
printf("return value is: '%s'\n", duk_to_string(ctx, -1));
duk_pop(ctx);
/* Protected compile, syntax error */
rc = duk_pcompile_lstring(ctx, 0, src3, strlen(src3) - 1);
printf("compile rc=%ld -> %s\n", (long) rc, duk_safe_to_string(ctx, -1));
duk_pop(ctx);
printf("top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
void test(duk_context *ctx) {
TEST_SAFE_CALL(test_string);
TEST_SAFE_CALL(test_lstring);
}