mathjslab
Version:
MathJSLab - An interpreter with language syntax like MATLAB®/Octave, ISBN 978-65-00-82338-7.
184 lines (183 loc) • 7.23 kB
TypeScript
/**
* Shared runtime-value helpers that cut across parser, interpreter, built-ins,
* and validation modules.
*
* This module centralizes contracts that must remain identical wherever a
* MATLAB/Octave value is inspected. In particular, `CharString` is a row
* character vector, `MultiArray` values keep their stored dimensions, and all
* other scalar runtime values are `1x1`.
*/
type RuntimeStructureField = Record<string, unknown> | string[];
type RuntimeStructureLike = {
type: number;
field: Record<string, unknown>;
copy?: () => RuntimeStructureLike;
};
declare abstract class RuntimeValue {
/** Runtime tag used by function-handle values. */
static readonly FUNCTION_HANDLE = 5;
/** Runtime tag shared by class instances and some class metadata values. */
static readonly CLASS_INSTANCE = 7;
private static structureFactory;
/**
* Test whether a value implements the runtime copy protocol.
*
* Runtime scalar/object values own their copy semantics. This structural
* test keeps generic runtime helpers decoupled from concrete value classes.
*
* @param value Value to inspect.
* @returns `true` when `value.copy()` can be used.
*/
private static readonly hasCopy;
/**
* Copy a runtime value through its own copy protocol.
*
* Immutable or singleton-like values may return themselves from `copy()`.
* Values without a copy method are returned unchanged so parser-only nodes
* and plain host values can still flow through generic runtime paths.
*
* @param value Runtime value to copy.
* @returns Copied value when supported, otherwise `value`.
*/
static readonly copy: <T>(value: T) => T;
/**
* Test whether a runtime field table owns one key directly.
*
* MATLAB/Octave structures and class instances expose only stored fields or
* properties; inherited JavaScript prototype names must never be visible as
* runtime members.
*
* @param table Runtime field/property table.
* @param key Field or property name.
* @returns `true` when `key` is an own property of `table`.
*/
static readonly hasOwnField: (table: object, key: string) => boolean;
/**
* Register the runtime structure constructor without coupling generic
* runtime helpers to the concrete `Structure` module.
*
* @param factory Factory that creates a structure value.
*/
static readonly registerStructureFactory: (factory: (field: RuntimeStructureField) => RuntimeStructureLike) => void;
/**
* Create a runtime structure through the registered factory.
*
* @param field Field map or nested field path.
* @returns New runtime structure value.
*/
static readonly createStructure: (field: RuntimeStructureField) => RuntimeStructureLike;
/**
* Test whether a value structurally behaves as a MATLAB/Octave object instance.
*
* The runtime deliberately avoids importing `ClassInstance` here so generic
* array and dispatch helpers can identify object values without coupling to
* the concrete class module. Metadata objects may share the same type tag, so
* the test also requires an object-property bag and a class definition.
*
* @param value Value to inspect.
* @returns `true` when `value` looks like a class instance.
*/
static readonly isClassInstance: (value: unknown) => value is {
type: number;
classDefinition: {
name: string;
};
properties: Map<string, unknown> | Record<string, unknown>;
};
/**
* Return the class-definition object associated with a runtime class instance.
*
* @param value Value to inspect.
* @returns The instance class definition, or `undefined` for non-instances.
*/
static readonly classDefinitionOfInstance: (value: unknown) => {
name: string;
} | undefined;
/**
* Detect values that expose MATLAB/Octave shape metadata.
*
* This intentionally uses a structural test instead of importing concrete
* runtime classes. Shape is a cross-cutting contract, and keeping this
* helper dependency-light avoids adding cycles among the interpreter core
* modules.
*/
private static readonly hasDimensions;
/**
* Test whether a structural candidate is an object-like value.
*/
private static readonly isObjectRecord;
/**
* Test whether a structural class-definition candidate exposes a name.
*/
private static readonly isNamedClassDefinition;
/**
* Test whether a structural dimension candidate is a numeric shape vector.
*/
private static readonly isNumericDimensionVector;
/**
* Return MATLAB/Octave-style dimensions for a runtime value.
*
* @param value Runtime value to inspect.
* @param minDimensions Minimum number of dimensions to expose.
* @returns A fresh dimension array padded with singleton dimensions.
*/
static readonly dimensions: (value: unknown, minDimensions?: number) => number[];
/**
* Return the MATLAB/Octave element count for a runtime value.
*
* @param value Runtime value to inspect.
* @returns Product of runtime dimensions.
*/
static readonly elementCount: (value: unknown) => number;
/**
* Test whether the runtime value has at least one zero dimension.
*
* @param value Runtime value to inspect.
* @returns `true` for empty arrays and empty character vectors.
*/
static readonly isEmpty: (value: unknown) => boolean;
/**
* Test whether the runtime value contains exactly one element.
*
* @param value Runtime value to inspect.
* @returns `true` for `1x1` values.
*/
static readonly isScalar: (value: unknown) => boolean;
/**
* Test whether the runtime value is two-dimensional.
*
* @param value Runtime value to inspect.
* @returns `true` when the value has exactly two runtime dimensions.
*/
static readonly isMatrix: (value: unknown) => boolean;
/**
* Test whether the runtime value is a row or column vector.
*
* @param value Runtime value to inspect.
* @returns `true` for two-dimensional values with one singleton dimension.
*/
static readonly isVector: (value: unknown) => boolean;
/**
* Test whether the runtime value is a row vector.
*
* @param value Runtime value to inspect.
* @returns `true` for two-dimensional values with one row.
*/
static readonly isRowVector: (value: unknown) => boolean;
/**
* Test whether the runtime value is a column vector.
*
* @param value Runtime value to inspect.
* @returns `true` for two-dimensional values with one column.
*/
static readonly isColumnVector: (value: unknown) => boolean;
/**
* Test whether the runtime value is a square two-dimensional matrix.
*
* @param value Runtime value to inspect.
* @returns `true` for `NxN` values.
*/
static readonly isSquareMatrix: (value: unknown) => boolean;
}
export { RuntimeValue };
export default RuntimeValue;