Skip to content

Commit 6ada255

Browse files
authored
🤖 Merge PR DefinitelyTyped#73530 feat(emscripten) : Update FileSystem API by @R3gardless
1 parent 042dd4f commit 6ada255

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

‎types/emscripten/emscripten-tests.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ function FSTest(): void {
7171
FS.init(null, null, null);
7272

7373
FS.mkdir("/working");
74+
FS.mkdirTree("/nested/directory/structure");
75+
FS.mkdirTree("/another/path", parseInt("0755", 8));
7476
FS.mount(NODEFS, { root: "." }, "/working");
7577

7678
function myAppStartup(): void {
7779
FS.mkdir("/data");
7880
FS.mount(IDBFS, {}, "/data");
7981

82+
// Test isMountpoint - checks if a node is a mount point
83+
const testNode = FS.lookupPath("/data", {}).node;
84+
const isMount: boolean = FS.isMountpoint(testNode);
85+
8086
FS.syncfs(true, (err) => {
8187
// handle callback
8288
});
@@ -92,6 +98,12 @@ function FSTest(): void {
9298
FS.registerDevice(id, {});
9399
FS.mkdev("/dummy", id);
94100

101+
// Test createDevice - creates a device with input/output functions
102+
const inputDevice = FS.createDevice("/", "stdin", () => 65, undefined); // Returns 'A' (65)
103+
const outputDevice = FS.createDevice("/", "stdout", undefined, (c: number) => console.log(String.fromCharCode(c)));
104+
const bothDevice = FS.createDevice("/", "console", () => 66, (c: number) => console.log(c)); // Returns 'B' (66)
105+
const simpleDevice = FS.createDevice("/", "null");
106+
95107
FS.writeFile("file", "foobar");
96108
FS.symlink("file", "link");
97109

@@ -119,7 +131,19 @@ function FSTest(): void {
119131
const data = new Uint8Array(32);
120132
const wstream = FS.open("dummy1", "w+");
121133
FS.write(wstream, data, 0, data.length, 0);
122-
FS.close(wstream);
134+
135+
// Test FS.open with numeric flags
136+
const numericWriteStream = FS.open("numeric-write-stream-test", 1);
137+
FS.write(numericWriteStream, data, 0, data.length, 0);
138+
FS.close(numericWriteStream);
139+
140+
// Test getStream - gets stream by file descriptor
141+
const streamFromFD = FS.getStream(wstream.fd!);
142+
// $ExpectType FSStream
143+
streamFromFD;
144+
145+
// Test closeStream - closes stream by file descriptor
146+
FS.closeStream(wstream.fd!);
123147

124148
FS.createDataFile("/", "dummy2", data, true, true, true);
125149

‎types/emscripten/index.d.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ declare namespace FS {
268268
function makedev(ma: number, mi: number): number;
269269
function registerDevice(dev: number, ops: Partial<StreamOps>): void;
270270
function getDevice(dev: number): { stream_ops: StreamOps };
271+
function createDevice(
272+
parent: string | FSNode,
273+
name: string,
274+
input?: () => number | null | undefined,
275+
output?: (c: number) => any,
276+
): FSNode;
271277

272278
//
273279
// core
@@ -277,8 +283,13 @@ declare namespace FS {
277283
function syncfs(callback: (e: any) => any, populate?: boolean): void;
278284
function mount(type: Emscripten.FileSystemType, opts: any, mountpoint: string): any;
279285
function unmount(mountpoint: string): void;
286+
function isMountpoint(node: FSNode): boolean;
287+
288+
function closeStream(fd: number): void;
289+
function getStream(fd: number): FSStream;
280290

281291
function mkdir(path: string, mode?: number): FSNode;
292+
function mkdirTree(path: string, mode?: number): void;
282293
function mkdev(path: string, mode?: number, dev?: number): FSNode;
283294
function symlink(oldpath: string, newpath: string): FSNode;
284295
function rename(old_path: string, new_path: string): void;
@@ -297,7 +308,7 @@ declare namespace FS {
297308
function truncate(path: string, len: number): void;
298309
function ftruncate(fd: number, len: number): void;
299310
function utime(path: string, atime: number, mtime: number): void;
300-
function open(path: string, flags: string, mode?: number, fd_start?: number, fd_end?: number): FSStream;
311+
function open(path: string, flags: string | number, mode?: number): FSStream;
301312
function close(stream: FSStream): void;
302313
function llseek(stream: FSStream, offset: number, whence: number): number;
303314
function read(stream: FSStream, buffer: ArrayBufferView, offset: number, length: number, position?: number): number;
@@ -309,7 +320,6 @@ declare namespace FS {
309320
position?: number,
310321
canOwn?: boolean,
311322
): number;
312-
function allocate(stream: FSStream, offset: number, length: number): void;
313323
function mmap(
314324
stream: FSStream,
315325
buffer: ArrayBufferView,

0 commit comments

Comments
 (0)