<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>エスブレイン &#187; Lua</title>
	<atom:link href="http://www.esbrain.com/category/%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9%e3%83%a0%e5%b0%8f%e6%8a%80/lua-%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9%e3%83%a0%e5%b0%8f%e6%8a%80/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.esbrain.com</link>
	<description>高い技術力・創造力・機動力でお客様のビジネスをさらに加速させます</description>
	<lastBuildDate>Wed, 25 Aug 2010 07:34:27 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://www.esbrain.com/category/%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9%e3%83%a0%e5%b0%8f%e6%8a%80/lua-%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9%e3%83%a0%e5%b0%8f%e6%8a%80/feed/" />
		<item>
		<title>luaで共有ライブラリを作成する方法</title>
		<link>http://www.esbrain.com/2010/05/lua%e3%81%a7%e5%85%b1%e6%9c%89%e3%83%a9%e3%82%a4%e3%83%96%e3%83%a9%e3%83%aa%e3%82%92%e4%bd%9c%e6%88%90%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://www.esbrain.com/2010/05/lua%e3%81%a7%e5%85%b1%e6%9c%89%e3%83%a9%e3%82%a4%e3%83%96%e3%83%a9%e3%83%aa%e3%82%92%e4%bd%9c%e6%88%90%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95/#comments</comments>
		<pubDate>Mon, 24 May 2010 03:33:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[liblua5.1.so]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[コンパイル]]></category>
		<category><![CDATA[共有ライブラリ]]></category>

		<guid isPermaLink="false">http://www.esbrain.com/?p=748</guid>
		<description><![CDATA[Luaバイナリをソースファイルからコンパイルして作成するとき、
標準では静的ライブラリが生成されます。
通常Luaを組み込み用途で使用するときは静的ライブラリで十分なのですが、
時には共有ライブラリが欲しくなるので、作成 [...]]]></description>
			<content:encoded><![CDATA[<p>Luaバイナリをソースファイルからコンパイルして作成するとき、</p>
<p>標準では静的ライブラリが生成されます。</p>
<p>通常Luaを組み込み用途で使用するときは静的ライブラリで十分なのですが、</p>
<p>時には共有ライブラリが欲しくなるので、作成してみます。</p>
<p>(lua5.1を想定しています)</p>
<blockquote><p>src/Makefileに以下を追加する。</p>
<p>LUA_O=lua.oと書かれた行の下に、以下の行を挿入する。</p>
<p>LUA_SO=liblua5.1.so</p>
<p>次の行の最後に、$(LUA_SO)を追記する。</p>
<p>ALL_T=$(LUA_A) $(LUA_T) $(LUAC_T)</p>
<p>↓</p>
<p>ALL_T=$(LUA_A) $(LUA_T) $(LUAC_T) $(LUA_SO)</p>
<p>clean:と書かれた行の前に、以下の2行を挿入する。</p>
<p>（2行の前後には1行の空行があるようにする）</p>
<div id="_mcePaste">$(LUA_SO): $(CORE_O) $(LIB_O)</div>
<div id="_mcePaste">$(CC) -o $@ -shared $?</div>
<p>保存する。</p>
<p>次にMakefileを下記のように編集する。</p>
<p>TO_LIB= liblua.aとなっている行の最後に liblua5.1.soを追記する。</p>
<p>↓</p>
<p>TO_LIB=liblua.a liblua5.1.so</p></blockquote>
<p>これで、makeするとliblua5.1.soが生成されます。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.esbrain.com/2010/05/lua%e3%81%a7%e5%85%b1%e6%9c%89%e3%83%a9%e3%82%a4%e3%83%96%e3%83%a9%e3%83%aa%e3%82%92%e4%bd%9c%e6%88%90%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://www.esbrain.com/2010/05/lua%e3%81%a7%e5%85%b1%e6%9c%89%e3%83%a9%e3%82%a4%e3%83%96%e3%83%a9%e3%83%aa%e3%82%92%e4%bd%9c%e6%88%90%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95/" />
	</item>
		<item>
		<title>Luaの配列展開</title>
		<link>http://www.esbrain.com/2009/06/lua%e3%81%ae%e9%85%8d%e5%88%97%e5%b1%95%e9%96%8b/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://www.esbrain.com/2009/06/lua%e3%81%ae%e9%85%8d%e5%88%97%e5%b1%95%e9%96%8b/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 11:38:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Lua]]></category>

		<guid isPermaLink="false">http://www.esbrain.com/?p=538</guid>
		<description><![CDATA[前回call_user_func関数の実装を行いました。
かなり便利になったのですが、すぐに1点の曇りが見つかりました。
そう、call_user_funcの場合、引数は直接指定しないといけないのです。
この手の関数を使 [...]]]></description>
			<content:encoded><![CDATA[<p>前回call_user_func関数の実装を行いました。</p>
<p>かなり便利になったのですが、すぐに1点の曇りが見つかりました。</p>
<p>そう、call_user_funcの場合、引数は直接指定しないといけないのです。</p>
<p>この手の関数を使う場面では、引数もあらかじめ配列に格納しておき、呼び出すことが多いのです。</p>
<p>例えば、以下のような関数が定義されていたとします。</p>
<pre class="brush: cpp;">
function funcA(a,b,c)
        print(a,b,c)
end
</pre>
<p>これを普通に呼び出すのは簡単</p>
<pre>
funcA("a", "b", "c")

実行結果
a	b	c
</pre>
<p>しかし、とあるケースではこの引数を配列にしておきたいときがあります。</p>
<pre>
args = {"a", "b", "c"}
</pre>
<p>さて、funcAをどう呼び出すのか?</p>
<pre>
funcA(args)

実行結果
table: 0x806f168	nil	nil
</pre>
<p>当然ですが、第1引数にテーブルの参照が渡っているのです。</p>
<p>お察しのよろしい方は、タイトルでピンとくるでしょう。</p>
<p>そう、unpack関数があるのです。</p>
<pre>
funcA(unpack(args))

実行結果
a	b	c
</pre>
<p>あっという間に解決しました。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.esbrain.com/2009/06/lua%e3%81%ae%e9%85%8d%e5%88%97%e5%b1%95%e9%96%8b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://www.esbrain.com/2009/06/lua%e3%81%ae%e9%85%8d%e5%88%97%e5%b1%95%e9%96%8b/" />
	</item>
		<item>
		<title>Luaのdestructor(デストラクタ)とGC</title>
		<link>http://www.esbrain.com/2009/06/lua%e3%81%aedestructor%e3%83%87%e3%82%b9%e3%83%88%e3%83%a9%e3%82%af%e3%82%bf%e3%81%a8gc/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://www.esbrain.com/2009/06/lua%e3%81%aedestructor%e3%83%87%e3%82%b9%e3%83%88%e3%83%a9%e3%82%af%e3%82%bf%e3%81%a8gc/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 10:29:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Lua]]></category>

		<guid isPermaLink="false">http://www.esbrain.com/?p=530</guid>
		<description><![CDATA[LuaにはGCが付いていますので、使われなくなったメモリは回収されます。
強制的に呼び出すのは簡単で、回収したいところで
collectgarbage(&#8220;collect&#8221;)
とするだけです。
C+ [...]]]></description>
			<content:encoded><![CDATA[<p>LuaにはGCが付いていますので、使われなくなったメモリは回収されます。</p>
<p>強制的に呼び出すのは簡単で、回収したいところで</p>
<p>collectgarbage(&#8220;collect&#8221;)</p>
<p>とするだけです。</p>
<p>C++ではクラスインスタンスの解放時にデストラクタが呼ばれます。</p>
<p>自前のリソースの解放には便利です。</p>
<p>さっそく、Luaの自作クラスでやってみます。</p>
<p>何やらメタテーブルに__gcメソッドを書けばよろしいようで、簡単簡単。</p>
<pre class="brush: cpp;">
function destruct()
        print(&quot;destructor called&quot;)
end

t = {}
setmetatable(t, {__gc = destruct})

t = nil
collectgarbage(&quot;collect&quot;)
print(&quot;end&quot;)
</pre>
<p>実行してみたものの、endしか表示されず、destruct関数が呼ばれない・・・</p>
<p>仕様を見てみると、ユーザデータ（テーブルではない）のメタテーブルに対して</p>
<p>__gcメソッドを定義しないといけない模様・・・</p>
<p>ユーザデータを作ってくれるnewproxy関数を発見！</p>
<p>この関数は非公開メソッドのようです。</p>
<p>この関数の引数がtrueなら、新しいメタテーブルを内包するユーザデータを作ります。</p>
<p>引数が既に作成してあるユーザデータなら、そのユーザデータに含まれるメタテーブルをコピーします。</p>
<p>先ほどのプログラムを次のように変更します。</p>
<pre class="brush: cpp;">
function destruct()
        print(&quot;destructor called&quot;)
end

t = {_udata = newproxy(true) }
setmetatable(t._udata, {__gc = destruct})
t = nil
collectgarbage(&quot;collect&quot;)
print(&quot;end&quot;)
</pre>
<p>うまくいくだろうと思いきや、Luaが怒りました。</p>
<blockquote><p>
bad argument #1 to &#8216;setmetatable&#8217; (table expected, got userdata)
</p></blockquote>
<p>つまり、setmetatable関数にはテーブルしか指定できないようです。</p>
<p>また調べると、getmetatable関数はユーザデータでも構わないとののこと。</p>
<p>もう一度プログラムを書き換えてみました。</p>
<pre class="brush: cpp;">
function destruct()
        print(&quot;destructor called&quot;)
end

t = {_udata = newproxy(true) }
getmetatable(t._udata).__gc = destruct
t = nil
collectgarbage(&quot;collect&quot;)
print(&quot;end&quot;)
</pre>
<p>実行してみると・・・無事デストラクタが呼び出されました。</p>
<pre>
destructor called
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.esbrain.com/2009/06/lua%e3%81%aedestructor%e3%83%87%e3%82%b9%e3%83%88%e3%83%a9%e3%82%af%e3%82%bf%e3%81%a8gc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://www.esbrain.com/2009/06/lua%e3%81%aedestructor%e3%83%87%e3%82%b9%e3%83%88%e3%83%a9%e3%82%af%e3%82%bf%e3%81%a8gc/" />
	</item>
		<item>
		<title>Luaでcall_user_func 〜 クラスメソッドを変数で呼び出したい</title>
		<link>http://www.esbrain.com/2009/06/lua%e3%81%a7call_user_func-%e3%80%9c-%e3%82%af%e3%83%a9%e3%82%b9%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%82%92%e5%a4%89%e6%95%b0%e3%81%a7%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%97%e3%81%9f%e3%81%84/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://www.esbrain.com/2009/06/lua%e3%81%a7call_user_func-%e3%80%9c-%e3%82%af%e3%83%a9%e3%82%b9%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%82%92%e5%a4%89%e6%95%b0%e3%81%a7%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%97%e3%81%9f%e3%81%84/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 09:51:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[call_user_func]]></category>
		<category><![CDATA[lua]]></category>

		<guid isPermaLink="false">http://www.esbrain.com/?p=526</guid>
		<description><![CDATA[PHPのcall_user_func関数って便利です。
イベント駆動系のプログラムを書くときには必須です。
そんな関数がLuaにはない・・・　（見つけられないだけかも知れませんが）
ということで、試作してみました。

f [...]]]></description>
			<content:encoded><![CDATA[<p>PHPのcall_user_func関数って便利です。</p>
<p>イベント駆動系のプログラムを書くときには必須です。</p>
<p>そんな関数がLuaにはない・・・　（見つけられないだけかも知れませんが）</p>
<p>ということで、試作してみました。</p>
<pre class="brush: cpp;">
function call_user_func(func, ...)
        local t = type(func)
        if t == 'function' then
                return func(...)
        elseif t == 'string' then
                if _G[func] == nil or type(_G[func]) ~= 'function' then
                        error(&quot;function is not defined '&quot;..func..&quot;'&quot;)
                end
                return _G[func](...)
        elseif t == 'table' then
                local _instance = func[1]
                local _method = func[2]
                if _instance == nil or _method == nil then
                        error(&quot;instance or method name is nil&quot;)
                end
                if _instance[_method] == nil then
                        error(&quot;class method is not defined '&quot;.._method..&quot;'&quot;)
                end
                return _instance[_method](_instance, ...)
        else
                error(&quot;func is not matched type '&quot;..type(func)..&quot;'&quot;)
        end
end

function funcA(name)
        print(&quot;My name is &quot;..name)
end

Class = {}

function Class:new(name)
        local t = {name = name}
        setmetatable(t, {__index = Class})
        return t
end

function Class:funcA()
        print(&quot;I am &quot;..self.name)
end

function Class:funcB(...)
        self:funcA()
        print(&quot;data are &quot;..table.concat({...}, &quot;,&quot;))
end

a = Class:new(&quot;taro&quot;)
a:funcA()
a:funcB(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;)

call_user_func({a, &quot;funcB&quot;}, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;)
call_user_func(funcA, &quot;jiro&quot;)
call_user_func(&quot;funcA&quot;, &quot;saburo&quot;)
</pre>
<p>実行結果は正常</p>
<pre>
I am taro
I am taro
data are 1,2,3
I am taro
data are a,b,c
My name is jiro
My name is saburo
</pre>
<p>19行目の_instance[_method](_instance, &#8230;)が肝です。</p>
<p>メソッドの第1引数はインスタンスを格納しなければならないオキテがあります。</p>
<p>よって、直接引数を&#8230;にしてしまうと、おかしなことになりますから注意！</p>
]]></content:encoded>
			<wfw:commentRss>http://www.esbrain.com/2009/06/lua%e3%81%a7call_user_func-%e3%80%9c-%e3%82%af%e3%83%a9%e3%82%b9%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%82%92%e5%a4%89%e6%95%b0%e3%81%a7%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%97%e3%81%9f%e3%81%84/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://www.esbrain.com/2009/06/lua%e3%81%a7call_user_func-%e3%80%9c-%e3%82%af%e3%83%a9%e3%82%b9%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%82%92%e5%a4%89%e6%95%b0%e3%81%a7%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%97%e3%81%9f%e3%81%84/" />
	</item>
	</channel>
</rss>
