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 8655533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:03:13+00:00 2026-06-12T15:03:13+00:00

I am using the answer to this question to redirect lua’s print to a

  • 0

I am using the answer to this question to redirect lua’s print to a stringstream. My function code is below.

My issue is that the code doesn’t always match what lua would have printed on it’s own. Most notably, when I attempt to print a function, I just get the function, rather than the function and address. Example:

> --in lua
> print(os.exit)
function: 0xabcdef01

> --in my interpreter
> print(os.exit)
function

The obvious solution is to force my custom print function to call lua’s tostring prior to writing to luaout (like the default print does). However, I can’t really figure out how to make this work. If anyone can help me out, I’d appreciate it a lot.

Here’s my custom print:

static int l_my_print(lua_State* L) {
    int nargs = lua_gettop(L);

    for (int i=1; i <= nargs; i++) {
        int t = lua_type(L, i);
        switch (t) {
            case LUA_TSTRING: { /* strings */
                luaout << lua_tostring(L, i);
                break;
            }
            case LUA_TBOOLEAN: { /* booleans */
                luaout << (lua_toboolean(L, i) ? "true" : "false");
                break;
            }
            case LUA_TNUMBER: { /* numbers */
                luaout << lua_tonumber(L, i);
                break;
            }
            default: { /* other values */
                luaout << lua_typename(L, t);
                break;
            }
        }
        if (i!=nargs){
            luaout << "\t";
        }
    }
    luaout << endl;

    return 0;
}
  • 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-06-12T15:03:14+00:00Added an answer on June 12, 2026 at 3:03 pm

    You can try on default this:

    lua_pushfstring(L, "%s: %p", luaL_typename(L, i), lua_topointer(L, i));
    

    or

     luaout << luaL_typename(L, i) << ": " << lua_topointer(L, i);
    

    This will add the name of the function and the pointer.

    You can call lua function from C++ (don’t guarantee to work as I have not tested it, but it should aside from grammatical errors)

    lua_getglobal(L, "tostring");
    lua_pushvalue (L, i);
    
    if (lua_pcall(L, 1, 1, 0) != 0) {
            printf("error running function `%s': %s\n", "tostring", lua_tostring(L, -1));
            return -1;
    }
    // get result
    char *result = luaL_checkstring (L, -1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the accepted answer from this question . The relevant code is
So I saw an answer in another question saying that this should work: using
Im using the answer to this question to convert an array of XML to
I'm using the accepted answer to this question to help me build an HTML
I'm using the first answer to this question Overcoming Python's limitations regarding instance methods
An answer to this question has led me to look into using Event Tracing
I am trying to integrated linkedIn using this question answer Posting LinkedIn message from
Using this answer , I created a sample localized app. My question is, is
I know that this has already been asked here but the answer (using a
Using this question and answer ( Use both Account and User tables with Devise

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.