使用下面方式可保存lua任何值,目前只实现fucntion的保存,且无参数。如果需要实现参数,可自己扩展:

可实现下面方式:

CFun( lua_fun ) -- ok
CFun( function() print("Hello world") end ) --ok
local xxx = function print("Hello world" ) end
CFun( xxx ) --ok

lua_value.h

#ifndef __LUA_VALUE_H__
#define __LUA_VALUE_H__

extern "C" {
#include "lualib.h"
#include "lauxlib.h"
}

class LuaFunction
{
public:
    LuaFunction(lua_State* L):m_L(L), m_ref(0){};
    LuaFunction(lua_State* L, int index);
    ~LuaFunction();

void registerValue();
    void push()const
    {
        lua_rawgeti(m_L,LUA_REGISTRYINDEX,m_ref);
    }

public:
    int Call();
    bool Validate();
protected:
    lua_State* m_L;
    int m_ref;
};
#endif

lua_value.cpp

#include "lua_value.h"

LuaFunction::LuaFunction( lua_State* L, int index ): m_L(L)
{
    m_ref = lua_ref( m_L, index );
}

LuaFunction::~LuaFunction()
{
    if (m_L && m_ref != 0)
    {
        luaL_unref(m_L,LUA_REGISTRYINDEX,m_ref);
    }
}

bool LuaFunction::Validate()
{
    if (!m_L)
    {
        return false;
    }
    push();
    bool v = lua_isfunction(m_L,-1);
    lua_pop(m_L,1);
    return v;
}

int LuaFunction::Call()
{
    if (!Validate())
    {
        return -1;
    }
    int status = -1;
    push();
    if( lua_isfunction( m_L, -1 ) )
    {
        status = lua_pcall(m_L, 0, 1,NULL);
    }
    lua_pop(m_L, 1);
    return status;
}

void LuaFunction::registerValue()
{
    m_ref = lua_ref( m_L, 2 );  //2为参数索引号,如果调用函数中包括其他参数,需要根据实际需要修改
}

lua测试代码:

valueTest=nil

for i=1,5,1 do
    function showidx()

print(i)
        return i;
    end
    if i== 3 then
        valueTest = LuaFunction:new()
        valueTest:registerValue(showidx)
    end
end

valueTest:Call()

lua封装代码可用tolua实现,需要注意regisgerValue需要修改一下:

static int tolua_lua_value_LuaFunction_registerValue00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
 tolua_Error tolua_err;
 if (
     !tolua_isusertype(tolua_S,1,"LuaFunction",0,&tolua_err)
 )
  goto tolua_lerror;
 else
#endif
 {
  LuaFunction* self = (LuaFunction*)  tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
  if (!self) tolua_error(tolua_S,"invalid 'self' in function 'registerValue'",NULL);
#endif
  {
   self->registerValue();
  }
 }
 return 0;
#ifndef TOLUA_RELEASE
 tolua_lerror:
 tolua_error(tolua_S,"#ferror in function 'registerValue'.",&tolua_err);
 return 0;
#endif
}

最新文章

  1. jQuery 中的 39 个技巧
  2. U盘容量减少的解决办法
  3. 自建数据源(RSO2)、及数据源增强
  4. Abstract Algebra chapter 7
  5. FlashFXP命令行
  6. apache 开启服务器包含(SSI)技术
  7. Android调用相机并将照片存储到sd卡上
  8. SpringInAction读书笔记--第2章装配Bean
  9. DELPHI语法基础学习笔记-Windows 句柄、回调函数、函数重载等(Delphi中很少需要直接使用句柄,因为句柄藏在窗体、 位图及其他Delphi 对象的内部)
  10. 弹框modal, 获取id与绑定id
  11. jdk源码阅读笔记-String
  12. (简单)华为M3揽月 BTV-W09的Usb调试模式在哪里开启的经验
  13. Linux 查找文件命令 find whereis locate
  14. [20180423]flashback tablespace与snapshot standby.txt
  15. 重拾 BFC、IFC、GFC、FFC
  16. 一主多从+Binlog Server,主库故障无法访问,如何在从库中选举一个新主库
  17. ubuntu-14.04.2-desktop-i386.iso:ubuntu-14.04.2-desktop-i386:安装Oracle11gR2
  18. 使用rviz 查看远程主机
  19. Linux driver 板级文件跟踪一般方法
  20. 用phonegap和jquery-mobile写android应用

热门文章

  1. C#操作IE
  2. [R语言画图]气泡图symbols
  3. html5lib-python doc
  4. 常用LINUX脚本汇总(1)
  5. [汇编学习笔记][第十章 CALL和RET指令]
  6. SSH连接LINUX乱码解决方法
  7. Webfrom 上传 单个上传 多个上传
  8. YUI的UA检测
  9. 【Ecstore2.0】第三方信任登陆问题解决_备忘
  10. Cows(poj 2481 树状数组)