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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
"devDependencies": {
"@eslint/js": "^10.0.1",
"@types/emscripten": "^1.40.0",
"@types/emscripten": "^1.41.6",
"eslint": "^10.1.0",
"globals": "^17.0.0",
"prettier": "^3.2.5",
Expand Down
3 changes: 2 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ export default class EmscriptenPlugin implements EmscriptenNodeFS {
if (!this.em_fs.isDir(mode) && !this.em_fs.isFile(mode) && !this.em_fs.isLink(mode)) {
throw new this.em_fs.ErrnoError(toEmscriptenErrno(Errno.EINVAL));
}
const node: Node = new this.em_fs.FSNode(parent!, name, mode, rdev!);
const node: Node = new this.em_fs.FSNode(parent, name, mode, rdev);
node.node_ops = this.node_ops;
node.stream_ops = this.stream_ops;
this.em_fs.hashAddNode(node);
return node;
}

Expand Down
65 changes: 65 additions & 0 deletions tests/plugin.test.ts
Comment thread
james-pre marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,32 @@
}
}

const hashed: EmFS.FSNode[] = [];

// FS.nameTable and friends, straight out of emscripten's src/lib/libfs.js, since
// what the cache is worth can't be seen without the thing that reads it.
const nameTable: (EmFS.FSNode | undefined)[] = new Array(4096);

Check warning on line 41 in tests/plugin.test.ts

View workflow job for this annotation

GitHub Actions / CI

'FSNode' is an 'error' type that acts as 'any' and overrides all other types in this union type

function hashName(parentid: number, name: string): number {
let hash = 0;
for (let i = 0; i < name.length; i++) {
hash = ((hash << 5) - hash + name.charCodeAt(i)) | 0;
}
return ((parentid + hash) >>> 0) % nameTable.length;
}

const em_fs = {
ErrnoError,
FSNode,
isDir: (mode: number) => (mode & 0o170000) === 0o040000,
isFile: (mode: number) => (mode & 0o170000) === 0o100000,
isLink: (mode: number) => (mode & 0o170000) === 0o120000,
hashAddNode: (node: EmFS.FSNode) => {
hashed.push(node);
const hash = hashName(node.parent.id, node.name);
node.name_next = nameTable[hash];
nameTable[hash] = node;
},
} as unknown as typeof EmFS;

mount('/zen', InMemory.create({ label: 'plugin-test' }));
Expand All @@ -50,7 +70,7 @@

/** The mounted root, wired the way `FS.mount()` wires it. */
function rooted(): EmFS.FSNode {
const node = plugin.mount({ opts: { root: '/zen/root' } } as EmFS.Mount & { opts: { root: string } });

Check warning on line 73 in tests/plugin.test.ts

View workflow job for this annotation

GitHub Actions / CI

'Mount' is an 'error' type that acts as 'any' and overrides all other types in this intersection type
(node as unknown as FSNode).mount = { opts: { root: '/zen/root' } };
return node;
}
Expand All @@ -76,7 +96,7 @@

test('a mount root that is not there says so', () => {
assert.equal(
errnoOf(() => plugin.mount({ opts: { root: '/zen/absent' } } as EmFS.Mount & { opts: { root: string } })),

Check warning on line 99 in tests/plugin.test.ts

View workflow job for this annotation

GitHub Actions / CI

'Mount' is an 'error' type that acts as 'any' and overrides all other types in this intersection type
44
);
});
Expand All @@ -102,3 +122,48 @@
assert.equal(plugin.node_ops.getattr(node).size, 5);
});
});

/** FS.lookupNode: the table first, node_ops.lookup only when it misses. */
function lookupNode(parent: EmFS.FSNode, name: string): EmFS.FSNode {
for (let node = nameTable[hashName(parent.id, name)]; node; node = node.name_next) {
if (node.parent.id === parent.id && node.name === name) return node;
}
return plugin.node_ops.lookup(parent, name);
}

/** And FS.lookupPath, for a path with no symlinks or mounts in it. */
function resolve(root: EmFS.FSNode, path: string): EmFS.FSNode {
let current = root;
for (const part of path.split('/')) current = lookupNode(current, part);
return current;
}

suite("Emscripten's lookup cache", () => {
test('a created node is hashed into it #7', () => {
const node = plugin.createNode(null, 'hashed.txt', 0o100644);
assert.ok(hashed.includes(node));
});

test('a resolved path is resolved once, not once per resolution #7', () => {
fs.mkdirSync('/zen/root/a/b/c/d', { recursive: true });
fs.writeFileSync('/zen/root/a/b/c/d/leaf.txt', 'x');

const root = rooted();
(root as unknown as FSNode).mount = { opts: { root: '/zen/root' } };

let lookups = 0;
const real = plugin.node_ops.lookup.bind(plugin.node_ops);
plugin.node_ops.lookup = (parent: EmFS.FSNode, name: string) => {
lookups++;
return real(parent, name);
};

try {
for (let i = 0; i < 100; i++) resolve(root, 'a/b/c/d/leaf.txt');
} finally {
plugin.node_ops.lookup = real;
}

assert.equal(lookups, 5, '5 components, resolved once each — not 500');
});
});
Loading