3

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?

3
  • There should be no need to reset the line number. Two luaL_loadbuffer should no interfere with each other. You'll have to show us the contents of str and str2.
    – lhf
    Jun 6, 2013 at 17:59
  • And the line number of the error actually increases if you put more new lines in str1?
    – dualed
    Jun 7, 2013 at 9:18
  • The contents of str and str2 is already shown. Yes, the line number increases adding lines to str. Jun 8, 2013 at 6:50

3 Answers 3

1

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;

2
  • This is good observation but it does not explain what the OP saw. His code is not "constructing lua code sequential".
    – lhf
    Jun 7, 2013 at 13:04
  • It returns line 9 as error source. Same like using loadbuffer. Jun 8, 2013 at 7:00
0

You are right Enigma, the code from str2 is appended consecutive. A breakpoint in

static void statement (LexState *ls) {

in lparser.cpp shows LexState.linenumber to be 5 and 7 for str, and 5, 7, 14 and 16 for str2. So str is lexed and added to the VM twice. I will find a different way to put a script made of multiple files into one VM.

0

Just if someone would need it too.

Add this function to lauxlib.h

LUALIB_API int (luaL_loadbuffers) (lua_State *L, size_t count, const char **buff, size_t *sz,
                                   const char **name, const char *mode);

and to lauxlib.c

#include"lzio.h"
#include"ldo.h"
#include"ltable.h"
#include"lgc.h"
LUALIB_API int luaL_loadbuffers (lua_State *L, size_t count, const char **buff, size_t *sz,
                                   const char **name, const char *mode)
{
    ZIO z;
    int status;

    int i;

    for( i=0; i<count; i++)
    {
        LoadS ls;
        ls.s = buff[i];
        ls.size = sz[i];

        lua_lock(L);

        luaZ_init(L, &z, getS, &ls);
        status = luaD_protectedparser(L, &z, name[i], mode);

        if (status == LUA_OK) {  /* no errors? */
            LClosure *f = clLvalue(L->top - 1);  /* get newly created function */
            if (f->nupvalues == 1) {  /* does it have one upvalue? */

                /* get global table from registry */
                Table *reg = hvalue(&G(L)->l_registry);
                const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
                /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
                setobj(L, f->upvals[0]->v, gt);
                luaC_barrier(L, f->upvals[0], gt);


            } // == 1

            lua_pcall( L, 0, 0, 0);
        }

        lua_unlock(L);

        if( status != LUA_OK )
            break;
    }

    return status;
}

Every string/file gets its own line numnbering. It is just a copy, almost, of lua_load in lapi.c. So easy to adjust in a new release of LUA.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.