2011-01-26 15 views
5

皆さん、皆様!私はC++のアプリをスクリプトとして埋め込んでいます。非プログラマはLuaスクリプトを編集し、C++アプリケーションはLuaスクリプトを呼び出し、LuaスクリプトもC++登録関数を呼び出します。Safe LuaはC++登録関数を呼び出す

私はLuaplusを使って上記の仕事をしています。私の質問です:スクリプトエディタは、パラメータのスペルミスなどの間違いをすると、C + +のアプリクラッシュ!このような事態を防ぐために私は何ができますか?ありがとう

答えて

6

lua_cpcallとlua_pcallを見てください。両方とも、cでluaの保護された関数呼び出しを許可します。それらが負でない数値を返すと、呼び出しは失敗し、luaスタックにはエラー文字列だけが含まれます。 cpcallsの場合、スタックはそれ以外は変更されません。 pcallの場合は、関数を安全に呼び出すためにlua_pushcclosureを見る必要があります。

あなたがしているのは、loadfileとdofileなど、必要なlua_ *呼び出しのすべてを使ってc関数を作成することです。この関数は、lua_cpcallまたはlua_pushcclosure amd lua_pcallを使用して呼び出します。これにより、あなたがcpcallに渡した機能をエラー で検出したかどうかを検出することができます。

例:

function hello() { 
    string hello_ = "Hello Lua!"; 
    struct C { 
    static int call(lua_State* L) { 
     C *p = static_cast<C*>(lua_touserdata(L,-1)); 
     lua_pushstring(L, p->str.c_str()); 
     lua_getglobal(L, "print"); 
     lua_call(L, 1, 0); //ok 
     lua_pushstring(L, p->str.c_str()); 
     lua_getglobal(L, "notprint"); 
     lua_call(L, 1, 0); //error -> longjmps 
     return 0; //Number of values on stack to 'return' to lua 
    } 
    const string& str; 
    } p = { hello_ }; 
    //protected call of C::call() above 
    //with &p as 1st/only element on Lua stack 
    //any errors encountered will trigger a longjmp out of lua and 
    //return a non-0 error code and a string on the stack 
    //A return of 0 indicates success and the stack is unmodified 
    //to invoke LUA functions safely use the lua_pcall function 
    int res = lua_cpcall(L, &C::call, &p); 
    if(res) { 
    string err = lua_tostring(L, -1); 
    lua_pop(L, 1); 
    //Error hanlder here 
    } 
    //load a .lua file 
    if((res=luaL_loadfile(L, "myLuaFile.lua"))) { 
    string err = lua_tostring(L, -1); 
    lua_pop(L, 1); 
    //res is one of 
    //LUA_ERRSYNTAX - Lua syntax error 
    //LUA_ERRMEM - Out of memory error 
    //LUE_ERRFILE - File not found/accessible error 
    } 
    //execute it 
    if((res=lua_pcall(L,0,0,0))) { 
    string err = lua_tostring(L, -1); 
    lua_pop(L, 1); 
    // res is one of 
    // LUA_ERRRUN: a runtime error. 
    // LUA_ERRMEM: memory allocation error. 
    // LUA_ERRERR: error while running the error handler function (NULL in this case). 
    } 
    // try to call [a_int,b_str] = Foo(1,2,"3") 
    lua_getglobal(L,"Foo"); 
    if(lua_isfunction(L,lua_gettop(L))) { //Foo exists 
    lua_pushnumber(L,1); 
    lua_pushnumber(L,2); 
    lua_pushstring(L,"3"); 
    lua_pushvalue(L, -4); //copy of foo() 

    if((res = lua_pcall(L, 3, 2, 0/*default error func*/))) { 
     string err = lua_tostring(L, -1); 
     lua_pop(L, 1); 
     //error: see above 
    } 
    int a_int = (int)lua_tointeger(L,-2); 
    string b_str = lua_tostring(L,-1); 
    lua_pop(L,2+1); //2 returns, + extra copy of Foo() 
    } 
} 
関連する問題