Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/kernel/src/wasm/posix/errno.ts
1068 views
1
import constants, { Constant } from "./constants";
2
3
export default function Errno(error: Constant) {
4
const errno = constants[error];
5
const err = Error(`Error ${error} (errno=${errno}).`);
6
(err as any).wasiErrno = errno;
7
return err;
8
}
9
10
// Return map from standard native error codes to
11
// WASM error codes. These can be *very* different
12
// and have to be translated.
13
export function nativeToWasm(posix) {
14
// DO **NOT** add anything more to this, e.g., ENOTSUP, since we are making
15
// a mapping back and forth usig it, and any overlaps will lead to subtle errors!
16
const names = [
17
"E2BIG",
18
"EACCES",
19
"EBADF",
20
"EBUSY",
21
"ECHILD",
22
"EDEADLK",
23
"EEXIST",
24
"EFAULT",
25
"EFBIG",
26
"EINTR",
27
"EINVAL",
28
"EIO",
29
"EISDIR",
30
"EMFILE",
31
"EMLINK",
32
"ENFILE",
33
"ENODEV",
34
"ENOENT",
35
"ENOEXEC",
36
"ENOMEM",
37
"ENOSPC",
38
"ENOTDIR",
39
"ENOTTY",
40
"ENXIO",
41
"EPERM",
42
"EPIPE",
43
"EROFS",
44
"ESPIPE",
45
"ESRCH",
46
"ETXTBSY",
47
"EXDEV",
48
];
49
const map: { [native: number]: number } = {};
50
for (const name of names) {
51
const eNative = posix.constants?.[name];
52
if (!eNative) {
53
throw Error(`posix constant ${name} not known`);
54
}
55
const eWasm = constants[name];
56
if (!eWasm) {
57
throw Error(`wasm constant ${name} not known`);
58
}
59
map[eNative] = eWasm;
60
}
61
return map;
62
}
63
64