Esempio n. 1
0
        void EmitSpecial(ILGenerator gen)
        {
            switch (special)
            {
            case Specials.Label:
                gen.MarkLabel((Label)operand);
                break;

            case Specials.BeginTry:
                gen.BeginExceptionBlock();
                break;

            case Specials.BeginFinally:
                gen.BeginFinallyBlock();
                break;

            case Specials.EndTry:
                gen.EndExceptionBlock();
                break;

            case Specials.BeginTotemScope:
                gen.Emit(OpCodes.Nop);
                gen.Emit(OpCodes.Ldarg_0);
                gen.Emit(OpCodes.Newobj, Generator.scope_ctor);
                gen.Emit(OpCodes.Stloc, (LocalBuilder)operand);
                gen.BeginExceptionBlock();
                break;

            case Specials.EndTotemScope:
                var endFinallyLabel = gen.DefineLabel();
                gen.BeginFinallyBlock();
                gen.Emit(OpCodes.Ldloc, (LocalBuilder)operand);
                gen.Emit(OpCodes.Ldnull);
                gen.Emit(OpCodes.Ceq);
                gen.Emit(OpCodes.Brtrue, endFinallyLabel);

                gen.Emit(OpCodes.Ldloc, (LocalBuilder)operand);
                gen.Emit(OpCodes.Callvirt, Generator.dispose);
                gen.Emit(OpCodes.Nop);

                gen.MarkLabel(endFinallyLabel);
                gen.EndExceptionBlock();
                break;

            default:
                throw new InvalidOperationException("default:special");
            }
        }
Esempio n. 2
0
        internal override void Emit(EmitContext ec)
        {
            ILGenerator ig = ec.ig;

            if (exp != null)
            {
                exp.Emit(ec);
            }

            ig.Emit(OpCodes.Ldarg_0);
            ig.Emit(OpCodes.Ldfld, typeof(ScriptObject).GetField("engine"));
            ig.Emit(OpCodes.Call, typeof(With).GetMethod("JScriptWith"));
            ig.Emit(OpCodes.Pop);

            ig.BeginExceptionBlock();

            if (stm != null)
            {
                stm.Emit(ec);
            }

            ig.BeginFinallyBlock();
            ig.Emit(OpCodes.Ldarg_0);
            ig.Emit(OpCodes.Ldfld, typeof(ScriptObject).GetField("engine"));
            ig.Emit(OpCodes.Call, typeof(VsaEngine).GetMethod("PopScriptObject"));
            ig.Emit(OpCodes.Pop);

            ig.EndExceptionBlock();
        }
        public void Emit(ILGenerator il, int offset, ExceptionHandlingClauseData clause)
        {
            if (offset == clause.TryOffset)
            {
                il.BeginExceptionBlock();
            }
            if (offset == clause.HandlerOffset)
            {
                switch (clause.Flags)
                {
                case ExceptionHandlingClauseOptions.Finally:
                    il.BeginFinallyBlock();
                    break;

                case ExceptionHandlingClauseOptions.Fault:
                    il.BeginFaultBlock();
                    break;

                case ExceptionHandlingClauseOptions.Clause:
                    il.BeginCatchBlock(typeResolver.GetType(clause.CatchType));
                    break;

                default:
                    throw new DelegateDeserializationException(string.Format("Unknown clause {0}", clause.Flags));
                }
            }
            if (offset == clause.HandlerOffset + clause.HandlerLength)
            {
                il.EndExceptionBlock();
            }
        }
Esempio n. 4
0
        internal override void Emit(EmitContext ec)
        {
            ILGenerator ig = ec.ig;

            ig.BeginExceptionBlock();

            if (guarded_block != null)
            {
                guarded_block.Emit(ec);
            }

            if (catch_blocks != null && catch_blocks.Count > 0)
            {
                foreach (Catch c in catch_blocks)
                {
                    c.Emit(ec);
                }
            }
            if (finally_block != null)
            {
                ig.BeginFinallyBlock();
                finally_block.Emit(ec);
            }
            ig.EndExceptionBlock();
        }
Esempio n. 5
0
        public ILGenerator Weave(ILGenerator ilGenerator)
        {
            ilGenerator.BeginFinallyBlock();
            finallyWeaversQueue.Weave(ilGenerator);

            return(ilGenerator);
        }
