2020-06-14 08:25:47 +02:00
|
|
|
#![cfg(not(feature = "unchecked"))]
|
2022-01-08 16:23:43 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, ParseErrorType, INT};
|
2020-06-14 08:25:47 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
use rhai::Array;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
use rhai::Map;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_max_string_size() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_max_string_size(10);
|
|
|
|
|
2020-08-08 10:03:14 +02:00
|
|
|
assert_eq!(
|
|
|
|
*engine
|
|
|
|
.compile(r#"let x = "hello, world!";"#)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err()
|
2022-07-29 03:42:30 +02:00
|
|
|
.err_type(),
|
2022-11-30 07:11:09 +01:00
|
|
|
ParseErrorType::LiteralTooLarge("Length of string".to_string(), 10)
|
2020-08-08 10:03:14 +02:00
|
|
|
);
|
2020-06-14 08:25:47 +02:00
|
|
|
|
2020-08-08 10:03:14 +02:00
|
|
|
assert_eq!(
|
|
|
|
*engine
|
|
|
|
.compile(r#"let x = "朝に紅顔、暮に白骨";"#)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err()
|
2022-07-29 03:42:30 +02:00
|
|
|
.err_type(),
|
2022-11-30 07:11:09 +01:00
|
|
|
ParseErrorType::LiteralTooLarge("Length of string".to_string(), 10)
|
2020-08-08 10:03:14 +02:00
|
|
|
);
|
2020-06-14 08:25:47 +02:00
|
|
|
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2022-01-06 15:10:16 +01:00
|
|
|
.run(
|
2020-06-14 08:25:47 +02:00
|
|
|
r#"
|
|
|
|
let x = "hello, ";
|
|
|
|
let y = "world!";
|
|
|
|
x + y
|
|
|
|
"#
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorDataTooLarge(..)
|
2020-06-14 08:25:47 +02:00
|
|
|
));
|
|
|
|
|
2020-07-01 16:21:43 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-06-15 15:49:02 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2022-01-06 15:10:16 +01:00
|
|
|
.run(
|
2020-06-15 15:49:02 +02:00
|
|
|
r#"
|
|
|
|
let x = "hello";
|
|
|
|
x.pad(100, '!');
|
|
|
|
x
|
|
|
|
"#
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorDataTooLarge(..)
|
2020-06-15 15:49:02 +02:00
|
|
|
));
|
|
|
|
|
2020-06-14 08:25:47 +02:00
|
|
|
engine.set_max_string_size(0);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<String>(
|
|
|
|
r#"
|
|
|
|
let x = "hello, ";
|
|
|
|
let y = "world!";
|
|
|
|
x + y
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
"hello, world!"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
fn test_max_array_size() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_max_array_size(10);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
engine.set_max_map_size(10);
|
|
|
|
|
2020-08-08 10:03:14 +02:00
|
|
|
assert_eq!(
|
|
|
|
*engine
|
2020-06-14 08:25:47 +02:00
|
|
|
.compile("let x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];")
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err()
|
2022-07-29 03:42:30 +02:00
|
|
|
.err_type(),
|
2020-08-08 10:03:14 +02:00
|
|
|
ParseErrorType::LiteralTooLarge("Size of array literal".to_string(), 10)
|
|
|
|
);
|
2020-06-14 08:25:47 +02:00
|
|
|
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2022-01-06 15:10:16 +01:00
|
|
|
.run(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-06-14 08:25:47 +02:00
|
|
|
let x = [1,2,3,4,5,6];
|
|
|
|
let y = [7,8,9,10,11,12];
|
|
|
|
x + y
|
|
|
|
"
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorDataTooLarge(..)
|
2020-06-14 08:25:47 +02:00
|
|
|
));
|
2020-07-01 16:21:43 +02:00
|
|
|
|
2022-01-08 16:23:43 +01:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let x = 42;
|
|
|
|
let y = [];
|
|
|
|
let f = || x;
|
|
|
|
for n in 0..10 {
|
|
|
|
y += x;
|
|
|
|
}
|
|
|
|
len(y)
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
10
|
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
|
|
|
.run(
|
|
|
|
"
|
|
|
|
let x = 42;
|
|
|
|
let y = [];
|
|
|
|
let f = || x;
|
|
|
|
for n in 0..11 {
|
|
|
|
y += x;
|
|
|
|
}
|
|
|
|
"
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorDataTooLarge(..)
|
2022-01-08 16:23:43 +01:00
|
|
|
));
|
|
|
|
|
2022-01-06 15:10:16 +01:00
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
|
|
|
.run(
|
|
|
|
"
|
|
|
|
let x = [ 42 ];
|
|
|
|
loop { x[0] = x; }
|
|
|
|
"
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorDataTooLarge(..)
|
2022-01-06 15:10:16 +01:00
|
|
|
));
|
|
|
|
|
2022-01-08 16:23:43 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let x = [1,2,3,4,5,6];
|
|
|
|
x.pad(10, 42);
|
|
|
|
len(x)
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
10
|
|
|
|
);
|
|
|
|
|
2020-07-01 16:21:43 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-06-15 15:49:02 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2022-01-06 15:10:16 +01:00
|
|
|
.run(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-06-15 15:49:02 +02:00
|
|
|
let x = [1,2,3,4,5,6];
|
2022-01-08 16:23:43 +01:00
|
|
|
x.pad(11, 42);
|
2020-06-15 15:49:02 +02:00
|
|
|
x
|
|
|
|
"
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorDataTooLarge(..)
|
2020-06-15 15:49:02 +02:00
|
|
|
));
|
2020-06-14 08:25:47 +02:00
|
|
|
|
2022-01-08 16:23:43 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
2022-12-27 03:09:40 +01:00
|
|
|
let x = [1,2];
|
2022-01-08 16:23:43 +01:00
|
|
|
len([x, x, x])
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
3
|
|
|
|
);
|
|
|
|
|
2020-06-14 08:25:47 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2022-01-06 15:10:16 +01:00
|
|
|
.run(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-06-14 08:25:47 +02:00
|
|
|
let x = [1,2,3];
|
|
|
|
[x, x, x, x]
|
|
|
|
"
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorDataTooLarge(..)
|
2020-06-14 08:25:47 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2022-01-06 15:10:16 +01:00
|
|
|
.run(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-06-14 08:25:47 +02:00
|
|
|
let x = #{a:1, b:2, c:3};
|
|
|
|
[x, x, x, x]
|
|
|
|
"
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorDataTooLarge(..)
|
2020-06-14 08:25:47 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2022-01-06 15:10:16 +01:00
|
|
|
.run(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-06-14 08:25:47 +02:00
|
|
|
let x = [1];
|
|
|
|
let y = [x, x];
|
|
|
|
let z = [y, y];
|
|
|
|
[z, z, z]
|
|
|
|
"
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorDataTooLarge(..)
|
2020-06-14 08:25:47 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
engine.set_max_array_size(0);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine
|
|
|
|
.eval::<Array>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-06-14 08:25:47 +02:00
|
|
|
let x = [1,2,3,4,5,6];
|
|
|
|
let y = [7,8,9,10,11,12];
|
|
|
|
x + y
|
|
|
|
"
|
|
|
|
)?
|
|
|
|
.len(),
|
|
|
|
12
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine
|
|
|
|
.eval::<Array>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-06-14 08:25:47 +02:00
|
|
|
let x = [1,2,3];
|
|
|
|
[x, x, x, x]
|
|
|
|
"
|
|
|
|
)?
|
|
|
|
.len(),
|
|
|
|
4
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
fn test_max_map_size() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_max_map_size(10);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
engine.set_max_array_size(10);
|
|
|
|
|
2020-08-08 10:03:14 +02:00
|
|
|
assert_eq!(
|
|
|
|
*engine
|
|
|
|
.compile(
|
|
|
|
"let x = #{a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8,i:9,j:10,k:11,l:12,m:13,n:14,o:15};"
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err()
|
2022-07-29 03:42:30 +02:00
|
|
|
.err_type(),
|
2020-08-08 10:03:14 +02:00
|
|
|
ParseErrorType::LiteralTooLarge(
|
|
|
|
"Number of properties in object map literal".to_string(),
|
|
|
|
10
|
|
|
|
)
|
|
|
|
);
|
2020-06-14 08:25:47 +02:00
|
|
|
|
2021-06-12 04:26:50 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2021-08-06 08:46:27 +02:00
|
|
|
.run(
|
2021-06-12 04:26:50 +02:00
|
|
|
"
|
|
|
|
let x = #{};
|
|
|
|
loop { x.a = x; }
|
|
|
|
"
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorDataTooLarge(..)
|
2021-06-12 04:26:50 +02:00
|
|
|
));
|
|
|
|
|
2020-06-14 08:25:47 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2022-01-06 15:10:16 +01:00
|
|
|
.run(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-06-14 08:25:47 +02:00
|
|
|
let x = #{a:1,b:2,c:3,d:4,e:5,f:6};
|
|
|
|
let y = #{g:7,h:8,i:9,j:10,k:11,l:12};
|
|
|
|
x + y
|
|
|
|
"
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorDataTooLarge(..)
|
2020-06-14 08:25:47 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2022-01-06 15:10:16 +01:00
|
|
|
.run(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-06-14 08:25:47 +02:00
|
|
|
let x = #{a:1,b:2,c:3};
|
|
|
|
#{u:x, v:x, w:x, z:x}
|
|
|
|
"
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorDataTooLarge(..)
|
2020-06-14 08:25:47 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2022-01-06 15:10:16 +01:00
|
|
|
.run(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-06-14 08:25:47 +02:00
|
|
|
let x = [1, 2, 3];
|
|
|
|
#{u:x, v:x, w:x, z:x}
|
|
|
|
"
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorDataTooLarge(..)
|
2020-06-14 08:25:47 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
engine.set_max_map_size(0);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine
|
|
|
|
.eval::<Map>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-06-14 08:25:47 +02:00
|
|
|
let x = #{a:1,b:2,c:3,d:4,e:5,f:6};
|
|
|
|
let y = #{g:7,h:8,i:9,j:10,k:11,l:12};
|
|
|
|
x + y
|
|
|
|
"
|
|
|
|
)?
|
|
|
|
.len(),
|
|
|
|
12
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine
|
|
|
|
.eval::<Map>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-06-14 08:25:47 +02:00
|
|
|
let x = #{a:1,b:2,c:3};
|
|
|
|
#{u:x, v:x, w:x, z:x}
|
|
|
|
"
|
|
|
|
)?
|
|
|
|
.len(),
|
|
|
|
4
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|