forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallconvutil.cs
More file actions
executable file
·145 lines (116 loc) · 2.89 KB
/
callconvutil.cs
File metadata and controls
executable file
·145 lines (116 loc) · 2.89 KB
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
// CallConvUtil.cs - A utility to rewrite IL and insert calling
// convention metadata. This is needed to ensure that Python
// type callbacks are called using cdecl rather than stdcall.
//
// Author: Brian Lloyd <brian@zope.com>
//
// (c) 2002 Brian Lloyd
using System;
using System.IO;
using System.Collections;
public class CallConvUtil {
static string ccAttr =
".custom instance void Python.Runtime.CallConvCdeclAttribute";
static string modOpt =
"\n modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl)";
StreamReader reader;
StreamWriter writer;
public static int Main(string[] args) {
CallConvUtil munger = new CallConvUtil();
return munger.Run();
}
public int Run() {
string inputFile = "Python.Runtime.il";
string outputFile = "Python.Runtime.il2";
string buff;
string line;
if (!File.Exists(inputFile)) {
Console.WriteLine("{0} does not exist!", inputFile);
return -1;
}
reader = File.OpenText(inputFile);
writer = File.CreateText(outputFile);
while ((line = reader.ReadLine())!= null) {
buff = line.Trim();
if (buff.StartsWith(".class ")) {
ReadClass(line, false);
}
else {
writer.WriteLine(line);
}
}
reader.Close();
writer.Close();
return 0;
}
public void ReadClass(string line, bool nested) {
ArrayList lines = new ArrayList();
bool hasAttr = false;
string data;
string buff;
if (!nested) {
lines.Add(line);
}
while ((data = reader.ReadLine()) != null) {
buff = data.Trim();
if (buff.StartsWith(".class ")) {
WriteBuffer(lines);
writer.WriteLine(data);
ReadClass(data, true);
lines = new ArrayList();
}
else if (buff.StartsWith(ccAttr)) {
hasAttr = true;
lines.Add(data);
}
else if ( (!hasAttr) && buff.StartsWith(".method ")) {
WriteBuffer(lines);
ReadMethod(data);
lines = new ArrayList();
}
else if (buff.StartsWith("} // end of class")) {
WriteBuffer(lines);
writer.WriteLine(data);
return;
}
else if (hasAttr && buff.StartsWith("Invoke(")) {
WriteBuffer(lines);
writer.WriteLine(modOpt);
writer.WriteLine(data);
lines = new ArrayList();
}
else {
lines.Add(data);
}
}
}
public void ReadMethod(string line) {
ArrayList lines = new ArrayList();
string mline = line;
string data;
string buff;
while ((data = reader.ReadLine()) != null) {
buff = data.Trim();
if (buff.StartsWith(ccAttr)) {
writer.WriteLine(mline);
writer.WriteLine(modOpt);
WriteBuffer(lines);
writer.WriteLine(data);
return;
}
else if (buff.StartsWith("} // end of method")) {
writer.WriteLine(mline);
WriteBuffer(lines);
writer.WriteLine(data);
return;
}
lines.Add(data);
}
}
public void WriteBuffer(ArrayList data) {
IEnumerator iter = data.GetEnumerator();
while (iter.MoveNext()) {
writer.WriteLine((String)iter.Current);
}
}
}