Skip to content

Commit

Permalink
feat(lib/[fs|thread].js): added types
Browse files Browse the repository at this point in the history
  • Loading branch information
lostrepo committed Jun 29, 2024
1 parent cca7fe2 commit cb6651f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ const DT_DIR = 4
const DT_REG = 8
const default_options = { recursive: true }

/**
* @param {number} val
* @param {number} mode
*/
function checkMode (val, mode) {
return (val & S_IFMT) === mode
}

/** @param {string} path */
function file_size (path) {
const fd = open(path, O_RDONLY)
assert(fd > 0, `failed to open ${path}`)
Expand All @@ -50,6 +55,10 @@ function file_size (path) {
return size
}

/**
* @param {string} path
* @param {{ path: string; name: string; isFile?: true; isChar?: true; isDirectory?: true }[]} entries
*/
function readdir_sync (path, entries = [], options = default_options) {
const dir = opendir(path)
let next = readdir(dir)
Expand All @@ -73,6 +82,7 @@ function readdir_sync (path, entries = [], options = default_options) {
return entries
}

/** @param {number} handle */
function readEntry (handle) {
readMemory(u8, handle, 19)
const d_ino = dir_view.getUint32(0, true)
Expand All @@ -83,6 +93,7 @@ function readEntry (handle) {
return { d_ino, d_off, d_reclen, d_type, name }
}

/** @param {string} path */
function isFile (path) {
const fd = open(path, O_RDONLY)
if (fd <= 2) return false
Expand All @@ -91,6 +102,7 @@ function isFile (path) {
return checkMode(stat32[MODE_WORD], S_IFREG)
}

/** @param {string} path */
function isDir (path) {
if (access(path, F_OK) !== 0) return false
const fd = open(path, O_RDONLY)
Expand All @@ -103,6 +115,7 @@ function isDir (path) {

// todo: make these safe

/** @param {string} full_path */
function mkDirAll (full_path, fileMode = S_IRWXU | S_IRWXG | S_IROTH) {
const subdirs = full_path.split('/').filter(dir => dir)
let path = ''
Expand All @@ -117,6 +130,7 @@ function mkDirAll (full_path, fileMode = S_IRWXU | S_IRWXG | S_IROTH) {
}

// todo: make these safe and write comprehensive tests
/** @param {string} full_path */
function mkDirAllSafe (full_path, fileMode = S_IRWXU | S_IRWXG | S_IROTH) {
const subdirs = full_path.split('/').filter(dir => dir)
let path = full_path[0] === '/' ? '/' : ''
Expand Down Expand Up @@ -150,6 +164,10 @@ function list_children (path) {

// todo: check all paths before deletion or removal - must be relative
// and no traversal above current allowed
/**
* @param {string} path
* @returns {string[]}
*/
function rmDir (path) {
const dir = opendir(path)
assert(dir)
Expand All @@ -173,10 +191,15 @@ function rmDir (path) {
return directories
}

/**
* @param {string} a
* @param {string} b
*/
function str_compare(a, b) {
return a < b ? 1 : (a === b ? 0 : -1)
}

/** @param {string} path */
function rmDirAll (path) {
if (!isDir(path)) return
const dirs = rmDir(path).sort(str_compare)
Expand Down
6 changes: 6 additions & 0 deletions lib/thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ const { addr, assert, ptr } = lo
const rcbuf = ptr(new Uint32Array(2))
const tbuf = new Uint32Array(2)

/**
* @param {number} address
* @param {TypedArray} ctx
*/
function spawn (address, ctx) {
assert(pthread.create(tbuf, 0, address, ctx) === 0)
return addr(tbuf)
}

/** @param {number} tid */
function join (tid) {
const rc = pthread.join(tid, rcbuf.ptr)
// assert(addr(rcbuf) === 0)
return [rc, addr(rcbuf)]
}

/** @param {number} tid */
function try_join (tid) {
const rc = pthread.tryJoin(tid, rcbuf.ptr)
return [rc, addr(rcbuf)]
Expand Down

0 comments on commit cb6651f

Please sign in to comment.