~kvik/lu9-p9

ref: 9d97d20f6fa95b1f6ca455ac6a277b3bced5eb1c lu9-p9/base/base.c -rw-r--r-- 2.1 KiB
9d97d20fkvik misc: implement p9.{enc,dec}{16,32,64} bindings 1 year, 1 month ago
                                                                                
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
#include <u.h>
#include <libc.h>
#include <ctype.h>
#include <tos.h>

#include <lua.h>
#include <lauxlib.h>

#include "../base/common.c"

#include "fs.c"
#include "walk.c"
#include "env.c"
#include "ns.c"
#include "proc.c"
#include "misc.c"

static luaL_Reg p9_module[] = {
	{"open", p9_open},
	{"create", p9_create},
	{"pipe", p9_pipe},
	{"remove", p9_remove},
	{"access", p9_access},
	{"fd", nil}, /* table placeholder */
	
	{"stat", p9_stat},
	{"wstat", p9_wstat},
	{"walk", p9_walk},
	
	{"bind", p9_bind},
	{"mount", p9_mount},
	{"unmount", p9_unmount},
	
	{"getenv", p9_getenv},
	{"setenv", p9_setenv},
	{"env", nil}, /* table placeholder */
	
	{"abort", p9_abort},
	{"exits", p9_exits},
	{"fatal", p9_fatal},
	{"sleep", p9_sleep},
	{"alarm", p9_alarm},
	{"rfork", p9_rfork},
	{"wait", p9_wait},
	{"exec", p9_exec},
	{"wdir", p9_wdir},
	{"pid", p9_pid},
	{"ppid", p9_ppid},
	{"user", p9_user},
	{"sysname", p9_sysname},
	
	{"nanosec", p9_nanosec},
	{"nsec", p9_nsec},
	{"cleanname", p9_cleanname},
	
	{"enc64", p9_enc64},
	{"dec64", p9_dec64},
	{"enc32", p9_enc32},
	{"dec32", p9_dec32},
	{"enc16", p9_enc16},
	{"dec16", p9_dec16},
	
	{nil, nil}
};

int
luaopen_p9(lua_State *L)
{
	int lib;
	Buf *buf;
	
	luaL_newlib(L, p9_module);
	lib = lua_gettop(L);
	
	buf = resizebuffer(L, nil, Iosize);
	lua_pushlightuserdata(L, buf);
	lua_setfield(L, LUA_REGISTRYINDEX, "p9-buffer");
	
	luaL_newmetatable(L, "p9-File");
	luaL_setfuncs(L, p9_file_proto, 0);
	lua_pushvalue(L, -1);
	lua_setfield(L, -2, "__index");
	
	/* Populate p9.fd[] with standard descriptors */
	lua_createtable(L, 3, 0);
	for(int i = 0; i < 3; i++){
		filenew(L, i);
		lua_rawseti(L, -2, i);
	}
	lua_setfield(L, lib, "fd");
	
	static luaL_Reg walkmt[] = {
		{"__close", p9_walkclose},
		{nil, nil},
	};
	luaL_newmetatable(L, "p9-Walk");
	luaL_setfuncs(L, walkmt, 0);
	
	static luaL_Reg envmt[] = {
		{"__index", p9_getenv_index},
		{"__newindex", p9_setenv_newindex},
		{nil, nil},
	};
	lua_createtable(L, 0, 2);
	luaL_setfuncs(L, envmt, 0);
	lua_pushvalue(L, -1);
	lua_setmetatable(L, -2);
	lua_setfield(L, lib, "env");
	
	lua_pushvalue(L, lib);
	return 1;
}