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>> {
|
2022-08-29 08:27:05 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2022-08-09 10:31:25 +02:00
|
|
|
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;
|
2022-08-09 12:18:41 +02:00
|
|
|
type IntoIter = std::vec::IntoIter<Self::Item>;
|
2022-08-10 14:00:43 +02:00
|
|
|
|
2022-09-28 06:06:22 +02:00
|
|
|
#[inline]
|
|
|
|
#[must_use]
|
2022-08-10 14:00:43 +02:00
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
2022-08-09 12:18:41 +02:00
|
|
|
vec![self.x, self.y, self.z].into_iter()
|
2022-08-10 14:00:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2022-08-29 08:27:05 +02:00
|
|
|
.with_fn("==", |x: &mut Vec3, y: Vec3| *x == y)
|
|
|
|
.with_fn("!=", |x: &mut Vec3, y: Vec3| *x != y)
|
2022-08-09 10:31:25 +02:00
|
|
|
.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"))]
|
2022-08-22 16:16:26 +02:00
|
|
|
builder.with_indexer_get(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(())
|
|
|
|
}
|