Compare commits
2 commits
469e9aca15
...
b339283901
| Author | SHA1 | Date | |
|---|---|---|---|
| b339283901 | |||
| db573426da |
8 changed files with 76 additions and 23 deletions
|
|
@ -120,7 +120,7 @@ Add the following to your `Cargo.toml`:
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy_scriptum = { version = "0.7", features = ["lua"] }
|
bevy_scriptum = { version = "0.8", features = ["lua"] }
|
||||||
```
|
```
|
||||||
|
|
||||||
or execute `cargo add bevy_scriptum --features lua` from your project directory.
|
or execute `cargo add bevy_scriptum --features lua` from your project directory.
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
def test_func
|
def test_func
|
||||||
print('abc' + 5)
|
raise
|
||||||
end
|
end
|
||||||
|
|
|
||||||
2
assets/tests/ruby/eval_that_causes_runtime_error.rb
Normal file
2
assets/tests/ruby/eval_that_causes_runtime_error.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
mark_called
|
||||||
|
raise
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
def test_func
|
def test_func
|
||||||
rust_func.and_then do |x|
|
rust_func.and_then do |x|
|
||||||
print('abc' + 5)
|
raise
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
15
src/lib.rs
15
src/lib.rs
|
|
@ -21,8 +21,10 @@
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! use bevy::prelude::*;
|
//! use bevy::prelude::*;
|
||||||
//! use bevy_scriptum::prelude::*;
|
//! use bevy_scriptum::prelude::*;
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! use bevy_scriptum::runtimes::lua::prelude::*;
|
//! use bevy_scriptum::runtimes::lua::prelude::*;
|
||||||
//!
|
//!
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! App::new()
|
//! App::new()
|
||||||
//! .add_plugins(DefaultPlugins)
|
//! .add_plugins(DefaultPlugins)
|
||||||
//! .add_scripting::<LuaRuntime>(|runtime| {
|
//! .add_scripting::<LuaRuntime>(|runtime| {
|
||||||
|
|
@ -42,11 +44,13 @@
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! use bevy::prelude::*;
|
//! use bevy::prelude::*;
|
||||||
//! use bevy_scriptum::prelude::*;
|
//! use bevy_scriptum::prelude::*;
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! use bevy_scriptum::runtimes::lua::prelude::*;
|
//! use bevy_scriptum::runtimes::lua::prelude::*;
|
||||||
//!
|
//!
|
||||||
//! #[derive(Component)]
|
//! #[derive(Component)]
|
||||||
//! struct Player;
|
//! struct Player;
|
||||||
//!
|
//!
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! App::new()
|
//! App::new()
|
||||||
//! .add_plugins(DefaultPlugins)
|
//! .add_plugins(DefaultPlugins)
|
||||||
//! .add_scripting::<LuaRuntime>(|runtime| {
|
//! .add_scripting::<LuaRuntime>(|runtime| {
|
||||||
|
|
@ -66,8 +70,10 @@
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! use bevy::prelude::*;
|
//! use bevy::prelude::*;
|
||||||
//! use bevy_scriptum::prelude::*;
|
//! use bevy_scriptum::prelude::*;
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! use bevy_scriptum::runtimes::lua::prelude::*;
|
//! use bevy_scriptum::runtimes::lua::prelude::*;
|
||||||
//!
|
//!
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! App::new()
|
//! App::new()
|
||||||
//! .add_plugins(DefaultPlugins)
|
//! .add_plugins(DefaultPlugins)
|
||||||
//! .add_scripting::<LuaRuntime>(|runtime| {
|
//! .add_scripting::<LuaRuntime>(|runtime| {
|
||||||
|
|
@ -89,9 +95,11 @@
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! use bevy::prelude::*;
|
//! use bevy::prelude::*;
|
||||||
//! use bevy_scriptum::prelude::*;
|
//! use bevy_scriptum::prelude::*;
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! use bevy_scriptum::runtimes::lua::prelude::*;
|
//! use bevy_scriptum::runtimes::lua::prelude::*;
|
||||||
//!
|
//!
|
||||||
//! struct MyPlugin;
|
//! struct MyPlugin;
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! impl Plugin for MyPlugin {
|
//! impl Plugin for MyPlugin {
|
||||||
//! fn build(&self, app: &mut App) {
|
//! fn build(&self, app: &mut App) {
|
||||||
//! app.add_scripting_api::<LuaRuntime>(|runtime| {
|
//! app.add_scripting_api::<LuaRuntime>(|runtime| {
|
||||||
|
|
@ -102,6 +110,7 @@
|
||||||
//! }
|
//! }
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! App::new()
|
//! App::new()
|
||||||
//! .add_plugins(DefaultPlugins)
|
//! .add_plugins(DefaultPlugins)
|
||||||
//! .add_scripting::<LuaRuntime>(|_| {
|
//! .add_scripting::<LuaRuntime>(|_| {
|
||||||
|
|
@ -128,8 +137,10 @@
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! use bevy::prelude::*;
|
//! use bevy::prelude::*;
|
||||||
//! use bevy_scriptum::prelude::*;
|
//! use bevy_scriptum::prelude::*;
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! use bevy_scriptum::runtimes::lua::prelude::*;
|
//! use bevy_scriptum::runtimes::lua::prelude::*;
|
||||||
//!
|
//!
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! App::new()
|
//! App::new()
|
||||||
//! .add_plugins(DefaultPlugins)
|
//! .add_plugins(DefaultPlugins)
|
||||||
//! .add_scripting::<LuaRuntime>(|runtime| {
|
//! .add_scripting::<LuaRuntime>(|runtime| {
|
||||||
|
|
@ -154,8 +165,10 @@
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! use bevy::prelude::*;
|
//! use bevy::prelude::*;
|
||||||
//! use bevy_scriptum::prelude::*;
|
//! use bevy_scriptum::prelude::*;
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! use bevy_scriptum::runtimes::lua::prelude::*;
|
//! use bevy_scriptum::runtimes::lua::prelude::*;
|
||||||
//!
|
//!
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! App::new()
|
//! App::new()
|
||||||
//! .add_plugins(DefaultPlugins)
|
//! .add_plugins(DefaultPlugins)
|
||||||
//! .add_scripting::<LuaRuntime>(|runtime| {
|
//! .add_scripting::<LuaRuntime>(|runtime| {
|
||||||
|
|
@ -209,8 +222,10 @@
|
||||||
//! ```
|
//! ```
|
||||||
//! use bevy::prelude::*;
|
//! use bevy::prelude::*;
|
||||||
//! use bevy_scriptum::prelude::*;
|
//! use bevy_scriptum::prelude::*;
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! use bevy_scriptum::runtimes::lua::prelude::*;
|
//! use bevy_scriptum::runtimes::lua::prelude::*;
|
||||||
//!
|
//!
|
||||||
|
//! # #[cfg(feature = "lua")]
|
||||||
//! App::new()
|
//! App::new()
|
||||||
//! .add_plugins(DefaultPlugins)
|
//! .add_plugins(DefaultPlugins)
|
||||||
//! .add_scripting::<LuaRuntime>(|runtime| {
|
//! .add_scripting::<LuaRuntime>(|runtime| {
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,12 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use ::magnus::value::Opaque;
|
use ::magnus::value::Opaque;
|
||||||
|
use anyhow::anyhow;
|
||||||
use bevy::{
|
use bevy::{
|
||||||
asset::Asset,
|
asset::Asset,
|
||||||
ecs::{component::Component, entity::Entity, resource::Resource, schedule::ScheduleLabel},
|
ecs::{component::Component, entity::Entity, resource::Resource, schedule::ScheduleLabel},
|
||||||
math::Vec3,
|
math::Vec3,
|
||||||
reflect::TypePath,
|
reflect::TypePath,
|
||||||
tasks::futures_lite::io,
|
|
||||||
};
|
};
|
||||||
use magnus::{
|
use magnus::{
|
||||||
DataType, DataTypeFunctions, IntoValue, Object, RClass, RModule, Ruby, TryConvert, TypedData,
|
DataType, DataTypeFunctions, IntoValue, Object, RClass, RModule, Ruby, TryConvert, TypedData,
|
||||||
|
|
@ -223,7 +223,7 @@ impl TryConvert for BevyVec3 {
|
||||||
|
|
||||||
impl From<magnus::Error> for ScriptingError {
|
impl From<magnus::Error> for ScriptingError {
|
||||||
fn from(value: magnus::Error) -> Self {
|
fn from(value: magnus::Error) -> Self {
|
||||||
ScriptingError::RuntimeError(Box::new(io::Error::other(value.to_string())))
|
ScriptingError::RuntimeError(anyhow!(value.to_string()).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -373,13 +373,15 @@ impl Runtime for RubyRuntime {
|
||||||
|
|
||||||
var.ivar_set("_current", BevyEntity(entity))
|
var.ivar_set("_current", BevyEntity(entity))
|
||||||
.expect("Failed to set current entity handle");
|
.expect("Failed to set current entity handle");
|
||||||
let value = ruby.eval::<magnus::value::Value>(&script).unwrap();
|
|
||||||
|
ruby.eval::<magnus::value::Value>(&script)
|
||||||
|
.map_err(|e| ScriptingError::RuntimeError(anyhow!(e.to_string()).into()))?;
|
||||||
|
|
||||||
var.ivar_set("_current", ruby.qnil().as_value())
|
var.ivar_set("_current", ruby.qnil().as_value())
|
||||||
.expect("Failed to unset current entity handle");
|
.expect("Failed to unset current entity handle");
|
||||||
|
|
||||||
RubyValue::new(value)
|
Ok::<Self::ScriptData, ScriptingError>(RubyScriptData)
|
||||||
}));
|
}))
|
||||||
Ok(RubyScriptData)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn register_fn(
|
fn register_fn(
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
use bevy::{prelude::*, log::tracing};
|
use bevy::{log::tracing, prelude::*};
|
||||||
use std::{
|
use std::{
|
||||||
fmt::Display,
|
fmt::Display,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
Callback, Callbacks, Runtime, ScriptingError,
|
||||||
callback::FunctionCallEvent,
|
callback::FunctionCallEvent,
|
||||||
promise::{Promise, PromiseInner},
|
promise::{Promise, PromiseInner},
|
||||||
Callback, Callbacks, Runtime, ScriptingError,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::components::Script;
|
use super::components::Script;
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,24 @@ use bevy_scriptum::{FuncArgs, Runtime, prelude::*};
|
||||||
#[cfg(any(feature = "rhai", feature = "lua", feature = "ruby"))]
|
#[cfg(any(feature = "rhai", feature = "lua", feature = "ruby"))]
|
||||||
static TRACING_SUBSCRIBER: OnceLock<()> = OnceLock::new();
|
static TRACING_SUBSCRIBER: OnceLock<()> = OnceLock::new();
|
||||||
|
|
||||||
|
#[cfg(any(feature = "rhai", feature = "lua", feature = "ruby"))]
|
||||||
|
#[derive(Default, Resource)]
|
||||||
|
struct TimesCalled {
|
||||||
|
times_called: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! assert_n_times_called {
|
||||||
|
($app: expr, $count: expr) => {
|
||||||
|
assert_eq!(
|
||||||
|
$app.world()
|
||||||
|
.get_resource::<TimesCalled>()
|
||||||
|
.unwrap()
|
||||||
|
.times_called,
|
||||||
|
$count
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "rhai", feature = "lua", feature = "ruby"))]
|
#[cfg(any(feature = "rhai", feature = "lua", feature = "ruby"))]
|
||||||
fn build_test_app() -> App {
|
fn build_test_app() -> App {
|
||||||
let mut app = App::new();
|
let mut app = App::new();
|
||||||
|
|
@ -224,6 +242,33 @@ macro_rules! scripting_tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn eval_that_casues_runtime_error_doesnt_panic() {
|
||||||
|
let mut app = build_test_app();
|
||||||
|
|
||||||
|
app.add_scripting::<$runtime>(|r| {
|
||||||
|
r.add_function(
|
||||||
|
String::from("mark_called"),
|
||||||
|
|mut times_called: ResMut<TimesCalled>| {
|
||||||
|
times_called.times_called += 1;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.init_resource::<TimesCalled>();
|
||||||
|
|
||||||
|
run_script::<$runtime, _, _>(
|
||||||
|
&mut app,
|
||||||
|
format!(
|
||||||
|
"tests/{}/eval_that_causes_runtime_error.{}",
|
||||||
|
$script, $extension
|
||||||
|
)
|
||||||
|
.to_string(),
|
||||||
|
|| {},
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_n_times_called!(app, 2);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn call_script_function_that_casues_runtime_error() {
|
fn call_script_function_that_casues_runtime_error() {
|
||||||
let mut app = build_test_app();
|
let mut app = build_test_app();
|
||||||
|
|
@ -347,11 +392,6 @@ macro_rules! scripting_tests {
|
||||||
fn rust_function_gets_called_from_script() {
|
fn rust_function_gets_called_from_script() {
|
||||||
let mut app = build_test_app();
|
let mut app = build_test_app();
|
||||||
|
|
||||||
#[derive(Default, Resource)]
|
|
||||||
struct TimesCalled {
|
|
||||||
times_called: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
app.world_mut().init_resource::<TimesCalled>();
|
app.world_mut().init_resource::<TimesCalled>();
|
||||||
|
|
||||||
app.add_scripting::<$runtime>(|runtime| {
|
app.add_scripting::<$runtime>(|runtime| {
|
||||||
|
|
@ -370,13 +410,7 @@ macro_rules! scripting_tests {
|
||||||
call_script_on_update_from_rust::<$runtime>,
|
call_script_on_update_from_rust::<$runtime>,
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_n_times_called!(app, 1);
|
||||||
app.world()
|
|
||||||
.get_resource::<TimesCalled>()
|
|
||||||
.unwrap()
|
|
||||||
.times_called,
|
|
||||||
1
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue