Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>//This has a large number of steps, but I'm gonna post them all. This is all using native Lua 5 and the lua CAPI. int CreateInstanceOfT(lua_State* L) { new (lua_newuserdata(L, sizeof(T))) T(constructor args); return 1; } int CallSomeFuncOnT(lua_State* L) { if (lua_istable(L, 1)) { // If we're passed a table, get CData lua_getfield(L, 1, "CData"); lua_replace(L, 1); } if (!lua_touserdata(L, 1)) lua_error(L); // longjmp out. T&amp; ref = *(T*)lua_touserdata(L, 1); ref.SomeFunc(); // If you want args, I'll assume that you can pass them yourself return 0; } int main() { lua_State* L = luaL_newstate(); lua_pushcfunction(L, CreateInstanceOfT); lua_setglobal(L, "CreateInstanceOfT"); lua_pushcfunction(L, CallSomeFuncOnT); lua_setglobal(L, "CallSomeFuncOnT"); luaL_dofile(L, "something.lua"); lua_close(L); } -- Accompanying Lua code: semicolons are optional but I do out of habit. In something.lua function CreateCInstance() local Instance = { CData = CreateInstanceOfT(); SomeFunc = CallSomeFuncOnT; } return Instance; end local object = CreateCInstance(); object:SomeFunc(); // Calls somefunc. </code></pre> <p>I could post a great quantity of detail about how to make exposure easier, and how to make inheritance, and suchlike - and it'll need altering if you want to expose more than one T (I think the most common solution is a simple <code>struct { std::auto_ptr&lt;void&gt;, int type }</code> deal). But, it should be a starting point if you don't understand anything about this process.</p> <p>Bascally, first, we ask Lua to allocate some space (the userdata), then put T in it. When CallSomeFuncOnT comes up, first we ask it if we have a table (many Lua classes are based around tables, as they support object orientation, metatables, and such), and get the userdata out, which we then convert into a pointer to our object, and then convert into a reference. Remember that lua_touserdata gives you a void*, so you'd better be damn sure about what's on the other end. Then we call somefunc and return. In Main, we just register the functions as globals.</p> <p>Now, in Lua, when you call CreateInstanceOfT, it effectively just calls the T constructor, transparently to the Lua user. Then we ditch it in a table, which is simpler for Lua novices, and call SomeFunc by passing it this table. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload