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
|
The `loadfile()` function operates similar to `loadstring()` but reads the
input to compile from the specified file path instead.
It compiles the given file name into a ucode program and returns the resulting
entry function.
Throws an exception on compilation or file i/o failure.
Returns the compiled program entry function.
Compile a simple program with default options
-- Testcase --
{%
let fn = loadfile('./files/test1.uc');
fn();
%}
-- End --
-- File test1.uc --
return 1 + 1;
-- End --
-- Expect stdout --
return 1 + 1;
-- End --
Compile a program in raw mode
-- Testcase --
{%
let fn = loadfile('./files/test2.uc', { raw_mode: true });
fn();
%}
-- End --
-- File test2.uc --
printf("%d\n", 1 + 1);
-- End --
-- Expect stdout --
2
-- End --
Compile a program in template mode
-- Testcase --
{%
let fn = loadfile('./files/test3.uc', { raw_mode: false });
fn();
%}
-- End --
-- File test3.uc --
{{ 1 + 1 }}
-- End --
-- Expect stdout --
2
-- End --
Override module search path during compilation (import should fail due to empty path)
-- Testcase --
{%
loadfile('./files/test4.uc', {
raw_mode: true,
module_search_path: []
});
%}
-- End --
-- File test4.uc --
import { readfile } from "fs";
-- End --
-- Expect stderr --
Runtime error: Unable to compile source file './files/test4.uc':
| Syntax error: Unable to resolve path for module 'fs'
| In line 1, byte 30:
|
| `import { readfile } from "fs";`
| Near here -------------------^
In line 5, byte 3:
` });`
^-- Near here
-- End --
Force dynamic loading of unknown extensions at compile time (should succeed)
-- Testcase --
{%
loadfile('./files/test5.uc', {
raw_mode: true,
force_dynlink_list: [ "doesnotexist" ]
});
print("OK\n");
%}
-- End --
-- File test5.uc --
import foo from "doesnotexist";
-- End --
-- Expect stdout --
OK
-- End --
Compiling a syntax error (should fail with syntax error exception)
-- Testcase --
{%
loadfile('./files/test6.uc', { raw_mode: true });
%}
-- End --
-- File test6.uc --
1 +
-- End --
-- Expect stderr --
Runtime error: Unable to compile source file './files/test6.uc':
| Syntax error: Expecting expression
| In line 1, byte 5:
|
| `1 +`
| ^-- Near here
In line 2, byte 49:
` loadfile('./files/test6.uc', { raw_mode: true });`
Near here -----------------------------------------^
-- End --
Test loading precompiled bytecode
-- Testcase --
{%
import { readlink } from 'fs';
system(`${readlink('/proc/self/exe')} -T, -c -o ./files/test7.uc -e 'Hello world\n'`);
let fn = loadfile('./files/test7.uc');
fn();
%}
-- End --
-- Expect stdout --
Hello world
-- End --
|