import { CldAsmModule , LanguageResult } from './cldAsmModule' ; import { CldFactory } from './cldFactory' ; import { LanguageCode } from './languageCode' ; import { log } from './util/logger' ; import { wrapCldInterface } from './wrapCldInterface' ; const PTR_SIZE = 4 ; internal param { CldAsmModule } asmModule returns { CldFactory } export const cldLoader = ( asmModule ) : CldFactory => { const { cwrap , _free , allocateUTF8 , _malloc , getValue , UTF8ToString , setValue } = asmModule ; const cldInterface = wrapCldInterface ( cwrap ) ; /**
     * Naive auto-dispose interface to call cld interface with string params.
     *
     */ const usingParamPtr = < T = void > ( ... args ) : T => { const params = [ ... args ] ; const fn = params . pop ( ) ! ; const paramsPtr = params . map ( ( param ) => allocateUTF8 ( param ) ) ; const ret = ( fn as Function ) ( ... paramsPtr ) ; paramsPtr . forEach ( paramPtr => _free ( paramPtr ) ) ; return ret ; } ; const unknownIdentifier = UTF8ToString ( cldInterface . getUnknownIdentifier ( ) ) ; const minBytesDefault = cldInterface . getMinNumBytesDefault ( ) ; const maxBytesDefault = cldInterface . getMaxNumBytesDefault ( ) ; const maxBytesInput = cldInterface . getMaxNumBytesInput ( ) ; const languageResultStructSize = cldInterface . sizeLanguageResult ( ) ; log ( `cldLoader: cld3 wasm initialized with default values` , { unknownIdentifier , minBytesDefault , maxBytesDefault , maxBytesInput , languageResultStructSize } ) ; if ( unknownIdentifier !== LanguageCode . UNKNOWN ) { throw new Error ( `cld3 binary unknownIdentifier constant does not match to LanguageCode enum` ) ; } /**
     * Wrapper function to read SpanInfo** array from pointer.
     * After interop, pointer will be freed.
     */ const volatileReadSpanInfoArray = ( arrayPtr , size ) => { const ret = Array . from ( new Array ( size ) ) . map ( ( _ , idx ) => { const spanInfoPtr = getValue ( arrayPtr + PTR_SIZE * idx , '*' ) ; const range = { start_index : getValue ( spanInfoPtr + PTR_SIZE * 0 , 'i8' ) , end_index : getValue ( spanInfoPtr + PTR_SIZE * 1 , 'i8' ) , probability : getValue ( spanInfoPtr + PTR_SIZE * 2 , 'float' ) } ; _free ( spanInfoPtr ) ; return range ; } ) ; _free ( arrayPtr ) ; return ret ; } ; param structPtr const volatileReadResultStruct = ( structPtr ) => { const languageStringPtr = getValue ( structPtr + PTR_SIZE * 0 , '*' ) ; const byteRangesSize = getValue ( structPtr + PTR_SIZE * 4 , 'i8' ) ; const byteRangesArrayPtr = getValue ( structPtr + PTR_SIZE * 5 , '*' ) ; const ret = { language : UTF8ToString ( languageStringPtr ) as LanguageCode , probability : getValue ( structPtr + PTR_SIZE * 1 , 'float' ) , is_reliable : ! ! getValue ( structPtr + PTR_SIZE * 2 , 'i8' ) , proportion : getValue ( structPtr + PTR_SIZE * 3 , 'float' ) , byte_ranges : volatileReadSpanInfoArray ( byteRangesArrayPtr , byteRangesSize ) } ; _free ( languageStringPtr ) ; _free ( structPtr ) ; return ret ; } ; return { create : ( minBytes = minBytesDefault , maxBytes = maxBytesDefault ) => { const cldPtr = cldInterface . create ( minBytes , maxBytes ) ; return { findLanguage : ( text ) => { const resultPtr = _malloc ( languageResultStructSize ) ; usingParamPtr ( text , textPtr => cldInterface . findLanguage ( cldPtr , textPtr , resultPtr ) ) ; return volatileReadResultStruct ( resultPtr ) ; } , findMostFrequentLanguages : ( text , numLangs ) => { const languageListPtr = _malloc ( numLangs * PTR_SIZE ) ; const resultStructsPtr = [ ] ; for ( let idx = 0 ; idx < numLangs ; idx ++ ) { const resultPtr = _malloc ( languageResultStructSize ) ; resultStructsPtr . push ( resultPtr ) ; setValue ( languageListPtr + idx * PTR_SIZE , resultPtr , '*' ) ; } const languageCount = usingParamPtr ( text , textPtr => cldInterface . findTopNMostFreqLangs ( cldPtr , textPtr , numLangs , languageListPtr ) ) ; const ret = resultStructsPtr . map ( ptr => volatileReadResultStruct ( ptr ) ) . filter ( x => x . language !== unknownIdentifier ) ; _free ( languageListPtr ) ; return languageCount > 0 ? ret : [ ] ; } , dispose : ( ) => cldInterface . destroy ( cldPtr ) } ; } } ; } ;