ShellState, SessionState implemented, all tested, and passing

This commit is contained in:
2026-07-25 03:37:13 +02:00
parent 0fce10895f
commit 994b93ae47
7 changed files with 275 additions and 6 deletions
+20 -1
View File
@@ -1,4 +1,8 @@
interface SessionState {
import { type HistoryState } from "./history_state";
import type { ShellState } from "./shell_state";
import type { UserInfo } from "./user_info";
export interface SessionState {
readonly user: UserInfo;
readonly envVars: ReadonlyMap<string, string>;
readonly history: HistoryState;
@@ -6,3 +10,18 @@ interface SessionState {
readonly pwd: number;
readonly exitCode: number;
}
export function createInitialSessionState(user: UserInfo): SessionState {
return Object.freeze({
user,
envVars: Object.freeze(new Map()),
history: Object.freeze([]),
cwd: user.home,
pwd: user.home,
exitCode: 0,
} satisfies SessionState);
}
export function getCurrentSession(state: ShellState): SessionState {
return state.sessionStack[state.sessionStack.length - 1]!;
}
+112 -2
View File
@@ -1,7 +1,117 @@
import type { FileSystem } from "./filesystem/filesystem";
import { addHistoryEntry } from "./history_state";
import { createInitialSessionState, getCurrentSession, type SessionState } from "./session";
import type { UserInfo } from "./user_info";
interface ShellState {
export interface ShellState {
readonly filesystem: FileSystem;
readonly users: ReadonlyMap<number, UserInfo>;
readonly sessionState: ReadonlyArray<SessionState>;
readonly sessionStack: ReadonlyArray<SessionState>;
}
export function createInitialShellState(
filesystem: FileSystem,
users: ReadonlyMap<number, UserInfo>,
user: UserInfo,
): ShellState {
return Object.freeze({
filesystem,
users,
sessionStack: Object.freeze([createInitialSessionState(user)]),
});
}
export function withCwd(state: ShellState, inode: number): ShellState {
const session = getCurrentSession(state);
const newSession = Object.freeze({
user: session.user,
envVars: Object.freeze(session.envVars),
history: Object.freeze(session.history),
cwd: inode,
pwd: session.cwd,
exitCode: session.exitCode,
} satisfies SessionState);
const sessionStack = [...state.sessionStack];
sessionStack[sessionStack.length - 1] = newSession;
return Object.freeze({
filesystem: state.filesystem,
users: state.users,
sessionStack: Object.freeze(sessionStack),
} satisfies ShellState);
}
export function withHistory(state: ShellState, input: string): ShellState {
const session = getCurrentSession(state);
const newSession = Object.freeze({
user: session.user,
envVars: Object.freeze(session.envVars),
history: Object.freeze(addHistoryEntry(session.history, input)),
cwd: session.cwd,
pwd: session.pwd,
exitCode: session.exitCode,
} satisfies SessionState);
const sessionStack = [...state.sessionStack];
sessionStack[sessionStack.length - 1] = newSession;
return Object.freeze({
filesystem: state.filesystem,
users: state.users,
sessionStack: Object.freeze(sessionStack),
} satisfies ShellState);
}
export function withEnv(state: ShellState, key: string, value: string): ShellState {
const session = getCurrentSession(state);
const envVars = new Map(session.envVars);
envVars.set(key, value);
const newSession = Object.freeze({
user: session.user,
envVars: Object.freeze(envVars),
history: Object.freeze(session.history),
cwd: session.cwd,
pwd: session.pwd,
exitCode: session.exitCode,
} satisfies SessionState);
const sessionStack = [...state.sessionStack];
sessionStack[sessionStack.length - 1] = newSession;
return Object.freeze({
filesystem: state.filesystem,
users: state.users,
sessionStack: Object.freeze(sessionStack),
} satisfies ShellState);
}
export function withExitCode(state: ShellState, exitCode: number): ShellState {
const session = getCurrentSession(state);
const newSession = Object.freeze({
user: session.user,
envVars: Object.freeze(session.envVars),
history: Object.freeze(session.history),
cwd: session.cwd,
pwd: session.pwd,
exitCode: exitCode,
} satisfies SessionState);
const sessionStack = [...state.sessionStack];
sessionStack[sessionStack.length - 1] = newSession;
return Object.freeze({
filesystem: state.filesystem,
users: state.users,
sessionStack: Object.freeze(sessionStack),
} satisfies ShellState);
}
+2 -1
View File
@@ -1,4 +1,5 @@
interface UserInfo {
export interface UserInfo {
readonly name: string;
readonly uid: number;
readonly gid: number;
readonly group: readonly number[];
+2 -2
View File
@@ -1,4 +1,4 @@
type ParserError = UnclosedQuoteError;
//type ParserError = UnclosedQuoteError;
interface UnclosedQuoteError {
type: "UnclosedQuote";
@@ -21,7 +21,7 @@ function tokenize(input: string): string[] {
}
const tokens: string[] = [];
let buffer: string[] = [];
const buffer: string[] = [];
let state = State.Normal;
let escapeReturnState = State.Normal;