Skip to content

Commit 90e9189

Browse files
authored
refactor: inline CallbackBuilder<P> into IntoScriptPluginParams at compile time (#456)
1 parent 3f9a87e commit 90e9189

File tree

6 files changed

+82
-64
lines changed

6 files changed

+82
-64
lines changed

crates/bevy_mod_scripting_core/src/commands.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{
44
asset::ScriptAsset,
55
bindings::{ScriptValue, WorldGuard},
6-
context::ContextBuilder,
6+
context::ScriptingLoader,
77
error::{InteropError, ScriptError},
88
event::{
99
CallbackLabel, IntoCallbackLabel, OnScriptLoaded, OnScriptReloaded, OnScriptUnloaded,
@@ -151,8 +151,7 @@ impl<P: IntoScriptPluginParams> CreateOrUpdateScript<P> {
151151
) -> Result<(), ScriptError> {
152152
bevy::log::debug!("{}: reloading context {}", P::LANGUAGE, attachment);
153153
// reload context
154-
(ContextBuilder::<P>::reload)(
155-
handler_ctxt.context_loading_settings.loader.reload,
154+
P::reload(
156155
attachment,
157156
content,
158157
context,
@@ -172,8 +171,7 @@ impl<P: IntoScriptPluginParams> CreateOrUpdateScript<P> {
172171
handler_ctxt: &HandlerContext<P>,
173172
) -> Result<P::C, ScriptError> {
174173
bevy::log::debug!("{}: loading context {}", P::LANGUAGE, attachment);
175-
let context = (ContextBuilder::<P>::load)(
176-
handler_ctxt.context_loading_settings.loader.load,
174+
let context = P::load(
177175
attachment,
178176
content,
179177
&handler_ctxt.context_loading_settings.context_initializers,

crates/bevy_mod_scripting_core/src/context.rs

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::{
44
bindings::{ThreadWorldContainer, WorldContainer, WorldGuard},
5-
error::{InteropError, ScriptError},
5+
error::ScriptError,
66
script::ScriptAttachment,
77
IntoScriptPluginParams,
88
};
@@ -29,8 +29,6 @@ pub struct ContextLoadingSettings<P: IntoScriptPluginParams> {
2929
/// Whether to emit responses from core script_callbacks like `on_script_loaded` or `on_script_unloaded`.
3030
/// By default, this is `false` and responses are not emitted.
3131
pub emit_responses: bool,
32-
/// Defines the strategy used to load and reload contexts
33-
pub loader: ContextBuilder<P>,
3432
/// Initializers run once after creating a context but before executing it for the first time
3533
pub context_initializers: Vec<ContextInitializer<P>>,
3634
/// Initializers run every time before executing or loading a script
@@ -41,7 +39,6 @@ impl<P: IntoScriptPluginParams> Default for ContextLoadingSettings<P> {
4139
fn default() -> Self {
4240
Self {
4341
emit_responses: false,
44-
loader: ContextBuilder::default(),
4542
context_initializers: Default::default(),
4643
context_pre_handling_initializers: Default::default(),
4744
}
@@ -52,7 +49,6 @@ impl<T: IntoScriptPluginParams> Clone for ContextLoadingSettings<T> {
5249
fn clone(&self) -> Self {
5350
Self {
5451
emit_responses: self.emit_responses,
55-
loader: self.loader.clone(),
5652
context_initializers: self.context_initializers.clone(),
5753
context_pre_handling_initializers: self.context_pre_handling_initializers.clone(),
5854
}
@@ -77,29 +73,34 @@ pub type ContextReloadFn<P> = fn(
7773
runtime: &<P as IntoScriptPluginParams>::R,
7874
) -> Result<(), ScriptError>;
7975

80-
/// A strategy for loading and reloading contexts
81-
pub struct ContextBuilder<P: IntoScriptPluginParams> {
82-
/// The function to load a context
83-
pub load: ContextLoadFn<P>,
84-
/// The function to reload a context
85-
pub reload: ContextReloadFn<P>,
86-
}
76+
/// A utility trait for types implementing `IntoScriptPluginParams`.
77+
///
78+
/// Provides methods for initializing and reloading script contexts using the plugin's context loader and reloader functions.
79+
pub trait ScriptingLoader<P: IntoScriptPluginParams> {
80+
/// Loads a script context using the provided loader function
81+
fn load(
82+
attachment: &ScriptAttachment,
83+
content: &[u8],
84+
context_initializers: &[ContextInitializer<P>],
85+
pre_handling_initializers: &[ContextPreHandlingInitializer<P>],
86+
world: WorldGuard,
87+
runtime: &P::R,
88+
) -> Result<P::C, ScriptError>;
8789

88-
impl<P: IntoScriptPluginParams> Default for ContextBuilder<P> {
89-
fn default() -> Self {
90-
Self {
91-
load: |_, _, _, _, _| Err(InteropError::invariant("no context loader set").into()),
92-
reload: |_, _, _, _, _, _| {
93-
Err(InteropError::invariant("no context reloader set").into())
94-
},
95-
}
96-
}
90+
/// Reloads a script context using the provided reloader function
91+
fn reload(
92+
attachment: &ScriptAttachment,
93+
content: &[u8],
94+
previous_context: &mut P::C,
95+
context_initializers: &[ContextInitializer<P>],
96+
pre_handling_initializers: &[ContextPreHandlingInitializer<P>],
97+
world: WorldGuard,
98+
runtime: &P::R,
99+
) -> Result<(), ScriptError>;
97100
}
98101

99-
impl<P: IntoScriptPluginParams> ContextBuilder<P> {
100-
/// load a context
101-
pub fn load(
102-
loader: ContextLoadFn<P>,
102+
impl<P: IntoScriptPluginParams> ScriptingLoader<P> for P {
103+
fn load(
103104
attachment: &ScriptAttachment,
104105
content: &[u8],
105106
context_initializers: &[ContextInitializer<P>],
@@ -109,7 +110,7 @@ impl<P: IntoScriptPluginParams> ContextBuilder<P> {
109110
) -> Result<P::C, ScriptError> {
110111
WorldGuard::with_existing_static_guard(world.clone(), |world| {
111112
ThreadWorldContainer.set_world(world)?;
112-
(loader)(
113+
Self::context_loader()(
113114
attachment,
114115
content,
115116
context_initializers,
@@ -119,9 +120,7 @@ impl<P: IntoScriptPluginParams> ContextBuilder<P> {
119120
})
120121
}
121122

122-
/// reload a context
123-
pub fn reload(
124-
reloader: ContextReloadFn<P>,
123+
fn reload(
125124
attachment: &ScriptAttachment,
126125
content: &[u8],
127126
previous_context: &mut P::C,
@@ -132,7 +131,7 @@ impl<P: IntoScriptPluginParams> ContextBuilder<P> {
132131
) -> Result<(), ScriptError> {
133132
WorldGuard::with_existing_static_guard(world, |world| {
134133
ThreadWorldContainer.set_world(world)?;
135-
(reloader)(
134+
Self::context_reloader()(
136135
attachment,
137136
content,
138137
previous_context,
@@ -143,12 +142,3 @@ impl<P: IntoScriptPluginParams> ContextBuilder<P> {
143142
})
144143
}
145144
}
146-
147-
impl<P: IntoScriptPluginParams> Clone for ContextBuilder<P> {
148-
fn clone(&self) -> Self {
149-
Self {
150-
load: self.load,
151-
reload: self.reload,
152-
}
153-
}
154-
}

crates/bevy_mod_scripting_core/src/lib.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
//!
33
//! Contains language agnostic systems and types for handling scripting in bevy.
44
5-
use crate::{bindings::MarkAsCore, event::ScriptErrorEvent};
5+
use crate::{
6+
bindings::MarkAsCore,
7+
context::{ContextLoadFn, ContextReloadFn},
8+
event::ScriptErrorEvent,
9+
};
610
use asset::{
711
configure_asset_systems, configure_asset_systems_for_plugin, Language, ScriptAsset,
812
ScriptAssetLoader,
@@ -14,10 +18,7 @@ use bindings::{
1418
DynamicScriptComponentPlugin, ReflectAllocator, ReflectReference, ScriptTypeRegistration,
1519
};
1620
use commands::{AddStaticScript, RemoveStaticScript};
17-
use context::{
18-
Context, ContextBuilder, ContextInitializer, ContextLoadingSettings,
19-
ContextPreHandlingInitializer,
20-
};
21+
use context::{Context, ContextInitializer, ContextLoadingSettings, ContextPreHandlingInitializer};
2122
use error::ScriptError;
2223
use event::{ScriptCallbackEvent, ScriptCallbackResponseEvent, ScriptEvent};
2324
use handler::HandlerFn;
@@ -73,16 +74,19 @@ pub trait IntoScriptPluginParams: 'static {
7374

7475
/// Returns the handler function for the plugin
7576
fn handler() -> HandlerFn<Self>;
77+
78+
/// Returns the context loader function for the plugin
79+
fn context_loader() -> ContextLoadFn<Self>;
80+
81+
/// Returns the context reloader function for the plugin
82+
fn context_reloader() -> ContextReloadFn<Self>;
7683
}
7784

7885
/// Bevy plugin enabling scripting within the bevy mod scripting framework
7986
pub struct ScriptingPlugin<P: IntoScriptPluginParams> {
8087
/// Settings for the runtime
8188
pub runtime_settings: RuntimeSettings<P>,
8289

83-
/// The context builder for loading contexts
84-
pub context_builder: ContextBuilder<P>,
85-
8690
/// The strategy used to assign contexts to scripts
8791
pub context_policy: ContextPolicy,
8892

@@ -121,7 +125,6 @@ impl<P: IntoScriptPluginParams> Default for ScriptingPlugin<P> {
121125
fn default() -> Self {
122126
Self {
123127
runtime_settings: Default::default(),
124-
context_builder: Default::default(),
125128
context_policy: ContextPolicy::default(),
126129
language: Default::default(),
127130
context_initializers: Default::default(),
@@ -139,7 +142,6 @@ impl<P: IntoScriptPluginParams> Plugin for ScriptingPlugin<P> {
139142
runtime: P::build_runtime(),
140143
})
141144
.insert_resource::<ContextLoadingSettings<P>>(ContextLoadingSettings {
142-
loader: self.context_builder.clone(),
143145
context_initializers: self.context_initializers.clone(),
144146
context_pre_handling_initializers: self.context_pre_handling_initializers.clone(),
145147
emit_responses: self.emit_responses,

crates/languages/bevy_mod_scripting_lua/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use bevy_mod_scripting_core::{
1010
function::namespace::Namespace, globals::AppScriptGlobalsRegistry,
1111
script_value::ScriptValue, ThreadWorldContainer, WorldContainer,
1212
},
13-
context::{ContextBuilder, ContextInitializer, ContextPreHandlingInitializer},
13+
context::{ContextInitializer, ContextPreHandlingInitializer},
1414
error::ScriptError,
1515
event::CallbackLabel,
1616
reflection_extensions::PartialReflectExt,
@@ -38,6 +38,14 @@ impl IntoScriptPluginParams for LuaScriptingPlugin {
3838
fn handler() -> bevy_mod_scripting_core::handler::HandlerFn<Self> {
3939
lua_handler
4040
}
41+
42+
fn context_loader() -> bevy_mod_scripting_core::context::ContextLoadFn<Self> {
43+
lua_context_load
44+
}
45+
46+
fn context_reloader() -> bevy_mod_scripting_core::context::ContextReloadFn<Self> {
47+
lua_context_reload
48+
}
4149
}
4250

4351
// necessary for automatic config goodies
@@ -58,10 +66,6 @@ impl Default for LuaScriptingPlugin {
5866
LuaScriptingPlugin {
5967
scripting_plugin: ScriptingPlugin {
6068
runtime_settings: RuntimeSettings::default(),
61-
context_builder: ContextBuilder::<LuaScriptingPlugin> {
62-
load: lua_context_load,
63-
reload: lua_context_reload,
64-
},
6569
context_initializers: vec![
6670
|_script_id, context| {
6771
// set the world global

crates/languages/bevy_mod_scripting_rhai/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use bevy_mod_scripting_core::{
1313
function::namespace::Namespace, globals::AppScriptGlobalsRegistry,
1414
script_value::ScriptValue, ThreadWorldContainer, WorldContainer,
1515
},
16-
context::{ContextBuilder, ContextInitializer, ContextPreHandlingInitializer},
16+
context::{ContextInitializer, ContextPreHandlingInitializer},
1717
error::ScriptError,
1818
event::CallbackLabel,
1919
reflection_extensions::PartialReflectExt,
@@ -55,6 +55,14 @@ impl IntoScriptPluginParams for RhaiScriptingPlugin {
5555
fn handler() -> bevy_mod_scripting_core::handler::HandlerFn<Self> {
5656
rhai_callback_handler
5757
}
58+
59+
fn context_loader() -> bevy_mod_scripting_core::context::ContextLoadFn<Self> {
60+
rhai_context_load
61+
}
62+
63+
fn context_reloader() -> bevy_mod_scripting_core::context::ContextReloadFn<Self> {
64+
rhai_context_reload
65+
}
5866
}
5967

6068
/// The rhai scripting plugin. Used to add rhai scripting to a bevy app within the context of the BMS framework.
@@ -83,10 +91,6 @@ impl Default for RhaiScriptingPlugin {
8391
Ok(())
8492
}],
8593
},
86-
context_builder: ContextBuilder {
87-
load: rhai_context_load,
88-
reload: rhai_context_reload,
89-
},
9094
context_initializers: vec![
9195
|_, context| {
9296
context.scope.set_or_push(

crates/testing_crates/test_utils/src/test_plugin.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ macro_rules! make_test_plugin {
3939
Ok($ident::bindings::script_value::ScriptValue::Unit)
4040
}) as $ident::HandlerFn<Self>
4141
}
42+
43+
fn context_loader() -> $ident::ContextLoadFn<Self> {
44+
(|attachment, content, context_initializers, pre_handling_initializers, runtime| {
45+
Ok(TestContext {
46+
invocations: vec![],
47+
})
48+
})
49+
}
50+
51+
fn context_reloader() -> $ident::ContextReloadFn<Self> {
52+
(|attachment,
53+
content,
54+
previous_context,
55+
context_initializers,
56+
pre_handling_initializers,
57+
runtime| {
58+
previous_context.invocations.clear();
59+
Ok(())
60+
})
61+
}
4262
}
4363

4464
#[derive(Default, std::fmt::Debug)]

0 commit comments

Comments
 (0)