@@ -16,14 +16,25 @@ 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 {
2425 throw new ArgumentNullException ( nameof ( store ) ) ;
2526 }
2627
28+ if ( minimum < 0 )
29+ {
30+ throw new ArgumentOutOfRangeException ( nameof ( minimum ) ) ;
31+ }
32+
33+ if ( maximum < 0 )
34+ {
35+ throw new ArgumentOutOfRangeException ( nameof ( maximum ) ) ;
36+ }
37+
2738 if ( maximum < minimum )
2839 {
2940 throw new ArgumentException ( "The maximum cannot be less than the minimum." , nameof ( maximum ) ) ;
@@ -32,14 +43,12 @@ public Memory(IStore store, uint minimum = 0, uint maximum = uint.MaxValue)
3243 this . store = store ;
3344 Minimum = minimum ;
3445 Maximum = maximum ;
46+ Is64Bit = is64Bit ;
3547
3648 unsafe
3749 {
38- var limits = new Native . Limits ( ) ;
39- limits . min = minimum ;
40- limits . max = maximum ;
50+ using var type = new TypeHandle ( Native . wasmtime_memorytype_new ( ( ulong ) minimum , maximum is not null , ( ulong ) ( maximum ?? 0 ) , is64Bit ) ) ;
4151
42- using var type = new TypeHandle ( Native . wasm_memorytype_new ( limits ) ) ;
4352 var error = Native . wasmtime_memory_new ( store . Context . handle , type , out this . memory ) ;
4453 if ( error != IntPtr . Zero )
4554 {
@@ -54,22 +63,30 @@ public Memory(IStore store, uint minimum = 0, uint maximum = uint.MaxValue)
5463 public const int PageSize = 65536 ;
5564
5665 /// <summary>
57- /// The minimum memory size (in WebAssembly page units).
66+ /// Gets the minimum memory size (in WebAssembly page units).
5867 /// </summary>
59- public uint Minimum { get ; private set ; }
68+ /// <value>The minimum memory size (in WebAssembly page units).</value>
69+ public long Minimum { get ; }
6070
6171 /// <summary>
62- /// The maximum memory size (in WebAssembly page units).
72+ /// Gets the maximum memory size (in WebAssembly page units).
6373 /// </summary>
64- public uint Maximum { get ; private set ; }
74+ /// <value>The maximum memory size (in WebAssembly page units), or <c>null</c> if no maximum is specified.</value>
75+ public long ? Maximum { get ; }
76+
77+ /// <summary>
78+ /// Gets a value that indicates whether this type of memory represents a 64-bit memory.
79+ /// </summary>
80+ /// <value><c>true</c> if this type of memory represents a 64-bit memory, <c>false</c> otherwise.</value>
81+ public bool Is64Bit { get ; }
6582
6683 /// <summary>
6784 /// Gets the current size of the memory, in WebAssembly page units.
6885 /// </summary>
6986 /// <returns>Returns the current size of the memory, in WebAssembly page units.</returns>
70- public uint GetSize ( )
87+ public long GetSize ( )
7188 {
72- return Native . wasmtime_memory_size ( store . Context . handle , this . memory ) ;
89+ return ( long ) Native . wasmtime_memory_size ( store . Context . handle , this . memory ) ;
7390 }
7491
7592 /// <summary>
@@ -78,7 +95,7 @@ public uint GetSize()
7895 /// <returns>Returns the current length of the memory, in bytes.</returns>
7996 public long GetLength ( )
8097 {
81- return checked ( ( long ) ( nuint ) Native . wasmtime_memory_data_size ( store . Context . handle , this . memory ) ) ;
98+ return checked ( ( long ) Native . wasmtime_memory_data_size ( store . Context . handle , this . memory ) ) ;
8299 }
83100
84101 /// <summary>
@@ -485,20 +502,25 @@ public void WriteDouble(long address, double value)
485502 /// <param name="delta">The number of WebAssembly pages to grow the memory by.</param>
486503 /// <returns>Returns the previous size of the Webassembly memory, in pages.</returns>
487504 /// <remarks>This method will invalidate previously returned values from `GetSpan`.</remarks>
488- public uint Grow ( uint delta )
505+ public long Grow ( long delta )
489506 {
490507 if ( store is null )
491508 {
492509 throw new ArgumentNullException ( nameof ( store ) ) ;
493510 }
494511
495- var error = Native . wasmtime_memory_grow ( store . Context . handle , this . memory , delta , out var prev ) ;
512+ if ( delta < 0 )
513+ {
514+ throw new ArgumentOutOfRangeException ( nameof ( delta ) ) ;
515+ }
516+
517+ var error = Native . wasmtime_memory_grow ( store . Context . handle , this . memory , ( ulong ) delta , out var prev ) ;
496518 if ( error != IntPtr . Zero )
497519 {
498520 throw WasmtimeException . FromOwnedError ( error ) ;
499521 }
500522
501- return prev ;
523+ return ( long ) prev ;
502524 }
503525
504526 Extern IExternal . AsExtern ( )
@@ -518,9 +540,14 @@ internal Memory(IStore store, ExternMemory memory)
518540
519541 unsafe
520542 {
521- var limits = Native . wasm_memorytype_limits ( type . DangerousGetHandle ( ) ) ;
522- Minimum = limits ->min ;
523- Maximum = limits ->max ;
543+ Minimum = ( long ) Native . wasmtime_memorytype_minimum ( type . DangerousGetHandle ( ) ) ;
544+
545+ if ( Native . wasmtime_memorytype_maximum ( type . DangerousGetHandle ( ) , out ulong max ) )
546+ {
547+ Maximum = ( long ) max ;
548+ }
549+
550+ Is64Bit = Native . wasmtime_memorytype_is64 ( type . DangerousGetHandle ( ) ) ;
524551 }
525552 }
526553
@@ -541,37 +568,37 @@ protected override bool ReleaseHandle()
541568
542569 internal static class Native
543570 {
544- [ StructLayout ( LayoutKind . Sequential ) ]
545- internal struct Limits
546- {
547- public uint min ;
548-
549- public uint max ;
550- }
551-
552571 [ DllImport ( Engine . LibraryName ) ]
553572 public static extern IntPtr wasmtime_memory_new ( IntPtr context , TypeHandle type , out ExternMemory memory ) ;
554573
555574 [ DllImport ( Engine . LibraryName ) ]
556575 public static unsafe extern byte * wasmtime_memory_data ( IntPtr context , in ExternMemory memory ) ;
557576
558577 [ DllImport ( Engine . LibraryName ) ]
559- public static extern UIntPtr wasmtime_memory_data_size ( IntPtr context , in ExternMemory memory ) ;
578+ public static extern nuint wasmtime_memory_data_size ( IntPtr context , in ExternMemory memory ) ;
560579
561580 [ DllImport ( Engine . LibraryName ) ]
562- public static extern uint wasmtime_memory_size ( IntPtr context , in ExternMemory memory ) ;
581+ public static extern ulong wasmtime_memory_size ( IntPtr context , in ExternMemory memory ) ;
563582
564583 [ DllImport ( Engine . LibraryName ) ]
565- public static extern IntPtr wasmtime_memory_grow ( IntPtr context , in ExternMemory memory , uint delta , out uint prev ) ;
584+ public static extern IntPtr wasmtime_memory_grow ( IntPtr context , in ExternMemory memory , ulong delta , out ulong prev ) ;
566585
567586 [ DllImport ( Engine . LibraryName ) ]
568587 public static extern IntPtr wasmtime_memory_type ( IntPtr context , in ExternMemory memory ) ;
569588
570589 [ DllImport ( Engine . LibraryName ) ]
571- public static extern IntPtr wasm_memorytype_new ( in Limits limits ) ;
590+ public static extern IntPtr wasmtime_memorytype_new ( ulong min , [ MarshalAs ( UnmanagedType . I1 ) ] bool max_present , ulong max , [ MarshalAs ( UnmanagedType . I1 ) ] bool is_64 ) ;
591+
592+ [ DllImport ( Engine . LibraryName ) ]
593+ public static unsafe extern ulong wasmtime_memorytype_minimum ( IntPtr type ) ;
594+
595+ [ DllImport ( Engine . LibraryName ) ]
596+ [ return : MarshalAs ( UnmanagedType . I1 ) ]
597+ public static unsafe extern bool wasmtime_memorytype_maximum ( IntPtr type , out ulong max ) ;
572598
573599 [ DllImport ( Engine . LibraryName ) ]
574- public static unsafe extern Limits * wasm_memorytype_limits ( IntPtr type ) ;
600+ [ return : MarshalAs ( UnmanagedType . I1 ) ]
601+ public static unsafe extern bool wasmtime_memorytype_is64 ( IntPtr type ) ;
575602
576603 [ DllImport ( Engine . LibraryName ) ]
577604 public static extern void wasm_memorytype_delete ( IntPtr handle ) ;
0 commit comments