Esempio n. 6
0
        /// <summary>
        /// 编织创建无返回值的方法
        /// </summary>
        /// <param name="methodIL">方法指令器</param>
        /// <param name="proxyMethod">代理的方法</param>
        /// <param name="parameterTypes">方法的参数</param>
        /// <param name="_testfb"></param>
        private static void CreateVoidMethod(ILGenerator methodIL, MethodInfo proxyMethod, MethodInfo souceMethod, FieldBuilder _testfb)
        {
            Type[] parameterTypes = proxyMethod.GetParameters().Select(o => o.ParameterType).ToArray();
            var    _try           = methodIL.BeginExceptionBlock();

            callMethodAfter(methodIL, souceMethod);

            #region 调用方法等同于  ReturnType result= method(arg1,arg2,....);
            methodIL.Emit(OpCodes.Ldarg_0);
            methodIL.Emit(OpCodes.Ldfld, _testfb);
            for (short i = 0; i < parameterTypes.Length; i++)
            {
                methodIL.Emit(OpCodes.Ldarg, i + 1);
            }
            methodIL.Emit(OpCodes.Callvirt, proxyMethod);
            #endregion

            callMethodBefore(methodIL, _testfb, souceMethod);
            methodIL.BeginCatchBlock(typeof(Exception));
            methodIL.EmitWriteLine("异常");
            methodIL.BeginFinallyBlock();
            methodIL.EmitWriteLine("一定");
            methodIL.EndExceptionBlock();
            methodIL.Emit(OpCodes.Ret);
        }
Esempio n. 7
0
        /// <summary>
        /// Aspect code to inject at the end of weaved method
        /// </summary>
        /// <param name="typeBuilder">Type Builder</param>
        /// <param name="method">Method</param>
        /// <param name="parameter">Parameter</param>
        /// <param name="il">ILGenerator</param>
        internal void EndAspectBlock(TypeBuilder typeBuilder, MethodBase method, ParameterInfo parameter, ILGenerator il)
        {
            if (exLocal != null)
            {
                il.Emit(OpCodes.Stloc, exLocal);
            }

            il.BeginFinallyBlock();

            var takenLabel = il.DefineLabel();

            il.Emit(OpCodes.Ldloc, lockWasTokenLocal);
            il.Emit(OpCodes.Brfalse, takenLabel);

            il.Emit(OpCodes.Ldloc, tempLocal);
            il.Emit(OpCodes.Call, ExitMethod);

            il.MarkLabel(takenLabel);

            il.EndExceptionBlock();

            if (exLocal != null)
            {
                il.Emit(OpCodes.Ldloc, exLocal);
            }
        }
Esempio n. 8
0
        internal override void EmitSet(ILGenerator ilg, bool preserve)
        {
            var push = Compilation.Get(WellKnownMembers.XSharp_RT_Functions___pushWorkarea) as MethodSymbol;
            var pop  = Compilation.Get(WellKnownMembers.XSharp_RT_Functions___popWorkarea) as MethodSymbol;
            var temp = preserve ? ilg.DeclareLocal(Datatype.Type) : null;
            var v    = ilg.DeclareLocal(Compilation.Get(NativeType.Usual).Type);

            ilg.Emit(OpCodes.Stloc, v.LocalIndex);
            Alias.Emit(ilg);
            ilg.Emit(OpCodes.Call, push.Method);
            ilg.BeginExceptionBlock();
            ilg.Emit(OpCodes.Ldloc, v.LocalIndex);
            Field.EmitSet(ilg, preserve);
            if (preserve)
            {
                ilg.Emit(OpCodes.Stloc, temp.LocalIndex);
            }
            ilg.BeginFinallyBlock();
            ilg.Emit(OpCodes.Call, pop.Method);
            ilg.EndExceptionBlock();
            if (preserve)
            {
                ilg.Emit(OpCodes.Ldloc, temp.LocalIndex);
            }
        }
