merge from trunk revisions 58933, 58935, 58936
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / Try.cs
1 //
2 // Try.cs:
3 //
4 // Author: Cesar Octavio Lopez Nataren
5 //
6 // (C) 2003, 2004, Cesar Octavio Lopez Nataren
7 //
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using Microsoft.JScript.Vsa;
32 using System.Reflection;
33 using System.Reflection.Emit;
34 using System.Collections;
35
36 namespace Microsoft.JScript {
37
38         public sealed class Try : AST, ICanModifyContext {
39
40                 internal FieldBuilder field_info;
41                 internal LocalBuilder local_builder;
42                 
43                 internal AST guarded_block;
44                 internal ArrayList catch_blocks;
45                 internal AST finally_block;
46
47
48                 internal Try (AST guarded_block, ArrayList catch_block, AST finally_block, AST parent, Location location)
49                         : base (parent, location)
50                 {
51                         this.guarded_block = guarded_block;
52                         this.catch_blocks = catch_block;
53                         this.finally_block = finally_block;
54                 }               
55
56                 public static Object JScriptExceptionValue (object e, VsaEngine engine)
57                 {
58                         Exception exc = e as Exception;
59                         string message = null;
60                         if (exc != null)
61                                 message = exc.Message;
62                         else
63                                 message = String.Format ("Unknown exception of type {0}", exc.GetType ());
64                         return new ErrorObject (message);
65                 }
66
67                 public static void PushHandlerScope (VsaEngine engine, string id, int scopeId)
68                 {
69                         throw new NotImplementedException ();
70                 }
71
72                 internal override bool Resolve (Environment env)
73                 {
74                         bool r = true;
75                         if (guarded_block != null)
76                                 r &= guarded_block.Resolve (env);
77
78                         if (catch_blocks != null && catch_blocks.Count > 0) {
79                                 foreach (Catch c in catch_blocks) {
80                                         env.BeginScope (String.Empty, true);
81                                         env.Enter (String.Empty, Symbol.CreateSymbol (c.id), c);
82                                         r &= c.Resolve (env);
83                                         env.EndScope (String.Empty);
84                                 }
85                         }
86                         if (finally_block != null)
87                                 r &= finally_block.Resolve (env);
88                         return r;
89                 }
90
91                 internal override void Emit (EmitContext ec)
92                 {
93                         ILGenerator ig = ec.ig;
94                         
95                         ig.BeginExceptionBlock ();
96
97                         if (guarded_block != null)
98                                 guarded_block.Emit (ec);
99
100                         if (catch_blocks != null && catch_blocks.Count > 0) {
101                                 foreach (Catch c in catch_blocks)
102                                         c.Emit (ec);
103                         }               
104                         if (finally_block != null) {
105                                 ig.BeginFinallyBlock ();
106                                 finally_block.Emit (ec);
107                         }
108                         ig.EndExceptionBlock ();
109                 }
110
111
112                 void ICanModifyContext.PopulateContext (Environment env, string ns)
113                 {
114                         if (guarded_block is ICanModifyContext)
115                                 ((ICanModifyContext) guarded_block).PopulateContext (env, ns);
116
117                         foreach (AST ast in catch_blocks)
118                                 if (ast is ICanModifyContext)
119                                         ((ICanModifyContext) ast).PopulateContext (env, ns);
120
121                         if (finally_block is ICanModifyContext)
122                                 ((ICanModifyContext) finally_block).PopulateContext (env, ns);
123                 }
124
125                 void ICanModifyContext.EmitDecls (EmitContext ec)
126                 {
127                         if (guarded_block is ICanModifyContext)
128                                 ((ICanModifyContext) guarded_block).EmitDecls (ec);
129
130                         foreach (AST ast in catch_blocks)
131                                 if (ast is ICanModifyContext)
132                                         ((ICanModifyContext) ast).EmitDecls (ec);
133
134                         if (finally_block is ICanModifyContext)
135                                 ((ICanModifyContext) finally_block).EmitDecls (ec);
136                 }
137         }
138 }