mlua icon indicating copy to clipboard operation
mlua copied to clipboard

Add option to allow error messages to use __type metafield on luau if set over the type name

Open cheesycod opened this issue 8 months ago • 2 comments

This normally doesn't matter much unless the userdata struct itself is generic. E.g.

pub struct Foo<T: FooerTrait> {
   pub foo: T
}

impl LuaUserData for Foo<T> {
  <some code here that defines methods and fields, particularly a __type metamethod on Luau>
}

In luau, ops like type() and even the debug display will use __type correctly however argument conversion errors (forgetting to use method syntax etc) uses the full type name instead of the __type metamethod set. This leads to longer/harder to read error messages, especialy if the name of T is long (e.g. Foo<TestingFooerTrait> instead of Foo in argument conversion errors assuming __type is set to Foo)

Due to backward compatibility reasons and cases where this behaviour is not desired however, this should probably be a flag (either in LuaOptions or in UserData itself)

cheesycod avatar May 05 '25 17:05 cheesycod

Can you show an example to fully understand what specific type conversion / error message you meant?

Generally error messages and type names are initialised statically during metatable creation. Whatever user defined in __type is set well after everything is initialised. Passing wrong values to methods and generated error messages as a result does not even have access to metatable and I doubt will have.

khvzak avatar May 05 '25 19:05 khvzak

Can you show an example to fully understand what specific type conversion / error message you meant?

Generally error messages and type names are initialised statically during metatable creation. Whatever user defined in __type is set well after everything is initialised. Passing wrong values to methods and generated error messages as a result does not even have access to metatable and I doubt will have.

E.g.

> evt,ctx=...
> ctx.has_cap()
error: bad argument `self` to `TemplateContext<CliKhronosContext>.has_cap`: error converting Lua missing argument to userdata
stack traceback:
        [C]: in ?
        repl:1: in ?

Since type is a static metafield, it should(?) be possible to extract out in the metatable init itself(?)

cheesycod avatar May 05 '25 20:05 cheesycod