Esempio n. 9
0
        public static void MarkBlockBefore(ILGenerator il, ExceptionBlock block, out Label?label)
        {
            label = null;
            switch (block.blockType)
            {
            case ExceptionBlockType.BeginExceptionBlock:
                label = il.BeginExceptionBlock();
                return;

            case ExceptionBlockType.BeginCatchBlock:
                il.BeginCatchBlock(block.catchType);
                return;

            case ExceptionBlockType.BeginExceptFilterBlock:
                il.BeginExceptFilterBlock();
                return;

            case ExceptionBlockType.BeginFaultBlock:
                il.BeginFaultBlock();
                return;

            case ExceptionBlockType.BeginFinallyBlock:
                il.BeginFinallyBlock();
                return;
            }
        }
Esempio n. 10
0
 public IFluentILGenerator BeginFinallyBlock()
 {
     _logger?.LogMeta("finally");
     _logger?.LogMeta("{");
     _generator.BeginFinallyBlock();
     return(this);
 }
Esempio n. 11
0
        /// <summary>
        /// 发行变量和代码块(有返回值)。
        /// </summary>
        /// <param name="ilg">指令。</param>
        /// <param name="variable">存储结果的变量。</param>
        /// <param name="label">跳转位置。</param>
        protected override void Emit(ILGenerator ilg, LocalBuilder variable, Label label)
        {
            ilg.BeginExceptionBlock();

            base.Emit(ilg, variable, label);

            if (catchAsts.Count > 0)
            {
                foreach (var catchAst in catchAsts)
                {
                    FlowControl(catchAst, ilg, variable, label);
                }

                ilg.Emit(OpCodes.Nop);
            }

            if (finallyAst != null)
            {
                ilg.BeginFinallyBlock();

                FlowControl(finallyAst, ilg, label);

                ilg.Emit(OpCodes.Nop);
            }

            ilg.EndExceptionBlock();
        }
Esempio n. 12
0
        /// <summary>
        /// Generates method logic.
        /// </summary>
        /// <param name="il">The IL generator to use.</param>
        /// <param name="method">The method to proxy.</param>
        /// <param name="interfaceMethod">
        /// The interface definition of the method, if applicable.
        /// </param>
        protected override void GenerateMethodLogic(
            ILGenerator il, MethodInfo method, MethodInfo interfaceMethod)
        {
            Label jmpEndFinally = il.DefineLabel();

            // save target source so we can call Dispose later
            PushAdvisedProxy(il);
            il.Emit(OpCodes.Ldfld, References.TargetSourceWrapperField);
            il.Emit(OpCodes.Stloc, targetSource);

            // open try/finally block
            il.BeginExceptionBlock();

            base.GenerateMethodLogic(il, method, interfaceMethod);

            // open finally block
            il.BeginFinallyBlock();

            // call Dispose on target source
            il.Emit(OpCodes.Ldloc, targetSource);
            il.Emit(OpCodes.Brfalse, jmpEndFinally);
            il.Emit(OpCodes.Ldloc, targetSource);
            il.EmitCall(OpCodes.Callvirt, References.DisposeMethod, null);

            il.MarkLabel(jmpEndFinally);

            // close try/finally block
            il.EndExceptionBlock();
        }
Esempio n. 13
0
 public void BeginFinallyBlock()
 {
     if (!Active)
     {
         return;
     }
     IL.BeginFinallyBlock(); Text.Append("Finally").AppendLine();
 }
Esempio n. 14
0
 public static void EmitTryFinally(this ILGenerator g, Action emitTry, Action emitFinally)
 {
     g.BeginExceptionBlock();
     emitTry();
     g.BeginFinallyBlock();
     emitFinally();
     g.EndExceptionBlock();
 }
Esempio n. 15
0
 public IILGen Finally()
 {
     _sourceCodeWriter.CloseScope();
     _sourceCodeWriter.MarkAndWriteLine(_ilGenerator, "finally");
     _sourceCodeWriter.OpenScope();
     _ilGenerator.BeginFinallyBlock();
     return(this);
 }
