-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathchapter27.cpp
150 lines (131 loc) · 3.25 KB
/
chapter27.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//g++.exe -g main.cpp -o main.exe -llua
//put 'liblua.a' in the mingw's library path. (e.g. C:\TDM-GCC-64\x86_64-w64-mingw32\lib)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
extern "C"
{
#include "../lua_src/lua.h"
#include "../lua_src/lauxlib.h"
#include "../lua_src/lualib.h"
}
static void stackDump(lua_State *L)
{
int i;
int top = lua_gettop(L); // stack's depth
for (i = 1; 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");
}
void Example27_2()
{
lua_State *L = luaL_newstate();
lua_pushboolean(L, 1);
lua_pushnumber(L, 10);
lua_pushnil(L);
lua_pushstring(L, "hello");
stackDump(L); // true 10 nil hello
lua_pushvalue(L, -4); stackDump(L); // true 10 nil hello true
lua_replace(L, 3); stackDump(L); // true 10 true hello
lua_settop(L, 6); 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_settop(L, -5); stackDump(L); // true
lua_close(L);
}
void Exercise27_1()
{
printf("simple lua.exe\n");
char buff[255];
int error;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
while (fgets(buff, sizeof(buff), stdin) != NULL)
{
error = luaL_loadstring(L, buff) || lua_pcall(L, 0, 0, 0);
if (error)
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
}
void Exercise27_2()
{
lua_State *L = luaL_newstate();
lua_pushnumber(L, 3.5); // 3.5
lua_pushstring(L, "hello"); // 3.5 hello
lua_pushnil(L); // 3.5 hello nil
lua_rotate(L, 1, -1); // hello nil 3.5
lua_pushvalue(L, -2); // hello nil 3.5 nil
lua_remove(L, 1); // nil 3.5 nil
lua_insert(L, -2); // nil nil 3.5
// exercise27_3
stackDump(L);
}
void *allocator(void *ud,void *ptr,size_t osize,size_t nsize)
{
if (nsize == 0)
{
free(ptr);
return NULL;
}
else
{
int* limit = (int*)ud;
if(limit != NULL && *limit < nsize)
{
printf("fail, out of memory!\n");
return NULL;
}
return realloc(ptr , nsize);
}
}
void setlimit(lua_State* L,int* memsize)
{
lua_setallocf(L , allocator, memsize);
}
void Exercise27_4()
{
lua_State *L = luaL_newstate();
int limit = 8;
setlimit(L, &limit);
printf("Push 1234567\n");
lua_pushstring(L,"1234567"); //need 7
printf("Push 89\n");
lua_pushstring(L, "89"); // need 9
}
int main(void)
{
//Example27_2();
//Exercise27_1();
//Exercise27_2();
Exercise27_4();
getchar();
}