hulkkiller
hulkkiller

Reputation: 41

Lua Dump in C++

I want to export a binary Chunk with lua_dump or luaU_dump..

The error: the return is only <-LuaR

Don't have a compile error or static problems with the code, only the <-LuaR return

What can i do? to result the problem?

private:
const char* buildLua(QString luaScript)
{
    const Proto* f;
    char *byteCode = 0L;
    size_t byteCodeLen = 0;
    wdata wd = { &byteCodeLen, &byteCode };
    string ts = luaScript.toStdString();
    const char* cs;
    lua_State *L = luaL_newstate();
    f=combine(L,0);
    luaL_loadstring(L,ts.c_str());
    luaL_openlibs(L);
    lua_lock(L);
    luaU_dump(L,f,kpt_lua_Writer,&wd,1);
    lua_unlock(L);
    lua_close(L);
    cs = byteCode;
    return cs;
}

static const char* kpt_lua_Reader(lua_State *L, void *ud, size_t *size)
{
    UNUSED(L);
    if ((*(int*)ud)--)
    {
        *size=sizeof(FUNCTION)-1;
        return FUNCTION;
    }
    else
    {
        *size=0;
        return NULL;
    }
}

static int kpt_lua_Writer(lua_State * /*l*/, const void *p, size_t sz, void *ud)
{
    wdata *wd = (wdata *)ud;

    char *newData;

    if((newData = (char *)realloc(*(wd->data), (*(wd->len)) + sz))) {
        memcpy(newData + (*(wd->len)), p, sz);
        *(wd->data) = newData;
        *(wd->len) += sz;
    } else {
        free(newData);
        return 1;
    }

    return 0;
}

static const Proto* combine(lua_State* L, int n)
{
    if (n==1)
        return toproto(L,-1);
    else
    {
        Proto* f;
        int i=n;
        if (lua_load(L,kpt_lua_Reader,&i,"=(keppedev)",NULL)!=LUA_OK) fatal(lua_tostring(L,-1));
        f=toproto(L,-1);
        for (i=0; i<n; i++)
        {
            f->p[i]=toproto(L,i-n-1);
            if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0;
        }
        f->sizelineinfo=0;
        return f;
    }
}

static void fatal(const char* message)
{
    QWidget *widget = new QWidget();
    QMessageBox::warning(widget,"Keppe Develop",message);
}

http://www.keppe.org/img/LuaR.png

Upvotes: 4

Views: 3978

Answers (3)

hulkkiller
hulkkiller

Reputation: 41

I am fixing it self with the Code

Source File (Contain)

void frmDevelop::on_actionBuild_Project_triggered()
{
    if (!isInLuaMode) return;
    const char* output = buildLua(cedit->document()->toPlainText());
    int length = strlen(output);
}

Header File (Contain)

private:
const char* buildLua(QString luaScript)
    {
        string ts = luaScript.toStdString();
        lua_State *L = luaL_newstate();
        FILE* D = fopen("test.luac","wb");
        luaL_openlibs(L);
        luaL_loadstring(L,ts.c_str());
        lua_lock(L);
        int re = lua_dump(L,kpt_lua_Writer,D);
        lua_unlock(L);
        lua_close(L);
        fclose(D);
        return QString::number(re).toStdString().c_str();
    }

    static int kpt_lua_Writer(lua_State * /*l*/, const void *p, size_t sz, void *u)
    {
        return (fwrite(p,sz,1,(FILE*)u)!=1) && (sz!=0);
    }

test.luac must replace with your File Name :D

Upvotes: 0

greatwolf
greatwolf

Reputation: 20838

The compiler error is cause by an invalid function prototype where frmDevelop::writer is implemented.

extern "C"
{
static int frmDevelop::writer(lua_State *L, const void *p, size_t size, void *u)
{
  // ...
}
}

You only need the static qualifier at the point of declaration in class frmDevelop. Also, extern "C" here isn't right since you're telling the compiler to not mangle the function name. But the compiler cannot honor that request because you're using a C++ feature(writer is part of frmDevelop and so you need the :: scope operator) -- extern "C" just gets ignored for frmDevelop::writer.

Change writer's implementation from:

extern "C"
static int frmDevelop::writer(lua_State *L, const void *p, size_t size, void *u)
{ /* ... */ }

to

int frmDevelop::writer(lua_State *L, const void *p, size_t size, void *u)
{ /* ... */ }

and that'll fix the compile error you're getting.

Upvotes: 0

lhf
lhf

Reputation: 72312

There is no need to use the internals of Lua. In any case, you should call luaL_loadstring or luaL_loadbuffer, not luaL_dostring, which executes the code in the string:

lua_State *L = luaL_newstate();
luaL_loadstring(L,s.c_str());
lua_dump(L,writer,NULL);
lua_close(L);

However, you should test the return values of both luaL_loadstring and lua_dump.

Upvotes: 0

Related Questions