Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/kernel/src/wasm/posix/other.ts
1068 views
1
import { notImplemented } from "./util";
2
3
export default function other(context) {
4
const { callFunction, posix, recv, send, wasi } = context;
5
6
context.state.user_from_uid_cache = {};
7
8
function sendStatvfs(bufPtr, x) {
9
callFunction(
10
"set_statvfs",
11
bufPtr,
12
x.f_bsize,
13
x.f_frsize,
14
BigInt(x.f_blocks),
15
BigInt(x.f_bfree),
16
BigInt(x.f_bavail),
17
BigInt(x.f_files),
18
BigInt(x.f_ffree),
19
BigInt(x.f_favail),
20
x.f_fsid,
21
x.f_flag,
22
x.f_namemax
23
);
24
}
25
26
function real_fd(virtual_fd: number): number {
27
const data = wasi.FD_MAP.get(virtual_fd);
28
if (data == null) {
29
return -1;
30
}
31
return data.real;
32
}
33
34
const lib = {
35
syslog: () => {
36
notImplemented("syslog");
37
},
38
login_tty: (fd: number): number => {
39
if (posix.login_tty == null) {
40
notImplemented("login_tty");
41
}
42
posix.login_tty(real_fd(fd));
43
return 0;
44
},
45
46
// TODO: worry about virtual filesystem that WASI provides,
47
// versus this just being the straight real one?!
48
// int statvfs(const char *restrict path, struct statvfs *restrict buf);
49
statvfs: (pathPtr: string, bufPtr: number): number => {
50
if (posix.statvfs == null) {
51
notImplemented("statvfs");
52
}
53
const path = recv.string(pathPtr);
54
sendStatvfs(bufPtr, posix.statvfs(path));
55
return 0;
56
},
57
58
// int fstatvfs(int fd, struct statvfs *buf);
59
fstatvfs: (fd: number, bufPtr: number): number => {
60
if (posix.fstatvfs == null) {
61
notImplemented("fstatvfs");
62
}
63
sendStatvfs(bufPtr, posix.fstatvfs(real_fd(fd)));
64
return 0;
65
},
66
67
ctermid: (ptr?: number): number => {
68
if (posix.ctermid == null) {
69
notImplemented("ctermid");
70
}
71
if (ptr) {
72
const s = posix.ctermid();
73
send.string(s, { ptr, len: s.length + 1 });
74
return ptr;
75
}
76
if (context.state.ctermidPtr) {
77
return context.state.ctermidPtr;
78
}
79
const s = posix.ctermid();
80
return (context.state.ctermidPtr = send.string(s));
81
},
82
83
// password stuff
84
// int getpwnam_r(const char *name, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result);
85
getpwnam_r: (
86
_namePtr: number,
87
_passwdPtr: number,
88
_bufferPtr: number,
89
_bufsize: number,
90
result_ptr_ptr: number
91
): number => {
92
// this means "not found".
93
send.pointer(result_ptr_ptr, 0);
94
return 0;
95
},
96
97
// struct passwd *getpwuid(uid_t uid);
98
getpwuid: () => {
99
// not found
100
return 0;
101
},
102
103
// int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer,
104
// size_t bufsize, struct passwd **result);
105
getpwuid_r: (
106
_uid: number,
107
_passwdPtr: number,
108
_bufferPtr: number,
109
_bufsize: number,
110
result_ptr_ptr: number
111
): number => {
112
send.pointer(result_ptr_ptr, 0);
113
return 0;
114
},
115
116
openpty: () => {
117
// TOOD: plan to do this inspired by https://github.com/microsoft/node-pty, either
118
// using that or just a little inspired by it to add to posix-node.
119
notImplemented("openpty");
120
},
121
122
msync: () => {
123
// This is part of mmap.
124
notImplemented("msync");
125
},
126
127
madvise: () => {
128
notImplemented("madvise");
129
},
130
131
mremap: () => {
132
notImplemented("mremap");
133
},
134
135
// The curses cpython module wants this:
136
// FILE *tmpfile(void);
137
/* ~/test/tmpfile$ more a.c
138
#include<stdio.h>
139
int main() {
140
FILE* f = tmpfile();
141
printf("f = %p\n", f);
142
}
143
~/test/tmpfile$ zig cc -target wasm32-wasi ./a.c
144
./a.c:3:14: warning: 'tmpfile' is deprecated: tmpfile is not defined on WASI [-Wdeprecated-declarations]
145
*/
146
tmpfile: () => {
147
notImplemented("tmpfile");
148
},
149
150
openlog: () => {
151
notImplemented("openlog");
152
},
153
154
// curses also wants this:
155
// int tcflush(int fildes, int action);
156
tcflush: () => {
157
notImplemented("tcflush");
158
},
159
160
// struct passwd *getpwnam(const char *login);
161
getpwnam: () => {
162
console.log("STUB: getpwnam");
163
// return 0 indicates failure
164
return 0;
165
},
166
167
// int getrlimit(int resource, struct rlimit *rlp);
168
getrlimit: () => {
169
notImplemented("getrlimit");
170
},
171
172
// int setrlimit(int resource, const struct rlimit *rlp);
173
setrlimit: () => {
174
notImplemented("setrlimit");
175
},
176
177
// numpy wants this thing that can't exist in wasm:
178
// int backtrace(void** array, int size);
179
// Commenting this out and instead patching numpy to not try to use this, since we
180
// have to do that anyways to get it to build with clang15.
181
// backtrace: () => {
182
// notImplemented("backgrace");
183
// },
184
185
// These are for coreutils, and we come up with a WebAssembly version,
186
// which is the documented fallback.
187
// char * user_from_uid(uid_t uid, int nouser);
188
// char * group_from_gid(gid_t gid, int nogroup);
189
// TODO: for speed this would be better at the C level.
190
user_from_uid: (uid: number, nouser: number = 0): number => {
191
if (nouser) {
192
return 0;
193
}
194
// cache the pointers for speed and to reduce memory leaks
195
if (context.state.user_from_uid_cache[uid]) {
196
return context.state.user_from_uid_cache[uid];
197
}
198
return (context.state.user_from_uid_cache[uid] = send.string(`${uid}`));
199
},
200
group_from_gid: (gid: number, nogroup: number = 0): number => {
201
return lib.user_from_uid(gid, nogroup);
202
},
203
204
// TODO -- see how this is used in code, or maybe make it
205
// do something like "#define getrusage(A,B) memset(B,0,sizeof(*B))"
206
// to make everything 0, as a stub.
207
// int getrusage(int who, struct rusage *r_usage);
208
getrusage: (_who: number, _r_usage_ptr: number): number => {
209
notImplemented("getrusage");
210
return 0;
211
},
212
213
// C++ stuff we don't support:
214
_Znwm: () => {
215
// operator new
216
notImplemented("_Znwm");
217
},
218
_ZdlPv: () => {
219
// operator delete
220
notImplemented("_ZdlPv");
221
},
222
__cxa_throw: () => {
223
notImplemented("__cxa_throw");
224
},
225
// exception
226
__cxa_allocate_exception: () => {
227
notImplemented("__cxa_allocate_exception");
228
},
229
_ZNSt20bad_array_new_lengthC1Ev: () => {
230
notImplemented("_ZNSt20bad_array_new_lengthC1Ev");
231
},
232
_ZNSt20bad_array_new_lengthD1Ev: () => {
233
notImplemented("_ZNSt20bad_array_new_lengthD1Ev");
234
},
235
_ZTISt20bad_array_new_length: () => {
236
notImplemented("_ZTISt20bad_array_new_length");
237
},
238
239
ngettext: () => {
240
notImplemented("ngettext");
241
},
242
dngettext: () => {
243
notImplemented("dngettext");
244
},
245
dcngettext: () => {
246
notImplemented("dcngettext");
247
},
248
};
249
250
return lib;
251
}
252
253