diff --git a/src/fsharp/FSharp.Core/nativeptr.fs b/src/fsharp/FSharp.Core/nativeptr.fs index bb70928c47c..29e2ecb67ae 100644 --- a/src/fsharp/FSharp.Core/nativeptr.fs +++ b/src/fsharp/FSharp.Core/nativeptr.fs @@ -49,3 +49,6 @@ module NativePtr = [] let inline stackalloc (count:int) : nativeptr<'T> = (# "localloc" (count * sizeof<'T>) : nativeptr<'T> #) + [] + [] + let inline toByRef (address: nativeptr<'T>) : byref<'T> = (# "" address : 'T byref #) diff --git a/src/fsharp/FSharp.Core/nativeptr.fsi b/src/fsharp/FSharp.Core/nativeptr.fsi index 0a5b959ff63..47a0509e71f 100644 --- a/src/fsharp/FSharp.Core/nativeptr.fsi +++ b/src/fsharp/FSharp.Core/nativeptr.fsi @@ -81,3 +81,11 @@ namespace Microsoft.FSharp.NativeInterop [] [] val inline stackalloc : count:int -> nativeptr<'T> + + /// Converts a given typed native pointer to a managed pointer. + /// The input pointer. + /// The managed pointer. + [] + [] + [] + val inline toByRef : nativeptr<'T> -> byref<'T> diff --git a/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofDateTime01.fs b/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofDateTime01.fs index a58902665f6..19c259bb0c7 100644 --- a/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofDateTime01.fs +++ b/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofDateTime01.fs @@ -15,4 +15,13 @@ module M3 = if not (NativeInterop.NativePtr.get data i = now) then noerr <- false + let later = now.AddDays 1. + for i = 0 to 99 do + let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i) + datai <- later + for i = 0 to 99 do + let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i) + if not (datai = later) then + noerr <- false + (if noerr then 0 else 1) |> exit diff --git a/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofenum01.fs b/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofenum01.fs index d10b2b70512..885f3b88ac3 100644 --- a/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofenum01.fs +++ b/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofenum01.fs @@ -19,4 +19,13 @@ module M6 = if not (NativeInterop.NativePtr.get data i = (if (i % 2)=0 then E.A else E.B)) then noerr <- false + for i = 0 to 9 do + let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i) + datai <- (if (i % 2)=1 then E.A else E.B) + + for i = 0 to 9 do + let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i) + if not (datai = (if (i % 2)=1 then E.A else E.B)) then + noerr <- false + (if noerr then 0 else 1) |> exit diff --git a/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofint01.fs b/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofint01.fs index a882d32448c..e81be3fa1b2 100644 --- a/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofint01.fs +++ b/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofint01.fs @@ -16,5 +16,14 @@ module M1 = if not (NativeInterop.NativePtr.get data i = (i*i)) then noerr <- false + for i = 0 to 99 do + let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i) + datai <- 1-i + + for i = 0 to 99 do + let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i) + if not (datai = 1-i) then + noerr <- false + (if noerr then 0 else 1) |> exit diff --git a/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofint6401.fs b/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofint6401.fs index 182b80f3e42..401855c5de5 100644 --- a/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofint6401.fs +++ b/tests/fsharpqa/Source/Libraries/Core/NativeInterop/stackalloc/ofint6401.fs @@ -14,5 +14,14 @@ module M2 = if not (NativeInterop.NativePtr.get data i = (int64 (i*i))) then noerr <- false + for i = 0 to 99 do + let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i) + datai <- int64 (1-i) + + for i = 0 to 99 do + let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i) + if not (datai = int64 (1-i)) then + noerr <- false + (if noerr then 0 else 1) |> exit