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,
1);  /* get argument */

lua_pushnumber(L, sin(d)); 
/* push result */

return
1; 
/* number of results */

}

int main()

{

float pi =
3.1415926;

float pidiv6 = pi /
6;

float pidiv4 = pi /
4;

float rt =
0;

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("error load file: %s\n", lua_tostring(L, -1));

return -1;

}

lua_getglobal(L,
"lsin");

lua_pushnumber(L, pidiv4);

if ( !lua_pcall(L,
1, 1,
0) ) {

rt = lua_tonumber(L, -1);

}
else {

printf("error : %s\n", lua_tostring(L, -1));

return -1;

}

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);

if ( !lua_pcall(L, 1, 1, 0)
) {

rt = lua_tonumber(L, -1);

} else {

printf("error : %s\n", lua_tostring(L, -1));

return -1;

}

时间: 2024-11-10 10:40:19

call lua function from c and called back to c的相关文章

Calling Lua function from C++

http://blog.csdn.net/cnjet/article/details/5909519 Calling Lua function from c++ is very simple. Value passing between c++ and Lua goes through stack, Lua C API provides a convenience ways for you to call Lua function from C. To call Lua function, yo

Lua function 函数

Lua支持面向对象,操作符为冒号‘:’.o:foo(x) <==> o.foo(o, x). Lua程序可以调用C语言或者Lua实现的函数.Lua基础库中的所有函数都是用C实现的.但这些细节对于lua程序员是透明的.调用一个用C实现的函数,和调用一个用Lua实现的函数,二者没有任何区别. 函数的参数跟局部变量一样,用传入的实参来初始化,多余的实参被丢弃,多余的形参初始化为nil. count=0 function incCount(n) n=n or 1 count=count+n end i

Lua function函数,可变参数, 局部函数,尾递归优化

在Lua中,函数是作为"第一类值"(First-Class Value),这表示函数可以存储在变量中,可以通过参数传递给其他函数,或者作为函数的返回值(类比C/C++中的函数指针),这种特性使Lua具有极大的灵活性. Lua对函数式编程提供了良好的支持,可以支持嵌套函数. 另外,Lua既可以调用Lua编写的函数,还可以调用C语言编写的函数(Lua所有的标准库都是C语言写的). 定义一个函数 function hello() print('hello') end hello函数不接收参数

[ISSUE]lua function

刚开始使用lua,还有点不熟悉 class 是网上常见的实现 Test = class("Test") function Test:ctor() self.vark = 0 end function Test:isTest() self.vark = 0 end 如果调用self.isTest() 报错 self is a nil var 如果使用self:isTest() 正确 待解

lua function

local function demoA() print "demoA" end local function demoB () print "demoB" end local function forfun() print "in forfun" end forfun( demoB ) print("========") forfun( demoA() ) 输出结果: in forfun ======== demoA in

calling c++ function from Lua, implement sleep function

http://blog.csdn.net/cnjet/article/details/5909548 You can’t call c function directly from Lua, you have to create a wrapper function that allows calling from Lua. In this post, shows a simple example to implement millisecond sleep function to Lua us

lua 环境探索

什么是环境? http://www.lua.org/manual/5.1/manual.html#2.9 Besides metatables, objects of types thread, function, and userdata have another table associated with them, called their environment. Like metatables, environments are regular tables and multiple

转让lua性能executeGlobalFunction

没有其他的,搞搞cocos2dx的lua文字,话lua这件事情在几年前学过一段时间.还曾对自己c++介面,我已经做了一些小东西.只是时间的流逝,模糊记忆. 拿起点功夫和成本.下面是我的一些经验. cocos2dx运用tolua++来制作的lua接口,tolua++文档不多,网上的一些文章也是答非所问,所以自己看代码是最佳学习途径. cocos2dx操作lua的类是CCLuaEngine,当中实现了载入与实行lua脚本.以及操作lua stack. LuaCocos2d.cpp非常主要,文件的开头

lua解释执行脚本流程

1 #include "lua.hpp" 2 3 #include <iostream> 4 using namespace std; 5 6 #pragma comment(lib, "lua5.1.lib") 7 8 struct lua_guard{ 9 lua_State *pL; 10 lua_guard(lua_State *s) :pL(s){} 11 ~lua_guard(){ lua_close(pL); } 12 }; 13 14 i