Skip to content

Commit ef90872

Browse files
committed
get static calls working
1 parent 98319a2 commit ef90872

File tree

25 files changed

+170
-1472
lines changed

25 files changed

+170
-1472
lines changed

crates/bevy_mod_scripting_core/src/bindings/query.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ impl ScriptQueryBuilder {
104104

105105
#[derive(Clone, Reflect)]
106106
#[reflect(opaque)]
107-
pub struct ScriptQueryResult(pub Entity, pub Vec<ReflectReference>);
107+
pub struct ScriptQueryResult {
108+
pub entity: Entity,
109+
pub components: Vec<ReflectReference>,
110+
}
108111

109112
impl WorldCallbackAccess {
110113
pub fn query(
@@ -159,7 +162,10 @@ impl<'w> WorldAccessGuard<'w> {
159162
reflect_path: ParsedPath(vec![]),
160163
})
161164
.collect();
162-
ScriptQueryResult(r.id(), references)
165+
ScriptQueryResult {
166+
entity: r.id(),
167+
components: references,
168+
}
163169
})
164170
.collect())
165171
})

crates/bevy_mod_scripting_core/src/error.rs

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use thiserror::Error;
1818

1919
use crate::{
2020
bindings::{
21-
access_map::DisplayCodeLocation, pretty_print::{DisplayWithWorld, DisplayWithWorldAndDummy}, ReflectAllocationId, ReflectBase, ReflectBaseType, ReflectReference
21+
access_map::DisplayCodeLocation,
22+
pretty_print::{DisplayWithWorld, DisplayWithWorldAndDummy},
23+
ReflectAllocationId, ReflectBase, ReflectBaseType, ReflectReference,
2224
},
2325
impl_dummy_display,
2426
prelude::ScriptValue,
@@ -201,16 +203,16 @@ impl From<InteropError> for ScriptError {
201203
}
202204
}
203205

