Ruby support #1
5 changed files with 63 additions and 27 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
fn test_func(vec3) {
|
fn test_func(vec3) {
|
||||||
if type_of(vec3) != "tests::rhai_tests::BevyVec3" { throw() }
|
if type_of(vec3) != "Vec3" { throw() }
|
||||||
// TODO: assert x,y,z
|
// TODO: assert x,y,z
|
||||||
mark_success();
|
mark_success();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,12 @@ pub struct LuaRuntime {
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct BevyEntity(pub Entity);
|
pub struct BevyEntity(pub Entity);
|
||||||
|
|
||||||
|
impl BevyEntity {
|
||||||
|
pub fn index(&self) -> u32 {
|
||||||
|
self.0.index()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl UserData for BevyEntity {}
|
impl UserData for BevyEntity {}
|
||||||
|
|
||||||
impl FromLua<'_> for BevyEntity {
|
impl FromLua<'_> for BevyEntity {
|
||||||
|
|
@ -63,6 +69,18 @@ impl BevyVec3 {
|
||||||
pub fn new(x: f32, y: f32, z: f32) -> Self {
|
pub fn new(x: f32, y: f32, z: f32) -> Self {
|
||||||
BevyVec3(Vec3 { x, y, z })
|
BevyVec3(Vec3 { x, y, z })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn x(&self) -> f32 {
|
||||||
|
self.0.x
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn y(&self) -> f32 {
|
||||||
|
self.0.y
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn z(&self) -> f32 {
|
||||||
|
self.0.z
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UserData for BevyVec3 {}
|
impl UserData for BevyVec3 {}
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,36 @@ pub struct RhaiScriptData {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct RhaiValue(pub rhai::Dynamic);
|
pub struct RhaiValue(pub rhai::Dynamic);
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct BevyEntity(pub Entity);
|
||||||
|
|
||||||
|
impl BevyEntity {
|
||||||
|
pub fn index(&self) -> u32 {
|
||||||
|
self.0.index()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct BevyVec3(pub Vec3);
|
||||||
|
|
||||||
|
impl BevyVec3 {
|
||||||
|
pub fn new(x: f32, y: f32, z: f32) -> Self {
|
||||||
|
Self(Vec3::new(x, y, z))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn x(&self) -> f32 {
|
||||||
|
self.0.x
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn y(&self) -> f32 {
|
||||||
|
self.0.y
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn z(&self) -> f32 {
|
||||||
|
self.0.z
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Runtime for RhaiRuntime {
|
impl Runtime for RhaiRuntime {
|
||||||
type Schedule = RhaiSchedule;
|
type Schedule = RhaiSchedule;
|
||||||
type ScriptAsset = RhaiScript;
|
type ScriptAsset = RhaiScript;
|
||||||
|
|
@ -66,7 +96,7 @@ impl Runtime for RhaiRuntime {
|
||||||
entity: Entity,
|
entity: Entity,
|
||||||
) -> Result<Self::ScriptData, ScriptingError> {
|
) -> Result<Self::ScriptData, ScriptingError> {
|
||||||
let mut scope = Scope::new();
|
let mut scope = Scope::new();
|
||||||
scope.push(ENTITY_VAR_NAME, entity);
|
scope.push(ENTITY_VAR_NAME, BevyEntity(entity));
|
||||||
|
|
||||||
let engine = &self.engine;
|
let engine = &self.engine;
|
||||||
|
|
||||||
|
|
@ -78,7 +108,7 @@ impl Runtime for RhaiRuntime {
|
||||||
.run_ast_with_scope(&mut scope, &ast)
|
.run_ast_with_scope(&mut scope, &ast)
|
||||||
.map_err(|e| ScriptingError::RuntimeError(Box::new(e)))?;
|
.map_err(|e| ScriptingError::RuntimeError(Box::new(e)))?;
|
||||||
|
|
||||||
scope.remove::<Entity>(ENTITY_VAR_NAME).unwrap();
|
scope.remove::<BevyEntity>(ENTITY_VAR_NAME).unwrap();
|
||||||
|
|
||||||
Ok(Self::ScriptData { ast, scope })
|
Ok(Self::ScriptData { ast, scope })
|
||||||
}
|
}
|
||||||
|
|
@ -114,7 +144,7 @@ impl Runtime for RhaiRuntime {
|
||||||
) -> Result<RhaiValue, ScriptingError> {
|
) -> Result<RhaiValue, ScriptingError> {
|
||||||
let ast = script_data.ast.clone();
|
let ast = script_data.ast.clone();
|
||||||
let scope = &mut script_data.scope;
|
let scope = &mut script_data.scope;
|
||||||
scope.push(ENTITY_VAR_NAME, entity);
|
scope.push(ENTITY_VAR_NAME, BevyEntity(entity));
|
||||||
let options = CallFnOptions::new().eval_ast(false);
|
let options = CallFnOptions::new().eval_ast(false);
|
||||||
let args = args
|
let args = args
|
||||||
.parse(&self.engine)
|
.parse(&self.engine)
|
||||||
|
|
@ -124,7 +154,7 @@ impl Runtime for RhaiRuntime {
|
||||||
let result = self
|
let result = self
|
||||||
.engine
|
.engine
|
||||||
.call_fn_with_options::<Dynamic>(options, scope, &ast, name, args);
|
.call_fn_with_options::<Dynamic>(options, scope, &ast, name, args);
|
||||||
scope.remove::<Entity>(ENTITY_VAR_NAME).unwrap();
|
scope.remove::<BevyEntity>(ENTITY_VAR_NAME).unwrap();
|
||||||
match result {
|
match result {
|
||||||
Ok(val) => Ok(RhaiValue(val)),
|
Ok(val) => Ok(RhaiValue(val)),
|
||||||
Err(e) => Err(ScriptingError::RuntimeError(Box::new(e))),
|
Err(e) => Err(ScriptingError::RuntimeError(Box::new(e))),
|
||||||
|
|
@ -188,8 +218,8 @@ impl Default for RhaiRuntime {
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
engine
|
engine
|
||||||
.register_type_with_name::<Entity>("Entity")
|
.register_type_with_name::<BevyEntity>("Entity")
|
||||||
.register_get("index", |entity: &mut Entity| entity.index());
|
.register_get("index", |entity: &mut BevyEntity| entity.index());
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
engine
|
engine
|
||||||
.register_type_with_name::<Promise<rhai::NativeCallContextStore, RhaiValue>>("Promise")
|
.register_type_with_name::<Promise<rhai::NativeCallContextStore, RhaiValue>>("Promise")
|
||||||
|
|
@ -202,9 +232,9 @@ impl Default for RhaiRuntime {
|
||||||
);
|
);
|
||||||
|
|
||||||
engine
|
engine
|
||||||
.register_type_with_name::<Vec3>("Vec3")
|
.register_type_with_name::<BevyVec3>("Vec3")
|
||||||
.register_fn("new_vec3", |x: f64, y: f64, z: f64| {
|
.register_fn("new_vec3", |x: f64, y: f64, z: f64| {
|
||||||
Vec3::new(x as f32, y as f32, z as f32)
|
BevyVec3(Vec3::new(x as f32, y as f32, z as f32))
|
||||||
})
|
})
|
||||||
.register_get("x", |vec: &mut Vec3| vec.x as f64)
|
.register_get("x", |vec: &mut Vec3| vec.x as f64)
|
||||||
.register_get("y", |vec: &mut Vec3| vec.y as f64)
|
.register_get("y", |vec: &mut Vec3| vec.y as f64)
|
||||||
|
|
@ -242,7 +272,7 @@ impl<T: Clone + 'static> FromRuntimeValueWithEngine<'_, RhaiRuntime> for T {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use super::{RhaiRuntime, RhaiScript, RhaiScriptData};
|
pub use super::{BevyEntity, BevyVec3, RhaiRuntime, RhaiScript, RhaiScriptData};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_tuple {
|
macro_rules! impl_tuple {
|
||||||
|
|
|
||||||
|
|
@ -176,7 +176,7 @@ fn then(r_self: magnus::Value) -> magnus::Value {
|
||||||
pub struct BevyEntity(pub Entity);
|
pub struct BevyEntity(pub Entity);
|
||||||
|
|
||||||
impl BevyEntity {
|
impl BevyEntity {
|
||||||
fn index(&self) -> u32 {
|
pub fn index(&self) -> u32 {
|
||||||
self.0.index()
|
self.0.index()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -458,7 +458,7 @@ macro_rules! scripting_tests {
|
||||||
runtime.add_function(
|
runtime.add_function(
|
||||||
String::from("rust_func"),
|
String::from("rust_func"),
|
||||||
|In((entity,)): In<($entity_type,)>, mut res: ResMut<State>| {
|
|In((entity,)): In<($entity_type,)>, mut res: ResMut<State>| {
|
||||||
res.index = Some(entity.0.index());
|
res.index = Some(entity.index());
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
@ -490,9 +490,9 @@ macro_rules! scripting_tests {
|
||||||
runtime.add_function(
|
runtime.add_function(
|
||||||
String::from("rust_func"),
|
String::from("rust_func"),
|
||||||
|In((v,)): In<($vec_type,)>, mut res: ResMut<State>| {
|
|In((v,)): In<($vec_type,)>, mut res: ResMut<State>| {
|
||||||
assert_eq!(v.0.x, 1.5);
|
assert_eq!(v.x(), 1.5);
|
||||||
assert_eq!(v.0.y, 2.5);
|
assert_eq!(v.y(), 2.5);
|
||||||
assert_eq!(v.0.z, -3.5);
|
assert_eq!(v.z(), -3.5);
|
||||||
res.success = true
|
res.success = true
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
@ -551,18 +551,6 @@ mod rhai_tests {
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_scriptum::runtimes::rhai::prelude::*;
|
use bevy_scriptum::runtimes::rhai::prelude::*;
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct BevyEntity(Entity);
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct BevyVec3(Vec3);
|
|
||||||
|
|
||||||
impl BevyVec3 {
|
|
||||||
fn new(x: f32, y: f32, z: f32) -> Self {
|
|
||||||
Self(Vec3 { x, y, z })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AssertStateKeyValue for RhaiRuntime {
|
impl AssertStateKeyValue for RhaiRuntime {
|
||||||
type ScriptData = RhaiScriptData;
|
type ScriptData = RhaiScriptData;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue