本文整理汇总了C#中ICpu.PushStack方法的典型用法代码示例。如果您正苦于以下问题:C# ICpu.PushStack方法的具体用法?C# ICpu.PushStack怎么用?C# ICpu.PushStack使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICpu
的用法示例。
在下文中一共展示了ICpu.PushStack方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReverseStackArgs
/// <summary>
/// Take the topmost arguments down to the ARG_MARKER_STRING, pop them off, and then
/// put them back again in reversed order so a function can read them in normal order.
/// Note that if this is an indirect call, it will also consume the thing just under
/// the ARG_MARKER, since that's expected to be the delegate or KOSDelegate that we already
/// read and pulled the needed information from.
/// <param name="cpu">the cpu we are running on, fur stack manipulation purposes</param>
/// <param name="direct">need to know if this was a direct or indirect call. If indirect,
/// then that means it also needs to consume the indirect reference off the stack just under
/// the args</param>
/// </summary>
public static void ReverseStackArgs(ICpu cpu, bool direct)
{
List<object> args = new List<object>();
object arg = cpu.PopValue();
while (cpu.GetStackSize() > 0 && arg.GetType() != ArgMarkerType)
{
args.Add(arg);
// It's important to dereference with PopValue, not using PopStack, because the function
// being called might not even be able to see the variable in scope anyway.
// In other words, if calling a function like so:
// declare foo to 3.
// myfunc(foo).
// The code inside myfunc needs to see that as being identical to just saying:
// myfunc(3).
// It has to be unaware of the fact that the name of the argument was 'foo'. It just needs to
// see the contents that were inside foo.
arg = cpu.PopValue();
}
if (! direct)
cpu.PopStack(); // throw away the delegate or KOSDelegate info - we already snarfed it by now.
// Push the arg marker back on again.
cpu.PushStack(new KOSArgMarkerType());
// Push the arguments back on again, which will invert their order:
foreach (object item in args)
cpu.PushStack(item);
}
示例2: Execute
public override void Execute(ICpu cpu)
{
object index = cpu.PopValue();
if (index is double || index is float)
{
index = Convert.ToInt32(index); // allow expressions like (1.0) to be indexes
}
object list = cpu.PopValue();
if (!(list is IIndexable)) throw new Exception(string.Format("Can't iterate on an object of type {0}", list.GetType()));
if (!(index is int)) throw new Exception("The index must be an integer number");
object value = ((IIndexable)list).GetIndex((int)index);
cpu.PushStack(value);
}
示例3: Execute
public override void Execute(ICpu cpu)
{
bool worked;
object shouldBeArgMarker = cpu.PeekRaw(0,out worked);
if ( !worked || (shouldBeArgMarker == null) || (shouldBeArgMarker.GetType() != OpcodeCall.ArgMarkerType) )
{
cpu.PushStack(false); // these are internally used, so no Strucutre.FromPrimitive wrapper call.
}
else
{
cpu.PushStack(true); // these are internally used, so no Strucutre.FromPrimitive wrapper call.
}
}
示例4: Execute
//.........这里部分代码省略.........
{
bool foundBottom = false;
int digDepth;
int argsCount = 0;
for (digDepth = 0; (! foundBottom) && digDepth < cpu.GetStackSize() ; ++digDepth)
{
object arg = cpu.PeekValue(digDepth);
if (arg != null && arg.GetType() == ArgMarkerType)
foundBottom = true;
else
++argsCount;
}
functionPointer = cpu.PeekValue(digDepth);
if (! ( functionPointer is Delegate))
{
// Indirect calls are meant to be delegates. If they are not, then that means the
// function parentheses were put on by the user when they weren't required. Just dig
// through the stack to the result of the getMember and skip the rest of the execute logic
// If args were passed to a non-method, then clean them off the stack, and complain:
if (argsCount>0)
{
for (int i=1 ; i<=argsCount; ++i)
cpu.PopValue();
throw new KOSArgumentMismatchException(
0, argsCount, "\n(In fact in this case the parentheses are entirely optional)");
}
cpu.PopValue(); // pop the ArgMarkerString too.
return;
}
}
// If it's a string it might not really be a built-in, it might still be a user func.
// Detect whether it's built-in, and if it's not, then convert it into the equivalent
// user func call by making it be an integer instruction pointer instead:
if (functionPointer is string)
{
string functionName = functionPointer as string;
if (functionName.EndsWith("()"))
functionName = functionName.Substring(0, functionName.Length - 2);
if (!(cpu.BuiltInExists(functionName)))
{
// It is not a built-in, so instead get its value as a user function pointer variable, despite
// the fact that it's being called AS IF it was direct.
if (!functionName.EndsWith("*")) functionName = functionName + "*";
if (!functionName.StartsWith("$")) functionName = "$" + functionName;
functionPointer = cpu.GetValue(functionName);
}
}
IUserDelegate userDelegate = functionPointer as IUserDelegate;
if (userDelegate != null)
functionPointer = userDelegate.EntryPoint;
if (functionPointer is int)
{
ReverseStackArgs(cpu);
int currentPointer = cpu.InstructionPointer;
DeltaInstructionPointer = (int)functionPointer - currentPointer;
var contextRecord = new SubroutineContext(currentPointer+1);
cpu.PushAboveStack(contextRecord);
if (userDelegate != null)
{
cpu.AssertValidDelegateCall(userDelegate);
// Reverse-push the closure's scope record, just after the function return context got put on the stack.
for (int i = userDelegate.Closure.Count - 1 ; i >= 0 ; --i)
cpu.PushAboveStack(userDelegate.Closure[i]);
}
}
else if (functionPointer is string)
{
// Built-ins don't need to dereference the stack values because they
// don't leave the scope - they're not implemented that way. But later we
// might want to change that.
var name = functionPointer as string;
string functionName = name;
if (functionName.EndsWith("()"))
functionName = functionName.Substring(0, functionName.Length - 2);
cpu.CallBuiltinFunction(functionName);
}
else if (functionPointer is Delegate)
{
delegateReturn = ExecuteDelegate(cpu, (Delegate)functionPointer);
}
else
{
// This is one of those "the user had better NEVER see this error" sorts of messages that's here to keep us in check:
throw new Exception(
string.Format("kOS internal error: OpcodeCall calling a function described using {0} which is of type {1} and kOS doesn't know how to call that.", functionPointer, functionPointer.GetType().Name)
);
}
if (! Direct)
{
cpu.PopValue(); // consume function name, branch index, or delegate
}
if (functionPointer is Delegate)
{
cpu.PushStack(delegateReturn); // And now leave the return value on the stack to be read.
}
}
示例5: StaticExecute
//.........这里部分代码省略.........
string functionName = functionPointer.ToString();
if (functionName.EndsWith("()"))
functionName = functionName.Substring(0, functionName.Length - 2);
if (!(cpu.BuiltInExists(functionName)))
{
// It is not a built-in, so instead get its value as a user function pointer variable, despite
// the fact that it's being called AS IF it was direct.
if (!functionName.EndsWith("*")) functionName = functionName + "*";
if (!functionName.StartsWith("$")) functionName = "$" + functionName;
functionPointer = cpu.GetValue(functionName);
}
}
KOSDelegate kosDelegate = functionPointer as KOSDelegate;
if (kosDelegate != null)
{
if (! calledFromKOSDelegateCall)
kosDelegate.InsertPreBoundArgs();
}
IUserDelegate userDelegate = functionPointer as IUserDelegate;
if (userDelegate != null)
functionPointer = userDelegate.EntryPoint;
BuiltinDelegate builtinDel = functionPointer as BuiltinDelegate;
if (builtinDel != null && (! calledFromKOSDelegateCall) )
functionPointer = builtinDel.Name;
// If the IP for a jump location got encapsulated as a user int when it got stored
// into the internal variable, then get the primitive int back out of it again:
ScalarIntValue userInt = functionPointer as ScalarIntValue;
if (userInt != null)
functionPointer = userInt.GetIntValue();
// Convert to int instead of cast in case the identifier is stored
// as an encapsulated ScalarValue, preventing an unboxing collision.
if (functionPointer is int || functionPointer is ScalarValue)
{
CpuUtility.ReverseStackArgs(cpu, direct);
var contextRecord = new SubroutineContext(cpu.InstructionPointer+1);
newIP = Convert.ToInt32(functionPointer);
cpu.PushAboveStack(contextRecord);
if (userDelegate != null)
{
cpu.AssertValidDelegateCall(userDelegate);
// Reverse-push the closure's scope record, just after the function return context got put on the stack.
for (int i = userDelegate.Closure.Count - 1 ; i >= 0 ; --i)
cpu.PushAboveStack(userDelegate.Closure[i]);
}
}
else if (functionPointer is string)
{
// Built-ins don't need to dereference the stack values because they
// don't leave the scope - they're not implemented that way. But later we
// might want to change that.
var name = functionPointer as string;
string functionName = name;
if (functionName.EndsWith("()"))
functionName = functionName.Substring(0, functionName.Length - 2);
cpu.CallBuiltinFunction(functionName);
// If this was indirect, we need to consume the thing under the return value.
// as that was the indirect BuiltInDelegate:
if ((! direct) && builtinDel != null)
{
object topThing = cpu.PopStack();
cpu.PopStack(); // remove BuiltInDelegate object.
cpu.PushStack(topThing); // put return value back.
}
}
else if (functionPointer is ISuffixResult)
{
var result = (ISuffixResult) functionPointer;
if (!result.HasValue)
{
result.Invoke(cpu);
}
delegateReturn = result.Value;
}
// TODO:erendrake This else if is likely never used anymore
else if (functionPointer is Delegate)
{
throw new KOSYouShouldNeverSeeThisException("OpcodeCall unexpected function pointer delegate");
}
else
{
throw new KOSNotInvokableException(functionPointer);
}
if (functionPointer is ISuffixResult)
{
if (! (delegateReturn is KOSPassThruReturn))
cpu.PushStack(delegateReturn); // And now leave the return value on the stack to be read.
}
return newIP;
}
示例6: ReverseStackArgs
/// <summary>
/// Take the topmost arguments down to the ARG_MARKER_STRING, pop them off, and then
/// put them back again in reversed order so a function can read them in normal order.
/// </summary>
public void ReverseStackArgs(ICpu cpu)
{
List<object> args = new List<object>();
object arg = cpu.PopValue();
while (arg != null && (!(arg.ToString().Equals(ARG_MARKER_STRING))))
{
args.Add(arg);
// It's important to dereference with PopValue, not using PopStack, because the function
// being called might not even be able to see the variable in scope anyway.
// In other words, if calling a function like so:
// declare foo to 3.
// myfunc(foo).
// The code inside myfunc needs to see that as being identical to just saying:
// myfunc(3).
// It has to be unaware of the fact that the name of the argument was 'foo'. It just needs to
// see the contents that were inside foo.
arg = cpu.PopValue();
}
// Push the arg marker back on again.
cpu.PushStack(ARG_MARKER_STRING);
// Push the arguments back on again, which will invert their order:
foreach (object item in args)
cpu.PushStack(item);
}