Skip to content

Commit d55b4fd

Browse files
committed
AOT flag
1 parent 25cddbb commit d55b4fd

10 files changed

Lines changed: 107 additions & 5 deletions

src/runtime/classderived.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5-
using System.Reflection.Emit;
65
using System.Runtime.InteropServices;
76
using System.Threading.Tasks;
87

8+
#if !AOT
9+
using System.Reflection.Emit;
10+
911
namespace Python.Runtime
1012
{
1113
/// <summary>
@@ -887,3 +889,22 @@ public static void Finalize(IPythonDerivedType obj)
887889
}
888890
}
889891
}
892+
#else
893+
894+
namespace Python.Runtime
895+
{
896+
/// <summary>
897+
/// Managed class that provides the implementation for reflected types.
898+
/// Managed classes and value types are represented in Python by actual
899+
/// Python type objects. Each of those type objects is associated with
900+
/// an instance of ClassObject, which provides its implementation.
901+
/// </summary>
902+
/// <remarks>
903+
/// interface used to identify which C# types were dynamically created as python subclasses
904+
/// </remarks>
905+
public interface IPythonDerivedType
906+
{
907+
}
908+
909+
}
910+
#endif

src/runtime/codegenerator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#if !AOT
12
using System;
23
using System.Reflection;
34
using System.Reflection.Emit;
@@ -44,3 +45,4 @@ internal TypeBuilder DefineType(string name, Type basetype)
4445
}
4546
}
4647
}
48+
#endif //!AOT

src/runtime/delegatemanager.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Collections;
33
using System.Reflection;
4-
using System.Reflection.Emit;
4+
55

66
namespace Python.Runtime
77
{
8+
#if !AOT
9+
using System.Reflection.Emit;
810
/// <summary>
911
/// The DelegateManager class manages the creation of true managed
1012
/// delegate instances that dispatch calls to Python methods.
@@ -280,4 +282,9 @@ public ConversionException(string msg) : base(msg)
280282
{
281283
}
282284
}
285+
#else
286+
internal class DelegateManager
287+
{
288+
}
289+
#endif
283290
}

src/runtime/delegateobject.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
6262
{
6363
return Exceptions.RaiseTypeError("argument must be callable");
6464
}
65-
65+
#if AOT
66+
throw new NotImplementedException();
67+
#else
6668
Delegate d = PythonEngine.DelegateManager.GetDelegate(self.type, method);
6769
return CLRObject.GetInstHandle(d, self.pyHandle);
70+
#endif
6871
}
6972

7073

src/runtime/eventobject.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ internal bool AddEventHandler(IntPtr target, IntPtr handler)
3737
// wrap the Python handler. Note that wrapper delegate creation
3838
// always succeeds, though calling the wrapper may fail.
3939
Type type = info.EventHandlerType;
40+
#if AOT
41+
throw new NotImplementedException();
42+
#else
4043
Delegate d = PythonEngine.DelegateManager.GetDelegate(type, handler);
4144

4245
// Now register the handler in a mapping from instance to pairs
@@ -63,6 +66,7 @@ internal bool AddEventHandler(IntPtr target, IntPtr handler)
6366
mi.Invoke(obj, BindingFlags.Default, null, args, null);
6467

6568
return true;
69+
#endif
6670
}
6771

6872

src/runtime/interop.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@ static Interop()
442442
pmap["bf_getwritebuffer"] = p["IntObjArgFunc"];
443443
pmap["bf_getsegcount"] = p["ObjObjFunc"];
444444
pmap["bf_getcharbuffer"] = p["IntObjArgFunc"];
445+
#if AOT
446+
pmap["__instancecheck__"] = p["ObjObjFunc"];
447+
pmap["__subclasscheck__"] = p["ObjObjFunc"];
448+
pmap["__import__"] = p["TernaryFunc"];
449+
#endif
445450
}
446451

447452
internal static Type GetPrototype(string name)
@@ -461,10 +466,14 @@ internal static IntPtr GetThunk(MethodInfo method, string funcType = null)
461466
{
462467
IntPtr tmp = Marshal.AllocHGlobal(IntPtr.Size);
463468
Delegate d = Delegate.CreateDelegate(dt, method);
469+
#if AOT
470+
fp = Marshal.GetFunctionPointerForDelegate(d);
471+
#else
464472
Thunk cb = new Thunk(d);
465473
Marshal.StructureToPtr(cb, tmp, false);
466474
IntPtr fp = Marshal.ReadIntPtr(tmp, 0);
467475
Marshal.FreeHGlobal(tmp);
476+
#endif
468477
keepAlive.Add(d);
469478
return fp;
470479
}

