call lua function from c and called back to c
2017-05-03 10:31
281 查看
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;
}
--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;
}
相关文章推荐
- call lua function from c and called back to c
- How to call javascript function after validation and before a postback
- File System Redirector and Sysnative - how to call 64 bit program from 32 bit application
- UVa 11175:From D to E and back(图论)
- lua study -- how to call lua function use C language
- How to call an external C function from within Oracle
- How to call C/C++ sytle function from C# solution?
- thinkphp open,Call to undefined function imagecreatefromjpeg()
- How to call the EnumJobs function from a Visual Basic .NET application
- VIP does not relocate back to the original node starting from 10.2.0.4 and 11.1 even after the publi
- Call to undefined function imagecreatefromjpeg() in
- ADS转RVDS invalid call from '~PRES8' function to 'REQ8' function
- ADS 转Keil, ADS升级至MDK常见错误 invalid call from '~PRES8' function to 'REQ8' 解决方法
- UVa - 11175 - From D to E and Back
- From HTTP to HTTPS and back
- Error armlink L6238 Startup.o(Start) contains invalid call from '~PRES8' function to 'REQ8' functio
- Call to undefined function imagecreatefromjpeg()
- How to call function from register view
- UVa - 11175 - From D to E and Back
- Convert IP addresses from a dots-and-number string to a struct in_addr and back