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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
pub use futures_executor::block_on;
pub use rune::CompileError::*;
pub use rune::ParseError::*;
pub use rune::Warning::*;
pub use runestick::VmErrorKind::*;
use runestick::{Component, Item};
pub use runestick::{FnPtr, Meta, Span, Value};
use std::rc::Rc;
pub type Result<T, E = Error> = std::result::Result<T, E>;
pub type Error = Box<dyn std::error::Error + 'static + Send + Sync>;
pub async fn run_async<N, A, T>(function: N, args: A, source: &str) -> Result<T>
where
N: IntoIterator,
N::Item: Into<Component>,
A: runestick::IntoArgs,
T: runestick::FromValue,
{
let context = runestick::Context::with_default_packages()?;
let (unit, _) = rune::compile(&context, source)?;
let mut vm = runestick::Vm::new(Rc::new(context), Rc::new(unit));
let mut task: runestick::Task<T> = vm.call_function(Item::of(function), args)?;
let output = task.run_to_completion().await?;
Ok(output)
}
pub fn run<N, A, T>(function: N, args: A, source: &str) -> Result<T>
where
N: IntoIterator,
N::Item: Into<Component>,
A: runestick::IntoArgs,
T: runestick::FromValue,
{
block_on(run_async(function, args, source))
}
#[macro_export]
macro_rules! rune {
($ty:ty => $source:expr) => {
$crate::run::<_, (), $ty>(&["main"], (), $source).expect("program to run successfully")
};
}
#[macro_export]
macro_rules! assert_parse_error {
($source:expr, $pat:pat => $cond:expr) => {{
let context = runestick::Context::with_default_packages().unwrap();
let err = rune::compile(&context, $source).unwrap_err();
match err {
rune::Error::ParseError($pat) => ($cond),
_ => {
panic!("expected error `{}` but was `{:?}`", stringify!($pat), err);
}
}
}};
}
#[macro_export]
macro_rules! assert_vm_error {
($source:expr, $pat:pat => $cond:expr) => {{
let e = $crate::run::<_, _, ()>(&["main"], (), $source).unwrap_err();
let e = match e.downcast_ref::<runestick::VmError>() {
Some(e) => e,
None => {
panic!("{:?}", e);
}
};
let (kind, _) = e.kind().from_unwinded_ref();
match kind {
$pat => $cond,
_ => {
panic!("expected error `{}` but was `{:?}`", stringify!($pat), e);
}
}
}};
}
#[macro_export]
macro_rules! assert_parse {
($source:expr) => {{
let context = runestick::Context::with_default_packages().unwrap();
rune::compile(&context, $source).unwrap();
}};
}
#[macro_export]
macro_rules! assert_compile_error {
($source:expr, $pat:pat => $cond:expr) => {{
let context = runestick::Context::with_default_packages().unwrap();
let err = rune::compile(&context, $source).unwrap_err();
match err {
rune::Error::CompileError($pat) => ($cond),
_ => {
panic!("expected error `{}` but was `{:?}`", stringify!($pat), err);
}
}
}};
}
#[macro_export]
macro_rules! assert_warnings {
($source:expr $(, $pat:pat => $cond:expr)*) => {{
let context = runestick::Context::with_default_packages().unwrap();
let (_, warnings) = rune::compile(&context, $source).expect("source should compile");
assert!(!warnings.is_empty(), "no warnings produced");
let mut it = warnings.into_iter();
$(
let warning = it.next().expect("expected a warning");
match warning {
$pat => ($cond),
warning => {
panic!("expected warning `{}` but was `{:?}`", stringify!($pat), warning);
}
}
)*
assert!(it.next().is_none(), "there should be no more warnings");
}};
}