使用lua扩展应用程序
- 全局变量的操作
void lua_getglobal(lua_State * L ,const char * name)
此函数从lua中取出一个名为name的全局变量并将其压入栈中。
如当lua文件内容为
width = 200
height = 300时,以下代码
int _tmain(int argc, _TCHAR* argv[])
{ lua_State *L = luaL_newstate();
luaL_openlibs(L); if( != luaL_loadfile(L,"config_width_height.lua"))
{
printf("loadbuff error :%s",lua_tostring(L,-));
lua_pop(L,-);
}
if( != lua_pcall(L,,,))
{
printf("pcall error :%s",lua_tostring(L,-));
lua_pop(L,-);
} lua_getglobal(L,"width");
printf("width = %d\n",lua_tointeger(L,-));
lua_getglobal(L,"height");
printf("height = %d\n",lua_tointeger(L,-)); lua_settop(L,);
lua_close(L); system("pause");
return ;
}
运行结果为
width = 200height = 300
请按任意键继续. . .
2. table的操作
文件内容:
width = 200
height = 300background =
{r = 0,
g = 0,
b = 1
}
获取以上 r g b的代码
int red;
int blue;
int green;
lua_getglobal(L,"background"); //push table
if(lua_istable(L,-))
{
red = getfield(L,"r");
green = getfield(L,"g");
blue = getfield(L,"b");
printf("red:%d,green:%d blue:%d ",red,green,blue);
}
注意:getfield不是 lua内置函数。getfield函数如下。
/*假设table 们位于栈顶*/
int getfield(lua_State * L,const char * key)
{
int result ;
lua_pushstring(L,key); //push index
lua_gettable(L,-);//获取table //pop index push table[index]
if(!lua_isnumber(L,-))
printf("error is the value is not a value\n");
result = (int ) lua_tonumber(L,-) * MAX_COLOR;
lua_pop(L,);
return result;
}
使用lua扩展应用程序的更多相关文章
- 用lua扩展你的Nginx(整理)-----openresty
用lua扩展你的Nginx(整理) 首先得声明.这不是我的原创,是在网上搜索到的一篇文章,原著是谁也搞不清楚了.按风格应该是属于章亦春的文章. 整理花了不少时间,所以就暂写成原创吧. 一. 概述 Ng ...
- 如何用 MEF 扩展应用程序
最近在写一篇关于如何扩展 Visual Studio 编辑器的文章时,用到了 MEF,因此打算写一篇文章提一下这个技术点.本篇文章并不打算详细介绍 MEF,只是一个最简单的入门,相信您在阅读本篇文章后 ...
- 【开源】前端练手笔记,Chrome扩展应用程序(html+CSS+JS) (1)
项目名称:github-notification 项目地址:https://github.com/WQTeam/github-notification 说明:本人打算抽时间学习前端(html + cs ...
- 使用Managed Extensibility Framework方便的扩展应用程序
概述 Managed Extensibility Framework(MEF)是.NET平台下的一个扩展性管理框架,它是一系列特性的集合,包括依赖注入(DI)以及Duck Typing等.MEF为开发 ...
- Lua的协同程序初探
Content: 前两天把Lua的协同程序概念看了一下,不是很懂,只能说<Programming In Lua>中把它解释成线程让人很好的理解起来,但是真正去看的时候,收获并不是很大.第一 ...
- 给lnmp一键包中的nginx安装openresty的lua扩展
lnmp一键包(https://lnmp.org)本人在使用之后发现确实好用,能帮助我们快速搭建起lnmp.lamp和lnmpa的web生产环境,因此推荐大家可以多试试.但有的朋友可能需要使用open ...
- 在小程序中实现全局混入,以混入的形式扩展小程序的api
GitHub: https://github.com/WozHuang/mp-extend 相关文章: 小程序全局状态管理,在页面中获取globalData和使用globalSetData 通过页面预 ...
- linux下安装php的lua扩展
1. 进入管理员权限使用yum安装 readline(也可以使用wget下载后./configure 然后 make && make install进行安装) yum install ...
- lua使用笔记2:Linux 中安装php的lua扩展
安装lua扩展的前提是lua已经安装好,如果没有安装,参照 1.http://pecl.php.net/package/lua 下载lua扩展 或者Linux下直接输入 wget http://pec ...
随机推荐
- [BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 885 So ...
- (1)Java Spring
Spring 4种关键策略: 基于POJO的轻量级和最小侵入编程 通过依赖注入和面向接口实现松耦合 基于切面和惯例进行声明式编程 通过切面和模板减少样板式代码
- FZU-2216 The Longest Straight(尺取法)
Problem 2216 The Longest Straight Accept: 523 Submit: 1663Time Limit: 1000 mSec Memory Limit ...
- Binary Tree Iterative Traversal
Preorder class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<in ...
- 自己写Tiny6410的Bootloader总结!
1.由于Tiny6410 2G版的Nand flash(K9GAG08U0E)的页大小是8K的,但是s3c6410芯片设置为nand flash启动时先从nand flash复制8K代码到片内内存中去 ...
- WPF中的DesignMode判断
WPF中提供你一个类似WinForm的DesignMode属性的方法来判断当前是否处于设计器模式: bool IsInDesignMode { get { return Desig ...
- 在C#中用RX库和await来实现直观的状态机
在程序的设计过程中,我们经常会遇到一些需要使用状态机的场景,相信状态机的编写和维护是令每一个程序员都非常头大的事情.到了C# 5.0后,由于引进了await语法糖,我们可以通过await和Reacti ...
- UBI介绍
转:http://blog.csdn.net/kickxxx/article/details/6707589 目录 Table of contents Big red note User-space ...
- Kyle 的 iOS 面试题
1.简单介绍下你对swizzling方法的了解,一般你什么时候使用. 2.有三个对象 A,B,C..:A retain B, B retain C, C retain B..当 A release B ...
- ASIHTTPRequest学习(二)
Handling compressed responses, and compressing request bodies Using gzip to handle compressed respon ...