Path: blob/main/system/lib/wasmfs/emscripten.cpp
6174 views
// Copyright 2023 The Emscripten Authors. All rights reserved.1// Emscripten is available under two separate licenses, the MIT license and the2// University of Illinois/NCSA Open Source License. Both these licenses can be3// found in the LICENSE file.45//6// This file contains implementations of emscripten APIs that are compatible7// with WasmFS. These replace APIs in src/library*js, and basically do things8// in a simpler manner for the situation where the FS is in wasm and not JS9// (in particular, these implementations avoid calling from JS to wasm, and10// dependency issues that arise from that).11//1213#include <emscripten.h>14#include <stdio.h>1516#include <string>1718#include "file.h"19#include "file_table.h"20#include "wasmfs.h"2122namespace wasmfs {2324// Given an fd, returns the string path that the fd refers to.25// TODO: full error handling26// TODO: maybe make this a public API, as it is useful for debugging27std::string getPath(int fd) {28auto fileTable = wasmfs::wasmFS.getFileTable().locked();2930auto openFile = fileTable.getEntry(fd);31if (!openFile) {32return "!";33}3435auto curr = openFile->locked().getFile();36std::string result = "";37while (curr != wasmfs::wasmFS.getRootDirectory()) {38auto parent = curr->locked().getParent();39// Check if the parent exists. The parent may not exist if curr was40// unlinked.41if (!parent) {42return "?/" + result;43}4445auto parentDir = parent->dynCast<wasmfs::Directory>();46auto name = parentDir->locked().getName(curr);47result = '/' + name + result;48curr = parentDir;49}50return result;51}5253} // namespace wasmfs5455extern "C" {5657char* emscripten_get_preloaded_image_data_from_FILE(FILE* file,58int* w,59int* h) {60auto fd = fileno(file);61if (fd < 0) {62return 0;63}6465auto path = wasmfs::getPath(fd);66return emscripten_get_preloaded_image_data(path.c_str(), w, h);67}6869} // extern "C"707172