Source Code Implementation of Data Types in Lua and source code of lua Data Types

Source: Internet
Author: User

Source Code Implementation of Data Types in Lua and source code of lua Data Types

Overview

There are eight basic types in Lua: nil, boolean, number, string, userdata, function, thread, and table. You can use the function type to view the type of a variable or value and return the corresponding type name. Like other dynamic languages, there is no type definition syntax in the language, and each value carries its own type information. The following describes the implementation of the type through the source code of Lua 5.2.1.

Source code implementation

Lua represents a union structure with a flag. The Code is as follows (lobject. h ):

 90 /* 91 ** Union of all Lua values 92 */ 93 typedef union Value Value;100 /*101 ** Tagged Values. This is the basic representation of values in Lua,102 ** an actual value plus a tag with its type.103 */104 105 #define TValuefields    Value value_; int tt_400 struct lua_TValue {401   TValuefields;402 };

 
 

We can see that the struct has two members:

One is the integer tt _ used to represent the data type. All data types in Lua are defined as follows (lua. h ):

 78 #define LUA_TNIL        0 79 #define LUA_TBOOLEAN        1 80 #define LUA_TLIGHTUSERDATA  2 81 #define LUA_TNUMBER     3 82 #define LUA_TSTRING     4 83 #define LUA_TTABLE      5 84 #define LUA_TFUNCTION       6 85 #define LUA_TUSERDATA       7 86 #define LUA_TTHREAD     8

Nine data types are implemented. In the syntax, userdata is divided into LUA_TLIGHTUSERDATA and LUA_TUSERDATA. The previous type is lightweight userdata (light userdata ), lightweight userdata is a value that represents the C pointer. For a Lua virtual machine, this data type does not require GC (garbage collection), and the memory to which it points is allocated and released by the user; the latter userdata type is completely userdata (full userdata). The memory is allocated by the Lua Virtual Machine and the GC mechanism is responsible for processing.

Structure lua_TValue another data member is value _, which is a consortium, the Code is as follows (lobject. h ):

 96 #define numfield    lua_Number n;    /* numbers */103 typedef LUA_NUMBER lua_Number;(lua.h)392 #define LUA_NUMBER     double(luaconf.h)391 union Value { 392   GCObject *gc;    /* collectable objects */393   void *p;         /* light userdata */394   int b;           /* booleans */395   lua_CFunction f; /* light C functions */396   numfield         /* numbers */397 };

 
 

Through comments, you can easily understand the meaning of each member, but it is necessary to explain to the following members:

Numfield: used to represent all numeric values. Actually, it corresponds to the double type. This is also used to represent integer types. In addition, the implementation in Lua 5.3 separates the representation of integer and floating point numbers.

GCObject * gc: used to point to objects that require garbage collection, including string, table, function, full userdata, and thread types. GCObject is used to indicate objects that can be recycled by garbage collection. It is also a consortium, and its code is as follows (lstate. h)

185 union GCObject {186   GCheader gch;  /* common header */187   union TString ts;188   union Udata u;189   union Closure cl;190   struct Table h;191   struct Proto p;192   struct UpVal uv;193   struct lua_State th;  /* thread */194 };

The member GCheader gch is mainly used for GC collection. Other members such as TString ts are the structures that actually store values, and these data structures also have GCheader for GC management.

In general, the structure of various numeric types in Lua is as follows:



Finally, regarding the data type Implementation of Lua, it is worth noting that a struct with a flag is used to represent the numeric type of Lua, the space occupied by any data type in Lua is at least 16 bytes (struct needs to be replaced). Even if the nil type is used, the space occupied is 8 bytes, therefore, copying the Lua value is time-consuming. The next article will discuss the implementation of the Lua string.

References

Http://blog.aliyun.com/761

Lua5.2.1 source code








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.