Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion types/emscripten/emscripten-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand All @@ -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");

Expand Down Expand Up @@ -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);

Expand Down
14 changes: 12 additions & 2 deletions types/emscripten/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ declare namespace FS {
function makedev(ma: number, mi: number): number;
function registerDevice(dev: number, ops: Partial<StreamOps>): 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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand Down