一.Hello World
1.前言
偶最近在學(xué)習(xí)Lua, 所以寫(xiě)出心得和大家共享, 爭(zhēng)取一天寫(xiě)一篇, 嘿嘿.
才開(kāi)始學(xué)所以?xún)?nèi)容很淺, 希望大家包涵.
Lua是一種完全免費(fèi)的腳本語(yǔ)言, 可以和C/C++語(yǔ)言緊密結(jié)合,
它的官方網(wǎng)站在http://www.lua.org. 在網(wǎng)站上可以下載到lua的源碼, 沒(méi)有可
執(zhí)行版本, 不過(guò)不用擔(dān)心, 因?yàn)閘ua源碼可以在任何一種C/C++的編譯器上編譯.
如果要學(xué)習(xí)Lua, 官方網(wǎng)站上的Reference是必備的,上面有每個(gè)命令的用法,非常詳細(xì)。
參考手冊(cè) http://www.lua.org/manual/5.0/
作者寫(xiě)的Programming in Lua http://www.lua.org/pil/
2.編譯
如果用的VC6, 可以下載所需的project文件,地址在
http://sourceforge.net/project/showfiles.php?group_id=32250&package_id=115604
VSNET2003可以下載這個(gè)sln文件http://home.comcast.net/~vertigrated/lua/vs7.zip
偶用的是cygwin和linux, 打入以下命令即可,
tar -zxvf lua-5.0.2.tar.gz
cd lua-5.0.2
sh ./configure
make
這樣就OK了。
為了以后使用方便,最好把bin目錄加入到path里面。
3."Hello, world!"
現(xiàn)在開(kāi)始偶們的第一個(gè)小程序"Hello, world!"
把以下程序打入文件e01.lua
例1:e01.lua
-- Hello World in Lua
print("Hello World.")
Lua有兩種執(zhí)行方式,一種是嵌入到C程序中執(zhí)行,還有一種是直接從命令行方式下執(zhí)行。
這里為了調(diào)試方便,采用第二種方式,執(zhí)行命令 lua e01.lua
輸出結(jié)果應(yīng)該是:
Hello World.
4.程序說(shuō)明
第一行 -- Hello World in Lua
這句是注釋?zhuān)渲?-和C++中的//意思是一樣的
第二行 print("Hello World.")
調(diào)用lua內(nèi)部命令print,輸出"Hello World."字符串到屏幕,Lua中的字符串全部是由"括起來(lái)的。
這個(gè)命令是一個(gè)函數(shù)的調(diào)用,print是lua的一個(gè)函數(shù),而"Hello World."是print的參數(shù)。
5.試試看
在Lua中有不少字符串的處理操作,本次的課后試試看的內(nèi)容就是,找出連接兩個(gè)字符串的操作,
并且print出來(lái)。
二.流程控制
1. 函數(shù)的使用
以下程序演示了如何在Lua中使用函數(shù), 及局部變量
例e02.lua
-- functions
function pythagorean(a, b)
local c2 = a^2 + b^2
return sqrt(c2)
end
print(pythagorean(3,4))
運(yùn)行結(jié)果
5
程序說(shuō)明
在Lua中函數(shù)的定義格式為:
function 函數(shù)名(參數(shù))
...
end
與Pascal語(yǔ)言不同, end不需要與begin配對(duì), 只需要在函數(shù)結(jié)束后打個(gè)end就可以了.
本例函數(shù)的作用是已知直角三角形直角邊, 求斜邊長(zhǎng)度. 參數(shù)a,b分別表示直角邊長(zhǎng),
在函數(shù)內(nèi)定義了local形變量用于存儲(chǔ)斜邊的平方. 與C語(yǔ)言相同, 定義在函數(shù)內(nèi)的代
碼不會(huì)被直接執(zhí)行, 只有主程序調(diào)用時(shí)才會(huì)被執(zhí)行.
local表示定義一個(gè)局部變量, 如果不加local剛表示c2為一個(gè)全局變量, local的作用域
是在最里層的end和其配對(duì)的關(guān)鍵字之間, 如if ... end, while ... end等。全局變量的
作用域是整個(gè)程序。
2. 循環(huán)語(yǔ)句
例e03.lua
-- Loops
for i=1,5 do
print("i is now " .. i)
end
運(yùn)行結(jié)果
i is now 1
i is now 2
i is now 3
i is now 4
i is now 5
程序說(shuō)明
這里偶們用到了for語(yǔ)句
for 變量 = 參數(shù)1, 參數(shù)2, 參數(shù)3 do
循環(huán)體
end
變量將以參數(shù)3為步長(zhǎng), 由參數(shù)1變化到參數(shù)2
例如:
for i=1,f(x) do print(i) end
for i=10,1,-1 do print(i) end
這里print("i is now " .. i)中,偶們用到了..,這是用來(lái)連接兩個(gè)字符串的,
偶在(1)的試試看中提到的,不知道你們答對(duì)了沒(méi)有。
雖然這里i是一個(gè)整型量,Lua在處理的時(shí)候會(huì)自動(dòng)轉(zhuǎn)成字符串型,不需偶們費(fèi)心。
3. 條件分支語(yǔ)句
例e04.lua
-- Loops and conditionals
for i=1,5 do
print(“i is now “ .. i)
if i < 2 then
print(“small”)
elseif i < 4 then
print(“medium”)
else
print(“big”)
end
end
運(yùn)行結(jié)果
i is now 1
small
i is now 2
medium
i is now 3
medium
i is now 4
big
i is now 5
big
程序說(shuō)明
if else用法比較簡(jiǎn)單, 類(lèi)似于C語(yǔ)言, 不過(guò)此處需要注意的是整個(gè)if只需要一個(gè)end,
哪怕用了多個(gè)elseif, 也是一個(gè)end.
例如
if op == "+" then
r = a + b
elseif op == "-" then
r = a - b
elseif op == "*" then
r = a*b
elseif op == "/" then
r = a/b
else
error("invalid operation")
end
4.試試看
Lua中除了for循環(huán)以外, 還支持多種循環(huán), 請(qǐng)用while...do和repeat...until改寫(xiě)本文中的for程序
三.Lua數(shù)據(jù)結(jié)構(gòu)
1.簡(jiǎn)介
Lua語(yǔ)言只有一種基本數(shù)據(jù)結(jié)構(gòu), 那就是table, 所有其他數(shù)據(jù)結(jié)構(gòu)如數(shù)組啦,
類(lèi)啦, 都可以由table實(shí)現(xiàn).
例e05.lua
-- Arrays
myData = {}
myData[0] = “foo”
myData[1] = 42
-- Hash tables
myData[“bar”] = “baz”
-- Iterate through the
-- structure
for key, value in myData do
print(key .. “=“ .. value)
end
輸出結(jié)果
0=foo
1=42
bar=baz
程序說(shuō)明
首先定義了一個(gè)table myData={}, 然后用數(shù)字作為下標(biāo)賦了兩個(gè)值給它. 這種
定義方法類(lèi)似于C中的數(shù)組, 但與數(shù)組不同的是, 每個(gè)數(shù)組元素不需要為相同類(lèi)型,
就像本例中一個(gè)為整型, 一個(gè)為字符串.
程序第二部分, 以字符串做為下標(biāo), 又向table內(nèi)增加了一個(gè)元素. 這種table非常
像STL里面的map. table下標(biāo)可以為L(zhǎng)ua所支持的任意基本類(lèi)型, 除了nil值以外.
Lua對(duì)Table占用內(nèi)存的處理是自動(dòng)的, 如下面這段代碼
a = {}
a["x"] = 10
b = a -- `b' refers to the same table as `a'
print(b["x"]) --> 10
b["x"] = 20
print(a["x"]) --> 20
a = nil -- now only `b' still refers to the table
b = nil -- now there are no references left to the table
b和a都指向相同的table, 只占用一塊內(nèi)存, 當(dāng)執(zhí)行到a = nil時(shí), b仍然指向table,
而當(dāng)執(zhí)行到b=nil時(shí), 因?yàn)闆](méi)有指向table的變量了, 所以Lua會(huì)自動(dòng)釋放table所占內(nèi)存
3.Table的嵌套
Table的使用還可以嵌套,如下例
例e06.lua
-- Table ‘constructor’
myPolygon = {
color=“blue”,
thickness=2,
npoints=4;
{x=0, y=0},
{x=-10, y=0},
{x=-5, y=4},
{x=0, y=4}
}
-- Print the color
print(myPolygon[“color”])
-- Print it again using dot
-- notation
print(myPolygon.color)
-- The points are accessible
-- in myPolygon[1] to myPolygon[4]
-- Print the second point’s x
-- coordinate
print(myPolygon[2].x)
程序說(shuō)明
首先建立一個(gè)table, 與上一例不同的是,在table的constructor里面有{x=0,y=0},
這是什么意思呢? 這其實(shí)就是一個(gè)小table, 定義在了大table之內(nèi), 小table的
table名省略了.
最后一行myPolygon[2].x,就是大table里面小table的訪問(wèn)方式.
四.函數(shù)的調(diào)用
1.不定參數(shù)
例e07.lua
-- Functions can take a
-- variable number of
-- arguments.
function funky_print (...)
for i=1, arg.n do
print("FuNkY: " .. arg[i])
end
end
funky_print("one", "two")
運(yùn)行結(jié)果
FuNkY: one
FuNkY: two
程序說(shuō)明
* 如果以...為參數(shù), 則表示參數(shù)的數(shù)量不定.
* 參數(shù)將會(huì)自動(dòng)存儲(chǔ)到一個(gè)叫arg的table中.
* arg.n中存放參數(shù)的個(gè)數(shù). arg[]加下標(biāo)就可以遍歷所有的參數(shù).
2.以table做為參數(shù)
例e08.lua
-- Functions with table
-- parameters
function print_contents(t)
for k,v in t do
print(k .. "=" .. v)
end
end
print_contents{x=10, y=20}
運(yùn)行結(jié)果
x=10
y=20
程序說(shuō)明
* print_contents{x=10, y=20}這句參數(shù)沒(méi)加圓括號(hào), 因?yàn)橐詥蝹€(gè)table為參數(shù)的時(shí)候, 不需要加圓括號(hào)
* for k,v in t do 這個(gè)語(yǔ)句是對(duì)table中的所有值遍歷, k中存放名稱(chēng), v中存放值
3.把Lua變成類(lèi)似XML的數(shù)據(jù)描述語(yǔ)言
例e09.lua
function contact(t)
-- add the contact ‘t’, which is
-- stored as a table, to a database
end
contact {
name = "Game Developer",
email = "hack@ogdev.net",
url = "http://www.ogdev.net",
quote = [[
There are
10 types of people
who can understand binary.]]
}
contact {
-- some other contact
}
程序說(shuō)明
* 把function和table結(jié)合, 可以使Lua成為一種類(lèi)似XML的數(shù)據(jù)描述語(yǔ)言
* e09中contact{...}, 是一種函數(shù)的調(diào)用方法, 不要弄混了
* [[...]]是表示多行字符串的方法
* 當(dāng)使用C API時(shí)此種方式的優(yōu)勢(shì)更明顯, 其中contact{..}部分可以另外存成一配置文件
4.試試看
想想看哪些地方可以用到例e09中提到的配置方法呢?
五.Lua與C的交互
1.簡(jiǎn)介
Lua與C/C++結(jié)合是很緊密的, Lua與C++交互是建立在Lua與C的基礎(chǔ)上的, 所
以偶先從Lua與C講起.
正如第一講所說(shuō), 運(yùn)行Lua程序或者說(shuō)調(diào)用Lua主要有兩種方式:
* 通過(guò)命令行執(zhí)行"Lua"命令
* 通過(guò)Lua的C庫(kù)
雖然此前偶們一直用第一種方式, 但偶要告訴你, 通過(guò)Lua的C庫(kù)執(zhí)行才是游戲中
常用的方式.
2.Lua的C庫(kù)
Lua的C庫(kù)可以做為Shared Library調(diào)用, 但一般開(kāi)發(fā)游戲時(shí)會(huì)把Lua的所有源程序
都包含在內(nèi), 并不把Lua編譯成共享庫(kù)的形式. 因?yàn)長(zhǎng)ua程序只有100多K, 而且?guī)缀?br /> 可以在任何編譯器下Clean Compile. 帶Lua源程序的另一個(gè)好處時(shí), 可以隨時(shí)對(duì)Lua
本身進(jìn)行擴(kuò)充, 增加偶們所需的功能.
Lua的C庫(kù)提供一系列API:
* 管理全局變量
* 管理tables
* 調(diào)用函數(shù)
* 定義新函數(shù), 這也可以完全由C實(shí)現(xiàn)
* 垃圾收集器Garbage collector, 雖然Lua可以自動(dòng)進(jìn)行, 但往往不是立即執(zhí)行的,
所以對(duì)實(shí)時(shí)性要求比較高的程序, 會(huì)自己調(diào)用垃圾收集器
* 載入并執(zhí)行Lua程序, 這也可以由Lua自身實(shí)現(xiàn)
* 任何Lua可以實(shí)現(xiàn)的功能, 都可以通過(guò)Lua的C API實(shí)現(xiàn), 這對(duì)于優(yōu)化程序的運(yùn)行速度
有幫助. 經(jīng)常調(diào)用的共用的Lua程序片斷可以轉(zhuǎn)成C程序, 以提高效率. 連Lua都是C寫(xiě)的
還有什么C不能實(shí)現(xiàn)呢?
3.Lua與C集成的例子
例e10.c
/* A simple Lua interpreter. */
#include
#include
int main(int argc, char *argv[]) {
char line[BUFSIZ];
lua_State *L = lua_open(0);
while (fgets(line, sizeof(line), stdin) != 0)
lua_dostring(L, line);
lua_close(L);
return 0;
}
編譯
Linux/Cygwin
* 先編譯Lua, 并把頭文件放入include路徑
* gcc e10.c -llua -llualib -o e10
VC6/VC2003
* 先編譯Lua, 在Option中設(shè)置頭文件和庫(kù)文件路徑
* 新建工程,在工程配置中加入附加庫(kù)lua.lib和lualib.lib
* 編譯成exe
運(yùn)行結(jié)果
本程序的功能是實(shí)現(xiàn)一個(gè)Lua解釋器, 輸入的每行字符都會(huì)被解釋成Lua并執(zhí)行.
程序說(shuō)明
* #include 包含lua頭文件, 然后才可以使用API
* lua_State *L = lua_open(0) 打開(kāi)一個(gè)Lua執(zhí)行器
* fgets(line, sizeof(line), stdin) 從標(biāo)準(zhǔn)輸入里讀入一行
* lua_dostring(L, line) 執(zhí)行此行
* lua_close(L) 關(guān)閉Lua執(zhí)行器
例e11.c
/* Another simple Lua interpreter. */
#include
#include
#include
int main(int argc, char *argv[]) {
char line[BUFSIZ];
lua_State *L = lua_open(0);
lua_baselibopen(L);
lua_iolibopen(L);
lua_strlibopen(L);
lua_mathlibopen(L);
while (fgets(line, sizeof(line), stdin) != 0)
lua_dostring(L, line);
lua_close(L);
return 0;
}
運(yùn)行結(jié)果
本程序的功能是實(shí)現(xiàn)一個(gè)Lua解釋器, 輸入的每行字符都會(huì)被解釋成Lua并執(zhí)行.
與上例不同的是, 本例調(diào)用了Lua的一些標(biāo)準(zhǔn)庫(kù).
程序說(shuō)明
* #include 包含Lua的標(biāo)準(zhǔn)庫(kù)
* 以下這幾行是用來(lái)讀入Lua的一些庫(kù), 這樣偶們的Lua程序就可以有更多的功能.
lua_baselibopen(L);
lua_iolibopen(L);
lua_strlibopen(L);
lua_mathlibopen(L);
4.試試看
把上面兩個(gè)小例子在你熟悉的編譯器中編譯執(zhí)行, 并試試能否與Lua源碼樹(shù)一起編譯
六.C/C++中用Lua函數(shù)
1.簡(jiǎn)介
偶們這次主要說(shuō)說(shuō)怎么由Lua定義函數(shù), 然后在C或者C++中調(diào)用. 這里偶們
暫不涉及C++的對(duì)象問(wèn)題, 只討論調(diào)用函數(shù)的參數(shù), 返回值和全局變量的使用.
2.程序
這里偶們?cè)趀12.lua里先定義一個(gè)簡(jiǎn)單的add(), x,y為加法的兩個(gè)參數(shù),
return 直接返回相加后的結(jié)果.
例e12.lua
-- add two numbers
function add ( x, y )
return x + y
end
在前一次里, 偶們說(shuō)到 lua_dofile() 可以直接在C中執(zhí)行l(wèi)ua文件. 因?yàn)榕紓?br /> 這個(gè)程序里只定義了一個(gè)add()函數(shù), 所以程序執(zhí)行后并不直接結(jié)果, 效果相當(dāng)
于在C中定義了一個(gè)函數(shù)一樣.
Lua的函數(shù)可以有多個(gè)參數(shù), 也可以有多個(gè)返回值, 這都是由棧(stack)實(shí)現(xiàn)的.
需要調(diào)用一個(gè)函數(shù)時(shí), 就把這個(gè)函數(shù)壓入棧, 然后順序壓入所有參數(shù), 然后用
lua_call()調(diào)用這個(gè)函數(shù). 函數(shù)返回后, 返回值也是存放在棧中. 這個(gè)過(guò)程和
匯編執(zhí)行函數(shù)調(diào)用的過(guò)程是一樣的.
例e13.cpp 是一個(gè)調(diào)用上面的Lua函數(shù)的例子
#include
extern "C" { // 這是個(gè)C++程序, 所以要extern "C",
// 因?yàn)閘ua的頭文件都是C格式的
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
/* the Lua interpreter */
lua_State* L;
int luaadd ( int x, int y )
{
int sum;
/* the function name */
lua_getglobal(L, "add");
/* the first argument */
lua_pushnumber(L, x);
/* the second argument */
lua_pushnumber(L, y);
/* call the function with 2
arguments, return 1 result */
lua_call(L, 2, 1);
/* get the result */
sum = (int)lua_tonumber(L, -1);
lua_pop(L, 1);
return sum;
}
int main ( int argc, char *argv[] )
{
int sum;
/* initialize Lua */
L = lua_open();
/* load Lua base libraries */
lua_baselibopen(L);
/* load the script */
lua_dofile(L, "e12.lua");
/* call the add function */
sum = luaadd( 10, 15 );
/* print the result */
printf( "The sum is %d\n", sum );
/* cleanup Lua */
lua_close(L);
return 0;
}
程序說(shuō)明:
main中過(guò)程偶們上次已經(jīng)說(shuō)過(guò)了, 所以這次只說(shuō)說(shuō)luaadd的過(guò)程
* 首先用lua_getglobal()把a(bǔ)dd函數(shù)壓棧
* 然后用lua_pushnumber()依次把x,y壓棧
* 然后調(diào)用lua_call(), 并且告訴程序偶們有兩個(gè)參數(shù)一個(gè)返回值
* 接著偶們從棧頂取回返回值, 用lua_tonumber()
* 最后偶們用lua_pop()把返回值清掉
運(yùn)行結(jié)果:
The sum is 25
編譯方法
Linux下把程序存成e13.cpp
g++ e13.cpp -llua -llualib -o e13
./e13
VC下編譯方法
* 首先建立一個(gè)空的Win32 Console Application Project
* 把e13.cpp加入工程中
* 點(diǎn)project setting,然后設(shè)置link選項(xiàng), 再加上lua.lib lualib.lib兩個(gè)額外的庫(kù)
* 最后編譯
建立好的project可以在這里下載
VC http://tonyandpaige.com/tutorials/luaadd.zip
Linux http://tonyandpaige.com/tutorials/luaadd.tar.gz
3.全局變量
上面偶們用到了lua_getglobal()但并沒(méi)有詳細(xì)講, 這里偶們?cè)倥e兩個(gè)小例子來(lái)說(shuō)下全局變量
lua_getglobal()的作用就是把lua中全局變量的值壓入棧
lua_getglobal(L, "z");
z = (int)lua_tonumber(L, 1);
lua_pop(L, 1);
假設(shè)Lua程序中定義了一個(gè)全局變量z, 這段小程序就是把z的值取出放入C的變量z中.
另外Lua中還有一個(gè)對(duì)應(yīng)的函數(shù)lua_setglobal(), 作用是用棧頂?shù)闹堤畛渲付ǖ娜肿兞?br /> lua_pushnumber(L, 10);
lua_setglobal(L, "z");
例如這段小程序就是把lua中的全局變量z設(shè)為10, 如果lua中未定義z的話, 就會(huì)自動(dòng)創(chuàng)建一個(gè)
全局變量z并設(shè)為10.
4.試試看
自己寫(xiě)個(gè)函數(shù)用C/C++來(lái)調(diào)用下試試
七.調(diào)用C/C++函數(shù)
1.前言
上次偶說(shuō)到從C/C++中調(diào)用Lua的函數(shù), 然后就有朋友問(wèn)從Lua中如何調(diào)用C/C++的
函數(shù), 所以偶們這次就來(lái)說(shuō)說(shuō)這個(gè)問(wèn)題. 首先偶們會(huì)在C++中建立一個(gè)函數(shù), 然后
告知Lua有這個(gè)函數(shù), 最后再執(zhí)行它. 另外, 由于函數(shù)不是在Lua中定義的, 所以
無(wú)法確定函數(shù)的正確性, 可能在調(diào)用過(guò)程中會(huì)出錯(cuò), 因此偶們還會(huì)說(shuō)說(shuō)Lua出錯(cuò)處
理的問(wèn)題.
2.Lua中調(diào)用C函數(shù)
在lua中是以函數(shù)指針的形式調(diào)用函數(shù), 并且所有的函數(shù)指針都必須滿(mǎn)足如下此種
類(lèi)型:
typedef int (*lua_CFunction) (lua_State *L);
也就是說(shuō), 偶們?cè)贑++中定義函數(shù)時(shí)必須以lua_State為參數(shù), 以int為返回值才能
被Lua所調(diào)用. 但是不要忘記了, 偶們的lua_State是支持棧的, 所以通過(guò)棧可以
傳遞無(wú)窮個(gè)參數(shù), 大小只受內(nèi)存大小限制. 而返回的int值也只是指返回值的個(gè)數(shù)
真正的返回值都存儲(chǔ)在lua_State的棧中. 偶們通常的做法是做一個(gè)wrapper, 把
所有需要調(diào)用的函數(shù)都wrap一下, 這樣就可以調(diào)用任意的函數(shù)了.
下面這個(gè)例子是一個(gè)C++的average()函數(shù), 它將展示如何用多個(gè)參數(shù)并返回多個(gè)值
例e14.cpp
#include
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
/* the Lua interpreter */
lua_State* L;
static int average(lua_State *L)
{
/* get number of arguments */
int n = lua_gettop(L);
double sum = 0;
int i;
/* loop through each argument */
for (i = 1; i <= n; i++)
{
/* total the arguments */
sum += lua_tonumber(L, i);
}
/* push the average */
lua_pushnumber(L, sum / n);
/* push the sum */
lua_pushnumber(L, sum);
/* return the number of results */
return 2;
}
int main ( int argc, char *argv[] )
{
/* initialize Lua */
L = lua_open();
/* load Lua base libraries */
lua_baselibopen(L);
/* register our function */
lua_register(L, "average", average);
/* run the script */
lua_dofile(L, "e15.lua");
/* cleanup Lua */
lua_close(L);
return 0;
}
例e15.lua
-- call a C++ function
avg, sum = average(10, 20, 30, 40, 50)
print("The average is ", avg)
print("The sum is ", sum)
程序說(shuō)明:
* lua_gettop()的作用是返回棧頂元素的序號(hào). 由于Lua的棧是從1開(kāi)始編號(hào)的,
所以棧頂元素的序號(hào)也相當(dāng)于棧中的元素個(gè)數(shù). 在這里, 棧中元素的個(gè)數(shù)就
是傳入的參數(shù)個(gè)數(shù).
* for循環(huán)計(jì)算所有傳入?yún)?shù)的總和. 這里用到了數(shù)值轉(zhuǎn)換lua_tonumber().
* 然后偶們用lua_pushnumber()把平均值和總和push到棧中.
* 最后, 偶們返回2, 表示有兩個(gè)返回值.
* 偶們雖然在C++中定義了average()函數(shù), 但偶們的Lua程序并不知道, 所以需
要在main函數(shù)中加入
/* register our function */
lua_register(L, "average", average);
這兩行的作用就是告訴e15.lua有average()這樣一個(gè)函數(shù).
* 這個(gè)程序可以存成cpp也可以存成c, 如果以.c為擴(kuò)展名就不需要加extern "C"
編譯的方法偶們上次說(shuō)過(guò)了, 方法相同.
e15.lua執(zhí)行的方法只能用上例中的C++中執(zhí)行, 而不能用命令行方式執(zhí)行.
3.錯(cuò)誤處理
在上例中, 偶們沒(méi)有對(duì)傳入的參數(shù)是否為數(shù)字進(jìn)行檢測(cè), 這樣做不好. 所以這里偶
們?cè)偌由襄e(cuò)誤處理的片斷.
把這段加在for循環(huán)之內(nèi):
if (!lua_isnumber(L, i)) {
lua_pushstring(L, "Incorrect argument to 'average'");
lua_error(L);
}
這段的作用就是檢測(cè)傳入的是否為數(shù)字.
加上這段之后, 偶們debug的時(shí)候就會(huì)簡(jiǎn)單許多. 對(duì)于結(jié)合兩種語(yǔ)言的編程, 它們之
間傳遞數(shù)據(jù)的正確性檢測(cè)是非常重要的.
這里有別人寫(xiě)好的例子:
VC的 http://tonyandpaige.com/tutorials/luaavg.zip
Linux的 http://tonyandpaige.com/tutorials/luaavg.tar.gz