204-
pub trait FlattenError<O,E> {
206+
pub trait FlattenError<O, E> {
205207
fn flatten_interop_error(self) -> Result<O, E>;
206208
}
207209

208-
impl <O>FlattenError<O, InteropError> for Result<Result<O,InteropError>, InteropError> {
210+
impl<O> FlattenError<O, InteropError> for Result<Result<O, InteropError>, InteropError> {
209211
fn flatten_interop_error(self) -> Result<O, InteropError> {
210212
match self {
211213
Ok(Ok(o)) => Ok(o),
212214
Ok(Err(e)) => Err(e),
213-
Err(e) => Err(e)
215+
Err(e) => Err(e),
214216
}
215217
}
216218
}
@@ -254,8 +256,14 @@ impl InteropError {
254256

255257
/// Thrown if access to the given reflection base is required but cannot be claimed.
256258
/// This is likely due to some other script already claiming access to the base.
257-
pub fn cannot_claim_access(base: ReflectBaseType, location: Option<std::panic::Location<'static>>) -> Self {
258-
Self(Arc::new(InteropErrorInner::CannotClaimAccess { base, location }))
259+
pub fn cannot_claim_access(
260+
base: ReflectBaseType,
261+
location: Option<std::panic::Location<'static>>,
262+
) -> Self {
263+
Self(Arc::new(InteropErrorInner::CannotClaimAccess {
264+
base,
265+
location,
266+
}))
259267
}
260268

261269
/// Thrown if a conversion into the given type is impossible.
@@ -266,11 +274,11 @@ impl InteropError {
266274

267275
/// Thrown if a conversion was not fully completed, as a better conversion exists.
268276
/// If a function might throw this error it should be handled by the caller.
269-
///
277+
///
270278
/// A user seeing this error is evidence of unfinished logic.
271279
pub fn better_conversion_exists<T>() -> Self {
272-
Self(Arc::new(InteropErrorInner::BetterConversionExists{
273-
context: std::any::type_name::<T>().to_string()
280+
Self(Arc::new(InteropErrorInner::BetterConversionExists {
281+
context: std::any::type_name::<T>().to_string(),
274282
}))
275283
}
276284

@@ -343,10 +351,7 @@ impl InteropError {
343351
}
344352

345353
/// Thrown when an error happens in a function call. The inner error provides details on the error.
346-
pub fn function_interop_error(
347-
function_info: &FunctionInfo,
348-
error: InteropError,
349-
) -> Self {
354+
pub fn function_interop_error(function_info: &FunctionInfo, error: InteropError) -> Self {
350355
Self(Arc::new(InteropErrorInner::FunctionInteropError {
351356
function_name: function_info
352357
.name()
@@ -366,17 +371,27 @@ impl InteropError {
366371
pub fn function_arg_conversion_error(argument: String, error: InteropError) -> Self {
367372
Self(Arc::new(InteropErrorInner::FunctionArgConversionError {
368373
argument,
369-
error
374+
error,
370375
}))
371376
}
372377
pub fn length_mismatch(expected: usize, got: usize) -> Self {
373-
Self(Arc::new(InteropErrorInner::LengthMismatch { expected, got }))
378+
Self(Arc::new(InteropErrorInner::LengthMismatch {
379+
expected,
380+
got,
381+
}))
374382
}
375-
383+
376384
pub fn external_error(error: Box<dyn std::error::Error + Send + Sync>) -> Self {
377385
Self(Arc::new(InteropErrorInner::OtherError { error }))
378386
}
379387

388+
pub fn missing_function(on: TypeId, function_name: String) -> Self {
389+
Self(Arc::new(InteropErrorInner::MissingFunctionError {
390+
on,
391+
function_name,
392+
}))
393+
}
394+
380395
pub fn inner(&self) -> &InteropErrorInner {
381396
&self.0
382397
}
@@ -418,7 +433,7 @@ pub enum InteropErrorInner {
418433
into: TypeId,
419434
},
420435
BetterConversionExists {
421-
context: String
436+
context: String,
422437
},
423438
TypeMismatch {
424439
expected: TypeId,
@@ -465,13 +480,17 @@ pub enum InteropErrorInner {
465480
FunctionCallError {
466481
inner: FunctionError,
467482
},
483+
MissingFunctionError {
484+
on: TypeId,
485+
function_name: String,
486+
},
468487
FunctionInteropError {
469488
function_name: String,
470489
error: InteropError,
471490
},
472491
FunctionArgConversionError {
473492
argument: String,
474-
error: InteropError
493+
error: InteropError,
475494
},
476495
OtherError {
477496
error: Box<dyn std::error::Error + Send + Sync>,
@@ -487,7 +506,13 @@ impl PartialEq for InteropErrorInner {
487506
impl DisplayWithWorld for InteropErrorInner {
488507
fn display_with_world(&self, world: crate::bindings::WorldGuard) -> String {
489508
match self {
490-
509+
InteropErrorInner::MissingFunctionError { on, function_name } => {
510+
format!(
511+
"Could not find function: {} for type: {}",
512+
function_name,
513+
on.display_with_world(world)
514+
)
515+
},
491516
InteropErrorInner::UnregisteredBase { base } => {
492517
format!("Unregistered base type: {}", base.display_with_world(world))
493518
}
@@ -629,4 +654,4 @@ impl Default for InteropErrorInner {
629654
fn default() -> Self {
630655
InteropErrorInner::StaleWorldAccess
631656
}
632-
}
657+
}

crates/bevy_mod_scripting_functions/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ categories = ["game-development"]
1212
readme = "readme.md"
1313

1414
[features]
15-
default = ["core_functions"]
16-
core_functions = ["bevy_mod_scripting_core"]
15+
1716

1817

1918
[dependencies]
@@ -22,4 +21,4 @@ bevy = { workspace = true, features = [
2221
] }
2322
uuid = "*"
2423
smol_str = "*"
25-
bevy_mod_scripting_core = { workspace = true, optional = true }
24+
bevy_mod_scripting_core = { workspace = true }

crates/bevy_mod_scripting_functions/src/core.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ use bindings::{
2424
use error::InteropError;
2525
use reflection_extensions::TypeIdExtensions;
2626

27-
use crate::namespaced_register::NamespaceBuilder;
28-
29-
pub struct CoreFunctionsPlugin;
27+
use crate::{bevy_bindings::LuaBevyScriptingPlugin, namespaced_register::NamespaceBuilder};
3028

3129
pub trait RegisterScriptFunction {
3230
fn overwrite_script_function<M, N, F>(&mut self, name: N, f: F) -> &mut Self
@@ -52,23 +50,11 @@ impl<S: 'static> RegisterScriptFunction for NamespaceBuilder<'_, S> {
5250
}
5351
}
5452

55-
impl Plugin for CoreFunctionsPlugin {
56-
fn build(&self, app: &mut App) {
57-
let world = app.world_mut();
58-
register_world_functions(world).expect("Failed to register world functions");
59-
60-
register_reflect_reference_functions(world)
61-
.expect("Failed to register reflect reference functions");
62-
63-
register_script_type_registration_functions(world)
64-
.expect("Failed to register script type registration functions");
65-
66-
register_script_query_builder_functions(world)
67-
.expect("Failed to register script query builder functions");
68-
}
53+
pub fn register_bevy_bindings(app: &mut App) {
54+
app.add_plugins(LuaBevyScriptingPlugin);
6955
}
7056

71-
fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrationError> {
57+
pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrationError> {
7258
NamespaceBuilder::<WorldCallbackAccess>::new(reg)
7359
.overwrite_script_function("spawn", |s: WorldCallbackAccess| Ok(Val(s.spawn()?)))
7460
.overwrite_script_function(
@@ -180,7 +166,9 @@ fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrationE
180166
Ok(())
181167
}
182168

183-
fn register_reflect_reference_functions(reg: &mut World) -> Result<(), FunctionRegistrationError> {
169+
pub fn register_reflect_reference_functions(
170+
reg: &mut World,
171+
) -> Result<(), FunctionRegistrationError> {
184172
NamespaceBuilder::<ReflectReference>::new(reg)
185173
.overwrite_script_function(
186174
"display_ref",
@@ -273,7 +261,7 @@ fn register_reflect_reference_functions(reg: &mut World) -> Result<(), FunctionR
273261
Ok(())
274262
}
275263

276-
fn register_script_type_registration_functions(
264+
pub fn register_script_type_registration_functions(
277265
registry: &mut World,
278266
) -> Result<(), FunctionRegistrationError> {
279267
NamespaceBuilder::<ScriptTypeRegistration>::new(registry)
@@ -290,7 +278,7 @@ fn register_script_type_registration_functions(
290278
Ok(())
291279
}
292280

293-
fn register_script_query_builder_functions(
281+
pub fn register_script_query_builder_functions(
294282
registry: &mut World,
295283
) -> Result<(), FunctionRegistrationError> {
296284
NamespaceBuilder::<ScriptQueryBuilder>::new(registry)
@@ -322,11 +310,14 @@ fn register_script_query_builder_functions(
322310
Ok(())
323311
}
324312

325-
// fn register_script_query_result_functions(
326-
// registry: &mut FunctionRegistry,
327-
// ) -> Result<(), FunctionRegistrationError> {
328-
// NamespaceBuilder::<ScriptQueryResult>::new(registry)
329-
// .overwrite_script_function("entity", |s: Ref<ScriptQueryResult>| s.entity())
330-
// .overwrite_script_function("components", |s: Ref<ScriptQueryResult>| s.components());
331-
// Ok(())
332-
// }
313+
pub fn register_script_query_result_functions(
314+
world: &mut World,
315+
) -> Result<(), FunctionRegistrationError> {
316+
NamespaceBuilder::<ScriptQueryResult>::new(world)
317+
.overwrite_script_function("entity", |s: Ref<ScriptQueryResult>| Val::new(s.entity))
318+
.overwrite_script_function("components", |s: Ref<ScriptQueryResult>| {
319+
let components = s.components.to_vec();
320+
Val::new(components)
321+
});
322+
Ok(())
323+
}
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
use ::bevy::prelude::*;
2-
#[cfg(feature = "core_functions")]
32
pub mod bevy_bindings;
4-
#[cfg(feature = "core_functions")]
53
pub mod core;
64

75
pub mod namespaced_register;
86

97
pub use core::*;
108
pub use namespaced_register::*;
119

12-
pub struct BevyFunctionsPlugin;
10+
pub struct ScriptFunctionsPlugin;
1311

14-
impl Plugin for BevyFunctionsPlugin {
12+
impl Plugin for ScriptFunctionsPlugin {
1513
fn build(&self, app: &mut App) {
16-
#[cfg(feature = "core_functions")]
17-
app.add_plugins(core::CoreFunctionsPlugin);
14+
register_bevy_bindings(app);
15+
let world = app.world_mut();
16+
17+
register_world_functions(world).expect("Failed to register world functions");
18+
19+
register_reflect_reference_functions(world)
20+
.expect("Failed to register reflect reference functions");
21+
22+
register_script_type_registration_functions(world)
23+
.expect("Failed to register script type registration functions");
24+
25+
register_script_query_builder_functions(world)
26+
.expect("Failed to register script query builder functions");
27+
28+
register_script_query_result_functions(world)
29+
.expect("Failed to register script query result functions");
1830
}
1931
}

crates/languages/bevy_mod_scripting_lua/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ path = "src/lib.rs"
4242
[dependencies]
4343
bevy = { workspace = true, default-features = false }
4444
bevy_mod_scripting_core = { workspace = true, features = ["mlua_impls"] }
45-
bevy_mod_scripting_functions = { workspace = true, features = [
46-
"core_functions",
47-
] }
45+
bevy_mod_scripting_functions = { workspace = true, features = [] }
4846
bevy_mod_scripting_derive = { path = "../../bevy_mod_scripting_derive" }
4947
mlua = { workspace = true, features = ["vendored", "send", "macros"] }
5048
parking_lot = "0.12.1"

0 commit comments

Comments
 (0)