Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lua line numbers after multiple calls to loadbuffer

Tags:

numbers

line

lua

I load two strings with loadbuffer into one lua_state.

if( luaL_loadbuffer( L, str.c_str(), str.size(), "line") != 0 )
{
    printf( "%s\n", lua_tostring ((lua_State *)L, -1));
}
lua_pcall(L, 0, 0, 0);

if( luaL_loadbuffer( L, str2.c_str(), str2.size(), "line2") != 0 )
{
    printf( "%s\n", lua_tostring ((lua_State *)L, -1));
}
lua_pcall(L, 0, 0, 0);

For example:

function f ()


    print( "Hello World!")
end

and

function g ()

    f(
end

The forgotten ) in the second string throws an error:

[string "line2"]:9: unexpected Symbol

But 9 is the line number from string 1 plus string 2. The line number should be 3.

Is there a way to reset the line number counter before call to loadbuffer?

like image 618
Karl Schulte Avatar asked Dec 29 '25 11:12

Karl Schulte


1 Answers

I guess this link describes your situation: http://www.corsix.org/content/common-lua-pitfall-loading-code

You are loading two chunks of information, calling the chunks will put them consecutive into the global table. The lua_pcall(L, 0, 0, 0); is not calling your f() and g(), but is constructing your lua code sequential.

Your code could possibly be simplified to:

if (luaL_dostring(L, str.c_str()))
{
    printf("%s\n", lua_tostring (L, -1));
}
if (luaL_dostring(L, str2.c_str()));
{
    printf("%s\n", lua_tostring (L, -1));
}

which also protects against calling a chunk when it fails to load;

like image 185
Enigma Avatar answered Jan 02 '26 14:01

Enigma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!