When a C work is called, its upvalues are observed at specific pseudo-indices

When a C work is called, its upvalues are observed at specific pseudo-indices

3.4 C Closures

When a C work is generated, you are able to associate some beliefs with it, therefore producing a-c closing; these standards are called upvalues and therefore are accessible to the big event each time it’s called (see lua_pushcclosure ).

These pseudo-indices are manufactured by macro lua_upvalueindex . The initial appreciate associated with a function are at place lua_upvalueindex(1) , etc. Any use of lua_upvalueindex(letter) , in which letter is actually greater than the number of upvalues for the recent features (however more than 256), produces an acceptable (but invalid) list.

3.5 Registry

Lua provides a registry, a pre-defined table that can be used by any C signal to store whatever Lua advantages it must store. This dining table is always found at pseudo-index LUA_REGISTRYINDEX . Any C collection can put data into this desk, nevertheless should take care to pick secrets distinctive from those utilized by other libraries, in order to avoid accidents. Generally, you should utilize as essential a string that contain the collection name or lighting userdata using address of a-c item inside code.

The integer important factors inside registry are employed from the guide system, implemented from the auxiliary collection, and so really should not be used in various other needs.

3.6 Mistake Handling in C

Internally, Lua makes use of the C longjmp premises to carry out errors. (you may also choose to use conditions if you utilize C++; see document luaconf.h .) Whenever Lua faces any mistake (instance memory space allocation errors, type problems, syntax problems, and runtime problems) it raises one; that’s, it does an extended leap. A protected conditions uses setjmp to create a recover aim; any mistake jumps with the newest productive heal aim.

More features into the API can put a mistake, for-instance due to a memory space allotment mistake. The paperwork for every features indicates whether or not it can toss problems.

3.7 Functions and Type

Here we identify all features and sort from C API in alphabetical order. Each features features indicative such as this: [-o, +p, x]

1st field, o , try exactly how many elements the function pops from stack. The 2nd industry http://datingmentor.org/police-chat-rooms/, p , is what amount of details the big event forces on the pile. (Any function always forces its information after swallowing the arguments.) A field for the form x|y ways the event can drive (or pop) x or y items, according to the situation; an interrogation mark ‘ ? ‘ means that we simply cannot know-how numerous characteristics the big event pops/pushes by lookin merely at its arguments (elizabeth.g., they could rely on something on heap). The next area, x , informs whether the function may throw mistakes: ‘ – ‘ means the big event never throws any error; ‘ m ‘ indicates the event may put one just as a result of insufficient memory space; ‘ elizabeth ‘ means the function may throw other forms of problems; ‘ v ‘ ways the event may put an error deliberately.

lua_Alloc

The type of the memory-allocation purpose employed by Lua reports. The allocator function must make provision for a functionality like realloc , but not exactly the same. The arguments tend to be ud , an opaque pointer passed away to lua_newstate ; ptr , a pointer towards the block are allocated/reallocated/freed; osize , the initial size of the block; nsize , brand new measurements of the block. ptr is actually NULL if and simply if osize is zero. When nsize is actually zero, the allocator must come back NULL ; if osize just isn’t zero, it will release the block pointed to by ptr . When nsize is certainly not zero, the allocator return NULL if and simply when it cannot complete the consult. When nsize isn’t zero and osize is zero, the allocator should respond love malloc . Whenever nsize and osize aren’t zero, the allocator acts like realloc . Lua assumes that the allocator never fails when osize >= nsize .