Skip to content

Commit

Permalink
feat: implement proper __eq override for schemas in Lua. (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag authored Nov 24, 2023
1 parent 8ac0772 commit a0e3784
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 42 deletions.
63 changes: 37 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions framework_crates/bones_scripting/src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use bones_lib::ecs::utils::*;
use parking_lot::Mutex;
use piccolo::{
registry::{Fetchable, Stashable},
AnyUserData, Closure, Context, Executor, FromValue, Fuel, Lua, ProtoCompileError,
StashedClosure, Table, Value,
AnyUserData, Closure, Context, Executor, FromValue, Lua, ProtoCompileError, StashedClosure,
Table, Value,
};
use send_wrapper::SendWrapper;
use std::{any::Any, rc::Rc, sync::Arc};
Expand Down
2 changes: 1 addition & 1 deletion framework_crates/bones_scripting/src/lua/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn env(ctx: Context) -> Table {
fn poll<'a>(
&mut self,
ctx: Context<'gc>,
_fuel: &mut Fuel,
_ex: piccolo::Execution<'gc, '_>,
mut stack: Stack<'gc, 'a>,
) -> Result<SequencePoll<'gc>, Error<'gc>> {
if self.mode == Mode::Init {
Expand Down
23 changes: 10 additions & 13 deletions framework_crates/bones_scripting/src/lua/bindings/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,17 @@ pub fn metatable(ctx: Context) -> Table {
}),
);

let eq_fn = ctx.state.registry.stash(
&ctx,
AnyCallback::from_fn(&ctx, move |ctx, _fuel, mut stack| {
let (this, other): (AnyUserData, AnyUserData) = stack.consume(ctx)?;
let (this, other) = (
this.downcast_static::<&Schema>()?,
other.downcast_static::<&Schema>()?,
);
stack.replace(ctx, this.id() == other.id());
Ok(CallbackReturn::Return)
}),
);
let eq_fn = AnyCallback::from_fn(&ctx, move |ctx, _fuel, mut stack| {
let (this, other): (AnyUserData, AnyUserData) = stack.consume(ctx)?;
let (this, other) = (
this.downcast_static::<&Schema>()?,
other.downcast_static::<&Schema>()?,
);
stack.replace(ctx, this.id() == other.id());
Ok(CallbackReturn::Return)
});

metatable.set(ctx, "__eq", eq_fn).unwrap();
metatable
.set(
ctx,
Expand All @@ -116,7 +114,6 @@ pub fn metatable(ctx: Context) -> Table {
let this = this.downcast_static::<&Schema>()?;

match key.as_bytes() {
b"eq" => stack.replace(ctx, ctx.state.registry.fetch(&eq_fn)),
b"name" => {
stack.replace(
ctx,
Expand Down

0 comments on commit a0e3784

Please sign in to comment.