2022-08-09 11:07:45 +02:00
|
|
|
#![cfg(not(feature = "no_object"))]
|
|
|
|
use rhai::{CustomType, Engine, EvalAltResult, Position, TypeBuilder, INT};
|
2022-08-09 10:31:25 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn build_type() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
struct Vec3 {
|
2022-08-09 11:07:45 +02:00
|
|
|
x: INT,
|
|
|
|
y: INT,
|
|
|
|
z: INT,
|
2022-08-09 10:31:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Vec3 {
|
2022-08-09 11:07:45 +02:00
|
|
|
fn new(x: INT, y: INT, z: INT) -> Self {
|
2022-08-09 10:31:25 +02:00
|
|
|
Self { x, y, z }
|
|
|
|
}
|
2022-08-09 11:07:45 +02:00
|
|
|
fn get_x(&mut self) -> INT {
|
2022-08-09 10:31:25 +02:00
|
|
|
self.x
|
|
|
|
}
|
2022-08-09 11:07:45 +02:00
|
|
|
fn set_x(&mut self, x: INT) {
|
2022-08-09 10:31:25 +02:00
|
|
|
self.x = x
|
|
|
|
}
|
2022-08-09 11:07:45 +02:00
|
|
|
fn get_y(&mut self) -> INT {
|
2022-08-09 10:31:25 +02:00
|
|
|
self.y
|
|
|
|
}
|
2022-08-09 11:07:45 +02:00
|
|
|
fn set_y(&mut self, y: INT) {
|
2022-08-09 10:31:25 +02:00
|
|
|
self.y = y
|
|
|
|
}
|
2022-08-09 11:07:45 +02:00
|
|
|
fn get_z(&mut self) -> INT {
|
2022-08-09 10:31:25 +02:00
|
|
|
self.z
|
|
|
|
}
|
2022-08-09 11:07:45 +02:00
|
|
|
fn set_z(&mut self, z: INT) {
|
2022-08-09 10:31:25 +02:00
|
|
|
self.z = z
|
|
|
|
}
|
2022-08-09 11:07:45 +02:00
|
|
|
fn get_component(&mut self, idx: INT) -> Result<INT, Box<EvalAltResult>> {
|
2022-08-09 10:31:25 +02:00
|
|
|
match idx {
|
|
|
|
0 => Ok(self.x),
|
|
|
|
1 => Ok(self.y),
|
|
|
|
2 => Ok(self.z),
|
|
|
|
_ => Err(Box::new(EvalAltResult::ErrorIndexNotFound(
|
|
|
|
idx.into(),
|
|
|
|
Position::NONE,
|
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 14:00:43 +02:00
|
|
|
impl IntoIterator for Vec3 {
|
|
|
|
type Item = INT;
|
|
|
|
|
|
|
|
type IntoIter = std::array::IntoIter<INT, 3>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
<[INT; 3] as IntoIterator>::into_iter([self.x, self.y, self.z])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-09 10:31:25 +02:00
|
|
|
impl CustomType for Vec3 {
|
|
|
|
fn build(mut builder: TypeBuilder<Self>) {
|
|
|
|
builder
|
|
|
|
.with_name("Vec3")
|
2022-08-10 14:00:43 +02:00
|
|
|
.is_iterable()
|
2022-08-09 10:31:25 +02:00
|
|
|
.with_fn("vec3", Self::new)
|
|
|
|
.with_get_set("x", Self::get_x, Self::set_x)
|
|
|
|
.with_get_set("y", Self::get_y, Self::set_y)
|
2022-08-09 11:07:45 +02:00
|
|
|
.with_get_set("z", Self::get_z, Self::set_z);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
builder.with_indexer_get_result(Self::get_component);
|
2022-08-09 10:31:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.build_type::<Vec3>();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<Vec3>(
|
2022-08-09 11:07:45 +02:00
|
|
|
"
|
|
|
|
let v = vec3(1, 2, 3);
|
|
|
|
v
|
|
|
|
",
|
2022-08-09 10:31:25 +02:00
|
|
|
)?,
|
|
|
|
Vec3::new(1, 2, 3),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-08-09 11:07:45 +02:00
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let v = vec3(1, 2, 3);
|
|
|
|
v.x
|
|
|
|
",
|
2022-08-09 10:31:25 +02:00
|
|
|
)?,
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-08-09 11:07:45 +02:00
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let v = vec3(1, 2, 3);
|
|
|
|
v.y
|
|
|
|
",
|
2022-08-09 10:31:25 +02:00
|
|
|
)?,
|
|
|
|
2,
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-08-09 11:07:45 +02:00
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let v = vec3(1, 2, 3);
|
|
|
|
v.z
|
|
|
|
",
|
2022-08-09 10:31:25 +02:00
|
|
|
)?,
|
|
|
|
3,
|
|
|
|
);
|
2022-08-09 11:07:45 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-08-09 10:31:25 +02:00
|
|
|
assert!(engine.eval::<bool>(
|
2022-08-09 11:07:45 +02:00
|
|
|
"
|
|
|
|
let v = vec3(1, 2, 3);
|
|
|
|
v.x == v[0] && v.y == v[1] && v.z == v[2]
|
|
|
|
",
|
2022-08-09 10:31:25 +02:00
|
|
|
)?);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<Vec3>(
|
2022-08-09 11:07:45 +02:00
|
|
|
"
|
|
|
|
let v = vec3(1, 2, 3);
|
|
|
|
v.x = 5;
|
|
|
|
v.y = 6;
|
|
|
|
v.z = 7;
|
|
|
|
v
|
|
|
|
",
|
2022-08-09 10:31:25 +02:00
|
|
|
)?,
|
|
|
|
Vec3::new(5, 6, 7),
|
|
|
|
);
|
2022-08-10 14:00:43 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let sum = 0;
|
|
|
|
let v = vec3(1, 2, 3);
|
|
|
|
for i in v {
|
|
|
|
sum += i;
|
|
|
|
}
|
|
|
|
sum
|
|
|
|
",
|
|
|
|
)?,
|
|
|
|
6,
|
|
|
|
);
|
2022-08-09 10:31:25 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|