C# Class kOS.Safe.Execution.CPU

Inheritance: ICpu
Afficher le fichier Open project: KSP-KOS/KOS

Méthodes publiques

Méthode Description
AddTrigger ( int triggerFunctionPointer ) : void
AddVariable ( kOS.Safe.Execution.Variable variable, string identifier, bool local, bool overwrite = false ) : void

Make a new variable at either the local depth or the global depth depending. throws exception if it already exists as a boundvariable at the desired scope level, unless overwrite = true.

AssertValidDelegateCall ( IUserDelegate userDelegate ) : void

Throw exception if the user delegate is not one the CPU can call right now.

Boot ( ) : void
BreakExecution ( bool manual ) : void
BuiltInExists ( string functionName ) : bool
CPU ( SafeSharedObjects shared ) : System
CallBuiltinFunction ( string functionName ) : void
Dispose ( ) : void
DumpStack ( ) : string
DumpVariables ( ) : string
GetCallTrace ( ) : List

Return the subroutine call trace of how the code got to where it is right now.

GetCodeFragment ( int contextLines ) : List
GetCurrentClosure ( ) : List

Build a clone of the current state of the scope stack, for the sake of capturing a closure.

GetCurrentOpcode ( ) : Opcode
GetInterpreterContext ( ) : IProgramContext
GetOpcodeAt ( int instructionPtr ) : Opcode
GetStackSize ( ) : int
GetStructureEncapsulated ( Structure testValue, bool barewordOkay = false ) : Structure

Identical to GetValue(), except that it guarantees the return value is either already a Structure, or it converts it into a Structure if it's a primitive. It bombs out with an exception if it can't be converted thusly.
Use this in places where the stack value *must* come out as an encapsulated value and something has gone seriously wrong if it can't. This applies to cases where you are attempting to store its value inside another user's variable, mostly.
Hypothetically this should never really be required, as the value is coming FROM a user varible in the first place.

GetValue ( object testValue, bool barewordOkay = false ) : object

Given a value which may or may not be a variable name, return the value back. If it's not a variable, return it as-is. If it's a variable, look it up and return that.

IdentifierExistsInScope ( string identifier ) : bool

Test if an identifier is a variable you can get the value of at the moment (var name exists and is in scope). Return true if you can, false if you can't.

KOSFixedUpdate ( double deltaTime ) : void
MakeUserDelegate ( int entryPoint, bool withClosure ) : IUserDelegate

Build a delegate call for the given function entry point, in which it will capture a closure of the current runtime scoping state to be used when that function gets called later by OpcodeCall:

MoveStackPointer ( int delta ) : void
PeekRaw ( int digDepth, bool &checkOkay ) : object

Peek at a value atop the stack without popping it, and without evaluating it to get the variable's value. (i.e. if the thing in the stack is $foo, and the variable foo has value 5, you'll get the string "$foo" returned, not the integer 5).

PeekStructureEncapsulated ( int digDepth, bool barewordOkay = false ) : Structure

Identical to PeekValue(), except that it guarantees the return value is either already a Structure, or it converts it into a Structure if it's a primitive. It bombs out with an exception if it can't be converted thusly.
Use this in places where the stack value *must* come out as an encapsulated value and something has gone seriously wrong if it can't. This applies to cases where you are attempting to store its value inside a user's variable, mostly.

PeekValue ( int digDepth, bool barewordOkay = false ) : object

Peek at a value atop the stack without popping it, and if it's a variable name then get its value, else just return it as it is.

NOTE: Evaluating variables when you don't really need to is pointlessly expensive, as it needs to walk the scoping stack to exhaust a search. If you don't need to evaluate variables, then consider using PeekRaw() instead.

PeekValueEncapsulated ( int digDepth, bool barewordOkay = false ) : object

Identical to PeekStructureEncapsulated(), except that it doesn't complain if the result can't be converted to a Structure. It's acceptable for it to not be a Structure, in which case the original object is returned as-is.
Use this in places where the stack value *should* come out as an encapsulated value if it can, but there are some valid cases where it might not be.

PopAboveStack ( int howMany ) : object

Pop one or more things from the secret "over" stack, only returning the finalmost thing popped. (i.e if you pop 3 things then you get: pop once and throw away, pop again and throw away, pop again and return the popped thing.)

PopStack ( ) : object
PopStructureEncapsulated ( bool barewordOkay = false ) : Structure