Esempio n. 16
0
        private void EmitILForExceptionHandlers(ILGenerator ilGenerator, Instruction instruction, List <ExceptionHandler> handlers)
        {
            var tryBlocks = handlers.Where(h => h.TryStart == instruction.Offset).GroupBy(h => h.TryEnd);

            foreach (var tryBlock in tryBlocks)
            {
                ilGenerator.BeginExceptionBlock();
                m_exceptionBlockLevel++;
            }

            var filterBlock = handlers.FirstOrDefault(h => h.FilterStart == instruction.Offset);

            if (filterBlock != null)
            {
                ilGenerator.BeginExceptFilterBlock();
            }

            var handler = handlers.FirstOrDefault(h => h.HandlerEnd == instruction.Offset);

            if (handler != null)
            {
                if (handler.Flags == ExceptionHandlingClauseOptions.Finally)
                {
                    // Finally blocks are always the last handler
                    ilGenerator.EndExceptionBlock();
                    m_exceptionBlockLevel--;
                }
                else if (handler.HandlerEnd == handlers.Where(h => h.TryStart == handler.TryStart && h.TryEnd == handler.TryEnd).Max(h => h.HandlerEnd))
                {
                    // We're dealing with the last catch block
                    ilGenerator.EndExceptionBlock();
                    m_exceptionBlockLevel--;
                }
            }

            var catchOrFinallyBlock = handlers.FirstOrDefault(h => h.HandlerStart == instruction.Offset);

            if (catchOrFinallyBlock != null)
            {
                if (catchOrFinallyBlock.Flags == ExceptionHandlingClauseOptions.Clause)
                {
                    ilGenerator.BeginCatchBlock(catchOrFinallyBlock.CatchType);
                }
                else if (catchOrFinallyBlock.Flags == ExceptionHandlingClauseOptions.Filter)
                {
                    ilGenerator.BeginCatchBlock(null);
                }
                else if (catchOrFinallyBlock.Flags == ExceptionHandlingClauseOptions.Finally)
                {
                    ilGenerator.BeginFinallyBlock();
                }
                else
                {
                    // No support for fault blocks
                    throw new NotSupportedException();
                }
            }
        }
Esempio n. 17
0
        public static void EmitTryFinally(this ILGenerator il, Action tryBody, Action finallyBody)
        {
            var block = il.BeginExceptionBlock();

            tryBody();
            il.Emit(OpCodes.Leave_S, block);
            il.BeginFinallyBlock();
            finallyBody();
            il.EndExceptionBlock();
        }
Esempio n. 18
0
        public override CodeNode VisitTryFinallyNode(TryFinallyNode node)
        {
            ILGen.BeginExceptionBlock();
            Visit(node.TryNode);
            ILGen.BeginFinallyBlock();
            Visit(node.FinallyNode);
            ILGen.EndExceptionBlock();

            return(node);
        }
Esempio n. 19
0
        public void BeginFinallyBlock()
        {
            TypeBuilder   type      = Helpers.DynamicType(TypeAttributes.NotPublic);
            MethodBuilder method    = type.DefineMethod("PosTest5_Method", MethodAttributes.Public | MethodAttributes.Static);
            ILGenerator   generator = method.GetILGenerator();

            generator.BeginExceptionBlock();
            generator.BeginFinallyBlock();
            VerifyDeclareLocal(generator);
        }
Esempio n. 20
0
        private void EmitJsonWriterFinallyDispose(ILGenerator ilGenerator)
        {
            ilGenerator.BeginFinallyBlock();

            ilGenerator.Emit(OpCodes.Ldloc_2);
            ilGenerator.Emit(OpCodes.Castclass, typeof(IDisposable));
            ilGenerator.Emit(OpCodes.Callvirt, typeof(IDisposable).GetRuntimeMethod("Dispose", new Type[0]));

            ilGenerator.EndExceptionBlock();
        }
Esempio n. 21
0
        public void ValidFilterBlock2()
        {
            DefineBasicMethod();
            ILGenerator il = il_gen;

            il.BeginExceptionBlock();
            il.BeginExceptFilterBlock();
            il.BeginFinallyBlock();
            il.EndExceptionBlock();
        }
