Skip to content

Commit 6540642

Browse files
committed
fill in all the other operators in lua
1 parent 0f46de6 commit 6540642

File tree

7 files changed

+166
-19
lines changed

7 files changed

+166
-19
lines changed

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

Lines changed: 113 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use bevy_mod_scripting_core::{
2323
pretty_print::{DisplayWithWorld, ReflectReferencePrinter},
2424
script_value::ScriptValue,
2525
ReflectAllocator, ReflectRefIter, ReflectReference, ReflectionPathExt, TypeIdSource,
26-
WorldCallbackAccess,
26+
WorldCallbackAccess, WorldGuard,
2727
},
2828
error::{InteropError, ScriptError, ScriptResult},
2929
reflection_extensions::{PartialReflectExt, TypeIdExtensions},
@@ -130,6 +130,27 @@ fn iter_dynamic_function_overloads(
130130
.into_iter()
131131
}
132132

133+
fn try_call_overloads(
134+
lua: &Lua,
135+
key: &str,
136+
type_id: TypeId,
137+
args: Vec<ScriptValue>,
138+
world: WorldGuard,
139+
) -> Result<LuaScriptValue, InteropError> {
140+
let overloads = iter_dynamic_function_overloads(lua, key, type_id);
141+
let mut last_error = None;
142+
for overload in overloads {
143+
match overload.call_script_function(args.clone(), world.clone(), lua_caller_context()) {
144+
Ok(out) => return Ok(out.into()),
145+
Err(e) => last_error = Some(e),
146+
}
147+
}
148+
149+
Err(last_error
150+
.unwrap_or_else(|| InteropError::missing_function(type_id, key.to_string()).into())
151+
.into())
152+
}
153+
133154
impl UserData for LuaReflectReference {
134155
fn add_methods<T: UserDataMethods<Self>>(m: &mut T) {
135156
m.add_meta_function(
@@ -190,28 +211,101 @@ impl UserData for LuaReflectReference {
190211
let world = lua.get_world();
191212
let self_: ReflectReference = self_.into();
192213
let other: ScriptValue = other.into();
214+
let target_type_id = self_.tail_type_id(world.clone())?.or_fake_id();
215+
let args = vec![ScriptValue::Reference(self_), other];
216+
Ok(try_call_overloads(lua, "sub", target_type_id, args, world)?)
217+
},
218+
);
193219

220+
m.add_meta_function(
221+
MetaMethod::Add,
222+
|lua, (self_, other): (LuaReflectReference, LuaScriptValue)| {
223+
let world = lua.get_world();
224+
let self_: ReflectReference = self_.into();
225+
let other: ScriptValue = other.into();
194226
let target_type_id = self_.tail_type_id(world.clone())?.or_fake_id();
227+
let args = vec![ScriptValue::Reference(self_), other];
228+
Ok(try_call_overloads(lua, "add", target_type_id, args, world)?)
229+
},
230+
);
195231

196-
let overloads = iter_dynamic_function_overloads(lua, "sub", target_type_id);
197-
let mut last_error = None;
198-
let call_args = vec![ScriptValue::Reference(self_), other];
199-
for overload in overloads {
200-
match overload.call_script_function(
201-
call_args.clone(),
202-
world.clone(),
203-
lua_caller_context(),
204-
) {
205-
Ok(out) => return LuaScriptValue::from(out).into_lua(lua),
206-
Err(e) => last_error = Some(e),
207-
}
208-
}
232+
m.add_meta_function(
233+
MetaMethod::Mul,
234+
|lua, (self_, other): (LuaReflectReference, LuaScriptValue)| {
235+
let world = lua.get_world();
236+
let self_: ReflectReference = self_.into();
237+
let other: ScriptValue = other.into();
238+
let target_type_id = self_.tail_type_id(world.clone())?.or_fake_id();
239+
let args = vec![ScriptValue::Reference(self_), other];
240+
Ok(try_call_overloads(lua, "mul", target_type_id, args, world)?)
241+
},
242+
);
243+
244+
m.add_meta_function(
245+
MetaMethod::Div,
246+
|lua, (self_, other): (LuaReflectReference, LuaScriptValue)| {
247+
let world = lua.get_world();
248+
let self_: ReflectReference = self_.into();
249+
let other: ScriptValue = other.into();
250+
let target_type_id = self_.tail_type_id(world.clone())?.or_fake_id();
251+
let args = vec![ScriptValue::Reference(self_), other];
252+
Ok(try_call_overloads(lua, "div", target_type_id, args, world)?)
253+
},
254+
);
209255

210-
Err(last_error
211-
.unwrap_or_else(|| {
212-
InteropError::missing_function(target_type_id, "sub".to_string()).into()
213-
})
214-
.into())
256+
m.add_meta_function(
257+
MetaMethod::Mod,
258+
|lua, (self_, other): (LuaReflectReference, LuaScriptValue)| {
259+
let world = lua.get_world();
260+
let self_: ReflectReference = self_.into();
261+
let other: ScriptValue = other.into();
262+
let target_type_id = self_.tail_type_id(world.clone())?.or_fake_id();
263+
let args = vec![ScriptValue::Reference(self_), other];
264+
Ok(try_call_overloads(lua, "rem", target_type_id, args, world)?)
265+
},
266+
);
267+
268+
m.add_meta_function(MetaMethod::Unm, |lua, self_: LuaReflectReference| {
269+
let world = lua.get_world();
270+
let self_: ReflectReference = self_.into();
271+
let target_type_id = self_.tail_type_id(world.clone())?.or_fake_id();
272+
let args = vec![ScriptValue::Reference(self_)];
273+
Ok(try_call_overloads(lua, "neg", target_type_id, args, world)?)
274+
});
275+
276+
m.add_meta_function(
277+
MetaMethod::Pow,
278+
|lua, (self_, other): (LuaReflectReference, LuaScriptValue)| {
279+
let world = lua.get_world();
280+
let self_: ReflectReference = self_.into();
281+
let other: ScriptValue = other.into();
282+
let target_type_id = self_.tail_type_id(world.clone())?.or_fake_id();
283+
let args = vec![ScriptValue::Reference(self_), other];
284+
Ok(try_call_overloads(lua, "pow", target_type_id, args, world)?)
285+
},
286+
);
287+
288+
m.add_meta_function(
289+
MetaMethod::Eq,
290+
|lua, (self_, other): (LuaReflectReference, LuaScriptValue)| {
291+
let world = lua.get_world();
292+
let self_: ReflectReference = self_.into();
293+
let other: ScriptValue = other.into();
294+
let target_type_id = self_.tail_type_id(world.clone())?.or_fake_id();
295+
let args = vec![ScriptValue::Reference(self_), other];
296+
Ok(try_call_overloads(lua, "eq", target_type_id, args, world)?)
297+
},
298+
);
299+
300+
m.add_meta_function(
301+
MetaMethod::Lt,
302+
|lua, (self_, other): (LuaReflectReference, LuaScriptValue)| {
303+
let world = lua.get_world();
304+
let self_: ReflectReference = self_.into();
305+
let other: ScriptValue = other.into();
306+
let target_type_id = self_.tail_type_id(world.clone())?.or_fake_id();
307+
let args = vec![ScriptValue::Reference(self_), other];
308+
Ok(try_call_overloads(lua, "lt", target_type_id, args, world)?)
215309
},
216310
);
217311

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local a = Vec3.new(1.0, 2.0, 3.0)
2+
local b = Vec3.new(4.0, 5.0, 6.0)
3+
4+
assert((a + 1).x == 2.0, "Addition did not work")
5+
assert((a + 1).y == 3.0, "Addition did not work")
6+
assert((a + 1).z == 4.0, "Addition did not work")
7+
8+
assert((a + b).x == 5.0, "Addition did not work")
9+
assert((a + b).y == 7.0, "Addition did not work")
10+
assert((a + b).z == 9.0, "Addition did not work")
11+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local a = Vec3.new(2.0, 4.0, 6.0)
2+
local b = Vec3.new(1.0, 2.0, 3.0)
3+
4+
assert((a / 2).x == 1.0, "Division did not work")
5+
assert((a / 2).y == 2.0, "Division did not work")
6+
assert((a / 2).z == 3.0, "Division did not work")
7+
8+
assert((a / b).x == 2.0, "Division did not work")
9+
assert((a / b).y == 2.0, "Division did not work")
10+
assert((a / b).z == 2.0, "Division did not work")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local a = Vec3.new(2.0, -4.0, 6.0)
2+
local b = Vec3.new(4.0, 5.0, 6.0)
3+
4+
5+
assert((a == b) == false, "Equality did not work")
6+
assert((a ~= b) == true, "Inequality did not work")
7+
assert((a == a) == true, "Equality did not work")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local a = Vec3.new(2.0, 5.0, 6.0)
2+
local b = Vec3.new(1.0, 2.0, 3.0)
3+
4+
assert((a % 2).x == 0.0, "Modulus did not work")
5+
assert((a % 2).y == 1.0, "Modulus did not work")
6+
assert((a % 2).z == 0.0, "Modulus did not work")
7+
8+
assert((a % b).x == 0.0, "Modulus did not work")
9+
assert((a % b).y == 1.0, "Modulus did not work")
10+
assert((a % b).z == 0.0, "Modulus did not work")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local a = Vec3.new(1.0, 2.0, 3.0)
2+
local b = Vec3.new(4.0, 5.0, 6.0)
3+
4+
assert((a * 2).x == 2.0, "Multiplication did not work")
5+
assert((a * 2).y == 4.0, "Multiplication did not work")
6+
assert((a * 2).z == 6.0, "Multiplication did not work")
7+
8+
assert((a * b).x == 4.0, "Multiplication did not work")
9+
assert((a * b).y == 10.0, "Multiplication did not work")
10+
assert((a * b).z == 18.0, "Multiplication did not work")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local a = Vec3.new(2.0, -4.0, 6.0)
2+
3+
assert(-a.x == -2.0, "Negation did not work")
4+
assert(-a.y == 4.0, "Negation did not work")
5+
assert(-a.z == -6.0, "Negation did not work")

0 commit comments

Comments
 (0)