-2

Say I have the address of a table - how would I "cast" a table variable to it? I'm not concerned about "bad practice" or crashes because this is just an individual problem.

I want to do something like lua_table tab = *(lua_table*)0xaddr ...but within the Lua global environment.

I understand how bad this is but I really need to know if this is possible. It may not be the best way to do what I'm trying to do, but I'm quite certain it's the easiest and that it will work if this is possible. I am using Lua 5.1.4.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lupe
  • 319
  • 1
  • 3
  • 16
  • 2
    how did you get the address? – user1810087 Jan 04 '16 at 19:04
  • 1
    @user I'm assuming he ```tostring```'d it. – warspyking Jan 04 '16 at 19:06
  • 3
    "I'm quite certain it's the easiest and that it will work if this is possible" I'm quite certain that whatever you think is the "easiest" way to do whatever you're trying to do, is most assuredly *not* the "easiest" way to do it. – Nicol Bolas Jan 04 '16 at 19:06
  • Perhaps, but I'd like to try it. – Lupe Jan 04 '16 at 19:10
  • I am not sure what the question is. You already know it's bad. You already know how to do this BAD thing (by casting). So what is that you are asking? – SergeyA Jan 04 '16 at 19:20
  • "You already know how to do this BAD thing (by casting). So what is that you are asking?" Knowing that the logical way would be to cast doesn't mean I know how, as it seems to be forbidden black magic in Lua for good reason (mainly the gc?) – Lupe Jan 04 '16 at 19:26

2 Answers2

3

Lua exposes no API for doing this.

It would be easier and much more robust to fix your design rather than trying to force this to work.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
1

In order to do what you're attempting, you must:

  1. Get a pointer to the Lua table's data structure. I'm sure you believe that you already have such a pointer. But there's nothing in Lua that guarantees that the pointer you have obtained (through some means) is actually a pointer to the table data structure. It could be a pointer to something else. So you need to hunt through Lua's internals to make sure that wherever you're getting this pointer from is giving you a pointer to the actual object.

  2. Find the correct type, declared within Lua's internals. There is some C type (Lua's written in C, not C++) that Lua uses to represent the main table data structure. You will have to track down this struct definition and use that.

    A cursory examination of the Lua library suggests that the main table data structure is defined in lobject.h, under the name Table.

  3. Find the internal APIs that Lua uses to manipulate this table correctly. It's obviously some kind of hash table, but you're going to need to use Lua's functions to actually do anything with it.

    A cursory examination of Lua's internals suggests that this code would be found in ltable.h. However, there are probably more APIs than that. Also, do note that many of those APIs take a lua_State, so they may be doing some stack fiddling.

    You will also need to look through Lua's API so that you can learn how to use them without breaking the table. Lua may have certain expectations about when certain functions are called or the order between them or whatever. Break these at your own peril.

Even then, this:

Table tab = *(Table*)0xaddr

Will never work. Or at least, not the way you mean for it to. Lua is written in C. Which means that Table is not going to work like a C++ value type. Copying it will only do a bitwise copy. So modifying tab will only modify your local copy of those values. If those are pointers to other data structures, that may be OK, since your pointers and the original pointers point to the same data structures. But if you perform some operation that changes the Table::flags field on the table, for example, the table stored in Lua will not be affected, only your local tab copy will be.

You have to manipulate the object as a pointer, not a copy of the original.

Table *tab = reinterpret_cast<Table*>(0xaddr);
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982