4 // Author: Cesar Octavio Lopez Nataren
6 // (C) 2003, 2004, Cesar Octavio Lopez Nataren
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using Microsoft.JScript.Vsa;
32 using System.Reflection;
33 using System.Reflection.Emit;
34 using System.Collections;
36 namespace Microsoft.JScript {
38 public sealed class Try : AST, ICanModifyContext {
40 internal FieldBuilder field_info;
41 internal LocalBuilder local_builder;
43 internal AST guarded_block;
44 internal ArrayList catch_blocks;
45 internal AST finally_block;
48 internal Try (AST guarded_block, ArrayList catch_block, AST finally_block, AST parent, Location location)
49 : base (parent, location)
51 this.guarded_block = guarded_block;
52 this.catch_blocks = catch_block;
53 this.finally_block = finally_block;
56 public static Object JScriptExceptionValue (object e, VsaEngine engine)
58 Exception exc = e as Exception;
59 string message = null;
61 message = exc.Message;
63 message = String.Format ("Unknown exception of type {0}", exc.GetType ());
64 return new ErrorObject (message);
67 public static void PushHandlerScope (VsaEngine engine, string id, int scopeId)
69 throw new NotImplementedException ();
72 internal override bool Resolve (Environment env)
75 if (guarded_block != null)
76 r &= guarded_block.Resolve (env);
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);
83 env.EndScope (String.Empty);
86 if (finally_block != null)
87 r &= finally_block.Resolve (env);
91 internal override void Emit (EmitContext ec)
93 ILGenerator ig = ec.ig;
95 ig.BeginExceptionBlock ();
97 if (guarded_block != null)
98 guarded_block.Emit (ec);
100 if (catch_blocks != null && catch_blocks.Count > 0) {
101 foreach (Catch c in catch_blocks)
104 if (finally_block != null) {
105 ig.BeginFinallyBlock ();
106 finally_block.Emit (ec);
108 ig.EndExceptionBlock ();
112 void ICanModifyContext.PopulateContext (Environment env, string ns)
114 if (guarded_block is ICanModifyContext)
115 ((ICanModifyContext) guarded_block).PopulateContext (env, ns);
117 foreach (AST ast in catch_blocks)
118 if (ast is ICanModifyContext)
119 ((ICanModifyContext) ast).PopulateContext (env, ns);
121 if (finally_block is ICanModifyContext)
122 ((ICanModifyContext) finally_block).PopulateContext (env, ns);
125 void ICanModifyContext.EmitDecls (EmitContext ec)
127 if (guarded_block is ICanModifyContext)
128 ((ICanModifyContext) guarded_block).EmitDecls (ec);
130 foreach (AST ast in catch_blocks)
131 if (ast is ICanModifyContext)
132 ((ICanModifyContext) ast).EmitDecls (ec);
134 if (finally_block is ICanModifyContext)
135 ((ICanModifyContext) finally_block).EmitDecls (ec);