forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionType.cs
More file actions
210 lines (173 loc) · 7.84 KB
/
UnionType.cs
File metadata and controls
210 lines (173 loc) · 7.84 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
// 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;
using System.Collections.Generic;
using System.Reflection.Emit;
using Microsoft.Scripting;
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 unions.
/// </summary>
[PythonType, PythonHidden]
public class UnionType : PythonType, INativeType {
internal Field[] _fields;
private int _size, _alignment;
public UnionType(CodeContext/*!*/ context, string name, PythonTuple bases, PythonDictionary members)
: base(context, name, bases, members) {
object fields;
if (members.TryGetValue("_fields_", out fields)) {
SetFields(fields);
}
}
public new void __setattr__(CodeContext/*!*/ context, string name, object value) {
if (name == "_fields_") {
lock (this) {
if (_fields != null) {
throw PythonOps.AttributeError("_fields_ is final");
}
SetFields(value);
}
}
base.__setattr__(context, name, value);
}
private UnionType(Type underlyingSystemType)
: base(underlyingSystemType) {
}
/// <summary>
/// Converts an object into a function call parameter.
/// </summary>
public object from_param(object obj) {
return null;
}
internal static PythonType MakeSystemType(Type underlyingSystemType) {
return PythonType.SetPythonType(underlyingSystemType, new UnionType(underlyingSystemType));
}
public static ArrayType/*!*/ operator *(UnionType type, int count) {
return MakeArrayType(type, count);
}
public static ArrayType/*!*/ operator *(int count, UnionType type) {
return MakeArrayType(type, count);
}
public _Union from_buffer(CodeContext/*!*/ context, object/*?*/ data, int offset = 0) {
_Union res = (_Union)CreateInstance(context);
res.InitializeFromBuffer(data, offset, ((INativeType)this).Size);
return res;
}
public _Union from_buffer_copy(CodeContext/*!*/ context, object/*?*/ data, int offset = 0) {
_Union res = (_Union)CreateInstance(context);
res.InitializeFromBufferCopy(data, offset, ((INativeType)this).Size);
return res;
}
#region INativeType Members
int INativeType.Size {
get {
return _size;
}
}
int INativeType.Alignment {
get {
return _alignment;
}
}
object INativeType.GetValue(MemoryHolder owner, object readingFrom, int offset, bool raw) {
_Union res = (_Union)CreateInstance(this.Context.SharedContext);
res.MemHolder = owner.GetSubBlock(offset);
return res;
}
object INativeType.SetValue(MemoryHolder address, int offset, object value) {
IList<object> init = value as IList<object>;
if (init != null) {
if (init.Count > _fields.Length) {
throw PythonOps.TypeError("too many initializers");
}
for (int i = 0; i < init.Count; i++) {
_fields[i].SetValue(address, offset, init[i]);
}
} else {
CData data = value as CData;
if (data != null) {
data.MemHolder.CopyTo(address, offset, data.Size);
return data.MemHolder.EnsureObjects();
} else {
throw new NotImplementedException("Union set value");
}
}
return null;
}
Type INativeType.GetNativeType() {
return GetMarshalTypeFromSize(_size);
}
MarshalCleanup INativeType.EmitMarshalling(ILGenerator/*!*/ method, LocalOrArg argIndex, List<object>/*!*/ constantPool, int constantPoolArgument) {
Type argumentType = argIndex.Type;
argIndex.Emit(method);
if (argumentType.IsValueType) {
method.Emit(OpCodes.Box, argumentType);
}
constantPool.Add(this);
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(nameof(ModuleOps.CheckCDataType)));
method.Emit(OpCodes.Call, typeof(CData).GetProperty(nameof(CData.UnsafeAddress)).GetGetMethod());
method.Emit(OpCodes.Ldobj, ((INativeType)this).GetNativeType());
return null;
}
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 "B";
}
}
#endregion
private void SetFields(object fields) {
lock (this) {
IList<object> list = GetFieldsList(fields);
IList<object> anonFields = StructType.GetAnonymousFields(this);
int size = 0, alignment = 1;
List<Field> allFields = new List<Field>();//GetBaseSizeAlignmentAndFields(out size, out alignment);
int? bitCount;
for (int fieldIndex = 0; fieldIndex < list.Count; fieldIndex++) {
object o = list[fieldIndex];
string fieldName;
INativeType cdata;
GetFieldInfo(this, o, out fieldName, out cdata, out bitCount);
alignment = Math.Max(alignment, cdata.Alignment);
size = Math.Max(size, cdata.Size);
Field newField = new Field(fieldName, cdata, 0, allFields.Count);
allFields.Add(newField);
AddSlot(fieldName, newField);
if (anonFields != null && anonFields.Contains(fieldName)) {
StructType.AddAnonymousFields(this, allFields, cdata, newField);
}
}
StructType.CheckAnonymousFields(allFields, anonFields);
_fields = allFields.ToArray();
_size = PythonStruct.Align(size, alignment);
_alignment = alignment;
}
}
internal void EnsureFinal() {
if (_fields == null) {
SetFields(PythonTuple.EMPTY);
}
}
}
}
}
#endif