lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Am 25.08.2014 um 17:52 schröbte Coroutines:
On Mon, Aug 25, 2014 at 8:47 AM, Philipp Janda <siffiejoe@gmx.net> wrote:

You will probably have to write your own buffer userdata for that. I don't
think such a userdata is useful enough for most people that it should be
included in Lua's standard library (compared to a `FILE*` which is the only
userdata in Lua right now). If you really need pattern matching etc. in your
socket buffers, I suggest you start with a modified copy of `lstrlib.c`
(it's MIT after all). But even if you implement all string functions for
your buffer userdata, you won't be able to replace the immutable string type
in Lua because (immutable) strings are hashed by contents and (mutable)
userdata are hashed by identity/address. It would get awkward fast if you
modified the contents of a table key ...

I can make my own version of the string library that would largely be
duplicated code, as I'm just trying to get those functions to operate
on userdata.  I was hoping an interface/function-or-3 would be made
upstream to allow one to trick functions that take strings to accept
userdata.

The string library doesn't make sense for generic userdata, it only makes sense for character arrays a.k.a buffers. That's why I suggested you write a buffer userdata type. And please don't try to replace the string library with your own version. Just take `lstrlib.c`, replace `luaL_checkstring` with `luaL_checkudata`, and register those functions as methods of your buffer userdata. Yes, you duplicate a lot of code, but you stay backwards compatible with current Lua, and you stay type-safe.


Repeating what I have said earlier: I realize this would be a security
risk, and I hope that if something is possible it could be controlled
through a function or boolean switch within the debug library.

But it would be a shame if your socket library only worked in debug mode, wouldn't it?

Philipp