简体   繁体   English

C ++使用loadbuffer和runbuffer链接并运行LuaJit编译文件

[英]C++ Linking and running LuaJit compiled files with loadbuffer and runbuffer

I have compiled test.lua with LuaJit into test.obj and test.h. 我已经用LuaJit将test.lua编译为test.obj和test.h。 How do I correctly use the loadBuffer or runBuffer functions that I have? 如何正确使用我拥有的loadBuffer或runBuffer函数? All I need to find out is basically how to place test.lua, test.obj and test.h into the command but I just cant, Ive tried hundreds of ways but nothing seems to work. 我需要找出的基本上是如何将test.lua,test.obj和test.h放入命令中,但是我只是做不到,我尝试了数百种方法,但似乎无济于事。 I have stripped some other functions off from main and so forth that it would just leave the problem visible and not other things that work just fine. 我已经从main中删除了其他一些功能,以此类推,这只会使问题可见,而其他无法正常工作的东西。

C++: This is the main C ++:这是主要的

    int main(int argc, const char* argv[])
    {
        std::vector<std::string> args(argv, argv + argc);

        g_lua.loadBuffer("test.lua", "test.obj")
    // I have tried both, runBuffer and loadBuffer but I just cant get it right, it always fails.


    }

Here is the loadBuffer function: 这是loadBuffer函数:

void LuaCodes::loadBuffer(const std::string& buffer, const std::string& source)
{
    int ret = luaL_loadbuffer(L, buffer.c_str(), buffer.length(), source.c_str());
    if(ret != 0)
        throw LuaException(popString(), 0);
}

Here is the runBuffer function: 这是runBuffer函数:

    void LuaCodes::runBuffer(const std::string& buffer, const std::string& source)
    {
        loadBuffer(buffer, source);
        safeCall(0, 0);
    }

Here are the insides of test.h: 这是test.h的内部内容:

