C++ 中的 Lua 转储

问题描述 投票:0回答:3

我想使用 lua_dump 或 luaU_dump 导出二进制块..

错误:仅返回<-LuaR

代码没有编译错误或静态问题,只有 <-LuaR return

我能做什么?结果出现问题?

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

c++ qt lua dump
3个回答
0
投票

不需要使用Lua的内部。无论如何,您应该调用

luaL_loadstring
luaL_loadbuffer
,而不是
luaL_dostring
,它执行字符串中的代码:

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

但是,您应该测试

luaL_loadstring
lua_dump
的返回值。


0
投票

编译器错误是由实现

frmDevelop::writer
的无效函数原型引起的。

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

您只需要static

限定符
class frmDevelop中的声明点
。另外,这里的 
extern "C"
 是不正确的,因为你告诉编译器不要破坏函数名称。但编译器无法满足该请求,因为您使用的是 C++ 功能(编写器是 
frmDevelop
 的一部分,因此您需要 
::
 作用域运算符)——
extern "C"
 只是被忽略为 
frmDevelop::writer

更改作者的实现:

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

int frmDevelop::writer(lua_State *L, const void *p, size_t size, void *u) { /* ... */ }
这将修复您遇到的编译错误。


0
投票
我正在用代码自行修复它

源文件(包含)

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

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 必须替换为您的文件名:D

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.