Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe4c481

Browse files
committed
fixed uses of Marshal.Read/Marshal.Write overloads with first argument of type object
we will need a diagnostic for it
1 parent 0728e21 commit fe4c481

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

src/embed_tests/TestPyType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void CanCreateHeapType()
3131
using var doc = new StrPtr(docStr, Encoding.UTF8);
3232
var spec = new TypeSpec(
3333
name: name,
34-
basicSize: Marshal.ReadInt32(Runtime.Runtime.PyBaseObjectType, TypeOffset.tp_basicsize),
34+
basicSize: Util.ReadInt32(Runtime.Runtime.PyBaseObjectType, TypeOffset.tp_basicsize),
3535
slots: new TypeSpec.Slot[] {
3636
new (TypeSlotID.tp_doc, doc.RawPointer),
3737
},

src/runtime/debughelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ internal static void DumpType(BorrowedReference type)
5252
objMember = Util.ReadRef(type, TypeOffset.tp_bases);
5353
Print(" bases: ", objMember);
5454

55-
//op = Marshal.ReadIntPtr(type, TypeOffset.tp_mro);
55+
//op = Util.ReadIntPtr(type, TypeOffset.tp_mro);
5656
//DebugUtil.Print(" mro: ", op);
5757

5858

src/runtime/extensiontype.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public ExtensionType()
2020

2121
BorrowedReference tp = TypeManager.GetTypeReference(GetType());
2222

23-
//int rc = (int)Marshal.ReadIntPtr(tp, TypeOffset.ob_refcnt);
23+
//int rc = (int)Util.ReadIntPtr(tp, TypeOffset.ob_refcnt);
2424
//if (rc > 1050)
2525
//{
2626
// DebugUtil.Print("tp is: ", tp);

src/runtime/managedtype.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,10 @@ internal static void GetGCHandle(BorrowedReference reflectedClrObject, BorrowedR
275275
Debug.Assert(IsManagedType(type) || type == Runtime.CLRMetaType);
276276
Debug.Assert(Runtime.PyObject_TypeCheck(reflectedClrObject, type));
277277

278-
int gcHandleOffset = Marshal.ReadInt32(type.DangerousGetAddress(), Offsets.tp_clr_inst_offset);
278+
int gcHandleOffset = Util.ReadInt32(type, Offsets.tp_clr_inst_offset);
279279
Debug.Assert(gcHandleOffset > 0);
280280

281-
handle = Marshal.ReadIntPtr(reflectedClrObject.DangerousGetAddress(), gcHandleOffset);
281+
handle = Util.ReadIntPtr(reflectedClrObject, gcHandleOffset);
282282
}
283283

284284
internal static GCHandle? TryGetGCHandle(BorrowedReference reflectedClrObject, BorrowedReference type)
@@ -313,10 +313,10 @@ internal static void SetGCHandle(BorrowedReference reflectedClrObject, BorrowedR
313313
Debug.Assert(reflectedClrObject != null);
314314
Debug.Assert(Runtime.PyObject_TypeCheck(reflectedClrObject, type));
315315

316-
int offset = Marshal.ReadInt32(type.DangerousGetAddress(), Offsets.tp_clr_inst_offset);
316+
int offset = Util.ReadInt32(type, Offsets.tp_clr_inst_offset);
317317
Debug.Assert(offset > 0);
318318

319-
Marshal.WriteIntPtr(reflectedClrObject.DangerousGetAddress(), offset, (IntPtr)newHandle);
319+
Util.WriteIntPtr(reflectedClrObject, offset, (IntPtr)newHandle);
320320
}
321321
internal static void SetGCHandle(BorrowedReference reflectedClrObject, GCHandle newHandle)
322322
=> SetGCHandle(reflectedClrObject, Runtime.PyObject_TYPE(reflectedClrObject), newHandle);
@@ -325,7 +325,7 @@ internal static class Offsets
325325
{
326326
static Offsets()
327327
{
328-
int pyTypeSize = Marshal.ReadInt32(Runtime.PyTypeType, TypeOffset.tp_basicsize);
328+
int pyTypeSize = Util.ReadInt32(Runtime.PyTypeType, TypeOffset.tp_basicsize);
329329
if (pyTypeSize < 0) throw new InvalidOperationException();
330330

331331
tp_clr_inst_offset = pyTypeSize;

src/runtime/platform/LibraryLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void ClearError()
9292
libDL.dlerror();
9393
}
9494

95-
string GetError()
95+
string? GetError()
9696
{
9797
var res = libDL.dlerror();
9898
if (res != IntPtr.Zero)

src/runtime/typemanager.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ static BorrowedReference InitializeBases(PyType pyType, PyTuple baseTuple)
312312

313313
if (baseTuple.Length() > 1)
314314
{
315-
Marshal.WriteIntPtr(pyType.Handle, TypeOffset.tp_bases, baseTuple.NewReferenceOrNull().DangerousMoveToPointer());
315+
Util.WriteIntPtr(pyType, TypeOffset.tp_bases, baseTuple.NewReferenceOrNull().DangerousMoveToPointer());
316316
}
317317
return primaryBase;
318318
}
@@ -323,7 +323,7 @@ static void InitializeCoreFields(PyType type)
323323

324324
if (ManagedType.IsManagedType(type.BaseReference))
325325
{
326-
int baseClrInstOffset = Marshal.ReadInt32(type.BaseReference.DangerousGetAddress(), ManagedType.Offsets.tp_clr_inst_offset);
326+
int baseClrInstOffset = Util.ReadInt32(type.BaseReference, ManagedType.Offsets.tp_clr_inst_offset);
327327
Util.WriteInt32(type, ManagedType.Offsets.tp_clr_inst_offset, baseClrInstOffset);
328328
}
329329
else
@@ -344,7 +344,7 @@ static void InitializeClass(PyType type, ClassBase impl, Type clrType)
344344
SlotsHolder slotsHolder = CreateSolotsHolder(type);
345345
InitializeSlots(type, impl.GetType(), slotsHolder);
346346

347-
if (Marshal.ReadIntPtr(type, TypeOffset.mp_length) == IntPtr.Zero
347+
if (Util.ReadIntPtr(type, TypeOffset.mp_length) == IntPtr.Zero
348348
&& mp_length_slot.CanAssign(clrType))
349349
{
350350
InitializeSlot(type, TypeOffset.mp_length, mp_length_slot.Method, slotsHolder);
@@ -381,7 +381,7 @@ static void InitializeClass(PyType type, ClassBase impl, Type clrType)
381381
throw PythonException.ThrowLastAsClrException();
382382
}
383383

384-
var dict = new BorrowedReference(Marshal.ReadIntPtr(type, TypeOffset.tp_dict));
384+
var dict = Util.ReadRef(type, TypeOffset.tp_dict);
385385
string mn = clrType.Namespace ?? "";
386386
using (var mod = Runtime.PyString_FromString(mn))
387387
Runtime.PyDict_SetItem(dict, PyIdentifier.__module__, mod.Borrow());
@@ -410,7 +410,7 @@ static int InheritOrAllocateStandardFields(BorrowedReference typeRef, BorrowedRe
410410
{
411411
IntPtr baseAddress = @base.DangerousGetAddress();
412412
IntPtr type = typeRef.DangerousGetAddress();
413-
int baseSize = Marshal.ReadInt32(baseAddress, TypeOffset.tp_basicsize);
413+
int baseSize = Util.ReadInt32(@base, TypeOffset.tp_basicsize);
414414
int newFieldOffset = baseSize;
415415

416416
void InheritOrAllocate(int typeField)
@@ -538,7 +538,7 @@ internal static IntPtr WriteMethodDef(IntPtr mdef, IntPtr name, IntPtr func, int
538538
}
539539

540540
internal static IntPtr WriteMethodDef(IntPtr mdef, string name, IntPtr func, int flags = 0x0001,
541-
string doc = null)
541+
string? doc = null)
542542
{
543543
IntPtr namePtr = Marshal.StringToHGlobalAnsi(name);
544544
IntPtr docPtr = doc != null ? Marshal.StringToHGlobalAnsi(doc) : IntPtr.Zero;
@@ -581,7 +581,7 @@ internal static PyType CreateMetaType(Type impl, out SlotsHolder slotsHolder)
581581
PyType py_type = Runtime.PyTypeType;
582582
Util.WriteRef(type, TypeOffset.tp_base, new NewReference(py_type).Steal());
583583

584-
int size = Marshal.ReadInt32(Runtime.PyTypeType, TypeOffset.tp_basicsize)
584+
int size = Util.ReadInt32(Runtime.PyTypeType, TypeOffset.tp_basicsize)
585585
+ IntPtr.Size // tp_clr_inst_offset
586586
+ IntPtr.Size // tp_clr_inst
587587
;
@@ -641,7 +641,7 @@ internal static SlotsHolder SetupMetaSlots(Type impl, PyType type)
641641
{
642642
slotsHolder.Set(TypeOffset.tp_methods, (t, offset) =>
643643
{
644-
var p = Marshal.ReadIntPtr(t, offset);
644+
var p = Util.ReadIntPtr(t, offset);
645645
Runtime.PyMem_Free(p);
646646
Util.WriteIntPtr(t, offset, IntPtr.Zero);
647647
});

0 commit comments

Comments
 (0)