{"id":599,"date":"2025-05-28T14:54:11","date_gmt":"2025-05-28T06:54:11","guid":{"rendered":"https:\/\/reverieland.cn\/?p=599"},"modified":"2025-06-11T00:49:00","modified_gmt":"2025-06-10T16:49:00","slug":"lua%e7%9f%a5%e8%af%86%e7%82%b9%e5%a4%8d%e4%b9%a0%ef%bc%88%e4%b8%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/reverieland.cn\/index.php\/599\/","title":{"rendered":"LUA\u77e5\u8bc6\u70b9\u590d\u4e60\uff08\u4e0b\uff09"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1.\u4ecb\u7ecd\u4e00\u4e0b\u95ed\u5305<\/h2>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><strong>\u4ec0\u4e48\u662f\u95ed\u5305\uff1a\u5b50\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528\u7236\u51fd\u6570\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf<\/strong><\/mark><\/p>\n\n\n\n<p>\u95ed\u5305\u7684<strong>\u4e3b\u8981\u4f5c\u7528<\/strong>\u6709\u4e24\u4e2a\uff1a<\/p>\n\n\n\n<p>\u4e00\u662f\u7b80\u6d01\uff0c\u5728\u4e0d\u9700\u8981\u4f7f\u7528\u65f6\uff0c\u5c31\u4e0d\u751f\u6210\u5bf9\u8c61\uff0c\u4e5f\u4e0d\u9700\u8981\u51fd\u6570\u540d\uff1b<\/p>\n\n\n\n<p>\u4e8c\u662f\u6355\u83b7\u5916\u90e8\u53d8\u91cf\uff0c\u5f62\u6210\u4e0d\u540c\u7684\u8c03\u7528\u73af\u5883\u3002<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u95ed\u5305\u7684\u539f\u7406<\/mark><\/strong><\/p>\n\n\n\n<p id=\"uacf9079d\">\u95ed\u5305\uff08\u51fd\u6570\uff09\u7f16\u8bd1\u65f6\u4f1a\u751f\u6210\u539f\u578b\uff08prototype\uff09\uff0c\u5305\u542b\u53c2\u6570\u3001\u8c03\u8bd5\u4fe1\u606f\u3001\u865a\u62df\u673a\u6307\u4ee4\u7b49\u4e00\u7cfb\u5217\u8be5\u95ed\u5305\u7684\u6e90\u4fe1\u606f\uff0c\u5176\u4e2d\u5728\u9012\u5f52\u7f16\u8f91\u5185\u5c42\u51fd\u6570\u65f6\uff0c\u4f1a\u4e3a\u5185\u5c42\u51fd\u6570\u751f\u6210\u6307\u4ee4\uff0c\u540c\u65f6\u4e3a\u8be5\u5185\u5c42\u51fd\u6570\u9700\u8981\u7684\u6240\u6709upvalue\u521b\u5efa\u8868\uff0c\u4ee5\u4fbf\u4e4b\u540e\u8c03\u7528\u65f6\u8fdb\u884cupvalue\u503c\u641c\u7d22<\/p>\n\n\n\n<p id=\"ua14b9306\">\u5728lua\u4e2d\uff0c\u4f1a\u751f\u6210\u4e00\u4e2a\u5168\u5c40\u6808\uff0c\u6240\u6709\u7684upvalue\u90fd\u4f1a\u6307\u5411\u8be5\u6808\u4e2d\u7684\u503c\uff0c\u82e5\u5bf9\u5e94\u7684\u53c2\u6570\u79bb\u5f00\u7684\u4f5c\u7528\u57df\uff0c\u6808\u4e2d\u7684\u503c\u4e5f\u4f1a\u88ab\u91ca\u653e\uff0cupvalue\u7684\u6307\u9488\u4f1a\u6307\u5411\u81ea\u5df1\uff0c\u7b49\u5f85\u88abgc<\/p>\n\n\n\n<p id=\"u0d402db3\">\u95ed\u5305\u8fd0\u884c\u65f6\uff0c\u4f1a\u901a\u8fc7\u521b\u5efa\u6307\u5411upvalue\u7684\u6307\u9488\uff0c\u5e76\u5faa\u73afupvalue linked list\uff0c\u627e\u5230\u6240\u9700\u8981\u7684\u5916\u90e8\u53d8\u91cf\u8fdb\u884c\u8fd0\u884c<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Lua\u7684\u5185\u5b58\u7ba1\u7406\u673a\u5236<\/h2>\n\n\n\n<p><br>Lua\u7684\u5185\u5b58\u7ba1\u7406\u673a\u5236\u662f\u57fa\u4e8e\u5783\u573e\u56de\u6536\uff08garbage collection\uff09\u7684\u539f\u7406\uff0c\u5e76\u91c7\u7528\u4e86\u6807\u8bb0-\u6e05\u9664\uff08mark and sweep\uff09\u7b97\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"HRhJG\">\u5bf9\u8c61\u5206\u914d\uff1a<\/h3>\n\n\n\n<p id=\"u4ca5b3e8\">\u5f53Lua\u7a0b\u5e8f\u9700\u8981\u5206\u914d\u5185\u5b58\u6765\u5b58\u50a8\u5bf9\u8c61\u65f6\uff0c\u5b83\u4f1a\u4f7f\u7528\u81ea\u5df1\u7684\u5185\u5b58\u5206\u914d\u5668\u3002Lua\u7684\u5185\u5b58\u5206\u914d\u5668\u662f\u57fa\u4e8e\u56fa\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\u5757\uff08blocks\uff09\u7684\uff0c\u8fd9\u4e9b\u5185\u5b58\u5757\u88ab\u79f0\u4e3a\"\u5185\u5b58\u5757\u5217\u8868\"\uff08block list\uff09\u3002\u5f53\u7a0b\u5e8f\u9700\u8981\u5206\u914d\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u5185\u5b58\u5206\u914d\u5668\u4f1a\u4ece\u5185\u5b58\u5757\u5217\u8868\u4e2d\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u5185\u5b58\u5757\u6765\u5b58\u50a8\u8be5\u5bf9\u8c61\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"kIqyZ\">\u5783\u573e\u56de\u6536\u5668\uff1a<\/h3>\n\n\n\n<p id=\"u4cb3139f\">Lua\u7684\u5783\u573e\u56de\u6536\u5668\u5468\u671f\u6027\u5730\u8fd0\u884c\uff0c\u4ee5\u68c0\u6d4b\u5e76\u6e05\u9664\u4e0d\u518d\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u3002\u5783\u573e\u56de\u6536\u5668\u5206\u4e3a\u4e24\u4e2a\u9636\u6bb5\uff1a\u6807\u8bb0\u9636\u6bb5\u548c\u6e05\u9664\u9636\u6bb5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aGnoX\">\u6807\u8bb0\u9636\u6bb5\uff1a<\/h3>\n\n\n\n<p id=\"ub8852a68\">\u5783\u573e\u56de\u6536\u5668\u4ece\u4e00\u4e2a\u79f0\u4e3a\"\u6839\u96c6\"\uff08root set\uff09\u7684\u5730\u65b9\u5f00\u59cb\uff0c\u9010\u6b65\u904d\u5386\u6240\u6709\u53ef\u8fbe\u7684\u5bf9\u8c61\uff0c<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u5e76\u5c06\u5b83\u4eec\u6807\u8bb0\u4e3a<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u6d3b\u52a8<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u5bf9\u8c61\u3002<\/mark><\/strong>\u6839\u96c6\u5305\u62ec\u5168\u5c40\u53d8\u91cf\u3001\u5f53\u524d\u6267\u884c\u7684\u51fd\u6570\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\u4ee5\u53caC\u51fd\u6570\u4e2d\u7684\u4e00\u4e9b\u7279\u6b8a\u6570\u636e\u7ed3\u6784\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"SvKzG\">\u6e05\u9664\u9636\u6bb5\uff1a<\/h3>\n\n\n\n<p id=\"u2f1a850d\">\u5728\u6807\u8bb0\u9636\u6bb5\u5b8c\u6210\u540e\uff0c\u5783\u573e\u56de\u6536\u5668\u4f1a\u626b\u63cf\u6240\u6709\u7684\u5bf9\u8c61\uff0c\u5e76<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u6e05\u9664\u90a3\u4e9b\u6ca1\u6709\u88ab\u6807\u8bb0\u7684\u5bf9\u8c61<\/mark>\uff08\u4e0d\u5728<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u6d3b\u52a8<\/mark>\u72b6\u6001\uff09\u3002\u8fd9\u4e9b\u672a\u88ab\u6807\u8bb0\u7684\u5bf9\u8c61\u88ab\u8ba4\u4e3a\u662f\u4e0d\u518d\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u88ab\u5b89\u5168\u5730\u91ca\u653e\u5185\u5b58\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"EG1tn\">\u5206\u4ee3\u56de\u6536\uff1a<\/h3>\n\n\n\n<p id=\"u611ddf1f\">Lua\u7684\u5783\u573e\u56de\u6536\u5668\u91c7\u7528\u4e86<strong>\u5206\u4ee3\u56de\u6536<\/strong>\u7684\u7b56\u7565\u3002\u5b83\u5c06\u5185\u5b58\u5bf9\u8c61\u5206\u4e3a\u51e0\u4ee3\uff08generation\uff09\uff0c\u6bcf\u4e00\u4ee3\u90fd\u6709\u4e0d\u540c\u7684\u4f18\u5148\u7ea7\u3002\u65b0\u5206\u914d\u7684\u5bf9\u8c61\u901a\u5e38\u88ab\u653e\u7f6e\u5728\u7b2c\u4e00\u4ee3\u4e2d\uff0c\u800c\u8f83\u8001\u7684\u5bf9\u8c61\u5219\u4f1a\u9010\u6e10\u664b\u5347\u5230\u66f4\u9ad8\u7684\u4ee3\u3002\u8fd9\u79cd\u5206\u4ee3\u7684\u673a\u5236\u53ef\u4ee5\u63d0\u9ad8\u5783\u573e\u56de\u6536\u7684\u6548\u7387\uff0c\u56e0\u4e3a\u65b0\u5206\u914d\u7684\u5bf9\u8c61\u901a\u5e38\u5177\u6709\u8f83\u77ed\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"DJwWA\">\u624b\u52a8\u5185\u5b58\u7ba1\u7406\uff1a<\/h3>\n\n\n\n<p id=\"u687b871e\">Lua\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u7684\u673a\u5236\u3002\u5176\u4e2d\u4e00\u4e2a\u662fcollectgarbage\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u624b\u52a8\u89e6\u53d1\u5783\u573e\u56de\u6536\u8fc7\u7a0b\u3002\u901a\u8fc7\u8c03\u6574collectgarbage\u51fd\u6570\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u5bf9\u5783\u573e\u56de\u6536\u5668\u7684\u884c\u4e3a\u8fdb\u884c\u4e00\u4e9b\u63a7\u5236\uff0c\u4f8b\u5982\u8bbe\u7f6e\u4e0d\u540c\u7684\u5783\u573e\u56de\u6536\u9608\u503c\u3001\u5173\u95ed\u5783\u573e\u56de\u6536\u7b49\u3002\u6b64\u5916\uff0cLua\u8fd8\u63d0\u4f9b\u4e86weak table\u6765\u5e2e\u52a9\u7ba1\u7406\u5bf9\u8c61\u4e4b\u95f4\u7684\u5f15\u7528\u5173\u7cfb\uff0c\u4ee5\u907f\u514d\u5faa\u73af\u5f15\u7528\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u591a\u4eba\u5f00\u53d1\u5982\u4f55\u907f\u514d\u5168\u5c40\u53d8\u91cf\u6cdb\u6ee5<\/h2>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><code>_G<\/code> \u662f Lua \u4e2d\u7684\u4e00\u4e2a\u7279\u6b8a\u8868\uff0c\u8868\u793a\u5168\u5c40\u73af\u5883\uff0c\u6240\u6709\u5168\u5c40\u53d8\u91cf\u5b9e\u9645\u4e0a\u90fd\u4fdd\u5b58\u5728\u8fd9\u4e2a\u8868\u4e2d\u3002<\/mark>\u901a\u8fc7\u5143\u8868\uff08metatable\uff09\u673a\u5236\uff0c\u6211\u4eec\u53ef\u4ee5\u62e6\u622a\u5bf9\u5168\u5c40\u53d8\u91cf <code>_G<\/code> \u7684\u8bbf\u95ee\u548c\u8d4b\u503c\u64cd\u4f5c<\/strong>\uff0c\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u8c03\u8bd5\u4ee3\u7801\u4e2d\u672a\u5b9a\u4e49\u6216\u610f\u5916\u4f7f\u7528\u7684\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n\n\n\n<p><strong><code>setmetatable(_G, {...})<\/code><\/strong> \u4e3a <code>_G<\/code> \u8bbe\u7f6e\u4e00\u4e2a\u5143\u8868\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e24\u4e2a\u5143\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>__newindex<\/code>\uff1a\u62e6\u622a\u5bf9\u672a\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\u7684\u8d4b\u503c\u64cd\u4f5c\uff1b<\/li>\n\n\n\n<li><code>__index<\/code>\uff1a\u62e6\u622a\u5bf9\u672a\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\u7684\u8bbf\u95ee\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">-- \u8bbe\u7f6e _G\uff08\u5168\u5c40\u73af\u5883\u8868\uff09\u7684\u5143\u8868\nsetmetatable(\n  _G,\n  {\n    -- \u5f53\u5c1d\u8bd5\u7ed9\u4e0d\u5b58\u5728\u7684\u5168\u5c40\u53d8\u91cf\u8d4b\u503c\u65f6\u89e6\u53d1\uff08\u5982\uff1afoo = 123\uff0c\u4f46 foo \u6ca1\u6709\u4e8b\u5148\u5b9a\u4e49\uff09\n    __newindex = function(_, key)\n      print(\"attempt to add a new value to global, key: \" .. key)\n    end,\n\n    -- \u5f53\u5c1d\u8bd5\u8bbf\u95ee\u4e00\u4e2a\u672a\u5b9a\u4e49\u7684\u5168\u5c40\u53d8\u91cf\u65f6\u89e6\u53d1\uff08\u5982\uff1aprint(bar)\uff0c\u4f46 bar \u6ca1\u6709\u5b9a\u4e49\uff09\n    __index = function(_, key)\n      print(\"attempt to index a global value, key: \" .. key)\n    end\n  }\n)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u70ed\u66f4\u65b0\u7684\u539f\u7406<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u524d\u7f6e1\uff1alua\u4e2d\u7684require\u662f\u4ec0\u4e48<\/h3>\n\n\n\n<p>Lua \u4e2d\u7684 <code>require<\/code> \u662f\u4e00\u4e2a\u7528\u4e8e<strong>\u6a21\u5757\u52a0\u8f7d<\/strong>\u7684\u51fd\u6570\uff0c\u5176\u4f5c\u7528\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u52a0\u8f7d\u5e76\u8fd0\u884c\u6307\u5b9a\u7684 Lua \u6a21\u5757\uff08\u6587\u4ef6\uff09<\/strong>\u3002<\/li>\n\n\n\n<li><strong>\u53ea\u52a0\u8f7d\u4e00\u6b21<\/strong>\uff1a\u540c\u4e00\u4e2a\u6a21\u5757\u5728\u6574\u4e2a\u8fd0\u884c\u671f\u95f4\u53ea\u4f1a\u88ab\u52a0\u8f7d\u548c\u6267\u884c\u4e00\u6b21\uff08\u9664\u975e\u663e\u5f0f\u6e05\u9664\u7f13\u5b58\uff09\u3002<\/li>\n\n\n\n<li>\u901a\u5e38\u7528\u6765\u7ec4\u7ec7\u4ee3\u7801\u3001\u62c6\u5206\u529f\u80fd\u6a21\u5757\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">-- \u524d\u63d0\uff1a\u6211\u4eec\u6709my_module.lua\u6587\u4ef6\nlocal myModule = require(\"my_module\")\nmyModule.functionInModule()\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u524d\u7f6e2\uff1apackage<\/h3>\n\n\n\n<p><code>package.loaded<\/code> \u662f\u4e00\u4e2aLUA\u5185\u7f6e\u7684\u8868\uff0cLua \u5185\u90e8\u7528\u5b83\u6765<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u8bb0\u5f55\u6240\u6709\u5df2\u52a0\u8f7d\u7684\u6a21\u5757<\/mark><\/strong>\u3002<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">print(package.loaded)       --  --> table: 0x...\nprint(package.path)         --  --> \u5f53\u524d\u6a21\u5757\u641c\u7d22\u8def\u5f84\nprint(package.preload)      --  --> \u624b\u52a8\u6ce8\u518c\u7684\u6a21\u5757\u8868<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u70ed\u66f4\u65b0\u7684\u4ee3\u7801<\/h3>\n\n\n\n<p><strong>\u6211\u4eec\u8981\u628a\u65b0\u6a21\u5757\u7684\u5185\u5bb9\uff0c\u6ce8\u5165\u8fdb\u6e38\u620f\u903b\u8f91\u539f\u672c\u5f15\u7528\u7684\u65e7\u6a21\u5757\u8868\u4e2d<\/strong>\uff0c\u8fd9\u6837\u624d\u4f1a\u5728\u8fd0\u884c\u4e2d\u7684\u6e38\u620f\u4e2d\u751f\u6548\uff08\u4e0d\u80fd\u4ec5\u9760require\u6765\u5de5\u4f5c\uff09\u3002<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\n-- \u5b9a\u4e49\u70ed\u66f4\u65b0\u51fd\u6570\uff0c\u53c2\u6570\u4e3a\u6a21\u5757\u540d\nfunction reloadUp(module_name)\n  -- \u4ece\u5168\u5c40\u73af\u5883_G\u4e2d\u83b7\u53d6\u65e7\u7684\u6a21\u5757\u8868\n  local old_module = _G[module_name]\n  -- \u5c06package.loaded\u4e2d\u5bf9\u5e94\u6a21\u5757\u6807\u8bb0\u4e3a\u672a\u52a0\u8f7d\uff0c\u5f3a\u5236\u91cd\u65b0\u52a0\u8f7d\n  package.loaded[module_name] = nil\n  -- \u91cd\u65b0\u52a0\u8f7d\u6a21\u5757\uff0c\u5b9e\u9645\u4e0a\u662f\u6267\u884c\u4e86\u6a21\u5757\u7684\u6587\u4ef6\uff0c\u52a0\u8f7d\u65b0\u7684\u5185\u5bb9\u5230\u5185\u5b58\n  require(module_name)\n  -- \u4ece\u5168\u5c40\u73af\u5883_G\u4e2d\u83b7\u53d6\u521a\u52a0\u8f7d\u7684\u65b0\u6a21\u5757\u8868\n  local new_module = _G[module_name]\n  -- \u904d\u5386\u65b0\u6a21\u5757\u8868\u4e2d\u7684\u6240\u6709\u5b57\u6bb5\n  for k, v in pairs(new_module) do\n    -- \u5c06\u65b0\u6a21\u5757\u8868\u4e2d\u7684\u5b57\u6bb5\u590d\u5236\u5230\u65e7\u6a21\u5757\u8868\u4e2d\uff0c\u5b9e\u73b0\u66f4\u65b0\n    old_module[k] = v\n  end\n  -- \u5c06\u66f4\u65b0\u540e\u7684\u65e7\u6a21\u5757\u8868\u91cd\u65b0\u653e\u56depackage.loaded\uff0c\u8868\u793a\u6a21\u5757\u5df2\u88ab\u52a0\u8f7d\uff08\u8d77\u5230\u6807\u8bb0\u7684\u4f5c\u7528\uff0c\u907f\u514d\u518d\u8c03\u7528\u51fd\u6570\u65f6\u91cd\u590d\u52a0\u8f7d\uff09\n  package.loaded[module_name] = old_module\nend\n<\/code><\/pre>\n\n\n\n<p>\u6700\u7ec8\u5f97\u5230\u7684\u7ed3\u679c\u7684\u4ecd\u662f\u65e7\u7684\u6a21\u5757\u8868\uff0c\u53ea\u662f\u5185\u5bb9\u5df2\u7ecf\u88ab\u70ed\u66f4\u65b0\u3002\u56e0\u6b64\uff0c\u51fd\u6570\u4e2d\u7684\u65b0\u6a21\u5757\u8868\u53ea\u662f\u662f\u901a\u8fc7\u91cd\u65b0 <code>require(module_name)<\/code> \u52a0\u8f7d\u6a21\u5757\u6587\u4ef6\u751f\u6210\u7684\u4e00\u4e2a\u201c\u4e34\u65f6\u7684\u65b0\u6a21\u5757\u8868\u201d\uff0c\u800c<strong>\u65e7\u8868<\/strong><code> <\/code><code>old_module<\/code> \u662f\u5916\u754c\uff08\u5176\u4ed6\u6a21\u5757\u6216\u4ee3\u7801\uff09\u5df2\u7ecf\u5f15\u7528\u8fc7\u7684\u6a21\u5757\u8868\uff0c<code>old_module<\/code> \u4fdd\u5b58\u7684\u662f<strong>\u5bf9\u65e7\u6a21\u5757\u8868\u7684\u5f15\u7528<\/strong>\uff0c\u6211\u4eec\u8981\u4fdd\u7559\u8fd9\u4e2a\u5f15\u7528\u3002<\/p>\n\n\n\n<p><code>require()<\/code> \u662f\u52a0\u8f7d\u52a8\u4f5c\uff0c<code>for k,v in pairs(new_module)<\/code> \u662f\u8fc1\u79fb\u52a8\u4f5c\uff0c<code>package.loaded[...] = old_module<\/code> \u662f\u6ce8\u518c\u52a8\u4f5c<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.C#\u4e2d\uff0c\u503c\u7c7b\u578b\u4f20\u9012\u4e3a\u4ec0\u4e48\u4f1a\u4ea7\u751fGC\uff1f XLua\u7684\u89e3\u51b3\u529e\u6cd5\u662f\u4ec0\u4e48\uff1f<\/h2>\n\n\n\n<p>\u901a\u5e38\u6765\u8bf4\uff0c\u53ea\u8981\u5806\u5206\u914d\u4e86\u5185\u5b58\uff0c\u4e5f\u5c31\u662f\u5b9e\u4f8b\u5316\u5f15\u7528\u5bf9\u8c61\uff0c\u5728\u5bf9\u8c61\u4f7f\u7528\u5b8c\u65f6\uff0c\u5c31\u4f1a\u88abGC\u3002\u6240\u4ee5\u503c\u7c7b\u578b\u65f6\u4ece\u6808\u4e0a\u5206\u914d\u7684\uff0c\u539f\u5219\u4e0a\u4e0d\u4f1a\u4ea7\u751fGC\uff0c\u4f46\u662f\u5728lua\u548cC#\u4ea4\u4e92\u8fc7\u7a0b\u4e2d\uff0c<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u503c\u4f20\u9012\u4f1a\u56e0\u4e3a\u88c5\u7bb1\u62c6\u7bb1\u4ea7\u751fGC\u3002<\/mark><\/strong><\/p>\n\n\n\n<p>\u4ee5\u4e0b\u9762\u7684MyStruct\u4e3a\u4f8b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#x5B;GCOptimize]\n&#x5B;LuaCallCSharp]\npublic struct MyStruct\n{\n    public MyStruct(int p1, int p2)\n    {\n        a = p1;\n        b = p2;\n        c = p2;\n        e.c = (byte)p1;\n    }\n    public int a;\n    public int b;\n    public decimal c;\n    public Pedding e;\n}\n<\/pre><\/div>\n\n\n<p>\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86<code>[GCOptimize]<\/code>\u5982\u4f55\u901a\u8fc7\u751f\u6210\u7279\u5316\u4ee3\u7801\uff0c\u7ed5\u8fc7.NET\u88c5\u7bb1\u673a\u5236\uff0c\u907f\u514d\u88c5\u7bb1\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ XLuaTest.MyStruct\u751f\u6210\u4ee3\u7801\n&#x5B;MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]\nstatic int __CreateInstance(RealStatePtr L)\n{\n    try\n    {\n        ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);\n        if (LuaAPI.lua_gettop(L) == 3 &amp;&amp; LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 2) &amp;&amp; LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 3))\n        {\n            int _p1 = LuaAPI.xlua_tointeger(L, 2);\n            int _p2 = LuaAPI.xlua_tointeger(L, 3);\n\n            var gen_ret = new XLuaTest.MyStruct(_p1, _p2);\n            \/\/ \u65e0&#x5B;GCOptimize]\u6807\u7b7e\n            \/\/ translator.Push(L, gen_ret);\n            \/\/ \u6709&#x5B;GCOptimize]\u6807\u7b7e\n            translator.PushXLuaTestMyStruct(L, gen_ret);\n\n            return 1;\n        }\n\n        if (LuaAPI.lua_gettop(L) == 1)\n        {\n            \/\/ \u65e0&#x5B;GCOptimize]\u6807\u7b7e\n            \/\/ translator.Push(L, default(XLuaTest.MyStruct));\n            \/\/ \u6709&#x5B;GCOptimize]\u6807\u7b7e\n            translator.PushXLuaTestMyStruct(L, default(XLuaTest.MyStruct));\n            return 1;\n        }\n    }\n    catch (System.Exception gen_e)\n    {\n        return LuaAPI.luaL_error(L, &quot;c# exception:&quot; + gen_e);   \/\/ \u6355\u83b7C#\u5f02\u5e38\uff0c\u8f6c\u6362\u6210LUA\u5f02\u5e38\n    }\n    return LuaAPI.luaL_error(L, &quot;invalid arguments to XLuaTest.MyStruct constructor!&quot;);  \/\/ \u53c2\u6570\u4e0d\u5339\u914d\uff0c\u62a5\u9519\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">(1.\u00a0<strong>\u7ed3\u6784\u4f53\u6784\u9020\u7684Lua\u7ed1\u5b9a\u673a\u5236<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7<code>[MonoPInvokeCallback]<\/code>\u6807\u8bb0\u5c06C#\u65b9\u6cd5\u6ce8\u518c\u4e3aLua\u53ef\u8c03\u7528\u7684\u51fd\u6570<\/li>\n\n\n\n<li>\u51fd\u6570\u540d<code>__CreateInstance<\/code>\u8868\u660e\u8fd9\u662fLua\u4e2d\u8c03\u7528<code>XLuaTest.MyStruct.new()<\/code>\u65f6\u89e6\u53d1\u7684\u6784\u9020\u51fd\u6570<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">(2.\u00a0<strong>\u53c2\u6570\u5904\u7406\u903b\u8f91<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5e26\u53c2\u6570\u7684\u6784\u9020<\/strong>\uff1a<br>\u5f53\u68c0\u6d4b\u52303\u4e2a\u53c2\u6570\uff08\u542b\u9690\u542b\u7684\u5143\u8868\u53c2\u6570\uff09\u4e14\u540e\u4e24\u4e2a\u4e3anumber\u7c7b\u578b\u65f6\uff1a\n<ul class=\"wp-block-list\">\n<li>new XLuaTest.MyStruct(LuaAPI.xlua_tointeger(L, 2), LuaAPI.xlua_tointeger(L, 3))<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u9ed8\u8ba4\u6784\u9020<\/strong>\uff1a<br>\u5f53\u53ea\u67091\u4e2a\u53c2\u6570\uff08\u4ec5\u5143\u8868\uff09\u65f6\u521b\u5efa\u9ed8\u8ba4\u7ed3\u6784\u4f53\uff1a\n<ul class=\"wp-block-list\">\n<li>default(XLuaTest.MyStruct)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">(3.\u00a0<strong>[GCOptimize]\u6807\u7b7e\u7684\u6838\u5fc3\u4f5c\u7528<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b58\u5728\u6807\u7b7e\u65f6<\/strong>\uff1a\u4f7f\u7528\u7279\u5316\u7684\u503c\u7c7b\u578b\u4f20\u9012\u65b9\u6cd5\n<ul class=\"wp-block-list\">\n<li>translator.PushXLuaTestMyStruct(L, gen_ret); <em>\/\/ \u907f\u514d\u88c5\u7bb1<\/em><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u65e0\u6807\u7b7e\u65f6<\/strong>\uff1a\u4f7f\u7528\u901a\u7528object\u4f20\u9012\n<ul class=\"wp-block-list\">\n<li>translator.Push(L, gen_ret); <em>\/\/ \u4f1a\u5bfc\u81f4\u7ed3\u6784\u4f53\u88c5\u7bb1<\/em><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8fd9\u9a8c\u8bc1\u4e86<code>[GCOptimize]<\/code>\u7684\u6838\u5fc3\u4ef7\u503c\uff1a\u6d88\u9664\u503c\u7c7b\u578b\u5728Lua-C#\u4ea4\u4e92\u65f6\u7684\u88c5\u7bb1\u5f00\u9500<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">6.\u4ec0\u4e48\u662f\u5f31\u5f15\u7528\uff1f<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>1.\u4ecb\u7ecd\u4e00\u4e0b\u95ed\u5305 \u4ec0\u4e48\u662f\u95ed\u5305\uff1a\u5b50\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528\u7236\u51fd\u6570\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf \u95ed\u5305\u7684\u4e3b\u8981\u4f5c\u7528\u6709\u4e24\u4e2a\uff1a \u4e00\u662f\u7b80\u6d01\uff0c\u5728\u4e0d\u9700\u8981\u4f7f\u7528\u65f6\uff0c\u5c31\u4e0d\u751f\u6210\u5bf9\u8c61\uff0c\u4e5f &#8230;<\/p>\n","protected":false},"author":1,"featured_media":551,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[34],"tags":[36,35,41],"class_list":["post-599","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c","tag-c","tag-lua","tag-41"],"_links":{"self":[{"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/posts\/599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/comments?post=599"}],"version-history":[{"count":8,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/posts\/599\/revisions"}],"predecessor-version":[{"id":614,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/posts\/599\/revisions\/614"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/media\/551"}],"wp:attachment":[{"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/media?parent=599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/categories?post=599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/tags?post=599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}