|
|
使用LUA封装和调用DELPHI的对象,使用上有些麻烦,封装好后在LUA中调用DELPHI对象非常方便!
我在我的BLOG放了代码包供大家下载:)
http://blog.gameres.com/thread.a ... ;amp;threadid=43069
// luaoop.pas 注解
uses
SysUtils, Lua, LuaPlus;
{$apptype console}
const
// 每个单独的类,都必须有个不同的TAG标志用来区分
ZIP_TAG = 1000;
type
// 一个简单的类,只返回字符串参数而已
TZip = class
public
function GetId: Integer;
constructor Create(const filename: String);
private
m_filename: String;
published
property filename: String read m_filename;
end;
// 创建
constructor TZip.Create(const filename: String);
begin
m_filename := filename;
end;
// 暂时没有用到
function TZip.GetId: Integer;
begin
Result := 0;
end;
// 通过ZIP_TAG判断LUA调用是否为TZip类
procedure _isArgZip(LS: p_lua_State; arg: Integer; const method: PChar);
begin
if not _isChild(LS,arg,ZIP_TAG) then
_argError(LS,arg,'Zip',method);
end;
// 用来创建LUA中用的ZIP类(也就是TABLE)
function Zip(LS: p_lua_State): Integer; cdecl;
var
filename: PChar;
zipfile: TZip;
msg: string;
begin
if(typeCheck) then
begin
_hasArgs(LS,1,'Zip');
_isArgString(LS,1,'Zip'); // zipFileName
end;
fileName := lua_tostring(LS,1);
try
zipFile := TZip.Create(filename);
_createInstance(LS,ZIP_TAG,zipFile);
except
on e: Exception do
begin
msg := format('''Zip'' error: %s', [e.Message]);
lua_pushstring(LS, pchar(msg));
lua_error(LS);
end;
end;
result := 1;
end;
// LUA中用来删除ZIP类的函数
function ZipDelete(LS: p_lua_State): Integer; cdecl;
var
zipfile: TZip;
begin
if(typeCheck) then
begin
_hasArgs(LS,1,'ZipDelete');
_isArgZip(LS,1,'ZipDelete'); // self
end;
zipFile := TZip(_getSelfInstance(LS));
zipFile.Destroy;
_deleteSelfInstance(LS);
result := 0;
end;
// LUA中用来调用TZip.filename
function ZipGetZippedFileName(LS: p_lua_State): Integer; cdecl;
var
zipfile: TZip;
begin
if(typeCheck) then
begin
_hasArgs(LS,1,'ZipGetZippedFileName');
_isArgZip(LS,1,'ZipGetZippedFileName'); // self
end;
zipFile := TZip(_getSelfInstance(LS));
lua_pushstring(LS, PChar(zipFile.filename));
result := 1;
end;
// 为LUA中的Zip添加方法
procedure _addZipMethods(LS: p_lua_State);
begin
_addMethod(LS,'getZippedFileName',ZipGetZippedFileName);
_addMethod(LS,'delete',ZipDelete);
end;
{
var
lua_engnlib : array[0..1] of luaL_reg = (
(name:'Zip';func:Zip),
(name:nil;func:nil)
);
}
var
LS: p_lua_State;
cmd: String;
begin
LS := lua_open();
luaopen_base(LS);
luaopen_table(LS);
luaopen_io(LS);
luaopen_string(LS);
luaopen_math(LS);
luaopen_debug(LS);
// luaL_openlib(LS, nil,@lua_engnlib,0);
// 注册创建ZIP对象的LUA函数
lua_register(LS,'Zip',Zip);
// 设置全局表
lua_pushliteral(LS,'_G');
lua_pushvalue(LS,LUA_GLOBALSINDEX);
// Class
lua_newtable(LS);
lua_setglobal(LS,'_CLASS');
// 注册LUA中用的Zip类
_setupClass(LS,'Zip',ZIP_TAG,_addZipMethods,ZipDelete);
// 运行 LuaOOP.lua 脚本
lua_dofile(LS, 'LuaOOP.lua');
// 以下为直接运行LUA脚本
lua_dostring(LS,'zips = Zip("abc")');
lua_dostring(LS,'print(zips:getZippedFileName())');
lua_dostring(LS,'if zips then zips:delete(); zips = nil end');
lua_dostring(LS,'print(zips)');
// 处理命令行输入的脚本
while true do
begin
readln(cmd);
if cmd = 'quit' then break;
lua_dostring(LS, PChar(cmd));
end;
// 关闭LUA
lua_close(LS);
end.
-- luaopp.lua 的文件内容:
objs = {}
for i = 0,9 do
-- 创建对象
objs = Zip('对象 ' .. i)
-- 打印对象方法返回的字符串
print(objs:getZippedFileName())
-- 删除对象
objs:delete()
-- 别忘了设置为空
objs = nil
end
|
|