src/runtime/nativecall.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
11
using System;
22
using System.Reflection;
3-
using System.Reflection.Emit;
43
using System.Runtime.InteropServices;
54
using System.Threading;
65

76
namespace Python.Runtime
87
{
8+
#if AOT
9+
internal class NativeCall
10+
{
11+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
12+
private delegate void Void_1_Delegate(IntPtr a1);
13+
14+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
15+
private delegate int Int_3_Delegate(IntPtr a1, IntPtr a2, IntPtr a3);
16+
17+
public static void Void_Call_1(IntPtr fp, IntPtr a1)
18+
{
19+
((Void_1_Delegate)Marshal.GetDelegateForFunctionPointer(fp, typeof(Void_1_Delegate)))(a1);
20+
}
21+
22+
public static IntPtr Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3)
23+
{
24+
var d = (Interop.TernaryFunc)Marshal.GetDelegateForFunctionPointer(fp, typeof(Interop.TernaryFunc));
25+
return d(a1, a2, a3);
26+
}
27+
28+
29+
public static int Int_Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3)
30+
{
31+
return ((Int_3_Delegate)Marshal.GetDelegateForFunctionPointer(fp, typeof(Int_3_Delegate)))(a1, a2, a3);
32+
}
33+
}
34+
#endif
35+
36+
#if !AOT
37+
using System.Reflection.Emit;
938
/// <summary>
1039
/// Provides support for calling native code indirectly through
1140
/// function pointers. Most of the important parts of the Python
@@ -174,4 +203,5 @@ public interface INativeCall
174203
IntPtr Call_3(IntPtr funcPtr, IntPtr a1, IntPtr a2, IntPtr a3);
175204
}
176205
#endif
206+
#endif
177207
}

src/runtime/pythonengine.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
@@ -163,7 +163,9 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true)
163163
// throws an exception in its ctor. This exception is eaten somehow
164164
// during an initial "import clr", and the world ends shortly thereafter.
165165
// This is probably masking some bad mojo happening somewhere in Runtime.Initialize().
166+
#if !AOT
166167
delegateManager = new DelegateManager();
168+
#endif
167169
Runtime.Initialize();
168170
initialized = true;
169171
Exceptions.Clear();

src/runtime/runtime.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,12 +1237,32 @@ internal static bool PyUnicode_Check(IntPtr ob)
12371237
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
12381238
public static extern IntPtr PyUnicode_FromEncodedObject(IntPtr ob, IntPtr enc, IntPtr err);
12391239

1240+
#if AOT
1241+
[DllImport(_PythonDll,EntryPoint = "PyUnicode_FromKindAndData", CallingConvention = CallingConvention.Cdecl)]
1242+
internal static extern IntPtr _PyUnicode_FromKindAndData(
1243+
int kind,
1244+
IntPtr s,
1245+
int size
1246+
);
1247+
1248+
static IntPtr PyUnicode_FromKindAndData(int kind, string s, int size)
1249+
{
1250+
// Custom marshalers are not currently supported by IL2CPP
1251+
IntPtr mem = UcsMarshaler.GetInstance(s).MarshalManagedToNative(s);
1252+
IntPtr res = _PyUnicode_FromKindAndData(kind,
1253+
UcsMarshaler.GetInstance(s).MarshalManagedToNative(s),
1254+
size);
1255+
Marshal.FreeHGlobal(mem);
1256+
return res;
1257+
}
1258+
#else
12401259
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
12411260
public static extern IntPtr PyUnicode_FromKindAndData(
12421261
int kind,
12431262
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UcsMarshaler))] string s,
12441263
int size
12451264
);
1265+
#endif // AOT
12461266

12471267
internal static IntPtr PyUnicode_FromUnicode(string s, int size)
12481268
{

src/runtime/typemanager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr
242242
return Exceptions.RaiseTypeError("invalid base class, expected CLR class type");
243243
}
244244

245+
#if AOT
246+
throw new NotImplementedException();
247+
#else
245248
try
246249
{
247250
Type subType = ClassDerivedObject.CreateDerivedType(name,
@@ -265,6 +268,7 @@ internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr
265268
{
266269
return Exceptions.RaiseTypeError(e.Message);
267270
}
271+
#endif
268272
}
269273

270274
internal static IntPtr CreateMethodDef(string name, IntPtr func, int flags, string doc = null)

0 commit comments

Comments
 (0)