当前位置: 首页 > wzjs >正文

这里是我做的网站链接平台

这里是我做的网站,链接平台,新世纪建设集团网站,网站建设维护推广合同一、核心交互机制:Lua 虚拟栈 Lua 与 C 的交互通过一个 ​​虚拟栈(Stack)​​ 完成,所有数据传递、函数调用均通过此栈实现。栈的每个元素可以是任意 Lua 类型(如数字、字符串、表、函数等)。 栈的结构与…

一、核心交互机制:Lua 虚拟栈

Lua 与 C 的交互通过一个 ​​虚拟栈(Stack)​​ 完成,所有数据传递、函数调用均通过此栈实现。栈的每个元素可以是任意 Lua 类型(如数字、字符串、表、函数等)。

栈的结构与索引​​:
​​正索引​​:从栈底(1)到栈顶(n)。
​​负索引​​:从栈顶(-1)到栈底(-n)。
​​动态扩容​​:当栈空间不足时,自动扩容(通常翻倍)。

栈操作示意图:

二、代码准备工作

定义C语言侧的基本代码:

#include<stdio.h>
#include<lua.h>
#include<lauxlib.h>
#include<lualib.h>int main()
{//创建Lua虚拟机lua_State* L = luaL_newstate();    /*opens lua*/luaopen_base(L);   /*opens the basic library*/luaopen_table(L);  /*opens the table library*/luaopen_io(L);     /*opens the I/O library*/luaopen_string(L); /*opens the string lib.*/luaopen_math(L);   /*opens the math lib.*//*************定义执行代码**************///lua_example_first(L);//lua_example_dofile(L);/***************************///清理Lua虚拟机lua_close(L);return 0;
}

三、Lua与C交互代码的使用:

1.获取lua侧一个全局变量:

/// <summary>
/// 得到一个全局变量
/// </summary>
/// <param name="L"></param>
void lua_example_getvar(lua_State* L) {luaL_dostring(L,"some_var = 500");lua_getglobal(L,"some_var");lua_Number some_var_in_c = lua_tonumber(L,-1);printf("some_var 在C侧的值是 %d\n",(int)some_var_in_c);
}

结果:

2.Lua栈 :

/// <summary>
/// Lua的栈操作
/// </summary>
/// <param name="L"></param>
void lua_example_stack(lua_State* L) {lua_settop(L, 0); // 清空栈,确保初始为空	lua_pushnumber(L,300);  //stack[1] 或者 stack[-3]lua_pushnumber(L,450);  //stack[2] 或者 stack[-2]lua_pushnumber(L,438);  //stack[3] 或者 stack[-1]lua_Number element;element = lua_tonumber(L,-1);printf("最后增加的元素,位置在栈的索引3处的值为 %d\n",(int)element);// lua_remove(L,2)执行之后栈变化过程:// 移除索引 2 后,栈中元素变为 [300, 438]// 索引 1: 300(原索引 1)// 索引 2: 438(原索引 3 下移填补空缺)lua_remove(L,2); //移除栈中索引为2的值438element = lua_tonumber(L,2);printf("当前栈索引为2处的值为 %d\n", (int)element);
}

结果:

 3.C调用Lua侧函数:

lua代码:

luafunction = function(a,b)return a+b;
end

c代码:

