Skip to content

Commit d9604a5

Browse files
committed
Fnished 'src/techniques.md'.
1 parent 631e57e commit d9604a5

File tree

14 files changed

+535
-5
lines changed

14 files changed

+535
-5
lines changed

scripts/c_func_tips/a

15.3 KB
Binary file not shown.

scripts/c_func_tips/demo_tconcat.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
local mylib = require "mylib"
2+
3+
t = {1, "a", "this", 3.14}
4+
5+
print( mylib.t_concat(t) )
6+

scripts/c_func_tips/demo_tuple.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
local mylib = require "mylib"
2+
3+
x = mylib.new_tuple(10, "hi", {}, 3)
4+
print(x(1))
5+
print(x(2))
6+
print(x())
7+
8+
print(x(10))

scripts/c_func_tips/demo_upvalue.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local mylib = require "mylib"
2+
3+
c1 = mylib.newCounter()
4+
print(c1(), c1(), c1())
5+
6+
c2 = mylib.newCounter()
7+
print(c2(), c2(), c1())

scripts/c_func_tips/main.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
#include "lua.h"
3+
#include "lauxlib.h"
4+
5+
6+
void main () {
7+
char* myStr = "测试字符串";
8+
lua_State *L = luaL_newstate(); /* opens Lua */
9+
10+
/* variable with a unique address */
11+
static char Key = 'k';
12+
13+
/* store a string */
14+
lua_pushstring(L, myStr); /* push value */
15+
lua_rawsetp(L, LUA_REGISTRYINDEX, (void *)&Key); /* registry[&Key] = myStr */
16+
17+
/* retrieve a string */
18+
lua_rawgetp(L, LUA_REGISTRYINDEX, (void *)&Key); /* retrieve value */
19+
myStr = lua_tostring(L, -1); /* convert to string */
20+
21+
printf("%s\n", myStr);
22+
}

scripts/c_func_tips/mylib.so

728 Bytes
Binary file not shown.

scripts/c_func_tips/tips_lib.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,37 @@
22
#include "lauxlib.h"
33
#include "tips_lib.h"
44

5+
int t_tuple (lua_State *L) {
6+
lua_Integer op = luaL_optinteger(L, 1, 0);
7+
if (op == 0) { /* no arguments? */
8+
int i;
9+
/* push each valid upvalue onto the stack */
10+
for (i = 1; !lua_isnone(L, lua_upvalueindex(i)); i++)
11+
lua_pushvalue(L, lua_upvalueindex(i));
12+
return i - 1; /* number of values */
13+
}
14+
else { /* get field 'op' */
15+
luaL_argcheck(L, 0 < op && op <= 256, 1,
16+
"index out of range");
17+
if (lua_isnone(L, lua_upvalueindex(op)))
18+
return 0; /* no such field */
19+
lua_pushvalue(L, lua_upvalueindex(op));
20+
return 1;
21+
}
22+
}
23+
24+
int t_new (lua_State *L) {
25+
int top = lua_gettop(L);
26+
luaL_argcheck(L, top < 256, top, "too many fields");
27+
lua_pushcclosure(L, t_tuple, top);
28+
return 1;
29+
}
30+
31+
int newCounter (lua_State *L) {
32+
lua_pushinteger(L, 0);
33+
lua_pushcclosure(L, &counter, 1);
34+
return 1;
35+
}
536

637
int l_map (lua_State *L) {
738
int i, n;
@@ -26,6 +57,9 @@ int l_map (lua_State *L) {
2657
static const struct luaL_Reg mylib [] = {
2758
{"map", l_map},
2859
{"split", l_split},
60+
{"new_tuple", t_new},
61+
{"t_concat", tconcat},
62+
{"newCounter", newCounter},
2963
{NULL, NULL} /* sentinel */
3064
};
3165

scripts/c_func_tips/tips_lib.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
#include <string.h>
55
#include "lua.h"
66

7-
extern int l_map (lua_State *L);
8-
extern int luaopen_mylib (lua_State *L);
9-
107
static int l_split (lua_State *L) {
118
const char *s = luaL_checkstring(L, 1); /* subject */
129
const char *sep = luaL_checkstring(L, 2); /* separator */
@@ -29,4 +26,29 @@ static int l_split (lua_State *L) {
2926
return 1; /* return the table */
3027
}
3128

29+
30+
static int tconcat (lua_State *L) {
31+
luaL_Buffer b;
32+
int i, n;
33+
34+
luaL_checktype(L, 1, LUA_TTABLE);
35+
n = luaL_len(L, 1);
36+
luaL_buffinit(L, &b);
37+
38+
for (i = 1; i <= n; i++) {
39+
lua_geti(L, 1, i); /* get string from table */
40+
luaL_addvalue(&b); /* add it to the buffer */
41+
}
42+
43+
luaL_pushresult(&b);
44+
return 1;
45+
}
46+
47+
static int counter (lua_State *L) {
48+
int val = lua_tointeger(L, lua_upvalueindex(1));
49+
lua_pushinteger(L, ++val); /* new value */
50+
lua_copy(L, -1, lua_upvalueindex(1)); /* update upvalue */
51+
return 1; /* return new value */
52+
}
53+
3254
#endif

scripts/c_func_tips/tips_lib.o

2.48 KB
Binary file not shown.

scripts/calling_c/calling_c_lib.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#ifndef EXTENDING_LIB_H
32
#define EXTENDING_LIB_H
43

0 commit comments

Comments
 (0)