A weak table is a table whose elements are weak references. A weak reference is ignored by the garbage collector. In other words, if the only references to an object are weak references, then the garbage collector will collect this object.
A weak table can have weak keys, weak values, or both. A table with weak keys allows the collection of its keys, but prevents the collection of its values. A table with both weak keys and weak values allows the collection of both keys and values. In any case, if either the key or the value is collected, the whole pair is removed from the table. The weakness of a table is controlled by the __mode field of its metatable. If the __mode field is a string containing the character 'k', the keys in the table are weak. If __mode contains 'v', the values in the table are weak.
After you use a table as a metatable, you should not change the value of its __mode field. Otherwise, the weak behavior of the tables controlled by this metatable is undefined.
鍦╨ua涓?鍍弔able,userdata,function榪欎簺綾誨瀷鐨勫奸兘鏄紩鐢ㄤ紶閫?閫氳繃寮曠敤璁℃暟鏉ュ垽鏂槸鍚︽敹鎺夊璞?鑰屽急寮曠敤(weak reference)浼氳鍨冨溇鍥炴敹鍣ㄥ拷鐣?weak table灝辨槸瀹冪殑鍏冪礌鏄急寮曠敤鐨?涓涓厓绱?閿煎)鍙兘閿槸寮卞紩鐢?涔熷彲鑳藉兼槸寮卞紩鐢ㄧ殑,涔熷彲鑳介兘鏄急寮曠敤, 榪欎釜鐗規(guī)ф槸閫氳繃寮辮〃鐨刴etatable鐨刜_mode鐨勫兼潵璁劇疆鐨?鐗瑰埆鏈夋剰鎬濈殑鏄?褰撳急琛ㄤ腑涓涓敭鍊煎,瀹冪殑閿垨鍊煎叧鑱?寮曠敤/鎸囧悜)鐨勯偅涓璞¤鍨冨溇鍥炴敹鍣ㄥ洖鏀剁殑鏃跺?榪欎釜閿煎浼氫粠寮辮〃涓鑷姩鍒犻櫎鎺?榪欐槸涓緢閲嶈鐨勭壒鐐?
閭d箞寮辮〃鍒板簳鏈変粈涔堢敤鍛? 鍦╨ua鐨剋iki涓湁涓綃囦嬌鐢╱serdata鐨勪緥瀛?,鍏朵腑灝卞緢宸у鐨勭敤鍒頒簡寮辮〃,鍘熸枃鍦板潃
http://lua-users.org/wiki/CppBindingWithLunar
榪欑瘒鏂囩珷浠嬬粛浜嗗浣曢氳繃userdata緇戝畾c++瀵硅薄鍒拌剼鏈腑
fulluserdata鑳藉璁劇疆metatable,涔熷氨鑳芥ā鎷熷嚭瀵硅薄鐨勬晥鏋滃嚭鏉?瀵逛竴涓狢++鐨勭被鐨勫璞″疄渚嬫潵璇?push鍒拌剼鏈腑,涓鑸槸鍒涘緩浜嗕竴涓猽serdata,鏂囩珷涓敤寮辮〃閬垮厤浜嗗悓涓涓璞″疄渚?鎸囬拡) push鍒拌剼鏈腑,澶氭鍒涘緩userdata鐨勯棶棰?
鎹㈠彞璇濇潵璇?濡傛灉C++瀵硅薄鐨勭敓瀛樺懆鏈熸槸闈爈ua鐨勫瀮鍦懼洖鏀舵潵鎺у埗鐨勮瘽(userdata琚洖鏀舵椂,璋冪敤鍏冭〃鐨刜_gc鏂規(guī)硶,__gc鏂規(guī)硶涓瀽鏋刢++瀵硅薄),涓涓狢++瀵硅薄鍙兘鏈変竴涓敮涓鐨剈serdata. 鍦╱serdata鐨刴etatable涓垱寤轟竴涓兼槸寮卞紩鐢ㄧ殑寮辮〃,鐢–++瀵硅薄鎸囬拡鍋氶敭,姣忔push c++瀵硅薄鐨勬椂鍊?灝卞幓鐢ㄦ寚閽堝兼煡寮辮〃,濡傛灉鏈?灝眕ush閭d釜userdata,娌℃湁灝卞垱寤? 鍚屾椂,褰搖serdata鏄寮卞紩鐢ㄧ殑,褰撹鍨冨溇鍥炴敹鎺夌殑鏃跺?寮辮〃涓畠鎵鍦ㄧ殑閿煎鑷姩琚攢姣佷簡.