Debian Codegraph @

js/src/shell/OSObject.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 // OSObject.h - os object for exposing posix system calls in the JS shell
8
9 #ifndef shell_OSObject_h
10 #define shell_OSObject_h
11
12 #include <stdio.h>
13
14 #include "js/TypeDecls.h"
15 #include "js/Utility.h"
16
17 class JSLinearString;
18
19 namespace js {
20 namespace shell {
21
22 #ifdef XP_WIN
23 constexpr char PathSeparator = '\\';
24 #else
25 constexpr char PathSeparator = '/';
26 #endif
27
28 struct RCFile;
29
30 /* Define an os object on the given global object. */
31 bool DefineOS(JSContext* cx, JS::HandleObject global, bool fuzzingSafe,
32 RCFile** shellOut, RCFile** shellErr);
33
34 enum PathResolutionMode { RootRelative, ScriptRelative };
35
36 bool IsAbsolutePath(JSLinearString* filename);
37
38 JSString* ResolvePath(JSContext* cx, JS::HandleString filenameStr,
39 PathResolutionMode resolveMode);
40
41 JSObject* FileAsTypedArray(JSContext* cx, JS::HandleString pathnameStr);
42
43 /**
44 * Return the current working directory as a UTF-8 encoded string.
45 *
46 * @param cx current js-context
47 * @return the working directory name or {@code nullptr} on error
48 */
49 JS::UniqueChars GetCWD(JSContext* cx);
50
51 /**
52 * Open the requested file.
53 *
54 * @param cx current js-context
55 * @param filename file name encoded in UTF-8
56 * @param mode file mode specifier, see {@code fopen} for valid values
57 * @return a FILE pointer or {@code nullptr} on failure
58 */
59 FILE* OpenFile(JSContext* cx, const char* filename, const char* mode);
60
61 /**
62 * Read {@code length} bytes in the given buffer.
63 *
64 * @param cx current js-context
65 * @param filename file name encoded in UTF-8, only used for error reporting
66 * @param file file pointer to read from
67 * @param buffer destination buffer to copy read bytes into
68 * @param length number of bytes to read
69 * @return returns false and reports an error if not exactly {@code length}
70 * bytes could be read from the input file
71 */
72 bool ReadFile(JSContext* cx, const char* filename, FILE* file, char* buffer,
73 size_t length);
74
75 /**
76 * Compute the file size in bytes.
77 *
78 * @param cx current js-context
79 * @param filename file name encoded in UTF-8, only used for error reporting
80 * @param file file object to inspect
81 * @param size output parameter to store the file size into
82 * @return returns false and reports an error if an I/O error occurred
83 */
84 bool FileSize(JSContext* cx, const char* filename, FILE* file, size_t* size);
85
86 /**
87 * Return the system error message for the given error number. The error
88 * message is UTF-8 encoded.
89 *
90 * @param cx current js-context
91 * @param errnum error number
92 * @return error message or {@code nullptr} on error
93 */
94 JS::UniqueChars SystemErrorMessage(JSContext* cx, int errnum);
95
96 } // namespace shell
97 } // namespace js
98
99 #endif /* shell_OSObject_h */