forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointerType.cs
More file actions
271 lines (228 loc) · 10.9 KB
/
PointerType.cs
File metadata and controls
271 lines (228 loc) · 10.9 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
#if FEATURE_CTYPES
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Reflection.Emit;
using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
namespace IronPython.Modules {
/// <summary>
/// Provides support for interop with native code from Python code.
/// </summary>
public static partial class CTypes {
/// <summary>
/// The meta class for ctypes pointers.
/// </summary>
[PythonType, PythonHidden]
public class PointerType : PythonType, INativeType {
internal INativeType _type;
private readonly string _typeFormat;
public PointerType(CodeContext/*!*/ context, string name, PythonTuple bases, PythonDictionary members)
: base(context, name, bases, members) {
object type;
if (members.TryGetValue("_type_", out type) && !(type is INativeType)) {
throw PythonOps.TypeError("_type_ must be a type");
}
_type = (INativeType)type;
if (_type != null) {
_typeFormat = _type.TypeFormat;
}
}
private PointerType(Type underlyingSystemType)
: base(underlyingSystemType) {
}
public object from_param([NotNull]CData obj) {
return new NativeArgument((CData)PythonCalls.Call(this, obj), "P");
}
/// <summary>
/// Converts an object into a function call parameter.
/// </summary>
public object from_param(Pointer obj) {
if (obj == null) {
return ScriptingRuntimeHelpers.Int32ToObject(0);
}
if (obj.NativeType != this) {
throw PythonOps.TypeError("assign to pointer of type {0} from {1} is not valid", Name, ((PythonType)obj.NativeType).Name);
}
Pointer res = (Pointer)PythonCalls.Call(this);
res._memHolder.WriteIntPtr(0, obj._memHolder.ReadMemoryHolder(0));
return res;
}
public object from_param([NotNull]NativeArgument obj) {
return (CData)PythonCalls.Call(this, obj._obj);
}
/// <summary>
/// Access an instance at the specified address
/// </summary>
public object from_address(object obj) {
throw new NotImplementedException("pointer from address");
}
public void set_type(PythonType type) {
_type = (INativeType)type;
}
internal static PythonType MakeSystemType(Type underlyingSystemType) {
return PythonType.SetPythonType(underlyingSystemType, new PointerType(underlyingSystemType));
}
public static ArrayType/*!*/ operator *(PointerType type, int count) {
return MakeArrayType(type, count);
}
public static ArrayType/*!*/ operator *(int count, PointerType type) {
return MakeArrayType(type, count);
}
#region INativeType Members
int INativeType.Size {
get {
return IntPtr.Size;
}
}
int INativeType.Alignment {
get {
return IntPtr.Size;
}
}
object INativeType.GetValue(MemoryHolder owner, object readingFrom, int offset, bool raw) {
if (!raw) {
Pointer res = (Pointer)PythonCalls.Call(Context.SharedContext, this);
res._memHolder.WriteIntPtr(0, owner.ReadIntPtr(offset));
res._memHolder.AddObject(offset, readingFrom);
return res;
}
return owner.ReadIntPtr(offset).ToPython();
}
object INativeType.SetValue(MemoryHolder address, int offset, object value) {
Pointer ptr;
_Array array;
if (value == null) {
address.WriteIntPtr(offset, IntPtr.Zero);
} else if (value is int) {
address.WriteIntPtr(offset, new IntPtr((int)value));
} else if (value is BigInteger) {
address.WriteIntPtr(offset, new IntPtr((long)(BigInteger)value));
} else if ((ptr = value as Pointer) != null) {
address.WriteIntPtr(offset, ptr._memHolder.ReadMemoryHolder(0));
return PythonOps.MakeDictFromItems(ptr, "0", ptr._objects, "1");
} else if ((array = value as _Array) != null) {
address.WriteIntPtr(offset, array._memHolder);
return array;
} else {
throw PythonOps.TypeErrorForTypeMismatch(Name, value);
}
return null;
}
Type INativeType.GetNativeType() {
return typeof(IntPtr);
}
MarshalCleanup INativeType.EmitMarshalling(ILGenerator/*!*/ method, LocalOrArg argIndex, List<object>/*!*/ constantPool, int constantPoolArgument) {
Type argumentType = argIndex.Type;
Label nextTry = method.DefineLabel();
Label done = method.DefineLabel();
if (!argumentType.IsValueType) {
argIndex.Emit(method);
method.Emit(OpCodes.Ldnull);
method.Emit(OpCodes.Bne_Un, nextTry);
method.Emit(OpCodes.Ldc_I4_0);
method.Emit(OpCodes.Conv_I);
method.Emit(OpCodes.Br, done);
}
method.MarkLabel(nextTry);
nextTry = method.DefineLabel();
argIndex.Emit(method);
if (argumentType.IsValueType) {
method.Emit(OpCodes.Box, argumentType);
}
constantPool.Add(this);
SimpleType st = _type as SimpleType;
MarshalCleanup res = null;
if (st != null && !argIndex.Type.IsValueType) {
if (st._type == SimpleTypeKind.Char || st._type == SimpleTypeKind.WChar) {
if (st._type == SimpleTypeKind.Char) {
SimpleType.TryToCharPtrConversion(method, argIndex, argumentType, done);
} else {
SimpleType.TryArrayToWCharPtrConversion(method, argIndex, argumentType, done);
}
Label notStr = method.DefineLabel();
LocalOrArg str = argIndex;
if (argumentType != typeof(string)) {
LocalBuilder lb = method.DeclareLocal(typeof(string));
method.Emit(OpCodes.Isinst, typeof(string));
method.Emit(OpCodes.Brfalse, notStr);
argIndex.Emit(method);
method.Emit(OpCodes.Castclass, typeof(string));
method.Emit(OpCodes.Stloc, lb);
method.Emit(OpCodes.Ldloc, lb);
str = new Local(lb);
}
if (st._type == SimpleTypeKind.Char) {
res = SimpleType.MarshalCharPointer(method, str);
} else {
SimpleType.MarshalWCharPointer(method, str);
}
method.Emit(OpCodes.Br, done);
method.MarkLabel(notStr);
argIndex.Emit(method);
}
}
// native argument being pased (byref)
method.Emit(OpCodes.Ldarg, constantPoolArgument);
method.Emit(OpCodes.Ldc_I4, constantPool.Count - 1);
method.Emit(OpCodes.Ldelem_Ref);
method.Emit(OpCodes.Call, typeof(ModuleOps).GetMethod("CheckNativeArgument"));
method.Emit(OpCodes.Dup);
method.Emit(OpCodes.Brfalse, nextTry);
method.Emit(OpCodes.Call, typeof(CData).GetMethod("get_UnsafeAddress"));
method.Emit(OpCodes.Br, done);
// lone cdata being passed
method.MarkLabel(nextTry);
nextTry = method.DefineLabel();
method.Emit(OpCodes.Pop); // extra null native arg
argIndex.Emit(method);
if (argumentType.IsValueType) {
method.Emit(OpCodes.Box, argumentType);
}
method.Emit(OpCodes.Ldarg, constantPoolArgument);
method.Emit(OpCodes.Ldc_I4, constantPool.Count - 1);
method.Emit(OpCodes.Ldelem_Ref);
method.Emit(OpCodes.Call, typeof(ModuleOps).GetMethod("TryCheckCDataPointerType"));
method.Emit(OpCodes.Dup);
method.Emit(OpCodes.Brfalse, nextTry);
method.Emit(OpCodes.Call, typeof(CData).GetMethod("get_UnsafeAddress"));
method.Emit(OpCodes.Br, done);
// pointer object being passed
method.MarkLabel(nextTry);
method.Emit(OpCodes.Pop); // extra null cdata
argIndex.Emit(method);
if (argumentType.IsValueType) {
method.Emit(OpCodes.Box, argumentType);
}
method.Emit(OpCodes.Ldarg, constantPoolArgument);
method.Emit(OpCodes.Ldc_I4, constantPool.Count - 1);
method.Emit(OpCodes.Ldelem_Ref);
method.Emit(OpCodes.Call, typeof(ModuleOps).GetMethod("CheckCDataType"));
method.Emit(OpCodes.Call, typeof(CData).GetMethod("get_UnsafeAddress"));
method.Emit(OpCodes.Ldind_I);
method.MarkLabel(done);
return res;
}
Type/*!*/ INativeType.GetPythonType() {
return typeof(object);
}
void INativeType.EmitReverseMarshalling(ILGenerator method, LocalOrArg value, List<object> constantPool, int constantPoolArgument) {
value.Emit(method);
EmitCDataCreation(this, method, constantPool, constantPoolArgument);
}
string INativeType.TypeFormat {
get {
return "&" + (_typeFormat ?? _type.TypeFormat);
}
}
#endregion
}
}
}
#endif