本文整理汇总了C#中Jurassic.Compiler.ILGenerator.DefineLabelPosition方法的典型用法代码示例。如果您正苦于以下问题:C# ILGenerator.DefineLabelPosition方法的具体用法?C# ILGenerator.DefineLabelPosition怎么用?C# ILGenerator.DefineLabelPosition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Jurassic.Compiler.ILGenerator
的用法示例。
在下文中一共展示了ILGenerator.DefineLabelPosition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateEndOfStatement
/// <summary>
/// Generates CIL for the end of every statement.
/// </summary>
/// <param name="generator"> The generator to output the CIL to. </param>
/// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
/// <param name="locals"> Variables common to both GenerateStartOfStatement() and GenerateEndOfStatement(). </param>
public void GenerateEndOfStatement(ILGenerator generator, OptimizationInfo optimizationInfo, StatementLocals locals)
{
if (locals.NonDefaultBreakStatementBehavior == false && this.HasLabels == true)
{
// Revert the information needed by the break statement.
generator.DefineLabelPosition(locals.EndOfStatement);
optimizationInfo.PopBreakOrContinueInfo();
}
#if DEBUG && !SILVERLIGHT && !XBOX
// Check that the stack count is zero.
if (generator is DynamicILGenerator && ((DynamicILGenerator)generator).StackSize != locals.OriginalStackSize)
throw new InvalidOperationException("Encountered unexpected stack imbalance.");
#endif
}
示例2: GenerateSet
//.........这里部分代码省略.........
// Call the indexer.
generator.LoadVariable(value);
generator.LoadBoolean(optimizationInfo.StrictMode);
generator.Call(ReflectionHelpers.ObjectInstance_SetPropertyValue_Int);
}
else if (propertyName != null)
{
//// Load the left-hand side and convert to an object instance.
//var lhs = this.GetOperand(0);
//lhs.GenerateCode(generator, optimizationInfo);
//EmitConversion.ToObject(generator, lhs.ResultType);
//// Call the indexer.
//generator.LoadString(propertyName);
//generator.LoadVariable(value);
//generator.LoadBoolean(optimizationInfo.StrictMode);
//generator.Call(ReflectionHelpers.ObjectInstance_SetPropertyValue_String);
// Named property modification (e.g. x.property = y)
// -------------------------------------------------
// __object_cacheKey = null;
// __object_property_cachedIndex = 0;
// ...
// if (__object_cacheKey != object.InlineCacheKey)
// object.InlineSetPropertyValue("property", value, strictMode, out __object_property_cachedIndex, out __object_cacheKey)
// else
// object.InlinePropertyValues[__object_property_cachedIndex] = value;
// Load the left-hand side and convert to an object instance.
var lhs = this.GetOperand(0);
lhs.GenerateCode(generator, optimizationInfo);
EmitConversion.ToObject(generator, lhs.ResultType);
// TODO: share these variables somehow.
var cacheKey = generator.DeclareVariable(typeof(object));
var cachedIndex = generator.DeclareVariable(typeof(int));
// Store the object into a temp variable.
var objectInstance = generator.DeclareVariable(PrimitiveType.Object);
generator.StoreVariable(objectInstance);
// if (__object_cacheKey != object.InlineCacheKey)
generator.LoadVariable(cacheKey);
generator.LoadVariable(objectInstance);
generator.Call(ReflectionHelpers.ObjectInstance_InlineCacheKey);
var elseClause = generator.CreateLabel();
generator.BranchIfEqual(elseClause);
// xxx = object.InlineSetPropertyValue("property", value, strictMode, out __object_property_cachedIndex, out __object_cacheKey)
generator.LoadVariable(objectInstance);
generator.LoadString(propertyName);
generator.LoadVariable(value);
generator.LoadBoolean(optimizationInfo.StrictMode);
generator.LoadAddressOfVariable(cachedIndex);
generator.LoadAddressOfVariable(cacheKey);
generator.Call(ReflectionHelpers.ObjectInstance_InlineSetPropertyValue);
var endOfIf = generator.CreateLabel();
generator.Branch(endOfIf);
// else
generator.DefineLabelPosition(elseClause);
// object.InlinePropertyValues[__object_property_cachedIndex] = value;
generator.LoadVariable(objectInstance);
generator.Call(ReflectionHelpers.ObjectInstance_InlinePropertyValues);
generator.LoadVariable(cachedIndex);
generator.LoadVariable(value);
generator.StoreArrayElement(typeof(object));
// End of the if statement
generator.DefineLabelPosition(endOfIf);
}
else
{
// Dynamic property access
// -----------------------
// xxx = object.Get(x)
// Load the left-hand side and convert to an object instance.
var lhs = this.GetOperand(0);
lhs.GenerateCode(generator, optimizationInfo);
EmitConversion.ToObject(generator, lhs.ResultType);
// Load the property name and convert to a string.
var rhs = this.GetOperand(1);
rhs.GenerateCode(generator, optimizationInfo);
EmitConversion.ToString(generator, rhs.ResultType);
// Call the indexer.
generator.LoadVariable(value);
generator.LoadBoolean(optimizationInfo.StrictMode);
generator.Call(ReflectionHelpers.ObjectInstance_SetPropertyValue_String);
}
// The temporary variable is no longer needed.
generator.ReleaseTemporaryVariable(value);
}
示例3: GenerateThis
/// <summary>
/// Generates code to push the "this" value for a function call.
/// </summary>
/// <param name="generator"> The generator to output the CIL to. </param>
public void GenerateThis(ILGenerator generator)
{
// Optimization: if there are no with scopes, simply emit undefined.
bool scopeChainHasWithScope = false;
var scope = this.Scope;
do
{
if (scope is ObjectScope && ((ObjectScope)scope).ProvidesImplicitThisValue == true)
{
scopeChainHasWithScope = true;
break;
}
scope = scope.ParentScope;
} while (scope != null);
if (scopeChainHasWithScope == false)
{
// No with scopes in the scope chain, use undefined as the "this" value.
EmitHelpers.EmitUndefined(generator);
return;
}
var end = generator.CreateLabel();
scope = this.Scope;
ILLocalVariable scopeVariable = generator.CreateTemporaryVariable(typeof(Scope));
EmitHelpers.LoadScope(generator);
generator.StoreVariable(scopeVariable);
do
{
if (scope is DeclarativeScope)
{
if (scope.HasDeclaredVariable(this.Name))
{
// The variable exists but declarative scopes always produce undefined for
// the "this" value.
EmitHelpers.EmitUndefined(generator);
break;
}
}
else
{
var objectScope = (ObjectScope)scope;
// Check if the property exists by calling scope.ScopeObject.HasProperty(propertyName)
if (objectScope.ProvidesImplicitThisValue == false)
EmitHelpers.EmitUndefined(generator);
generator.LoadVariable(scopeVariable);
generator.CastClass(typeof(ObjectScope));
generator.Call(ReflectionHelpers.ObjectScope_ScopeObject);
if (objectScope.ProvidesImplicitThisValue == true)
generator.Duplicate();
generator.LoadString(this.Name);
generator.Call(ReflectionHelpers.ObjectInstance_HasProperty);
generator.BranchIfTrue(end);
generator.Pop();
// If the name is not defined, use undefined for the "this" value.
if (scope.ParentScope == null)
{
EmitHelpers.EmitUndefined(generator);
}
}
// Try the parent scope.
if (scope.ParentScope != null && scope.ExistsAtRuntime == true)
{
generator.LoadVariable(scopeVariable);
generator.Call(ReflectionHelpers.Scope_ParentScope);
generator.StoreVariable(scopeVariable);
}
scope = scope.ParentScope;
} while (scope != null);
// Release the temporary variable.
generator.ReleaseTemporaryVariable(scopeVariable);
// Define a label at the end.
generator.DefineLabelPosition(end);
}
示例4: GenerateGet
/// <summary>
/// Pushes the value of the reference onto the stack.
/// </summary>
/// <param name="generator"> The generator to output the CIL to. </param>
/// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
/// <param name="throwIfUnresolvable"> <c>true</c> to throw a ReferenceError exception if
/// the name is unresolvable; <c>false</c> to output <c>null</c> instead. </param>
public void GenerateGet(ILGenerator generator, OptimizationInfo optimizationInfo, bool throwIfUnresolvable)
{
// This method generates code to retrieve the value of a variable, given the name of
// variable and scope in which the variable is being referenced. The variable was
// not necessary declared in this scope - it might be declared in any of the parent
// scopes (together called a scope chain). The general algorithm is to start at the
// head of the chain and search backwards until the variable is found. There are
// two types of scopes: declarative scopes and object scopes. Object scopes are hard -
// it cannot be known at compile time whether the variable exists or not so runtime
// checks have to be inserted. Declarative scopes are easier - variables have to be
// declared and cannot be deleted. There is one tricky bit: new variables can be
// introduced into a declarative scope at runtime by a non-strict eval() statement.
// Even worse, variables that were introduced by means of an eval() *can* be deleted.
var scope = this.Scope;
ILLocalVariable scopeVariable = null;
var endOfGet = generator.CreateLabel();
do
{
if (scope is DeclarativeScope)
{
// The variable was declared in this scope.
var variable = scope.GetDeclaredVariable(this.Name);
if (variable != null)
{
if (scope.ExistsAtRuntime == false)
{
// The scope has been optimized away. The value of the variable is stored
// in an ILVariable.
// Declare an IL local variable if no storage location has been allocated yet.
if (variable.Store == null)
variable.Store = generator.DeclareVariable(typeof(object), variable.Name);
// Load the value from the variable.
generator.LoadVariable(variable.Store);
// Ensure that we match ResultType.
EmitConversion.Convert(generator, variable.Type, this.ResultType, optimizationInfo);
}
else
{
// scope.Values[index]
if (scopeVariable == null)
EmitHelpers.LoadScope(generator);
else
generator.LoadVariable(scopeVariable);
generator.CastClass(typeof(DeclarativeScope));
generator.Call(ReflectionHelpers.DeclarativeScope_Values);
generator.LoadInt32(variable.Index);
generator.LoadArrayElement(typeof(object));
}
// The variable was found - no need to search any more parent scopes.
break;
}
else
{
// The variable was not defined at compile time, but may have been
// introduced by an eval() statement.
if (optimizationInfo.MethodOptimizationHints.HasEval == true)
{
// Check the variable exists: if (scope.HasValue(variableName) == true) {
if (scopeVariable == null)
EmitHelpers.LoadScope(generator);
else
generator.LoadVariable(scopeVariable);
generator.CastClass(typeof(DeclarativeScope));
generator.LoadString(this.Name);
generator.Call(ReflectionHelpers.Scope_HasValue);
var hasValueClause = generator.CreateLabel();
generator.BranchIfFalse(hasValueClause);
// Load the value of the variable.
if (scopeVariable == null)
EmitHelpers.LoadScope(generator);
else
generator.LoadVariable(scopeVariable);
generator.CastClass(typeof(DeclarativeScope));
generator.LoadString(this.Name);
generator.Call(ReflectionHelpers.Scope_GetValue);
generator.Branch(endOfGet);
// }
generator.DefineLabelPosition(hasValueClause);
}
}
}
else
{
if (scope.ParentScope == null)
{
//.........这里部分代码省略.........
示例5: GenerateCode
/// <summary>
/// Generates IL for the script.
/// </summary>
/// <param name="generator"> The generator to output the CIL to. </param>
/// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
protected override void GenerateCode(ILGenerator generator, OptimizationInfo optimizationInfo)
{
// Method signature: object FunctionDelegate(Compiler.Scope scope, object thisObject, Library.FunctionInstance functionObject, object[] arguments)
// Initialize the scope (note: the initial scope for a function is always declarative).
this.InitialScope.GenerateScopeCreation(generator, optimizationInfo);
// Verify the scope is correct.
VerifyScope(generator);
// In ES3 the "this" value must be an object. See 10.4.3 in the spec.
if (this.StrictMode == false && this.MethodOptimizationHints.HasThis == true)
{
// if (thisObject == null || thisObject == Null.Value || thisObject == Undefined.Value)
EmitHelpers.LoadThis(generator);
generator.LoadNull();
generator.CompareEqual();
EmitHelpers.LoadThis(generator);
EmitHelpers.EmitNull(generator);
generator.CompareEqual();
generator.BitwiseOr();
EmitHelpers.LoadThis(generator);
EmitHelpers.EmitUndefined(generator);
generator.CompareEqual();
generator.BitwiseOr();
// {
var startOfFalse = generator.CreateLabel();
generator.BranchIfFalse(startOfFalse);
// thisObject = engine.Global;
EmitHelpers.LoadScriptEngine(generator);
generator.Call(ReflectionHelpers.ScriptEngine_Global);
// } else {
var endOfIf = generator.CreateLabel();
generator.Branch(endOfIf);
generator.DefineLabelPosition(startOfFalse);
// thisObject = TypeConverter.ToObject(thisObject);
EmitHelpers.LoadThis(generator);
EmitConversion.ToObject(generator, PrimitiveType.Any);
// }
generator.DefineLabelPosition(endOfIf);
EmitHelpers.StoreThis(generator);
}
// Transfer the function name into the scope.
if (string.IsNullOrEmpty(this.Name) == false &&
this.ArgumentNames.Contains(this.Name) == false &&
optimizationInfo.MethodOptimizationHints.HasVariable(this.Name))
{
EmitHelpers.LoadFunction(generator);
var functionName = new NameExpression(this.InitialScope, this.Name);
functionName.GenerateSet(generator, optimizationInfo, PrimitiveType.Any, false);
}
// Transfer the arguments object into the scope.
if (this.MethodOptimizationHints.HasArguments == true && this.ArgumentNames.Contains("arguments") == false)
{
// prototype
EmitHelpers.LoadScriptEngine(generator);
generator.Call(ReflectionHelpers.ScriptEngine_Object);
generator.Call(ReflectionHelpers.FunctionInstance_InstancePrototype);
// callee
EmitHelpers.LoadFunction(generator);
generator.CastClass(typeof(Library.UserDefinedFunction));
// scope
EmitHelpers.LoadScope(generator);
generator.CastClass(typeof(DeclarativeScope));
// argumentValues
EmitHelpers.LoadArgumentsArray(generator);
generator.NewObject(ReflectionHelpers.Arguments_Constructor);
var arguments = new NameExpression(this.InitialScope, "arguments");
arguments.GenerateSet(generator, optimizationInfo, PrimitiveType.Any, false);
}
// Transfer the argument values into the scope.
// Note: the arguments array can be smaller than expected.
if (this.ArgumentNames.Count > 0)
{
var endOfArguments = generator.CreateLabel();
for (int i = 0; i < this.ArgumentNames.Count; i++)
{
// Check if a duplicate argument name exists.
bool duplicate = false;
for (int j = i + 1; j < this.ArgumentNames.Count; j++)
if (this.ArgumentNames[i] == this.ArgumentNames[j])
{
duplicate = true;
break;
}
if (duplicate == true)
continue;
//.........这里部分代码省略.........
示例6: GenerateCode
/// <summary>
/// Generates CIL for the statement.
/// </summary>
/// <param name="generator"> The generator to output the CIL to. </param>
/// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
public override void GenerateCode(ILGenerator generator, OptimizationInfo optimizationInfo)
{
// Generate code for the start of the statement.
var statementLocals = new StatementLocals() { NonDefaultBreakStatementBehavior = true };
GenerateStartOfStatement(generator, optimizationInfo, statementLocals);
// We need a label for each case clause and one for the default case.
var jumpTargets = new ILLabel[this.CaseClauses.Count];
int defaultIndex = -1;
ILLabel endOfSwitch = generator.CreateLabel();
// Generate code for the switch value.
var startOfSwitch = generator.CreateLabel();
this.Value.GenerateCode(generator, optimizationInfo);
EmitConversion.ToAny(generator, this.Value.ResultType);
// Save the switch value in a variable.
var switchValue = generator.CreateTemporaryVariable(typeof(object));
generator.StoreVariable(switchValue);
for (int i = 0; i < this.CaseClauses.Count; i ++)
{
var caseClause = this.CaseClauses[i];
// Create a label for each clause.
jumpTargets[i] = generator.CreateLabel();
if (caseClause.Value == null)
{
// This is a default clause.
defaultIndex = i;
continue;
}
// TypeComparer.StrictEquals(switchValue, caseValue)
generator.LoadVariable(switchValue);
caseClause.Value.GenerateCode(generator, optimizationInfo);
EmitConversion.ToAny(generator, caseClause.Value.ResultType);
generator.Call(ReflectionHelpers.TypeComparer_StrictEquals);
// if (TypeComparer.StrictEquals(switchValue, caseValue) == true)
// goto case i
generator.BranchIfTrue(jumpTargets[i]);
}
// None of the cases matched, jump to the default clause or the end of the switch.
if (defaultIndex >= 0)
generator.Branch(jumpTargets[defaultIndex]);
else
generator.Branch(endOfSwitch);
for (int i = 0; i < this.CaseClauses.Count; i++)
{
// Define a label at the start of the case clause.
generator.DefineLabelPosition(jumpTargets[i]);
// Set up the information needed by the break statement.
optimizationInfo.PushBreakOrContinueInfo(this.Labels, endOfSwitch, null, labelledOnly: false);
// Emit the case clause statements.
foreach (var statement in this.CaseClauses[i].BodyStatements)
statement.GenerateCode(generator, optimizationInfo);
// Revert the information needed by the break statement.
optimizationInfo.PopBreakOrContinueInfo();
}
// Define a label for the end of the switch statement.
generator.DefineLabelPosition(endOfSwitch);
// Release the switch value variable for use elsewhere.
generator.ReleaseTemporaryVariable(switchValue);
// Generate code for the end of the statement.
GenerateEndOfStatement(generator, optimizationInfo, statementLocals);
}
示例7: GenerateGet
//.........这里部分代码省略.........
// Load the left-hand side and convert to an object instance.
var lhs = this.GetOperand(0);
lhs.GenerateCode(generator, optimizationInfo);
EmitConversion.ToObject(generator, lhs.ResultType, optimizationInfo);
// Load the right-hand side and convert to a uint32.
var rhs = this.GetOperand(1);
rhs.GenerateCode(generator, optimizationInfo);
EmitConversion.ToUInt32(generator, rhs.ResultType);
// Call the indexer.
generator.Call(ReflectionHelpers.ObjectInstance_GetPropertyValue_Int);
}
else if (propertyName != null)
{
//// Load the left-hand side and convert to an object instance.
//var lhs = this.GetOperand(0);
//lhs.GenerateCode(generator, optimizationInfo);
//EmitConversion.ToObject(generator, lhs.ResultType);
//// Call Get(string)
//generator.LoadString(propertyName);
//generator.Call(ReflectionHelpers.ObjectInstance_GetPropertyValue_String);
// Named property access (e.g. x = y.property)
// -------------------------------------------
// __object_cacheKey = null;
// __object_property_cachedIndex = 0;
// ...
// if (__object_cacheKey != object.InlineCacheKey)
// xxx = object.InlineGetPropertyValue("property", out __object_property_cachedIndex, out __object_cacheKey)
// else
// xxx = object.InlinePropertyValues[__object_property_cachedIndex];
// Load the left-hand side and convert to an object instance.
var lhs = this.GetOperand(0);
lhs.GenerateCode(generator, optimizationInfo);
EmitConversion.ToObject(generator, lhs.ResultType, optimizationInfo);
// TODO: share these variables somehow.
var cacheKey = generator.DeclareVariable(typeof(object));
var cachedIndex = generator.DeclareVariable(typeof(int));
// Store the object into a temp variable.
var objectInstance = generator.DeclareVariable(PrimitiveType.Object);
generator.StoreVariable(objectInstance);
// if (__object_cacheKey != object.InlineCacheKey)
generator.LoadVariable(cacheKey);
generator.LoadVariable(objectInstance);
generator.Call(ReflectionHelpers.ObjectInstance_InlineCacheKey);
var elseClause = generator.CreateLabel();
generator.BranchIfEqual(elseClause);
// value = object.InlineGetProperty("property", out __object_property_cachedIndex, out __object_cacheKey)
generator.LoadVariable(objectInstance);
generator.LoadString(propertyName);
generator.LoadAddressOfVariable(cachedIndex);
generator.LoadAddressOfVariable(cacheKey);
generator.Call(ReflectionHelpers.ObjectInstance_InlineGetPropertyValue);
var endOfIf = generator.CreateLabel();
generator.Branch(endOfIf);
// else
generator.DefineLabelPosition(elseClause);
// value = object.InlinePropertyValues[__object_property_cachedIndex];
generator.LoadVariable(objectInstance);
generator.Call(ReflectionHelpers.ObjectInstance_InlinePropertyValues);
generator.LoadVariable(cachedIndex);
generator.LoadArrayElement(typeof(object));
// End of the if statement
generator.DefineLabelPosition(endOfIf);
}
else
{
// Dynamic property access
// -----------------------
// xxx = object.Get(x)
// Load the left-hand side and convert to an object instance.
var lhs = this.GetOperand(0);
lhs.GenerateCode(generator, optimizationInfo);
EmitConversion.ToObject(generator, lhs.ResultType, optimizationInfo);
// Load the property name and convert to a string.
var rhs = this.GetOperand(1);
rhs.GenerateCode(generator, optimizationInfo);
EmitConversion.ToString(generator, rhs.ResultType);
// Call Get(string)
generator.Call(ReflectionHelpers.ObjectInstance_GetPropertyValue_String);
}
}
示例8: GenerateCode
/// <summary>
/// Generates CIL for the statement.
/// </summary>
/// <param name="generator"> The generator to output the CIL to. </param>
/// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
public override void GenerateCode(ILGenerator generator, OptimizationInfo optimizationInfo)
{
// Generate code for the start of the statement.
var statementLocals = new StatementLocals() { NonDefaultBreakStatementBehavior = true, NonDefaultDebugInfoBehavior = true };
GenerateStartOfStatement(generator, optimizationInfo, statementLocals);
// Construct a loop expression.
// var enumerator = TypeUtilities.EnumeratePropertyNames(rhs).GetEnumerator();
// while (true) {
// continue-target:
// if (enumerator.MoveNext() == false)
// goto break-target;
// lhs = enumerator.Current;
//
// <body statements>
// }
// break-target:
// Call IEnumerable<string> EnumeratePropertyNames(ScriptEngine engine, object obj)
EmitHelpers.LoadScriptEngine(generator);
this.TargetObject.GenerateCode(generator, optimizationInfo);
EmitConversion.ToAny(generator, this.TargetObject.ResultType);
generator.Call(ReflectionHelpers.TypeUtilities_EnumeratePropertyNames);
// Call IEnumerable<string>.GetEnumerator()
generator.Call(ReflectionHelpers.IEnumerable_GetEnumerator);
// Store the enumerator in a temporary variable.
var enumerator = generator.CreateTemporaryVariable(typeof(IEnumerator<string>));
generator.StoreVariable(enumerator);
var breakTarget = generator.CreateLabel();
var continueTarget = generator.DefineLabelPosition();
#if !XBOX
// Emit debugging information.
if (optimizationInfo.DebugDocument != null)
generator.MarkSequencePoint(optimizationInfo.DebugDocument, this.VariableDebugInfo);
#endif
// if (enumerator.MoveNext() == false)
// goto break-target;
generator.LoadVariable(enumerator);
generator.Call(ReflectionHelpers.IEnumerator_MoveNext);
generator.BranchIfFalse(breakTarget);
// lhs = enumerator.Current;
generator.LoadVariable(enumerator);
generator.Call(ReflectionHelpers.IEnumerator_Current);
this.Variable.GenerateSet(generator, optimizationInfo, PrimitiveType.String, false);
// Emit the body statement(s).
optimizationInfo.PushBreakOrContinueInfo(this.Labels, breakTarget, continueTarget, labelledOnly: false);
this.Body.GenerateCode(generator, optimizationInfo);
optimizationInfo.PopBreakOrContinueInfo();
generator.Branch(continueTarget);
generator.DefineLabelPosition(breakTarget);
// Generate code for the end of the statement.
GenerateEndOfStatement(generator, optimizationInfo, statementLocals);
}
示例9: GenerateStub
/// <summary>
/// Generates a method that does type conversion and calls the bound method.
/// </summary>
/// <param name="generator"> The ILGenerator used to output the body of the method. </param>
/// <param name="argumentCount"> The number of arguments that will be passed to the delegate. </param>
/// <returns> A delegate that does type conversion and calls the method represented by this
/// object. </returns>
protected override void GenerateStub(ILGenerator generator, int argumentCount)
{
// Determine the methods that have the correct number of arguments.
var candidateMethods = new List<BinderMethod>();
foreach (var candidateMethod in this.targetMethods)
{
if (candidateMethod.IsArgumentCountCompatible(argumentCount) == true)
candidateMethods.Add(candidateMethod);
}
// Zero candidates means no overload had the correct number of arguments.
if (candidateMethods.Count == 0)
{
EmitHelpers.EmitThrow(generator, "TypeError", string.Format("No overload for method '{0}' takes {1} arguments", this.Name, argumentCount));
EmitHelpers.EmitDefaultValue(generator, PrimitiveType.Any);
generator.Complete();
return;
}
// Select the method to call at run time.
generator.LoadInt32(candidateMethods.Count);
generator.NewArray(typeof(RuntimeMethodHandle));
for (int i = 0; i < candidateMethods.Count; i ++)
{
generator.Duplicate();
generator.LoadInt32(i);
generator.LoadToken(candidateMethods[i]);
generator.StoreArrayElement(typeof(RuntimeMethodHandle));
}
generator.LoadArgument(0);
generator.LoadArgument(1);
generator.LoadArgument(2);
generator.Call(ReflectionHelpers.BinderUtilities_ResolveOverloads);
var endOfMethod = generator.CreateLabel();
for (int i = 0; i < candidateMethods.Count; i++)
{
// Check if this is the selected method.
ILLabel endOfIf = null;
if (i < candidateMethods.Count - 1)
{
generator.Duplicate();
generator.LoadInt32(i);
endOfIf = generator.CreateLabel();
generator.BranchIfNotEqual(endOfIf);
}
generator.Pop();
var targetMethod = candidateMethods[i];
// Convert the arguments.
foreach (var argument in targetMethod.GenerateArguments(generator, argumentCount))
{
// Load the input parameter value.
switch (argument.Source)
{
case BinderArgumentSource.ScriptEngine:
generator.LoadArgument(0);
break;
case BinderArgumentSource.ThisValue:
generator.LoadArgument(1);
break;
case BinderArgumentSource.InputParameter:
generator.LoadArgument(2);
generator.LoadInt32(argument.InputParameterIndex);
generator.LoadArrayElement(typeof(object));
break;
}
// Convert to the target type.
EmitConversionToType(generator, argument.Type, convertToAddress: argument.Source == BinderArgumentSource.ThisValue);
}
// Call the target method.
targetMethod.GenerateCall(generator);
// Convert the return value.
if (targetMethod.ReturnType == typeof(void))
EmitHelpers.EmitUndefined(generator);
else
EmitConversionToObject(generator, targetMethod.ReturnType);
// Branch to the end of the method if this was the selected method.
if (endOfIf != null)
{
generator.Branch(endOfMethod);
generator.DefineLabelPosition(endOfIf);
}
}
generator.DefineLabelPosition(endOfMethod);
generator.Complete();
}
示例10: EmitConversionToObject
/// <summary>
/// Pops the value on the stack, converts it to an object, then pushes the result onto the
/// stack.
/// </summary>
/// <param name="generator"> The IL generator. </param>
/// <param name="fromType"> The type to convert from. </param>
internal static void EmitConversionToObject(ILGenerator generator, Type fromType)
{
// If the from type is a reference type, check for null.
ILLabel endOfNullCheck = null;
if (fromType.IsValueType == false)
{
var startOfElse = generator.CreateLabel();
endOfNullCheck = generator.CreateLabel();
generator.Duplicate();
generator.BranchIfNotNull(startOfElse);
generator.Pop();
EmitHelpers.EmitNull(generator);
generator.Branch(endOfNullCheck);
generator.DefineLabelPosition(startOfElse);
}
switch (Type.GetTypeCode(fromType))
{
case TypeCode.Boolean:
generator.Box(typeof(bool));
break;
case TypeCode.Byte:
generator.Box(typeof(int));
break;
case TypeCode.Char:
generator.LoadInt32(1);
generator.NewObject(ReflectionHelpers.String_Constructor_Char_Int);
break;
case TypeCode.DBNull:
throw new NotSupportedException("DBNull is not a supported return type.");
case TypeCode.Decimal:
generator.Call(ReflectionHelpers.Decimal_ToDouble);
generator.Box(typeof(double));
break;
case TypeCode.Double:
generator.Box(typeof(double));
break;
case TypeCode.Empty:
throw new NotSupportedException("Empty is not a supported return type.");
case TypeCode.Int16:
generator.Box(typeof(int));
break;
case TypeCode.Int32:
generator.Box(typeof(int));
break;
case TypeCode.Int64:
generator.ConvertToDouble();
generator.Box(typeof(double));
break;
case TypeCode.DateTime:
case TypeCode.Object:
// Check if the type must be wrapped with a ClrInstanceWrapper.
// Note: if the type is a value type it cannot be a primitive or it would
// have been handled elsewhere in the switch.
ILLabel endOfWrapCheck = null;
if (fromType.IsValueType == false)
{
generator.Duplicate();
generator.Call(ReflectionHelpers.TypeUtilities_IsPrimitiveOrObject);
endOfWrapCheck = generator.CreateLabel();
generator.BranchIfTrue(endOfWrapCheck);
}
// The type must be wrapped.
var temp = generator.CreateTemporaryVariable(fromType);
generator.StoreVariable(temp);
generator.LoadArgument(0);
generator.LoadVariable(temp);
if (fromType.IsValueType == true)
generator.Box(fromType);
generator.ReleaseTemporaryVariable(temp);
generator.NewObject(ReflectionHelpers.ClrInstanceWrapper_Constructor);
// End of wrap check.
if (fromType.IsValueType == false)
generator.DefineLabelPosition(endOfWrapCheck);
break;
case TypeCode.SByte:
generator.Box(typeof(int));
break;
case TypeCode.Single:
generator.Box(typeof(double));
break;
case TypeCode.String:
break;
case TypeCode.UInt16:
generator.Box(typeof(int));
break;
case TypeCode.UInt32:
generator.Box(typeof(uint));
break;
//.........这里部分代码省略.........
示例11: EmitConversionToType
/// <summary>
/// Pops the value on the stack, converts it from an object to the given type, then pushes
/// the result onto the stack.
/// </summary>
/// <param name="generator"> The IL generator. </param>
/// <param name="toType"> The type to convert to. </param>
/// <param name="convertToAddress"> <c>true</c> if the value is intended for use as an
/// instance pointer; <c>false</c> otherwise. </param>
internal static void EmitConversionToType(ILGenerator generator, Type toType, bool convertToAddress)
{
// Convert Null.Value to null if the target type is a reference type.
ILLabel endOfNullCheck = null;
if (toType.IsValueType == false)
{
var startOfElse = generator.CreateLabel();
endOfNullCheck = generator.CreateLabel();
generator.Duplicate();
EmitHelpers.EmitNull(generator);
generator.BranchIfNotEqual(startOfElse);
generator.Pop();
generator.LoadNull();
generator.Branch(endOfNullCheck);
generator.DefineLabelPosition(startOfElse);
}
switch (Type.GetTypeCode(toType))
{
case TypeCode.Boolean:
EmitConversion.ToBool(generator, PrimitiveType.Any);
break;
case TypeCode.Byte:
EmitConversion.ToInt32(generator, PrimitiveType.Any);
break;
case TypeCode.Char:
EmitConversion.ToString(generator, PrimitiveType.Any);
generator.Duplicate();
generator.Call(ReflectionHelpers.String_Length);
generator.LoadInt32(1);
var endOfCharCheck = generator.CreateLabel();
generator.BranchIfEqual(endOfCharCheck);
EmitHelpers.EmitThrow(generator, "TypeError", "Cannot convert string to char - the string must be exactly one character long");
generator.DefineLabelPosition(endOfCharCheck);
generator.LoadInt32(0);
generator.Call(ReflectionHelpers.String_GetChars);
break;
case TypeCode.DBNull:
throw new NotSupportedException("DBNull is not a supported parameter type.");
case TypeCode.Decimal:
EmitConversion.ToNumber(generator, PrimitiveType.Any);
generator.NewObject(ReflectionHelpers.Decimal_Constructor_Double);
break;
case TypeCode.Double:
EmitConversion.ToNumber(generator, PrimitiveType.Any);
break;
case TypeCode.Empty:
throw new NotSupportedException("Empty is not a supported return type.");
case TypeCode.Int16:
EmitConversion.ToInt32(generator, PrimitiveType.Any);
break;
case TypeCode.Int32:
EmitConversion.ToInt32(generator, PrimitiveType.Any);
break;
case TypeCode.Int64:
EmitConversion.ToNumber(generator, PrimitiveType.Any);
generator.ConvertToInt64();
break;
case TypeCode.DateTime:
case TypeCode.Object:
// Check if the type must be unwrapped.
generator.Duplicate();
generator.IsInstance(typeof(Jurassic.Library.ClrInstanceWrapper));
var endOfUnwrapCheck = generator.CreateLabel();
generator.BranchIfFalse(endOfUnwrapCheck);
// Unwrap the wrapped instance.
generator.Call(ReflectionHelpers.ClrInstanceWrapper_GetWrappedInstance);
generator.DefineLabelPosition(endOfUnwrapCheck);
// Value types must be unboxed.
if (toType.IsValueType == true)
{
if (convertToAddress == true)
// Unbox.
generator.Unbox(toType);
else
// Unbox and copy to the stack.
generator.UnboxAny(toType);
//// Calling methods on value required the address of the value type, not the value type itself.
//if (argument.Source == BinderArgumentSource.ThisValue && argument.Type.IsValueType == true)
//{
// var temp = generator.CreateTemporaryVariable(argument.Type);
// generator.StoreVariable(temp);
// generator.LoadAddressOfVariable(temp);
// generator.ReleaseTemporaryVariable(temp);
//}
}
//.........这里部分代码省略.........
示例12: GenerateCode
/// <summary>
/// Generates CIL for the expression.
/// </summary>
/// <param name="generator"> The generator to output the CIL to. </param>
/// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
public override void GenerateCode(ILGenerator generator, OptimizationInfo optimizationInfo)
{
// If a return value is not expected, generate only the side-effects.
/*if (optimizationInfo.SuppressReturnValue == true)
{
this.GenerateSideEffects(generator, optimizationInfo);
return;
}*/
// Emit the condition.
var condition = this.GetOperand(0);
condition.GenerateCode(generator, optimizationInfo);
// Convert the condition to a boolean.
EmitConversion.ToBool(generator, condition.ResultType);
// Branch if the condition is false.
var startOfElse = generator.CreateLabel();
generator.BranchIfFalse(startOfElse);
// Calculate the result type.
var outputType = this.ResultType;
// Emit the second operand and convert it to the result type.
var operand2 = this.GetOperand(1);
operand2.GenerateCode(generator, optimizationInfo);
EmitConversion.Convert(generator, operand2.ResultType, outputType);
// Branch to the end.
var end = generator.CreateLabel();
generator.Branch(end);
generator.DefineLabelPosition(startOfElse);
// Emit the third operand and convert it to the result type.
var operand3 = this.GetOperand(2);
operand3.GenerateCode(generator, optimizationInfo);
EmitConversion.Convert(generator, operand3.ResultType, outputType);
// Define the end label.
generator.DefineLabelPosition(end);
}
示例13: GenerateSet
/// <summary>
/// Stores the value on the top of the stack in the reference.
/// </summary>
/// <param name="generator"> The generator to output the CIL to. </param>
/// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
/// <param name="valueType"> The primitive type of the value that is on the top of the stack. </param>
/// <param name="throwIfUnresolvable"> <c>true</c> to throw a ReferenceError exception if
/// the name is unresolvable; <c>false</c> to create a new property instead. </param>
public void GenerateSet(ILGenerator generator, OptimizationInfo optimizationInfo, PrimitiveType valueType, bool throwIfUnresolvable)
{
string propertyName = null;
TypeOfMemberAccess memberAccessType = DetermineTypeOfMemberAccess(optimizationInfo, out propertyName);
if (memberAccessType == TypeOfMemberAccess.ArrayIndex)
{
// Array indexer
// -------------
// xxx = object[index]
// Call the indexer.
EmitConversion.ToAny(generator, valueType);
generator.LoadBoolean(optimizationInfo.StrictMode);
generator.Call(ReflectionHelpers.ObjectInstance_SetPropertyValue_Int);
}
else if (memberAccessType == TypeOfMemberAccess.Static)
{
// Named property modification (e.g. x.property = y)
// -------------------------------------------------
// __object_cacheKey = null;
// __object_property_cachedIndex = 0;
// ...
// if (__object_cacheKey != object.InlineCacheKey)
// object.InlineSetPropertyValue("property", value, strictMode, out __object_property_cachedIndex, out __object_cacheKey)
// else
// object.InlinePropertyValues[__object_property_cachedIndex] = value;
// Convert the value to an object and store it in a temporary variable.
var value = generator.CreateTemporaryVariable(typeof(object));
EmitConversion.ToAny(generator, valueType);
generator.StoreVariable(value);
// TODO: share these variables somehow.
var cacheKey = generator.DeclareVariable(typeof(object));
var cachedIndex = generator.DeclareVariable(typeof(int));
// Store the object into a temp variable.
var objectInstance = generator.DeclareVariable(PrimitiveType.Object);
generator.StoreVariable(objectInstance);
// if (__object_cacheKey != object.InlineCacheKey)
generator.LoadVariable(cacheKey);
generator.LoadVariable(objectInstance);
generator.Call(ReflectionHelpers.ObjectInstance_InlineCacheKey);
var elseClause = generator.CreateLabel();
generator.BranchIfEqual(elseClause);
// xxx = object.InlineSetPropertyValue("property", value, strictMode, out __object_property_cachedIndex, out __object_cacheKey)
generator.LoadVariable(objectInstance);
generator.LoadString(propertyName);
generator.LoadVariable(value);
generator.LoadBoolean(optimizationInfo.StrictMode);
generator.LoadAddressOfVariable(cachedIndex);
generator.LoadAddressOfVariable(cacheKey);
generator.Call(ReflectionHelpers.ObjectInstance_InlineSetPropertyValue);
var endOfIf = generator.CreateLabel();
generator.Branch(endOfIf);
// else
generator.DefineLabelPosition(elseClause);
// object.InlinePropertyValues[__object_property_cachedIndex] = value;
generator.LoadVariable(objectInstance);
generator.Call(ReflectionHelpers.ObjectInstance_InlinePropertyValues);
generator.LoadVariable(cachedIndex);
generator.LoadVariable(value);
generator.StoreArrayElement(typeof(object));
// End of the if statement
generator.DefineLabelPosition(endOfIf);
// The temporary variable is no longer needed.
generator.ReleaseTemporaryVariable(value);
}
else
{
// Dynamic property access
// -----------------------
// xxx = object.Get(x)
// Call the indexer.
EmitConversion.ToAny(generator, valueType);
generator.LoadBoolean(optimizationInfo.StrictMode);
generator.Call(ReflectionHelpers.ObjectInstance_SetPropertyValue_Object);
}
}
示例14: GenerateGet
/// <summary>
/// Pushes the value of the reference onto the stack.
/// </summary>
/// <param name="generator"> The generator to output the CIL to. </param>
/// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
/// <param name="throwIfUnresolvable"> <c>true</c> to throw a ReferenceError exception if
/// the name is unresolvable; <c>false</c> to output <c>null</c> instead. </param>
public void GenerateGet(ILGenerator generator, OptimizationInfo optimizationInfo, bool throwIfUnresolvable)
{
string propertyName = null;
TypeOfMemberAccess memberAccessType = DetermineTypeOfMemberAccess(optimizationInfo, out propertyName);
if (memberAccessType == TypeOfMemberAccess.ArrayIndex)
{
// Array indexer
// -------------
// xxx = object[index]
// Call the indexer.
generator.Call(ReflectionHelpers.ObjectInstance_GetPropertyValue_Int);
}
else if (memberAccessType == TypeOfMemberAccess.Static)
{
// Named property access (e.g. x = y.property)
// -------------------------------------------
// __object_cacheKey = null;
// __object_property_cachedIndex = 0;
// ...
// if (__object_cacheKey != object.InlineCacheKey)
// xxx = object.InlineGetPropertyValue("property", out __object_property_cachedIndex, out __object_cacheKey)
// else
// xxx = object.InlinePropertyValues[__object_property_cachedIndex];
// TODO: share these variables somehow.
var cacheKey = generator.DeclareVariable(typeof(object));
var cachedIndex = generator.DeclareVariable(typeof(int));
// Store the object into a temp variable.
var objectInstance = generator.DeclareVariable(PrimitiveType.Object);
generator.StoreVariable(objectInstance);
// if (__object_cacheKey != object.InlineCacheKey)
generator.LoadVariable(cacheKey);
generator.LoadVariable(objectInstance);
generator.Call(ReflectionHelpers.ObjectInstance_InlineCacheKey);
var elseClause = generator.CreateLabel();
generator.BranchIfEqual(elseClause);
// value = object.InlineGetProperty("property", out __object_property_cachedIndex, out __object_cacheKey)
generator.LoadVariable(objectInstance);
generator.LoadString(propertyName);
generator.LoadAddressOfVariable(cachedIndex);
generator.LoadAddressOfVariable(cacheKey);
generator.Call(ReflectionHelpers.ObjectInstance_InlineGetPropertyValue);
var endOfIf = generator.CreateLabel();
generator.Branch(endOfIf);
// else
generator.DefineLabelPosition(elseClause);
// value = object.InlinePropertyValues[__object_property_cachedIndex];
generator.LoadVariable(objectInstance);
generator.Call(ReflectionHelpers.ObjectInstance_InlinePropertyValues);
generator.LoadVariable(cachedIndex);
generator.LoadArrayElement(typeof(object));
// End of the if statement
generator.DefineLabelPosition(endOfIf);
}
else
{
// Dynamic property access
// -----------------------
// xxx = object.Get(x)
// Call Get(object)
generator.Call(ReflectionHelpers.ObjectInstance_GetPropertyValue_Object);
}
}
示例15: GenerateLogical
/// <summary>
/// Generates CIL for the logical operators.
/// </summary>
/// <param name="generator"> The generator to output the CIL to. </param>
/// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
private void GenerateLogical(ILGenerator generator, OptimizationInfo optimizationInfo)
{
// Get the statically-determined types of the left and right operands.
PrimitiveType leftType = this.Left.ResultType;
PrimitiveType rightType = this.Right.ResultType;
// Load the left-hand side operand.
this.Left.GenerateCode(generator, optimizationInfo);
// Make sure the output type is consistant.
if (leftType != rightType)
{
if (PrimitiveTypeUtilities.IsNumeric(leftType) == true && PrimitiveTypeUtilities.IsNumeric(rightType) == true)
{
EmitConversion.ToNumber(generator, leftType);
leftType = PrimitiveType.Number;
}
else
{
EmitConversion.ToAny(generator, leftType);
leftType = PrimitiveType.Any;
}
}
// Duplicate and convert to a Boolean.
generator.Duplicate();
EmitConversion.ToBool(generator, leftType);
// Stack contains "left, (bool)left"
var endOfIf = generator.CreateLabel();
if (this.OperatorType == OperatorType.LogicalAnd)
generator.BranchIfFalse(endOfIf);
else
generator.BranchIfTrue(endOfIf);
// Stack contains "left". Load the right-hand side operand.
generator.Pop();
this.Right.GenerateCode(generator, optimizationInfo);
// Make sure the output type is consistant.
if (leftType != rightType)
{
if (PrimitiveTypeUtilities.IsNumeric(leftType) == true && PrimitiveTypeUtilities.IsNumeric(rightType) == true)
EmitConversion.ToNumber(generator, rightType);
else
EmitConversion.ToAny(generator, rightType);
}
// Define the label used above.
generator.DefineLabelPosition(endOfIf);
}