Lua 与C/C++ 交互系列:通过C调用Lua函数(1)

本文介绍了如何通过C代码调用Lua编写的全局函数,遵循Lua与C交互的协议规则,详细解析了lua_call函数的使用,包括参数nargs和nresults的作用。并提供了C代码调用Lua全局函数及Table中Field函数的示例。
摘要由CSDN通过智能技术生成

1、Lua通过ANSI C 进行编写,Lua与C交互要遵循一定的协议规则。在Lua 5.1 Reference Manual中明确规定如何通过C Code调用Lua 编写的函数。

     C  code可以调用Lua Code编写的任何函数.本文主要以全局函数来做演示。


2、首先把C语言调用Lua 函数的协议规则说明。Lua 5.1 Reference Manual 中关于void lua_call (lua_State *L, int nargs, int nresults); 函数的描述中详细说明了协议规则。



void lua_call (lua_State *L, int nargs, int nresults);

To call a function you must use the following protocol:

first, the function to be called is pushed onto the stack;

then, the arguments to the function are pushed in direct order;

that is, the first argument is pushed first.

Finally you call lua_call;

nargs is the number of arguments that you pushed onto the stack.

All arguments and the function value are popped from the stack when the function is called.

The function results are pushed onto the st

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值