2005-08-13 Florian Gross <flgr@ccan.de>
[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 string type_annot;
41                 internal Block body;
42
43                 internal Location location;
44
45                 internal FunctionObject (string name)
46                 {
47                         this._prototype = ObjectPrototype.Proto;
48                         this.name = name;
49                 }
50
51                 internal FunctionObject (MethodInfo info)
52                 {
53                         this._prototype = ObjectPrototype.Proto;
54                         this.method = info;
55                         this.name = info.Name;
56                         this.attr = info.Attributes;
57                         this.return_type = info.ReturnType;
58                 }
59
60                 internal FunctionObject (string name, FormalParameterList p, string ret_type, Block body, Location location)
61                 {
62                         this._prototype = ObjectPrototype.Proto;
63                         //
64                         // FIXME
65                         // 1) Must collect the attributes given.
66                         // 2) Check if they are semantically correct.
67                         // 3) Assign those values to 'attr'.
68                         //
69                         this.attr = MethodAttributes.Public | MethodAttributes.Static;
70
71                         this.name = name;
72                         this.parameters = p;
73
74                         this.type_annot = ret_type;
75                         //
76                         // FIXME: Must check that return_type it's a valid type,
77                         // and assign that to 'return_type' field.
78                         //
79                         this.return_type = typeof (void);
80
81                         this.body = body;
82                         this.location = location;
83                 }
84
85                 public override int length {
86                         get {
87                                 if (this.parameters != null)
88                                         return this.parameters.ids.Count;
89                                 else
90                                         return base.length;
91                         }
92                         set { base.length = value; }
93                 }
94             
95                 internal FunctionObject ()
96                 {
97                         this.parameters = new FormalParameterList (location);
98                 }
99
100                 internal Type [] params_types ()
101                 {
102                         if (parameters == null)
103                                 return 
104                                         new Type [] {typeof (Object), typeof (Microsoft.JScript.Vsa.VsaEngine)};
105                         else {
106                                 int i, size;
107                                 ArrayList p = parameters.ids;
108
109                                 size = p.Count + 2;
110
111                                 Type [] types = new Type [size];
112
113                                 types [0] = typeof (Object);
114                                 types [1] = typeof (Microsoft.JScript.Vsa.VsaEngine);
115
116                                 for (i = 2; i < size; i++)
117                                         types [i] = ((FormalParam) p [i - 2]).type;
118
119                                 return types;
120                         }
121                 }
122         }
123 }