#define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */ #define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */ #define ClosureHeader \ CommonHeader; lu_byte nupvalues; GCObject *gclist typedef struct CClosure { ClosureHeader; lua_CFunction f; TValue upvalu...
nil, boolean, number和lua_CFunction直接存储在TValue中,占用至少12个字节。 string, lua function, userdata, thread和table这些可以被垃圾回收管理的类型,TValue只是索引,具体数据存储在堆上,被gc指针索引。 下面重点介绍table的实现和性能。 Table的实现 Table对外的表现是一个Key-Value的Hash容器,除了nil以外的任...
typedef int (*lua_CFunction) (lua_State*L); lu_byte是个unsigned char类型,官方将其取名作lu_byte也许是表示lua unsigned byte的意思。 lua_CFunction表示Lua栈中能被调用的light c function的形式。 现在来看Lua的通用数据类型Value和TValue // lobject.h /* ** Tagged Values. This is the basic repr...
从类型上来看,lua一共有3种函数,分别是light c function、c closure和lua closure。关于第一种,light c function的概念,操作和调用流程,在第一章有着非常详细的阐述。至于第二种c closure,它有自己独立的数据结构,包含一个函数指针,以及一个upvalue列表,c closure实例受gc管控,c closure的创建流程,和运行流程,可...
其次,它们只能统计Lua函数(包括C编写的闭包和lightCFunction),无法统计C模块内部的C函数开销。而使用其他C性能分析工具时,也无法分析与Lua函数相关的耗时。这在进行性能分析时会导致非常不连贯的感觉。 此外,当使用C的性能分析器进行分析时,我们会失去上下文信息。由于Lua是用C语言编写的虚拟机,当我们发现某个C函数的...
function有三个子类: C Function, Lua Function和light C Function userdata有两个子类:userdata和light userdata thread就是lua中的协程 table是lua中唯一的聚合类型,不像c++的STL那样,拥有vector、map、set等多种容器,在lua中,只有table。 这8种类型以union的形式定义在TValue中 ...
intn;/* number of arguments (Lua) or returns (C) */ ptrdiff_t funcr = savestack(L, func); switch(ttype(func)) { caseLUA_TLCF:/* light C function */ f = fvalue(func); gotoCfunc; caseLUA_TCCL: {/* C closure */
{/* 判断函数类型,是C函数还是Lua函数 */ caseLUA_TCCL: /* C closure */ /* do something */ goto Cfunc; caseLUA_TLCF: /* light C function */ /* do something */ return 1; default: { /* 既不是C函数,也不是Lua函数 */
因为你调用 Lua C API 的 C 代码块都是直接或间接被 Lua 调用的。但把 Lua C API 遍布在宿主程序中时却很容易忽视。完善的做法是,你应该把你的业务逻辑写到一个lua_CFunction中,然后用lua_pcall去调用它。而这个代码块的参数则应该用 void * 通过lua_pushlightuserdata来传递。
Lua代码中调用C函数对Lua来说至关重要,让Lua能真正站到C这个巨人的肩膀上。 要写一个能让Lua调用的C函数,就要符合lua_CFunction定义:typedef int(*lua_CFunction) (lua_State *L); 当Lua调用C函数的时候,同样使用栈来交互。C函数从..