#define luaJIT_BC_test_SIZE 1186
static const char luaJIT_BC_test[] = {
27,76,74,1,2,154,9,2,0,12,0,47,0,151,1,52,0,0,0,55,0,1,0,52,1,2,0,55,1,3,1,62,
1,1,2,52,2,4,0,55,2,5,2,62,2,1,2,37,3,6,0,36,1,3,1,62,0,2,1,52,0,0,0,55,0,7,0,
52,1,8,0,55,1,9,1,37,2,10,0,62,1,2,0,61,0,0,1,52,0,0,0,55,0,7,0,52,1,4,0,55,1,
11,1,62,1,1,2,37,2,12,0,52,3,4,0,55,3,13,3,62,3,1,2,37,4,14,0,52,5,4,0,55,5,
15,5,62,5,1,2,37,6,16,0,52,7,4,0,55,7,17,7,62,7,1,2,37,8,18,0,52,9,4,0,55,9,
19,9,62,9,1,2,37,10,20,0,52,11,4,0,55,11,21,11,62,11,1,2,36,1,11,1,62,0,2,1,
52,0,2,0,55,0,22,0,52,1,2,0,55,1,3,1,62,1,1,2,37,2,23,0,36,1,2,1,41,2,2,0,62,
0,3,2,14,0,0,0,84,0,4,128,52,0,0,0,55,0,24,0,37,1,25,0,62,0,2,1,52,0,2,0,55,0,
22,0,52,1,2,0,55,1,3,1,62,1,1,2,37,2,26,0,36,1,2,1,41,2,2,0,62,0,3,2,14,0,0,0,
84,0,4,128,52,0,0,0,55,0,24,0,37,1,27,0,62,0,2,1,52,0,2,0,55,0,22,0,52,1,2,0,
55,1,3,1,62,1,1,2,37,2,28,0,36,1,2,1,41,2,2,0,62,0,3,1,52,0,2,0,55,0,29,0,52,
1,4,0,55,1,5,1,62,1,1,0,61,0,0,1,52,0,2,0,55,0,30,0,37,1,31,0,37,2,32,0,41,3,
2,0,62,0,4,1,52,0,33,0,55,0,34,0,37,1,35,0,62,0,2,1,52,0,36,0,55,0,37,0,62,0,
1,1,52,0,36,0,55,0,38,0,39,1,99,0,62,0,2,1,52,0,36,0,55,0,39,0,37,1,40,0,62,0,
2,1,52,0,36,0,55,0,39,0,37,1,41,0,62,0,2,1,52,0,36,0,55,0,38,0,39,1,243,1,62,
0,2,1,52,0,36,0,55,0,39,0,37,1,42,0,62,0,2,1,52,0,36,0,55,0,38,0,39,1,231,3,
62,0,2,1,52,0,36,0,55,0,39,0,37,1,43,0,62,0,2,1,52,0,36,0,55,0,38,0,39,1,15,
39,62,0,2,1,37,0,31,0,52,1,4,0,55,1,5,1,62,1,1,2,37,2,44,0,36,0,2,0,52,1,2,0,
55,1,45,1,16,2,0,0,62,1,2,2,15,0,1,0,84,2,3,128,52,1,46,0,16,2,0,0,62,1,2,1,
71,0,1,0,11,100,111,102,105,108,101,15,102,105,108,101,69,120,105,115,116,115,
7,114,99,19,103,97,109,101,95,105,110,116,101,114,102,97,99,101,11,99,108,105,
101,110,116,12,103,97,109,101,108,105,98,12,99,111,114,101,108,105,98,23,101,
110,115,117,114,101,77,111,100,117,108,101,76,111,97,100,101,100,20,97,117,
116,111,76,111,97,100,77,111,100,117,108,101,115,20,100,105,115,99,111,118,
101,114,77,111,100,117,108,101,115,14,103,95,109,111,100,117,108,101,115,17,
47,99,111,110,102,105,103,46,111,116,109,108,9,108,111,97,100,14,103,95,99,
111,110,102,105,103,115,11,46,111,116,112,107,103,6,47,25,115,101,97,114,99,
104,65,110,100,65,100,100,80,97,99,107,97,103,101,115,22,115,101,116,117,112,
85,115,101,114,87,114,105,116,101,68,105,114,9,109,111,100,115,56,85,110,97,
98,108,101,32,116,111,32,97,100,100,32,109,111,100,117,108,101,115,32,100,105,
114,101,99,116,111,114,121,32,116,111,32,116,104,101,32,115,101,97,114,99,104,
32,112,97,116,104,46,12,109,111,100,117,108,101,115,53,85,110,97,98,108,101,
32,116,111,32,97,100,100,32,100,97,116,97,32,100,105,114,101,99,116,111,114,
121,32,116,111,32,116,104,101,32,115,101,97,114,99,104,32,112,97,116,104,46,
10,102,97,116,97,108,9,100,97,116,97,18,97,100,100,83,101,97,114,99,104,80,97,
116,104,17,103,101,116,66,117,105,108,100,65,114,99,104,15,32,102,111,114,32,
97,114,99,104,32,17,103,101,116,66,117,105,108,100,68,97,116,101,16,41,32,98,
117,105,108,116,32,111,110,32,19,103,101,116,66,117,105,108,100,67,111,109,
109,105,116,7,32,40,21,103,101,116,66,117,105,108,100,82,101,118,105,115,105,
111,110,10,32,114,101,118,32,15,103,101,116,86,101,114,115,105,111,110,6,32,
12,103,101,116,78,97,109,101,42,61,61,32,97,112,112,108,105,99,97,116,105,111,
110,32,115,116,97,114,116,101,100,32,97,116,32,37,98,32,37,100,32,37,89,32,37,
88,9,100,97,116,101,7,111,115,9,105,110,102,111,9,46,108,111,103,19,103,101,
116,67,111,109,112,97,99,116,78,97,109,101,10,103,95,97,112,112,15,103,101,
116,87,111,114,107,68,105,114,16,103,95,114,101,115,111,117,114,99,101,115,15,
115,101,116,76,111,103,70,105,108,101,13,103,95,108,111,103,103,101,114,0
};

For luaL_loadbuffer (and hence LuaCodes::loadBuffer ) the 1st argument should be a string containing the bytecode and the 2nd argument should be a human-readable name (eg the filename that the bytecode was compiled from.) 对于luaL_loadbuffer (以及LuaCodes::loadBuffer ),第一个参数应为包含字节码的字符串,第二个参数应为人类可读的名称(例如,编译字节码的文件名。)

Try: 尝试:

#include "test.h"

// ...

int main(int argc, const char* argv[])
{
    // ...

    std::string bytecode(luaJIT_BC_test, luaJIT_BC_test_SIZE);
    g_lua.loadBuffer(bytecode, "@test.lua")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM