Skip to content

Commit d39135c

Browse files
committed
remove rhai and rune support for now
1 parent de4d2da commit d39135c

File tree

14 files changed

+433
-361
lines changed

14 files changed

+433
-361
lines changed

Cargo.toml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ name = "bevy_mod_scripting"
1717
path = "src/lib.rs"
1818

1919
[package.metadata."docs.rs"]
20-
features = ["lua54", "rhai", "rune"]
20+
features = ["lua54"]
2121

2222
[features]
2323
default = ["core_functions", "bevy_bindings", "unsafe_lua_modules"]
@@ -44,25 +44,25 @@ mlua_macros = ["bevy_mod_scripting_lua/mlua_macros"]
4444
mlua_async = ["bevy_mod_scripting_lua/mlua_async"]
4545

4646
## rhai
47-
rhai = ["bevy_mod_scripting_rhai"]
47+
# rhai = ["bevy_mod_scripting_rhai"]
4848

4949
## rune
50-
rune = ["bevy_mod_scripting_rune"]
50+
# rune = ["bevy_mod_scripting_rune"]
5151

5252
[dependencies]
5353
bevy = { workspace = true }
5454
bevy_mod_scripting_core = { workspace = true }
5555
bevy_mod_scripting_lua = { path = "crates/languages/bevy_mod_scripting_lua", version = "0.9.0-alpha.2", optional = true }
56-
bevy_mod_scripting_rhai = { path = "crates/languages/bevy_mod_scripting_rhai", version = "0.9.0-alpha.2", optional = true }
57-
bevy_mod_scripting_rune = { path = "crates/languages/bevy_mod_scripting_rune", version = "0.9.0-alpha.2", optional = true }
56+
# bevy_mod_scripting_rhai = { path = "crates/languages/bevy_mod_scripting_rhai", version = "0.9.0-alpha.2", optional = true }
57+
# bevy_mod_scripting_rune = { path = "crates/languages/bevy_mod_scripting_rune", version = "0.9.0-alpha.2", optional = true }
5858
bevy_mod_scripting_functions = { workspace = true }
5959

6060
[workspace.dependencies]
6161
bevy = { version = "0.15.0", default-features = false }
6262
bevy_mod_scripting_core = { path = "crates/bevy_mod_scripting_core", version = "0.9.0-alpha.2" }
6363
bevy_mod_scripting_functions = { path = "crates/bevy_mod_scripting_functions", version = "0.9.0-alpha.2", default-features = false }
6464
mlua = { version = "0.10" }
65-
rhai = { version = "1.20.1" }
65+
# rhai = { version = "1.20.1" }
6666

6767
# test utilities
6868
script_integration_test_harness = { path = "crates/script_integration_test_harness" }
@@ -73,15 +73,15 @@ bevy = { workspace = true, default-features = true }
7373
clap = { version = "4.1", features = ["derive"] }
7474
rand = "0.8.5"
7575
bevy_console = "0.13"
76-
rhai-rand = "0.1"
76+
# rhai-rand = "0.1"
7777
ansi-parser = "0.9"
7878