Identical to PopValue(), except that it guarantees the return value is either already a Structure, or it converts it into a Structure if it's a primitive. It bombs out with an exception if it can't be converted thusly.
Use this in places where the stack value *must* come out as an encapsulated value and something has gone seriously wrong if it can't. This applies to cases where you are attempting to store its value inside a user's variable, mostly.

PopValue ( bool barewordOkay = false ) : object

Pop a value off the stack, and if it's a variable name then get its value, else just return it as it is.

PopValueEncapsulated ( bool barewordOkay = false ) : object

Identical to PopStructureEncapsulated(), except that it doesn't complain if the result can't be converted to a Structure. It's acceptable for it to not be a Structure, in which case the original object is returned as-is.
Use this in places where the stack value *should* come out as an encapsulated value if it can, but there are some valid cases where it might not be.

PushAboveStack ( object thing ) : void

Push a single thing onto the secret "over" stack.

PushStack ( object item ) : void
RemoveTrigger ( int triggerFunctionPointer ) : void
RemoveVariable ( string identifier ) : void

Removes a variable, following current scoping rules, removing the innermost scope of the variable that is found.

If the variable cannot be found, it fails silently without complaint.

ResetStatistics ( ) : void
RunProgram ( List program ) : void
RunProgram ( List program, bool silent ) : void
SelectAutopilotMode ( string autopilotMode ) : void
SetGlobal ( string identifier, object value ) : void

Make a new global variable at the localmost scoping level and give it a starting value, or overwrite an existing variable at the localmost level with a starting value.

This does NOT scan up the scoping stack like SetValue() does. It operates at the global level only.

SetNewLocal ( string identifier, object value ) : void

Try to make a new local variable at the localmost scoping level and give it a starting value. It errors out of there is already one there by the same name.

This does NOT scan up the scoping stack like SetValue() does. It operates at the local level only.

This is the normal way to make a new local variable. You cannot make a local variable without attempting to give it a value.

SetValue ( string identifier, object value ) : void

Try to set the value of the identifier at the localmost level possible, by scanning up the scope stack to find the local-most level at which the identifier is a variable, and assigning it the value there.

If no such value is found, all the way up to the global level, then it resorts to making a global variable with the name and using that.

This is the normal way to make a new global variable. You cannot make a global variable without attempting to give it a value.

SetValueExists ( string identifier, object value ) : void

Try to set the value of the identifier at the localmost level possible, by scanning up the scope stack to find the local-most level at which the identifier is a variable, and assigning it the value there.

If no such value is found, an error is thrown. It only stores into variables that already exist, refusing to create new variables.

StartCompileStopwatch ( ) : void
StatisticsDump ( bool doProfiling ) : string
StopCompileStopwatch ( ) : void
SwitchToProgramContext ( ) : IProgramContext
ToggleFlyByWire ( string paramName, bool enabled ) : void
VariableIsRemovable ( Variable variable ) : bool
YieldProgram ( YieldFinishedDetector yieldTracker ) : void

Call when you want to suspend execution of the Opcodes until some future condition becomes true. The CPU will call yieldTracker.Begin() right away, and then after that call yieldTracker.IsFinished() again and again until it returns true. Until IsFinished() returns true, the CPU will not advance any further into the program in its current "mode". Note that the CPU will track "trigger" and "mainline" code separately for this purpose. Waiting in mainline code will still allow triggers to run.

Private Methods

Méthode Description
AbortAllYields ( ) : void
CalculateProfileResult ( ) : void
ContinueExecution ( bool doProfiling ) : void
ExecuteInstruction ( IProgramContext context, bool doProfiling ) : bool
GetNestedDictionary ( int peekDepth ) : VariableScope

Gets the dictionary N levels of nesting down the dictionary stack, where zero is the current localmost level. Never errors out or fails. If N is too large you just end up with the global scope dictionary. Does not allow the walk to go past the start of the current function scope.

GetNestedDictionary ( string identifier, List searchReport = null ) : VariableScope

Gets the dictionary that contains the given identifier, starting the search at the local level and scanning the scopes upward all the way to the global dictionary.
Does not allow the walk to use scope frames that were not directly in this scope's lexical chain. It skips over scope frames from other branches of the parse tree. (i.e. if a function calls a function elsewhere).
Returns null when no hit was found.

GetOrCreateVariable ( string identifier ) : Variable

Get the value of a variable or create it at global scope if not found.

GetVariable ( string identifier, bool barewordOkay = false, bool failOkay = false ) : Variable

Get the variable's contents, performing a lookup through all nesting levels up to global.

