目录

  1. lua堆栈操作

只有疯狂过,你才知道自己究竟能不能成功。

lua堆栈操作

针对于lua堆栈的操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
static void stackDump(lua_State* L) {
	int i;
	int top = lua_gettop(L);
	for (int i = 0; i <= top; i++)
	{
		int t = lua_type(L, i);
		switch (t)
		{
		case LUA_TSTRING: {
			printf("%s", lua_tostring(L, i));
			break;
		}
		case LUA_TBOOLEAN: {
			printf(lua_toboolean(L, i) ? "true" : "false");
			break;
		}
		case LUA_TNUMBER: {
			printf("%g", lua_tonumber(L, i));
			break;
		}
		default:
			printf("%s", lua_typename(L, t));
			break;
		}
		printf(" ");
	}
	printf("n");
}

static void test() {
lua_State* L = luaL_newstate();

<span class="n">lua_pushboolean</span><span class="p">(</span><span class="n">L</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="n">lua_pushnumber</span><span class="p">(</span><span class="n">L</span><span class="p">,</span> <span class="mi">10</span><span class="p">);</span>
<span class="n">lua_pushnil</span><span class="p">(</span><span class="n">L</span><span class="p">);</span>
<span class="n">lua_pushstring</span><span class="p">(</span><span class="n">L</span><span class="p">,</span> <span class="s">&#34;hello&#34;</span><span class="p">);</span>
<span class="n">stackDump</span><span class="p">(</span><span class="n">L</span><span class="p">);</span><span class="c1">//true 10 nil hello

lua_pushvalue(L, -4);//将指定索引的值压到栈顶
stackDump(L); //true 10 nil hello true
lua_replace(L, 3);//pop栈顶元素,并将pop的值设置到指定索引
stackDump(L);// true 10 true hello
lua_settop(L, 6);//设置栈中元素个数,0的话清空栈,大于原来个数补nil
stackDump(L);//true 10 true hello nil nil
lua_rotate(L, 3, 1);//旋转指定位置的值,并把栈顶元素补充过来
stackDump(L);//true 10 nil true hello nil
lua_remove(L, -3);//移除指定位置的值
stackDump(L);//true 10 nil hello nil
lua_close(L);
}

            <hr style="visibility: hidden;"/>
            
            <hr style="visibility: hidden;"/>