/// <summary>
/// C侧调用lua函数
/// </summary>
/// <param name="L"></param>
void lua_example_call_lua_function(lua_State* L) {luaL_dofile(L,"./scripts/luaFunction.lua");lua_getglobal(L,"luafunction");if (lua_isfunction(L, -1)) {lua_pushnumber(L, 5); //将第一个参数压栈lua_pushnumber(L, 6); //将第二个参数压栈const int NUM_ARGS = 2; //参数的数量const int NUM_RETURNS = 1; //返回值的数量lua_pcall(L,NUM_ARGS,NUM_RETURNS,0);lua_Number luaResult = lua_tonumber(L,-1);printf("执行求和的lua函数的结果为:%f\n",(float)luaResult);}
}

结果:

4.Lua调用C侧函数:

lua代码:

cfunction = function(a,b)result = native_cfunction(a,b);return result; 
end

c代码:

int native_cfunction(lua_State* L) {lua_Number b = lua_tonumber(L, -1); //得到第二个参数blua_Number a = lua_tonumber(L, -2); //得到第一个参数alua_Number result = a + b;lua_pushnumber(L, result);return 1;  //返回函数有多少个返回值作为结果返回虚拟栈中
}
/// <summary>
/// Lua侧调用C函数
/// </summary>
/// <param name="L"></param>
void lua_example_call_c_function(lua_State* L) {lua_pushcfunction(L, native_cfunction); // 将 C 函数压入栈顶lua_setglobal(L,"native_cfunction"); // 将栈顶的值(native_cfunction)设置为 Lua 的全局变量 native_cfunction。操作后,栈被清空:[]。luaL_dofile(L,"./scripts/cFunction.lua");lua_getglobal(L,"cfunction");if (lua_isfunction(L, -1)) {lua_pushnumber(L,3); //将3压栈,作为第一个参数lua_pushnumber(L,5); //将5压栈,作为第二个参数const int NUM_ARGS = 2; //参数的数量const int NUM_RETURNS = 1; //返回值的数量lua_pcall(L, NUM_ARGS, NUM_RETURNS, 0);lua_Number luaResult = lua_tonumber(L, -1);printf("最终计算的结果 cfunction(3,5) = %f\n", (float)luaResult);}
}

结果:

注:c侧函数的返回值含义如下:
return 0​​:不返回任何值(栈中无数据传递给 Lua)。
​​return 1​​:将栈顶的 1 个值作为返回值。
​​return N​​(N > 1):将栈顶的 N 个值依次作为返回值。 

5.检查处理错误:

/// <summary>
/// 检查处理错误
/// </summary>
/// <param name="L"></param>
void lua_example_check_handle_error(lua_State* L) {if (luaL_dostring(L, "some_var = 500...") != LUA_OK) {luaL_error(L,"Error: %s\n",lua_tostring(L,-1));}else {//执行正确逻辑}
}

结果: 

6.C侧发送与接收userData:

lua代码:

square = create_rectangle()
--将square,200,100依次压入栈中,
--[-1]100
--[-2]200
--[-3]square
change_rectangle_size(square,200,100) 

c代码:

typedef struct reacangle2d {int x;int y;int width;int height;
} rectangle;/// <summary>
/// C侧发送数据到lua
/// </summary>
/// <param name="L"></param>
/// <returns></returns>
int create_rectangle(lua_State* L) {rectangle* rect = (rectangle*)lua_newuserdata(L,sizeof(rectangle));rect->x = 0;rect->y = 0;rect->width = 0;rect->height = 0;return 1; //表示返回一个类型作为userdata可供lua使用
}int change_rectangle_size(lua_State* L) {rectangle* rect = (rectangle*)lua_touserdata(L,-3);rect->width = (int)lua_tonumber(L,-2);rect->height = (int)lua_tonumber(L,-1);return 0; //表示没有参数返回
}void lua_example_userdata(lua_State* L) {//设置发送函数lua_pushcfunction(L,create_rectangle);lua_setglobal(L,"create_rectangle");//设置接收函数lua_pushcfunction(L, change_rectangle_size);lua_setglobal(L, "change_rectangle_size");luaL_dofile(L, "./scripts/rectangle.lua");lua_getglobal(L,"square");if (lua_isuserdata(L, -1)) {rectangle* r = (rectangle*)lua_touserdata(L, -1);printf("从lua侧得到一个rectangle值,width: %d, height: %d ",r->width,r->height);}else {printf("从lua侧没有获取到rectangle的值");}
}

结果:

7.从C侧读取与写入Lua Table:

lua代码:

config_table = {window_width = 800,window_height = 600,num_enemies = 15,num_levels = 10
}

c代码:

void lua_example_table(lua_State* L) {lua_settop(L, 0); // 清空栈,确保初始为空if (luaL_dofile(L, "./scripts/configtable.lua") == LUA_OK) {lua_getglobal(L,"config_table");if (lua_istable(L, -1)) {lua_getfield(L,-1,"window_width");printf("在lua侧table中定义的 window_width的值为 %s\n",lua_tostring(L,-1));/*lua_getfield 将字段值压入栈顶后,原 table 的索引从 -1 变为 -2。此时 lua_setfield 的 table 索引参数 应为 -2,而非 -1。lua_setfield 需要将栈顶的值作为新字段值,所以需要提前将一个值压栈。*/// 方法 1:将 window_width 的值赋给 window_height//lua_setfield(L, -2, "window_height"); // 复用栈顶值// 方法 2:设置自定义值lua_pushnumber(L, 600);    //栈顶将600压栈之后table索引变为-3// 压入新值lua_setfield(L, -3, "window_height"); // 赋给 table//lua_setfield之后栈顶的 600 被弹出/*栈中的值分布如下:table索引从-3变为-2[-1]: window_width 的值[-2]: table*/lua_getfield(L, -2, "window_height");printf("在lua侧table中定义的 window_height的值为 %s\n", lua_tostring(L, -1));lua_pop(L, 1); // 清理栈顶的临时值}}else{luaL_error(L,"Error: %s\n",lua_tostring(L,-1));}
}

结果:

http://www.dtcms.com/wzjs/43355.html

相关文章:

  • 宁波网站建设开发公司专门做排名的软件
  • 高端网站设计元素图片逆冬seo
  • 怎么样做国外推广网站百度数据研究中心
  • 网站建设找哪个好外包seo公司
  • wordpress 主题 小说昆明seo案例
  • 中学生制作的网站知乎推广公司
  • wordpress 插件配置seo关键字优化软件
  • 顺德销售型网站建设江门网站定制多少钱
  • 微网站建站系统源码广州seo效果
  • 自己做网站怎么搜索广州线下教学
  • 同时优化几个网站推广普通话的内容
  • wordpress+整站下载百度云官网登录入口
  • 网站运营服务商网站推广什么意思
  • 动态网站建设方面的书域名买卖交易平台
  • 个人社团网站怎么做百度贴吧首页
  • 工程机械网站模板宁波seo网络推广咨询热线
  • 公司页面网站设计模板百度系app有哪些
  • 购物网站建设案件seo优化是啥
  • 合肥婚恋网站建设友情链接买卖代理
  • 成都直销网站开发网上如何推广产品
  • 园区网互联及网站建设项目网络推广工作好吗
  • python 做下载网站爱站网关键词工具
  • 福州建设企业网站淘宝怎么推广自己的产品
  • dreamweaver cs6新区seo整站优化公司
  • 网站建设基础摘要百度竞价ocpc投放策略
  • 网站怎么做兼容测试国内搜索引擎有哪些
  • 平台推广网站营销型网站分析
  • 网站点击深圳网站优化培训
  • 广州建设水务局网站百度收录
  • b2b 网站建设培训心得体会1000字通用