// cpp
{
lua_getglobal( s, "config" );
lua_getfield( s, -1, "width" );
int v = luaL_checknumber( s, -1 );
}
生成
lua_createtable
SYNTAX
void lua_createtable (lua_State *L, int narr, int nrec);
STACK
[-0, +1, m]
DESC
空のテーブルを作成してスタックに追加。
The new table has space pre-allocated for narr array elements
and nrec non-array elements.
This pre-allocation is useful
when you know exactly how many elements the table will have.
SYNTAX
void lua_setfield (lua_State *L, int index, const char *key);
STACK
[-1, +0, e]
DESC
index で指定したテーブルに key の名前でスタック最上段の値を登録する。
値はポップされる。
table[ key ] = value という設定をする。
Lua の中では, newindex meta method が呼ばれる可能性がある.
Tbl に G を指定すれば, VarGbl にも指定可能.
As in Lua,
this function may trigger a metamethod for the "newindex" event
lua_pushnumber( s, 10 );
lua_setfield( s, -2, "test" );
SYNTAX
void lua_rawgeti (lua_State *s, int idxTbl, int n );
DESC
lua_rawget のキーの値を整数の n に限定した関数。
table[ n ] の結果をスタックへ積む。
( MetaMethod はよばれない )
STACK
[-0, +1, -]
POP
tbl[n];
SYNTAX
void lua_getfield (lua_State *L, int index, const char *k);
desc
index が指す table["k"] の値を stack top へ積む。
key は stack top の値
field 名を指定できるのが point
lua_rawset( s, LUA_ENVIRONINDEX );
POP
KEY の値に対応する VAL
POINT
指定した KEY がない場合は nil を返す。
-->
-------- --------
number
-------- --------
tbl tbl
-------- --------
通常のテーブルと同じだが、
メタテーブルのキーに対して, 関数などを設定することで + などの演算を上書きして
独自の動作をさせることができる
C++ での operator +() と同じ。
Lua の各値はメタテーブルをもつ。
クラスのインスタンスを作ったときに メタテーブルを設定しておくと
+ などで独自の設定ができる。
メタテーブルのキーは getmetatable() でリストできる。
Every value in Lua can have a metatable.
This metatable is an ordinary Lua table that
defines the behavior of the original value
under certain special operations.
You can change several aspects of the behavior of operations over a value
by setting specific fields in its metatable.
For instance,
when a non-numeric value is the operand of an addition,
Lua checks for a function in the field "__add" in its metatable.
If it finds one,
Lua calls this function to perform the addition.
We call the keys in a metatable events and the values metamethods.
In the previous example,
the event is "add" and the metamethod is the function that performs the addition.
You can query the metatable of any value
through the getmetatable function.
You can replace the metatable of tables
through the setmetatable function.
You cannot change the metatable of other types from Lua (except by using the debug library);
you must use the C API for that.
Tables and full userdata have individual metatables
(although multiple tables and userdata can share their metatables).
Values of all other types share one single metatable per type; that is,
there is one single metatable for all numbers, one for all strings, etc.
A metatable controls how an object behaves in arithmetic operations,
order comparisons,
concatenation,
length operation,
and indexing.
A metatable also can define a function to be called
when a userdata is garbage collected.
For each of these operations Lua associates a specific key called an event.
When Lua performs one of these operations over a value,
it checks whether this value has a metatable with the corresponding event.
If so, the value associated with that key (the metamethod) controls
how Lua will perform the operation.
Metatables control the operations listed next.
Each operation is identified by its corresponding name.
The code shown here in Lua is only illustrative;
the real behavior is hard coded in the interpreter
and it is much more efficient than this simulation.
All functions used in these descriptions (rawget, tonumber, etc.) are described in §5.1.
In particular,
to retrieve the metamethod of a given object,
we use the expression
metatable(obj)[event]
lua_setmetatable
SYNTAX
int lua_setmetatable (lua_State *L, int index);
DESC
index で指定したオブジェクトにスタック最上段のテーブルをメタテーブルとして設定する。
最上段のテーブルはPOPされる。
lua_setmetatable( s, -2 );
SYNTAX
int lua_pcall(lua_State *s, int nrArg, int nrRet, int errfunc);
DESC
Protected Mode で関数をコールする。
エラーがなければ, lua_call と同じ処理。
エラーが発生すると、エラーメッセージをスタックへ積みエラーコードを返す。
errfunc で指定したエラーハンドラー関数があればエラーメッセージを引数に呼ばれる。
スタックトレースなどの追加情報をいれるために使う。
RET
0 : 成功
N : 失敗( lua.h )
POINT
失敗した場合は, lua_pop() する
SYNTAX
void lua_getfenv (lua_State *s, int index);
desc
指定した index の値の環境テーブルをスタックに積む
PUSH
指定 index がさす table
lua_setfenv
SYNTAX
int lua_setfenv (lua_State *s, int index);
DESC
index が指す値が 関数, thread, userdata ならば, stacktop の
環境テーブルを新しい環境として set する.
POP
stacktop の EnvTable