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

  • Home
  • SEARCH
  • 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 1086253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:47:24+00:00 2026-05-16T22:47:24+00:00

I need a very simple c++ function that calls a lua function that returns

  • 0

I need a very simple c++ function that calls a lua function that returns an array of strings, and stores them as a c++ vector. The function can look something like this:

std::vector<string> call_lua_func(string lua_source_code);

(where lua source code contains a lua function that returns an array of strings).

Any ideas?

Thanks!

  • 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-16T22:47:25+00:00Added an answer on May 16, 2026 at 10:47 pm

    Here is some source that may work for you. It may need some more polish and testing. It expects that the Lua chunk is returning the array of strings, but with slight modification could call a named function in the chunk. So, as-is, it works with "return {'a'}" as a parameter, but not "function a() return {'a'} end" as a parameter.

    extern "C" {
    #include "../src/lua.h"
    #include "../src/lauxlib.h"
    }
    
    std::vector<string> call_lua_func(string lua_source_code)
    {
      std::vector<string> list_strings;
    
      // create a Lua state
      lua_State *L = luaL_newstate();
      lua_settop(L,0);
    
      // execute the string chunk
      luaL_dostring(L, lua_source_code.c_str());
    
      // if only one return value, and value is a table
      if(lua_gettop(L) == 1 && lua_istable(L, 1))
      {
        // for each entry in the table
        int len = lua_objlen(L, 1);
        for(int i=1;i <= len; i++)
        {
          // get the entry to stack
          lua_pushinteger(L, i);
          lua_gettable(L, 1);
    
          // get table entry as string
          const char *s = lua_tostring(L, -1);
          if(s)
          {
            // push the value to the vector
            list_strings.push_back(s);
          }
    
          // remove entry from stack
          lua_pop(L,1);
        }
      }
    
      // destroy the Lua state
      lua_close(L);
    
      return list_strings;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have very simple problem. I need to create model, that represent element of
I need a very simple function to draw a bunch of lines with anti-aliasing.
I need a very simple and clear example of how to create an OCX
I need to be able to merge two (very simple) JavaScript objects at runtime.
I need to do a few very simple URL manipulations in Java. Like get
I'm new here and I'm very stuck. I need help. Hopefully someone can tell
I'd like to write a very simple replacement for w3btrv7.dll so I can pass
I have a generic function that returns URLs. (It's a plugin function that returns
I need a very quick fix in a mod_rewrite expression for drupal we have
I need a very accurate way to time parts of my program. I could

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.