Esempio n. 22
0
        internal override void TranslateToIL(ILGenerator il, Type rtype)
        {
            //This assumes that rtype == Void.class
            this.context.EmitLineInfo(il);
            Globals.ScopeStack.Push(new WithObject(Globals.ScopeStack.Peek(), new JSObject(null, false)));
            bool savedInsideProtectedRegion = compilerGlobals.InsideProtectedRegion;

            compilerGlobals.InsideProtectedRegion = true;
            Label lab = il.DefineLabel();

            compilerGlobals.BreakLabelStack.Push(lab);
            compilerGlobals.ContinueLabelStack.Push(lab);
            this.obj.TranslateToIL(il, Typeob.Object);
            this.EmitILToLoadEngine(il);
            il.Emit(OpCodes.Call, CompilerGlobals.jScriptWithMethod); // JScriptWith returns the with object as an 'Object' (used by the debugger EE)

            // define a local named 'with()' that the debugger EE will use to bind to the with object
            LocalBuilder withObj = null;

            if (context.document.debugOn)
            {
                il.BeginScope(); // used by the debugger to mark a with block
                withObj = il.DeclareLocal(Typeob.Object);
                withObj.SetLocalSymInfo("with()");
                il.Emit(OpCodes.Stloc, withObj);
            }
            else
            {
                il.Emit(OpCodes.Pop);
            }

            il.BeginExceptionBlock();
            this.block.TranslateToILInitializer(il);
            this.block.TranslateToIL(il, Typeob.Void);
            il.BeginFinallyBlock();
            if (context.document.debugOn)
            {
                // null the local used by the debugger EE
                il.Emit(OpCodes.Ldnull);
                il.Emit(OpCodes.Stloc, withObj);
            }
            this.EmitILToLoadEngine(il);
            il.Emit(OpCodes.Call, CompilerGlobals.popScriptObjectMethod);
            il.Emit(OpCodes.Pop);
            il.EndExceptionBlock();
            if (context.document.debugOn)
            {
                il.EndScope(); // used by the debugger to mark a with block
            }
            il.MarkLabel(lab);
            compilerGlobals.BreakLabelStack.Pop();
            compilerGlobals.ContinueLabelStack.Pop();
            compilerGlobals.InsideProtectedRegion = savedInsideProtectedRegion;
            Globals.ScopeStack.Pop();
        }
Esempio n. 23
0
        internal static void EmitExceptionBlock(ILGenerator il, CodeInstruction ci)
        {
            if (ci.startException > 0)
            {
                for (int i = 0; i < ci.startException; i++)
                {
                    if (HarmonyInstance.DEBUG)
                    {
                        FileLog.Log("try {\n");
                    }
                    il.BeginExceptionBlock();
                }
            }

            if (ci.endException > 0)
            {
                for (int i = 0; i < ci.endException; i++)
                {
                    if (HarmonyInstance.DEBUG)
                    {
                        FileLog.Log("}\n");
                    }
                    il.EndExceptionBlock();
                }
            }

            if (ci.isStartCatch)
            {
                if (HarmonyInstance.DEBUG)
                {
                    FileLog.Log("} // try");

                    String catchLog = "catch ";

                    if (ci.catchType != null)
                    {
                        catchLog += "(" + ci.catchType.ToString() + ")";
                    }

                    FileLog.Log(catchLog + " {");
                }

                il.BeginCatchBlock(ci.catchType);
            }
            else if (ci.isStartFinally)
            {
                if (HarmonyInstance.DEBUG)
                {
                    FileLog.Log("} // try");
                    FileLog.Log("finally { ");
                }

                il.BeginFinallyBlock();
            }
        }
