Suggestion: let string.unpack support userdata

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

Suggestion: let string.unpack support userdata

云风 Cloud Wu
string.unpack is a power tool to process the binary data. Sometimes, we use userdata as a string (binary data) buffer. I think if string.unpack support userdata as parameter would be better for this case.

And, if the userdata is a C struct, we can simply use string.unpack to extract the data from the C struct, otherwise we should copy it (by lua_pushlstring) to a string first.

Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: let string.unpack support userdata

Sean Conner
It was thus said that the Great 云风 Cloud Wu once stated:
> string.unpack is a power tool to process the binary data. Sometimes, we use
> userdata as a string (binary data) buffer. I think if string.unpack support
> userdata as parameter would be better for this case.
>
> And, if the userdata is a C struct, we can simply use string.unpack to
> extract the data from the C struct, otherwise we should copy it (by
> lua_pushlstring) to a string first.

  http://lua-users.org/lists/lua-l/2014-08/msg00720.html

  It's part of a much larger thread about using userdata as strings.  I
realize that what you are proposing isn't quite the same as the above
thread, but that particular message is probably worth repeating here.

  -spc


Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: let string.unpack support userdata

Tim Hill
In reply to this post by 云风 Cloud Wu

> On Feb 21, 2017, at 1:21 AM, 云风 Cloud Wu <[hidden email]> wrote:
>
> string.unpack is a power tool to process the binary data. Sometimes, we use userdata as a string (binary data) buffer. I think if string.unpack support userdata as parameter would be better for this case.
>
> And, if the userdata is a C struct, we can simply use string.unpack to extract the data from the C struct, otherwise we should copy it (by lua_pushlstring) to a string first.
>

One problem with this is that it violates the principle that userdata is opaque to Lua. The cycle userdata -> string -> userdata allows Lua to manipulate the contents of userdata, which is currently not allowed.

—Tim