Lua调用C/C++函数
2016-07-18 21:00
483 查看
// // main.cpp // LuaAndCpp // #include <iostream> static int lua_getName(lua_State* L){ lua_pushstring(L, "string from c"); std::cout << "lua call c function \n" ; return 1; } static int showOne(lua_State* L){ const char* value = luaL_checkstring(L, -1); std::cout << value << std::endl; lua_pushstring(L, "get lua string"); return 1; } static int showTwo(lua_State* L){ const char* value1 = luaL_checkstring(L, -1); const char* value2 = luaL_checkstring(L, -2); std::cout << value1 << " " << value2 << std::endl; lua_pushstring(L, "test -1-"); return 1; } static int showTwo2(lua_State* L){ const char* value1 = luaL_checkstring(L, -1); const char* value2 = luaL_checkstring(L, -2); std::cout << value1 << " " << value2 << std::endl; lua_pushstring(L, "test -2-"); return 0; } static int backTwo(lua_State* L){ lua_pushstring(L, "--back--"); lua_pushnumber(L, 100); return 2; } static int backTable(lua_State* L){ lua_newtable(L); char str[20] = {0}; for (int i = 1 ; i<=10 ; i++) { lua_pushnumber(L, i); // key sprintf(str, "num is %i", i); lua_pushstring(L, str); // value lua_settable(L, -3); } return 1; } static int backTable2(lua_State* L){ lua_newtable(L); char str[20] = {0}; int loop = 1; while (loop <= 10) { sprintf(str, "num is %i",loop); lua_pushstring(L, str); lua_pushstring(L, "app"); loop++; lua_settable(L, -3); } return 1; } void register_my_functions(lua_State* L){ lua_pushcfunction(L, lua_getName); lua_setglobal(L, "lua_getName"); lua_pushcfunction(L, showOne); lua_setglobal(L, "showOne"); lua_pushcfunction(L, showTwo); lua_setglobal(L, "showTwo"); lua_pushcfunction(L, showTwo2); lua_setglobal(L, "showTwo2"); lua_pushcfunction(L, backTwo); lua_setglobal(L, "backTwo"); lua_pushcfunction(L, backTable); lua_setglobal(L, "backTable"); lua_pushcfunction(L, backTable2); lua_setglobal(L, "backTable2"); } int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, World!\n"; lua_State* m_luaState = luaL_newstate(); luaL_openlibs(m_luaState); register_my_functions(m_luaState); luaL_dofile(m_luaState, "/Users/Forest/Documents/LuaAndCpp/LuaAndCpp/scripts/config.lua"); lua_close(m_luaState); return 0; }
Lua中调用C/C++函数
print(lua_getName()) print(showOne('test string from lua')) print('--1--',showTwo('cocos','2dx')) print('--2--',showTwo2('2dx','cocos')) local x , y = backTwo() print(type(x),x,type(y),y) local t = backTable() for key , value in pairs(t) do print(key,value) end local t2 = backTable2() for k , v in pairs(t2) do print(k,v) end
运行结果:
lua call c function string from c test string from lua get lua string 2dx cocos --1-- test -1- cocos 2dx --2-- string --back-- number 100 1 num is 1 2 num is 2 3 num is 3 4 num is 4 5 num is 5 6 num is 6 7 num is 7 8 num is 8 9 num is 9 10 num is 10 num is 10 app num is 2 app num is 6 app num is 1 app num is 5 app num is 3 app num is 9 app num is 7 app num is 8 app num is 4 app
相关文章推荐
- 详解Lua中的表的概念及其相关操作方法
- Lua编程示例(二):面向对象、metatable对表进行扩展
- 把Lua编译进nginx步骤方法
- Lua脚本自动生成APK包
- Lua中的元表(metatable)、元方法(metamethod)详解
- Lua中的metatable介绍
- Lua中ipair和pair的区别
- Lua中的函数精讲笔记
- 浅谈Lua的面向对象特性
- 详解Lua中的变量相关知识点
- Lua脚本语言入门笔记
- Lua脚本调用外部脚本
- 详解Lua中的if语句的使用方法
- Lua中调用函数使用点号和冒号的区别
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中强大的元方法__index详解
- Lua中调用C++函数示例
- Lua面向对象之类和继承浅析
- Lua性能优化技巧(一):前言
- Lua中获取table长度问题探讨