import { LibSdbTypes } from "../types/types" ; import { AstWalker } from "./astWalker" ; export function GetFunctionProgramCount ( bytecode , methodId ) { const bytecodeSequence = "63" + methodId + "14" ; const pos = bytecode . indexOf ( bytecodeSequence ) ; if ( pos < 0 ) { return null ; } else { const pc = bytecode [ pos + bytecodeSequence . length ] + bytecode [ pos + bytecodeSequence . length + 1 ] ; return parseInt ( pc , 16 ) ; } } export function adjustBreakpointLineNumbers ( breakpoints , path , startLine , numLines ) { for ( let i = 0 ; i < breakpoints . length ; i ++ ) { if ( breakpoints [ i ] . line >= startLine ) { breakpoints [ i ] . line += numLines ; } } } ; export function adjustCallstackLineNumbers ( callstack , path , startLine , numLines ) { for ( let i = 0 ; i < callstack . length ; i ++ ) { if ( callstack [ i ] . file === path && callstack [ i ] . line >= startLine ) { callstack [ i ] . line += numLines ; } } } ; export function addLineOffset ( line , numLines , lineOffsets ) { const numPrevLines = lineOffsets . get ( line ) || 0 ; lineOffsets . set ( line , numPrevLines + numLines ) ; } export function getOriginalLine ( newLine , lineOffsets ) { let originalLine = newLine ; lineOffsets . forEach ( ( numLines , line ) => { if ( newLine >= line ) { originalLine -= numLines ; } } ) ; return originalLine ; } export function getNewLine ( originalLine , lineOffsets ) { let newLine = originalLine ; lineOffsets . forEach ( ( numLines , line ) => { if ( originalLine >= line ) { newLine += numLines ; } } ) ; return newLine ; } export function findScope ( index , ast ) { let scope = [ ] ; const astWalker = new AstWalker ( ) ; astWalker . walkDetail ( ast , null , 0 , ( node , parent , depth ) => { const src = node . src . split ( ":" ) . map ( ( s ) => { return parseInt ( s ) ; } ) ; if ( src . length >= 2 && src [ 0 ] <= index && index <= src [ 0 ] + src [ 1 ] ) { let childIndex = null ; if ( parent ) { for ( let i = 0 ; i < parent . children . length ; i ++ ) { if ( parent . children [ i ] . id === node . id ) { childIndex = i ; } } } let astScope = new LibSdbTypes . AstScope ( ) ; astScope . id = node . id ; astScope . childIndex = childIndex ; astScope . depth = depth ; scope . unshift ( astScope ) ; return true ; } else { return false ; } } ) ; return scope ; } export function findLowerBound ( target , array ) { let start = 0 ; let length = array . length ; while ( length > 0 ) { let half = length >> 1 ; let middle = start + half ; if ( array [ middle ] <= target ) { length = length - 1 - half ; start = middle + 1 ; } else { length = half ; } } return start - 1 ; }