A metatable settings exactly how an item behaves in arithmetic operations, order comparisons, concatenation, size process, and indexing

A metatable settings exactly how an item behaves in arithmetic operations, order comparisons, concatenation, size process, and indexing

A metatable settings exactly how an item behaves in arithmetic operations, order comparisons, concatenation, size process, and indexing

A metatable can also establish a work to-be also known as when a userdata is actually garbage accumulated. For every of these procedures Lua associates a specific trick known as an event. Whenever Lua works one of these brilliant functions over a value, it checks whether this advantages have a metatable utilizing the matching occasion. If that’s the case, the value connected with that secret (the metamethod) controls exactly how Lua will perform the procedure.

Metatables control the procedures noted further. Each process is recognized by its matching title. The important thing per operation is a sequence featuring its title prefixed by two underscores, ‘ __ ‘; including, the important thing for process « add » may be the sequence « __add » . The semantics of the surgery is better demonstrated by a Lua features describing the way the interpreter executes the process.

The laws found here in Lua is only illustrative; the real actions is difficult coded inside interpreter as well as being a lot more effective than this representation. All functions found in these descriptions ( rawget , tonumber , etc.) are described in A§5.1. In particular, to retrieve the metamethod of a given item, we make use of the phrase

The event getbinhandler below defines exactly how Lua chooses a handler for a binary procedure. Very first, Lua attempts 1st operand. If their sort does not define a handler for procedure, then Lua attempts the 2nd operand.

2.9 Circumstances

Besides metatables, things of type thread, function, and userdata bring another dining table involving all of them, called their planet. Like metatables, situations include standard tables and multiple things can promote exactly the same environment.

Threads are created discussing the environmental surroundings in the creating thread. Userdata and C functions are made revealing environmental surroundings in the producing C work. Non-nested Lua functions (created by loadfile , loadstring or burden ) are created discussing the environment of generating thread. Nested Lua applications are created revealing the surroundings associated with the creating Lua features.

Situations associated with userdata haven’t any definition for Lua. It is only a convenience ability for code writers to associate a table to a userdata.

Environments connected with threads have been called global surroundings. One Can Use Them once the default atmosphere for threads and non-nested Lua functions developed by the thread and can be immediately reached by C laws (read A§3.3).

The Environmental Surroundings of a-c function tends to be right accessed by C signal (see A§3.3). It’s utilized since the default surroundings for other C applications and userdata produced by the event.

Surroundings involving Lua functions are used to solve all accesses to global variables within function (see A§2.3). They are utilized due to the fact default conditions for nested Lua performance created by the event.

You can easily change the atmosphere of a Lua features or even the run thread by contacting setfenv . You can get the surroundings of a Lua features or perhaps the running thread by phoning getfenv . To manipulate the environment of www.datingmentor.org/pof-vs-okcupid/ more objects (userdata, C performance, different posts) you have to utilize the C API.

2.10 Garbage Range

Lua runs automatic memory space management. Therefore you must stress neither about allocating memory for new things nor about releasing they once the items are not any lengthier required. Lua handles storage automatically by running a garbage collector every so often to collect all dead stuff (definitely, objects which happen to be no longer obtainable from Lua). All storage employed by Lua are at the mercy of automated management: tables, userdata, performance, threads, strings, etc.

Lua implements an incremental mark-and-sweep enthusiast. They makes use of two numbers to manage their garbage-collection cycles: the garbage-collector pause and garbage-collector action multiplier. Both utilize amount information as devices (in order for a value of 100 indicates an inside property value 1).

À propos de l’auteur

Pro editor

Laisser un commentaire