Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/wasmfs/no_fs.c
6174 views
1
//
2
// "No-FS" implementation for WasmFS. This file implements just enough of the
3
// syscall API to support printf. In particular, enough for a program that
4
// only does some file writes, but no open() or such. Then those writes must be
5
// to the standard streams, and we can implement very simple printing for them
6
// as opposed to linking in all of WasmFS.
7
//
8
// This uses weak linking, and as such it depends on all the normal syscalls
9
// being implemented in a single file, which is the case (syscalls.cpp). That
10
// way, if that single other file is included then all these methods get
11
// overridden. (Otherwise, if we had multiple files, we could end up with a mix
12
// of this file's functions and others.)
13
//
14
15
#include <emscripten.h>
16
#include <wasi/api.h>
17
18
#include "wasmfs_internal.h"
19
20
#define WEAK __attribute__((weak))
21
22
// Import the outside (JS or VM) fd_write under a different name. We must
23
// implement __wasi_fd_write in this file so that all the normal WasmFS code is
24
// not included - that is the point of no-fs mode - since if we don't implement
25
// it here then the normal WasmFS code for that function will be linked in. But
26
// in the cases where this file is linked in, we just want to direct all calls
27
// to the outside fd_write.
28
__attribute__((import_module("wasi_snapshot_preview1"),
29
import_name("fd_write"))) __wasi_errno_t
30
imported__wasi_fd_write(__wasi_fd_t fd,
31
const __wasi_ciovec_t* iovs,
32
size_t iovs_len,
33
__wasi_size_t* nwritten);
34
35
WEAK
36
__wasi_errno_t __wasi_fd_write(__wasi_fd_t fd,
37
const __wasi_ciovec_t* iovs,
38
size_t iovs_len,
39
__wasi_size_t* nwritten) {
40
return imported__wasi_fd_write(fd, iovs, iovs_len, nwritten);
41
}
42
43
WEAK
44
__wasi_errno_t __wasi_fd_close(__wasi_fd_t fd) {
45
// The only possible file descriptors are the standard streams, and there is
46
// nothing special to do to close them.
47
return __WASI_ERRNO_SUCCESS;
48
}
49
50
WEAK
51
__wasi_errno_t __wasi_fd_fdstat_get(__wasi_fd_t fd, __wasi_fdstat_t* stat) {
52
// This operation is not supported (but it does appear in system libraries
53
// even in hello world, even if it isn't actually called, so we do need to
54
// implement this stub here).
55
return __WASI_ERRNO_NOSYS;
56
}
57
58