{"id":2344,"date":"2025-09-19T15:20:20","date_gmt":"2025-09-19T07:20:20","guid":{"rendered":"https:\/\/www.madbull.site\/?p=2344"},"modified":"2025-10-15T10:56:32","modified_gmt":"2025-10-15T02:56:32","slug":"lua-c-api-%e4%b8%ad%e7%9a%84-lua_rawseti-%e4%b8%8e-lua_rawgeti-%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=2344","title":{"rendered":"Lua C API \u4e2d\u7684 lua_rawseti \u4e0e lua_rawgeti  \u4ecb\u7ecd"},"content":{"rendered":"\n<p>\u5728 Lua \u7684 C API \u4e2d\uff0c<code>lua_rawseti<\/code> \u548c <code>lua_rawgeti<\/code> \u662f\u4e24\u4e2a\u7528\u4e8e<strong>\u76f4\u63a5\u64cd\u4f5c\u8868\u4e2d\u6574\u6570\u952e<\/strong>\u7684\u51fd\u6570\u3002\u5b83\u4eec\u4e0d\u89e6\u53d1\u5143\u8868\u673a\u5236\uff0c\u6267\u884c\u7684\u662f\u201c\u539f\u59cb\u201d\u7684\u8bfb\u5199\u64cd\u4f5c\uff0c\u5e38\u7528\u4e8e\u6027\u80fd\u654f\u611f\u6216\u9700\u8981\u7ed5\u8fc7\u5143\u65b9\u6cd5\u7684\u573a\u666f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u51fd\u6570\u539f\u578b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>void<\/strong> <strong>lua_rawseti<\/strong> (lua_State *L, <strong>int<\/strong> index, lua_Integer n);\n<strong>void<\/strong> <strong>lua_rawgeti<\/strong> (lua_State *L, <strong>int<\/strong> index, lua_Integer n);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>L<\/code>\uff1aLua \u72b6\u6001\u673a\u3002<\/li>\n\n\n\n<li><code>index<\/code>\uff1a\u6808\u4e2d<strong>\u8868\u7684\u4f4d\u7f6e<\/strong>\uff08\u53ef\u4ee5\u662f\u6b63\u6570\u3001\u8d1f\u6570\u6216\u4f2a\u7d22\u5f15\u5982 <code>LUA_REGISTRYINDEX<\/code>\uff09\u3002<\/li>\n\n\n\n<li><code>n<\/code>\uff1a<strong>\u6574\u6570\u952e<\/strong>\uff0c\u901a\u5e38\u4ece 1 \u5f00\u59cb\uff08Lua \u6570\u7ec4\u60ef\u4f8b\uff09\uff0c\u4f46\u4e5f\u652f\u6301 0\u3001\u8d1f\u6570\u6216\u5927\u6574\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u529f\u80fd\u8bf4\u660e<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 <code>lua_rawseti(L, index, n)<\/code><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06<strong>\u6808\u9876\u7684\u503c<\/strong>\u8d4b\u7ed9\u8868\u7684\u7b2c <code>n<\/code> \u4e2a\u4f4d\u7f6e\uff1a<code>table[n] = value<\/code><\/li>\n\n\n\n<li>\u8d4b\u503c\u540e<strong>\u5f39\u51fa\u6808\u9876\u7684\u503c<\/strong><\/li>\n\n\n\n<li><strong>\u4e0d\u89e6\u53d1 <code>__newindex<\/code><\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lua_newtable(L);              \/\/ \u521b\u5efa\u8868 \u2192 \u6808\u9876\nlua_pushstring(L, \"hello\");   \/\/ \u538b\u5165\u503c \u2192 \u6808\u9876\u662f \"hello\"\uff0c\u8868\u5728 -2\nlua_rawseti(L, -2, 1);        \/\/ table&#91;1] = \"hello\"\uff0c\u5f39\u51fa \"hello\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 <code>lua_rawgeti(L, index, n)<\/code><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4ece\u8868\u4e2d\u53d6\u51fa\u7b2c <code>n<\/code> \u4e2a\u503c\uff0c\u5e76<strong>\u538b\u5165\u6808\u9876<\/strong><\/li>\n\n\n\n<li><strong>\u4e0d\u89e6\u53d1 <code>__index<\/code><\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u793a\u4f8b\uff08\u63a5\u4e0a\u4e00\u4e2a\u4f8b\u5b50\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lua_rawgeti(L, -1, 1);        \/\/ \u53d6 table&#91;1]\uff0c\u538b\u5165\u6808\u9876\n<strong>const<\/strong> <strong>char<\/strong>* s = lua_tostring(L, -1);  \/\/ \u5f97\u5230 \"hello\"\uff0c\u5c06 Lua \u6808\u4e0a\u7684\u67d0\u4e2a\u503c\u8f6c\u6362\u4e3a C \u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff08const char*\uff09\nlua_pop(L, 1);                \/\/ \u6e05\u7406\u6808\u9876\uff0c\u4ece Lua \u6808\uff08Lua Stack\uff09\u7684\u9876\u90e8\u79fb\u9664\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\u3002<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u5173\u952e\u7279\u6027<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\ud83d\udd01 <strong>\u4e0d\u89e6\u53d1\u5143\u8868<\/strong><\/td><td>\u7ed5\u8fc7 <code>__index<\/code> \/ <code>__newindex<\/code>\uff0c\u9002\u5408\u5728\u5143\u65b9\u6cd5\u5185\u90e8\u5b89\u5168\u64cd\u4f5c\u8868<\/td><\/tr><tr><td>\ud83c\udfaf <strong>\u4ec5\u652f\u6301\u6574\u6570\u952e<\/strong><\/td><td>\u5982\u9700\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u7c7b\u578b\u952e\uff0c\u8bf7\u4f7f\u7528 <code>lua_rawget<\/code> \/ <code>lua_rawset<\/code><\/td><\/tr><tr><td>\ud83e\uddf1 <strong>\u52a8\u6001\u6269\u5c55\u8868<\/strong><\/td><td>\u5373\u4f7f\u952e <code>n<\/code> \u539f\u672c\u4e0d\u5b58\u5728\uff0c\u4e5f\u4f1a\u81ea\u52a8\u521b\u5efa\uff0c<strong>\u4e0d\u4f1a\u62a5\u9519<\/strong><\/td><\/tr><tr><td>\u26a1 <strong>\u6027\u80fd\u7565\u4f18<\/strong><\/td><td>\u65e0\u5143\u8868\u67e5\u627e\u5f00\u9500\uff0c\u9002\u5408\u9ad8\u9891\u6570\u7ec4\u64cd\u4f5c<\/td><\/tr><tr><td>\ud83d\udce6 <strong>\u5e38\u7528\u6ce8\u518c\u8868\u64cd\u4f5c<\/strong><\/td><td>\u914d\u5408 <code>luaL_ref<\/code> \u4f7f\u7528\uff0c\u5b58\u53d6\u5f15\u7528\u7f16\u53f7\uff1a<code>lua_rawgeti(L, LUA_REGISTRYINDEX, ref)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u5178\u578b\u4f7f\u7528\u573a\u666f<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5728 <code>__index<\/code> \/ <code>__newindex<\/code> \u5143\u65b9\u6cd5\u4e2d\u64cd\u4f5c\u8868<\/strong><br>\u2192 \u907f\u514d\u9012\u5f52\u8c03\u7528\u3002<\/li>\n\n\n\n<li><strong>\u64cd\u4f5c\u6ce8\u518c\u8868\u4e2d\u7684\u5f15\u7528<\/strong><br>\u2192 <code>luaL_ref<\/code> \u8fd4\u56de\u6574\u6570\uff0c\u7528 <code>lua_rawgeti<\/code> \u53d6\u56de\u3002<\/li>\n\n\n\n<li><strong>\u9ad8\u6548\u8bbf\u95ee\u6570\u7ec4\u578b\u6570\u636e\u7ed3\u6784<\/strong><br>\u2192 \u5982\u5411\u91cf\u3001\u5217\u8868\u3001\u7f13\u51b2\u533a\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u5b9e\u73b0\u5185\u90e8\u7f13\u5b58\u6216\u7d22\u5f15\u7ed3\u6784<\/strong><br>\u2192 \u7528\u6574\u6570 ID \u5feb\u901f\u5b58\u53d6\uff0c\u907f\u514d\u5b57\u7b26\u4e32\u67e5\u627e\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u2705 <strong>\u6808\u64cd\u4f5c\u8981\u5c0f\u5fc3<\/strong>\uff1a<code>rawseti<\/code> \u4f1a\u5f39\u51fa\u6808\u9876\u503c\uff0c<code>rawgeti<\/code> \u4f1a\u538b\u5165\u503c \u2014\u2014 \u6ce8\u610f\u6808\u5e73\u8861\u3002<\/li>\n\n\n\n<li>\u2705 <strong>\u952e\u4ece 1 \u5f00\u59cb\u662f\u60ef\u4f8b<\/strong>\uff0c\u4f46\u975e\u5f3a\u5236\uff1b<code>n=0<\/code>\u3001<code>n=-1<\/code> \u4e5f\u53ef\u7528\uff0c\u4f46\u9700\u660e\u786e\u610f\u56fe\u3002<\/li>\n\n\n\n<li>\u274c <strong>\u4e0d\u8981\u7528\u4e8e\u975e\u8868\u5bf9\u8c61<\/strong>\uff1a\u82e5 <code>index<\/code> \u4e0d\u6307\u5411\u8868\uff0c\u4f1a\u62a5\u7c7b\u578b\u9519\u8bef\u3002<\/li>\n\n\n\n<li>\u274c <strong>\u4e0d\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u952e<\/strong>\uff1a\u8bf7\u7528 <code>lua_rawset<\/code> \/ <code>lua_rawget<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5bf9\u6bd4\u5176\u4ed6\u51fd\u6570<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u89e6\u53d1\u5143\u8868\uff1f<\/th><th>\u952e\u7c7b\u578b<\/th><th>\u7528\u9014<\/th><\/tr><\/thead><tbody><tr><td><code>lua_gettable<\/code> \/ <code>lua_settable<\/code><\/td><td>\u2705 \u662f<\/td><td>\u4efb\u610f<\/td><td>\u666e\u901a\u8868\u8bbf\u95ee<\/td><\/tr><tr><td><code>lua_rawget<\/code> \/ <code>lua_rawset<\/code><\/td><td>\u274c \u5426<\/td><td>\u4efb\u610f<\/td><td>\u7ed5\u8fc7\u5143\u8868\uff0c\u4efb\u610f\u952e<\/td><\/tr><tr><td><code>lua_rawgeti<\/code> \/ <code>lua_rawseti<\/code><\/td><td>\u274c \u5426<\/td><td><strong>\u6574\u6570<\/strong><\/td><td>\u7ed5\u8fc7\u5143\u8868\uff0c\u6570\u7ec4\u8bbf\u95ee\uff0c\u6027\u80fd\u66f4\u4f18<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u603b\u7ed3<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>lua_rawgeti<\/code> \/ <code>lua_rawseti<\/code> \u662f<strong>\u4e13\u4e3a\u6574\u6570\u952e\u8bbe\u8ba1\u7684\u539f\u59cb\u8868\u64cd\u4f5c\u51fd\u6570<\/strong>\u3002<\/li>\n\n\n\n<li>\u5b83\u4eec<strong>\u5b89\u5168\u3001\u9ad8\u6548\u3001\u7a33\u5b9a<\/strong>\uff0c\u662f Lua C API \u4e2d\u6700\u5e38\u7528\u7684\u57fa\u7840\u5de5\u5177\u4e4b\u4e00\u3002<\/li>\n\n\n\n<li>\u65e0\u8bba\u76ee\u6807\u952e\u662f\u5426\u5b58\u5728\uff0c\u64cd\u4f5c\u90fd\u4e0d\u4f1a\u62a5\u9519 \u2014\u2014 Lua \u8868\u4f1a\u81ea\u52a8\u6269\u5c55\u3002<\/li>\n\n\n\n<li>\u5728\u5143\u8868\u64cd\u4f5c\u3001\u6ce8\u518c\u8868\u7ba1\u7406\u3001\u6570\u7ec4\u5904\u7406\u7b49\u573a\u666f\u4e2d\u4e0d\u53ef\u6216\u7f3a\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Lua \u7684 C API \u4e2d\uff0clua_rawseti \u548c lua_rawgeti \u662f\u4e24\u4e2a\u7528\u4e8e\u76f4\u63a5\u64cd\u4f5c\u8868\u4e2d\u6574\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2330,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-my","format":"standard","meta":{"footnotes":""},"categories":[156,695,154],"tags":[175,690],"class_list":["post-2344","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-lua","category-154","tag-c","tag-lua"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2344"}],"version-history":[{"count":4,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2344\/revisions"}],"predecessor-version":[{"id":2386,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2344\/revisions\/2386"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/2330"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}