Esempio n. 24
0
        private static void BuildAssembly(string sourceCode, string assemblyFilePath)
        {
            AssemblyName    assemblyName    = new AssemblyName("Temp");
            AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save, Path.GetDirectoryName(assemblyFilePath));
            ModuleBuilder   moduleBuilder   = assemblyBuilder.DefineDynamicModule(assemblyName.Name, Path.GetFileName(assemblyFilePath));
            TypeBuilder     typeBuilder     = moduleBuilder.DefineType("Application", TypeAttributes.NotPublic | TypeAttributes.Sealed);
            MethodBuilder   methodBuilder   = typeBuilder.DefineMethod("Main", MethodAttributes.Private | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
            ILGenerator     generator       = methodBuilder.GetILGenerator();

            generator.BeginExceptionBlock();
            using (StringReader reader = new StringReader(sourceCode))
            {
                MagnetoCompiler.Compile(MagnetoApplication.Current.ModuleManager, reader, generator);
            }
            generator.BeginCatchBlock(typeof(Exception));
            generator.Emit(OpCodes.Callvirt, exceptionMessageGetter);
            generator.Emit(OpCodes.Ldc_I4, (int)ConsoleColor.Red);
            generator.Emit(OpCodes.Call, foregroundColorSetter);
            generator.Emit(OpCodes.Ldstr, "");
            generator.Emit(OpCodes.Call, consoleWriteLineMethod);
            generator.Emit(OpCodes.Call, consoleWriteLineMethod);
            generator.BeginFinallyBlock();
            generator.Emit(OpCodes.Ldc_I4, (int)ConsoleColor.Cyan);
            generator.Emit(OpCodes.Call, foregroundColorSetter);
            generator.Emit(OpCodes.Ldstr, "");
            generator.Emit(OpCodes.Call, consoleWriteLineMethod);
            generator.Emit(OpCodes.Ldstr, "Finished: press <Enter> to exit.");
            generator.Emit(OpCodes.Call, consoleWriteLineMethod);
            generator.Emit(OpCodes.Call, consoleReadLineMethod);
            generator.EndExceptionBlock();
            generator.Emit(OpCodes.Ret);

            //	Generated code:
            //		try
            //		{
            //			// compiled application code
            //		}
            //		catch(Exception lException)
            //		{
            //			Console.ForegroundColor = ConsoleColor.Red;
            //			Console.WriteLine("");
            //			Console.WriteLine(lException.Message);
            //		}
            //		finally
            //		{
            //			Console.ForegroundColor = ConsoleColor.Cyan;
            //			Console.WriteLine("");
            //			Console.WriteLine("Finished: press <Enter> to exit.");
            //			Console.ReadLine();
            //		}

            typeBuilder.CreateType();
            assemblyBuilder.SetEntryPoint(methodBuilder, PEFileKinds.ConsoleApplication);
            assemblyBuilder.Save(Path.GetFileName(assemblyFilePath));
        }
Esempio n. 25
0
        private void EmitMethod(TypeBuilder typeBuilder)
        {
            MethodBuilder mapMethod = typeBuilder.DefineMethod("Map", MethodAttributes.Public | MethodAttributes.Virtual, typeof(void), new[] { typeof(IEnumerable) });

            ILGenerator  il         = mapMethod.GetILGenerator();
            LocalBuilder result     = il.DeclareLocal(typeof(List <int>));  //0
            LocalBuilder item       = il.DeclareLocal(typeof(object));      //1
            LocalBuilder enumeartor = il.DeclareLocal(typeof(IEnumerator)); //2
            LocalBuilder dispose    = il.DeclareLocal(typeof(IDisposable)); //3

            Label labelWhile      = il.DefineLabel();
            Label labelReturn     = il.DefineLabel();
            Label labelMoveNext   = il.DefineLabel();
            Label labelEndFinally = il.DefineLabel();

            //Create result List
            ConstructorInfo constructorInfo = (typeof(List <int>).GetConstructor(Type.EmptyTypes));

            il.Emit(OpCodes.Newobj, constructorInfo);
            il.Emit(OpCodes.Stloc_0, result);

            il.Emit(OpCodes.Ldarg_1);
            il.EmitCall(OpCodes.Callvirt, typeof(IEnumerable).GetMethod("GetEnumerator"), Type.EmptyTypes);
            il.Emit(OpCodes.Stloc_2, enumeartor);

            il.BeginExceptionBlock();
            il.Emit(OpCodes.Br_S, labelMoveNext);
            il.MarkLabel(labelWhile);

            il.Emit(OpCodes.Ldloc_2);
            il.EmitCall(OpCodes.Callvirt, typeof(IEnumerator).GetProperty("Current").GetGetMethod(), Type.EmptyTypes);
            il.Emit(OpCodes.Stloc_1, item);

            il.MarkLabel(labelMoveNext);
            il.Emit(OpCodes.Ldloc_2);
            il.EmitCall(OpCodes.Callvirt, typeof(IEnumerator).GetMethod("MoveNext"), Type.EmptyTypes);
            il.Emit(OpCodes.Brtrue_S, labelWhile);

            il.BeginFinallyBlock();

            il.Emit(OpCodes.Ldloc_2);
            il.Emit(OpCodes.Isinst, typeof(IDisposable));
            il.Emit(OpCodes.Stloc_3, dispose);
            il.Emit(OpCodes.Ldloc_3);
            il.Emit(OpCodes.Brfalse_S, labelEndFinally);

            il.Emit(OpCodes.Ldloc_3);
            il.EmitCall(OpCodes.Callvirt, typeof(IDisposable).GetMethod("Dispose"), Type.EmptyTypes);

            il.MarkLabel(labelEndFinally);
            il.EndExceptionBlock();

            il.MarkLabel(labelReturn);
            il.Emit(OpCodes.Ret);
        }
