首页 > 其他分享 >[十万个为什么] 添加lua交互

[十万个为什么] 添加lua交互

时间:2024-07-16 15:29:35浏览次数:9  
标签:return 十万个 LUA int lua tostring printf 交互


#include "util_lua.h"

// 调试
// ---------------------------------------------------------------------------
static int traceback(lua_State* L)
{
    const char* msg = lua_tostring(L, 1);
    if (msg == NULL)
    {
        if (luaL_callmeta(L, 1, "__tostring") && lua_type(L, -1) == LUA_TSTRING)
            return 1;
        else
            msg = lua_pushfstring(L, "(error object is a %s value)", luaL_typename(L, 1));
    }
    luaL_traceback(L, L, msg, 1);
    return 1;
}

// 调用lua函数
// ---------------------------------------------------------------------------
static int call(lua_State *L, int n, int r)
{
	int err = lua_pcall(L, n, r, 1);

	switch (err)
	{
		case LUA_OK:
			break;

        case LUA_YIELD:
            printf("!LUA_YIELD : %s\n", lua_tostring(L, -1));
            break;

		case LUA_ERRRUN:
			printf("!LUA_ERRRUN : %s\n", lua_tostring(L, -1));
			break;

		case LUA_ERRSYNTAX:
			printf("!LUA_ERRSYNTAX : %s\n", lua_tostring(L, -1));
			break;

		case LUA_ERRMEM:
			printf("!LUA_ERRMEM : %s\n", lua_tostring(L, -1));
			break;

		case LUA_ERRERR:
			printf("!LUA_ERRERR : %s\n", lua_tostring(L, -1));
			break;

		default:
			printf("!Unknown Lua error: %d\n", err);
			break;
	}
	return err;
}


// ---------------------------------------------------------------------------
void lua_load_script(lua_State* L, const char* script_path)
{
    // 加载脚本和执行脚本
    lua_settop(L, 0);
    int status = luaL_loadfile(L, script_path);
    
    if (status == LUA_OK)
    {
        status = call(L, 0, 1);
    }

    if (status != LUA_OK || !lua_istable(L, -1))
    {
        lua_newtable(L);
    }
    lua_setfield(L, LUA_REGISTRYINDEX, "fw");
    
    // 添加trace
    lua_settop(L, 0);
    lua_pushcfunction(L, traceback);
    lua_getfield(L, LUA_REGISTRYINDEX, "fw");
}

// 无参调用
// ---------------------------------------------------------------------------
bool lua_call_vv(lua_State* L, const char* func)
{
    lua_getfield(L, 2, func);
    
    if (!lua_isfunction(L, -1))
    {
        printf("not found function export.%s\n", func);
        lua_settop(L, 2);
        return false;
    }

	call(L, 0, 0);
	
    lua_settop(L, 2);

    return true;
}

// ---------------------------------------------------------------------------
bool lua_call_vi2(lua_State* L, const char* func, int i1, int i2)
{
    lua_getfield(L, 2, func);
    
    if (!lua_isfunction(L, -1))
    {
        printf("not found function export.%s\n", func);
        lua_settop(L, 2);
        return false;
    }

    lua_pushinteger(L, i1);
    lua_pushinteger(L, i2);
	call(L, 2, 0);
	
    lua_settop(L, 2);

    return true;
}

 

标签:return,十万个,LUA,int,lua,tostring,printf,交互
From: https://www.cnblogs.com/kehuadong/p/18305332

相关文章

  • Auto-GPT Command evaluate_code returned: Error: The model: `gpt-4` does not exis
    题意:Auto-GPT命令evaluate_code返回:错误:模型 gpt-4 不存在。问题背景:I'mworkingwith auto-gpt andIgotthiserror:Commandevaluate_codereturned:Error:Themodel:`gpt-4`doesnotexistandit'slikeitcan'tgofurthermore.whatshouldIdo?......
  • VTK-自定义交互器、可拖拽坐标轴、视图定向立方体
    源代码:https://github.com/qianqiu10000/mySWInteractorStyle1.0.git仿照SolidWorks的操作习惯自定义的VTK交互器:1.左键单击Actor,可以选择Actor,并显示红色2.左键双击Actor,可以在Actor位置弹出拖拽坐标轴,可以移动、旋转3.单击空格键,可以弹出立方体视图定向工具4.按住鼠标......
  • R语言中交互式图表绘制
    revenue<-read.csv("data/revenue.csv")数据集放在了文章末尾,需要自取。if(!require(plotly))install.packages("plotly")#绘制柱状图p<-plot_ly(revenue,y=~本周,x=~游戏名称,type="bar",name="本周")pp%>%add_trace(y=~上周,na......
  • 国产渲染引擎ssRender(Lua+LuaPanda调试篇)
        秉承着说多了都是故事的理念,直接上干货。    今天给大家带来的是一篇关于Lua+Vscode+LuaPanda的远程调试篇,或许对你有一些启发。    资源给大家放在链接里:LuaPanda+LuaSocketDebug资源文件http://[email protected]:hwYang1995/ssRender_Lua_Debug_......
  • 用Vue3和Plotly.js实现3D小提琴图的交互式可视化
    本文由ScriptEcho平台提供技术支持项目地址:传送门小提琴图:绘制性别账单分布应用场景小提琴图是一种数据可视化工具,用于比较不同组别的分布。它结合了箱线图和核密度估计,可以直观地展示数据的中心趋势、离散度和分布形状。小提琴图常用于比较不同性别、年龄组或其他类别......
  • 使用 Vue 和 Plotly.js 创建交互式 3D 网格图
    本文由ScriptEcho平台提供技术支持项目地址:传送门使用Vue和Plotly.js创建交互式3D网格图应用场景介绍3D网格图是一种强大的可视化工具,可用于表示具有三个维度的数据。它们广泛应用于科学、工程和医学等领域,用于显示复杂数据并揭示潜在模式。代码基本功能介绍......
  • Lua 中的可变长函数
    可变长函数Lua中的可变长函数的参数用...来表示(3个.)在函数内部有一个特殊的内置变量arg其格式如下arg={1,"Hello",true,n=3}--functionmakeVarStr(...)toseeprint_Table.luafunctionprintMultiArg(...) print("...="..makeVarStr(arg))end......
  • 简单理解Lua 协程(coroutine)
    也许更好的阅读体验协程简单理解为可以暂停的线程,但是同一时刻只有一个协程可以处于运行状态。coroutine.create()lua中使用coroutine.create()创建一个协程,参数是一个函数,返回值为创建的协程,这个协程运行内容就是这个函数了。协程有三种状态挂起、运行、停止。协程刚创建时......
  • Lua调用C的函数
    #include<iostream>#include<string>usingnamespacestd;extern"C"{#include"lua.h"#include"lualib.h"#include"lauxlib.h"}intmyTest(lua_State*L){//获得Lua函数的参数的数量intn......
  • MES 与 PLC 的几种交互方式
       在MES开发领域,想要从PLC获取数据就必须要和PLC有信号交互。高效准确的获取PLC数据一直是优秀MES系统开发的目标之一。初涉相关系统开发的工程师往往不能很好的理解PLC和MES之间编程逻辑的本质差别,在设计交互逻辑是难免顾此失彼。因此本文结合本人这些年来和......