Analyze several data structures commonly used in Lua:
Let's take a look at the following in opcode. h:
typedef unsigned char Byte;typedef unsigned short Word;typedef union{ struct {char c1; char c2;} m; Word w;} CodeWord;typedef union{ struct {char c1; char c2; char c3; char c4;} m; float f;} CodeFloat;
These two union types are mainly used for VM commands.
In lua1.1, the VM command is a byte. When the number of files to be saved is a word or float, take word as an example. You can assign a value to W of the codeword type directly, use the two fields of the codeword struct m to generate commands and store them in the Command array.
The implementation of the following code_word method shows how this structure is used.
static void code_word (Word n){ CodeWord code; code.w = n; code_byte(code.m.c1); code_byte(code.m.c2);}
Opcode enumeration is all commands supported by the lua1.1 virtual machine.
Type enumeration is the data types in Lua.
typedef enum{ T_MARK, T_NIL, T_NUMBER, T_STRING, T_ARRAY, T_FUNCTION, T_CFUNCTION, T_USERDATA} Type;
The value union is the data definition of Lua.
typedef union{ Cfunction f; real n; char *s; Byte *b; struct Hash *a; void *u;} Value;
The tag field indicates the object type, and the value indicates the object value.
typedef struct Object{ Type tag; Value value;} Object;
Symbol, the name of the symbol, and the value of the object symbol. It is of the object type.
typedef struct{ char *name; Object object;} Symbol;struct List{ Symbol *s; struct List *next;};
A symbol table is an array of symbols. Next is mainly used for searching in searchlist.
Hash. h defines the joined array, that is, the table type in Lua.
// Table
Typedef struct node {Object ref; // key object Val of the element; // value of the element struct node * Next; // pointer to the next element .} Node;
// Table definition
typedef struct Hash{ char mark; unsigned int nhash; Node **list;} Hash;
Where:
Mark During garbage collection
Number of elements in the nhash table
List element list
Lua1.1 Data Structure