call lua function from c and called back to c
Just a simple example:
--The c file:
#include <stdio.h>
#include "lua.h"
#include "luaconf.h"
#include "lualib.h"
#include "lauxlib.h"
#include "math.h"
static
int l_sin (lua_State *L) {
double d = lua_tonumber(L,
); /* get argument */
lua_pushnumber(L, sin(d));
/* push result */
return
;
/* number of results */
}
int main()
{
float pi =
3.1415926;
float pidiv6 = pi /
;
float pidiv4 = pi /
;
float rt =
;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_pushcfunction(L, l_sin);
//Lua know the address of l_sin in c context
lua_setglobal(L,
"mysin");
//map l_sin to mysin will be called in lua context
if ( !luaL_dofile(L,
"./cal.lua") ) {
printf("load cal.lua successful\n");
}
else {
printf());
;
}
lua_getglobal(L,
"lsin");
lua_pushnumber(L, pidiv4);
if ( !lua_pcall(L,
, , ) ) {
rt = lua_tonumber(L, -);
}
else {
printf());
;
}
printf("rt = %f\n", rt);
return
0;
}
--cal.lua
print("start...")
function lsin ( angle )
return mysin(angle)
end
print("end...")
==output==
start...
end...
load cal.lua successful
rt = 0.707107
//Actually a more simpler way is to call mysin directly without cal.lua
lua_getglobal(L, "mysin");
lua_pushnumber(L, pidiv4);
, , )
) {
rt = lua_tonumber(L, -);
} else {
printf());
;
}
call lua function from c and called back to c的更多相关文章
- lua function
This is the main reference for the World of Warcraft Lua Runtime. Note that these are mostly standar ...
- Lua function 函数
Lua支持面向对象,操作符为冒号‘:’.o:foo(x) <==> o.foo(o, x). Lua程序可以调用C语言或者Lua实现的函数.Lua基础库中的所有函数都是用C实现的.但这些细 ...
- First Lua function running in C
这是我在C里面跑出来的第一个Lua 文件, 纪念一下. 1.Set up envirnonment: Mac下面 Lua的src (即include) 和lib(binary)是分开的, 所以需要分别 ...
- lua执行字节码的过程介绍
前面一篇文章中介绍了lua给下面代码生成最终的字节码的整个过程,这次我们来看看lua vm执行这些字节码的过程. foo = "bar" local a, b = "a& ...
- lua解释执行脚本流程
#include "lua.hpp" #include <iostream> using namespace std; #pragma comment(lib, &qu ...
- Lua热更系统
1.介绍 使用脚本开发游戏业务逻辑其中一个好处就是代码可线上热更,不停机修复bug.而热更代码的写法与需要被热更的文件的代码又有着密切的关系,本文介绍一种热更方法. 2.热更原理 Lua提供一个叫re ...
- 《The Evolution of Lua》读书笔记 1
lua的优点: 可移植性 容易嵌入 体积小 高效率 这些优点都来自于lua的设计目标:简洁.从Scheme获得了很多灵感,包括匿名函数,合理的语义域概念 lua前身: 巴西被禁运,引入计算机软件和 ...
- 用VC编译lua源码,生成lua语言的解释器和编译器
用VC编译lua源码,生成lua语言的解释器和编译器 1.去网址下载源码 http://www.lua.org/download.html 2.装一个VC++,我用的是VC6.0 3.接下来我们开始编 ...
- Lua与C的交互
Lua 与 C 的交互 Lua是一个嵌入式的语言,它不仅可以是一个独立运行的程序,也可以是一个用来嵌入其它应用的程序库. C API是一个C代码与Lua进行交互的函数集,它由以下几部分构成: 1. ...
随机推荐
- Re:从零开始的Linux之路(文件权限)
基于 Red Hat Enterprise Linux 7.5 或者 CentOS 7.4 基本概念 Linux最核心的一个概念就是:Linux里面任何东西都可以被视为一个文件,包括系统本身(说到底L ...
- proto_ops和proto区别
**proto_ops:**该结构体包含了某个特定的"protocol family"的一系列functions. 其包含的functions和struct proto很类似,但是 ...
- Python 列表相关
python列表 列表推导式 例1 [ i*i for i in range(10) ] 打印如下: >>> [i*i for i in range(10)] [0, 1, 4, 9 ...
- invalid LOC header (bad signature)
[产生原因] 本地maven仓库相关jar存在问题. [解决方案] 删除本地maven相关jar并重新下载.
- PAT Basic 1070
1070 结绳 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连.每次串连后,原 ...
- Android自动化测试Uiautomator--UiSelector接口简介
UiSelector对象可以理解为一种条件对象,描述的是一种条件,可以配合UiObject使用得到某个符合条件的控件对象. 所有的方法都是public的,且都返回UiSelector类的对象. 文本方 ...
- IOS 自动布局-UIStackPanel和UIGridPanel(一)
我以前是做windows phone开发的,后来转做IOS的开发,因此很多windows phone上面的开发经验也被我带到了IOS中.其实有些经验本身跟平台无关,跟平台有关的无非就是实现方法而已.好 ...
- HDU 5514 Frogs
Frogs Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5514 ...
- [android开发篇] android apidemo 学习网址
http://www.2cto.com/special/ApiDemos/ApiDemos/type-160-10.html
- 【Luogu】P1005矩阵取数游戏(高精度+DP)
题目链接 yeah终于过辣! DP,f[i][j]表示每行还剩i到j这个区间的数没取的时候的值.借这个题我也把高精度的短板弥补了一下,以后高精加高精乘应该是没问题了. 哇终于不怂高精了…… 放上代码. ...