A metatable settings how an item acts in arithmetic operations, purchase contrasting, concatenation, duration process, and indexing

A metatable also can determine a function to be called when a userdata try garbage compiled. For each of the functions Lua associates a certain trick labeled as an event. When Lua executes one of these brilliant businesses over a value, it monitors whether this value has a metatable with the matching event. If that’s the case, the value of that secret (the metamethod) controls just how Lua will perform the operation.

Metatables control the procedures noted after that. Each procedure try identified by the matching identity. The key for each procedure is actually a string with its term prefixed by two underscores, ‘ __ ‘; as an example, the main element for procedure “add” may be the string “__add” . The semantics of these businesses is way better demonstrated by a Lua purpose describing how the interpreter executes the process.

The code shown within Lua is illustrative; the true actions is hard coded into the interpreter and it is even more efficient than this representation. All features utilized in these descriptions ( rawget , tonumber , etc.) were defined in A§5.1. In particular, to access the metamethod of a given item, we make use of the term

The big event getbinhandler below defines just how Lua picks a handler for a digital process. 1st, Lua tries the very first operand. If its sort does not determine a handler when it comes down to process, next Lua attempts the next operand.

2.9 Circumstances

Besides metatables, things of kinds thread, purpose, and userdata need another dining table involving all of them, also known as their atmosphere. Like metatables, conditions tend to be routine dining tables and several items can show the same planet.

Posts are created discussing the surroundings for the promoting thread. Userdata and C functions are created sharing the surroundings on the generating C function. Non-nested Lua features (created by loadfile , loadstring or load ) are made discussing the surroundings on the creating thread. Nested Lua functions are created sharing the environmental surroundings regarding the producing Lua features.

Situations associated with userdata haven’t any definition for Lua. It can be a convenience ability for programmers to link a table to a userdata.

Environments related to posts have been called international circumstances. You Can Use Them because standard atmosphere for posts and non-nested Lua functionality escort services in Provo created by the bond and can become directly utilized by C code (discover A§3.3).

Environmental Surroundings associated with a-c work is directly reached by C rule (see A§3.3). Really utilized due to the fact default ecosystem for other C functions and userdata created by the big event.

Surroundings associated with Lua applications are acclimatized to solve all accesses to worldwide variables within work (read A§2.3). They are utilized since the standard planet for nested Lua applications developed by the event.

You can alter the environment of a Lua purpose or even the run bond by phoning setfenv . You could get the environmental surroundings of a Lua function or the operating bond by contacting getfenv . To control environmental surroundings of additional stuff (userdata, C applications, some other posts) you have to make use of the C API.

2.10 Trash Collection

Lua runs automated mind administration. Which means you need to be concerned neither about allocating memory for new things nor about freeing it once the stuff are not any lengthier demanded. Lua controls memory immediately by running a garbage collector from time to time to get all dead things (that will be, stuff that are not accessible from Lua). All memory space employed by Lua was at the mercy of automated control: tables, userdata, functionality, threads, chain, etc.

Lua implements a progressive mark-and-sweep collector. It utilizes two figures to control its garbage-collection rounds: the garbage-collector stop in addition to garbage-collector action multiplier. Both utilize amount details as devices (with the intention that a value of 100 ways an internal property value 1).

About adminjian

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

  • Huddleston Tax CPAs / Huddleston Tax CPAs – Bellevue CPAs
    Certified Public Accountants Focused on Small Business
    40 Lake Bellevue Suite 100 / Bellevue, WA 98005
    (425) 273-6512

    Huddleston Tax CPAs & accountants provide tax preparation, tax planning, business coaching,
    QuickBooks consulting, bookkeeping, payroll, offer in compromise debt relief, and business valuation services for small business.

    We serve: Tukwila, SeaTac, Renton. We have a few meeting locations. Call to meet John C. Huddleston, J.D., LL.M., CPA, Lance Hulbert, CPA, Grace Lee-Choi, CPA, Jennifer Zhou, CPA, or Jessica Chisholm, CPA. Member WSCPA.