{"id":24,"date":"2022-04-01T11:00:27","date_gmt":"2022-04-01T03:00:27","guid":{"rendered":"http:\/\/www.mapleriver.cn\/?p=24"},"modified":"2022-05-03T10:48:52","modified_gmt":"2022-05-03T02:48:52","slug":"lua%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86","status":"publish","type":"post","link":"https:\/\/www.mapleriver.cn\/?p=24","title":{"rendered":"Lua\u57fa\u7840\u77e5\u8bc6"},"content":{"rendered":"<h3>1.Lua\u76848\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff1a<\/h3>\n<p>&emsp;&emsp; <font color=\"D87093\"><strong>nil\uff0cstring\uff0cnumber\uff0cboolean\uff0ctable\uff0cfunction\uff0cthread\uff0cuserdata<\/strong><\/font><br \/>\n&emsp;&emsp; nil\u8868\u793a\u7a7a\u503c\uff0c\u548c\u5176\u4ed6\u7c7b\u578b\u90fd\u4e0d\u76f8\u7b49\uff0c\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\u53ea\u6709nil\u548cfalse\u8868\u793a\u5426\u5b9a\u3002 number\u8868\u793a\u503c\u7c7b\u578b\uff0c5.3\u4ee5\u524dlua\u662f\u4e0d\u533a\u5206\u6574\u578b\u548c\u6d6e\u70b9\u578b\uff0c5.3\u4ee5\u540e\u65b0\u589e\u4e86\u6574\u578b\u4f5c\u4e3anumber\u7684\u5b50\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528math.type\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u6574\u578b\u8fd8\u662f\u6d6e\u70b9\u578b\u3002<\/p>\n<h3>2.table\u662f\u552f\u4e00\u7684\u6570\u636e\u7ed3\u6784<\/h3>\n<p>&emsp;&emsp; table\u662flua\u552f\u4e00\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4ed6\u7ed3\u6784\u90fd\u53ef\u4ee5\u7528table\u5b9e\u73b0\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7528\u6570\u503c\u4f5c\u7d22\u5f15\uff0c\u4e5f\u53ef\u4ee5\u7528\u5176\u4ed6\u7c7b\u578b(\u6bd4\u5982\u5b57\u7b26\u4e32\uff0c\u51fd\u6570\uff0c\u751a\u81f3\u53e6\u4e00\u4e2atable)\uff0c<font color=\"D87093\">lua\u5185\u90e8\u7528\u4e00\u4e2a\u6570\u7ec4\u548c\u4e00\u4e2a\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0table<\/font>\u3002\u8fd9\u4f7f\u5f97table\u975e\u5e38\u5f3a\u5927\uff0c\u5373\u53ef\u4ee5\u7528\u4f5c\u5b57\u5178\uff0c\u4e5f\u53ef\u4ee5\u7528\u4f5c\u6570\u7ec4\uff0c\u914d\u5408\u5143\u8868\u673a\u5236\u8fd8\u53ef\u4ee5\u6a21\u62df\u9762\u5411\u5bf9\u8c61\u3002lua\u7684\u5f88\u591a\u57fa\u7840\u8bbe\u65bd\uff0c\u6bd4\u5982\u6a21\u5757\uff0c\u5168\u5c40\u53d8\u91cf\uff0c\u5143\u8868\uff0c\u90fd\u662f\u57fa\u4e8etable\u5b9e\u73b0\u7684\u3002\u5728\u8bed\u8a00\u5c42\u9762\u5b83\u662f\u5982\u6b64\u7b80\u6d01\uff0c\u53ef\u53c8\u80fd\u5b9e\u73b0\u65e0\u6bd4\u5f3a\u5927\u7684\u529f\u80fd\u3002 \u5728table\u4e2d\u8981\u8c28\u614e\u7684\u4f7f\u7528nil\uff0c\u4f8b:<\/p>\n<pre><code class=\"language-lua\">    local t = {1, 2, nil, nil, 5}\n    print(#t) --&gt; 5\n    t = {[1]=1, [2]=2, [3]=nil, [4]=nil, [5]=4}\n    print(#t) --&gt; 2<\/code><\/pre>\n<p>&emsp;&emsp; \u4e24\u79cd\u8868\u8fbe\u65b9\u5f0f\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u4e0d\u4e00\u6837\uff0c\u6240\u4ee5\u5728table\u4e2d\u4e0d\u80fd\u4f7f\u7528nil\uff0c\u5e94\u8be5\u4f7f\u7528false\u4ee3\u66ff\u3002<\/p>\n<h3>3.\u51fd\u6570<\/h3>\n<p>&emsp;&emsp; Lua\u51fd\u6570\u652f\u6301\u591a\u8fd4\u56de\u503c\uff0c\u4e5f\u652f\u6301\u53ef\u53d8\u53c2\u6570\uff0c\u4f8b\uff1a<\/p>\n<pre><code class=\"language-lua\">  function myprint(...)\n    local str = table.concat({...}, \\t) .. &#039;\\n&#039;\n    io.stdout:write(str)\n    io.stdout:flush()\n  end\n  function myfun()\n    return 1, 2, 3\n  end\n\n  myprint(myfun())    --&gt; 1   2   3<\/code><\/pre>\n<p>&emsp;&emsp; <font color=\"D87093\">pcall\u4e0expcall:<\/font> lua\u53ef\u4ee5\u4f7f\u7528pcall, xpcall\u6765\u5b9e\u73b0\u9519\u8bef\u7684\u6355\u83b7\uff0c\u5728pcall\u4e2d\u6267\u884c\u7684\u51fd\u6570\u5982\u679c\u51fa\u9519\uff0c\u4f1a\u8fd4\u56defalse\uff0c\u540e\u9762\u5e26\u4e00\u4e2a\u9519\u8bef\u6d88\u606f:\uff08pcall\u76f4\u63a5\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u540e\u9762\u8ddf\u51fd\u6570\u7684\u53c2\u6570\uff09<\/p>\n<pre><code class=\"language-lua\">local function test(a)\n    if a == 2 then\n        error(test error)\n    end\n    return true\nend\nlocal ok, ret = pcall(test, 1)      --&gt; true    true\nlocal ok, ret = pcall(test, 2)      --&gt; false   test.lua:5: test error<\/code><\/pre>\n<p>&emsp;&emsp; pcall\u8fd4\u56de\u7684\u7b2c1\u4e2a\u503c\u8868\u660etest\u51fd\u6570\u662f\u5426\u6210\u529f\u6267\u884c\uff0c\u5982\u679c\u4e3atrue\uff0c\u5219\u540e\u9762\u7684\u8fd4\u56de\u503c\u5c31\u662ftest\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff1b\u5982\u679c\u4e3afalse\uff0c\u540e\u9762\u7684\u503c\u662f\u4e00\u4e2a\u9519\u8bef\u6d88\u606f\u3002\u4f46\u662fpcall\u6709\u65f6\u5019\u5e76\u4e0d\u80fd\u6ee1\u8db3\u8981\u6c42\uff0c\u6bd4\u5982\u6211\u4eec\u60f3\u77e5\u9053\u9519\u8bef\u662f\u5728\u54ea\u91cc\u53d1\u751f\u7684\uff0c\u90a3\u4e48\u5c31\u8981\u7528xpcall\u6765\u5b9e\u73b0\uff1a<font color=\"D87093\">\uff08xpcall\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u8981\u8c03\u7528\u7684\u51fd\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u8c03\u7528\u5931\u8d25\u65f6\u7684\u56de\u8c03\u51fd\u6570\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u5f00\u59cb\u4e3a\u8981\u8c03\u7528\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\uff09<\/font><\/p>\n<pre><code class=\"language-lua\">local function msghander(msg)\n    print(msg)\n    print(debug.traceback())\nend\nlocal ok, ret = xpcall(test, msghander, 1)    --&gt; true    true   \u4e0d\u4f1a\u8c03\u7528msghander\nlocal ok, ret = xpcall(test, msghander, 2)    --&gt; false   nil    \u8c03\u7528msghander<\/code><\/pre>\n<p>&emsp;&emsp; \u5185\u90e8\u51fd\u6570\u91cc\u53ef\u4ee5\u5f15\u7528\u5916\u90e8\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u8fd9\u4e9b\u5f15\u7528\u88ab\u79f0\u4e3aupvalue\uff0c\u800cLua\u7684\u51fd\u6570\u4e5f\u53ef\u4ee5\u53eb\u95ed\u5305\u3002<font color=\"D87093\">\u95ed\u5305\u548cupvalue\u662f\u975e\u5e38\u91cd\u8981\u7684\u7279\u6027\u3002<\/font> \u4f5c\u4e3a\u4e00\u4e2a\u5178\u578b\u7684\u5e94\u7528\uff0c\u5047\u8bbe\u8981\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5bf9\u5916\u53ea\u63d0\u4f9b\u6709\u9650\u7684\u8bbf\u95ee\u63a5\u53e3\uff0c\u800c\u5bf9\u8c61\u5185\u90e8\u7684\u6570\u636e\u4e0d\u80fd\u76f4\u63a5\u88ab\u4fee\u6539\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code class=\"language-lua\">local function new_object()\n    local obj = {       -- \u8fd9\u5c31\u662f\u8981\u521b\u5efa\u7684\u5bf9\u8c61\n        _data1 = 1,     -- \u5047\u8bbe\u8fd9\u662f\u5185\u90e8\u6570\u636e\n        _data2 = 2,     -- \u8fd9\u662f\u5916\u90e8\u53ef\u4fee\u6539\u7684\u6570\u636e\n    }\n    return {            -- \u8fd9\u662f\u8fd4\u56de\u7684\u63a5\u53e3table\n        get_data2 = function() return obj._data2 end,\n        set_data2 = function(value) obj._data2 = value end,\n    }\nend\n\nlocal obj_inteface = new_object()\nobj_inteface.set_data2(100)\nprint(obj_inteface.get_data2())     --&gt; 100<\/code><\/pre>\n<h3>4.\u5143\u8868<\/h3>\n<p>&emsp;&emsp; \u7ed9\u4e00\u4e2atable\u8bbe\u7f6e\u5143\u8868\uff0c\u5f53\u5bf9\u8fd9\u4e2atable\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u65f6\uff0c\u5982\u679c\u5143\u8868\u5b58\u5728\u5bf9\u5e94\u7684\u5143\u65b9\u6cd5\uff0c\u5219\u4f1a\u8c03\u7528\u8be5\u5143\u65b9\u6cd5\u3002\u5143\u8868\u5927\u6982\u662f\u8fd9\u6837\u8bbe\u7f6e\u7684\uff1a<\/p>\n<pre><code class=\"language-lua\">local t = {}    -- \u8868\nlocal mt = {}   -- \u5143\u8868\nsetmetatable(t, mt) -- \u5c06mt\u8bbe\u7f6e\u4e3at\u7684\u5143\u8868<\/code><\/pre>\n<p>&emsp;&emsp; \u5143\u65b9\u6cd5\uff1a<br \/>\n&emsp;&emsp; <strong>1.__tostring<\/strong><br \/>\n&emsp;&emsp; \u5982\u679ct\u7684\u5143\u8868\u5b58\u5728tostring\u5143\u65b9\u6cd5\uff0c\u8c03\u7528tostring(t)\u65f6\uff08\u6216\u8005print(t)\u65f6\uff0c\u56e0\u4e3aprint\u5185\u90e8\u4f1a\u81ea\u52a8\u8c03\u7528tostring\uff09\uff0c\u4f1a\u8c03\u7528\u5143\u8868\u7684tostring\u65b9\u6cd5\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5bf9t\u8fdb\u884c\u5e8f\u5217\u5316\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-lua\">local mt = {\n    __tostring = function(t)\n        return string.format(&quot;a=%s, b=%s&quot;, t.a, t.b)\n    end\n}\nlocal t = {a = 1, b = 2}\nsetmetatable(t, mt)\nprint(tostring(t))      --&gt; a=1,b=2<\/code><\/pre>\n<p>&emsp;&emsp; <strong>2.__pairs<\/strong><br \/>\n&emsp;&emsp; \u5f53\u8c03\u7528pairs(t)\u65f6\uff0c\u5982\u679c\u5143\u8868\u5b58\u5728<strong>__pairs<\/strong>\uff0c\u5c31\u4f1a\u8c03\u7528\u8be5\u5143\u65b9\u6cd5\u5e76\u4f20\u5165t\u3002\u8fd9\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u8868\u7684\u7279\u6b8a\u904d\u5386\u3002<br \/>\n&emsp;&emsp; \u6211\u4eec\u60f3\u904d\u5386\u51fa\u503c\u662fnumber\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u8fd9\u65f6\u5c31\u53ef\u4ee5\u4f7f\u7528<strong>__pairs<\/strong>\u6765\u5b9e\u73b0\uff0c\u5728\u5b9e\u73b0\u4e4b\u524d\u6211\u4eec\u5148\u5bf9\u6cdb\u578bfor\u5faa\u73af\u4f5c\u4e00\u4e2a\u4ecb\u7ecd\uff0cpairs\u8fd4\u56de3\u4e2a\u53d8\u91cf\uff1a<font color=\"D87093\">\u8fed\u4ee3\u5668\u51fd\u6570(\u6bd4\u5982next)\uff0c\u8868\uff0c\u5f53\u524d\u7d22\u5f15<\/font>\u3002for\u5faa\u73af\u4ecepairs\u5f97\u5230\u8fd93\u4e2a\u53d8\u91cf\u540e\uff0c\u4f1a\u8c03\u7528\u8fed\u4ee3\u51fd\u6570\uff0c\u5982\uff1anext(t, index)\uff0c\u4e00\u5f00\u59cbindex\u4e3anil\uff0c\u8fd9\u6837next\u5c31\u8fd4\u56de\u7b2c\u4e00\u4e2aindex\u548cvalue\u3002 \u63a5\u7740for\u5faa\u73af\u4e0d\u65ad\u7684\u8c03\u7528next\u5e76\u4f20\u5165\u4e0a\u4e00\u6b21\u8c03\u7528\u5f97\u5230\u7684index\uff0c\u76f4\u5230next\u8fd4\u56denil\u4e3a\u6b62\u3002<\/p>\n<pre><code class=\"language-lua\">local mt = {\n    __pairs = function(t)\n        local function itr(t, idx)\n            local v\n            while true do\n                idx, v = next(t, idx)\n                if not idx then\n                    return nil\n                elseif type(v) == &#039;number&#039; then\n                    return idx, v\n                end\n            end\n        end\n        return itr, t, nil\n    end\n}\nlocal t = {a = 1, b = &quot;s&quot;, c = &quot;ss&quot;, d = 1}\nsetmetatable(t, mt)\n\nfor k, v in pairs(t) do\n    print(k, v)\nend<\/code><\/pre>\n<p>&emsp;&emsp; <strong>3.__len<\/strong><br \/>\n&emsp;&emsp; \u5982\u679ct\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u7528#t\u53ef\u4ee5\u53d6\u5f97\u8be5\u6570\u7ec4\u7684\u957f\u5ea6\uff0c \u4f46\u5982\u679c\u662f\u5b57\u5178\u5c31\u4e0d\u884c\u4e86\uff0c\u5fc5\u987b\u7528<strong>__len<\/strong>\u5143\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-lua\">local mt = {\n    __len = function(t)\n        local n = 0\n        for k, v in pairs(t) do\n            n = n + 1\n        end\n        return n\n    end\n}\nlocal t = {a = 1, b = &quot;s&quot;, c = &quot;ss&quot;, d = 1}\nsetmetatable(t, mt)\n\nprint(#t)       --&gt; 4<\/code><\/pre>\n<p>&emsp;&emsp; <strong>4.__index<\/strong>\u4e0e<strong>__newindex<\/strong><br \/>\n&emsp;&emsp; \u8fd9\u4e24\u4e2a\u5143\u65b9\u6cd5\u7528\u4e8e\u5bf9table\u7684\u8bfb\u5199\u63a7\u5236\u3002<br \/>\n&emsp;&emsp; \u8bfb\u8868\u4e2dkey\u5173\u8054\u7684\u503c\uff0c\u5982\u679ckey\u4e0d\u5b58\u5728\uff0c\u4f1a\u8c03\u7528\u5143\u8868\u7684index\u3002index\u53ef\u4ee5\u662f\u4e00\u4e2a\u51fd\u6570<strong>__index<\/strong>(t, k)\uff1b\u4e5f\u53ef\u4ee5\u662f\u53e6\u4e00\u4e2a\u8868\uff0c\u6b64\u65f6\u4f1a\u76f4\u63a5\u8bfb\u53d6\u8be5\u8868\u7684key\u503c\u3002\u5982\u679c\u4e0d\u60f3\u8c03\u7528t[k]\u65f6\u89e6\u53d1\u5143\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528rawget(t, k)\u3002<br \/>\n&emsp;&emsp; \u8981\u8bbe\u7f6e\u8868\u4e2dkey\u5173\u8054\u7684\u503c\uff0c\u5982\u679ckey\u4e0d\u5b58\u5728\uff0c\u4f1a\u8c03\u7528\u5143\u8868\u7684newindex\u3002\u540c\u6837newindex\u53ef\u4ee5\u662f\u4e00\u4e2a\u51fd\u6570<strong>__newindex<\/strong>(t, k, v)\uff1b\u4e5f\u53ef\u4ee5\u662f\u53e6\u4e00\u5f20\u8868\uff0c\u6b64\u65f6\u4f1a\u76f4\u63a5\u5411\u8be5\u8868\u7684key\u8bbe\u7f6e\u503c\u3002\u5982\u679c\u4e0d\u60f3t[k]=v\u65f6\u89e6\u53d1\u5143\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528rawset(t, k, v)\u3002<br \/>\n&emsp;&emsp; \u8fd9\u4e24\u4e2a\u5143\u65b9\u6cd5\uff0c\u53ef\u7528\u4e8e\u5b9e\u73b0\u7c7b\u4f3c\u9762\u5411\u5bf9\u8c61\u7684\u673a\u5236\u3002<br \/>\n&emsp;&emsp; P.S. <font color=\"D87093\">rawget\u4e0erawset\u5c31\u662f\u4e0d\u4f7f\u7528\u5143\u8868\u548c\u5143\u65b9\u6cd5\u7684\u60c5\u51b5\u4e0b\u5bf9\u8868\u7684\u8bbf\u95ee\u548c\u8d4b\u503c\u3002<\/font><br \/>\n&emsp;&emsp; <strong>5.__call<\/strong><br \/>\n&emsp;&emsp; \u8fd9\u4e2a\u5143\u65b9\u6cd5\u53ef\u4f7f\u975e\u51fd\u6570\u5bf9\u8c61\u50cf\u51fd\u6570\u4e00\u6837\u8c03\u7528\uff0c\u6bd4\u5982\u4e00\u4e2a\u8868t\uff0c\u6211\u4eec\u8fd9\u6837\u5199\uff1at(&quot;hi&quot;)\uff0c\u5982\u679c\u5b83\u7684\u5143\u8868\u5b58\u5728<strong>__call<\/strong>\u51fd\u6570\uff0c\u5219\u4f1a\u89e6\u53d1\u8c03\u7528\uff1a<\/p>\n<pre><code class=\"language-lua\">__call = function(t, a1)        -- a1 == &quot;hi&quot;\n    --do something\nend<\/code><\/pre>\n<h3>5.Debug\u5e93<\/h3>\n<p>&emsp;&emsp; debug.debug \u8fdb\u5165\u547d\u4ee4\u884c\u4ea4\u4e92\u6a21\u5f0f\uff0c\u5728\u8fd9\u91cc\u76f8\u5f53\u4e8e\u8fdb\u5165\u4e86\u865a\u62df\u673a\u5185\u90e8\uff0c\u53ef\u4ee5\u67e5\u770b\u5168\u5c40\u53d8\u91cf\uff0c\u4f46\u662f\u8fd9\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u4e0a\u4e0b\u6587\u73af\u5883\uff0c\u4e0d\u80fd\u67e5\u770b\u8c03\u7528\u5b83\u7684\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u3002<br \/>\n&emsp;&emsp; debug.getinfo ([thread,] f [, what]) \u53d6\u51fd\u6570\u8c03\u8bd5\u4fe1\u606f\uff0cf\u53ef\u4ee5\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f\u8c03\u7528\u5806\u6808\u7684\u5c42\u6570\uff0c1\u8868\u793a\u8c03\u7528getinfo\u7684\u51fd\u6570\uff0c2\u8868\u793a\u518d\u4e0a\u4e00\u5c42\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002what\u6307\u5b9a\u60f3\u67e5\u54ea\u4e9b\u4fe1\u606f\uff0c\u4e0d\u6307\u5b9a\u8868\u793a\u5168\u90e8\u4fe1\u606f\u3002<br \/>\n&emsp;&emsp; debug.getlocal ([thread,] f, local) \u53d6\u51fd\u6570\u7684\u672c\u5730\u53d8\u91cf\uff0c\u6ce8\u610ff\u662f\u6808\u7684\u5c42\u6b21\uff0c\u548c\u4e0a\u9762\u4e00\u68371\u8868\u793a\u8c03\u7528debug.getlocal\u7684\u51fd\u6570\u3002local\u4ece1\u5f00\u59cb\u53d6\u51fd\u6570\u53c2\u6570\u548c\u672c\u5730\u53d8\u91cf\uff0c\u4ece-1\u5f00\u59cb\u662f\u53ef\u53d8\u53c2\u6570\uff0c\u8fd4\u56de\u540d\u5b57\u548c\u503c\u3002\u5982\u679c\u53d6\u4e0d\u5230\u8fd4\u56denil<br \/>\n&emsp;&emsp; debug.getupvalue (f, up) \u53d6\u51fd\u6570\u5f15\u7528\u7684upvalue\uff0cf\u662f\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0cup\u4ece1\u5f00\u59cb\uff0c\u8fd4\u56de\u540d\u5b57\u548c\u503c\u3002\u5982\u679c\u53d6\u4e0d\u5230\u8fd4\u56denil\u3002<br \/>\n&emsp;&emsp; debug.sethook ([thread,] hook, mask [, count])\uff0c\u8bbe\u7f6e\u4e00\u4e2ahook\uff0c\u5f53\u6307\u5b9a\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u8fd9\u4e2ahook\u4f1a\u88ab\u56de\u8c03\uff0chook\u662f\u56de\u8c03\u51fd\u6570\uff0cmask\u8868\u660e\u60f3\u89e6\u53d1\u4ec0\u4e48\u4e8b\u4ef6\uff0c\u5982\u679cmask\u4e3a\u7a7a\uff0ccount\u987b\u6307\u5b9a\u4e00\u4e2a\u503c\u8868\u793a\u6267\u884c\u591a\u5c11\u6761\u6307\u4ee4\u540e\u89e6\u53d1\u3002mask\u662f\u4e0b\u9762\u503c\u7684\u7ec4\u5408\uff1a<\/p>\n<ul>\n<li>'c' \u6bcf\u6b21\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u89e6\u53d1\u3002<\/li>\n<li>'l' \u6bcf\u6267\u884c\u4e00\u884c\u89e6\u53d1\u3002<\/li>\n<li>'r' \u6bcf\u6b21\u4ece\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u89e6\u53d1<\/li>\n<\/ul>\n<p>&emsp;&emsp; debug.traceback ([thread,] [message [, level]]) \u8fd4\u7528\u8c03\u7528\u5806\u6808\u7684\u4fe1\u606f\uff0cmessage\u8868\u793a\u8fd4\u56de\u7684\u5934\u90e8\u52a0\u4e00\u6bb5\u4fe1\u606f\uff0clevel\u8868\u793a\u6808\u7684\u5c42\u6b21\uff0c1\u8868\u793a\u8c03\u7528debug.traceback\u90a3\u4e2a\u51fd\u6570\uff0c\u5f80\u4e0a\u7c7b\u63a8\u3002<br \/>\n&emsp;&emsp; debug.setupvalue (f, up, value) \u7ed9\u51fd\u6570f\u8bbe\u7f6eupvalue\uff0cup\u540c\u6837\u662f\u4e00\u4e2a\u5e8f\u53f7<br \/>\n&emsp;&emsp; debug.setlocal ([thread,] level, local, value) \u7ed9\u51fd\u6570\u8bbe\u7f6e\u672c\u5730\u53d8\u91cf, level\u662f\u6808\u5c42\u6b21\uff0clocal\u662f\u672c\u5730\u53d8\u91cf\u7684\u5e8f\u53f7\uff0cvalue\u662f\u503c\u3002<\/p>\n<h3>6.\u534f\u7a0b<\/h3>\n<pre><code class=\"language-lua\">function foo (a)\n    print(&quot;foo&quot;, a)\n    return coroutine.yield(2*a)\nend\n\nco = coroutine.create(function (a,b)\n    print(&quot;co-body&quot;, a, b)\n    local r = foo(a+1)\n    print(&quot;co-body&quot;, r)\n    local r, s = coroutine.yield(a+b, a-b)\n    print(&quot;co-body&quot;, r, s)\n    return b, &quot;end&quot;\nend)\n\nprint(&quot;main&quot;, coroutine.resume(co, 1, 10))\nprint(&quot;main&quot;, coroutine.resume(co, &quot;r&quot;))\nprint(&quot;main&quot;, coroutine.resume(co, &quot;x&quot;, &quot;y&quot;))\nprint(&quot;main&quot;, coroutine.resume(co, &quot;x&quot;, &quot;y&quot;))<\/code><\/pre>\n<p>&emsp;&emsp; \u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-lua\">co-body 1       10\nfoo     2\nmain    true    4\nco-body r\nmain    true    11      -9\nco-body x       y\nmain    true    10      end\nmain    false   cannot resume dead coroutine<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.Lua\u76848\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff1a &emsp;&emsp; nil\uff0cstring\uff0cnumber\uff0cboolean\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-lua"],"_links":{"self":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24"}],"version-history":[{"count":11,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":302,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions\/302"}],"wp:attachment":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}