首页 > 其他分享 >Lua调用C的函数

Lua调用C的函数

时间:2024-07-12 15:11:05浏览次数:12  
标签:调用 函数 lua -- nArgs Lua include cout

#include <iostream>
#include <string>
using namespace std;

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

int myTest(lua_State* L)
{
    // 获得Lua函数的参数的数量
    int nArgs = lua_gettop(L);
    cout << "nArgs = " << nArgs << endl;
    cout << "==================================================" << endl;
    for(int i = 1; i <= nArgs; ++i) {
        const char* str_arg_n = lua_tostring(L,i);
        cout << "[" << i << "] = " << str_arg_n << endl;
    }
    cout << "==================================================" << endl;

    //cout << "==================================================" << endl;
    //for(int i = -1; i >= -nArgs; --i) {
    //    const char* str_arg_n = lua_tostring(L,i);
    //    cout << "[" << i << "] = " << str_arg_n << endl;
    //}
    //cout << "==================================================" << endl;
    return 0;
}

int main(int argc,char* argv[])
{
    cout << "===============" << endl;
    cout << "Hello Lua :)" << endl;
    cout << "===============" << endl << endl;

    // char anyKey = 0;
    lua_State* L = lua_open();
    if( L == nullptr) {
        cout << "init Lua Environment Failed!!! " << endl;
        cout << "Press any key to exit ... ";
        getchar();
        return -1;
    }

    
    luaL_openlibs(L);
    lua_pushcfunction(L,myTest);
    lua_setglobal(L,"myTest");

    int ret = luaL_dofile(L, "script/test.lua");
    if( ret != 0 ) {
        const char* error = lua_tostring(L,-1);
        cout << "========== Error occurred =========="<< endl;
        cout << "execute test.lua , Meet some error" << endl;
        cout << "Error : " << endl << "\t"  << error << endl;
        cout << "==================================" << endl;
        cout << "Press any Key to quit ... ";
        getchar();
    } else {
        cout << "execute test.lua Successfully :) " << endl;
        cout << "Press any Key to quit ... ";
        getchar();
        // cin.get(anyKey);
    }
    return 0;
}


--
-- script/test.lua
--
myTest(333, "Hello World", 456.7, "Google")

--[[
nArgs = 4
==================================================
[1] = 333
[2] = Hello World
[3] = 456.7
[4] = Google
==================================================
--]]

标签:调用,函数,lua,--,nArgs,Lua,include,cout
From: https://www.cnblogs.com/edisonewton/p/18298373

相关文章

  • CRC_CCITT js版的计算函数
    CRC_CCITT,特征多项式:X16+X12+X5+1,即多项式系数为0x1021,初始值为全0,对于单个字节来说最高位先计算,不需要取反直接输出。functioncalculateCRC_CCITT(buffer){constPOLYNOMIAL=0x1021;letcrc=0x0000;//Initialvaluesettoall0sforthisspecificcase......
  • 最全C语言个人笔记【第四章节-函数】
    函数入门在C语言中,函数意味着功能模块。一个典型的C语言程序,就是由一个个的功能模块拼接而成的整体。也因为如此,C语言被称为模块化语言。对于函数的使用者,可以简单的将函数理解为黑箱子,使用者只管按照规定给黑箱一些输入,就会得到一些输出,而不必要理会黑箱子里面的运行细节......
  • 从零开始学习嵌入式----C语言指针函数
    目录拨开迷雾:深入浅出C语言指针函数一、指针?函数?傻傻分不清楚二、指针函数闪亮登场三、抽丝剥茧:解析指针函数四、实例讲解:指针函数的应用五、总结拨开迷雾:深入浅出C语言指针函数    指针,一直是C语言学习路上的「拦路虎」,而指针函数更是让许多初学者望而却......
  • 从零开始学习嵌入式----C语言函数指针
    C语言的函数指针:从入门到深入理解    函数指针是C语言中一个强大且灵活的特性,它允许我们将函数作为参数传递给其他函数,或者将函数存储在数据结构中。正确理解和使用函数指针可以帮助我们编写更加模块化、高效和可扩展的代码。一、什么是函数指针?    在C......
  • 易优CMS文章付费设置与调用标签
    {eyou:articlepayid='field'}<!--内容显示--><div{$field.contentId}>{$field.content}</div><!--付费文档--><div{$field.displayId}><div><div>当前隐藏内容需要支付<span>{$eyou.field.users_price......
  • 帝国CMS网站提示信息函数1
    提示信息函数1语法:printerror($error,$gotourl,$ecms,$noautourl,$novar)说明:$error:提示信息内容,可以为语言包变量,也可以直接提示内容。$gotourl:提示信息转向链接地址$ecms:0为后台提示信息、1为前台提示信息、8为后台提示信息(alert方式)、9为前台提示信息(alert方式)$noautourl:0......
  • TS定义数组 ts声明函数
    ts定义数组//定义整数型数组constarr1:number[]=[]constarr1_1:Array<number>=[]//定义字符窜型数组constarr2:string[]=[]constarr2_1:Array<string>=[]//定义对象型数组constarr3:object[]=[]constarr3_1:Array<Object>......
  • 使用token调用apiserver
    一、前置条件和说明:1.已安装k3s,并完成kubeconfig配置2.k8s1.24之后的版本,创建service account时,不会自动创建secret,需要手工创建secret 二、步骤1.创建service accountapiVersion:v1kind:ServiceAccountmetadata:name:apiserver-sanamespace:kube-system......
  • 0168-过程调用
    环境Time2022-11-09WSL-Ubuntu22.04QEMU6.2.0NASM2.15.05前言说明参考:《x86汇编语言:从实模式到保护模式》李忠目标通过过程调用来向屏幕输出helloworld。过程调用过程调用有点类似跳转,不过调用后,还会回到原来的位置继续执行。过程调用时,首先将下一条指令入栈......
  • Lingo学习(三)——工厂合并、运算符、内置函数
    一、工厂合并(一)工厂合并——生产二维矩阵【引入】sets:                                factory/1..6/:a;                  plant/1..8/:d;                    Cooperation(fact......