@casual-simulation/aux-runtime
Version:
Runtime for AUX projects
278 lines • 12.1 kB
TypeScript
import type { StateUpdatedEvent, Bot, BotSpace, RuntimeBot, BotModuleResult, ResolvedBotModule, ImportMetadata, DynamicListener } from '@casual-simulation/aux-common/bots';
import type { Observable, SubscriptionLike } from 'rxjs';
import type { AuxGlobalContext } from './AuxGlobalContext';
import type { AuxLibrary } from './AuxLibrary';
import type { RuntimeBotInterface, RuntimeBotFactory, RealtimeEditConfig, RuntimeInterpreterGeneratorProcessor } from './RuntimeBot';
import { RealtimeEditMode } from './RuntimeBot';
import type { CompiledBot, CompiledBotsState, RuntimeBreakpoint, RuntimeStop } from './CompiledBot';
import type { ScriptError, ActionResult } from './AuxResults';
import type { AuxVersion } from './AuxVersion';
import type { AuxDevice } from './AuxDevice';
import type { RuntimePromise } from './Utils';
import type { AuxRealtimeEditModeProvider } from './AuxRealtimeEditModeProvider';
import type { CurrentVersion } from '@casual-simulation/aux-common';
import type { RuntimeStateVersion } from './RuntimeStateVersion';
import type { Interpreter as InterpreterType, InterpreterContinuation, InterpreterStop } from '@casual-simulation/js-interpreter';
import type { RuntimeActions } from './RuntimeEvents';
export declare function registerInterpreterModule(module: any): void;
/**
* Defines an class that is able to manage the runtime state of an AUX.
*
* Being a runtime means providing and managing the execution state that an AUX is in.
* This means taking state updates events, shouts and whispers, and emitting additional events to affect the future state.
*/
export declare class AuxRuntime implements RuntimeBotInterface, RuntimeBotFactory, RuntimeInterpreterGeneratorProcessor, SubscriptionLike {
private _compiledState;
private _existingMasks;
private _compiler;
private _onActions;
private _onErrors;
private _onRuntimeStop;
private _stopState;
private _breakpoints;
private _currentStopCount;
private _currentPromise;
private _actionBatch;
private _errorBatch;
private _userId;
private _sub;
private _currentVersion;
private _updatedBots;
private _newBots;
private _globalContext;
private _library;
private _editModeProvider;
private _exemptSpaces;
private _batchPending;
private _jobQueueCheckPending;
private _jobQueueCheckCount;
private _processingErrors;
private _portalBots;
private _builtinPortalBots;
private _globalChanges;
private _globalObject;
private _interpretedApi;
private _interpretedTagSpecificApi;
private _beforeActionListeners;
private _scriptActionEnqueuedListeners;
private _scriptUpdatedTagListeners;
private _scriptUpdatedTagMaskListeners;
/**
* The counter that is used to generate function names.
*/
private _functionNameCounter;
/**
* A map of function names to their respective functions.
*/
private _functionMap;
/**
* A map of bot IDs to a list of function names.
*/
private _botFunctionMap;
/**
* Whether changes should be automatically batched.
*/
private _autoBatch;
private _forceSyncScripts;
private _currentDebugger;
private _libraryFactory;
private _interpreter;
/**
* The map of module IDs to their exports.
* Only used for global modules, which are modules that are not attached to a bot (e.g. source modules).
*/
private _cachedGlobalModules;
/**
* The map of system IDs to their respective bot IDs.
*/
private _systemMap;
/**
* The number of times that the runtime can call onError for an error from the same script.
*/
repeatedErrorLimit: number;
get library(): AuxLibrary;
get context(): AuxGlobalContext;
get currentVersion(): RuntimeStateVersion;
get globalObject(): any;
get canTriggerBreakpoint(): boolean;
get systemMap(): Map<string, Set<string>>;
/**
* Creates a new AuxRuntime using the given library factory.
* @param libraryFactory
* @param forceSignedScripts Whether to force the runtime to only allow scripts that are signed.
* @param exemptSpaces The spaces that are exempt from requiring signed scripts.
* @param interpreter The interpreter that should be used for the runtime.
*/
constructor(version: AuxVersion, device: AuxDevice, libraryFactory?: (context: AuxGlobalContext) => AuxLibrary, editModeProvider?: AuxRealtimeEditModeProvider, exemptSpaces?: BotSpace[], forceSyncScripts?: boolean, interpreter?: InterpreterType);
private _importModule;
private _importModuleCore;
private _resolveExports;
/**
* Performs a dynamic import() of the given module.
* Uses the JS Engine's native import() functionality.
* @param module The module that should be imported.
* @returns Returns a promise that resolves with the module's exports.
*/
dynamicImport(module: string): Promise<BotModuleResult>;
/**
* Attempts to resolve the module with the given name.
* @param moduleName The name of the module to resolve.
* @param meta The metadata that should be used to resolve the module.
*/
resolveModule(moduleName: string, meta?: ImportMetadata, allowCustomResolution?: boolean): Promise<ResolvedBotModule>;
getShoutTimers(): {
[shout: string]: number;
};
get closed(): boolean;
unsubscribe(): void;
/**
* Gets the current state that the runtime is operating on.
*/
get currentState(): CompiledBotsState;
get userId(): string;
set userId(id: string);
get userBot(): RuntimeBot;
/**
* An observable that resolves whenever the runtime issues an action.
*/
get onActions(): Observable<RuntimeActions[]>;
/**
* An observable that resolves whenever the runtime issues an error.
*/
get onErrors(): Observable<ScriptError[]>;
/**
* An observable that resolves whenever the runtime pauses in a script.
*/
get onRuntimeStop(): Observable<RuntimeStop>;
/**
* Processes the given bot actions and dispatches the resulting actions in the future.
* @param actions The actions to process.
*/
process(actions: RuntimeActions[]): MaybeRuntimePromise<ActionResult[]>;
private _getExecutingDebugger;
private _createDebugger;
private _mapCallStack;
private _processCore;
private _processAction;
private _registerPortalBot;
private _rejectAction;
/**
* Executes the given function based on the current execution state of the runtime.
* If the runtime is stopped, then the given function will be executed after the current promise finishes.
* If the runtime is executing normally, then the given function will be executed immediately.
* Returns the function result if the function executes immediately,
* or returns a runtime promise if the function executes after the current promise.
*
* This function essentially forces the given function to execute synchronously with respect to other functions wrapped with this function.
*
* @param func The function to execute.
*
*/
private _wrapWithCurrentPromise;
/**
* Executes a shout with the given event name on the given bot IDs with the given argument.
* Also dispatches any actions and errors that occur.
* @param eventName The name of the event.
* @param botIds The Bot IDs that the shout is being sent to.
* @param arg The argument to include in the shout.
*/
shout(eventName: string, botIds?: string[], arg?: any): MaybeRuntimePromise<ActionResult>;
/**
* Executes the given shout synchronously with respect to other scripts that may be running or paused.
* @param eventName The name of the event that should be executed.
* @param botIds The IDs of the bots that the shout should be sent to.
* @param arg The argument that should be sent with the shout.
* @param batch Whether to batch events.
* @param resetEnergy Whether to reset the runtime energy before executing the shout.
*/
private _synchronousShout;
private _shout;
/**
* Executes the given script.
* @param script The script to run.
*/
execute(script: string): MaybeRuntimePromise<{
result: any;
actions: RuntimeActions[];
errors: ScriptError[];
}>;
private _execute;
/**
* Signals to the runtime that the bots state has been updated.
* @param update The bot state update.
*/
stateUpdated(update: StateUpdatedEvent): StateUpdatedEvent;
/**
* Signals to the runtime that the state version has been updated.
* @param newVersion The version update.
*/
versionUpdated(newVersion: CurrentVersion): RuntimeStateVersion;
private _sendOnBotsAddedShouts;
private _sendOnBotsRemovedShouts;
private _sendOnBotsChangedShouts;
private _triggerPortalChangedHandlers;
private _addBotsToState;
private _removeBotsFromState;
private _updateBotsWithState;
notifyChange(): void;
notifyActionEnqueued(action: RuntimeActions): void;
createRuntimeBot(bot: Bot): RuntimeBot;
destroyScriptBot(bot: RuntimeBot): RealtimeEditMode;
private _processBatch;
private _processErrors;
private _batchScriptResults;
private _calculateScriptResults;
private _processUnbatchedErrors;
private _processUnbatchedActions;
private _compileTags;
private _createCompiledBot;
private _createRuntimeBot;
updateTag(bot: CompiledBot, tag: string, newValue: any): RealtimeEditConfig;
getValue(bot: CompiledBot, tag: string): any;
getRawValue(bot: CompiledBot, tag: string): any;
updateTagMask(bot: CompiledBot, tag: string, spaces: string[], value: any): RealtimeEditConfig;
getTagMask(bot: CompiledBot, tag: string): any;
getListener(bot: CompiledBot, tag: string): DynamicListener | null;
setListener(bot: CompiledBot, tag: string, listener: DynamicListener | null): void;
getDynamicListeners(bot: CompiledBot, tag: string): DynamicListener[] | null;
addDynamicListener(bot: CompiledBot, tag: string, listener: DynamicListener): void;
removeDynamicListener(bot: CompiledBot, tag: string, listener: DynamicListener): void;
getTagLink(bot: CompiledBot, tag: string): RuntimeBot | RuntimeBot[];
getSignature(bot: CompiledBot, signature: string): string;
private _compileTagOrMask;
private _compileTag;
private _updateListenerPresense;
private _compileTagValue;
private _compileValue;
private _compileTagMaskValue;
private _compile;
private _handleError;
private _getRuntimeBot;
/**
* Maps the given value to a new value where bots are replaced with script bots.
* This makes it easy to modify other bot values in listeners. If the value is not convertable,
* then it is returned unaffected. Only objects and arrays are convertable.
*
* Works by making a copy of the value where every bot value is replaced with a reference
* to a script bot instance for the bot. The copy has a reference to the original value in the ORIGINAL_OBJECT symbol property.
* We use this property in action.reject() to resolve the original action value so that doing a action.reject() in a onAnyAction works properly.
*
* @param context The sandbox context.
* @param value The value that should be converted.
*/
private _mapBotsToRuntimeBots;
private _mapBotsToRuntimeBotsCore;
private _getFunctionNamesForBot;
setBreakpoint(breakpoint: RuntimeBreakpoint): void;
removeBreakpoint(breakpointId: string): void;
continueAfterStop(stopId: RuntimeStop['stopId'], continuation?: InterpreterContinuation): void;
private _scheduleJob;
private _scheduleJobQueueCheck;
private _processJobQueueNow;
processGenerator<T>(generator: Generator<InterpreterStop, T, InterpreterContinuation>): void;
private _processGenerator;
private _executeGenerator;
}
type MaybeRuntimePromise<T> = T | RuntimePromise<T>;
export {};
//# sourceMappingURL=AuxRuntime.d.ts.map