Lua与C/C++的交互4:往Lua中写入C/C++变量

在第二,三章中,我完成了用C/C++读取Lua文件。今天,我又有一个新的目标:将C/C++中变量设置到Lua文件(其实是设置到Lua全局变量表中,为了方便简单理解,我们看做是设置到Lua文件中)。

以下是完整源代码:

#include "stdafx.h"
#include "stdio.h"
extern "C"
{
	#include "lua.h"
	#include "lualib.h"
	#include "lauxlib.h"
};

#pragma comment(lib,"lua5.1.lib")

struct ColorTable
{
	char *name;
	unsigned int red,green,blue;
}colorTable[] = 
{
	{"WHITE",1,1,1},
	{"RED",1,0,0},
	{"GREEN",0,1,0},
	{"BLUE",0,0,1},
	{NULL,0,0,0}
};

void stackDump(lua_State *L)
{
	int i;
	int top = lua_gettop(L);
	printf("the size of stack is:%d\n",top);
	for ( i = 1;i <= top;i++ )
	{
		int type = lua_type(L, i);
		switch(type)
		{
		case LUA_TSTRING:
			{
				printf("%s",lua_tostring(L, i));
				break;
			}

		case LUA_TBOOLEAN:
			{
				printf(lua_toboolean(L, i)?"true":"false");
				break;
			}
		case LUA_TNUMBER:
			{
				printf("%g",lua_tonumber(L, i));
				break;
			}
		case LUA_TTABLE:
			{
				printf("this is a table!");
				break;
			}
		default:
			{
				printf("%s",lua_typename(L ,i));
				break;
			}
		}
		printf(" ");
	}
	printf("\n");
}

//打印全局表中索引为r的信息
void getColorTable(lua_State *L, struct ColorTable *ct)
{
	stackDump(L);
	lua_getglobal(L, ct->name);
	lua_getfield(L, -1, "r");
	stackDump(L);
	lua_pop(L, 2);
	stackDump(L);
}

//构建表中元素(C -> stack)
void setField(lua_State *L, const char *key, int value)
{
	lua_pushnumber(L, value);
	lua_setfield(L, -2, key);
}

//设置全局表(C -> stack)
void setColor(lua_State *L, struct ColorTable *ct)
{
	lua_newtable(L);
	setField(L, "r", ct->red);
	setField(L, "b", ct->blue);
	setField(L, "g", ct->green);
	lua_setglobal(L, ct->name);
	getColorTable(L, ct);
}

//往lua的全局表中注册变量:一个colorTable表(C -> stack)
void setConfigTable(lua_State *L, const char *fname)
{
	//if ( luaL_loadfile(L, fname) || lua_pcall(L, 0, 0 ,0) )
		//printf("error,can't run config file:%s:",lua_tostring(L, -1));
	int i = 0;
	while( colorTable[i].name != NULL )
		setColor(L, &colorTable[i++]);
}

int _tmain(int argc, _TCHAR* argv[])
{
	lua_State *L;
	L = luaL_newstate();
	luaL_openlibs(L);	
	setConfigTable(L, "setConfigTable.lua");
	return 0;
}

打印信息如下:
the size of stack is:0

the size of stack is:2
this is a table! 1
the size of stack is:0

the size of stack is:0

the size of stack is:2
this is a table! 1
the size of stack is:0

the size of stack is:0

the size of stack is:2
this is a table! 0
the size of stack is:0

the size of stack is:0

the size of stack is:2
this is a table! 0
the size of stack is:0

其实,这里我们可以看作C/C++读取Lua文件的相反过程。代码中,我注释掉了对Lua文件的加载调用,因为我们是把C中的变量注册到Lua的全局变量表中。在写Lua程序时,这个全局变量表是_G,里面放置string,print这些lua程序中的表或者函数这类的。所有Lua程序中定义的全局变量都会放入这个表中。所以呢,这里可以看作是,我们把变量放到Lua全局表中,然后Lua所有的文件都可以用这份全局变量了。

解释下里面的几个函数。lua_newtable(L):创建一个表,然后压入栈顶;lua_setfield(L, idx, k)等同于lua_pushstring(L, k) && lua_settable(L, idx),用来设置表中key为k对应的value。要设置栈中表的元素值,步骤是这样的:先压入table,接着压入要设置的key,最后压入要设置的value。所以,当最后调用lua_settable(L, idx)时,栈中的value及key会依次弹出,同时也设置好了table。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值