A metatable handles how an object acts in arithmetic surgery, purchase comparisons, concatenation, length procedure, and indexing

A metatable handles how an object acts in arithmetic surgery, purchase comparisons, concatenation, length procedure, and indexing

A metatable can also define a features becoming also known as whenever a userdata try garbage built-up. For every single of those surgery Lua associates a particular secret known as an event. When Lua executes one of these simple operations over a value, they checks whether this importance features a metatable using matching show. If so, the https://datingmentor.org/escort/pittsburgh/ value related to that trick (the metamethod) controls exactly how Lua will do the procedure.

Metatables manage the procedures listed next. Each procedure is determined by the matching identity. The main element for every single process was a sequence along with its title prefixed by two underscores, ‘ __ ‘; as an instance, the key for procedure «add» will be the sequence «__add» . The semantics of those businesses is much better demonstrated by a Lua function describing the way the interpreter executes the process.

The rule shown in Lua is just illustrative; the real conduct is difficult coded during the interpreter as well as being so much more efficient than this representation. All functionality found in these information ( rawget , tonumber , etc.) become described in A§5.1. In particular, to retrieve the metamethod of certain item, we use the term

The function getbinhandler below defines how Lua decides a handler for a binary operation. Initially, Lua attempts initial operand. If the kind does not define a handler for operation, next Lua attempts the next operand.

2.9 Surroundings

Besides metatables, things of types thread, purpose, and userdata posses another desk of all of them, called their own conditions. Like metatables, circumstances become typical dining tables and multiple things can promote the exact same planet.

Threads are created discussing the environment in the producing thread. Userdata and C functionality are made sharing the surroundings of this generating C work. Non-nested Lua features (developed by loadfile , loadstring or load ) are made revealing the surroundings of the creating thread. Nested Lua features are manufactured revealing the environmental surroundings on the creating Lua features.

Circumstances connected with userdata do not have definition for Lua. It is simply a convenience feature for code writers to associate a table to a userdata.

Conditions involving threads have been called international circumstances. They Are Utilized since default atmosphere for threads and non-nested Lua functionality created by the thread and may feel right utilized by C signal (discover A§3.3).

Environmental Surroundings associated with a C work may be straight reached by C code (read A§3.3). Truly used because the default planet for other C performance and userdata developed by the function.

Situations associated with Lua features are used to fix all accesses to worldwide variables around the work (read A§2.3). They are used because standard ecosystem for nested Lua performance developed by the big event.

You’ll be able to alter the conditions of a Lua purpose or the run bond by contacting setfenv . You could get the environment of a Lua function or the run bond by phoning getfenv . To control the surroundings of additional objects (userdata, C functions, other posts) you need to use the C API.

2.10 Rubbish Range

Lua executes automated memory space control. Which means you have to stress neither about allocating memory for brand new items nor about releasing they whenever the stuff are no lengthier demanded. Lua handles storage instantly by run a garbage enthusiast every so often to get all dead items (this is certainly, items which happen to be don’t easily accessible from Lua). All memory space utilized by Lua try at the mercy of automated management: dining tables, userdata, functionality, posts, strings, etc.

Lua implements an incremental mark-and-sweep collector. It makes use of two data to regulate their garbage-collection series: the garbage-collector stop additionally the garbage-collector step multiplier. Both incorporate percentage things as products (to ensure that a value of 100 implies an interior value of 1).

Deja un comentario

Tu dirección de correo electrónico no será publicada.