Heim  >  Artikel  >  Backend-Entwicklung  >  lua与c语言互相调用

lua与c语言互相调用

WBOY
WBOYOriginal
2016-08-08 09:23:391866Durchsuche

lua和c语言之间的互调大量出现在lua-nginx-module中,这里给出《lua程序设计》第24-27章的一些测试代码。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdarg.h>
#include <stdlib.h>

#define MAX_COLOR 255

static void stackDump(lua_State *L);

static int l_split(lua_State *L)
{
	const char *s = luaL_checkstring(L, 1);
	const char *sep = luaL_checkstring(L, 2);
	const char *e;
	int i = 1;
	
	lua_newtable(L);
	
	while ((e = strchr(s, *sep)) != NULL)
	{
		lua_pushlstring(L, s, e-s);
		lua_rawseti(L, -2, i ++);
		s = e + 1;
	}
	
	lua_pushstring(L, s);
	lua_rawseti(L, -2, i);
	
	return 1;
}

int l_map(lua_State *L)
{
	int i, n;
	luaL_checktype(L, 1, LUA_TTABLE);
	luaL_checktype(L, 2, LUA_TFUNCTION);
	
	n = lua_objlen(L, 1);
	for (i = 1; i <p>test.lua</p>
<p></p>
<pre name="code">BULE = {r = 0, g = 0, b = 1}
table = {["r"] = 10, ["g"] = 23, ["b"] = 33}

function f(x, y)
	return x + y
end

编译:gcc test.c -I/usr/local/include/luajit-2.0 -llua-5.1 -lm

以上就介绍了lua与c语言互相调用,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn