Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 947781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:08:08+00:00 2026-05-15T23:08:08+00:00

I have some classes in C++ that I would like to expose to Lua.

  • 0

I have some classes in C++ that I would like to expose to Lua. I can call Widget:New() to get return userdata with a metatable set to the table WidgetMeta. WidgetMeta has all the C++ functions in it, and it’s __index is set to itself, so I can do this:

w = Widget:New()
w:Foo() -- Foo is defined in C code

That’s all pretty straightforward.

Now here’s the part I can’t figure out. I want to be able to put Lua defined variables and functions on my userdata as if it was a table. This can’t be done directly obviously. I can’t drop it on the userdata, because I want it to be unique per userdata.

w1 = Widget:New()
w2 = Widget:New()

function w1:Bar() print "Hello!" end -- Both functions unique
function w1:Baz() print "World!" end -- to their own userdata

My current plan of attack is to have the metatable have a special table on it that maps between userdata and table where I can store per-userdata functions and variables. The problem is that I’m not sure what the best way to go about doing that is, or if there’s a better solution. so my question is twofold: When I set up my __index and __newindex metamethods, do I write them in Lua code in a text file and run it before I run the rest of the stuff, or do I put the Lua code directly from a C string in my program via luaL_loadstring, or do I do it with the C interface and deal with all the stack manipulations? and second, how do I write that function… but I’ll deal with that once I decide which is the best route to take.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:08:08+00:00Added an answer on May 15, 2026 at 11:08 pm

    Add a function environment to the userdata, and redirect access through that.

    Here’s some old code of mine that describes the process.

    static int l_irc_index( lua_State* L )
    {
        /* object, key */
        /* first check the environment */ 
        lua_getfenv( L, -2 );
        lua_pushvalue( L, -2 );
        lua_rawget( L, -2 );
        if( lua_isnoneornil( L, -1 ) == 0 )
        {
            return 1;
        }
    
        lua_pop( L, 2 );
    
        /* second check the metatable */    
        lua_getmetatable( L, -2 );
        lua_pushvalue( L, -2 );
        lua_rawget( L, -2 );
    
        /* nil or otherwise, we return here */
        return 1;
    }
    
    static int l_irc_newindex( lua_State* L )
    {
        /* object, key, value */
    
        lua_getfenv( L, -3 );
        lua_pushvalue( L, -3 );
        lua_pushvalue( L, -3 );
        lua_rawset( L, -3 );
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.