Skip to content

Commit d751481

Browse files
committed
move namespace stuff into core
1 parent 5fb0776 commit d751481

File tree

10 files changed

+194
-31
lines changed

10 files changed

+194
-31
lines changed

crates/bevy_api_gen/templates/header.tera

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use bevy_mod_scripting_core::{
1414
StoreDocumentation,
1515
bindings::{
1616
ReflectReference,
17-
function::from::{Ref, Mut, Val}
17+
function::{from::{Ref, Mut, Val}, namespace::{NamespaceBuilder}}
1818
}
1919
};
2020
{% if args.self_is_bms_lua %}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod from;
22
pub mod from_ref;
33
pub mod into;
44
pub mod into_ref;
5+
pub mod namespace;
56
pub mod script_function;
67

78
use script_function::{CallerContext, DynamicScriptFunction, DynamicScriptFunctionMut};

crates/bevy_mod_scripting_functions/src/namespaced_register.rs renamed to crates/bevy_mod_scripting_core/src/bindings/function/namespace.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use crate::bindings::function::script_function::{
2+
AppScriptFunctionRegistry, DynamicScriptFunction, GetFunctionTypeDependencies, ScriptFunction,
3+
ScriptFunctionRegistry,
4+
};
15
use bevy::{
26
prelude::{AppTypeRegistry, World},
37
reflect::GetTypeRegistration,
48
};
5-
use bevy_mod_scripting_core::bindings::function::script_function::{
6-
AppScriptFunctionRegistry, DynamicScriptFunction, GetFunctionTypeDependencies, ScriptFunction,
7-
ScriptFunctionRegistry,
8-
};
99
use std::{any::TypeId, borrow::Cow, marker::PhantomData};
1010

