/// <summary> /// Creates a delegate that does type conversion and calls the bound method. /// </summary> /// <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> /// <remarks> No caching of the result occurs. </remarks> private BinderDelegate CreateDelegateCore(int argumentCount) { // Create a new dynamic method. System.Reflection.Emit.DynamicMethod dm; ILGenerator generator; if (ScriptEngine.LowPrivilegeEnvironment == false) { // Full trust only - skips visibility checks. dm = new System.Reflection.Emit.DynamicMethod( string.Format("binder_for_{0}", this.FullName), // Name of the generated method. typeof(object), // Return type of the generated method. new[] { typeof(ScriptEngine), typeof(object), typeof(object[]) }, // Parameter types of the generated method. typeof(JSBinder), // Owner type. true); // Skips visibility checks. generator = new DynamicILGenerator(dm); } else { // Partial trust / silverlight. dm = new System.Reflection.Emit.DynamicMethod( string.Format("binder_for_{0}", this.FullName), // Name of the generated method. typeof(object), // Return type of the generated method. new[] { typeof(ScriptEngine), typeof(object), typeof(object[]) }); // Parameter types of the generated method. generator = new ReflectionEmitILGenerator(dm.GetILGenerator()); } // Generate the body of the method. GenerateStub(generator, argumentCount); // Convert the DynamicMethod to a delegate. return((BinderDelegate)dm.CreateDelegate(typeof(BinderDelegate))); }
/// <summary> /// Generates IL for the script. /// </summary> public void GenerateCode() { // Generate the abstract syntax tree if it hasn't already been generated. if (this.AbstractSyntaxTree == null) { Parse(); Optimize(); } // Initialize global code-gen information. var optimizationInfo = new OptimizationInfo(this.Engine) { AbstractSyntaxTree = this.AbstractSyntaxTree, StrictMode = this.StrictMode, MethodOptimizationHints = this.MethodOptimizationHints, FunctionName = this.GetStackName(), Source = this.Source }; ILGenerator generator; if (this.Options.EnableDebugging == false) { // DynamicMethod requires full trust because of generator.LoadMethodPointer in the // FunctionExpression class. // Create a new dynamic method. System.Reflection.Emit.DynamicMethod dynamicMethod; if (ScriptEngine.LowPrivilegeEnvironment == false) { // High privilege path. dynamicMethod = new System.Reflection.Emit.DynamicMethod( GetMethodName(), // Name of the generated method. typeof(object), // Return type of the generated method. GetParameterTypes(), // Parameter types of the generated method. typeof(MethodGenerator), // Owner type. true); // Skip visibility checks. // TODO: Figure out why long methods give BadImageFormatException in .NET 3.5 when generated using DynamicILInfo. if (Environment.Version.Major >= 4) { generator = new DynamicILGenerator(dynamicMethod); } else { generator = new ReflectionEmitILGenerator(dynamicMethod.GetILGenerator()); } } else { // Low privilege path. dynamicMethod = new System.Reflection.Emit.DynamicMethod( GetMethodName(), // Name of the generated method. typeof(object), // Return type of the generated method. GetParameterTypes()); // Parameter types of the generated method. generator = new ReflectionEmitILGenerator(dynamicMethod.GetILGenerator()); } if (this.Engine.EnableILAnalysis) { // Replace the generator with one that logs. generator = new LoggingILGenerator(generator); } // Initialization code will appear to come from line 1. optimizationInfo.MarkSequencePoint(generator, new SourceCodeSpan(1, 1, 1, 1)); // Generate the IL. GenerateCode(generator, optimizationInfo); generator.Complete(); // Create a delegate from the method. this.GeneratedMethod = new GeneratedMethod(dynamicMethod.CreateDelegate(GetDelegate()), optimizationInfo.NestedFunctions); } else { #if WINDOWS_PHONE throw new NotImplementedException(); #else // Debugging or low trust path. ScriptEngine.ReflectionEmitModuleInfo reflectionEmitInfo = this.Engine.ReflectionEmitInfo; if (reflectionEmitInfo == null) { reflectionEmitInfo = new ScriptEngine.ReflectionEmitModuleInfo { AssemblyBuilder = System.Threading.Thread.GetDomain().DefineDynamicAssembly( new System.Reflection.AssemblyName("Jurassic Dynamic Assembly"), System.Reflection.Emit.AssemblyBuilderAccess.Run) }; // Create a dynamic assembly and module. // Mark the assembly as debuggable. This must be done before the module is created. var debuggableAttributeConstructor = typeof(System.Diagnostics.DebuggableAttribute).GetConstructor( new[] { typeof(System.Diagnostics.DebuggableAttribute.DebuggingModes) }); if (debuggableAttributeConstructor != null) { reflectionEmitInfo.AssemblyBuilder.SetCustomAttribute( new System.Reflection.Emit.CustomAttributeBuilder(debuggableAttributeConstructor, new object[] { System.Diagnostics.DebuggableAttribute.DebuggingModes.DisableOptimizations | System.Diagnostics.DebuggableAttribute.DebuggingModes.Default })); } // Create a dynamic module. reflectionEmitInfo.ModuleBuilder = reflectionEmitInfo.AssemblyBuilder.DefineDynamicModule("Module", this.Options.EnableDebugging); this.Engine.ReflectionEmitInfo = reflectionEmitInfo; } // Create a new type to hold our method. var typeBuilder = reflectionEmitInfo.ModuleBuilder.DefineType("JavaScriptClass" + reflectionEmitInfo.TypeCount.ToString(CultureInfo.InvariantCulture), System.Reflection.TypeAttributes.Public | System.Reflection.TypeAttributes.Class); reflectionEmitInfo.TypeCount++; // Create a method. var methodBuilder = typeBuilder.DefineMethod(this.GetMethodName(), System.Reflection.MethodAttributes.HideBySig | System.Reflection.MethodAttributes.Static | System.Reflection.MethodAttributes.Public, typeof(object), GetParameterTypes()); // Generate the IL for the method. generator = new ReflectionEmitILGenerator(methodBuilder.GetILGenerator()); if (this.Engine.EnableILAnalysis) { // Replace the generator with one that logs. generator = new LoggingILGenerator(generator); } if (this.Source.Path != null && this.Options.EnableDebugging) { // Initialize the debugging information. optimizationInfo.DebugDocument = reflectionEmitInfo.ModuleBuilder.DefineDocument(this.Source.Path, COMHelpers.LanguageType, COMHelpers.LanguageVendor, COMHelpers.DocumentType); methodBuilder.DefineParameter(1, System.Reflection.ParameterAttributes.None, "scriptEngine"); methodBuilder.DefineParameter(2, System.Reflection.ParameterAttributes.None, "scope"); methodBuilder.DefineParameter(3, System.Reflection.ParameterAttributes.None, "thisValue"); } optimizationInfo.MarkSequencePoint(generator, new SourceCodeSpan(1, 1, 1, 1)); GenerateCode(generator, optimizationInfo); generator.Complete(); // Bake it. var type = typeBuilder.CreateType(); var methodInfo = type.GetMethod(this.GetMethodName()); this.GeneratedMethod = new GeneratedMethod(Delegate.CreateDelegate(GetDelegate(), methodInfo), optimizationInfo.NestedFunctions); #endif //WINDOWS_PHONE } if (this.Engine.EnableILAnalysis) { // Store the disassembled IL so it can be retrieved for analysis purposes. this.GeneratedMethod.DisassembledIL = generator.ToString(); } }