import { getFileInfo } from 'pyright-internal/analyzer/analyzerNodeInfo' ; import { convertOffsetToPosition } from 'pyright-internal/common/positionUtils' ; import { ParseNodeType } from 'pyright-internal/parser/parseNodes' ; import { ctrToStr } from '../backend/constraintType' ; import { Context } from '../backend/context' ; import { simplifyConstraint } from '../backend/expUtils' ; import { CodeRange , CodeSource , ShContFlag , ShValue , SVError , SVErrorProps , SVType } from '../backend/sharpValues' ; export class FilePathStore { private _pathList : string [ ] ; private _idMap : Map < string , number > ; constructor ( pathList ? ) { this . _pathList = pathList ?? [ ] ; this . _idMap = new Map ( ) ; this . addPathList ( this . _pathList ) ; } addPath ( path ) { if ( this . _idMap . has ( path ) ) return this . _idMap . get ( path ) ! ; const id = this . _pathList . length ; this . _idMap . set ( path , id ) ; this . _pathList . push ( path ) ; return id ; } addPathList ( paths ) { paths . forEach ( ( p ) => this . addPath ( p ) ) ; } getPath ( id ) { if ( this . _pathList . length < id ) return ; return this . _pathList [ id ] ; } getId ( path ) { return this . _idMap . get ( path ) ; } getPathList ( ) { return [ ... this . _pathList ] ; } length ( ) { return this . _pathList . length ; } toCodeRange ( source ) { if ( ! source ) return ; if ( ! ( 'fileId' in source ) ) { let moduleNode = source ; while ( moduleNode . nodeType !== ParseNodeType . Module ) { moduleNode = moduleNode . parent ! ; } const fileInfo = getFileInfo ( moduleNode ) ! ; const filePath = fileInfo . filePath ; const lines = fileInfo . lines ; const start = convertOffsetToPosition ( source . start , lines ) ; const end = convertOffsetToPosition ( source . start + source . length , lines ) ; const fileId = this . addPath ( filePath ) ; return { fileId , range : { start , end } , } ; } return source ; } } export enum ExecutionPathStatus { Success , Stopped , Failed } export interface ExecutionPathProps { pathId : number ; projectRoot : string ; status : ExecutionPathStatus ; variables : { [ varName ] : string ; } ; ctrPool : [ string , CodeRange ? ] [ ] ; callStack : [ string , CodeRange ? ] [ ] ; hardCtr : number [ ] ; softCtr : number [ ] ; pathCtr : number [ ] ; logs : SVErrorProps [ ] ; envSize : number ; heapSize : number ; } export class ExecutionPath { readonly props : ExecutionPathProps ; readonly ctx : Context < ShValue | ShContFlag > ; private _pathId : number ; private _projectRoot : string ; private _pathStore : FilePathStore ; private _status : ExecutionPathStatus ; constructor ( result , pathId , projectRoot , status , pathStore ) { this . ctx = result ; this . _pathId = pathId ; this . _projectRoot = projectRoot ; this . _status = status ; this . _pathStore = pathStore ; this . props = this . _initialize ( ) ; } private _initialize ( ) { const status = this . _status ; const variables = { } ; const ctrPool = [ ] ; const logs = [ ] ; const callStack = [ ] ; const ctx = this . ctx ; const { env , heap , ctrSet } = ctx ; if ( status === ExecutionPathStatus . Success ) { const module = heap . getVal ( 1 ) ; if ( module ?. type === SVType . Object ) { } } else { ctx . callStack . filter ( ( [ f , _ ] ) => { if ( typeof f === 'string' ) { return f !== 'callKV' ; } else { return f . name !== 'callKV' ; } } ) . forEach ( ( [ func , node ] ) => { callStack . push ( [ `${ typeof func === 'string' ? func : func . name }` , this . _pathStore . toCodeRange ( node ) , ] ) ; } ) ; callStack . reverse ( ) ; } ctx . logs . forEach ( ( value ) => { if ( value . type === SVType . Error ) { const cleanValue = value . set ( 'source' , this . _pathStore . toCodeRange ( value . source ) ) ; logs . push ( cleanValue ) ; } else { } } ) ; ctx . ctrSet . ctrPool . forEach ( ( ctr ) => { const ctrStr = ctrToStr ( simplifyConstraint ( ctx . ctrSet , ctr ) ) ; const source = this . _pathStore . toCodeRange ( ctr . source ) ; ctrPool . push ( [ ctrStr , source ] ) ; } ) ; return { pathId : this . _pathId , projectRoot : this . _projectRoot , status , variables , ctrPool , callStack , hardCtr : ctrSet . hardCtr . toArray ( ) , softCtr : ctrSet . softCtr . toArray ( ) , pathCtr : ctrSet . pathCtr . toArray ( ) , logs , envSize : env . addrMap . size , heapSize : heap . valMap . size , } ; } }