Feature request: make typeerror public somehow

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Feature request: make typeerror public somehow

Steven Degutis
While working on a Swift wrapper around Lua's C API (code is here[1]),
I came up with a way to extend Lua's argument-checking functionality.

The upside is that now I can assert that argument #2 is a "point",
meaning a table that contains keys "x" and "y" pointing to numbers.
This is admittedly not as efficient as just checking lua_type, but
it's also opt-in.

The downside is that I can't use luaL_checktype for these kinds of
custom types, because that function uses lua_type directly.

Hoping to find another solution, I dug into the source code of
luaL_checktype, and found that typeerror is exactly what I would want
to use here, asking the custom type for typeerror's tname argument.
But typeerror is a private function.

So in my program, I've modified Lua to contain a new public function,
luaL_typeerror, which just calls typeerror directly. I'm not sure how
generally useful this would be, but since this function would be
purely opt-in and can coexist peacefully with luaL_checktype, would it
be a good idea to create this new function in Lua proper (even if with
a different name)?

[1]: https://github.com/sdegutis/diywm/tree/master/Lua/Lua --
especially VirtualMachine.swift

Sincerely,
Anonymous Lua User

Reply | Threaded
Open this post in threaded view
|

Re: Feature request: make typeerror public somehow

Philipp Janda
Am 24.12.2014 um 07:47 schröbte Steven Degutis:

> While working on a Swift wrapper around Lua's C API (code is here[1]),
> I came up with a way to extend Lua's argument-checking functionality.
>
> The upside is that now I can assert that argument #2 is a "point",
> meaning a table that contains keys "x" and "y" pointing to numbers.
> This is admittedly not as efficient as just checking lua_type, but
> it's also opt-in.
>
> The downside is that I can't use luaL_checktype for these kinds of
> custom types, because that function uses lua_type directly.

What about `luaL_argcheck(L, is_point(L, 1), 1, "point expected")`?

>
> Hoping to find another solution, I dug into the source code of
> luaL_checktype, and found that typeerror is exactly what I would want
> to use here, asking the custom type for typeerror's tname argument.
> But typeerror is a private function.
>
> So in my program, I've modified Lua to contain a new public function,
> luaL_typeerror, which just calls typeerror directly. I'm not sure how
> generally useful this would be, but since this function would be
> purely opt-in and can coexist peacefully with luaL_checktype, would it
> be a good idea to create this new function in Lua proper (even if with
> a different name)?

In Lua 5.1 we had[1] this function, but it got removed[2] in Lua 5.2. To
be fair, it was a trivial function then, but now got a little more
complex for Lua 5.3.

>
> Sincerely,
> Anonymous Lua User
>

Philipp

   [1]: http://www.lua.org/source/5.1/lauxlib.c.html#luaL_typerror
   [2]: http://www.lua.org/manual/5.2/manual.html#8.3



Reply | Threaded
Open this post in threaded view
|

Re: Feature request: make typeerror public somehow

Steven Degutis
> What about `luaL_argcheck(L, is_point(L, 1), 1, "point expected")`?

If I use luaL_argcheck, which is essentially just luaL_argerror, then
I have to manually figure out the type name of the actual argument and
create an according error message, which is exactly what typeerror
does.

> In Lua 5.1 we had[1] this function, but it got removed[2] in Lua 5.2. To be
> fair, it was a trivial function then, but now got a little more complex for
> Lua 5.3.

Sorry, I'm not understanding. This is the trivial function I added to
Lua, which seems to work:

https://github.com/sdegutis/diywm/commit/13598c9e23ca0b3c8bf29447da22f6ab1bf768fd

Regards,
Anonymous Lua User

Reply | Threaded
Open this post in threaded view
|

Re: Feature request: make typeerror public somehow

Philipp Janda
Am 24.12.2014 um 20:00 schröbte Steven Degutis:
>
>> In Lua 5.1 we had[1] this function, but it got removed[2] in Lua 5.2. To be
>> fair, it was a trivial function then, but now got a little more complex for
>> Lua 5.3.
>
> Sorry, I'm not understanding. This is the trivial function I added to
> Lua, which seems to work:

I meant that the private `typeerror` itself was quite trivial before Lua
5.3: It just called `lua_pushfstring` and `luaL_argerror`, and thus
wasn't really worth keeping in the Lua API. The new version in Lua 5.3
is more complex.

>
> Regards,
> Anonymous Lua User
>

Philipp



Reply | Threaded
Open this post in threaded view
|

Re: Feature request: make typeerror public somehow

Steven Degutis
Quoth Mr. Janda:
> I meant that the private `typeerror` itself was quite trivial before Lua
> 5.3: It just called `lua_pushfstring` and `luaL_argerror`, and thus wasn't
> really worth keeping in the Lua API. The new version in Lua 5.3 is more
> complex.

In that case I strongly vote that luaL_typerror is restored.

Dear PUC-Rio: please? pretty please?