Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/wasmfs/emscripten.cpp
6174 views
1
// Copyright 2023 The Emscripten Authors. All rights reserved.
2
// Emscripten is available under two separate licenses, the MIT license and the
3
// University of Illinois/NCSA Open Source License. Both these licenses can be
4
// found in the LICENSE file.
5
6
//
7
// This file contains implementations of emscripten APIs that are compatible
8
// with WasmFS. These replace APIs in src/library*js, and basically do things
9
// in a simpler manner for the situation where the FS is in wasm and not JS
10
// (in particular, these implementations avoid calling from JS to wasm, and
11
// dependency issues that arise from that).
12
//
13
14
#include <emscripten.h>
15
#include <stdio.h>
16
17
#include <string>
18
19
#include "file.h"
20
#include "file_table.h"
21
#include "wasmfs.h"
22
23
namespace wasmfs {
24
25
// Given an fd, returns the string path that the fd refers to.
26
// TODO: full error handling
27
// TODO: maybe make this a public API, as it is useful for debugging
28
std::string getPath(int fd) {
29
auto fileTable = wasmfs::wasmFS.getFileTable().locked();
30
31
auto openFile = fileTable.getEntry(fd);
32
if (!openFile) {
33
return "!";
34
}
35
36
auto curr = openFile->locked().getFile();
37
std::string result = "";
38
while (curr != wasmfs::wasmFS.getRootDirectory()) {
39
auto parent = curr->locked().getParent();
40
// Check if the parent exists. The parent may not exist if curr was
41
// unlinked.
42
if (!parent) {
43
return "?/" + result;
44
}
45
46
auto parentDir = parent->dynCast<wasmfs::Directory>();
47
auto name = parentDir->locked().getName(curr);
48
result = '/' + name + result;
49
curr = parentDir;
50
}
51
return result;
52
}
53
54
} // namespace wasmfs
55
56
extern "C" {
57
58
char* emscripten_get_preloaded_image_data_from_FILE(FILE* file,
59
int* w,
60
int* h) {
61
auto fd = fileno(file);
62
if (fd < 0) {
63
return 0;
64
}
65
66
auto path = wasmfs::getPath(fd);
67
return emscripten_get_preloaded_image_data(path.c_str(), w, h);
68
}
69
70
} // extern "C"
71
72