UNPKG

mathjslab

Version:

MathJSLab - An interpreter with language syntax like MATLAB®/Octave, ISBN 978-65-00-82338-7.

80 lines (79 loc) 3.08 kB
import type { NodeExpr, NodeFunctionDefinition } from './AST'; import { type WorkspaceScope } from './FunctionWorkspace'; type FunctionFrame = { func?: { type: string; node?: unknown; }; scope: WorkspaceScope; parentFrame?: FunctionFrame; name?: string; nargin: number; nargout: number; inputArgs: NodeExpr[]; }; type CreateScope = (parent?: WorkspaceScope) => WorkspaceScope; /** * Stack helpers for MATLAB/Octave-like function metadata. * * The interpreter keeps concrete `CallFrame` objects, but this module only * depends on the structural `FunctionFrame` shape. That lets unit tests cover * stack behavior without instantiating a full interpreter. */ declare class FunctionStack { /** * Return the active user-defined function definition, if any. */ static currentFunctionDefinition(frames: FunctionFrame[]): NodeFunctionDefinition | undefined; /** * Return the nearest user-defined function frame. * * Built-ins and anonymous-function frames are skipped because persistent * declarations and function-name introspection are tied to `function` * definitions, not to all callable forms. */ static currentFunctionFrame(frames: FunctionFrame[]): FunctionFrame | undefined; /** * Return the nearest frame that contributes `nargin`/`nargout`. * * Both ordinary functions and anonymous function handles have call counts. * Built-ins are skipped so helper built-ins such as `eval` do not mask the * user-visible caller. */ static currentFunctionCountFrame(frames: FunctionFrame[]): FunctionFrame | undefined; /** * Return the display name of the current user-defined function. */ static currentFunctionName(frames: FunctionFrame[]): string; /** * Return the caller-supplied input argument count for `nargin`. */ static currentArgumentCount(frames: FunctionFrame[]): number | undefined; /** * Return the caller-requested output count for `nargout`. */ static currentOutputCount(frames: FunctionFrame[]): number | undefined; /** * Resolve the caller workspace for `evalin('caller', ...)` and `assignin`. * * Anonymous functions need a special overlay for read-only caller lookup: * MATLAB/Octave-style closures should see captured variables while still * exposing caller-visible names to `evalin`. For assignment, the real caller * workspace is used instead of the overlay. */ static callerWorkspace(frames: FunctionFrame[], globalScope: WorkspaceScope, createScope: CreateScope, forAssignment?: boolean): WorkspaceScope; /** * Build the anonymous-function caller overlay used by `evalin`. */ private static anonymousCallerWorkspace; /** * Skip built-in helper frames while walking toward user-visible callers. */ private static skipBuiltInFrames; } export type { FunctionFrame }; export { FunctionStack }; declare const _default: { FunctionStack: typeof FunctionStack; }; export default _default;