chore: rewrite loadprg & fix some issues

This commit is contained in:
binekrasik
2026-05-22 23:27:04 +02:00
parent 4777552106
commit 3798a53395
6 changed files with 87 additions and 25 deletions

View File

@@ -9,20 +9,23 @@ export class Cat extends Program {
return 1
}
const item = await Item.open(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
const path = Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`)
let item: Item
if (!(await item.Exists())) {
stdout.emit(`cat: error: item ${item.GetPath()} doesn't exist.\n`)
try {
item = await Item.open(path)
} catch (err) {
stdout.emit(`cat: error: item ${path} is a directory\n`)
return 2
}
if (item.IsDirectory()) {
stdout.emit(`cat: error: can't read data from a directory; item ${item.GetPath()} is a directory.\n`)
if (!(await item.Exists())) {
stdout.emit(`cat: error: item ${item.GetPath()} doesn't exist.\n`)
return 3
}
stdout.emit(`${item.ReadData()}`)
stdout.emit('<- EOF\n')
stdout.emit(`${item.GetData()}`)
stdout.emit('\x1B[0;30m\x1B[47m EOF \x1B[0m\n')
return 0
}

View File

@@ -32,7 +32,7 @@ export class Edit extends Program {
let filePath = resolvePath(args[1])
let file = await Item.open(filePath, '')
let fileExists = await file.Exists()
let lines = this.normalizeLines(file.ReadData())
let lines = this.normalizeLines(file.GetData())
let dirty = false
let cursorLine = 0
@@ -93,7 +93,7 @@ export class Edit extends Program {
const resolved = resolvePath(path)
const nextFile = await Item.open(resolved, '')
const exists = await nextFile.Exists()
const content = nextFile.ReadData()
const content = nextFile.GetData()
filePath = resolved
file = nextFile

View File

@@ -4,7 +4,21 @@ import { Program } from './Program'
export class Eval extends Program {
async Exec(stdin: SimpleStream<string>, stdout: SimpleStream<string>, _workdir: Item, args: string[]): Promise<number> {
let javascript: string = args.slice(1).join(' ')
const inlineArgs = args.slice(1)
const source = inlineArgs.length > 0
? inlineArgs.join(' ')
: await stdin.wait()
if (source.trim().length === 0) {
stdout.emit('eval: error: missing javascript input\n')
return 1
}
try {
this.RunJs(source, stdout)
} catch {
return 1
}
return 0
}

View File

@@ -1,4 +1,4 @@
import type { Item } from '../fs/Item'
import { Item } from '../fs/Item'
import type { Shell } from '../shell/Shell'
import type { SimpleStream } from '../utils/SimpleStream'
import { Program } from './Program'
@@ -11,21 +11,59 @@ export class Loadprg extends Program {
this.shell = shell
}
async Exec(stdin: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, args: string[]): Promise<number> {
const javascript = args.slice(2).join(' ')
stdin.on(data => stdout.emit(`loadprg stdin: ${data}\x1B[0;30m\x1B[47m<- EOF\x1B[0m\n`))
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
const path = Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`)
let file: Item
try {
const program = eval(javascript)
this.shell.LoadProgram(new program(), args[1])
} catch (e) {
stdout.emit(`${String(e)}\n`)
stdout.emit(`${String((e as Error).stack)}\n`)
return 1
file = await Item.open(path)
} catch (err) {
stdout.emit(`loadprg: error: item ${path} is a directory\n`)
return 2
}
if (!(await file.Exists())) {
stdout.emit(`loadprg: error: item ${file.GetPath()} doesn't exist.\n`)
return 3
}
let programName: string | null = null
// check if we have a name argument
// use the file name if the program name wasn't explicitly specified
const nameArgIndex = args.findIndex(value => value === "--name")
if (nameArgIndex !== -1 && args[nameArgIndex + 1]) {
programName = args[nameArgIndex + 1]
} else programName = file.GetName()
// read the file
const javascript = file.GetData()
if (!javascript) {
stdout.emit(`loadprg: error: could not read the program data\n`)
return 3
}
// load the program
type EntrypointFunction = InstanceType<typeof Program>['Exec']
let programEntrypoint: EntrypointFunction
try {
// due to some inconveniences and limitations,
// we only allow for loading the program main method
// instead of an entire program class.
programEntrypoint = new Function("stdin", "stdout", "workdir", "args", javascript) as EntrypointFunction
} catch (err) {
stdout.emit(`loadprg: error: could not load the program: ${err}\n`)
stdout.emit(`-> Stacktrace: ${(err as Error).stack}\n`)
return 4
}
// create a wrapper class for the program
const wrapper: Program = new (class ProgramWrapper extends Program { Exec = programEntrypoint })
this.shell.LoadProgram(wrapper, programName)
stdout.emit(`-> Loaded program from ${file.GetPath()} as ${programName}\n`)
return 0
}
}