@@ -25,7 +25,7 @@ pub(crate) mod prelude {
25
25
pub ( crate ) use crate :: tests:: { eval, run} ;
26
26
pub ( crate ) use crate :: {
27
27
from_value, prepare, sources, span, vm_try, Any , Context , ContextError , Diagnostics ,
28
- FromValue , Hash , Item , ItemBuf , Module , Source , Sources , Value , Vm ,
28
+ FromValue , Hash , Item , ItemBuf , Module , Options , Source , Sources , Value , Vm ,
29
29
} ;
30
30
pub ( crate ) use futures_executor:: block_on;
31
31
@@ -43,11 +43,10 @@ use ::rust_alloc::sync::Arc;
43
43
44
44
use anyhow:: { Context as _, Error , Result } ;
45
45
46
- use crate :: alloc;
47
- use crate :: item:: IntoComponent ;
48
46
use crate :: runtime:: { Args , VmError } ;
49
47
use crate :: {
50
- termcolor, BuildError , Context , Diagnostics , FromValue , ItemBuf , Source , Sources , Unit , Vm ,
48
+ alloc, termcolor, BuildError , Context , Diagnostics , FromValue , Hash , Options , Source , Sources ,
49
+ Unit , Vm ,
51
50
} ;
52
51
53
52
/// An error that can be raised during testing.
@@ -97,9 +96,13 @@ pub fn compile_helper(source: &str, diagnostics: &mut Diagnostics) -> Result<Uni
97
96
let mut sources = Sources :: new ( ) ;
98
97
sources. insert ( Source :: new ( "main" , source) ?) ?;
99
98
99
+ let mut options = Options :: default ( ) ;
100
+ options. script ( true ) ;
101
+
100
102
let unit = crate :: prepare ( & mut sources)
101
103
. with_context ( & context)
102
104
. with_diagnostics ( diagnostics)
105
+ . with_options ( & options)
103
106
. build ( ) ?;
104
107
105
108
Ok ( unit)
@@ -111,10 +114,18 @@ pub fn vm(
111
114
context : & Context ,
112
115
sources : & mut Sources ,
113
116
diagnostics : & mut Diagnostics ,
117
+ script : bool ,
114
118
) -> Result < Vm , TestError > {
119
+ let mut options = Options :: default ( ) ;
120
+
121
+ if script {
122
+ options. script ( true ) ;
123
+ }
124
+
115
125
let result = crate :: prepare ( sources)
116
126
. with_context ( context)
117
127
. with_diagnostics ( diagnostics)
128
+ . with_options ( & options)
118
129
. build ( ) ;
119
130
120
131
let Ok ( unit) = result else {
@@ -134,24 +145,23 @@ pub fn vm(
134
145
135
146
/// Call the specified function in the given script sources.
136
147
#[ doc( hidden) ]
137
- pub fn run_helper < N , A , T > (
148
+ pub fn run_helper < T > (
138
149
context : & Context ,
139
150
sources : & mut Sources ,
140
151
diagnostics : & mut Diagnostics ,
141
- function : N ,
142
- args : A ,
152
+ args : impl Args ,
153
+ script : bool ,
143
154
) -> Result < T , TestError >
144
155
where
145
- N : IntoIterator ,
146
- N :: Item : IntoComponent ,
147
- A : Args ,
148
156
T : FromValue ,
149
157
{
150
- let mut vm = vm ( context, sources, diagnostics) ?;
158
+ let mut vm = vm ( context, sources, diagnostics, script ) ?;
151
159
152
- let item = ItemBuf :: with_item ( function) ?;
153
-
154
- let mut execute = vm. execute ( & item, args) . map_err ( TestError :: VmError ) ?;
160
+ let mut execute = if script {
161
+ vm. execute ( Hash :: EMPTY , args) . map_err ( TestError :: VmError ) ?
162
+ } else {
163
+ vm. execute ( [ "main" ] , args) . map_err ( TestError :: VmError ) ?
164
+ } ;
155
165
156
166
let output = :: futures_executor:: block_on ( execute. async_complete ( ) )
157
167
. into_result ( )
@@ -170,19 +180,16 @@ pub fn sources(source: &str) -> Sources {
170
180
}
171
181
172
182
/// Run the given source with diagnostics being printed to stderr.
173
- pub fn run < N , A , T > ( context : & Context , source : & str , function : N , args : A ) -> Result < T >
183
+ pub fn run < T > ( context : & Context , source : & str , args : impl Args , script : bool ) -> Result < T >
174
184
where
175
- N : IntoIterator ,
176
- N :: Item : IntoComponent ,
177
- A : Args ,
178
185
T : FromValue ,
179
186
{
180
187
let mut sources = Sources :: new ( ) ;
181
- sources. insert ( Source :: new ( "main" , source) ?) ?;
188
+ sources. insert ( Source :: memory ( source) ?) ?;
182
189
183
190
let mut diagnostics = Default :: default ( ) ;
184
191
185
- let e = match run_helper ( context, & mut sources, & mut diagnostics, function , args ) {
192
+ let e = match run_helper ( context, & mut sources, & mut diagnostics, args , script ) {
186
193
Ok ( value) => return Ok ( value) ,
187
194
Err ( e) => e,
188
195
} ;
@@ -229,7 +236,7 @@ where
229
236
let source = source. as_ref ( ) ;
230
237
let context = Context :: with_default_modules ( ) . expect ( "Failed to build context" ) ;
231
238
232
- match run ( & context, source, [ "main" ] , ( ) ) {
239
+ match run ( & context, source, ( ) , true ) {
233
240
Ok ( output) => output,
234
241
Err ( error) => {
235
242
panic ! ( "Program failed to run:\n {error}\n {source}" ) ;
@@ -257,10 +264,10 @@ macro_rules! rune_assert {
257
264
/// of native Rust data. This also accepts a tuple of arguments in the second
258
265
/// position, to pass native objects as arguments to the script.
259
266
macro_rules! rune_n {
260
- ( $module: expr, $args: expr, $ty : ty => $( $tt: tt) * ) => { {
267
+ ( $( mod $ module: expr, ) * $args: expr, $( $tt: tt) * ) => { {
261
268
let mut context = $crate:: Context :: with_default_modules( ) . expect( "Failed to build context" ) ;
262
- context. install( $module) . expect( "Failed to install native module" ) ;
263
- $crate:: tests:: run:: <_ , _ , $ty> ( & context, stringify!( $( $tt) * ) , [ "main" ] , $args) . expect( "Program ran unsuccessfully" )
269
+ $ ( context. install( & $module) . expect( "Failed to install native module" ) ; ) *
270
+ $crate:: tests:: run( & context, stringify!( $( $tt) * ) , $args, false ) . expect( "Program ran unsuccessfully" )
264
271
} } ;
265
272
}
266
273
@@ -277,7 +284,7 @@ macro_rules! assert_vm_error {
277
284
let mut diagnostics = Default :: default ( ) ;
278
285
279
286
let mut sources = $crate:: tests:: sources( $source) ;
280
- let e = match $crate:: tests:: run_helper:: <_ , _ , $ty>( & context, & mut sources, & mut diagnostics, [ "main" ] , ( ) ) {
287
+ let e = match $crate:: tests:: run_helper:: <$ty>( & context, & mut sources, & mut diagnostics, ( ) , true ) {
281
288
Err ( e) => e,
282
289
actual => {
283
290
expected!( "program error" , Err ( e) , actual, $source)
@@ -430,8 +437,6 @@ mod builtin_macros;
430
437
#[ cfg( not( miri) ) ]
431
438
mod capture;
432
439
#[ cfg( not( miri) ) ]
433
- mod collections;
434
- #[ cfg( not( miri) ) ]
435
440
mod comments;
436
441
#[ cfg( not( miri) ) ]
437
442
mod compiler_docs;
0 commit comments