2024-10-30 07:08:25 +00:00
|
|
|
use dust_lang::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn panic() {
|
|
|
|
let source = "panic(\"Goodbye world!\", 42)";
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-06 20:40:37 +00:00
|
|
|
compile(source),
|
2024-10-30 07:08:25 +00:00
|
|
|
Ok(Chunk::with_data(
|
|
|
|
None,
|
2024-11-16 10:16:51 +00:00
|
|
|
FunctionType {
|
|
|
|
type_parameters: None,
|
|
|
|
value_parameters: None,
|
|
|
|
return_type: Box::new(Type::None),
|
|
|
|
},
|
2024-10-30 07:08:25 +00:00
|
|
|
vec![
|
2024-11-28 02:13:35 +00:00
|
|
|
(
|
2024-11-28 06:10:49 +00:00
|
|
|
Instruction::load_constant(Destination::Register(0), 0, false),
|
2024-11-28 02:13:35 +00:00
|
|
|
Span(6, 22)
|
|
|
|
),
|
|
|
|
(
|
2024-11-28 06:10:49 +00:00
|
|
|
Instruction::load_constant(Destination::Register(1), 1, false),
|
2024-11-28 02:13:35 +00:00
|
|
|
Span(24, 26)
|
|
|
|
),
|
2024-10-30 07:08:25 +00:00
|
|
|
(
|
2024-11-28 06:10:49 +00:00
|
|
|
Instruction::call_native(Destination::Register(2), NativeFunction::Panic, 2),
|
2024-10-30 07:08:25 +00:00
|
|
|
Span(0, 27)
|
|
|
|
),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::r#return(false), Span(27, 27))
|
2024-10-30 07:08:25 +00:00
|
|
|
],
|
2024-11-11 00:28:21 +00:00
|
|
|
vec![
|
2024-11-16 06:29:21 +00:00
|
|
|
ConcreteValue::string("Goodbye world!"),
|
|
|
|
ConcreteValue::Integer(42)
|
2024-11-11 00:28:21 +00:00
|
|
|
],
|
2024-10-30 07:08:25 +00:00
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-28 00:43:50 +00:00
|
|
|
run(source),
|
2024-10-30 07:08:25 +00:00
|
|
|
Err(DustError::Runtime {
|
2024-10-30 13:32:46 +00:00
|
|
|
error: VmError::NativeFunction(NativeFunctionError::Panic {
|
2024-10-30 07:08:25 +00:00
|
|
|
message: Some("Goodbye world! 42".to_string()),
|
|
|
|
position: Span(0, 27)
|
2024-10-30 13:32:46 +00:00
|
|
|
}),
|
2024-10-30 07:08:25 +00:00
|
|
|
source
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2024-10-30 09:16:34 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_string() {
|
|
|
|
let source = "to_string(42)";
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-06 20:40:37 +00:00
|
|
|
compile(source),
|
2024-10-30 09:16:34 +00:00
|
|
|
Ok(Chunk::with_data(
|
|
|
|
None,
|
2024-11-16 10:16:51 +00:00
|
|
|
FunctionType {
|
|
|
|
type_parameters: None,
|
|
|
|
value_parameters: None,
|
2024-11-28 00:43:50 +00:00
|
|
|
return_type: Box::new(Type::String),
|
2024-11-16 10:16:51 +00:00
|
|
|
},
|
2024-10-30 09:16:34 +00:00
|
|
|
vec![
|
2024-11-28 02:13:35 +00:00
|
|
|
(
|
2024-11-28 06:10:49 +00:00
|
|
|
Instruction::load_constant(Destination::Register(0), 0, false),
|
2024-11-28 02:13:35 +00:00
|
|
|
Span(10, 12)
|
|
|
|
),
|
2024-10-30 09:16:34 +00:00
|
|
|
(
|
2024-11-28 06:10:49 +00:00
|
|
|
Instruction::call_native(Destination::Register(1), NativeFunction::ToString, 1),
|
2024-10-30 09:16:34 +00:00
|
|
|
Span(0, 13)
|
|
|
|
),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::r#return(true), Span(13, 13))
|
2024-10-30 09:16:34 +00:00
|
|
|
],
|
2024-11-16 06:29:21 +00:00
|
|
|
vec![ConcreteValue::Integer(42)],
|
2024-10-30 09:16:34 +00:00
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-11-28 00:43:50 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::string("42"))))
|
2024-10-30 09:16:34 +00:00
|
|
|
}
|