Skip to content

Commit 4f20f12

Browse files
committed
fix conversion
1 parent 1899ffa commit 4f20f12

File tree

4 files changed

+38
-63
lines changed

4 files changed

+38
-63
lines changed

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

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use bevy::reflect::{
1313
},
1414
PartialReflect,
1515
};
16-
use script_function::CallerContext;
16+
use script_function::{CallerContext, DynamicScriptFunction};
1717

1818
use crate::{
1919
error::{FlattenError, InteropError, InteropErrorInner, ScriptError, ScriptResult},
@@ -37,56 +37,20 @@ pub trait CallScriptFunction {
3737
) -> Result<ScriptValue, InteropError>;
3838
}
3939

40-
impl CallScriptFunction for DynamicFunction<'_> {
40+
impl CallScriptFunction for DynamicScriptFunction {
4141
fn call_script_function<I: IntoIterator<Item = ScriptValue>>(
4242
&self,
4343
args: I,
4444
world: WorldGuard,
4545
context: CallerContext,
4646
) -> Result<ScriptValue, InteropError> {
47-
let args = args.into_iter();
48-
49-
let add_context = self.is_script_function();
50-
let mut args_list = ArgList::new();
51-
52-
if add_context {
53-
args_list = args_list.push_arg(ArgValue::Owned(Box::new(context)));
54-
args_list = args_list.push_arg(ArgValue::Owned(Box::new(
55-
WorldCallbackAccess::from_guard(world.clone()),
56-
)));
57-
}
58-
59-
for arg in args {
60-
let arg_val = ArgValue::Owned(Box::new(arg));
61-
args_list = args_list.push_arg(arg_val);
47+
let args = args.into_iter().collect::<Vec<_>>();
48+
let world_callback_access = WorldCallbackAccess::from_guard(world.clone());
49+
// should we be inlining call errors into the return value?
50+
let return_val = self.call(context, world_callback_access, args);
51+
match return_val {
52+
ScriptValue::Error(e) => Err(InteropError::function_interop_error(self.name(), e)),
53+
v => Ok(v),
6254
}
63-
64-
let return_val = self
65-
.call(args_list)
66-
.map_err(InteropError::function_call_error)?;
67-
68-
match return_val.try_into_or_boxed::<ScriptValue>() {
69-
Ok(ScriptValue::Error(e)) => Err(InteropError::function_interop_error(self.info(), e)),
70-
Ok(v) => Ok(v),
71-
Err(b) => {
72-
let allocator = world.allocator();
73-
let mut allocator = allocator.write();
74-
75-
Ok(ReflectReference::new_allocated_boxed(b, &mut allocator).into())
76-
}
77-
}
78-
}
79-
}
80-
81-
pub trait DynamicFunctionExt {
82-
fn is_script_function(&self) -> bool;
83-
}
84-
85-
impl DynamicFunctionExt for DynamicFunction<'_> {
86-
fn is_script_function(&self) -> bool {
87-
self.info().args().first().map_or(false, |arg| {
88-
arg.type_id() == std::any::TypeId::of::<CallerContext>()
89-
|| arg.type_id() == std::any::TypeId::of::<WorldCallbackAccess>()
90-
})
9155
}
9256
}

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,29 @@ pub struct CallerContext {
117117
/// The Script Function equivalent for dynamic functions. Currently unused
118118
#[derive(Clone)]
119119
pub struct DynamicScriptFunction {
120+
name: Cow<'static, str>,
120121
// TODO: info about the function, this is hard right now because of non 'static lifetimes in wrappers, we can't use TypePath etc
121-
pub func: Arc<
122+
func: Arc<
122123
dyn Fn(CallerContext, WorldCallbackAccess, Vec<ScriptValue>) -> ScriptValue
123124
+ Send
124125
+ Sync
125126
+ 'static,
126127
>,
127128
}
129+
impl DynamicScriptFunction {
130+
pub fn call(
131+
&self,
132+
context: CallerContext,
133+
world: WorldCallbackAccess,
134+
args: Vec<ScriptValue>,
135+
) -> ScriptValue {
136+
(self.func)(context, world, args)
137+
}
138+
139+
pub fn name(&self) -> &Cow<'static, str> {
140+
&self.name
141+
}
142+
}
128143

