LUA 学习1

来源:互联网 发布:一个程序员眼中的fenng 编辑:程序博客网 时间:2024/06/10 08:12

Lua is a dynamically-typed language: types are attached to values rather than to
variables. Lua has eight basic types:nil,boolean,number,string,table,function,

userdata, and thread.

LUA是一种动态类型语言。类型依附值存在。LUA有8种基本类型。


Nil is a marker type having only one value, also called nil.

Boolean values are the usual true and false.

Numbers are double-precision floating-point numbers, corresponding to the type double in C, but it is easy to
compile Lua using float or long instead. (Several games consoles and smaller machines lack hardware support for double.)
Strings are arrays of bytes withan explicit size, and so can contain arbitrary binary data, including embedded zeros.
Tables are associative arrays, which can be indexed by any value (exceptnil) and can hold any value.
Functions are either Lua functions or C functionswritten according to a protocol for interfacing with the Lua virtual machine.
Userdata are essentially pointers to user memory blocks, and come in two flavors:heavy, whose blocks are allocated by Lua and are subject to garbage collection,and light, whose blocks are allocated and freed by the user.
Finally, threads represent coroutines.
Values of all types are first-class values: we can store them in global variables, local variables and table fields, pass them as arguments tofunctions, return them from functions, etc.

在LUA中所有类型都是第一值。第一值都可以在全局变量,局部变量和Table字段中存储、第一值也可以作为参数传递、也可以作为函数返回值。

/*
** Union of all Lua values
*/
typedef union
{
  GCObject *gc;
  void *p;  //通过指针来实现LUA语言中的table,function,thread,userdata的值。
  lua_Number n;
  int b;
} Value;
/*
** Tagged Values
*/
typedef struct lua_TValue
{
    Value v;
    int t;
} TValue;



Lua represents values as tagged unions, that is, as pairs (t,v), where t is  an integer tag identifying the type of the value v, which is a union of C types  implementing Lua types.、
在LUA中,所有的值都是使用 tagged unions 联合作为值。t是一个整型标记来v的类型。通过一个Union和 Int 在C语言中实现LUA的所有类型。
Nil has a single value.
Booleans and numbers are implemented as ‘unboxed’ values: v represents values of those types directly in the union.
This implies that the union must have enough space for a double.
Strings,tables, functions, threads, and userdata values are implemented by reference:v contains pointers to structures that implement those values.
LUA 语言中的strings,table ,functions,threads和userdata的值通过引用来实现。v中包含一个指针来指向结构体来实现LUA语言的这些类型。
Those structures share a common head, which keeps information needed for garbage collection.
The rest of the structure is specific to each type.



TValue is the main structure in this implementation: it represents the tagged unions (t,v)
described above.
Value is the union that implements the values.
Values of type nil are not explicitly represented in the union because the tag is enough to identify
them.

The field n is used for numbers (by default, lua_Number is double).
在Value struct结构体中的n用来存储 LUA语言的number类型。Lua_Number的类型为double.可以在luaconf.h中配置Lua_Number的实现。

the field b is used for booleans.
在Value struct 字段b用来实现lua语言的boolean类型。

The field p is used for light userdata.
在Value struct字段p用于实现lua语言的userdata类型
The field gc is used for the other values (strings, tables, functions, heavy userdata, and threads),which are those subject to garbage collection.
在Value struct 字段gc用来实现lua语言中的string,table,function, heavy userata 和thread类型。这些值需要被LUA来管理内存。

在lobject.h中如下源代码来判断lua语言中动态类型
/* Macros to test type */
#define ttisnil(o)    (ttype(o) == LUA_TNIL)       //TValue tt标记着TValue的类型。LUA 和Python这些动态数据类型,都是通过本身的来指定类型。
#define ttisnumber(o)    (ttype(o) == LUA_TNUMBER)
#define ttisstring(o)    (ttype(o) == LUA_TSTRING)
#define ttistable(o)    (ttype(o) == LUA_TTABLE)
#define ttisfunction(o)    (ttype(o) == LUA_TFUNCTION)
#define ttisboolean(o)    (ttype(o) == LUA_TBOOLEAN)
#define ttisuserdata(o)    (ttype(o) == LUA_TUSERDATA)
#define ttisthread(o)    (ttype(o) == LUA_TTHREAD)
#define ttislightuserdata(o)    (ttype(o) == LUA_TLIGHTUSERDATA)

/* Macros to access values */
#define ttype(o)    ((o)->tt) //通过判断TValue tt来判断对象类型



对LUA 的虚拟栈大小获取:

//通过对指针运算来获取当前虚拟栈大小。

LUA_API int lua_gettop (lua_State *L)
{
  return cast_int(L->top - L->base);
}


typedef TValue *StkId;  /* index to stack elements */


struct lua_State
{
  CommonHeader;
  lu_byte status;
  StkId top;  /* first free slot in the stack */ //与虚拟栈相关联。栈顶元素
  StkId base;  /* base of current function */   //与虚拟栈相关联。 当前栈元素

}



//通过C语言来管理LUA stack
LUA_API void lua_pushnil (lua_State *L)
{
  lua_lock(L);
  setnilvalue(L->top);//设置栈顶类型为nil
  api_incr_top(L);    //栈顶元素指向下一个TValue
  lua_unlock(L);
}

#define setnilvalue(obj) ((obj)->tt=LUA_TNIL) //设置当前TValue的类型为LUA_TNIL

#define api_incr_top(L)   {api_check(L, L->top < L->ci->top); L->top++;} //设当前栈指向下一个元素。



0 0
原创粉丝点击