C/C++中嵌入LUA脚本(C/C++与LUA相互交互调用)

/*

 *

 *

 * @author ada

 * @version 1.0

 * @since 1.0

 */

#include <stdio.h>

#include <string.h>

#include <log.h>

#include "../gs/LuaContext.hpp"

#include "../gs/LuaExecutor.hpp"

#include "../gs/LuaScriptExecutor.hpp"

/*

#include <lua.h>

#include <lualib.h>

#include <lauxlib.h>

*/

#include "../LinkedLibrary.h"

 

#define BUFSIZE 1024

 

LuaContext *luaContext;

char *strEventHandler = NULL;

 

 

/**

 * LuaGlue 函数原型

 * 

 *     int (__cdecl *)(struct lua_State *) 

 * 

 * 如果在C编译环境下,函数定义可以不严格遵循这种原型。

 */

int __lua_version(struct lua_State *L)

{

printf("int __lua_version(struct lua_State *L)\n");

 

puts(LUA_VERSION);

puts(LUA_COPYRIGHT);

puts(LUA_AUTHORS);

return 0;

}

 

int registerEvent(struct lua_State *L)

{

printf("registerEvent()\n");

return 0;

}

 

int registerEventHandler(struct lua_State *L) 

{

strEventHandler = (char *) luaContext->getStringArgument(1);

info("lua event handler %s registered\n", strEventHandler);

return 0;

}

 

void FireEvent(char* event, ...)

{

char callback[254];

sprintf(callback, "%s(\"%s\")", strEventHandler, event);

 

lua_State *luaHandle = luaContext->getLuaContext();

//lua_dostring(luaHandle, script);

luaL_dostring(luaHandle, callback);

}

 

int main(int argc, char *argv[])

{

luaContext = new LuaContext();

lua_State *L = NULL;

char line[BUFSIZE];

int len;

 

puts(LUA_VERSION);

puts(LUA_COPYRIGHT);

puts(LUA_AUTHORS);

 

/*

//luaContext = lua_open(); // #define lua_open()luaL_newstate()

L = luaL_newstate(); // 创建一个解释器句柄

 

luaL_openlibs(L); // 打开所有的Lua库

//info("lua runtime environment opened");

*/

L = luaContext->getLuaContext();

 

lua_register(L, "__lua_version", __lua_version);

luaContext->registerFunction("registerEvent", registerEvent);

luaContext->registerFunction("registerEventHandler", registerEventHandler);

 

 

printf("Type lua script from stdin terminal, and the script typed would be executed.\n");

printf("Usage: \n");

printf("\tType <Enter> to exit stdin\n");

while(fgets(line, sizeof(line), stdin) != 0) 

{

if (line[0] == '\n')

{

break;

}

len = strlen(line);

printf("lua script length: %d\n", len);

printf("executing %s\n", line);

 

//lua_dostring(L, line); 

//luaL_dostring(L, line);

LuaScriptExecutor et(luaContext, line, 0);

et.execute();

 

memset(line, 0, BUFSIZE);

}

printf("exit stdin terminal\n");

 

 

//luaL_dostring(L, "print(\"this is lua output\")");

LuaExecutor *executor = new LuaScriptExecutor(luaContext, "print(\"this is lua output\")", 0);

executor->execute();

delete executor;

 

printf("第一种方式:\n");

//luaL_loadfile(L, "test.lua"); // 调入Lua脚本文件

//luaContext->load("test.lua");

//lua_pcall(L, 0, 0, 0); //执行Lua脚本

    luaContext->execute("test.lua");

 

printf("第二种方式:\n");

//luaL_dofile(L, "test.lua");

luaContext->dexecute("test.lua");

 

printf("call lua function lua_version\n");

lua_getglobal(L, "lua_version");

//lua_pushnumber(m_pState, nParam1);

//lua_pushnumber(m_pState, nParam2);

lua_pcall(L, 0, 0, 0);

 

printf("call lua function lua_onEvent\n");

lua_getglobal(L, "lua_onEvent");

lua_pushstring(L, "Click");

lua_pcall(L, 1, 0, 0);

 

FireEvent("DoubleClick");

FireEvent("Move");

 

    delete luaContext;

    return 0;

}

 

============================================

 

运行结果:

 

[INFO] 2014-04-10 23:54:37 lua runtime environment opened

Lua 5.1

Copyright (C) 1994-2008 Lua.org, PUC-Rio

R. Ierusalimschy, L. H. de Figueiredo & W. Celes

[DEBUG] 2014-04-10 23:54:37 LuaGlue function registerEvent registered

[DEBUG] 2014-04-10 23:54:37 LuaGlue function registerEventHandler registered

Type lua script from stdin terminal, and the script typed would be executed.

Usage:

        Type <Enter> to exit stdin

 

exit stdin terminal

this is lua output

第一种方式:

[INFO] 2014-04-10 23:54:38 Loaded lua file test.lua

hello, world

hello, world

[LUA] this is lua runtime environment

register event handler lua_onEvent

[INFO] 2014-04-10 23:54:38 lua event handler lua_onEvent registered

 

[INFO] 2014-04-10 23:54:38 Executed lua file test.lua

第二种方式:

hello, world

hello, world

[LUA] this is lua runtime environment

register event handler lua_onEvent

[INFO] 2014-04-10 23:54:38 lua event handler lua_onEvent registered

 

[INFO] 2014-04-10 23:54:38 Executed lua file test.lua directly

call lua function lua_version

[LUA] this is lua runtime environment

call lua function lua_onEvent

on event        Click

Click

on event        DoubleClick

DoubleClick

on event        Move

Move

[INFO] 2014-04-10 23:54:38 lua runtime environment close

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值