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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:56:18+00:00 2026-06-08T03:56:18+00:00

When trying to dump a function’s bytecode to a buffer, I was surprised to

  • 0

When trying to dump a function’s bytecode to a buffer, I was surprised to see the Lua stack change size. Docs say that lua_dump should not push nor pop the stack. Some functions do and some do not.

This does:

function f() print "x" end

This does not:

function f() return "x" end

lua_dump in both cases returns 0 (no error).

I assume it has something to do with the use of the print function, but I can’t find any docs describing what is going on or what might be contained in the userdata. ..or whether that bytecode is going to be usable without it.

sample code:

static int base64_write(lua_State* L, unsigned char* str, size_t len, 
        struct luaL_Buffer *buf)
{
    unsigned int idx;
    char code[5];
    for (idx=0; idx<len; idx++){
        sprintf(code, "\\%03d", (unsigned int) str[idx]);
        //printf(code);
        luaL_addlstring(buf, code, 4);
    }
    //printf("\n");
    return 0;
}

void dump_function(lua_State* L){
    int stack_sz;
    int res;
    luaL_Buffer buf;

    luaL_buffinit(L, &buf);

    // ** test 1 - works as expected
    lua_settop(L,0);
    luaL_dostring(L, "function f() return 'x' end");
    lua_getglobal(L, "f");
    printf("stack sz: %i\n", lua_gettop(L));
    res = lua_dump(L, (lua_Writer)base64_write, &buf);
    printf("stack sz: %i\n", lua_gettop(L));

    // ** test 2 - not as expected
    lua_settop(L,0);
    luaL_dostring(L, "function f() print 'x' end");
    lua_getglobal(L, "f");
    printf("stack sz: %i\n", lua_gettop(L));
    res = lua_dump(L, (lua_Writer)base64_write, &buf);
    printf("stack sz: %i\n", lua_gettop(L));  // why is this 2?
    printf("type:%s\n", lua_typename(L, lua_type(L, -1))); // why userdata?
}

/* ----- Registration array ----- */
static const luaL_Reg R[] = {
    {"test",    dump_function},
    {NULL,      NULL} /* sentinel */
};

/* ----- Registration function ----- */

LUALIB_API int register_lua_tserialize(lua_State *L)
{
    lua_newtable(L); 
    lua_setglobal(L, "test"); 
    lua_getglobal(L, "test"); 
    luaL_setfuncs(L, R, 0); 
    lua_settop(L, 0);
    return 1;
}
  • 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-08T03:56:19+00:00Added an answer on June 8, 2026 at 3:56 am

    It’s because of the usage of the buffer object. See the documentation at http://pgl.yoyo.org/luai/i/luaL_Buffer

    You need to call luaL_pushresult to get rid of the intermediate buffer stack items.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that will dump a recursive tree of window
I'm trying to write a function that imports the mysql dump into the database.
I am trying to dump a process, say calc.exe When I run my program
I'm trying to write an extension method for objects that will dump the structure
I'm trying to run some statistics over the Stack Overflow data dump, and for
I've been trying to troubleshoot this, but can't find a good function that will
I'm trying to dump elements of an object's private property through an anonymous function
I'm trying to create a chaining function for working with strings that are returned
So I've got this simple login function that is trying to match email address
Chrome removes the function that I'm trying to pass through sendRequest. function sendQuery() {

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.