fixed tests.

This commit is contained in:
2025-11-30 16:35:14 -07:00
parent c5c4cfdc64
commit 5db31d087d
2 changed files with 101 additions and 96 deletions

View File

@@ -15,76 +15,82 @@
#pragma warning disable SA1500, SA1505, SA1507, #pragma warning disable SA1500, SA1505, SA1507,
#pragma warning disable SA1600, SA1601, SA1604, SA1605, SA1611, SA1615, SA1649, #pragma warning disable SA1600, SA1601, SA1604, SA1605, SA1611, SA1615, SA1649,
namespace Slang { namespace Slang
using System; {
using System.Runtime.InteropServices; using System;
using System.Runtime.InteropServices;
public unsafe partial class Ffi { public unsafe partial class Ffi
{
#if IOS #if IOS
private const string RustLib = "slang.framework/slang"; private const string RustLib = "slang.framework/slang";
#else #else
public const string RustLib = "slang_compiler.dll"; public const string RustLib = "slang_compiler.dll";
#endif #endif
} }
/// <summary> /// <summary>
/// <c>&'lt [T]</c> but with a guaranteed <c>#[repr(C)]</c> layout. /// <c>&'lt [T]</c> but with a guaranteed <c>#[repr(C)]</c> layout.
/// ///
/// # C layout (for some given type T) /// # C layout (for some given type T)
/// ///
/// ```c /// ```c
/// typedef struct { /// typedef struct {
/// // Cannot be NULL /// // Cannot be NULL
/// T * ptr; /// T * ptr;
/// size_t len; /// size_t len;
/// } slice_T; /// } slice_T;
/// ``` /// ```
/// ///
/// # Nullable pointer? /// # Nullable pointer?
/// ///
/// If you want to support the above typedef, but where the <c>ptr</c> field is /// If you want to support the above typedef, but where the <c>ptr</c> field is
/// allowed to be <c>NULL</c> (with the contents of <c>len</c> then being undefined) /// allowed to be <c>NULL</c> (with the contents of <c>len</c> then being undefined)
/// use the <c>Option< slice_ptr<_> ></c> type. /// use the <c>Option< slice_ptr<_> ></c> type.
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential, Size = 16)] [StructLayout(LayoutKind.Sequential, Size = 16)]
public unsafe struct slice_ref_uint16_t { public unsafe struct slice_ref_uint16_t
{
/// <summary> /// <summary>
/// Pointer to the first element (if any). /// Pointer to the first element (if any).
/// </summary> /// </summary>
public UInt16 /*const*/ * ptr; public UInt16 /*const*/
* ptr;
/// <summary> /// <summary>
/// Element count /// Element count
/// </summary> /// </summary>
public UIntPtr len; public UIntPtr len;
} }
/// <summary> /// <summary>
/// Same as [<c>Vec<T></c>][<c>rust::Vec</c>], but with guaranteed <c>#[repr(C)]</c> layout /// Same as [<c>Vec<T></c>][<c>rust::Vec</c>], but with guaranteed <c>#[repr(C)]</c> layout
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential, Size = 24)] [StructLayout(LayoutKind.Sequential, Size = 24)]
public unsafe struct Vec_uint8_t { public unsafe struct Vec_uint8_t
public byte * ptr; {
public byte* ptr;
public UIntPtr len; public UIntPtr len;
public UIntPtr cap; public UIntPtr cap;
} }
public unsafe partial class Ffi { public unsafe partial class Ffi
{
/// <summary> /// <summary>
/// C# handles strings as UTF16. We do NOT want to allocate that memory in C# because /// C# handles strings as UTF16. We do NOT want to allocate that memory in C# because
/// we want to avoid GC. So we pass it to Rust to handle all the memory allocations. /// we want to avoid GC. So we pass it to Rust to handle all the memory allocations.
/// This should result in the ability to compile many times without triggering frame drops /// This should result in the ability to compile many times without triggering frame drops
/// from the GC from a <c>GetBytes()</c> call on a string in C#. /// from the GC from a <c>GetBytes()</c> call on a string in C#.
/// </summary> /// </summary>
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern [DllImport(RustLib, ExactSpelling = true)]
Vec_uint8_t compile_from_string ( public static extern unsafe Vec_uint8_t compile_from_string(slice_ref_uint16_t input);
slice_ref_uint16_t input); }
}
[StructLayout(LayoutKind.Sequential, Size = 64)] [StructLayout(LayoutKind.Sequential, Size = 64)]
public unsafe struct FfiToken_t { public unsafe struct FfiToken_t
{
public Vec_uint8_t tooltip; public Vec_uint8_t tooltip;
public Vec_uint8_t error; public Vec_uint8_t error;
@@ -94,43 +100,42 @@ public unsafe struct FfiToken_t {
public Int32 length; public Int32 length;
public UInt32 token_kind; public UInt32 token_kind;
} }
/// <summary> /// <summary>
/// Same as [<c>Vec<T></c>][<c>rust::Vec</c>], but with guaranteed <c>#[repr(C)]</c> layout /// Same as [<c>Vec<T></c>][<c>rust::Vec</c>], but with guaranteed <c>#[repr(C)]</c> layout
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential, Size = 24)] [StructLayout(LayoutKind.Sequential, Size = 24)]
public unsafe struct Vec_FfiToken_t { public unsafe struct Vec_FfiToken_t
public FfiToken_t * ptr; {
public FfiToken_t* ptr;
public UIntPtr len; public UIntPtr len;
public UIntPtr cap; public UIntPtr cap;
} }
public unsafe partial class Ffi { public unsafe partial class Ffi
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern {
void free_ffi_token_vec ( [DllImport(RustLib, ExactSpelling = true)]
Vec_FfiToken_t v); public static extern unsafe void free_ffi_token_vec(Vec_FfiToken_t v);
} }
public unsafe partial class Ffi { public unsafe partial class Ffi
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern {
void free_string ( [DllImport(RustLib, ExactSpelling = true)]
Vec_uint8_t s); public static extern unsafe void free_string(Vec_uint8_t s);
} }
public unsafe partial class Ffi { public unsafe partial class Ffi
{
/// <summary> /// <summary>
/// C# handles strings as UTF16. We do NOT want to allocate that memory in C# because /// C# handles strings as UTF16. We do NOT want to allocate that memory in C# because
/// we want to avoid GC. So we pass it to Rust to handle all the memory allocations. /// we want to avoid GC. So we pass it to Rust to handle all the memory allocations.
/// This should result in the ability to tokenize many times without triggering frame drops /// This should result in the ability to tokenize many times without triggering frame drops
/// from the GC from a <c>GetBytes()</c> call on a string in C#. /// from the GC from a <c>GetBytes()</c> call on a string in C#.
/// </summary> /// </summary>
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern [DllImport(RustLib, ExactSpelling = true)]
Vec_FfiToken_t tokenize_line ( public static extern unsafe Vec_FfiToken_t tokenize_line(slice_ref_uint16_t input);
slice_ref_uint16_t input); }
}
} /* Slang */ } /* Slang */

View File

@@ -92,7 +92,7 @@ fn test_priority_expression() -> Result<()> {
let expression = parser.parse()?.unwrap(); let expression = parser.parse()?.unwrap();
assert_eq!("(let x = (4))", expression.to_string()); assert_eq!("(let x = 4)", expression.to_string());
Ok(()) Ok(())
} }
@@ -109,7 +109,7 @@ fn test_binary_expression() -> Result<()> {
assert_eq!("(((45 * 2) - (15 / 5)) + (5 ** 2))", expr.to_string()); assert_eq!("(((45 * 2) - (15 / 5)) + (5 ** 2))", expr.to_string());
let expr = parser!("(5 - 2) * 10").parse()?.unwrap(); let expr = parser!("(5 - 2) * 10").parse()?.unwrap();
assert_eq!("(((5 - 2)) * 10)", expr.to_string()); assert_eq!("((5 - 2) * 10)", expr.to_string());
Ok(()) Ok(())
} }