“C like struct” in Lua for LONG BUFFER manipulatio

2020-06-24 08:25发布

I am wanting to code "mappings" in Lua rather than in C for the simplicity and beauty of Lua ;-)

So lets say in C I could have had the following:

typedef struct my_struct{
    char field_1[10];                              
    char field_2[250];                                
    char field_3[2000];                              
}my_struct;

my_struct   *pmy_struct;

pmy_struct = (my_struct *) some_buffer;

So I am wanting a way in Lua to have FIXED lengths on fields inside a "structure" so that the overall BUFFER OFFSETS stay in tact when it reaches the target system....

So lets say in the above struct I wanted to set "field_3" to the text "apple"......I still want that structure field to have an overall length of 2000 bytes...

标签: lua
2条回答
贼婆χ
2楼-- · 2020-06-24 08:32

You can do this with Lua.

If you wish to (or are forced to) stay with Lua 5.1, consider using the struct library.

raw_data = struct.pack("c10c250c2000", field1, field2, field3)
field1, field2, field3 = struct.unpack("c10c250c2000", raw_data)

However, the best solution available at the moment is LuaJIT; there are numerous benefits to using LuaJIT over the standard Lua implementation, but the one that most applies to you is the FFI library.

local ffi = require"ffi"
ffi.cdef[[
    typedef struct {
        char field_1[10];                              
        char field_2[250];                                
        char field_3[2000];                              
    } my_struct;
]]

local my_thing = ffi.new("my_struct")
my_thing.field_1 = "Ain't"
my_thing.field_2 = "this"
my_thing.field_3 = "great? :D"

local ptr_my_thing = ffi.new("my_struct*", my_thing)
ptr_my_thing.field_2 = [[
    LuaJIT does a great job at figuring out
    what you're trying to do.
]]
ptr_my_thing.field_3 = [[
    There are some cases where the generics of
    Lua cannot be used to infer information,
    so have a look at the LuaJIT site for specifics.
]]

print(ffi.string(ptr_my_thing.field_2))

It is a common misconception that generalised languages such as Lua and Python should not (or, in ignorance, cannot) be used for low-level specifics. LuaJIT is a major first step for "smart" language that allow you to work at any level.

查看更多
够拽才男人
3楼-- · 2020-06-24 08:33

Lua doesn't work that way. Lua does not have "structures". It does not have "fields" which have "sizes".

It has tables, which can have values of several different types. These values are mapped to keys (which are themselves values). While you can use metatables to prevent the addition of new keys, you can't force the "size" of a value to be anything in particular.

Or, to put it another way, stop trying to program Lua like it's C. They are different languages, and you should approach each language in its own way. In C, you want to care about the size of fields, the layout of structs, etc.

The reason to use Lua (and most scripting languages) is because you don't want to care about those things. You don't want to care if the string "apple" happens to be stored in a byte array 2000 bytes in size. And if you want to care about that, then you don't want to use Lua.

查看更多
登录 后发表回答