Chinaunix首页 | 论坛 | 博客
  • 博客访问: 511026
  • 博文数量: 130
  • 博客积分: 10060
  • 博客等级: 上将
  • 技术积分: 1720
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-21 12:35
文章分类

全部博文(130)

文章存档

2011年(2)

2010年(9)

2009年(41)

2008年(78)

我的朋友

分类:

2009-05-14 12:59:25

Lua 代码:

> print('s' < coroutine.create(function() end))
stdin:1: attempt to compare two string values
stack traceback:
        stdin:1: in main chunk
        [C]: ?



问题出在错误消息构建的环节:

int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
  const char *t1 = luaT_typenames[ttype(p1)];
  const char *t2 = luaT_typenames[ttype(p2)];
  if (t1[2] == t2[2])
    luaG_runerror(L, "attempt to compare two %s values", t1);
  else
    luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  return 0;
}


只比较类型字符串的第3个字符,'string'和'thread'是相同的,因此认为类型相同;

再者,Lua的比较操作只有==、<、<=存在对应的指令,所以>操作被交换操作数的顺序而转化为<,结果就总是取得期望较小的操作数的类型。

受影响的只限于小于和小于等于判断,及其调用者,如lua_lessthan接口和table库的sort函数。

阅读(747) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~