4c765872234b39985754c0fa7cc034897af2d014
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / ForIn.cs
1 //
2 // ForIn.cs:
3 //
4 // Author: 
5 //      Cesar Octavio Lopez Nataren
6 //
7 // (C) 2003, Cesar Octavio Lopez Nataren, <cesar@ciencias.unam.mx>
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Reflection.Emit;
34
35 namespace Microsoft.JScript {
36
37         public class ForIn : AST, ICanModifyContext {
38
39                 AST lhs, obj, body;
40                 
41                 internal ForIn (AST parent, AST lhs, AST obj, AST body, Location location)
42                         : base (parent, location)
43                 {
44                         this.lhs = lhs;
45                         this.obj = obj;
46                         this.body = body;
47                 }
48
49                 public static IEnumerator JScriptGetEnumerator (object coll)
50                 {
51                         IEnumerable e = coll as IEnumerable;
52                         if (e == null)
53                                 throw new JScriptException(JSError.NotCollection);
54                         
55                         return e.GetEnumerator();
56                 }
57
58                 void ICanModifyContext.PopulateContext (Environment env, string ns)
59                 {
60                         if (lhs is ICanModifyContext)
61                                 ((ICanModifyContext) lhs).PopulateContext (env, ns);
62
63                         if (body is ICanModifyContext)
64                                 ((ICanModifyContext) body).PopulateContext (env, ns);
65                 }
66
67                 void ICanModifyContext.EmitDecls (EmitContext ec)
68                 {
69                         if (lhs is ICanModifyContext)
70                                 ((ICanModifyContext) lhs).EmitDecls (ec);
71
72                         if (body is ICanModifyContext)
73                                 ((ICanModifyContext) body).EmitDecls (ec);
74                 }
75
76                 internal override bool Resolve (Environment env)
77                 {
78                         bool r = true;
79                         if (lhs != null)
80                                 if (lhs is VariableStatement)
81                                         ((ICanModifyContext) lhs).PopulateContext (env, String.Empty);
82                                 else
83                                         r &= lhs.Resolve (env);
84                         if (obj != null)
85                                 r &= obj.Resolve (env);
86
87                         if (body != null)
88                                 r &= body.Resolve (env);
89                         return r;
90                 }
91
92                 internal override void Emit (EmitContext ec)
93                 {
94                         ILGenerator ig = ec.ig;
95                         bool varStm = lhs is VariableStatement;
96                         object var = null;
97
98                         if (varStm) {
99                                 VariableStatement stm = (VariableStatement) lhs;
100                                 ig.Emit (OpCodes.Ldnull);
101                                 var = TypeManager.Get (((VariableDeclaration) stm.var_decls [0]).id);
102                                 set_builder (ig, var);
103                         }
104                         if (obj != null)
105                                 obj.Emit (ec);
106
107                         CodeGenerator.load_engine (InFunction, ig);
108
109
110                         Type convert = typeof (Convert);
111                         ig.Emit (OpCodes.Call, convert.GetMethod ("ToForInObject"));
112                         ig.Emit (OpCodes.Call, typeof (ForIn).GetMethod ("JScriptGetEnumerator"));
113                         Type ienumerator = typeof (IEnumerator);
114                         LocalBuilder iter = ig.DeclareLocal (ienumerator);
115                         LocalBuilder current = ig.DeclareLocal (typeof (object));
116
117                         ig.Emit (OpCodes.Stloc, iter);
118                                 
119                         Label init_loop = ig.DefineLabel ();
120                         Label move_next = ig.DefineLabel ();
121                         Label exit = ig.DefineLabel ();
122                                                                 
123                         ig.Emit (OpCodes.Br, move_next);
124                         ig.MarkLabel (init_loop);
125
126                         if (body != null)
127                                 body.Emit (ec);
128
129                         ig.MarkLabel (move_next);
130
131                         ig.Emit (OpCodes.Ldloc, iter);
132                         ig.Emit (OpCodes.Callvirt, ienumerator.GetMethod ("MoveNext"));
133                                 
134                         ig.Emit (OpCodes.Brfalse, exit);
135
136                         ig.Emit (OpCodes.Ldloc, iter);
137                         ig.Emit (OpCodes.Callvirt, ienumerator.GetProperty ("Current").GetGetMethod ());
138                         ig.Emit (OpCodes.Stloc, current);
139                         ig.Emit (OpCodes.Ldloc, current);
140
141                         if (varStm)
142                                 set_builder (ig, var);
143                         else {
144                                 if (lhs is Expression) {
145                                         AST ast = ((Expression) lhs).Last;
146                                         if (ast is Identifier)
147                                                 ((Identifier) ast).EmitStore (ec);
148                                         else 
149                                                 throw new NotImplementedException ();
150                                 } else
151                                         throw new NotImplementedException ();
152                         }
153
154                         ig.Emit (OpCodes.Br, init_loop);
155                         ig.MarkLabel (exit);
156                 }
157
158                 void set_builder (ILGenerator ig, object builder)
159                 {
160                         if (builder is FieldBuilder)
161                                 ig.Emit (OpCodes.Stsfld, (FieldBuilder) builder);
162                         else if (builder is LocalBuilder)
163                                 ig.Emit (OpCodes.Stloc, (LocalBuilder) builder);
164                 }
165         }
166 }