理解 Lua 的那些坑爹特性
1. 协程只能在Lua代码中使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
c = require ( 'c' ) co = coroutine.create ( function () print ( 'coroutine yielding' ) c.callback( function () coroutine.yield () end ) print ( 'coroutine resumed' ) end ) coroutine.resume (co) coroutine.resume (co) print ( 'the end' ) |
1
|
local c = require 'c' |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#include<stdio.h> #include<stdlib.h> #include<lua.h> #include<lualib.h> #include<lauxlib.h> static int c_callback(lua_State *L){ int ret = lua_pcall(L, 0, 0, 0); if (ret){ fprintf (stderr, "Error: %s\n" , lua_tostring(L, -1)); lua_pop(L, 1); exit (1); } return 0; } static const luaL_Reg c[] = { { "callback" , c_callback}, {NULL, NULL} }; LUALIB_API int luaopen_c (lua_State *L) { luaL_register(L, "c" , c); return 1; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include<stdio.h> #include<stdlib.h> #define LUA_LIB /* 告诉Lua,这是一个LIB文件 */ #include<lua.h> #include<lualib.h> #include<lauxlib.h> static int c_cont(lua_State *L) { /* 这里什么都不用做:因为你的原函数里面就没做什么 */ return 0; } static int c_callback(lua_State *L){ /* 使用 lua_pcallk,而不是lua_pcall */ int ret = lua_pcallk(L, 0, 0, 0, 0, c_cont); if (ret) { fprintf (stderr, "Error: %s\n" , lua_tostring(L, -1)); lua_pop(L, 1); exit (1); } /* 因为你这里什么都没做,所以c_cont里面才什么都没有。如果这里需要做 * 什么东西,将所有内容挪到c_cont里面去,然后在这里简单地调用 * return c_cont(L); * 即可。 */ return 0; } static const luaL_Reg c[] = { { "callback" , c_callback}, {NULL, NULL} }; LUALIB_API int luaopen_c (lua_State *L) { /* 使用新的 luaL_newlib 函数 */ luaL_newlib(L, c); return 1; } |
1
2
3
4
|
lua -- co.lua coroutine yielding coroutine resumed the end |
1
2
3
4
5
6
7
8
9
|
function do_login(server) server:login( function (data) -- 错误处理先不管,假设有一个全局处理错误的机制(后面会提到,实际 -- 上就是newtry/protect机制) server:get_player_info( function (data) player:move_to(data.x, data.y) end ) end , "username" , "password" ) end |
1
2
3
4
5
|
function d_login(server) server:login( "username" , "password" ) local data = server:get_player_info() player:move_to(data.x, data.y) end |
1
2
3
4
5
6
7
8
9
10
11
|
local current function server:login(name, password) assert ( not current, "already send login message!" ) server:callback_login( function (data) local cur = current current = nil coroutine.resume (cur, data) end , name, password) current = coroutine.running () coroutine.yield () end |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
function coroutinize(f, reenter_errmsg) local current return function (...) assert ( not current, reenter_errmsg) f( function (...) local cur = current current = nil coroutine.resume (cur, ...) end , ...) current = coroutine.running () coroutine.yield () end end |
2. 幽灵一般的 nil
1
2
3
4
5
6
7
|
undefined = {} -- 指定一个全局变量存在,但不指向任何地方: a = undefined -- 判断这个全局变量是否不指向任何地方: if a == undefine then ... end -- 彻底删除这个变量: a = nil |
3. 没有continue
1
2
|
local a = true repeat a = false until a |
1
2
3
4
5
6
7
|
for line in configfile do if string.sub (line, 1 , 1 ) == '#' then goto next end parse_config(line) :: next :: end |
1
2
3
4
5
6
7
8
9
10
|
local i repeat if i == 5 then goto next end local j = i * i print ( "i = " ..i.. ", j = " ..j) i = i + 1 :: next :: until i == 10 |
1
2
3
4
|
lua -- "noname\2013-01-03-1.lua" lua: noname\2013-01-03-1.lua:10: <goto next> at line 4 jumps into the scope of local 'j' shell returned 1 Hit any key to close this window... |
4. 错误信息的表达
5. 下标
6. 提前返回
7. 方法调用
8. 面向对象
9. 结论
理解 Lua 的那些坑爹特性的更多相关文章
- 【转载】【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态
[游戏开发]在Lua中实现面向对象特性——模拟类.继承.多态 阅读目录 一.简介 二.前提知识 三.Lua中实现类.继承.多态 四.总结 回到顶部 一.简介 Lua是一门非常强大.非常灵活的脚本语 ...
- 理解Javascript的动态语言特性
原文:理解Javascript的动态语言特性 理解Javascript的动态语言特性 Javascript是一种解释性语言,而并非编译性,它不能编译成二进制文件. 理解动态执行与闭包的概念 动态执行: ...
- 【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态
一.简介 Lua是一门非常强大.非常灵活的脚本语言,自它从发明以来,无数的游戏使用了Lua作为开发语言.但是作为一款脚本语言,Lua也有着自己的不足,那就是它本身并没有提供面向对象的特性,而游戏开发是 ...
- 两个函数彻底理解Lua中的闭包
本文通过两个函数彻底搞懂Lua中的闭包,相信看完这两个函数,应该能理解什么是Lua闭包.废话不多说,上 code: --[[************************************** ...
- 深入理解Lua的闭包一:概念、应用和实现原理
本文首先通过具体的例子讲解了Lua中闭包的概念,然后总结了闭包的应用场合,最后探讨了Lua中闭包的实现原理. 闭包的概念 在Lua中,闭包(closure)是由一个函数和该函数会访问到的非局部变量 ...
- 理解lua中 . : self
前言 在LUA中,经常可以看到:. self,如果你学习过Java或C#语言,可以这样理解 .对于c#和java的静态方法 :相当于是实例方法 今天在CSDN上看到一篇博客写的很清楚,转载过来 原文出 ...
- lua table integer index 特性
table.maxn (table) Returns the largest positive numerical index of the given table, or zero if the t ...
- lua语言三则特性
pack和unpack 对于一个函数, 要将其入参转换为一个表, 则pack函数合适. 对于一个表要将其转换为 一个函数的入参, 则 lua原生提供的 unpack函数可以实现. do arrayDa ...
- 理解lua 语言中的点、冒号与self
转载自: http://blog.csdn.net/wangbin_jxust/article/details/12170233 lua编程中,经常遇到函数的定义和调用,有时候用点号调用,有时候用冒号 ...
随机推荐
- Java 第27章 JDBC
JDBC 模版 JDBC 的工作原理 JDBC API 提供者:Sun公司 内容:供程序员调用的接口与类,集成在java.sql 和javax.sql 包中,如: DriverManager 类 Co ...
- Cocostudio 文章列表
Cocostudio 文章列表 Cocostudio(1) 容器层的使用- ScrollView ListView PageViewhttp://www.cnblogs.com/TS-qrt/arti ...
- IB交换机配置命令总结
串口通过远程CRT登录,波特率9600用户名和密码都是adminDo you want to use the wizard for initial configuration?选择no打开ip rou ...
- Hive使用技巧
hive默认查询不会显示列名, 当一个表字段比较多的时候,往往看不出值与列之间的对应关系,对日常查错及定位问题带来不便,像下面这样. hive> >select * from exampl ...
- IOS竖屏应用单个页面横屏的解决办法
昨天朋友问我,怎么实现在竖屏的应用里,显示一个横屏的应用,由于也没做过 就说不知道了,但是我觉得会有这样的API ,因为我手机里就安装有这种类型的软件 今天早上起来,就想做一个Demo出来,惯例的是查 ...
- debian下使用gitosis+gitweb搭建SSH认证的git服务器
搭建完成Git服务器后,需要使用两台机器进行测试,一台机器作为服务器端server,一台服务器作为客户端client.整个系统,需要三个计算机账户,这里假设server端的账户名为git,client ...
- 如何运用boolean跳出循环
用布尔类型跳出循环:1.首先申明一个布尔变量:boolean y =false:申明位置在:方法内,循环外:public void s(){//在此申明布尔变量:for(){}}if(!y){}2,进 ...
- [转]C语言指针学习经验总结浅谈
指针是C语言的难点和重点,但指针也是C语言的灵魂 . 这篇C语言指针学习经验总结主要是我入职以来学习C指针过程中的点滴记录.文档里面就不重复书上说得很清楚的概念性东西,只把一些说得不清楚或理解起来比较 ...
- poj 1195 - Mobile phones(树状数组)
二维的树状数组,,, 记得矩阵的求和运算要想好在写.... 代码如下: #include <cstdio> #include <cstdlib> #include <cm ...
- [ASE][Daily Scrum]11.30
燃烧图的页面进不去了…… 小结一下吧,sprint2的内容已经基本完成了, 推迟到之后进行的任务: ·地图块的刷新 一些bug尚未修复不过不是特别重要所以也推到后面了, 之后两个sprint主要会增加 ...