Lua1.0 数据结构

转载出处:http://my.oschina.net/xhan/blog/307171

先来看一下 Lua 中常用的几个数据结构:

先看一下 opcode.h 中的:

Type 枚举是 Lua 中的几种数据类型。

 1 typedef enum
 2 {
 3  T_MARK,
 4  T_NIL,
 5  T_NUMBER,
 6  T_STRING,
 7  T_ARRAY,
 8  T_FUNCTION,
 9  T_CFUNCTION,
10  T_USERDATA
11 } Type;
View Code

Value 联合体是 Lua 的数据类型定义。

1 typedef union
2 {
3  Cfunction      f;
4  real         n;
5  char          *s;
6  Byte          *b;
7  struct Hash    *a;
8  void           *u;
9 } Value;
View Code

Object 带标签的数据类型,其中 tag 字段是 Type 类型,Value 是 Object 的值。

1 typedef struct Object
2 {
3  Type  tag;
4  Value value;
5 } Object;
View Code

Symbol 符号,一个是符号的名字,一个是符号的值,其值是一个 Object 类型。

1 typedef struct
2 {
3  char   *name;
4  Object  object;
5 } Symbol;
View Code

以下的一些代码就是一些上面数据结构的操作宏。

hash.h  中定义了关联数组,也就是 lua 里的 table 类型。

// table 中的无素
typedef struct node
{
 Object ref; // 元素的 key
 Object val; // 元素的 value
 struct node *next; // 指向下一个元素的指针。
} Node;

// table 定义
typedef struct Hash
{
 char mark;
 unsigned int nhash;
 Node **list;
} Hash;

其中:

mark 在垃圾回收时的标记

nhash table 中的元素个数

list 元素的列表。

其它地方也没有别的数据结构了。

原文地址:https://www.cnblogs.com/vd01/p/4931362.html