7979
[workspace]
8080
members = [
8181
"crates/bevy_mod_scripting_core",
8282
"crates/languages/bevy_mod_scripting_lua",
83-
"crates/languages/bevy_mod_scripting_rhai",
84-
"crates/languages/bevy_mod_scripting_rune",
83+
# "crates/languages/bevy_mod_scripting_rhai",
84+
# "crates/languages/bevy_mod_scripting_rune",
8585
"crates/test_utils",
8686
"crates/bevy_mod_scripting_functions",
8787
"crates/xtask",
@@ -112,9 +112,4 @@ debug = true
112112
[[example]]
113113
name = "game_of_life"
114114
path = "examples/game_of_life.rs"
115-
required-features = [
116-
"lua54",
117-
"rhai",
118-
"bevy/file_watcher",
119-
"bevy/multi_threaded",
120-
]
115+
required-features = ["lua54", "bevy/file_watcher", "bevy/multi_threaded"]

crates/bevy_mod_scripting_core/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ doc_always = []
2121

2222
# if enabled enables some common mlua trait implementations
2323
mlua_impls = ["mlua"]
24-
rhai_impls = ["rhai"]
24+
# rhai_impls = ["rhai"]
2525

2626
[dependencies]
2727
mlua = { optional = true, workspace = true }
28-
rhai = { optional = true, workspace = true }
28+
# rhai = { optional = true, workspace = true }
2929

3030
bevy = { workspace = true, default-features = false, features = [
3131
"bevy_asset",

crates/bevy_mod_scripting_core/src/bindings/function/script_function.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl ScriptFunctionRegistryArc {
359359
}
360360

361361
#[derive(Debug, PartialEq, Eq, Hash)]
362-
struct FunctionKey {
362+
pub struct FunctionKey {
363363
name: Cow<'static, str>,
364364
namespace: Namespace,
365365
}
@@ -465,6 +465,11 @@ impl ScriptFunctionRegistry {
465465

466466
Ok(seed.chain(overloads))
467467
}
468+
469+
/// Iterates over all functions including overloads
470+
pub fn iter_all(&self) -> impl Iterator<Item = (&FunctionKey, &DynamicScriptFunction)> {
471+
self.functions.iter()
472+
}
468473
}
469474

470475
macro_rules! count {

crates/bevy_mod_scripting_core/src/bindings/world.rs

Lines changed: 74 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
88
use super::{
99
access_map::{AccessCount, AccessMap, ReflectAccessId},
10-
function::script_function::AppScriptFunctionRegistry,
10+
function::{
11+
namespace::Namespace,
12+
script_function::{AppScriptFunctionRegistry, CallerContext, DynamicScriptFunction},
13+
},
14+
script_value::ScriptValue,
1115
AppReflectAllocator, ReflectBase, ReflectBaseType, ReflectReference, ScriptTypeRegistration,
1216
};
1317
use crate::{error::InteropError, with_access_read, with_access_write, with_global_access};
@@ -25,6 +29,7 @@ use bevy::{
2529
};
2630
use std::{
2731
any::TypeId,
32+
borrow::Cow,
2833
cell::RefCell,
2934
fmt::Debug,
3035
sync::{Arc, Weak},
@@ -218,6 +223,36 @@ impl WorldCallbackAccess {
218223
world.exit();
219224
Ok(())
220225
}
226+
227+
/// Tries to call a fitting overload of the function with the given name and in the type id's namespace based on the arguments provided.
228+
/// Currently does this by repeatedly trying each overload until one succeeds or all fail.
229+
pub fn try_call_overloads(
230+
&self,
231+
type_id: TypeId,
232+
name: impl Into<Cow<'static, str>>,
233+
args: Vec<ScriptValue>,
234+
context: CallerContext,
235+
) -> Result<ScriptValue, InteropError> {
236+
let world = self.try_read()?;
237+
let registry = world.script_function_registry();
238+
let registry = registry.read();
239+
240+
let name = name.into();
241+
let overload_iter = match registry.iter_overloads(Namespace::OnType(type_id), name) {
242+
Ok(iter) => iter,
243+
Err(name) => return Err(InteropError::missing_function(type_id, name.to_string())),
244+
};
245+
246+
let mut last_error = None;
247+
for overload in overload_iter {
248+
match overload.call(args.clone(), world.clone(), context) {
249+
Ok(out) => return Ok(out),
250+
Err(e) => last_error = Some(e),
251+
}
252+
}
253+
254+
Err(last_error.expect("invariant, iterator should always return at least one item, and if the call fails it should return an error"))
255+
}
221256
}
222257

223258
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5);
@@ -459,109 +494,27 @@ impl<'w> WorldAccessGuard<'w> {
459494
)
460495
}
461496

462-
// #[track_caller]
463-
// /// Get access to the given component, this is the only way to access a component/resource safely (in the context of the world access guard)
464-
// pub fn get_component_with_access<T: Component>(
465-
// &self,
466-
// access: &WorldAccess,
467-
// entity: Entity,
468-
// ) -> ScriptResult<Option<&T>> {
469-
// let component_id = match self.0.cell.components().component_id::<T>() {
470-
// Some(id) => id,
471-
// None => return Ok(None),
472-
// };
473-
474-
// if access.can_read(ReflectAccessId {
475-
// kind: ReflectAccessKind::ComponentOrResource,
476-
// id: component_id.index(),
477-
// }) {
478-
// // Safety: we have the correct access id
479-
// unsafe { Ok(self.0.cell.get_entity(entity).and_then(|e| e.get::<T>())) }
480-
// } else {
481-
// Err(ScriptError::new_reflection_error(
482-
// "Cannot read component, received invalid access".to_string(),
483-
// ))
484-
// }
485-
// }
486-
487-
// #[track_caller]
488-
// /// Get access to the given component, this is the only way to access a component/resource safely (in the context of the world access guard)
489-
// pub fn get_component_with_access_mut<T: Component>(
490-
// &self,
491-
// access: &mut WorldAccess,
492-
// entity: Entity,
493-
// ) -> ScriptResult<Option<Mut<T>>> {
494-
// let component_id = match self.0.cell.components().component_id::<T>() {
495-
// Some(id) => id,
496-
// None => return Ok(None),
497-
// };
498-
499-
// if access.can_write(ReflectAccessId {
500-
// kind: ReflectAccessKind::ComponentOrResource,
501-
// id: component_id.index(),
502-
// }) {
503-
// // Safety: we have the correct access id
504-
// unsafe {
505-
// Ok(self
506-
// .0
507-
// .cell
508-
// .get_entity(entity)
509-
// .and_then(|e| e.get_mut::<T>()))
510-
// }
511-
// } else {
512-
// Err(ScriptError::new_reflection_error(
513-
// "Cannot write component, received invalid access".to_string(),
514-
// ))
515-
// }
516-
// }
517-
518-
// #[track_caller]
519-
// /// Get access to the given resource
520-
// pub fn get_resource_with_access<T: Resource>(
521-
// &self,
522-
// access: &WorldAccess,
523-
// ) -> ScriptResult<Option<&T>> {
524-
// let resource_id = match self.0.cell.components().resource_id::<T>() {
525-
// Some(id) => id,
526-
// None => return Ok(None),
527-
// };
528-
529-
// if access.can_read(ReflectAccessId {
530-
// kind: ReflectAccessKind::ComponentOrResource,
531-
// id: resource_id.index(),
532-
// }) {
533-
// // Safety: we have the correct access id
534-
// unsafe { Ok(self.0.cell.get_resource::<T>()) }
535-
// } else {
536-
// Err(ScriptError::new_reflection_error(
537-
// "Cannot read resource, received invalid access".to_string(),
538-
// ))
539-
// }
540-
// }
541-
542-
// #[track_caller]
543-
// /// Get access to the given resource, this is the only way to access a component/resource safely (in the context of the world access guard)
544-
// pub fn get_resource_with_access_mut<T: Resource>(
545-
// &self,
546-
// access: &mut WorldAccess,
547-
// ) -> ScriptResult<Option<Mut<T>>> {
548-
// let resource_id = match self.0.cell.components().resource_id::<T>() {
549-
// Some(id) => id,
550-
// None => return Ok(None),
551-
// };
552-
553-
// if access.can_write(ReflectAccessId {
554-
// kind: ReflectAccessKind::ComponentOrResource,
555-
// id: resource_id.index(),
556-
// }) {
557-
// // Safety: we have the correct access id
558-
// unsafe { Ok(self.0.cell.get_resource_mut::<T>()) }
559-
// } else {
560-
// Err(ScriptError::new_reflection_error(
561-
// "Cannot write resource, received invalid access".to_string(),
562-
// ))
563-
// }
564-
// }
497+
/// Try to lookup a function with the given name on the given type id's namespaces.
498+
///
499+
/// Returns the function if found, otherwise returns the name of the function that was not found.
500+
pub fn lookup_function(
501+
&self,
502+
type_ids: impl IntoIterator<Item = TypeId>,
503+
name: impl Into<Cow<'static, str>>,
504+
) -> Result<DynamicScriptFunction, Cow<'static, str>> {
505+
let registry = self.script_function_registry();
506+
let registry = registry.read();
507+
508+
let mut name = name.into();
509+
for type_id in type_ids {
510+
name = match registry.get_function(Namespace::OnType(type_id), name) {
511+
Ok(func) => return Ok(func.clone()),
512+
Err(name) => name,
513+
};
514+
}
515+
516+
Err(name)
517+
}
565518

566519
/// checks if a given entity exists and is valid
567520
pub fn is_valid_entity(&self, entity: Entity) -> bool {
@@ -926,8 +879,15 @@ pub trait WorldContainer {
926879
self.try_get_world().expect("World not set, or expired")
927880
}
928881

882+
fn get_callback_world(&self) -> WorldCallbackAccess {
883+
self.try_get_callback_world()
884+
.expect("World not set, or expired")
885+
}
886+
929887
/// Tries to get the world
930888
fn try_get_world(&self) -> Result<Arc<WorldAccessGuard<'static>>, Self::Error>;
889+
890+
fn try_get_callback_world(&self) -> Result<WorldCallbackAccess, Self::Error>;
931891
}
932892

933893
/// A world container that stores the world in a thread local
@@ -955,6 +915,16 @@ impl WorldContainer for ThreadWorldContainer {
955915
.ok_or_else(InteropError::missing_world)
956916
})?
957917
}
918+
919+
fn try_get_callback_world(&self) -> Result<WorldCallbackAccess, Self::Error> {
920+
WORLD_CALLBACK_ACCESS.with(|w| {
921+
w.borrow()
922+
.as_ref()
923+
.cloned()
924+
// .map(|w| w.try_read())
925+
.ok_or_else(InteropError::missing_world)
926+
})
927+
}
958928
}
959929

960930
// #[cfg(test)]

0 commit comments

Comments
 (0)