@@ -219,6 +219,8 @@ export class Options {
219219 noUnsafe : bool = false ;
220220 /** If true, enables pedantic diagnostics. */
221221 pedantic : bool = false ;
222+ /** Indicates a very low (<64k) memory limit. */
223+ lowMemoryLimit : i32 = 0 ;
222224
223225 /** Hinted optimize level. Not applied by the compiler itself. */
224226 optimizeLevelHint : i32 = 0 ;
@@ -361,7 +363,7 @@ export class Compiler extends DiagnosticEmitter {
361363 this . memoryOffset = i64_new ( options . memoryBase ) ;
362364 module . setLowMemoryUnused ( false ) ;
363365 } else {
364- if ( options . optimizeLevelHint >= 2 ) {
366+ if ( options . optimizeLevelHint >= 2 && ! options . lowMemoryLimit ) {
365367 this . memoryOffset = i64_new ( 1024 ) ;
366368 module . setLowMemoryUnused ( true ) ;
367369 } else {
@@ -502,6 +504,16 @@ export class Compiler extends DiagnosticEmitter {
502504 // update the heap base pointer
503505 var memoryOffset = this . memoryOffset ;
504506 memoryOffset = i64_align ( memoryOffset , options . usizeType . byteSize ) ;
507+ var lowMemoryLimit32 = this . options . lowMemoryLimit ;
508+ if ( lowMemoryLimit32 ) {
509+ let lowMemoryLimit = i64_new ( lowMemoryLimit32 & ~ 15 ) ;
510+ if ( i64_gt ( memoryOffset , lowMemoryLimit ) ) {
511+ this . error (
512+ DiagnosticCode . Low_memory_limit_exceeded_by_static_data_0_1 ,
513+ null , i64_to_string ( memoryOffset ) , i64_to_string ( lowMemoryLimit )
514+ ) ;
515+ }
516+ }
505517 this . memoryOffset = memoryOffset ;
506518 module . removeGlobal ( BuiltinNames . heap_base ) ;
507519 if ( this . runtimeFeatures & RuntimeFeatures . HEAP ) {
@@ -526,7 +538,7 @@ export class Compiler extends DiagnosticEmitter {
526538 var isSharedMemory = options . hasFeature ( Feature . THREADS ) && options . sharedMemory > 0 ;
527539 module . setMemory (
528540 this . options . memoryBase /* is specified */ || this . memorySegments . length
529- ? i64_low ( i64_shr_u ( i64_align ( memoryOffset , 0x10000 ) , i64_new ( 16 , 0 ) ) )
541+ ? i64_low ( i64_shr_u ( i64_align ( memoryOffset , 0x10000 ) , i64_new ( 16 ) ) )
530542 : 0 ,
531543 isSharedMemory ? options . sharedMemory : Module . UNLIMITED_MEMORY ,
532544 this . memorySegments ,
0 commit comments