1111
pub trait RegisterNamespacedFunction {

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

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,29 @@ pub struct CallerContext {
123123
pub self_type: Option<TypeId>,
124124
}
125125

126+
#[derive(Clone, Debug, PartialEq, Default)]
127+
pub struct FunctionInfo {
128+
pub name: Cow<'static, str>,
129+
pub on_type: Option<TypeId>,
130+
}
131+
132+
impl FunctionInfo {
133+
/// The name of the function
134+
pub fn name(&self) -> &Cow<'static, str> {
135+
&self.name
136+
}
137+
138+
/// If the function is namespaced to a specific type, this will return the type id of that type
139+
pub fn on_type(&self) -> Option<TypeId> {
140+
self.on_type
141+
}
142+
}
143+
126144
/// The Script Function equivalent for dynamic functions. Currently unused
127145
#[derive(Clone, Reflect)]
128146
#[reflect(opaque)]
129147
pub struct DynamicScriptFunction {
130-
name: Cow<'static, str>,
148+
pub info: FunctionInfo,
131149
// TODO: info about the function, this is hard right now because of non 'static lifetimes in wrappers, we can't use TypePath etc
132150
func: Arc<
133151
dyn Fn(CallerContext, WorldCallbackAccess, Vec<ScriptValue>) -> ScriptValue
@@ -139,14 +157,14 @@ pub struct DynamicScriptFunction {
139157

140158
impl PartialEq for DynamicScriptFunction {
141159
fn eq(&self, other: &Self) -> bool {
142-
self.name == other.name
160+
self.info == other.info
143161
}
144162
}
145163

146164
#[derive(Clone, Reflect)]
147165
#[reflect(opaque)]
148166
pub struct DynamicScriptFunctionMut {
149-
name: Cow<'static, str>,
167+
pub info: FunctionInfo,
150168
func: Arc<
151169
RwLock<
152170
// I'd rather consume an option or something instead of having the RWLock but I just wanna get this release out
@@ -160,7 +178,7 @@ pub struct DynamicScriptFunctionMut {
160178

161179
impl PartialEq for DynamicScriptFunctionMut {
162180
fn eq(&self, other: &Self) -> bool {
163-
self.name == other.name
181+
self.info == other.info
164182
}
165183
}
166184

@@ -175,20 +193,33 @@ impl DynamicScriptFunction {
175193
}
176194

177195
pub fn name(&self) -> &Cow<'static, str> {
178-
&self.name
196+
&self.info.name
179197
}
180198

181199
pub fn with_name<N: Into<Cow<'static, str>>>(self, name: N) -> Self {
182200
Self {
183-
name: name.into(),
201+
info: FunctionInfo {
202+
name: name.into(),
203+
..self.info
204+
},
205+
func: self.func,
206+
}
207+
}
208+
209+
pub fn with_on_type(self, on_type: Option<TypeId>) -> Self {
210+
Self {
211+
info: FunctionInfo {
212+
on_type,
213+
..self.info
214+
},
184215
func: self.func,
185216
}
186217
}
187218
}
188219

189220
impl DynamicScriptFunctionMut {
190221
pub fn call(
191-
&mut self,
222+
&self,
192223
context: CallerContext,
193224
world: WorldCallbackAccess,
194225
args: Vec<ScriptValue>,
@@ -198,12 +229,25 @@ impl DynamicScriptFunctionMut {
198229
}
199230

200231
pub fn name(&self) -> &Cow<'static, str> {
201-
&self.name
232+
&self.info.name
202233
}
203234

204235
pub fn with_name<N: Into<Cow<'static, str>>>(self, name: N) -> Self {
205236
Self {
206-
name: name.into(),
237+
info: FunctionInfo {
238+
name: name.into(),
239+
..self.info
240+
},
241+
func: self.func,
242+
}
243+
}
244+
245+
pub fn with_on_type(self, on_type: Option<TypeId>) -> Self {
246+
Self {
247+
info: FunctionInfo {
248+
on_type,
249+
..self.info
250+
},
207251
func: self.func,
208252
}
209253
}
@@ -234,9 +278,10 @@ where
234278
{
235279
fn from(fn_: F) -> Self {
236280
DynamicScriptFunction {
237-
name: std::any::type_name::<F>().into(),
281+
info: FunctionInfo::default(),
238282
func: Arc::new(fn_),
239283
}
284+
.with_name(std::any::type_name::<F>())
240285
}
241286
}
242287

@@ -249,9 +294,10 @@ where
249294
{
250295
fn from(fn_: F) -> Self {
251296
DynamicScriptFunctionMut {
252-
name: std::any::type_name::<F>().into(),
297+
info: FunctionInfo::default(),
253298
func: Arc::new(RwLock::new(fn_)),
254299
}
300+
.with_name(std::any::type_name::<F>())
255301
}
256302
}
257303

@@ -301,7 +347,7 @@ impl ScriptFunctionRegistry {
301347
self.register_overload(name, func);
302348
}
303349

304-
pub fn register_overload<F, M>(&mut self, name: impl Into<Cow<'static, str>>, func: F)
350+
fn register_overload<F, M>(&mut self, name: impl Into<Cow<'static, str>>, func: F)
305351
where
306352
F: ScriptFunction<'static, M>,
307353
{

crates/bevy_mod_scripting_functions/src/bevy_bindings/bevy_reflect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![cfg_attr(rustfmt, rustfmt_skip)]
55
use bevy_mod_scripting_core::{
66
AddContextInitializer, StoreDocumentation,
7-
bindings::{ReflectReference, function::from::{Ref, Mut, Val}},
7+
bindings::{ReflectReference, function::{namespace::NamespaceBuilder,from::{Ref, Mut, Val}}},
88
};
99
use crate::*;
1010
pub struct BevyReflectScriptingPlugin;

crates/bevy_mod_scripting_functions/src/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Contains functions defined by the [`bevy_mod_scripting_core`] crate
22
3-
use crate::NamespaceBuilder;
43
use bevy::{
54
prelude::*,
65
reflect::{func::FunctionRegistrationError, ParsedPath},
@@ -12,6 +11,7 @@ use bindings::{
1211
from::{Ref, Val},
1312
from_ref::FromScriptRef,
1413
into_ref::IntoScriptRef,
14+
namespace::NamespaceBuilder,
1515
script_function::{CallerContext, ScriptFunctionMut},
1616
},
1717
pretty_print::DisplayWithWorld,

crates/bevy_mod_scripting_functions/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ pub mod core;
77
#[cfg(feature = "test_functions")]
88
pub mod test_functions;
99

10-
pub mod namespaced_register;
11-
1210
pub use core::*;
13-
pub use namespaced_register::*;
1411

1512
pub struct ScriptFunctionsPlugin;
1613

crates/bevy_mod_scripting_functions/src/test_functions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::sync::Arc;
22

3-
use crate::NamespaceBuilder;
43
use bevy::{
54
app::App,
65
prelude::{Entity, World},
@@ -9,6 +8,7 @@ use bevy::{
98
use bevy_mod_scripting_core::{
109
bindings::{
1110
function::{
11+
namespace::NamespaceBuilder,
1212
script_function::{CallerContext, DynamicScriptFunctionMut},
1313
CallScriptFunction,
1414
},

crates/languages/bevy_mod_scripting_rhai/src/bindings/reference.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
use std::ops::{Deref, DerefMut};
2-
3-
use bevy_mod_scripting_core::bindings::{ReflectReference, ThreadWorldContainer, WorldContainer};
4-
use rhai::{CustomType, Dynamic};
1+
use super::script_value::FromDynamic;
2+
use bevy_mod_scripting_core::bindings::{
3+
function::script_function::{AppScriptFunctionRegistry, DynamicScriptFunction},
4+
script_value::ScriptValue,
5+
ReflectReference, ThreadWorldContainer, WorldContainer, WorldGuard,
6+
};
7+
use bevy_mod_scripting_functions::{GetNamespacedFunction, Namespace};
8+
use rhai::{CustomType, Dynamic, EvalAltResult};
9+
use std::{
10+
any::TypeId,
11+
ops::{Deref, DerefMut},
12+
};
513

614
#[derive(Clone, Debug, PartialEq)]
715
pub struct RhaiReflectReference(pub ReflectReference);
@@ -38,12 +46,30 @@ impl DerefMut for RhaiReflectReference {
3846
}
3947
}
4048

49+
fn lookup_dynamic_function(
50+
world: WorldGuard,
51+
name: &str,
52+
on: TypeId,
53+
) -> Option<DynamicScriptFunction> {
54+
let registry = world.with_resource(|registry: &AppScriptFunctionRegistry| registry.clone());
55+
let registry = registry.read();
56+
57+
registry
58+
.get_namespaced_function(name.to_string(), Namespace::OnType(on))
59+
.cloned()
60+
}
61+
4162
impl CustomType for RhaiReflectReference {
4263
fn build(mut builder: rhai::TypeBuilder<Self>) {
4364
builder
4465
.with_name(std::any::type_name::<ReflectReference>())
4566
.with_indexer_get(|_obj: &mut Self, _index: Dynamic| {
4667
let _world = ThreadWorldContainer.get_world();
68+
let key: ScriptValue = ScriptValue::from_dynamic(_index)?;
69+
if let ScriptValue::String(key) = key {
70+
// lookup function
71+
}
72+
Ok::<_, Box<EvalAltResult>>(())
4773
});
4874
}
4975
}

0 commit comments

Comments
 (0)