Skip to content
Browse files

Work on pointers. Add support of IntPtr :> xxx* conversion.

  • Loading branch information...
1 parent 930184e commit 4ea6628c82160d3303d346aaab343f0e19f6f4a5 @VladD2 VladD2 committed Aug 23, 2012
View
3 ncc/generation/ILEmitter.n
@@ -2212,6 +2212,9 @@ namespace Nemerle.Compiler
match (type)
{
+ | Class(_, []) when type.Equals(InternalType.IntPtr) =>
+ emit(expr);
+
| Class(_, []) when type.Equals(InternalType.String) =>
_ = target_type; // TODO: Add check!
View
13 ncc/testsuite/positive/Issue-git-0397.n
@@ -1,4 +1,5 @@
// REFERENCE: Nemerle.Unsafe.dll
+using System.Runtime.InteropServices;
using Nemerle.Text;
using Nemerle.Utility;
@@ -13,7 +14,7 @@ module Program
{
pinned (p1 = str)
{
- WriteLine(p1[1].ToString());
+ WriteLine(p1[2].ToString());
WriteLine((p1[3] : int).ToString());
WriteLine(p1[0]);
UnsafeMethod(p1);
@@ -38,6 +39,7 @@ module Program
{
_ptr = ptr;
WriteLine(*ptr);
+ WriteLine(*(ptr + 1));
}
Test2() : void
@@ -104,16 +106,21 @@ module Program
Test2();
pinned (p = ary)
Test3(p);
+
+ def h = GCHandle.Alloc("qwe", GCHandleType.Pinned);
+ UnsafeMethod(h.AddrOfPinnedObject() :> char*);
+ h.Free();
}
}
/*
BEGIN-OUTPUT
-b
+c
0
a
a
b
+b
bc
for with index
a
@@ -138,5 +145,7 @@ Test3()
3
3
42
+q
+w
END-OUTPUT
*/
View
1 snippets/Nemerle.Unsafe/Nemerle.Unsafe/DerefPointerOperator.n
@@ -8,7 +8,6 @@ using Nemerle.Utility;
using System;
using System.Collections.Generic;
-using System.Linq;
[assembly: Nemerle.Internal.OperatorAttribute ("Nemerle.Unsafe", "*", true, 285, 285)]
//[assembly: Nemerle.Internal.OperatorAttribute ("Nemerle.Unsafe", "*", false, 261, 260)]
View
10 snippets/Nemerle.Unsafe/Test/Main.n
@@ -1,4 +1,5 @@
-using Nemerle.Text;
+using System.Runtime.InteropServices;
+using Nemerle.Text;
using Nemerle.Utility;
using System;
@@ -12,7 +13,7 @@ module Program
{
pinned (p1 = str)
{
- WriteLine(p1[3].ToString());
+ WriteLine(p1[2].ToString());
WriteLine((p1[3] : int).ToString());
WriteLine(p1[0]);
UnsafeMethod(p1);
@@ -37,6 +38,7 @@ module Program
{
_ptr = ptr;
WriteLine(*ptr);
+ WriteLine(*(ptr + 1));
}
Test2() : void
@@ -103,5 +105,9 @@ module Program
Test2();
pinned (p = ary)
Test3(p);
+
+ def h = GCHandle.Alloc("qwe", GCHandleType.Pinned);
+ UnsafeMethod(h.AddrOfPinnedObject() :> char*);
+ h.Free();
}
}
View
60 snippets/Nemerle.Unsafe/UnsafeTest/Program.cs
@@ -21,48 +21,52 @@ byte Test3(byte* ptr)
static void Test4(char* ptr)
{
- var c = ptr[3];
+ var c = ptr[2];
Console.WriteLine(c.ToString());
}
- static void Main(string[] args)
+ static void Main()
{
- fixed (char* p = "abc")
- Test4(p);
+ var h = GCHandle.Alloc("qwe", GCHandleType.Pinned);
+ Test4((char*)(h.AddrOfPinnedObject()));
- return;
- var x = 1;
- //typeof(int).MakePointerType()
- //Console.WriteLine(ary[x]);
+ //fixed (char* p = "abc")
+ // Test4(p);
- //typeof(int).MakePointerType()
+ //return;
+ //var x = 1;
+ ////typeof(int).MakePointerType()
+ ////Console.WriteLine(ary[x]);
- //var handle = GCHandle.Alloc(ary, GCHandleType.Pinned);
- //int* ptr = (int*)handle.AddrOfPinnedObject();
+ ////typeof(int).MakePointerType()
- //System.Diagnostics.Trace.Assert(false);
- //var ptr = ary;
- fixed (int* ptr = &ary[1])
- {
- int* ptr2 = ptr;
- ++ptr2;
- Console.WriteLine(ptr2[x]);
- //handle.Free();
- //handle = GCHandle.Alloc(new int[] { 42, 43, 44 }, GCHandleType.Pinned);
- //ptr = (int*)handle.AddrOfPinnedObject();
- //Console.WriteLine(ptr[x]);
- }
+ ////var handle = GCHandle.Alloc(ary, GCHandleType.Pinned);
+ ////int* ptr = (int*)handle.AddrOfPinnedObject();
-
- //var str = "abcde";
-
- //fixed (Type* ptr = str)
+ ////System.Diagnostics.Trace.Assert(false);
+ ////var ptr = ary;
+ //fixed (int* ptr = &ary[1])
//{
- // Console.WriteLine(ptr[x]);
+ // int* ptr2 = ptr;
+ // ++ptr2;
+ // Console.WriteLine(ptr2[x]);
+
+ // //handle.Free();
+ // //handle = GCHandle.Alloc(new int[] { 42, 43, 44 }, GCHandleType.Pinned);
+ // //ptr = (int*)handle.AddrOfPinnedObject();
+ // //Console.WriteLine(ptr[x]);
//}
+
+
+ ////var str = "abcde";
+
+ ////fixed (Type* ptr = str)
+ ////{
+ //// Console.WriteLine(ptr[x]);
+ ////}
}
}
}

1 comment on commit 4ea6628

@emperon

Great!!

Please sign in to comment.
Something went wrong with that request. Please try again.