Lua 与 C 交互(2)

本文详细介绍了在Lua中如何通过元表(metaTable)及闭包(upvalue)实现类和对象的功能。具体步骤包括创建类的新表、设置metatable及其__index属性、创建并设置对象的userdata等。
摘要由CSDN通过智能技术生成
1.new tbClass{}, Binding "FunName" to C function "thunk" and set it's closure with upvalue;
2.rigister metatable "Class", and set metatable __index to table"tbClass"
3.new pObj, new userdata to point it, and set this userdata's metatable then push it into stack.
4.call Obj:FunName()
5.because Obj associate tbClass by metatable, so will call tbClass[FunName], then call thunk, remember thunk have upvalue.
6.when we have userdata whitch have pObj,and have upvalue,then CallFun



1.new tbClass{},Encapsulation all Class Memeber as KLuaData. tbClass[FunName] = KLuaData.KLuaData push as userdata.
2.rigister metatable "Class", and set metatable __index to C Function "_LuaIndex"
3.push userdata who's restore pObj, and set metatable
4.Call a.FunName()
5.Call C Function _LuaIndex,in this can get pObj and KLuaData,switch kind of KLuaData,execute diff Op.
6.if KLuaData is FunData, then push pObj,and FunData, and CFun "_LuaDispatcher",then () will Call this Fun
7.CallFun

转载于:https://www.cnblogs.com/SweetDream/archive/2008/10/15/1311749.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值