@@ -16,8 +16,9 @@ public class Memory : IExternal
1616 /// </summary>
1717 /// <param name="store">The store to create the memory in.</param>
1818 /// <param name="minimum">The minimum number of WebAssembly pages.</param>
19- /// <param name="maximum">The maximum number of WebAssembly pages.</param>
20- public Memory ( IStore store , uint minimum = 0 , uint maximum = uint . MaxValue )
19+ /// <param name="maximum">The maximum number of WebAssembly pages, or <c>null</c> to not specify a maximum.</param>
20+ /// <param name="is64Bit"><c>true</c> when memory type represents a 64-bit memory, <c>false</c> when it represents a 32-bit memory.</param>
21+ public Memory ( IStore store , long minimum = 0 , long ? maximum = null , bool is64Bit = false )
2122 {
2223 if ( store is null )
2324 {
@@ -32,14 +33,12 @@ public Memory(IStore store, uint minimum = 0, uint maximum = uint.MaxValue)
3233 this . store = store ;
3334 Minimum = minimum ;
3435 Maximum = maximum ;
36+ Is64Bit = is64Bit ;
3537
3638 unsafe
3739 {
38- var limits = new Native . Limits ( ) ;
39- limits . min = minimum ;
40- limits . max = maximum ;
40+ using var type = new TypeHandle ( Native . wasmtime_memorytype_new ( ( ulong ) minimum , maximum is not null , ( ulong ) ( maximum ?? 0 ) , is64Bit ) ) ;
4141
42- using var type = new TypeHandle ( Native . wasm_memorytype_new ( limits ) ) ;
4342 var error = Native . wasmtime_memory_new ( store . Context . handle , type , out this . memory ) ;
4443 if ( error != IntPtr . Zero )
4544 {
@@ -54,22 +53,30 @@ public Memory(IStore store, uint minimum = 0, uint maximum = uint.MaxValue)
5453 public const int PageSize = 65536 ;
5554
5655 /// <summary>
57- /// The minimum memory size (in WebAssembly page units).
56+ /// Gets the minimum memory size (in WebAssembly page units).
5857 /// </summary>
59- public uint Minimum { get ; private set ; }
58+ /// <value>The minimum memory size (in WebAssembly page units).</value>
59+ public long Minimum { get ; }
6060
6161 /// <summary>
62- /// The maximum memory size (in WebAssembly page units).
62+ /// Gets the maximum memory size (in WebAssembly page units).
6363 /// </summary>
64- public uint Maximum { get ; private set ; }
64+ /// <value>The maximum memory size (in WebAssembly page units), or <c>null</c> if no maximum is specified.</value>
65+ public long ? Maximum { get ; }
66+
67+ /// <summary>
68+ /// Gets a value that indicates whether this type of memory represents a 64-bit memory.
69+ /// </summary>
70+ /// <value><c>true</c> if this type of memory represents a 64-bit memory, <c>false</c> otherwise.</value>
71+ public bool Is64Bit { get ; }
6572
6673 /// <summary>
6774 /// Gets the current size of the memory, in WebAssembly page units.
6875 /// </summary>
6976 /// <returns>Returns the current size of the memory, in WebAssembly page units.</returns>
70- public uint GetSize ( )
77+ public long GetSize ( )
7178 {
72- return Native . wasmtime_memory_size ( store . Context . handle , this . memory ) ;
79+ return ( long ) Native . wasmtime_memory_size ( store . Context . handle , this . memory ) ;
7380 }
7481
7582 /// <summary>
@@ -78,7 +85,7 @@ public uint GetSize()
7885 /// <returns>Returns the current length of the memory, in bytes.</returns>
7986 public long GetLength ( )
8087 {
81- return checked ( ( long ) ( nuint ) Native . wasmtime_memory_data_size ( store . Context . handle , this . memory ) ) ;
88+ return checked ( ( long ) Native . wasmtime_memory_data_size ( store . Context . handle , this . memory ) ) ;
8289 }
8390
8491 /// <summary>
@@ -485,20 +492,25 @@ public void WriteDouble(long address, double value)
485492 /// <param name="delta">The number of WebAssembly pages to grow the memory by.</param>
486493 /// <returns>Returns the previous size of the Webassembly memory, in pages.</returns>
487494 /// <remarks>This method will invalidate previously returned values from `GetSpan`.</remarks>
488- public uint Grow ( uint delta )
495+ public long Grow ( long delta )
489496 {
490497 if ( store is null )
491498 {
492499 throw new ArgumentNullException ( nameof ( store ) ) ;
493500 }
494501
495- var error = Native . wasmtime_memory_grow ( store . Context . handle , this . memory , delta , out var prev ) ;
502+ if ( delta < 0 )
503+ {
504+ throw new ArgumentOutOfRangeException ( nameof ( delta ) ) ;
505+ }
506+
507+ var error = Native . wasmtime_memory_grow ( store . Context . handle , this . memory , ( ulong ) delta , out var prev ) ;
496508 if ( error != IntPtr . Zero )
497509 {
498510 throw WasmtimeException . FromOwnedError ( error ) ;
499511 }
500512
501- return prev ;
513+ return ( long ) prev ;
502514 }
503515
504516 Extern IExternal . AsExtern ( )
@@ -518,9 +530,15 @@ internal Memory(IStore store, ExternMemory memory)
518530
519531 unsafe
520532 {
521- var limits = Native . wasm_memorytype_limits ( type . DangerousGetHandle ( ) ) ;
522- Minimum = limits ->min ;
523- Maximum = limits ->max ;
533+ Minimum = ( long ) Native . wasmtime_memorytype_minimum ( type . DangerousGetHandle ( ) ) ;
534+
535+ bool hasMax = Native . wasmtime_memorytype_maximum ( type . DangerousGetHandle ( ) , out ulong max ) ;
536+ if ( hasMax )
537+ {
538+ Maximum = ( long ) max ;
539+ }
540+
541+ Is64Bit = Native . wasmtime_memorytype_is64 ( type . DangerousGetHandle ( ) ) ;
524542 }
525543 }
526544
@@ -541,37 +559,37 @@ protected override bool ReleaseHandle()
541559
542560 internal static class Native
543561 {
544- [ StructLayout ( LayoutKind . Sequential ) ]
545- internal struct Limits
546- {
547- public uint min ;
548-
549- public uint max ;
550- }
551-
552562 [ DllImport ( Engine . LibraryName ) ]
553563 public static extern IntPtr wasmtime_memory_new ( IntPtr context , TypeHandle type , out ExternMemory memory ) ;
554564
555565 [ DllImport ( Engine . LibraryName ) ]
556566 public static unsafe extern byte * wasmtime_memory_data ( IntPtr context , in ExternMemory memory ) ;
557567
558568 [ DllImport ( Engine . LibraryName ) ]
559- public static extern UIntPtr wasmtime_memory_data_size ( IntPtr context , in ExternMemory memory ) ;
569+ public static extern nuint wasmtime_memory_data_size ( IntPtr context , in ExternMemory memory ) ;
560570
561571 [ DllImport ( Engine . LibraryName ) ]
562- public static extern uint wasmtime_memory_size ( IntPtr context , in ExternMemory memory ) ;
572+ public static extern ulong wasmtime_memory_size ( IntPtr context , in ExternMemory memory ) ;
563573
564574 [ DllImport ( Engine . LibraryName ) ]
565- public static extern IntPtr wasmtime_memory_grow ( IntPtr context , in ExternMemory memory , uint delta , out uint prev ) ;
575+ public static extern IntPtr wasmtime_memory_grow ( IntPtr context , in ExternMemory memory , ulong delta , out ulong prev ) ;
566576
567577 [ DllImport ( Engine . LibraryName ) ]
568578 public static extern IntPtr wasmtime_memory_type ( IntPtr context , in ExternMemory memory ) ;
569579
570580 [ DllImport ( Engine . LibraryName ) ]
571- public static extern IntPtr wasm_memorytype_new ( in Limits limits ) ;
581+ public static extern IntPtr wasmtime_memorytype_new ( ulong min , [ MarshalAs ( UnmanagedType . I1 ) ] bool max_present , ulong max , [ MarshalAs ( UnmanagedType . I1 ) ] bool is_64 ) ;
582+
583+ [ DllImport ( Engine . LibraryName ) ]
584+ public static unsafe extern ulong wasmtime_memorytype_minimum ( IntPtr type ) ;
585+
586+ [ DllImport ( Engine . LibraryName ) ]
587+ [ return : MarshalAs ( UnmanagedType . I1 ) ]
588+ public static unsafe extern bool wasmtime_memorytype_maximum ( IntPtr type , out ulong max ) ;
572589
573590 [ DllImport ( Engine . LibraryName ) ]
574- public static unsafe extern Limits * wasm_memorytype_limits ( IntPtr type ) ;
591+ [ return : MarshalAs ( UnmanagedType . I1 ) ]
592+ public static unsafe extern bool wasmtime_memorytype_is64 ( IntPtr type ) ;
575593
576594 [ DllImport ( Engine . LibraryName ) ]
577595 public static extern void wasm_memorytype_delete ( IntPtr handle ) ;
0 commit comments