标签:
function fnex2(str_a, num_b, num_c)
print(str_a);
return num_b*100 + num_c*10, "Thank you";
end;
//初始化LUA虚拟机
void InitLuaState(lua_State* L)
{
/* Load Libraries */
luaopen_base(L);
luaopen_table(L);
luaL_openlibs(L);
luaopen_string(L);
luaopen_math(L);
}
int call_lua_function(void)
{
const char* szInParam = "This is an [IN] parameter";
const int iParam1 = 20, iParam2 = 50;
cout << "=================================" << endl
<< "02_Call_Function" << endl
<< "=================================" << endl
<< "This demo calls functions in LUA scripts." << endl
<< "Argument 1:" << szInParam << endl
<< "Argument 2:" << iParam1 << endl
<< "Argument 3:" << iParam2 << endl
<< "---------------------------------" << endl
<< "#OUTPUTS#" << endl;
lua_State* L = lua_open();
InitLuaState(L);
int iError;
/* Load Script */
iError = luaL_loadfile(L, "../test02.lua");
if (iError)
{
cout << "Load script FAILED!"
<< lua_tostring(L, -1)
<< endl;
lua_close(L);
return 1;
}
/* Run Script */
iError = lua_pcall(L, 0, 0, 0);
if (iError)
{
cout << "pcall FAILED"
<< lua_tostring(L, -1)
<< iError
<< endl;
lua_close(L);
return 1; } /* Push a FUNCTION_VAR to STACK */ lua_getglobal(L, "fnex2"); /* Push PARAMETERS to STACK */ lua_pushstring(L, szInParam); lua_pushnumber(L, iParam1); lua_pushnumber(L, iParam2); /* Call FUNCTION in LUA */ iError = lua_pcall( L, //VMachine 3, //Argument Count 2, //Return Value Count 0 ); if (iError) { cout << "pcall FAILED" << lua_tostring(L, -1) << iError << endl; lua_close(L); } /* Check Return Value Types */ if (lua_isstring(L, -1) && lua_isnumber(L, -2)) { cout << "Ret_1(string): " << lua_tostring(L, -1) << endl; cout << "Rec_2(double): " << lua_tonumber(L, -2) << endl; } else { cout << "Wrong Return Values" << endl; } /* POP STACK */ lua_pop(L,2); //只需要清理Return Value,pcall调用的入栈参数会自动清理 lua_close(L); return 0;}
#define CallLuaFunc(FuncName, Params, Results)
{
lua_getglobal (g_pLuaState, FuncName);
lua_call (g_pLuaState, Params, Results);
}
print (">>> LUA程序开始运行了 ");
function fnex3(num_a, num_b)
local c = rmath(num_a, num_b);
print("LUA PRINTTING:", c);
return c;
end;
//LUA脚本调用C函数
int call_c_function(void)
{
int iArg1 = 3, iArg2 = 10, iError;
cout << "=================================" << endl
<< "下面的程序演示从LUA脚本中调用C函数" << endl
<< "Argument 1:" << iArg1 << endl
<< "Argument 2:" << iArg2 << endl
<< "---------------------------------" << endl
<< "#OUTPUTS#" << endl;
lua_State* L = lua_open();
InitLuaState(L);
iError = luaL_loadfile(L, "../test03.lua");
if (iError) cout << "载入脚本失败" << endl;
iError = lua_pcall(L, 0, 0, 0);
if (iError) cout << "执行LUA脚本失败" << endl;
/* 将C函数(指针)压栈 */
lua_pushstring(L, "rmath");
lua_pushcfunction(L, RMath_LUA);
lua_settable(L, LUA_GLOBALSINDEX);
/* LUA函数也是变量(指针),可以压入栈 */
lua_getglobal(L, "fnex3");
/* 将提供给LUA函数的参数入栈 */
lua_pushnumber(L, iArg1);
lua_pushnumber(L, iArg2);
/* 调用LUA函数(pcall函数会自动清除入栈的变量) */
int Error = lua_pcall( L, //虚拟机指针
2, //2个参数
1, //1个返回值
0 );
if (Error) cout << "pcall调用fnex3函数失败" << endl;
/* 检验返回值类型 */
if (lua_isnumber(L, -1))
{
cout << "有1个(double)返回值 = "
<< lua_tonumber(L, -1)
<< endl; } /* 将LUA函数返回值出栈 */ lua_pop(L, 1); lua_close(L); return 0;}//可供LUA调用的C函数原型int RMath_LUA(lua_State* L){ if (!lua_isnumber(L, 1)) { lua_pushstring(L, "Arg_1不是数字"); lua_error(L); } if (!lua_isnumber(L, 2)) { lua_pushstring(L, "Arg_2不是数字"); lua_error(L); } /* GET ARGUMENT FROM STACK */ double a = lua_tonumber(L, 1); double b = lua_tonumber(L, 2); /* PUSH RESULT TO STACK */ lua_pushnumber(L, a * b); /* COUNT OF RETURN VARS*/ return 1;}
/* Push a FUNCTION_VAR to STACK */
lua_getglobal(L, "fnex2");
/* Push PARAMETERS to STACK */
lua_pushstring(L, szInParam);
lua_pushnumber(L, iParam1);
lua_pushnumber(L, iParam2);
/* Call FUNCTION in LUA */
iError = lua_pcall( L,3,2,0);
lua_pushstring(L, "rmath");
lua_pushcfunction(L, RMath_LUA);
lua_settable(L, LUA_GLOBALSINDEX);
标签:
原文地址:http://www.cnblogs.com/freebird92/p/4661502.html