Skip to content

Commit 8721a8a

Browse files
committed
add location information to function calls
1 parent ab3d48c commit 8721a8a

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

crates/bevy_mod_scripting_bindings/src/function/script_function.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,36 @@ pub trait ScriptFunctionMut<'env, Marker> {
4444
#[reflect(opaque)]
4545
pub struct FunctionCallContext {
4646
language: Language,
47+
location_context: Option<LocationContext>,
48+
}
49+
50+
#[derive(Clone, Debug, Reflect)]
51+
/// Describes a location within a script
52+
pub struct LocationContext {
53+
/// The line number
54+
pub line: u32,
55+
/// The column number
56+
pub col: u32,
4757
}
4858

4959
impl FunctionCallContext {
5060
/// Create a new FunctionCallContext with the given 1-indexing conversion preference
5161
pub const fn new(language: Language) -> Self {
52-
Self { language }
62+
Self {
63+
language,
64+
location_context: None,
65+
}
66+
}
67+
68+
/// Creates a new function call context with location information
69+
pub const fn new_with_location(
70+
language: Language,
71+
location_context: Option<LocationContext>,
72+
) -> Self {
73+
Self {
74+
language,
75+
location_context,
76+
}
5377
}
5478

5579
/// Tries to access the world, returning an error if the world is not available
@@ -68,6 +92,11 @@ impl FunctionCallContext {
6892
pub fn language(&self) -> Language {
6993
self.language.clone()
7094
}
95+
96+
/// Returns call location inside the script if available
97+
pub fn location(&self) -> Option<&LocationContext> {
98+
self.location_context.as_ref()
99+
}
71100
}
72101

73102
#[derive(Reflect, Clone, DebugWithTypeInfo)]

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use std::{
55

66
use bevy_mod_scripting_asset::Language;
77
use bevy_mod_scripting_bindings::{
8-
error::InteropError, function::script_function::FunctionCallContext, script_value::ScriptValue,
8+
LocationContext, error::InteropError, function::script_function::FunctionCallContext,
9+
script_value::ScriptValue,
910
};
1011
use bevy_platform::collections::HashMap;
1112
use mlua::{FromLua, IntoLua, Value, Variadic};
@@ -140,18 +141,32 @@ impl IntoLua for LuaScriptValue {
140141
ScriptValue::Reference(r) => LuaReflectReference::from(r).into_lua(lua)?,
141142
ScriptValue::Error(script_error) => return Err(mlua::Error::external(script_error)),
142143
ScriptValue::Function(function) => lua
143-
.create_function(move |_lua, args: Variadic<LuaScriptValue>| {
144+
.create_function(move |lua, args: Variadic<LuaScriptValue>| {
145+
let loc = lua.inspect_stack(0).map(|debug| LocationContext {
146+
line: debug.curr_line() as u32,
147+
col: 0,
148+
});
144149
let out = function
145-
.call(args.into_iter().map(Into::into), LUA_CALLER_CONTEXT)
150+
.call(
151+
args.into_iter().map(Into::into),
152+
FunctionCallContext::new_with_location(Language::Lua, loc),
153+
)
146154
.map_err(IntoMluaError::to_lua_error)?;
147155

148156
Ok(LuaScriptValue::from(out))
149157
})?
150158
.into_lua(lua)?,
151159
ScriptValue::FunctionMut(function) => lua
152-
.create_function(move |_lua, args: Variadic<LuaScriptValue>| {
160+
.create_function(move |lua, args: Variadic<LuaScriptValue>| {
161+
let loc = lua.inspect_stack(0).map(|debug| LocationContext {
162+
line: debug.curr_line() as u32,
163+
col: 0,
164+
});
153165
let out = function
154-
.call(args.into_iter().map(Into::into), LUA_CALLER_CONTEXT)
166+
.call(
167+
args.into_iter().map(Into::into),
168+
FunctionCallContext::new_with_location(Language::Lua, loc),
169+
)
155170
.map_err(IntoMluaError::to_lua_error)?;
156171

157172
Ok(LuaScriptValue::from(out))

0 commit comments

Comments
 (0)