IsYielding ( ) : bool
PopContext ( ) : void
PopFirstContext ( ) : void
PostUpdateBindings ( ) : void
PreUpdateBindings ( ) : void
PrintStatistics ( ) : void
ProcessTriggers ( ) : void
PushContext ( ProgramContext context ) : void
PushInterpreterContext ( ) : void
RestorePointers ( ) : void
SaveAndClearPointers ( ) : void
SkipCurrentInstructionId ( ) : void

Method Details

AddTrigger() public méthode

public AddTrigger ( int triggerFunctionPointer ) : void
triggerFunctionPointer int
Résultat void

AddVariable() public méthode

Make a new variable at either the local depth or the global depth depending. throws exception if it already exists as a boundvariable at the desired scope level, unless overwrite = true.
public AddVariable ( kOS.Safe.Execution.Variable variable, string identifier, bool local, bool overwrite = false ) : void
variable kOS.Safe.Execution.Variable variable to add
identifier string name of variable to add
local bool true if you want to make it at local depth
overwrite bool true if it's okay to overwrite an existing variable
Résultat void

AssertValidDelegateCall() public méthode

Throw exception if the user delegate is not one the CPU can call right now.
thrown if the cpu is in a state where it can't call this delegate.
public AssertValidDelegateCall ( IUserDelegate userDelegate ) : void
userDelegate IUserDelegate The userdelegate being checked
Résultat void

Boot() public méthode

public Boot ( ) : void
Résultat void

BreakExecution() public méthode

public BreakExecution ( bool manual ) : void
manual bool
Résultat void

BuiltInExists() public méthode

public BuiltInExists ( string functionName ) : bool
functionName string
Résultat bool

CPU() public méthode

public CPU ( SafeSharedObjects shared ) : System
shared SafeSharedObjects
Résultat System

CallBuiltinFunction() public méthode

public CallBuiltinFunction ( string functionName ) : void
functionName string
Résultat void

Dispose() public méthode

public Dispose ( ) : void
Résultat void

DumpStack() public méthode

public DumpStack ( ) : string
Résultat string

DumpVariables() public méthode

public DumpVariables ( ) : string
Résultat string

GetCallTrace() public méthode

Return the subroutine call trace of how the code got to where it is right now.
public GetCallTrace ( ) : List
Résultat List

GetCodeFragment() public méthode

public GetCodeFragment ( int contextLines ) : List
contextLines int
Résultat List

GetCurrentClosure() public méthode

Build a clone of the current state of the scope stack, for the sake of capturing a closure.
public GetCurrentClosure ( ) : List
Résultat List

GetCurrentOpcode() public méthode

public GetCurrentOpcode ( ) : Opcode
Résultat kOS.Safe.Compilation.Opcode

GetInterpreterContext() public méthode

public GetInterpreterContext ( ) : IProgramContext
Résultat IProgramContext

GetOpcodeAt() public méthode

public GetOpcodeAt ( int instructionPtr ) : Opcode
instructionPtr int
Résultat Opcode

GetStackSize() public méthode

public GetStackSize ( ) : int
Résultat int

GetStructureEncapsulated() public méthode

Identical to GetValue(), except that it guarantees the return value is either already a Structure, or it converts it into a Structure if it's a primitive. It bombs out with an exception if it can't be converted thusly.
Use this in places where the stack value *must* come out as an encapsulated value and something has gone seriously wrong if it can't. This applies to cases where you are attempting to store its value inside another user's variable, mostly.
Hypothetically this should never really be required, as the value is coming FROM a user varible in the first place.
public GetStructureEncapsulated ( Structure testValue, bool barewordOkay = false ) : Structure
testValue Structure the object which might be a variable name
barewordOkay bool /// Is this a case in which it's acceptable for the /// variable not to exist, and if it doesn't exist then the variable name itself /// is the value? ///
Résultat Structure

GetValue() public méthode

Given a value which may or may not be a variable name, return the value back. If it's not a variable, return it as-is. If it's a variable, look it up and return that.
public GetValue ( object testValue, bool barewordOkay = false ) : object
testValue object the object which might be a variable name
barewordOkay bool /// Is this a case in which it's acceptable for the /// variable not to exist, and if it doesn't exist then the variable name itself /// is the value? ///
Résultat object

IdentifierExistsInScope() public méthode

Test if an identifier is a variable you can get the value of at the moment (var name exists and is in scope). Return true if you can, false if you can't.
public IdentifierExistsInScope ( string identifier ) : bool
identifier string
Résultat bool

KOSFixedUpdate() public méthode

