Exposing recursive struct as Lua Table

For historical reasons, I am constructing a struct in C++ that emulates a lua Table.

typedef union Value Value;
typedef struct LuaTable LuaTable;
typedef struct TableValue TableValue;

enum ValueType {
    INT = 0,
    BOOL = 1,
    FLOAT = 2,
    STRING = 3,
    TABLE = 4,
};

struct LuaTable {
    std::vector<TableValue> array;
    std::map<std::string, TableValue> hashmap;
};

union Value {
    int c_int;
    bool c_bool;
    double c_float;
    std::string c_str;
    LuaTable table;
};

struct TableValue {
  ValueType type;
  Value val;
};

I may simplify this such that a table is either a list or a map.

I would like to access this struct through LuaJIT FFI with some high-level syntax:

x = c_tbl.a.b.c

is a proxy for

LuaTable l_tbl {...};
...
l_tbl['a']['b']['c']

Before I start thinking about lazy evaluation (build up the prefix in some metatable), I would generally like to know if I am thinking about this the right way.

The simplest way would be to initialize c_tbl by parsing the LuaTable struct. This is the point where I am stuck. Is there a straightforward way to parse a recursive struct like this? I am willing to re-design this to alleviate parsing / traversal difficulties.



Read more here: https://stackoverflow.com/questions/64936405/exposing-recursive-struct-as-lua-table

Content Attribution

This content was originally published by user18764 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: