{"id":115,"date":"2023-06-09T19:20:10","date_gmt":"2023-06-09T11:20:10","guid":{"rendered":"https:\/\/yanghanwen.cn\/?p=115"},"modified":"2023-06-09T19:22:17","modified_gmt":"2023-06-09T11:22:17","slug":"lua-%e4%b8%ad%e7%9a%84%e4%bb%a3%e7%a0%81%e7%89%87%e6%ae%b5","status":"publish","type":"post","link":"https:\/\/yanghanwen.cn\/?p=115","title":{"rendered":"lua \u4e2d\u7684\u4ee3\u7801\u7247\u6bb5"},"content":{"rendered":"<ul>\n<li><a href=\"#chunk\">chunk<\/a><\/li>\n<li><a href=\"#\u4f7f\u7528-load-\u52a0\u8f7d-chunk\">\u4f7f\u7528 load \u52a0\u8f7d chunk<\/a>\n<ul>\n<li><a href=\"#chunk-\u53c2\u6570\">chunk \u53c2\u6570<\/a><\/li>\n<li><a href=\"#chunkname-\u53c2\u6570\">chunkname \u53c2\u6570<\/a><\/li>\n<li><a href=\"#env-\u53c2\u6570\">env \u53c2\u6570<\/a><\/li>\n<li><a href=\"#dump\">dump<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#loadfile\">loadfile<\/a><\/li>\n<li><a href=\"#require-module\">require module<\/a>\n<ul>\n<li><a href=\"#preload\">preload<\/a><\/li>\n<li><a href=\"#loaded\">loaded<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#enviroment\">Enviroment<\/a>\n<ul>\n<li><a href=\"#_g\">_G<\/a>\n<ul>\n<li><a href=\"#_g-\u7684\u5143\u8868\">_G \u7684\u5143\u8868<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#_env\">_ENV<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h1>chunk<\/h1>\n<p>Lua \u91cc\u7684 \u4ee3\u7801\u7247\u6bb5 \u88ab\u79f0\u4e3a\u4e00\u4e2a <a href=\"https:\/\/www.lua.org\/pil\/1.1.html\">chunk<\/a><br \/>\n\u987e\u540d\u601d\u4e49\uff0c\u4ee3\u7801\u5757\uff0c\u53ef\u5927\u53ef\u5c0f\uff0c\u4e00\u4e2a\u6587\u4ef6\u3001\u4e00\u4e2a\u65b9\u6cd5\u3001\u4e00\u884c\u4ee3\u7801\uff0c\u90fd\u53ef\u4ee5\u53eb\u505a chunk<\/p>\n<p>\u901a\u5e38\u4ee5\u4e00\u884c\u4f5c\u4e3a\u4e00\u4e2a\u5757\uff0c\u4e0d\u4f1a\u5199\u5206\u53f7\u3001\u4e0d\u4f1a\u7528\u7a7a\u683c\u5206\u9694<br \/>\n\u4e00\u4e2a dofile\u3001require \u52a0\u8f7d\u7684\u4ee3\u7801\u6587\u4ef6\uff0c\u5176\u52a0\u8f7d\u7684\u5185\u5bb9\u4e5f\u53eb chunk\uff0c\u4f8b\u5982\u62a5\u9519\u7684\u65f6\u5019\u5c31\u4f1a\u63d0\u793a\u4f60 in xx chunk<\/p>\n<h1>\u4f7f\u7528 load \u52a0\u8f7d chunk<\/h1>\n<p><a href=\"https:\/\/www.lua.org\/manual\/5.3\/manual.html#load\">load<\/a> \u65b9\u6cd5\u662f\u6bd4\u8f83\u5e95\u5c42\u7684\uff0c\u5141\u8bb8\u4f60\u52a0\u8f7d\u4e00\u90e8\u5206\u4ee3\u7801\u7247\u6bb5\uff0c<\/p>\n<pre><code class=\"language-LUA\">load (chunk [, chunkname [, mode [, env]]])<\/code><\/pre>\n<p>\u8fd9\u4e9b\u53c2\u6570\u5206\u522b\u4ee3\u8868\uff1a<\/p>\n<ul>\n<li>chunk\uff1a\u4ee3\u7801\u7247\u6bb5<\/li>\n<li>chunkname\uff1a\u4ee3\u7801\u7684\u8bc6\u522b\u540d\uff0c\u5728\u65e5\u5fd7\u4e2d\u4f1a\u4ee5\u8fd9\u4e2a\u540d\u5b57\u6253\u5370<\/li>\n<li>mode\uff1a\u63a7\u5236chunk\u7684\u5b58\u5728\u65b9\u5f0f\n<ul>\n<li>b: binary<\/li>\n<li>t: text<\/li>\n<li>bt: \u4e24\u8005\u5747\u6709<\/li>\n<\/ul>\n<\/li>\n<li>env\uff1a\u4f20\u5165 chunk \u7684\u53c2\u6570<\/li>\n<\/ul>\n<h2>chunk \u53c2\u6570<\/h2>\n<p>chunk \u5728\u586b\u5165\u65f6 \u4e00\u5b9a\u662f\u4e00\u4e2a string\uff0c\u8868\u793a\u4ee3\u7801\u7247\u6bb5\uff08\u5c31\u50cf\u4f8b\u5b50\u4e2d\u90a3\u6837\uff09<br \/>\n\u800c\u6574\u4e2a chunk \u5728\u88ab load \u7f16\u8bd1\u6210\u529f\u65f6\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2a \u53ef\u6267\u884c\u65b9\u6cd5\uff08\u4e3a\u4e86\u4fbf\u4e8e\u53d9\u8ff0\uff0c\u6211\u4eec\u53eb\u5b83 <code>compiler func<\/code>\uff09<\/p>\n<pre><code class=\"language-LUA\">local loaded = load(chunk)\nif loaded then\n    local result = loaded()\nend<\/code><\/pre>\n<p>\u8c03\u7528\u4e86\u8fd9\u4e2a\u8fd4\u56de\u7684\u65b9\u6cd5\uff0c\u624d\u7b97\u662f\u8ba9\u8fd9\u4e2a chunk \u8fd0\u884c\u4e86\uff08\u5c31\u50cf\u52a0\u8f7d module \u90a3\u6837\uff09<\/p>\n<p>chunk \u7247\u6bb5\u5185\u90e8\uff08\u800c\u4e0d\u662f load \u7684\u7ed3\u679c <code>compiler func<\/code>\uff09 \u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u503c\uff1a<\/p>\n<ul>\n<li>\u8fd4\u56de\u4e00\u4e2a string<\/li>\n<li>\u6216\u8005 table\u3001function\uff0c<br \/>\n\u5982\u679c chunk \u5185\u90e8\u63d0\u524d\u8fd4\u56de\uff0c\u5219\u903b\u8f91\u4f1a\u5728\u8fd4\u56de\u5904\u622a\u65ad<\/li>\n<li>\u5982\u679c\u4f60\u6ca1\u6709\u8fd4\u56de\uff0c\u5219\u9ed8\u8ba4\u8fd4\u56de nil<\/li>\n<\/ul>\n<p>\u4f8b\u5982\u8fd9\u6837\u4e00\u4e2a\u4f8b\u5b50\uff0cchunk \u5185\u90e8\u8fd4\u56de\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5f53\u4f60\u6267\u884c <code>compiler func<\/code> \u65f6\uff0cchunk \u88ab\u8c03\u7528\u3001\u8fd4\u56de\u503c\uff1a<\/p>\n<pre><code class=\"language-LUA\">local loaded = load(\"return 'hello world'\")\nlocal str = loaded()\nprint(str)<\/code><\/pre>\n<p>\u800c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cchunk \u5185\u90e8\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u4f60\u5728\u8c03\u7528 <code>compiler func<\/code> \u540e\uff0c\u53ea\u662f\u6267\u884c\u4e86 chunk \u5185\u90e8\u7684\u903b\u8f91\uff0c\u4e0d\u9700\u8981\u63a5\u6536\u5b83\u7684\u8fd4\u56de\u503c\uff1a<\/p>\n<pre><code class=\"language-LUA\">local chunk = [[\n    globalVarInChunk = 123\n]]\nlocal loaded = load(chunk)\n\nloaded() \nprint(globalVarInChunk)<\/code><\/pre>\n<p>\u800c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c EmmyLua \u7b49\u8bed\u6cd5\u89e3\u91ca\u5668\u662f\u6355\u6349\u4e0d\u5230\u8fd9\u4e48\u9a9a\u7684\u64cd\u4f5c\u7684\uff0c\u4ed6\u4f1a\u8ba4\u4e3a\u4f60\u7684\u4ee3\u7801\u5b9e\u9645\u4e0a\u4e0d\u5b58\u5728\u8fd9\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u7ed9\u4f60\u63d0\u51fa\u5fe0\u544a<\/p>\n<h2>chunkname \u53c2\u6570<\/h2>\n<p>\u5bf9\u4e8e chunkname \u7684\u7406\u89e3\u53ef\u4ee5\u53c2\u8003\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u67e5\u770b\u63a7\u5236\u53f0\u8f93\u51fa\uff1a<\/p>\n<pre><code class=\"language-LUA\">local chunk = [[\n    local module = {}\n    function module:fun()\n        error(\"hello world\")\n    end\n    return module\n]]\nlocal loaded = load(chunk, \"Hello!World!\", \"bt\")\nlocal module = loaded()\nmodule:fun()<\/code><\/pre>\n<h2>env \u53c2\u6570<\/h2>\n<p>env \u5bf9\u5e94\u7684\u662f\u8fd9\u4e2a chunk \u7684\u4e00\u4e2a\u4e0a\u4e0b\u6587\u73af\u5883\uff0c\u4f8b\u5982\u524d\u9762\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86 <code>print<\/code> \u51fd\u6570\uff0c\u5c31\u662f\u56e0\u4e3a\u9ed8\u8ba4\u4f1a\u628a <code>_ENV<\/code> \u4f20\u8fdb\u53bb\u7ed9 chunk<br \/>\n\u65b9\u6cd5\u7684\u6ce8\u91ca\u91cc\u4f1a\u8bf4\u8fd9\u662f\u7528\u6237\u63d0\u4f9b\u7684  <code>\u8bbf\u95ee\u53c2\u6570(upvalues)<\/code><\/p>\n<p>_ENV \u548c _G \u662f\u7c7b\u4f3c\u7684\uff0c\u53ef\u4ee5\u53c2\u8003\uff1a<a href=\"https:\/\/www.lua.org\/manual\/5.3\/manual.html#2.2\">Environments and the Global Environment<\/a><br \/>\n\u800c\u5982\u679c\u6211\u4eec\u91cd\u5199 load \u7684 env \u53c2\u6570\u4e3a <code>{}<\/code>\uff0c\u4f1a\u53d1\u73b0 print \u51fd\u6570\u4e0d\u53ef\u7528\u4e86\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u4e3a\u5b83\u7684\u73af\u5883\u53d8\u91cf\u63d0\u4f9b\u5bf9\u5e94\u7684\u51fd\u6570\uff0c<\/p>\n<pre><code class=\"language-LUA\">local chunk = [[\n    local module = {}\n    function module:fun()\n        print(\"hello world\")\n        for k, v in pairs(_ENV) do\n            print(k, v)\n        end\n    end\n    return module\n]]\n\nlocal env = {}\nenv.print = print\nenv.pairs = pairs\nlocal loaded = load(chunk, \"test\", \"bt\", env)\nlocal module = loaded()\nmodule:fun()<\/code><\/pre>\n<p>\u8fd9\u79cd\u673a\u5236\u5bf9\u4e8e \u4e0a\u4e0b\u6587\u7684\u7ee7\u627f\u5173\u7cfb\uff0c\u5b8c\u5168\u662f\u7531 load \u7684\u8c03\u7528\u8005\u51b3\u5b9a\u7684\uff0c\u6362\u53e5\u8bdd\u8bf4\u6bcf\u4e2a chunk \u9ed8\u8ba4\u60c5\u51b5\u4e0b\u90fd\u62e5\u6709\u72ec\u7acb\u7684\u4e0a\u4e0b\u6587\u3002<br \/>\n\u4f46\u8fd8\u662f\u90a3\u53e5\u8bdd\uff0c\u672c\u8d28\u4e0a\u53ea\u662f\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5171\u4eab\u4e86\u540c\u4e00\u4e2a\u5168\u5c40\u4e0a\u4e0b\u6587\uff0c<br \/>\n\u5982\u679c\u5e95\u5c42\u7684\u7f16\u5199\u8005\u60f3\u8fd9\u4e48\u505a\uff0c\u4ed6\u751a\u81f3\u53ef\u4ee5\u5c06 env \u4e2d\u7684\u4e00\u4e9b\u65b9\u6cd5\u8fdb\u884c\u6539\u5199\uff0c<br \/>\n\u81f3\u4e8e\u8981\u4e0d\u8981\u7ee7\u627f\u8fd9\u4e2a\u65b0\u7684\u4e0a\u4e0b\u6587\uff0c\u5b8c\u5168\u662f\u65b0\u7684 load \u8c03\u7528\u8005\u51b3\u5b9a\u7684<\/p>\n<pre><code class=\"language-LUA\">local chunk1 = [[\n    print(globalVar)    \n    globalVar = 456\n\n    local fakerPrint = function()\n        print(\"never gonna give you up~ \")\n    end\n\n    local env = {}\n    env.print = fakerPrint\n    return env\n]]\n\nlocal chunk2 = [[\n    print(globalVar) \n]]\n\nlocal env = {}\nenv.print = print\nenv.globalVar = 123\n\nlocal loaded1 = load(chunk1, \"test1\", \"bt\", env)\nenv = loaded1()\n\nlocal loaded2 = load(chunk2, \"test2\", \"bt\", env)\nloaded2()<\/code><\/pre>\n<p>\u56e0\u4e3a lua \u672c\u8eab\u662f\u5404\u4e2a\u56fd\u5bb6\u7684\u4e00\u4e9b \u6c11\u95f4\u7ec4\u7ec7\u3001\u4e2a\u4eba\u5f00\u53d1\u8005 \u9f50\u5fc3\u534f\u529b\u641e\u7684\u4e00\u4e2a\u5f00\u6e90\u7684\u3001\u8f7b\u91cf\u7684\u3001\u6613\u4e8e\u6ce8\u5165\u7684\u8bed\u8a00\uff0c\u8fd9\u4e5f\u5bfc\u81f4\u5b83\u5341\u5206\u7075\u6d3b\uff0c\u7075\u6d3b\u8fc7\u5934\u4e86\uff0c<br \/>\n\u5982\u679c\u6ca1\u6709\u4e25\u683c\u7684 \u9879\u76ee\u89c4\u8303\u3001\u7ea6\u675f\uff0c\u5c31\u5f88\u5bb9\u6613\u5bfc\u81f4\u4e00\u4e9b\u653e\u98de\u81ea\u6211\u7684\u9a9a\u64cd\u4f5c\uff0c\u8ba9\u9879\u76ee\u7ef4\u62a4\u8d77\u6765\u6781\u5176\u56f0\u96be<\/p>\n<h2>dump<\/h2>\n<p>string \u63d0\u4f9b\u4e86 \u5c06 function \u8f6c\u5316\u4e3a binary \u5b57\u7b26\u4e32\u7684\u65b9\u6cd5<\/p>\n<pre><code class=\"language-LUA\">local dump = string.dump(function()\n    print(\"hello world\")\nend)\nlocal loaded = load(dump)\nloaded()\n\n-&gt; hello world<\/code><\/pre>\n<p>\u4f20\u53c2\u7684\u8bdd\u5c31\u662f\u8fd9\u6837\uff08\u7ed5\u4e00\u5927\u5708\u56de\u5230\u539f\u70b9\uff09<\/p>\n<pre><code class=\"language-LUA\">local dump = string.dump(function(worlds)\n    print(worlds)\nend)\nlocal loadedPrint = load(dump)\nloadedPrint(\"hello world\")<\/code><\/pre>\n<h1>loadfile<\/h1>\n<p>load file \u548c load \u5f88\u63a5\u8fd1\uff0c\u533a\u522b\u5728\u4e8e\u5b83\u662f\u76f4\u63a5\u4ece file \u52a0\u8f7d\u5185\u5bb9\uff0c\u800c\u4e0d\u9700\u8981\u4f60\u6307\u5b9a\u5b57\u7b26\u4e32<\/p>\n<h1>require module<\/h1>\n<p>module \u5c31\u662f\u57fa\u4e8e load \u518d\u8fdb\u884c\u4e86\u4e00\u4e9b\u5e38\u7528\u5c01\u88c5\uff0c<br \/>\n\u4e5f\u5c31\u662f\u8bf4 module \u662f lua \u62bd\u8c61\u51fa\u6765\u7684\u6982\u5ff5\uff0c\u662f\u6a21\u62df OOD \u7684\u4e00\u79cd\u673a\u5236\uff0c\u5e76\u4e0d\u662f\u62e5\u6709\u4ec0\u4e48\u7279\u6743<\/p>\n<p>\u5b83\u7531 require \u548c package \u4e24\u4e2a\u529f\u80fd\u8054\u52a8\u5b9e\u73b0<\/p>\n<p>\u5bf9\u4e8e\u5982\u4f55\u52a0\u8f7d module\uff0c\u6709\u4e13\u95e8\u7684\u56db\u79cd searchers\uff0c\u7528\u6765\u6307\u793a require \u5982\u4f55\u52a0\u8f7d\u4e00\u4e2a\u5916\u90e8\u7684 lua chunk \u4ee3\u7801:<\/p>\n<ul>\n<li>packaged.preloaded<\/li>\n<li>package.path\uff1a<\/li>\n<li>package.searchpath<\/li>\n<li>all-in-one loader<\/li>\n<\/ul>\n<p>\u4e4b\u540e\uff0crequire \u4f1a\u5c06\u6587\u4ef6\u52a0\u8f7d\u3001\u7f13\u5b58\u5230 package.loaded \u8868\uff0c\u7136\u540e\u6bcf\u6b21\u88ab\u8c03\u7528\u65f6\u8fd4\u56de\u8868\u91cc\u7f13\u5b58\u7684\u5185\u5bb9<\/p>\n<p>\u53c2\u8003\u8fd9\u7bc7\u6587\u7ae0\u8fdb\u884c searcher \u7684\u5b9a\u5236\uff1a <a href=\"https:\/\/zhuanlan.zhihu.com\/p\/346355282\">https:\/\/zhuanlan.zhihu.com\/p\/346355282<\/a><\/p>\n<h2>preload<\/h2>\n<p>\u9884\u52a0\u8f7d\u5141\u8bb8\u4f60\u5b9a\u4e49\u4e00\u7cfb\u5217\u6a21\u677f\uff0c\u5728 require \u7684\u65f6\u5019\u8fdb\u884c\u8c03\u7528\uff0c<\/p>\n<pre><code class=\"language-LUA\">package.preload[\"test\"] = function(module)\n    print(module)\nend\n\nlocal test = require(\"test\")<\/code><\/pre>\n<p>\u6b63\u5e38\u4e00\u70b9\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-LUA\">package.preload[\"some module\"] = function(module)\n    local innerVar = 0\n    local module = {}\n    module.Call = function(self)\n        innerVar = innerVar + 1\n        print(innerVar)\n    end\n    return module\nend\n\nlocal module1 = require(\"some module\")\nmodule1:Call()\n\nlocal module2 = require(\"some module\")\nmodule2:Call()\nmodule2:Call()<\/code><\/pre>\n<p>\u548c\u4e00\u822cmodule\u4e00\u6837\uff0c\u5916\u90e8\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u4e5f\u662f module \u5185\u5171\u4eab\u7684\uff0c\u7c7b\u4f3c\u4e8e \u9759\u6001\u53d8\u91cf<br \/>\n\u800c preload \u987e\u540d\u601d\u4e49\u4e5f\u5c31\u662f\u4e00\u79cd\u63d0\u524d\u628a\u9700\u8981\u7684\u6a21\u5757\u90fd\u52a0\u8f7d\u8fdb\u53bb\u3001\u6ce8\u518c\u4e3a\u4e00\u4e2a key\uff0c\u4ee5\u4fbf\u540e\u7eed require \u7684\u65f6\u5019\u91cd\u5b9a\u5411\u7684\u4e00\u79cd\u673a\u5236\uff0c<br \/>\n\u548c\u5e38\u89c4\u610f\u4e49\u4e0a\u7684\u63d0\u524d\u52a0\u8f7d\u4e0d\u592a\u4e00\u6837<\/p>\n<h2>loaded<\/h2>\n<p><a href=\"https:\/\/www.lua.org\/pil\/8.1.html\">Programming in Lua : 8.1<\/a><br \/>\nrequire \u7684\u4f5c\u7528\u7c7b\u4f3c\u4e8e dofile \u4f46\u662f\u66f4\u9762\u5411\u6a21\u5757<br \/>\n\u5b83\u4f1a\u907f\u514d\u91cd\u590d require \u4e00\u4e2a module \u4e24\u6b21\uff0c\u4f1a\u628a\u52a0\u8f7d\u6587\u4ef6\u7f13\u5b58\u5230 _loaded \u76ee\u5f55<br \/>\n\u5b98\u65b9\u4e5f\u63d0\u793a\u5982\u679c\u786e\u5b9e\u6709\u9700\u8981\u53ef\u4ee5\u81ea\u5df1\u628a loaded \u5bf9\u4e8e\u67d0\u4e2a\u6587\u4ef6\u7684\u7d22\u5f15\u7f6e\u4e3a nil<\/p>\n<h1>Enviroment<\/h1>\n<p><a href=\"https:\/\/www.lua.org\/pil\/14.1.html\">Programming in Lua : 14.1<\/a><\/p>\n<p>\u5728 load \u4e2d\u6709 env \u7684\u6982\u5ff5\uff0c\u4e5f\u5c31\u662f\u4e0a\u4e0b\u6587\u73af\u5883<br \/>\n\u800c\u5168\u5c40\u53d8\u91cf\u5c31\u662f\u540c\u4e00\u4e2a lua \u865a\u62df\u673a\uff08\u8fd9\u53d6\u51b3\u4e8e\u4f60\u4f7f\u7528\u7684\u6846\u67b6\uff09\u5185\u90e8\u5171\u4eab\u7684 \u4e0a\u4e0b\u6587\u73af\u5883\uff08\u6216\u8005\u8bf4\u73af\u5883\u53d8\u91cf\uff09<\/p>\n<h2>_G<\/h2>\n<p>_G \u8868\u4f1a\u6301\u6709\u6240\u6709\u5168\u5c40\u73af\u5883\u7684\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u4ee5\u53ca\u5b83\u81ea\u8eab\u7684\u5f15\u7528<\/p>\n<pre><code class=\"language-LUA\">value = \"hello world\"\nprint(_G[\"value\"])\nprint(_G[\"_G\"][\"value\"])<\/code><\/pre>\n<p>\u5305\u62ec\u5185\u7f6e\u7684\u4e00\u4e9b\u65b9\u6cd5\u90fd\u5728 _G \u8868\u4e2d\uff0c\u4f8b\u5982<\/p>\n<pre><code class=\"language-LUA\">_G.print(\"helloworld\")<\/code><\/pre>\n<p>\u4ee5\u81f3\u4e8e\u652f\u6301\u8fd9\u6837\u7684\u9a9a\u64cd\u4f5c<\/p>\n<pre><code class=\"language-LUA\">function func1()\n    print(\"1\")\nend\n\nfunction func2()\n    print(\"2\")\nend\n\nfunction func3()\n    print(\"3\")\nend\n\nfor i=1, 3 do\n    _G[\"func\"..i]()\nend<\/code><\/pre>\n<p>\u5e76\u4e14\u6587\u6863\u91cc\u8fd8\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u66f4\u9a9a\u7684\u64cd\u4f5c\uff0c<br \/>\n\u4f46\u6b63\u5e38\u5f00\u53d1\u4e2d\u4f7f\u7528\u8fd9\u79cd\u64cd\u4f5c\u4f1a\u589e\u52a0 \u7406\u89e3\u96be\u5ea6\u3001\u7ef4\u62a4\u6210\u672c\uff0c\u5c3d\u91cf\u907f\u514d<\/p>\n<h3>_G \u7684\u5143\u8868<\/h3>\n<p>\u5728 Lua \u91cc\u5b9a\u4e49\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3001\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u89c6\u4e3a\u8bbf\u95ee _G \u8868<br \/>\n\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u50cf\u8fd9\u6837\u5b9a\u4e49 _G \u7684 metatable \u6765\u5e2e\u52a9\u6392\u9519<\/p>\n<pre><code class=\"language-LUA\">setmetatable(_G, {\n    __newindex = function(_, n)\n        error(\"n&gt;&gt;&gt; attempt to write to undeclared variable \"..n, 2)\n    end,\n    __index = function(_, n)\n        error(\"n&gt;&gt;&gt; atempt to read to undeclared variable \"..n, 2)\n    end\n})\n\nhahaha()\n\n-&gt;\n&gt;&gt;&gt; atempt to read to undeclared variable hahaha<\/code><\/pre>\n<p>\u5bf9\u5e94\u7684\uff0c\u5982\u679c\u60f3\u76f4\u63a5\u4ece _G \u91cc\u6dfb\u52a0\u53d8\u91cf\uff0c\u53ef\u4ee5\u501f\u52a9 rawset \u907f\u514d\u89e6\u53d1\u5143\u65b9\u6cd5<br \/>\n\u5177\u4f53\u53c2\u8003<a href=\"https:\/\/www.lua.org\/pil\/14.2.html\">\u6587\u6863<\/a><\/p>\n<h2>_ENV<\/h2>\n<pre><code>TODO<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7 load \u8fdb\u884c\u4ee3\u7801\u52a0\u8f7d<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[8],"_links":{"self":[{"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/posts\/115"}],"collection":[{"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=115"}],"version-history":[{"count":2,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":118,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/posts\/115\/revisions\/118"}],"wp:attachment":[{"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}