public KOSFixedUpdate ( double deltaTime ) : void
deltaTime double
Résultat void

MakeUserDelegate() public méthode

Build a delegate call for the given function entry point, in which it will capture a closure of the current runtime scoping state to be used when that function gets called later by OpcodeCall:
public MakeUserDelegate ( int entryPoint, bool withClosure ) : IUserDelegate
entryPoint int Integer location in memory to jump to to start the call
withClosure bool Should the closure be captured for this delegate or ignored
Résultat IUserDelegate

MoveStackPointer() public méthode

public MoveStackPointer ( int delta ) : void
delta int
Résultat void

PeekRaw() public méthode

Peek at a value atop the stack without popping it, and without evaluating it to get the variable's value. (i.e. if the thing in the stack is $foo, and the variable foo has value 5, you'll get the string "$foo" returned, not the integer 5).
public PeekRaw ( int digDepth, bool &checkOkay ) : object
digDepth int Peek at the element this far down the stack (0 means top, 1 means just under the top, etc)
checkOkay bool Tells you whether or not the stack was exhausted. If it's false, then the peek went too deep.
Résultat object

PeekStructureEncapsulated() public méthode

Identical to PeekValue(), except that it guarantees the return value is either already a Structure, or it converts it into a Structure if it's a primitive. It bombs out with an exception if it can't be converted thusly.
Use this in places where the stack value *must* come out as an encapsulated value and something has gone seriously wrong if it can't. This applies to cases where you are attempting to store its value inside a user's variable, mostly.
public PeekStructureEncapsulated ( int digDepth, bool barewordOkay = false ) : Structure
digDepth int Peek at the element this far down the stack (0 means top, 1 means just under the top, etc)
barewordOkay bool Is this a context in which it's acceptable for /// a variable not existing error to occur (in which case the identifier itself /// should therefore become a string object returned)?
Résultat Structure

PeekValue() public méthode

Peek at a value atop the stack without popping it, and if it's a variable name then get its value, else just return it as it is.

NOTE: Evaluating variables when you don't really need to is pointlessly expensive, as it needs to walk the scoping stack to exhaust a search. If you don't need to evaluate variables, then consider using PeekRaw() instead.
public PeekValue ( int digDepth, bool barewordOkay = false ) : object
digDepth int Peek at the element this far down the stack (0 means top, 1 means just under the top, etc)
barewordOkay bool Is this a context in which it's acceptable for /// a variable not existing error to occur (in which case the identifier itself /// should therefore become a string object returned)?
Résultat object

PeekValueEncapsulated() public méthode

Identical to PeekStructureEncapsulated(), except that it doesn't complain if the result can't be converted to a Structure. It's acceptable for it to not be a Structure, in which case the original object is returned as-is.
Use this in places where the stack value *should* come out as an encapsulated value if it can, but there are some valid cases where it might not be.
public PeekValueEncapsulated ( int digDepth, bool barewordOkay = false ) : object
digDepth int Peek at the element this far down the stack (0 means top, 1 means just under the top, etc)
barewordOkay bool Is this a context in which it's acceptable for /// a variable not existing error to occur (in which case the identifier itself /// should therefore become a string object returned)?
Résultat object

PopAboveStack() public méthode

Pop one or more things from the secret "over" stack, only returning the finalmost thing popped. (i.e if you pop 3 things then you get: pop once and throw away, pop again and throw away, pop again and return the popped thing.)
public PopAboveStack ( int howMany ) : object
howMany int
Résultat object

PopStack() public méthode

public PopStack ( ) : object
Résultat object

PopStructureEncapsulated() public méthode

Identical to PopValue(), except that it guarantees the return value is either already a Structure, or it converts it into a Structure if it's a primitive. It bombs out with an exception if it can't be converted thusly.
Use this in places where the stack value *must* come out as an encapsulated value and something has gone seriously wrong if it can't. This applies to cases where you are attempting to store its value inside a user's variable, mostly.
public PopStructureEncapsulated ( bool barewordOkay = false ) : Structure
barewordOkay bool Is this a context in which it's acceptable for /// a variable not existing error to occur (in which case the identifier itself /// should therefore become a string object returned)?
Résultat Structure

PopValue() public méthode

Pop a value off the stack, and if it's a variable name then get its value, else just return it as it is.
public PopValue ( bool barewordOkay = false ) : object
barewordOkay bool Is this a context in which it's acceptable for /// a variable not existing error to occur (in which case the identifier itself /// should therefore become a string object returned)?
Résultat object

PopValueEncapsulated() public méthode

