license export class WasmHeapWriter { private ptr : number | null ; private size : number ; private uint8View : Uint8Array ; private uint32View : Uint32Array ; private float32View : Float32Array ; private float64View : Float64Array ; private offset : number ; constructor ( size ) { this . ptr = Module . _malloc ( size ) ; if ( typeof this . ptr === 'undefined' || this . ptr === null ) { throw new Error ( `Malloc failed: size (${ size })` ) ; } this . size = size ; this . uint8View = new Uint8Array ( Module . HEAPU8 . buffer , this . ptr , size ) ; this . uint32View = new Uint32Array ( Module . HEAPU32 . buffer , this . ptr , size >> 2 ) ; this . float32View = new Float32Array ( Module . HEAPF32 . buffer , this . ptr , size >> 2 ) ; this . float64View = new Float64Array ( Module . HEAPF64 . buffer , this . ptr , size >> 3 ) ; this . offset = 0 ; } snapToWordAlignment ( ) { const inWordOffset = this . offset % 4 ; if ( inWordOffset === 0 ) { return ; } this . offset += 4 - inWordOffset ; } snapToDoubleWordAlignment ( ) { const inWordOffset = this . offset % 8 ; if ( inWordOffset === 0 ) { return ; } this . offset += 8 - inWordOffset ; } writeUint8 ( value ) { this . uint8View [ this . offset ] = value ; this . offset += 1 ; } writeBool ( value ) { if ( typeof value === 'boolean' ) { value = value ? 1 : 0 ; } this . writeUint8 ( value ) ; } writeInt32 ( value ) { this . snapToWordAlignment ( ) ; this . uint32View [ this . offset >> 2 ] = value ; this . offset += 4 ; } writeFloat64 ( value ) { this . snapToDoubleWordAlignment ( ) ; this . float64View [ this . offset >> 3 ] = value ; this . offset += 8 ; } writePtr ( value ) { this . writeInt32 ( value ) ; } writeFloat ( value ) { this . snapToWordAlignment ( ) ; this . float32View [ this . offset >> 2 ] = value ; this . offset += 4 ; } getData ( warnOnByteMismatch = false ) { this . snapToWordAlignment ( ) ; const bytesWritten = this . offset ; if ( warnOnByteMismatch && bytesWritten !== this . size ) { console . error ( 'wrote ' + bytesWritten + ' bytes, but expected to write ' + this . size ) ; } const ptr = this . ptr ; this . ptr = null ; return ptr ; } }