Like coroutine.create , the coroutine.wrap work in addition brings a coroutine, but instead of coming back the coroutine alone, they comes back a function that, whenever also known as, resumes the coroutine. resume . coroutine.wrap returns most of the principles came back by coroutine.resume , except initial one (the boolean error signal). Unlike coroutine.resume , coroutine.wrap does not catch errors; any error is actually propagated to the person.
This point talks of the C API for Lua, that is, the pair of C functions open to the variety system to communicate with Lua. All API features and relevant kinds and constants tend to be stated in header document lua.h .
Even when we make use of the phase “function”, any premises from inside the API could be provided as a macro as an alternative. All these types of macros use each of their arguments precisely once (with the exception of one discussion, and is constantly a Lua state), and thus do not build any concealed side-effects.
As with more C libraries, the Lua API functionality cannot search their arguments for validity or persistence. However, possible changes this conduct by compiling Lua with an appropriate description for macro luai_apicheck , in file luaconf.h .
3.1 The Stack
Lua makes use of a virtual pile to pass values back and forth C. Each element in this pile presents a Lua benefits (nil, numbers, string, etc.).
When Lua phone calls C, the labeled as purpose becomes another heap, that will be separate of previous stacks as well as stacks of C features which can be however productive. This heap in the beginning have any arguments towards C features and it’s really where the C features pushes their leads to feel returned to the caller (see lua_CFunction ).
For efficiency, the majority of question businesses from inside the API do not stick to a strict heap discipline. Instead, capable refer to any take into account the heap using a list: a confident directory represents an outright heap situation (starting at 1); a negative list signifies an offset relative to the top of the stack. More particularly, if bunch has actually n items, after that list 1 presents the initial component (definitely, the element that was forced on the stack very first) and list letter represents the last factor; index -1 additionally represents the last component (this is certainly, the component towards the top) and index -n presents the initial element. We point out that an index is valid if this consist between 1 together with bunch top (that’s, if 1 a‰¤ abs(index) a‰¤ very top ).
3.2 Bunch Size
Once you connect to Lua API, you happen to be responsible for ensuring consistency. Particularly, you will be responsible for regulating bunch overflow. You can utilize the event lua_checkstack growing the pile dimensions.
Each time Lua calls C, they ensures that at the least LUA_MINSTACK heap spots are available. LUA_MINSTACK is described as 20, so as that typically you don’t have to consider stack space unless your own rule enjoys loops driving areas on the pile.
Most question performance recognize as indices any value within the offered heap room, definitely, indicator up to maximum pile proportions you may have arranged through lua_checkstack . Such indices are known as acceptable indices. More previously, we establish an appropriate list below:
3.3 Pseudo-Indices
Unless if not noted, any work that allows legitimate indices may also be called with pseudo-indices, which express some Lua beliefs that are available to C code but that are not into the pile. Pseudo-indices are used to access the thread environment, the big event conditions, the registry, and the upvalues of a-c purpose (discover A§3.4).
The thread planet (where worldwide variables reside) is always at pseudo-index LUA_GLOBALSINDEX . The environment from the operating C features is definitely at pseudo-index LUA_ENVIRONINDEX .