diff --git a/types/emscripten/emscripten-tests.ts b/types/emscripten/emscripten-tests.ts index 06f7c5e43690a5d..03c1863241755f0 100644 --- a/types/emscripten/emscripten-tests.ts +++ b/types/emscripten/emscripten-tests.ts @@ -71,12 +71,18 @@ function FSTest(): void { FS.init(null, null, null); FS.mkdir("/working"); + FS.mkdirTree("/nested/directory/structure"); + FS.mkdirTree("/another/path", parseInt("0755", 8)); FS.mount(NODEFS, { root: "." }, "/working"); function myAppStartup(): void { FS.mkdir("/data"); FS.mount(IDBFS, {}, "/data"); + // Test isMountpoint - checks if a node is a mount point + const testNode = FS.lookupPath("/data", {}).node; + const isMount: boolean = FS.isMountpoint(testNode); + FS.syncfs(true, (err) => { // handle callback }); @@ -92,6 +98,12 @@ function FSTest(): void { FS.registerDevice(id, {}); FS.mkdev("/dummy", id); + // Test createDevice - creates a device with input/output functions + const inputDevice = FS.createDevice("/", "stdin", () => 65, undefined); // Returns 'A' (65) + const outputDevice = FS.createDevice("/", "stdout", undefined, (c: number) => console.log(String.fromCharCode(c))); + const bothDevice = FS.createDevice("/", "console", () => 66, (c: number) => console.log(c)); // Returns 'B' (66) + const simpleDevice = FS.createDevice("/", "null"); + FS.writeFile("file", "foobar"); FS.symlink("file", "link"); @@ -119,7 +131,19 @@ function FSTest(): void { const data = new Uint8Array(32); const wstream = FS.open("dummy1", "w+"); FS.write(wstream, data, 0, data.length, 0); - FS.close(wstream); + + // Test FS.open with numeric flags + const numericWriteStream = FS.open("numeric-write-stream-test", 1); + FS.write(numericWriteStream, data, 0, data.length, 0); + FS.close(numericWriteStream); + + // Test getStream - gets stream by file descriptor + const streamFromFD = FS.getStream(wstream.fd!); + // $ExpectType FSStream + streamFromFD; + + // Test closeStream - closes stream by file descriptor + FS.closeStream(wstream.fd!); FS.createDataFile("/", "dummy2", data, true, true, true); diff --git a/types/emscripten/index.d.ts b/types/emscripten/index.d.ts index 0b9ab6b90d8e100..17d83f5a52453d5 100644 --- a/types/emscripten/index.d.ts +++ b/types/emscripten/index.d.ts @@ -268,6 +268,12 @@ declare namespace FS { function makedev(ma: number, mi: number): number; function registerDevice(dev: number, ops: Partial): void; function getDevice(dev: number): { stream_ops: StreamOps }; + function createDevice( + parent: string | FSNode, + name: string, + input?: () => number | null | undefined, + output?: (c: number) => any, + ): FSNode; // // core @@ -277,8 +283,13 @@ declare namespace FS { function syncfs(callback: (e: any) => any, populate?: boolean): void; function mount(type: Emscripten.FileSystemType, opts: any, mountpoint: string): any; function unmount(mountpoint: string): void; + function isMountpoint(node: FSNode): boolean; + + function closeStream(fd: number): void; + function getStream(fd: number): FSStream; function mkdir(path: string, mode?: number): FSNode; + function mkdirTree(path: string, mode?: number): void; function mkdev(path: string, mode?: number, dev?: number): FSNode; function symlink(oldpath: string, newpath: string): FSNode; function rename(old_path: string, new_path: string): void; @@ -297,7 +308,7 @@ declare namespace FS { function truncate(path: string, len: number): void; function ftruncate(fd: number, len: number): void; function utime(path: string, atime: number, mtime: number): void; - function open(path: string, flags: string, mode?: number, fd_start?: number, fd_end?: number): FSStream; + function open(path: string, flags: string | number, mode?: number): FSStream; function close(stream: FSStream): void; function llseek(stream: FSStream, offset: number, whence: number): number; function read(stream: FSStream, buffer: ArrayBufferView, offset: number, length: number, position?: number): number; @@ -309,7 +320,6 @@ declare namespace FS { position?: number, canOwn?: boolean, ): number; - function allocate(stream: FSStream, offset: number, length: number): void; function mmap( stream: FSStream, buffer: ArrayBufferView,