2005-04-12 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / FunctionObject.cs
1 //
2 // FunctionObject.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.Text;
33 using System.Reflection;
34 using System.Collections;
35
36 namespace Microsoft.JScript {
37
38         public class FunctionObject : ScriptFunction {
39
40                 internal MethodAttributes attr;
41                 internal string name;
42                 internal string type_annot;
43                 internal Type return_type;
44                 internal FormalParameterList parameters;
45                 internal Block body;
46
47                 internal FunctionObject (string name)
48                 {
49                         this.name = name;
50                 }
51
52                 internal FunctionObject (string name, FormalParameterList p, string ret_type, Block body)
53                 {
54                         //
55                         // FIXME
56                         // 1) Must collect the attributes given.
57                         // 2) Check if they are semantically correct.
58                         // 3) Assign those values to 'attr'.
59                         //
60                         this.attr = MethodAttributes.Public | MethodAttributes.Static;
61
62                         this.name = name;
63                         this.parameters = p;
64
65                         this.type_annot = ret_type;
66                         //
67                         // FIXME: Must check that return_type it's a valid type,
68                         // and assign that to 'return_type' field.
69                         //
70                         this.return_type = typeof (void);
71
72                         this.body = body;
73                 }
74             
75                 internal FunctionObject ()
76                 {
77                         this.parameters = new FormalParameterList ();
78                 }
79
80                 public override string ToString ()
81                 {
82                         StringBuilder sb = new StringBuilder ();
83
84                         sb.Append ("function ");
85                         sb.Append (name + " ");
86                         sb.Append ("(");
87
88                         if (parameters != null)
89                                 sb.Append (this.parameters.ToString ());
90                                         
91                         sb.Append (")");
92                         sb.Append (" : " + return_type);
93                         sb.Append ("{");
94
95                         if (body != null)
96                                 sb.Append (body.ToString ());
97
98                         sb.Append ("}");
99
100                         return sb.ToString ();          
101                 }
102
103                 internal Type [] params_types ()
104                 {
105                         if (parameters == null)
106                                 return 
107                                         new Type [] {typeof (Object), typeof (Microsoft.JScript.Vsa.VsaEngine)};
108                         else {
109                                 int i, size;
110                                 ArrayList p = parameters.ids;
111
112                                 size = p.Count + 2;
113
114                                 Type [] types = new Type [size];
115
116                                 types [0] = typeof (Object);
117                                 types [1] = typeof (Microsoft.JScript.Vsa.VsaEngine);
118
119                                 for (i = 2; i < size; i++)
120                                         types [i] = ((FormalParam) p [i - 2]).type;
121
122                                 return types;
123                         }
124                 }
125         }
126 }