summaryrefslogtreecommitdiff
path: root/ghc/rts/dotnet/Invoker.h
blob: d649a4c7162f976787fa0e5020b6d122a45d32ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// (c) 2003, sof.
//
// Dynamic invocation helper classes. The details of how
// to access the .NET object model via the Reflection API
// is taken care of by Invoker.{h,cpp}
//
#pragma once
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Text;
using namespace System::Runtime::InteropServices;

[assembly:AssemblyKeyFileAttribute(S"invoker.snk")];

namespace DynInvoke {

//
// Class: TypeName
//
// Purpose: pairing up an assembly name and the type/class name.
//
[ComVisible(false)]
public __gc class TypeName {

public:
  System::String* m_assembly;
  System::String* m_class;
  int     m_length;

  TypeName() { 
    m_assembly = String::Empty;
    m_class = String::Empty;
    m_length = 0;
  }

  void Print() {
    if (m_assembly && m_assembly != String::Empty ) {
      Console::Write("[");
      Console::Write(m_assembly);
      Console::Write("]");
    }
    Console::WriteLine(m_class);
  }
  
  int Length() { return m_length; }

  System::String* toStdString() {
    System::String* res = new System::String(m_class->ToCharArray());
    
    if (m_assembly && m_assembly != String::Empty ){
      res = String::Concat(res, S",");
      res = String::Concat(res, m_assembly);
    }
    return res;
  }
};

// 
// Class:   InvokeBridge
// 
// Purpose: Collection of (static) methods for dynamically creating
//          objects and accessing methods/fields on them. 
//
[ClassInterface(ClassInterfaceType::AutoDual),
GuidAttribute("39D497D9-60E0-3525-B7F2-7BC096D3A2A3"),
ComVisible(true)
]
public __gc class InvokeBridge {
public:
  InvokeBridge() {
    Assembly* corAss      = Assembly::Load("mscorlib.dll"); 
    System::String*  dir  = System::IO::Path::GetDirectoryName(corAss->Location);
   
    m_assemblies = new System::Collections::ArrayList();
   
    System::String* fs[] = System::IO::Directory::GetFiles(dir, "*.dll");
    for (int i=0;i < fs->Length; i++) {
      try {
	Assembly* tAss = Assembly::LoadFrom(fs[i]);
	m_assemblies->Add(tAss->FullName);
      } catch (Exception* e) {
	continue;
      }
    }
  }

  //
  // Method: CreateObject(String* assemName, String* objSpec, Object* args[])
  //
  // Purpose: Given a fully qualified name of a class/type, try
  //          to create an instance of it.
  //
  Object* CreateObject(System::String* assemName,
		       System::String* objSpec,
		       Object* args[]);
	  
  //
  // Method:  InvokeMethod
  // 
  // Purpose: Given a pointer to an already created object, look up
  //          one of its method. If found, invoke the method passing it
  //          'args' as arguments.
  //
  // Comments: the format of the method-spec is "methodName(type1,..,typeN)" [N>=0]
  //
  Object* InvokeMethod(Object* obj, 
		       System::String* methSpec,
		       Object* args[]);
			      
  //
  // Method:  InvokeStaticMethod
  // 
  // Purpose: Invoke a static method, given the fully qualified name
  //          of the method (and its arguments). If found, invoke the
  //          method passing it 'args' as arguments.
  //
  // Comments: the format of the method-spec is 
  //              "T1.T2.<..>.Tn.methodName(type1,..,typeN)" [N>=0]
  //
  Object* InvokeStaticMethod(System::String* assemName,
			     System::String* methSpec,
			     Object* args[]);
			      
  //
  // Method:  GetField
  //
  // Purpose: Fetch the (boxed) value of named field of a given object.
  //
  Object* GetField(Object* obj, System::String* fieldSpec);

  //
  // Method:  GetField
  //
  // Purpose: Fetch the (boxed) value of named static field.
  //
  Object* GetStaticField(System::String* clsName, 
			 System::String* fieldSpec);

  //
  // Method:  SetField
  //
  // Purpose: Replace the (boxed) value of named field of a given object.
  //
  void SetField(Object* obj, System::String* fieldSpec, Object* val);
	    
  //
  // Method:  SetStaticField
  //
  // Purpose: Replace the (boxed) value of named field of a given object.
  //
  void SetStaticField(System::String* clsName,
		      System::String* fieldSpec,
		      Object* val);
	    

  // 
  // Method:  NewString
  // 
  // Purpose: construct a System.String object copy in a manner that avoids
  //          COM Interop from deconstructing it to a BSTR.
  //
  System::Object* NewString( System::String* s);

  //
  // Method:  NewArgArray
  //
  // Purpose: create a new array for holding (boxed) arguments to constructors/
  //          methods.
  //
  Array* NewArgArray(int sz);
  
  //
  // Method: SetArg
  //
  // Purpose: set an entry in the argument vector.
  //
  void SetArg(Object* arr[], Object* val, int idx);

  //
  // Method: GetArg
  //
  // Purpose: get an entry in the argument vector.
  //
  Object* GetArg(Object* arr[], int idx);

  System::Type* InvokeBridge::GetType(System::String* typeName);

protected:
  System::Collections::ArrayList __gc* m_assemblies;
  Object* InvokeBridge::CreateInstance(TypeName* typeName,
				       Object* args[]);
};

} /* namespace */