Esempio n. 26
0
        private void EmitMemoryStreamFinallyDispose(ILGenerator ilGenerator)
        {
            ilGenerator.BeginFinallyBlock();

            ilGenerator.Emit(OpCodes.Ldloc_1);
            ilGenerator.Emit(OpCodes.Castclass, typeof(IDisposable));
            var disposeMethod = typeof(IDisposable).GetRuntimeMethod("Dispose", new Type[0]);

            ilGenerator.Emit(OpCodes.Callvirt, disposeMethod);

            ilGenerator.EndExceptionBlock();
        }
Esempio n. 27
0
        private void EmitILForExceptionHandlers(ILGenerator ilGenerator, Instruction instruction, List <ExceptionHandler> handlers)
        {
            int catchBlockCount = 0;

            foreach (var handler in handlers.Where(h => h.TryStart == instruction.Offset))
            {
                if (handler.Flag == "Clause")
                {
                    if (catchBlockCount >= 1)
                    {
                        continue;
                    }

                    ilGenerator.BeginExceptionBlock();
                    catchBlockCount++;
                    //break;
                    continue;
                }

                ilGenerator.BeginExceptionBlock();
            }

            foreach (var handler in handlers.Where(h => h.HandlerEnd == instruction.Offset)) //|| (h.HandlerEnd == instruction.Offset - 1 && instruction.OpCode == OpCodes.Ret)))
            {
                if (handler.Flag == "Clause")
                {
                    var _handlers = handlers.Where(h => h.TryEnd == handler.TryEnd && h.Flag == "Clause");
                    if (handler.HandlerEnd == _handlers.Select(h => h.HandlerEnd).Max())
                    {
                        ilGenerator.EndExceptionBlock();
                    }

                    //break;
                    continue;
                }

                ilGenerator.EndExceptionBlock();
            }

            foreach (var handler in handlers.Where(h => h.HandlerStart == instruction.Offset))
            {
                if (handler.Flag == "Clause")
                {
                    ilGenerator.BeginCatchBlock(handler.CatchType);
                }

                else if (handler.Flag == "Finally")
                {
                    ilGenerator.BeginFinallyBlock();
                }
            }
        }
