本文整理汇总了C#中Jurassic.Compiler.ILGenerator.LoadInt32方法的典型用法代码示例。如果您正苦于以下问题:C# ILGenerator.LoadInt32方法的具体用法?C# ILGenerator.LoadInt32怎么用?C# ILGenerator.LoadInt32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Jurassic.Compiler.ILGenerator
的用法示例。
在下文中一共展示了ILGenerator.LoadInt32方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EmitThrow
/// <summary>
/// Emits a JavaScriptException.
/// </summary>
/// <param name="generator"> The IL generator. </param>
/// <param name="type"> The type of error to generate, e.g. Error, RangeError, etc. </param>
/// <param name="message"> The error message. </param>
/// <param name="path"> The path of the javascript source file that is currently executing. </param>
/// <param name="function"> The name of the currently executing function. </param>
/// <param name="line"> The line number of the statement that is currently executing. </param>
public static void EmitThrow(ILGenerator generator, ErrorType type, string message, string path, string function, int line)
{
EmitHelpers.LoadScriptEngine(generator);
generator.LoadInt32((int)type);
generator.LoadString(message);
generator.LoadInt32(line);
generator.LoadStringOrNull(path);
generator.LoadStringOrNull(function);
generator.NewObject(ReflectionHelpers.JavaScriptException_Constructor_Error);
generator.Throw();
}
示例2: EmitValue
/// <summary>
/// Emits the given value. Only possible for certain types.
/// </summary>
/// <param name="generator"> The IL generator. </param>
/// <param name="value"> The value to emit. </param>
public static void EmitValue(ILGenerator generator, object value)
{
if (value == null)
generator.LoadNull();
else
{
switch (Type.GetTypeCode(value.GetType()))
{
case TypeCode.Boolean:
generator.LoadBoolean((bool)value);
break;
case TypeCode.Byte:
generator.LoadInt32((byte)value);
break;
case TypeCode.Char:
generator.LoadInt32((char)value);
break;
case TypeCode.Double:
generator.LoadDouble((double)value);
break;
case TypeCode.Int16:
generator.LoadInt32((short)value);
break;
case TypeCode.Int32:
generator.LoadInt32((int)value);
break;
case TypeCode.Int64:
generator.LoadInt64((long)value);
break;
case TypeCode.SByte:
generator.LoadInt32((sbyte)value);
break;
case TypeCode.Single:
generator.LoadDouble((float)value);
break;
case TypeCode.String:
generator.LoadString((string)value);
break;
case TypeCode.UInt16:
generator.LoadInt32((ushort)value);
break;
case TypeCode.UInt32:
generator.LoadInt32((uint)value);
break;
case TypeCode.UInt64:
generator.LoadInt64((ulong)value);
break;
case TypeCode.Object:
case TypeCode.Empty:
case TypeCode.DateTime:
case TypeCode.DBNull:
case TypeCode.Decimal:
throw new NotImplementedException(string.Format("Cannot emit the value '{0}'", value));
}
}
}
示例3: 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();
GenerateStartOfStatement(generator, optimizationInfo, statementLocals);
// Emit code to throw the given value.
this.Value.GenerateCode(generator, optimizationInfo);
EmitConversion.ToAny(generator, this.Value.ResultType);
generator.LoadInt32(0);
generator.LoadNull();
generator.NewObject(ReflectionHelpers.JavaScriptException_Constructor_Object);
generator.Throw();
// Generate code for the end of the statement.
GenerateEndOfStatement(generator, optimizationInfo, statementLocals);
}
示例4: 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)
{
// This code is only used for untagged template literals.
// Tagged template literals are handled by FunctionCallExpression.
// Load the values array onto the stack.
generator.LoadInt32(this.Strings.Count + this.Values.Count);
generator.NewArray(typeof(string));
for (int i = 0; i < this.Strings.Count; i++)
{
// Operands for StoreArrayElement() are: an array (string[]), index (int), value (string).
// Store the string.
generator.Duplicate();
generator.LoadInt32(i * 2);
generator.LoadString(this.Strings[i]);
generator.StoreArrayElement(typeof(string));
if (i == this.Strings.Count - 1)
break;
// Store the value.
generator.Duplicate();
generator.LoadInt32(i * 2 + 1);
this.Values[i].GenerateCode(generator, optimizationInfo);
EmitConversion.ToString(generator, this.Values[i].ResultType);
generator.StoreArrayElement(typeof(string));
}
// Call String.Concat(string[])
generator.CallStatic(ReflectionHelpers.String_Concat);
}
示例5: 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>();
//.........这里部分代码省略.........
示例6: GenerateCode
//.........这里部分代码省略.........
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;
// Check if an array element exists.
EmitHelpers.LoadArgumentsArray(generator);
generator.LoadArrayLength();
generator.LoadInt32(i);
generator.BranchIfLessThanOrEqual(endOfArguments);
// Store the array element in the scope.
EmitHelpers.LoadArgumentsArray(generator);
generator.LoadInt32(i);
generator.LoadArrayElement(typeof(object));
var argument = new NameExpression(this.InitialScope, this.ArgumentNames[i]);
argument.GenerateSet(generator, optimizationInfo, PrimitiveType.Any, false);
}
generator.DefineLabelPosition(endOfArguments);
}
// Initialize any declarations.
this.InitialScope.GenerateDeclarations(generator, optimizationInfo);
//EmitHelpers.LoadScope(generator);
//EmitConversion.ToObject(generator, PrimitiveType.Any);
//generator.Pop();
// Generate code for the body of the function.
this.AbstractSyntaxTree.GenerateCode(generator, optimizationInfo);
// Define the return target - this is where the return statement jumps to.
// ReturnTarget can be null if there were no return statements.
if (optimizationInfo.ReturnTarget != null)
generator.DefineLabelPosition(optimizationInfo.ReturnTarget);
// Load the return value. If the variable is null, there were no return statements.
if (optimizationInfo.ReturnVariable != null)
// Return the value stored in the variable. Will be null if execution hits the end
// of the function without encountering any return statements.
generator.LoadVariable(optimizationInfo.ReturnVariable);
else
// There were no return statements - return null.
generator.LoadNull();
}
示例7: 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);
}
//.........这里部分代码省略.........
示例8: 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)
{
// Generate a new method.
this.context.GenerateCode();
// Add the generated method to the nested function list.
if (optimizationInfo.NestedFunctions == null)
optimizationInfo.NestedFunctions = new List<GeneratedMethod>();
optimizationInfo.NestedFunctions.Add(this.context.GeneratedMethod);
// Add all the nested methods to the parent list.
if (this.context.GeneratedMethod.Dependencies != null)
{
foreach (var nestedFunctionExpression in this.context.GeneratedMethod.Dependencies)
optimizationInfo.NestedFunctions.Add(nestedFunctionExpression);
}
// Store the generated method in the cache.
long generatedMethodID = GeneratedMethod.Save(this.context.GeneratedMethod);
// Create a UserDefinedFunction.
// prototype
EmitHelpers.LoadScriptEngine(generator);
generator.Call(ReflectionHelpers.ScriptEngine_Function);
generator.Call(ReflectionHelpers.FunctionInstance_InstancePrototype);
// name
generator.LoadString(this.FunctionName);
// argumentNames
generator.LoadInt32(this.ArgumentNames.Count);
generator.NewArray(typeof(string));
for (int i = 0; i < this.ArgumentNames.Count; i++)
{
generator.Duplicate();
generator.LoadInt32(i);
generator.LoadString(this.ArgumentNames[i]);
generator.StoreArrayElement(typeof(string));
}
// scope
EmitHelpers.LoadScope(generator);
// bodyText
generator.LoadString(this.BodyText);
// body
generator.LoadInt64(generatedMethodID);
generator.Call(ReflectionHelpers.GeneratedMethod_Load);
// strictMode
generator.LoadBoolean(this.context.StrictMode);
// new UserDefinedFunction(ObjectInstance prototype, string name, IList<string> argumentNames, DeclarativeScope scope, Func<Scope, object, object[], object> body, bool strictMode)
generator.NewObject(ReflectionHelpers.UserDefinedFunction_Constructor);
}
示例9: 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;
}
}
}
示例10: ToBool
/// <summary>
/// Pops the value on the stack, converts it to a boolean, then pushes the boolean result
/// onto the stack.
/// </summary>
/// <param name="generator"> The IL generator. </param>
/// <param name="fromType"> The type to convert from. </param>
public static void ToBool(ILGenerator generator, PrimitiveType fromType)
{
// Check that a conversion is actually necessary.
if (fromType == PrimitiveType.Bool)
return;
switch (fromType)
{
case PrimitiveType.Undefined:
case PrimitiveType.Null:
// Converting from undefined or null produces false.
generator.Pop();
generator.LoadInt32(0);
break;
case PrimitiveType.Int32:
case PrimitiveType.UInt32:
// Converting from an integer produces true if the integer is non-zero.
generator.LoadInt32(0);
generator.CompareGreaterThanUnsigned();
break;
case PrimitiveType.Number:
// Converting from a number produces true if the number is non-zero and not NaN.
var temp = generator.CreateTemporaryVariable(fromType);
generator.StoreVariable(temp);
// input != 0
generator.LoadVariable(temp);
generator.LoadDouble(0.0);
generator.CompareEqual();
generator.LoadInt32(0);
generator.CompareEqual();
// input == input
generator.LoadVariable(temp);
generator.Duplicate();
generator.CompareEqual();
// &&
generator.CompareEqual();
// The temporary variable is no longer needed.
generator.ReleaseTemporaryVariable(temp);
break;
case PrimitiveType.String:
// Converting from a string produces true if the string is not empty.
generator.Call(ReflectionHelpers.String_Length);
generator.LoadInt32(0);
generator.CompareGreaterThan();
break;
case PrimitiveType.ConcatenatedString:
// Converting from a string produces true if the string is not empty.
generator.Call(ReflectionHelpers.ConcatenatedString_Length);
generator.LoadInt32(0);
generator.CompareGreaterThan();
break;
case PrimitiveType.Any:
case PrimitiveType.Object:
// Otherwise, fall back to calling TypeConverter.ToBoolean()
generator.Call(ReflectionHelpers.TypeConverter_ToBoolean);
break;
default:
throw new NotImplementedException(string.Format("Unsupported primitive type: {0}", fromType));
}
}
示例11: GenerateIn
/// <summary>
/// Generates CIL for the in operator.
/// </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 GenerateIn(ILGenerator generator, OptimizationInfo optimizationInfo)
{
// Emit the left-hand side expression and convert it to a string.
this.Left.GenerateCode(generator, optimizationInfo);
EmitConversion.ToString(generator, this.Left.ResultType);
// Store the left-hand side expression in a temporary variable.
var temp = generator.CreateTemporaryVariable(typeof(string));
generator.StoreVariable(temp);
// Emit the right-hand side expression.
this.Right.GenerateCode(generator, optimizationInfo);
EmitConversion.ToAny(generator, this.Right.ResultType);
// Check the right-hand side is a javascript object - if not, throw an exception.
generator.IsInstance(typeof(Library.ObjectInstance));
generator.Duplicate();
var endOfTypeCheck = generator.CreateLabel();
generator.BranchIfNotNull(endOfTypeCheck);
// Throw an nicely formatted exception.
var rightValue = generator.CreateTemporaryVariable(typeof(object));
generator.StoreVariable(rightValue);
EmitHelpers.LoadScriptEngine(generator);
generator.LoadString("TypeError");
generator.LoadString("The in operator expected an object, but found '{0}' instead");
generator.LoadInt32(1);
generator.NewArray(typeof(object));
generator.Duplicate();
generator.LoadInt32(0);
generator.LoadVariable(rightValue);
generator.Call(ReflectionHelpers.TypeUtilities_TypeOf);
generator.StoreArrayElement(typeof(object));
generator.Call(ReflectionHelpers.String_Format);
generator.LoadInt32(optimizationInfo.SourceSpan.StartLine);
generator.LoadStringOrNull(optimizationInfo.Source.Path);
generator.LoadStringOrNull(optimizationInfo.FunctionName);
generator.NewObject(ReflectionHelpers.JavaScriptException_Constructor_Error);
generator.Throw();
generator.DefineLabelPosition(endOfTypeCheck);
generator.ReleaseTemporaryVariable(rightValue);
// Load the left-hand side expression from the temporary variable.
generator.LoadVariable(temp);
// Call ObjectInstance.HasProperty(object)
generator.Call(ReflectionHelpers.ObjectInstance_HasProperty);
// Allow the temporary variable to be reused.
generator.ReleaseTemporaryVariable(temp);
}
示例12: GenerateEval
/// <summary>
/// Generates CIL for a call to eval().
/// </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 GenerateEval(ILGenerator generator, OptimizationInfo optimizationInfo)
{
// engine
EmitHelpers.LoadScriptEngine(generator);
// code
if (this.OperandCount < 2)
{
// No arguments were supplied.
generator.LoadNull();
}
else
{
// Take the first argument and convert it to a string.
GenerateArgumentsArray(generator, optimizationInfo);
generator.LoadInt32(0);
generator.LoadArrayElement(typeof(object));
}
// scope
EmitHelpers.LoadScope(generator);
// thisObject
EmitHelpers.LoadThis(generator);
// strictMode
generator.LoadBoolean(optimizationInfo.StrictMode);
// Call Global.Eval(engine, code, scope, thisValue, strictMode)
generator.Call(ReflectionHelpers.Global_Eval);
}
示例13: 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)
{
// Check if this is a direct call to eval().
if (this.Target is NameExpression && ((NameExpression)this.Target).Name == "eval")
{
GenerateEval(generator, optimizationInfo);
return;
}
// Emit the function instance first.
ILLocalVariable targetBase = null;
if (this.Target is MemberAccessExpression)
{
// The function is a member access expression (e.g. "Math.cos()").
// Evaluate the left part of the member access expression.
var baseExpression = ((MemberAccessExpression)this.Target).Base;
baseExpression.GenerateCode(generator, optimizationInfo);
EmitConversion.ToAny(generator, baseExpression.ResultType);
targetBase = generator.CreateTemporaryVariable(typeof(object));
generator.StoreVariable(targetBase);
// Evaluate the right part of the member access expression.
var memberAccessExpression = new MemberAccessExpression(((MemberAccessExpression)this.Target).Operator);
memberAccessExpression.Push(new TemporaryVariableExpression(targetBase));
memberAccessExpression.Push(((MemberAccessExpression)this.Target).GetOperand(1));
memberAccessExpression.GenerateCode(generator, optimizationInfo);
EmitConversion.ToAny(generator, this.Target.ResultType);
}
else
{
// Something else (e.g. "eval()").
this.Target.GenerateCode(generator, optimizationInfo);
EmitConversion.ToAny(generator, this.Target.ResultType);
}
// Check the object really is a function - if not, throw an exception.
generator.IsInstance(typeof(Library.FunctionInstance));
generator.Duplicate();
var endOfTypeCheck = generator.CreateLabel();
generator.BranchIfNotNull(endOfTypeCheck);
// Throw an nicely formatted exception.
generator.Pop();
EmitHelpers.EmitThrow(generator, "TypeError", string.Format("'{0}' is not a function", this.Target.ToString()));
generator.DefineLabelPosition(endOfTypeCheck);
// Pass in the path, function name and line.
generator.LoadStringOrNull(optimizationInfo.Source.Path);
generator.LoadStringOrNull(optimizationInfo.FunctionName);
generator.LoadInt32(optimizationInfo.SourceSpan.StartLine);
// Generate code to produce the "this" value. There are three cases.
if (this.Target is NameExpression)
{
// 1. The function is a name expression (e.g. "parseInt()").
// In this case this = scope.ImplicitThisValue, if there is one, otherwise undefined.
((NameExpression)this.Target).GenerateThis(generator);
}
else if (this.Target is MemberAccessExpression)
{
// 2. The function is a member access expression (e.g. "Math.cos()").
// In this case this = Math.
//var baseExpression = ((MemberAccessExpression)this.Target).Base;
//baseExpression.GenerateCode(generator, optimizationInfo);
//EmitConversion.ToAny(generator, baseExpression.ResultType);
generator.LoadVariable(targetBase);
}
else
{
// 3. Neither of the above (e.g. "(function() { return 5 })()")
// In this case this = undefined.
EmitHelpers.EmitUndefined(generator);
}
// Emit an array containing the function arguments.
GenerateArgumentsArray(generator, optimizationInfo);
// Call FunctionInstance.CallLateBound(thisValue, argumentValues)
generator.Call(ReflectionHelpers.FunctionInstance_CallWithStackTrace);
// Allow reuse of the temporary variable.
if (targetBase != null)
generator.ReleaseTemporaryVariable(targetBase);
}
示例14: GenerateArgumentsArray
/// <summary>
/// Generates an array containing the argument values.
/// </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 void GenerateArgumentsArray(ILGenerator generator, OptimizationInfo optimizationInfo)
{
// Emit the arguments. The arguments operand can be non-existant, a single expression,
// or a comma-delimited list.
if (this.OperandCount < 2)
{
// No parameters passed. Create an empty array.
generator.LoadInt32(0);
generator.NewArray(typeof(object));
}
else
{
// One or more arguments.
IList<Expression> arguments;
var argumentsOperand = this.GetRawOperand(1);
if (argumentsOperand is ListExpression)
{
// Multiple parameters were passed to the function.
arguments = ((ListExpression)argumentsOperand).Items;
}
else
{
// A single parameter was passed to the function.
arguments = new List<Expression>(1) { argumentsOperand };
}
// Generate an array containing the value of each argument.
generator.LoadInt32(arguments.Count);
generator.NewArray(typeof(object));
for (int i = 0; i < arguments.Count; i++)
{
generator.Duplicate();
generator.LoadInt32(i);
arguments[i].GenerateCode(generator, optimizationInfo);
EmitConversion.ToAny(generator, arguments[i].ResultType);
generator.StoreArrayElement(typeof(object));
}
}
}
示例15: GenerateTemplateArgumentsArray
/// <summary>
/// Generates an array containing the argument values for a tagged template literal.
/// </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="templateLiteral"> The template literal expression containing the parameter
/// values. </param>
internal void GenerateTemplateArgumentsArray(ILGenerator generator, OptimizationInfo optimizationInfo, TemplateLiteralExpression templateLiteral)
{
// Generate an array containing the value of each argument.
generator.LoadInt32(templateLiteral.Values.Count + 1);
generator.NewArray(typeof(object));
// Load the first parameter.
generator.Duplicate();
generator.LoadInt32(0);
// The first parameter to the tag function is an array of strings.
var stringsExpression = new List<Expression>(templateLiteral.Strings.Count);
foreach (var templateString in templateLiteral.Strings)
{
stringsExpression.Add(new LiteralExpression(templateString));
}
new LiteralExpression(stringsExpression).GenerateCode(generator, optimizationInfo);
generator.Duplicate();
// Now we need the name of the property.
generator.LoadString("raw");
// Now generate an array of raw strings.
var rawStringsExpression = new List<Expression>(templateLiteral.RawStrings.Count);
foreach (var rawString in templateLiteral.RawStrings)
{
rawStringsExpression.Add(new LiteralExpression(rawString));
}
new LiteralExpression(rawStringsExpression).GenerateCode(generator, optimizationInfo);
// Freeze array by calling ObjectInstance Freeze(ObjectInstance).
generator.CallStatic(ReflectionHelpers.ObjectConstructor_Freeze);
// Now store the raw strings as a property of the base strings array.
generator.LoadBoolean(optimizationInfo.StrictMode);
generator.Call(ReflectionHelpers.ObjectInstance_SetPropertyValue_Object);
// Freeze array by calling ObjectInstance Freeze(ObjectInstance).
generator.CallStatic(ReflectionHelpers.ObjectConstructor_Freeze);
// Store in the array.
generator.StoreArrayElement(typeof(object));
// Values are passed as subsequent parameters.
for (int i = 0; i < templateLiteral.Values.Count; i++)
{
generator.Duplicate();
generator.LoadInt32(i + 1);
templateLiteral.Values[i].GenerateCode(generator, optimizationInfo);
EmitConversion.ToAny(generator, templateLiteral.Values[i].ResultType);
generator.StoreArrayElement(typeof(object));
}
}