lua-快速阅读 - socketref,再见!高德 - C++博客
2013-01-08 18:51
267 查看
lua-快速阅读 - socketref,再见!高德 - C++博客
lua-快速阅读
table 是个怪物,有很多facets,类似array,map,struct,整个是个混合物,用起来也比较怪异。
t={1,2,3,a="gen",b=100}
t={[0]=1;["name"]="ultra"}
t.a, t.b , t[0] , t["name"]
表操作函数:
ipairs,pairs迭代函数
table.getn(t) len of table
================================================================
function() 可以接受任意多的参数,如果实参数过多将丢弃,过少将默认设置为nil
同样可以返回多个参数
a,b=foo()
表作为参数传递到function
function rename( arg ) os.rename(arg.old,arg.new) end
rename{old="";new=""}
匿名函数(lambda in python )
foo = function(x) return x*2 end
局部函数 local f=function(x) ... end
================================================================
for n=start,end,step do ... end
while b do ... end
repeat do .... until
if then .. elseif then ... end;
有意思的语法表达式:
print a or b or c 如果a=false,尝试b...
注释: -- --{ --}
字符串操作: .. 连接
==================================================
io 函数:
loadfile('test.lua')() execute external lua script
loadstring('print 100')()
代码测试:
=======================
c程序调用lua函数
c 程序:
void call_lua_func(){
lua_State *s = lua_open();
luaL_openlibs(s);
int c = lua_gettop(s);
luaL_dofile(s,"/nandflashpartition/test1.lua");
lua_getglobal(s,"add");
lua_pushnumber(s,0.25);
lua_pushnumber(s,8);
if( lua_pcall(s,2,1,0)){
std::cout<< lua_tostring(s,-1)<<std::endl;
}
double r;
r = lua_tonumber(s,-1);
lua_close(s);
}
lua程序:
function add(x,y)
return x*y
end
--------------------------------
lua访问c程序空间变量
1.定义变量student.h
extern char * gender;
extern int class_count;
2.创建pkg文件 student.pkg
$#include "student.h"
extern char * gender;
extern int class_count;
3.产生tolua++存根框架
tolua++ -o student.pkg
4.创建lua测试代码 call_c.lua
print(gender)
print(class_count) 访问c 空间的变量
5.c测试代码
char * gender;
int class_count;
void lua_call_c(){
int tolua_student_open (lua_State* tolua_S);
lua_State * s = lua_open();
luaopen_base(s);
gender ="my gender is male!";
class_count = 100;
tolua_student_open(s);
luaL_dofile(s,"/nandflashpartition/call_c.lua");
lua_close(s);
}
6.build && test it!
table 是个怪物,有很多facets,类似array,map,struct,整个是个混合物,用起来也比较怪异。
t={1,2,3,a="gen",b=100}
t={[0]=1;["name"]="ultra"}
t.a, t.b , t[0] , t["name"]
表操作函数:
ipairs,pairs迭代函数
table.getn(t) len of table
================================================================
function() 可以接受任意多的参数,如果实参数过多将丢弃,过少将默认设置为nil
同样可以返回多个参数
a,b=foo()
表作为参数传递到function
function rename( arg ) os.rename(arg.old,arg.new) end
rename{old="";new=""}
匿名函数(lambda in python )
foo = function(x) return x*2 end
局部函数 local f=function(x) ... end
================================================================
for n=start,end,step do ... end
while b do ... end
repeat do .... until
if then .. elseif then ... end;
有意思的语法表达式:
print a or b or c 如果a=false,尝试b...
注释: -- --{ --}
字符串操作: .. 连接
==================================================
io 函数:
loadfile('test.lua')() execute external lua script
loadstring('print 100')()
代码测试:
=======================
c程序调用lua函数
c 程序:
void call_lua_func(){
lua_State *s = lua_open();
luaL_openlibs(s);
int c = lua_gettop(s);
luaL_dofile(s,"/nandflashpartition/test1.lua");
lua_getglobal(s,"add");
lua_pushnumber(s,0.25);
lua_pushnumber(s,8);
if( lua_pcall(s,2,1,0)){
std::cout<< lua_tostring(s,-1)<<std::endl;
}
double r;
r = lua_tonumber(s,-1);
lua_close(s);
}
lua程序:
function add(x,y)
return x*y
end
--------------------------------
lua访问c程序空间变量
1.定义变量student.h
extern char * gender;
extern int class_count;
2.创建pkg文件 student.pkg
$#include "student.h"
extern char * gender;
extern int class_count;
3.产生tolua++存根框架
tolua++ -o student.pkg
4.创建lua测试代码 call_c.lua
print(gender)
print(class_count) 访问c 空间的变量
5.c测试代码
char * gender;
int class_count;
void lua_call_c(){
int tolua_student_open (lua_State* tolua_S);
lua_State * s = lua_open();
luaopen_base(s);
gender ="my gender is male!";
class_count = 100;
tolua_student_open(s);
luaL_dofile(s,"/nandflashpartition/call_c.lua");
lua_close(s);
}
6.build && test it!
相关文章推荐
- Python开发Http代理服务器 - socketref,再见!高德 - C++博客
- 函数char2Hex() - socketref,再见!高德 - C++博客
- python::写ctypes的好帮手pyglet.wrap.py - socketref,再见!高德 - C++博客
- Python开发Http代理服务器 - socketref,呆在autonavi.com - C++博客
- lua学习笔记之一(C/C++程序员的Lua快速入门[初阶话题])
- lua学习笔记之二(C/C++程序员的Lua快速入门[进阶话题])
- setsockopt 设置socket 详细用法 - C++乐园 - C++博客
- Django 快速搭建博客 第十节(修复首页,阅读量的数据)
- 对lua的简单扩展,使它能快速关联C++的数据。
- lua与c若干问题 - 专职C++ - C++博客
- 通过爬虫代理IP快速增加博客阅读量——亲测CSDN有效!
- 常用排序算法的c++实现(冒泡,选择,插入,堆,shell,快速,归并 )与sort()对比 - coder_xia的专栏 - 博客频道 - CSDN.NET
- lua学习笔记之三(C/C++程序员的Lua快速入门[高阶话题])
- 通过Python爬虫代理IP快速增加博客阅读量
- Django 快速搭建博客 第十一节(文章阅读量统计,自动生成文章摘要)
- C++ 记录学习过程中一些值得多次阅读的博客文章
- 069 - 问,lua程序设计(第四部分)笔记三,c++调用lua函数 - everettjf - C++博客
- Django 快速搭建博客 第十一节(文章阅读量统计,自动生成文章摘要)
- lua与c++的相互调用、C/C++修改Lua中Table的内容
- 快速排序 c++