129144
impl std::fmt::Debug for DynamicScriptFunction {
130145
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -274,7 +289,7 @@ macro_rules! impl_script_function {
274289
fn into_dynamic_script_function(self) -> DynamicScriptFunction {
275290
let func = (move |caller_context: CallerContext, world: WorldCallbackAccess, args: Vec<ScriptValue> | {
276291
let res: Result<ScriptValue, InteropError> = (|| {
277-
let expected_arg_count = count!($($param),*);
292+
let expected_arg_count = count!($($param )*);
278293
if args.len() != expected_arg_count {
279294
return Err(InteropError::function_call_error(FunctionError::ArgCountMismatch{
280295
expected: expected_arg_count,
@@ -308,7 +323,7 @@ macro_rules! impl_script_function {
308323
script_value
309324
});
310325

311-
DynamicScriptFunction { func: Arc::new(func) }
326+
DynamicScriptFunction { func: Arc::new(func), name: core::any::type_name::<Self>().into() }
312327
}
313328
}
314329
};

crates/bevy_mod_scripting_core/src/error.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,9 @@ impl InteropError {
351351
}
352352

353353
/// Thrown when an error happens in a function call. The inner error provides details on the error.
354-
pub fn function_interop_error(function_info: &FunctionInfo, error: InteropError) -> Self {
354+
pub fn function_interop_error(function_name: &str, error: InteropError) -> Self {
355355
Self(Arc::new(InteropErrorInner::FunctionInteropError {
356-
function_name: function_info
357-
.name()
358-
.map(|s| s.to_string())
359-
.unwrap_or("<unnamed function>".to_owned()),
356+
function_name: function_name.to_string(),
360357
error,
361358
}))
362359
}

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ use bevy::{
1616
};
1717
use bevy_mod_scripting_core::{
1818
bindings::{
19-
function::{script_function::CallerContext, CallScriptFunction},
19+
function::{
20+
script_function::{AppScriptFunctionRegistry, CallerContext, DynamicScriptFunction},
21+
CallScriptFunction,
22+
},
2023
pretty_print::{DisplayWithWorld, ReflectReferencePrinter},
2124
script_value::ScriptValue,
2225
ReflectAllocator, ReflectRefIter, ReflectReference, ReflectionPathExt, TypeIdSource,
@@ -91,25 +94,21 @@ fn lookup_function_typed<T: 'static + ?Sized>(
9194
lookup_function(lua, key, type_id)
9295
}
9396

94-
fn lookup_dynamic_function<'lua>(
95-
lua: &'lua Lua,
96-
key: &str,
97-
type_id: TypeId,
98-
) -> Option<DynamicFunction<'static>> {
97+
fn lookup_dynamic_function(lua: &Lua, key: &str, type_id: TypeId) -> Option<DynamicScriptFunction> {
9998
let function_registry = lua
10099
.get_world()
101-
.with_resource(|registry: &AppFunctionRegistry| registry.clone());
100+
.with_resource(|registry: &AppScriptFunctionRegistry| registry.clone());
102101
let registry = function_registry.read();
103102

104103
registry
105104
.get_namespaced_function(key.to_string(), Namespace::OnType(type_id))
106105
.cloned()
107106
}
108107

109-
fn lookup_dynamic_function_typed<'lua, T: 'static + ?Sized>(
110-
lua: &'lua Lua,
108+
fn lookup_dynamic_function_typed<T: 'static + ?Sized>(
109+
lua: &Lua,
111110
key: &str,
112-
) -> Option<DynamicFunction<'static>> {
111+
) -> Option<DynamicScriptFunction> {
113112
let type_id = TypeId::of::<T>();
114113
lookup_dynamic_function(lua, key, type_id)
115114
}

0 commit comments

Comments
 (0)