@@ -202,10 +202,16 @@ export class Options {
202202 target : Target = Target . WASM32 ;
203203 /** If true, replaces assertions with nops. */
204204 noAssert : bool = false ;
205+ /** It true, exports the memory to the embedder. */
206+ exportMemory : bool = true ;
205207 /** If true, imports the memory provided by the embedder. */
206208 importMemory : bool = false ;
207- /** If greater than zero, declare memory as shared by setting max memory to sharedMemory. */
208- sharedMemory : i32 = 0 ;
209+ /** Initial memory size, in pages. */
210+ initialMemory : u32 = 0 ;
211+ /** Maximum memory size, in pages. */
212+ maximumMemory : u32 = 0 ;
213+ /** If true, memory is declared as shared. */
214+ sharedMemory : bool = false ;
209215 /** If true, imports the function table provided by the embedder. */
210216 importTable : bool = false ;
211217 /** If true, exports the function table. */
@@ -215,9 +221,9 @@ export class Options {
215221 /** If true, generates an explicit start function. */
216222 explicitStart : bool = false ;
217223 /** Static memory start offset. */
218- memoryBase : i32 = 0 ;
224+ memoryBase : u32 = 0 ;
219225 /** Static table start offset. */
220- tableBase : i32 = 0 ;
226+ tableBase : u32 = 0 ;
221227 /** Global aliases, mapping alias names as the key to internal names to be aliased as the value. */
222228 globalAliases : Map < string , string > | null = null ;
223229 /** Features to activate by default. These are the finished proposals. */
@@ -227,7 +233,7 @@ export class Options {
227233 /** If true, enables pedantic diagnostics. */
228234 pedantic : bool = false ;
229235 /** Indicates a very low (<64k) memory limit. */
230- lowMemoryLimit : i32 = 0 ;
236+ lowMemoryLimit : u32 = 0 ;
231237
232238 /** Hinted optimize level. Not applied by the compiler itself. */
233239 optimizeLevelHint : i32 = 0 ;
@@ -554,15 +560,57 @@ export class Compiler extends DiagnosticEmitter {
554560 }
555561
556562 // set up memory
557- var isSharedMemory = options . hasFeature ( Feature . THREADS ) && options . sharedMemory > 0 ;
563+ var initialPages : u32 = 0 ;
564+ if ( this . options . memoryBase /* is specified */ || this . memorySegments . length ) {
565+ initialPages = u32 ( i64_low ( i64_shr_u ( i64_align ( memoryOffset , 0x10000 ) , i64_new ( 16 ) ) ) ) ;
566+ }
567+ if ( options . initialMemory ) {
568+ if ( options . initialMemory < initialPages ) {
569+ this . error (
570+ DiagnosticCode . Module_requires_at_least_0_pages_of_initial_memory ,
571+ null ,
572+ initialPages . toString ( )
573+ ) ;
574+ } else {
575+ initialPages = options . initialMemory ;
576+ }
577+ }
578+ var maximumPages = Module . UNLIMITED_MEMORY ;
579+ if ( options . maximumMemory ) {
580+ if ( options . maximumMemory < initialPages ) {
581+ this . error (
582+ DiagnosticCode . Module_requires_at_least_0_pages_of_maximum_memory ,
583+ null ,
584+ initialPages . toString ( )
585+ ) ;
586+ } else {
587+ maximumPages = options . maximumMemory ;
588+ }
589+ }
590+ var isSharedMemory = false ;
591+ if ( options . sharedMemory ) {
592+ isSharedMemory = true ;
593+ if ( ! options . maximumMemory ) {
594+ this . error (
595+ DiagnosticCode . Shared_memory_requires_maximum_memory_to_be_defined ,
596+ null
597+ ) ;
598+ isSharedMemory = false ;
599+ }
600+ if ( ! options . hasFeature ( Feature . THREADS ) ) {
601+ this . error (
602+ DiagnosticCode . Shared_memory_requires_feature_threads_to_be_enabled ,
603+ null
604+ ) ;
605+ isSharedMemory = false ;
606+ }
607+ }
558608 module . setMemory (
559- this . options . memoryBase /* is specified */ || this . memorySegments . length
560- ? i64_low ( i64_shr_u ( i64_align ( memoryOffset , 0x10000 ) , i64_new ( 16 ) ) )
561- : 0 ,
562- isSharedMemory ? options . sharedMemory : Module . UNLIMITED_MEMORY ,
609+ initialPages ,
610+ maximumPages ,
563611 this . memorySegments ,
564612 options . target ,
565- ExportNames . memory ,
613+ options . exportMemory ? ExportNames . memory : null ,
566614 isSharedMemory
567615 ) ;
568616
0 commit comments