Compare commits

..

2 commits

Author SHA1 Message Date
b339283901 error handling 2025-05-26 07:00:00 +02:00
db573426da conditional doctests 2025-05-26 07:00:00 +02:00
8 changed files with 76 additions and 23 deletions

View file

@ -120,7 +120,7 @@ Add the following to your `Cargo.toml`:
```toml
[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.

View file

@ -1,3 +1,3 @@
def test_func
print('abc' + 5)
raise
end

View file

@ -0,0 +1,2 @@
mark_called
raise

View file

@ -1,5 +1,5 @@
def test_func
rust_func.and_then do |x|
print('abc' + 5)
raise
end
end

View file

@ -21,8 +21,10 @@
//! ```no_run
//! use bevy::prelude::*;
//! use bevy_scriptum::prelude::*;
//! # #[cfg(feature = "lua")]
//! use bevy_scriptum::runtimes::lua::prelude::*;
//!
//! # #[cfg(feature = "lua")]
//! App::new()
//! .add_plugins(DefaultPlugins)
//! .add_scripting::<LuaRuntime>(|runtime| {
@ -42,11 +44,13 @@
//! ```no_run
//! use bevy::prelude::*;
//! use bevy_scriptum::prelude::*;
//! # #[cfg(feature = "lua")]
//! use bevy_scriptum::runtimes::lua::prelude::*;
//!
//! #[derive(Component)]
//! struct Player;
//!
//! # #[cfg(feature = "lua")]
//! App::new()
//! .add_plugins(DefaultPlugins)
//! .add_scripting::<LuaRuntime>(|runtime| {
@ -66,8 +70,10 @@
//! ```no_run
//! use bevy::prelude::*;
//! use bevy_scriptum::prelude::*;
//! # #[cfg(feature = "lua")]
//! use bevy_scriptum::runtimes::lua::prelude::*;
//!
//! # #[cfg(feature = "lua")]
//! App::new()
//! .add_plugins(DefaultPlugins)
//! .add_scripting::<LuaRuntime>(|runtime| {
@ -89,9 +95,11 @@
//! ```no_run
//! use bevy::prelude::*;
//! use bevy_scriptum::prelude::*;
//! # #[cfg(feature = "lua")]
//! use bevy_scriptum::runtimes::lua::prelude::*;
//!
//! struct MyPlugin;
//! # #[cfg(feature = "lua")]
//! impl Plugin for MyPlugin {
//! fn build(&self, app: &mut App) {
//! app.add_scripting_api::<LuaRuntime>(|runtime| {
@ -102,6 +110,7 @@
//! }
//! }
//!
//! # #[cfg(feature = "lua")]
//! App::new()
//! .add_plugins(DefaultPlugins)
//! .add_scripting::<LuaRuntime>(|_| {
@ -128,8 +137,10 @@
//! ```no_run
//! use bevy::prelude::*;
//! use bevy_scriptum::prelude::*;
//! # #[cfg(feature = "lua")]
//! use bevy_scriptum::runtimes::lua::prelude::*;
//!
//! # #[cfg(feature = "lua")]
//! App::new()
//! .add_plugins(DefaultPlugins)
//! .add_scripting::<LuaRuntime>(|runtime| {
@ -154,8 +165,10 @@
//! ```no_run
//! use bevy::prelude::*;
//! use bevy_scriptum::prelude::*;
//! # #[cfg(feature = "lua")]
//! use bevy_scriptum::runtimes::lua::prelude::*;
//!
//! # #[cfg(feature = "lua")]
//! App::new()
//! .add_plugins(DefaultPlugins)
//! .add_scripting::<LuaRuntime>(|runtime| {
@ -209,8 +222,10 @@
//! ```
//! use bevy::prelude::*;
//! use bevy_scriptum::prelude::*;
//! # #[cfg(feature = "lua")]
//! use bevy_scriptum::runtimes::lua::prelude::*;
//!
//! # #[cfg(feature = "lua")]
//! App::new()
//! .add_plugins(DefaultPlugins)
//! .add_scripting::<LuaRuntime>(|runtime| {

View file

@ -6,12 +6,12 @@ use std::{
};
use ::magnus::value::Opaque;
use anyhow::anyhow;
use bevy::{
asset::Asset,
ecs::{component::Component, entity::Entity, resource::Resource, schedule::ScheduleLabel},
math::Vec3,
reflect::TypePath,
tasks::futures_lite::io,
};
use magnus::{
DataType, DataTypeFunctions, IntoValue, Object, RClass, RModule, Ruby, TryConvert, TypedData,
@ -223,7 +223,7 @@ impl TryConvert for BevyVec3 {
impl From<magnus::Error> for ScriptingError {
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))
.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())
.expect("Failed to unset current entity handle");
RubyValue::new(value)
}));
Ok(RubyScriptData)
Ok::<Self::ScriptData, ScriptingError>(RubyScriptData)
}))
}
fn register_fn(

View file

@ -1,13 +1,13 @@
use bevy::{prelude::*, log::tracing};
use bevy::{log::tracing, prelude::*};
use std::{
fmt::Display,
sync::{Arc, Mutex},
};
use crate::{
Callback, Callbacks, Runtime, ScriptingError,
callback::FunctionCallEvent,
promise::{Promise, PromiseInner},
Callback, Callbacks, Runtime, ScriptingError,
};
use super::components::Script;

View file

@ -11,6 +11,24 @@ use bevy_scriptum::{FuncArgs, Runtime, prelude::*};
#[cfg(any(feature = "rhai", feature = "lua", feature = "ruby"))]
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"))]
fn build_test_app() -> App {
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]
fn call_script_function_that_casues_runtime_error() {
let mut app = build_test_app();
@ -347,11 +392,6 @@ macro_rules! scripting_tests {
fn rust_function_gets_called_from_script() {
let mut app = build_test_app();
#[derive(Default, Resource)]
struct TimesCalled {
times_called: u8,
}
app.world_mut().init_resource::<TimesCalled>();
app.add_scripting::<$runtime>(|runtime| {
@ -370,13 +410,7 @@ macro_rules! scripting_tests {
call_script_on_update_from_rust::<$runtime>,
);
assert_eq!(
app.world()
.get_resource::<TimesCalled>()
.unwrap()
.times_called,
1
);
assert_n_times_called!(app, 1);
}
#[test]