Repository navigation
Expand file tree
/
Copy pathField.cs
More file actions
276 lines (234 loc) · 9.71 KB
/
Copy pathField.cs
File metadata and controls
276 lines (234 loc) · 9.71 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
272
273
274
275
276
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* [email protected]. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
#if FEATURE_NATIVE
using System;
using System.Diagnostics;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
#if CLR2
using Microsoft.Scripting.Math;
#else
using System.Numerics;
#endif
namespace IronPython.Modules {
/// <summary>
/// Provides support for interop with native code from Python code.
/// </summary>
public static partial class CTypes {
/// <summary>
/// Fields are created when a Structure is defined and provide
/// introspection of the structure.
/// </summary>
[PythonType, PythonHidden]
public sealed class Field : PythonTypeDataSlot, ICodeFormattable {
private readonly INativeType _fieldType;
private readonly int _offset, _index, _bits = -1, _bitsOffset;
private readonly string _fieldName;
internal Field(string fieldName, INativeType fieldType, int offset, int index) {
_offset = offset;
_fieldType = fieldType;
_index = index;
_fieldName = fieldName;
}
internal Field(string fieldName, INativeType fieldType, int offset, int index, int? bits, int? bitOffset) {
_offset = offset;
_fieldType = fieldType;
_index = index;
_fieldName = fieldName;
if (bits != null) {
_bits = bits.Value;
_bitsOffset = bitOffset.Value;
}
}
public int offset {
get {
return _offset;
}
}
public int size {
get {
return _fieldType.Size;
}
}
#region Internal APIs
internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value) {
if (instance != null) {
CData inst = (CData)instance;
value = _fieldType.GetValue(inst._memHolder, inst, _offset, false);
if (_bits == -1) {
return true;
}
value = ExtractBits(value);
return true;
}
value = this;
return true;
}
internal override bool GetAlwaysSucceeds {
get {
return true;
}
}
internal override bool TrySetValue(CodeContext context, object instance, PythonType owner, object value) {
if (instance != null) {
SetValue(((CData)instance)._memHolder, 0, value);
return true;
}
return base.TrySetValue(context, instance, owner, value);
}
internal void SetValue(MemoryHolder address, int baseOffset, object value) {
if (_bits == -1) {
object keepAlive = _fieldType.SetValue(address, baseOffset + _offset, value);
if (keepAlive != null) {
address.AddObject(_index.ToString(), keepAlive);
}
} else {
SetBitsValue(address, baseOffset, value);
}
}
internal override bool TryDeleteValue(CodeContext context, object instance, PythonType owner) {
throw PythonOps.TypeError("cannot delete fields in ctypes structures/unions");
}
internal INativeType NativeType {
get {
return _fieldType;
}
}
internal int? BitCount {
get {
if (_bits == -1) {
return null;
}
return _bits;
}
}
internal string FieldName {
get {
return _fieldName;
}
}
#endregion
#region ICodeFormattable Members
public string __repr__(CodeContext context) {
if (_bits == -1) {
return String.Format("<Field type={0}, ofs={1}, size={2}>", ((PythonType)_fieldType).Name, offset, size);
}
return String.Format("<Field type={0}, ofs={1}:{2}, bits={3}>", ((PythonType)_fieldType).Name, offset, _bitsOffset, _bits);
}
#endregion
#region Private implementation
/// <summary>
/// Called for fields which have been limited to a range of bits. Given the
/// value for the full type this extracts the individual bits.
/// </summary>
private object ExtractBits(object value) {
if (value is int) {
int validBits = ((1 << _bits) - 1);
int iVal = (int)value;
iVal = (iVal >> _bitsOffset) & validBits;
if (IsSignedType) {
// need to sign extend if high bit is set
if ((iVal & (1 << (_bits - 1))) != 0) {
iVal |= (-1) ^ validBits;
}
}
value = ScriptingRuntimeHelpers.Int32ToObject(iVal);
} else {
Debug.Assert(value is BigInteger); // we only return ints or big ints from GetValue
ulong validBits = (1UL << _bits) - 1;
BigInteger biVal = (BigInteger)value;
ulong bits;
if (IsSignedType) {
bits = (ulong)(long)biVal;
} else {
bits = (ulong)biVal;
}
bits = (bits >> _bitsOffset) & validBits;
if (IsSignedType) {
// need to sign extend if high bit is set
if ((bits & (1UL << (_bits - 1))) != 0) {
bits |= ulong.MaxValue ^ validBits;
}
value = (BigInteger)(long)bits;
} else {
value = (BigInteger)bits;
}
}
return value;
}
/// <summary>
/// Called for fields which have been limited to a range of bits. Sets the
/// specified value into the bits for the field.
/// </summary>
private void SetBitsValue(MemoryHolder address, int baseOffset, object value) {
// get the value in the form of a ulong which can contain the biggest bitfield
ulong newBits;
if (value is int) {
newBits = (ulong)(int)value;
} else if (value is BigInteger) {
newBits = (ulong)(long)(BigInteger)value;
} else {
throw PythonOps.TypeErrorForTypeMismatch("int or long", value);
}
// do the same for the existing value
int offset = checked(_offset + baseOffset);
object curValue = _fieldType.GetValue(address, null, offset, false);
ulong valueBits;
if (curValue is int) {
valueBits = (ulong)(int)curValue;
} else {
valueBits = (ulong)(long)(BigInteger)curValue;
}
// get a mask for the bits this field owns
ulong targetBits = ((1UL << _bits) - 1) << _bitsOffset;
// clear the existing bits
valueBits &= ~targetBits;
// or in the new bits provided by the user
valueBits |= (newBits << _bitsOffset) & targetBits;
// and set the value
if (IsSignedType) {
if (_fieldType.Size <= 4) {
_fieldType.SetValue(address, offset, (int)(long)valueBits);
} else {
_fieldType.SetValue(address, offset, (BigInteger)(long)valueBits);
}
} else {
if (_fieldType.Size < 4) {
_fieldType.SetValue(address, offset, (int)valueBits);
} else {
_fieldType.SetValue(address, offset, (BigInteger)valueBits);
}
}
}
private bool IsSignedType {
get {
switch (((SimpleType)_fieldType)._type) {
case SimpleTypeKind.SignedByte:
case SimpleTypeKind.SignedInt:
case SimpleTypeKind.SignedLong:
case SimpleTypeKind.SignedLongLong:
case SimpleTypeKind.SignedShort:
return true;
}
return false;
}
}
#endregion
}
}
}
#endif