本文整理汇总了C#中Jurassic.Compiler.ILGenerator.CastClass方法的典型用法代码示例。如果您正苦于以下问题:C# ILGenerator.CastClass方法的具体用法?C# ILGenerator.CastClass怎么用?C# ILGenerator.CastClass使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Jurassic.Compiler.ILGenerator
的用法示例。
在下文中一共展示了ILGenerator.CastClass方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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)
{
// The value is initially on the top of the stack but is stored in this variable
// at the last possible moment.
ILLocalVariable value = null;
var scope = this.Scope;
ILLocalVariable scopeVariable = null;
var endOfSet = generator.CreateLabel();
do
{
if (scope is DeclarativeScope)
{
// Get information about the variable.
var variable = scope.GetDeclaredVariable(this.Name);
if (variable != null)
{
// The variable was declared in this scope.
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);
if (value == null)
{
// The value to store is on the top of the stack - convert it to the
// storage type of the variable.
EmitConversion.Convert(generator, valueType, variable.Type, optimizationInfo);
}
else
{
// The value to store is in a temporary variable.
generator.LoadVariable(value);
EmitConversion.Convert(generator, PrimitiveType.Any, variable.Type, optimizationInfo);
}
// Store the value in the variable.
generator.StoreVariable(variable.Store);
}
else if (variable.Writable == true)
{
if (value == null)
{
// The value to store is on the top of the stack - convert it to an
// object and store it in a temporary variable.
EmitConversion.Convert(generator, valueType, PrimitiveType.Any, optimizationInfo);
value = generator.CreateTemporaryVariable(typeof(object));
generator.StoreVariable(value);
}
// scope.Values[index] = value
if (scopeVariable == null)
EmitHelpers.LoadScope(generator);
else
generator.LoadVariable(scopeVariable);
generator.CastClass(typeof(DeclarativeScope));
generator.Call(ReflectionHelpers.DeclarativeScope_Values);
generator.LoadInt32(variable.Index);
generator.LoadVariable(value);
generator.StoreArrayElement(typeof(object));
}
else
{
// The variable exists, but is read-only.
// Pop the value off the stack (if it is still there).
if (value == null)
generator.Pop();
}
// 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)
{
if (value == null)
{
// The value to store is on the top of the stack - convert it to an
// object and store it in a temporary variable.
EmitConversion.Convert(generator, valueType, PrimitiveType.Any, optimizationInfo);
value = generator.CreateTemporaryVariable(typeof(object));
generator.StoreVariable(value);
}
//.........这里部分代码省略.........
示例2: 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);
}
示例3: 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)
{
//.........这里部分代码省略.........
示例4: GenerateDelete
/// <summary>
/// Deletes the reference and pushes <c>true</c> if the delete succeeded, or <c>false</c>
/// if the delete failed.
/// </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 void GenerateDelete(ILGenerator generator, OptimizationInfo optimizationInfo)
{
// Deleting a variable is not allowed in strict mode.
if (optimizationInfo.StrictMode == true)
throw new JavaScriptException(optimizationInfo.Engine, ErrorType.SyntaxError, string.Format("Cannot delete {0} because deleting a variable or argument is not allowed in strict mode", this.Name), optimizationInfo.SourceSpan.StartLine, optimizationInfo.Source.Path, optimizationInfo.FunctionName);
var endOfDelete = generator.CreateLabel();
var scope = this.Scope;
ILLocalVariable scopeVariable = generator.CreateTemporaryVariable(typeof(Scope));
EmitHelpers.LoadScope(generator);
generator.StoreVariable(scopeVariable);
do
{
if (scope is DeclarativeScope)
{
var variable = scope.GetDeclaredVariable(this.Name);
if (variable != null)
{
// The variable is known at compile-time.
if (variable.Deletable == false)
{
// The variable cannot be deleted - return false.
generator.LoadBoolean(false);
}
else
{
// The variable can be deleted (it was declared inside an eval()).
// Delete the variable.
generator.LoadVariable(scopeVariable);
generator.CastClass(typeof(DeclarativeScope));
generator.LoadString(this.Name);
generator.Call(ReflectionHelpers.Scope_Delete);
}
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) {
generator.LoadVariable(scopeVariable);
generator.CastClass(typeof(DeclarativeScope));
generator.LoadString(this.Name);
generator.Call(ReflectionHelpers.Scope_HasValue);
var hasValueClause = generator.CreateLabel();
generator.BranchIfFalse(hasValueClause);
// If the variable does exist, return true.
generator.LoadVariable(scopeVariable);
generator.CastClass(typeof(DeclarativeScope));
generator.LoadString(this.Name);
generator.Call(ReflectionHelpers.Scope_Delete);
generator.Branch(endOfDelete);
// }
generator.DefineLabelPosition(hasValueClause);
}
}
}
else
{
// Check if the property exists by calling scope.ScopeObject.HasProperty(propertyName)
generator.LoadVariable(scopeVariable);
generator.CastClass(typeof(ObjectScope));
generator.Call(ReflectionHelpers.ObjectScope_ScopeObject);
generator.Duplicate();
generator.LoadString(this.Name);
generator.Call(ReflectionHelpers.ObjectInstance_HasProperty);
// Jump past the delete if the property doesn't exist.
var endOfExistsCheck = generator.CreateLabel();
generator.BranchIfFalse(endOfExistsCheck);
// Call scope.ScopeObject.Delete(key, false)
generator.LoadString(this.Name);
generator.LoadBoolean(false);
generator.Call(ReflectionHelpers.ObjectInstance_Delete);
generator.Branch(endOfDelete);
generator.DefineLabelPosition(endOfExistsCheck);
generator.Pop();
// If the name is not defined, return true.
if (scope.ParentScope == null)
{
generator.LoadBoolean(true);
}
}
// Try the parent scope.
if (scope.ParentScope != null && scope.ExistsAtRuntime == true)
{
//.........这里部分代码省略.........
示例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: GenerateDeclarations
/// <summary>
/// Generates code that initializes the variable and function declarations.
/// </summary>
/// <param name="generator"> The generator to output the CIL to. </param>
/// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
internal virtual void GenerateDeclarations(ILGenerator generator, OptimizationInfo optimizationInfo)
{
// Initialize the declared variables and functions.
foreach (var variable in this.variables.Values)
{
// When a scope is reused, i.e. with an eval(), do not reinitialize the variables.
if (variable.Initialized == true)
continue;
if (variable.ValueAtTopOfScope != null)
{
// Emit the initialization code.
if (this is ObjectScope)
{
// Determine the property attributes.
var attributes = Library.PropertyAttributes.Enumerable;
if (variable.Writable == true)
attributes |= Library.PropertyAttributes.Writable;
if (variable.Deletable == true)
attributes |= Library.PropertyAttributes.Configurable;
// bool DefineProperty(string propertyName, PropertyDescriptor descriptor, bool throwOnError)
EmitHelpers.LoadScope(generator);
generator.CastClass(typeof(ObjectScope));
generator.Call(ReflectionHelpers.ObjectScope_ScopeObject);
generator.LoadString(variable.Name);
variable.ValueAtTopOfScope.GenerateCode(generator, optimizationInfo);
EmitConversion.Convert(generator, variable.ValueAtTopOfScope.ResultType, PrimitiveType.Any, optimizationInfo);
generator.LoadInt32((int)attributes);
generator.NewObject(ReflectionHelpers.PropertyDescriptor_Constructor2);
generator.LoadBoolean(false);
generator.Call(ReflectionHelpers.ObjectInstance_DefineProperty);
generator.Pop();
}
else
{
variable.ValueAtTopOfScope.GenerateCode(generator, optimizationInfo);
var name = new NameExpression(this, variable.Name);
name.GenerateSet(generator, optimizationInfo, variable.ValueAtTopOfScope.ResultType, false);
}
// Mark the variable as having been initialized.
variable.Initialized = true;
}
}
}
示例7: 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)
{
// Here is what we are going to generate.
//private static object SampleBinder(ScriptEngine engine, object thisObject, object[] arguments)
//{
// // Target function signature: int (bool, int, string, object).
// bool param1;
// int param2;
// string param3;
// object param4;
// param1 = arguments[0] != 0;
// param2 = TypeConverter.ToInt32(arguments[1]);
// param3 = TypeConverter.ToString(arguments[2]);
// param4 = Undefined.Value;
// return thisObject.targetMethod(param1, param2, param3, param4);
//}
// Find the target method.
var binderMethod = this.buckets[Math.Min(argumentCount, this.buckets.Length - 1)];
// Constrain the number of apparent arguments to within the required bounds.
int minArgumentCount = binderMethod.RequiredParameterCount;
int maxArgumentCount = binderMethod.RequiredParameterCount + binderMethod.OptionalParameterCount;
if (binderMethod.HasParamArray == true)
maxArgumentCount = int.MaxValue;
foreach (var argument in binderMethod.GenerateArguments(generator, Math.Min(Math.Max(argumentCount, minArgumentCount), maxArgumentCount)))
{
switch (argument.Source)
{
case BinderArgumentSource.ScriptEngine:
// Load the "engine" parameter passed by the client.
generator.LoadArgument(0);
break;
case BinderArgumentSource.ThisValue:
// Load the "this" parameter passed by the client.
generator.LoadArgument(1);
bool inheritsFromObjectInstance = typeof(ObjectInstance).IsAssignableFrom(argument.Type);
if (argument.Type.IsClass == true && inheritsFromObjectInstance == false &&
argument.Type != typeof(string) && argument.Type != typeof(object))
{
// If the "this" object is an unsupported class, pass it through unmodified.
generator.CastClass(argument.Type);
}
else
{
if (argument.Type != typeof(object))
{
// If the target "this" object type is not of type object, throw an error if
// the value is undefined or null.
generator.Duplicate();
var temp = generator.CreateTemporaryVariable(typeof(object));
generator.StoreVariable(temp);
generator.LoadArgument(0);
generator.LoadVariable(temp);
generator.LoadString(binderMethod.Name);
generator.Call(ReflectionHelpers.TypeUtilities_VerifyThisObject);
generator.ReleaseTemporaryVariable(temp);
}
// Convert to the target type.
EmitTypeConversion(generator, typeof(object), argument.Type);
if (argument.Type != typeof(ObjectInstance) && inheritsFromObjectInstance == true)
{
// EmitConversionToObjectInstance can emit null if the toType is derived from ObjectInstance.
// Therefore, if the value emitted is null it means that the "thisObject" is a type derived
// from ObjectInstance (e.g. FunctionInstance) and the value provided is a different type
// (e.g. ArrayInstance). In this case, throw an exception explaining that the function is
// not generic.
var endOfThrowLabel = generator.CreateLabel();
generator.Duplicate();
generator.BranchIfNotNull(endOfThrowLabel);
generator.LoadArgument(0);
EmitHelpers.EmitThrow(generator, "TypeError", string.Format("The method '{0}' is not generic", binderMethod.Name));
generator.DefineLabelPosition(endOfThrowLabel);
}
}
break;
case BinderArgumentSource.InputParameter:
if (argument.InputParameterIndex < argumentCount)
{
// Load the argument onto the stack.
generator.LoadArgument(2);
generator.LoadInt32(argument.InputParameterIndex);
generator.LoadArrayElement(typeof(object));
// Get some flags that apply to the parameter.
var parameterFlags = JSParameterFlags.None;
var parameterAttribute = argument.GetCustomAttribute<JSParameterAttribute>();
//.........这里部分代码省略.........