local h
if type(table) == "table" then
local v = rawget(table, key)
if v ~= nil then rawset(table, key, value); return end
h = metatable(table).__newindex
if h == nil then rawset(table, key, value); return end
else
h = metatable(table).__newindex
if h == nil then
error(...);
end
end
if type(h) == "function" then
return h(table, key,value) -- call the handler
else h[key] = value -- or repeat operation on it
end
<
"call": *__call()*
-------
called when Lua calls a value.
>lua
function function_event (func, ...)
if type(func) == "function" then
return func(...) -- primitive call
else
local h = metatable(func).__call
if h then
return h(func, ...)
else
error(...)
end
end
end
<
==============================================================================
2.9 Environments *lua-environments*
Besides metatables, objects of types thread, function, and userdata have
another table associated with them, called their environment. Like metatables,
environments are regular tables and multiple objects can share the same
environment.
Environments associated with userdata have no meaning for Lua. It is only a
convenience feature for programmers to associate a table to a userdata.
Environments associated with threads are called global environments. They are
used as the default environment for their threads and non-nested functions
created by the thread (through |loadfile()|, |loadstring()| or |load()|) and
can be directly accessed by C code (see |lua-pseudoindex|).
Environments associated with C functions can be directly accessed by C code
(see |lua-pseudoindex|). They are used as the default environment for
other C functions created by the function.
Environments associated with Lua functions are used to resolve all accesses to
global variables within the function (see |lua-variables|). They are
used as the default environment for other Lua functions created by the
function.
You can change the environment of a Lua function or the running thread by
calling `setfenv`. You can get the environment of a Lua function or the
running thread by calling `getfenv` (see |lua_getfenv()|). To manipulate the
environment of other objects (userdata, C functions, other threads) you must
use the C API.
==============================================================================
2.10 Garbage Collection *lua-gc*
Lua performs automatic memory management. This means that you do not have to
worry neither about allocating memory for new objects nor about freeing it
when the objects are no longer needed. Lua manages memory automatically by
running a garbage collector from time to time to collect all dead objects
(that is, these objects that are no longer accessible from Lua). All objects
in Lua are subject to automatic management: tables, userdata, functions,
threads, and strings.
Lua implements an incremental mark-and-sweep collector. It uses two numbers to
control its garbage-collection cycles: the garbage-collector pause and the
garbage-collector step multiplier.
The garbage-collector pause controls how long the collector waits before
starting a new cycle. Larger values make the collector less aggressive. Values
smaller than 1 mean the collector will not wait to start a new cycle. A value
of 2 means that the collector waits for the total memory in use to double
before starting a new cycle.
The step multiplier controls the relative speed of the collector relative to
memory allocation. Larger values make the collector more aggressive but also
increase the size of each incremental step. Values smaller than 1 make the
collector too slow and