Esempio n. 28
0
        internal override void TranslateToIL(ILGenerator il, Type rtype)
        {
            base.context.EmitLineInfo(il);
            base.Globals.ScopeStack.Push(new WithObject(base.Globals.ScopeStack.Peek(), new JSObject(null, false)));
            bool insideProtectedRegion = base.compilerGlobals.InsideProtectedRegion;

            base.compilerGlobals.InsideProtectedRegion = true;
            Label item = il.DefineLabel();

            base.compilerGlobals.BreakLabelStack.Push(item);
            base.compilerGlobals.ContinueLabelStack.Push(item);
            this.obj.TranslateToIL(il, Typeob.Object);
            base.EmitILToLoadEngine(il);
            il.Emit(OpCodes.Call, CompilerGlobals.jScriptWithMethod);
            LocalBuilder local = null;

            if (base.context.document.debugOn)
            {
                il.BeginScope();
                local = il.DeclareLocal(Typeob.Object);
                local.SetLocalSymInfo("with()");
                il.Emit(OpCodes.Stloc, local);
            }
            else
            {
                il.Emit(OpCodes.Pop);
            }
            il.BeginExceptionBlock();
            this.block.TranslateToILInitializer(il);
            this.block.TranslateToIL(il, Typeob.Void);
            il.BeginFinallyBlock();
            if (base.context.document.debugOn)
            {
                il.Emit(OpCodes.Ldnull);
                il.Emit(OpCodes.Stloc, local);
            }
            base.EmitILToLoadEngine(il);
            il.Emit(OpCodes.Call, CompilerGlobals.popScriptObjectMethod);
            il.Emit(OpCodes.Pop);
            il.EndExceptionBlock();
            if (base.context.document.debugOn)
            {
                il.EndScope();
            }
            il.MarkLabel(item);
            base.compilerGlobals.BreakLabelStack.Pop();
            base.compilerGlobals.ContinueLabelStack.Pop();
            base.compilerGlobals.InsideProtectedRegion = insideProtectedRegion;
            base.Globals.ScopeStack.Pop();
        }
 public override void Emit(IEasyMember member, ILGenerator gen)
 {
     ArgumentsUtil.EmitLoadOwnerAndReference(this._syncLockSource, gen);
     gen.Emit(OpCodes.Call, typeof(Monitor).GetMethod("Enter", new Type[] { typeof(object) }));
     gen.BeginExceptionBlock();
     foreach (Statement statement in this._stmts)
     {
         statement.Emit(member, gen);
     }
     gen.BeginFinallyBlock();
     ArgumentsUtil.EmitLoadOwnerAndReference(this._syncLockSource, gen);
     gen.Emit(OpCodes.Call, typeof(Monitor).GetMethod("Exit"));
     gen.EndExceptionBlock();
 }
        private void HandleException(IOperation op)
        {
            bool ignore;
            uint offset = op.Offset;

            if (offsetsUsedInExceptionInformation.TryGetValue(offset, out ignore))
            {
                foreach (var exceptionInfo in this.methodBody.OperationExceptionInformation)
                {
                    if (offset == exceptionInfo.TryStartOffset)
                    {
                        generator.BeginTryBody();
                    }

                    // Never need to do anthing when offset == exceptionInfo.TryEndOffset because
                    // we pick up an EndTryBody from the HandlerEndOffset below
                    //  generator.EndTryBody();

                    if (offset == exceptionInfo.HandlerStartOffset)
                    {
                        switch (exceptionInfo.HandlerKind)
                        {
                        case HandlerKind.Catch:
                            generator.BeginCatchBlock(exceptionInfo.ExceptionType);
                            break;

                        case HandlerKind.Fault:
                            generator.BeginFaultBlock();
                            break;

                        case HandlerKind.Filter:
                            generator.BeginFilterBody();
                            break;

                        case HandlerKind.Finally:
                            generator.BeginFinallyBlock();
                            break;
                        }
                    }
                    if (exceptionInfo.HandlerKind == HandlerKind.Filter && offset == exceptionInfo.FilterDecisionStartOffset)
                    {
                        generator.BeginFilterBlock();
                    }
                    if (offset == exceptionInfo.HandlerEndOffset)
                    {
                        generator.EndTryBody();
                    }
                }
            }
        }
Esempio n. 31
0
        void EmitSpecial(ILGenerator gen)
        {
            switch (special)
            {
                case Specials.Label:
                    gen.MarkLabel((Label)operand);
                    break;
                case Specials.BeginTry:
                    gen.BeginExceptionBlock();
                    break;
                case Specials.BeginFinally:
                    gen.BeginFinallyBlock();
                    break;
                case Specials.EndTry:
                    gen.EndExceptionBlock();
                    break;
                case Specials.BeginTotemScope:
                    gen.Emit(OpCodes.Nop);
                    gen.Emit(OpCodes.Ldarg_0);
                    gen.Emit(OpCodes.Newobj, Generator.scope_ctor);
                    gen.Emit(OpCodes.Stloc, (LocalBuilder)operand);
                    gen.BeginExceptionBlock();
                    break;
                case Specials.EndTotemScope:
                    var endFinallyLabel = gen.DefineLabel();
                    gen.BeginFinallyBlock();
                    gen.Emit(OpCodes.Ldloc, (LocalBuilder)operand);
                    gen.Emit(OpCodes.Ldnull);
                    gen.Emit(OpCodes.Ceq);
                    gen.Emit(OpCodes.Brtrue, endFinallyLabel);

                    gen.Emit(OpCodes.Ldloc, (LocalBuilder)operand);
                    gen.Emit(OpCodes.Callvirt, Generator.dispose);
                    gen.Emit(OpCodes.Nop);

                    gen.MarkLabel(endFinallyLabel);
                    gen.EndExceptionBlock();
                    break;
                default:
                    throw new InvalidOperationException("default:special");
            }
        }