微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将lua中的值放入栈顶

//将表内的值或普通值放入栈顶
void popval(lua_State *L,string val)
{
	int pos=0,oldpos=0;

	if((pos=val.find('.',pos))!=string::npos)
	{
		string ptr(val,pos-oldpos);
		lua_getglobal(L,ptr.c_str());

		pos++;
		oldpos = pos;
	}
	else
	{
		lua_getglobal(L,val.c_str());
		return;
	}

	while((pos=val.find('.',oldpos,pos-oldpos);
		lua_pushstring(L,ptr.c_str());
		lua_gettable(L,-2);

		pos++;
		oldpos = pos;
	}
	string ptr(val,val.size()-oldpos);
	lua_pushstring(L,ptr.c_str());
	lua_gettable(L,-2);
}

用法:

popval(L,"normal_num");
int nv = lua_tonumber(L,-1);
popval(L,"t.table_str");
string str = lua_tostring(L,-1);

其中测试用lua脚本:

normal_num = 152;
t = {table_str = "abctest"};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