美文网首页程序员
C/C++调用Lua接口封装技巧

C/C++调用Lua接口封装技巧

作者: ruilin_zn | 来源:发表于2019-01-16 15:14 被阅读0次

作为一个轻量级、高性能的脚本语言,Lua绝对是C/C++首选的脚本语言。但由于为了保证语言层面的灵活性,C/C++调用Lua传递参数是基于堆栈实现的,使得调用过程比较繁琐,这给C/C++开发者带来比较大的维护成本。

本文分享一种我个人在实践中掌握的一种封装技巧——实现调用Lua函数像调用本地函数一样简单的调用方法。

原文章地址

常规调用流程

double test(lua_State * L, double x, double y) {
    double z;
    lua_getglobal(L, "f");    // 获取lua函数f
    lua_pushnumber(L, x);    // 压入参数x和y
    lua_pushnumber(L, y);

    if(lua_pcall(L, 2, 1, 0) != 0)
        LOG(LERROR, ("error running function 'f': ", lua_tostring(L, -1)));

    if(!lua_isnumber(L, -1))
        LOG(LERROR, ("function 'f' must return a number"));
    z = lua_tonumber(L, -1);
    lua_pop(L, 1);
    return z;
}

正常的调用流程,每一个Lua接口都需要编写以上代码来实现与C/C++的对接,一旦接口多起来,就会难以维护。

改进方案


限制返回参数类型

为使接口更加简化,我们限制Lua函数返回类型只能有一个,对不同的返回类型分别封装不同的接口:
(对于返回复杂的数据类型,建议采用Json传递)

string callLuaFuncStr();
double callLuaFuncNum();

接口封装

使用C/C++可变参数,模仿print()的方式传参,保持Lua传参的灵活性。同时需要传入Lua函数名,最终的接口封装如下:

string callLuaFuncStr(const string funcName, const string argsFormat, ...);
double callLuaFuncNum(const string funcName, const string argsFormat, ...);

可变参数的实现

通过 setLuaFuncArgs() 对传入的数据类型进行解析,并分别传递给Lua:

uint32_t LuaHelper::setLuaFuncArgs(const string argsFormat, va_list args) {
    uint32_t count = 0;
    const char *format = argsFormat.c_str();
    while (*format != '\0') {
        char type = *format;
        if (type == '%' && *(format + 1) != '\0') {
            type = *(format + 1);
            switch (type) {
            case 's': {
                const char *arg = va_arg(args, const char *);
                lua_pushstring(L, arg);
                break;
            }
            case 'd': {
                int arg = va_arg(args, int);
                lua_pushinteger(L, arg);
                break;
            }
            case 'f': {
                float arg = va_arg(args, double);
                lua_pushnumber(L, arg);
                break;
            }
            default:
                LOG(LERROR, ("type undefined!!!"));
                format++;
                continue;
            }
            count++;
        }
        format++;
    }
    return count;
}

通过函数名调用Lua接口:

// 返回String
string LuaHelper::callLuaFuncStr(const string funcName, const string argsFormat, ...) {
    lua_getglobal(L, funcName.c_str());

    va_list args;
    va_start(args, argsFormat);
    uint32_t count = setLuaFuncArgs(argsFormat, args);
    va_end(args);

    if(lua_pcall(L, count, 1, 0) != 0)
        LOG(LERROR, ("error running function '", funcName ,"': ", lua_tostring(L, -1)));

    if(!lua_isstring(L, -1))
        LOG(LERROR, ("function '", funcName ,"' must return a string"));

    string result = lua_tostring(L, -1);
    lua_pop(L, 1);
    return result;
}

// 返回double
double LuaHelper::callLuaFuncNum(const string funcName, const string argsFormat, ...) {
    lua_getglobal(L, funcName.c_str());

    va_list args;
    va_start(args, argsFormat);
    uint32_t count = setLuaFuncArgs(argsFormat, args);
    va_end(args);

    if(lua_pcall(L, count, 1, 0) != 0)
        LOG(LERROR, ("error running function '", funcName ,"': ", lua_tostring(L, -1)));

    if(!lua_isnumber(L, -1))
        LOG(LERROR, ("function '", funcName ,"' must return a number"));

    double result = lua_tonumber(L, -1);
    lua_pop(L, 1);
    return result;
}

使用示例


Lua:

function testLua(speak)
    print(speak)
    return "Hello Cpp!!"
end

C++:

string result = callLuaFuncStr("testLua", "%s", "Hello Lua!!");

参考
[1][https://blog.csdn.net/jackystudio/article/details/17523523]
[2][https://blog.csdn.net/arnozhang12/article/details/6848678]

相关文章

  • C++调用lua方式

    目标 使用C++调用lua接口 示例 lua代码(test.lua) C++调用示例(lua_test.cpp) ...

  • C/C++调用Lua接口封装技巧

    作为一个轻量级、高性能的脚本语言,Lua绝对是C/C++首选的脚本语言。但由于为了保证语言层面的灵活性,C/C++...

  • Lua绑定流程

    绑定是为了实现将C++代码注册到lua环境,使得lua可以调用C++函数。https://blog.csdn.ne...

  • 对Lua ,C,C#互相调用的理解

    几种情况讨论 C调用Lua C调用C# C#调用C C#调用Lua Lua调用C Lua调用C# Lua调用C 本...

  • Go调用C/C++

    cgo golang是类C的语言 支持调用C接口(不支持调用C++)Go调用C/C++的方式 : C : 直接调用...

  • 各种加密算法编程演示实验

    实验目的 掌握各种对称算法接口的调用方法(C/C++方式) 掌握各种非对称算法接口的调用方法(C/C++方式) 掌...

  • C++与Objective-C类型转换

    最近接手的项目是C++和OC混编的,核心业务调用的是C++静态库提供的接口。为了方便项目使用,专门用OC封装了C+...

  • 三、Lua调用C++函数

    上一篇文章中我们已经知道了,C++怎么调用Lua中的函数,接下来我们学习一下,Lua怎么调用C++中的函数。 这篇...

  • Cocos2d-x Quick-3.3 常用API方法

    功能模块 functions 对Lua标准库扩展的函数库 coco2dx 对cocos2d-x c++接口的封装和...

  • lua调用c++中的函数(使用LuaBridge)

    前面一节简述描写了如何在c++中调用lua函数,这节简述描写如何在lua中调用c++中的函数,还是使用前一节的工程...

网友评论

    本文标题:C/C++调用Lua接口封装技巧

    本文链接:https://www.haomeiwen.com/subject/ieemdqtx.html