Identical to PopStructureEncapsulated(), except that it doesn't complain if the result can't be converted to a Structure. It's acceptable for it to not be a Structure, in which case the original object is returned as-is.
Use this in places where the stack value *should* come out as an encapsulated value if it can, but there are some valid cases where it might not be.
public PopValueEncapsulated ( bool barewordOkay = false ) : object
barewordOkay bool Is this a context in which it's acceptable for /// a variable not existing error to occur (in which case the identifier itself /// should therefore become a string object returned)?
Résultat object

PushAboveStack() public méthode

Push a single thing onto the secret "over" stack.
public PushAboveStack ( object thing ) : void
thing object
Résultat void

PushStack() public méthode

public PushStack ( object item ) : void
item object
Résultat void

RemoveTrigger() public méthode

public RemoveTrigger ( int triggerFunctionPointer ) : void
triggerFunctionPointer int
Résultat void

RemoveVariable() public méthode

Removes a variable, following current scoping rules, removing the innermost scope of the variable that is found.

If the variable cannot be found, it fails silently without complaint.
public RemoveVariable ( string identifier ) : void
identifier string varible to remove.
Résultat void

ResetStatistics() public méthode

public ResetStatistics ( ) : void
Résultat void

RunProgram() public méthode

public RunProgram ( List program ) : void
program List
Résultat void

RunProgram() public méthode

public RunProgram ( List program, bool silent ) : void
program List
silent bool
Résultat void

SelectAutopilotMode() public méthode

public SelectAutopilotMode ( string autopilotMode ) : void
autopilotMode string
Résultat void

SetGlobal() public méthode

Make a new global variable at the localmost scoping level and give it a starting value, or overwrite an existing variable at the localmost level with a starting value.

This does NOT scan up the scoping stack like SetValue() does. It operates at the global level only.
public SetGlobal ( string identifier, object value ) : void
identifier string variable name to attempt to store into
value object value to put into it
Résultat void

SetNewLocal() public méthode

Try to make a new local variable at the localmost scoping level and give it a starting value. It errors out of there is already one there by the same name.

This does NOT scan up the scoping stack like SetValue() does. It operates at the local level only.

This is the normal way to make a new local variable. You cannot make a local variable without attempting to give it a value.
public SetNewLocal ( string identifier, object value ) : void
identifier string variable name to attempt to store into
value object value to put into it
Résultat void

SetValue() public méthode

Try to set the value of the identifier at the localmost level possible, by scanning up the scope stack to find the local-most level at which the identifier is a variable, and assigning it the value there.

If no such value is found, all the way up to the global level, then it resorts to making a global variable with the name and using that.

This is the normal way to make a new global variable. You cannot make a global variable without attempting to give it a value.
public SetValue ( string identifier, object value ) : void
identifier string variable name to attempt to store into
value object value to put into it
Résultat void

SetValueExists() public méthode

Try to set the value of the identifier at the localmost level possible, by scanning up the scope stack to find the local-most level at which the identifier is a variable, and assigning it the value there.

If no such value is found, an error is thrown. It only stores into variables that already exist, refusing to create new variables.

public SetValueExists ( string identifier, object value ) : void
identifier string variable name to attempt to store into
value object value to put into it
Résultat void

StartCompileStopwatch() public méthode

public StartCompileStopwatch ( ) : void
Résultat void

StatisticsDump() public méthode

public StatisticsDump ( bool doProfiling ) : string
doProfiling bool
Résultat string

StopCompileStopwatch() public méthode

public StopCompileStopwatch ( ) : void
Résultat void

SwitchToProgramContext() public méthode

public SwitchToProgramContext ( ) : IProgramContext
Résultat IProgramContext

ToggleFlyByWire() public méthode

public ToggleFlyByWire ( string paramName, bool enabled ) : void
paramName string
enabled bool
Résultat void

VariableIsRemovable() public méthode

public VariableIsRemovable ( Variable variable ) : bool
variable Variable
Résultat bool

YieldProgram() public méthode

Call when you want to suspend execution of the Opcodes until some future condition becomes true. The CPU will call yieldTracker.Begin() right away, and then after that call yieldTracker.IsFinished() again and again until it returns true. Until IsFinished() returns true, the CPU will not advance any further into the program in its current "mode". Note that the CPU will track "trigger" and "mainline" code separately for this purpose. Waiting in mainline code will still allow triggers to run.
public YieldProgram ( YieldFinishedDetector yieldTracker ) : void
yieldTracker YieldFinishedDetector
Résultat void