service/dap: make handlesMap generic (#3798)

The oldest version of Go we compile with is 1.20 at this point, we can
thus make use of generic type parameters and the handlesMap support
type in service/dap is improved by them.
This commit is contained in:
Alessandro Arzilli 2024-09-03 19:40:34 +02:00 committed by GitHub
parent 162959baee
commit 7857f233ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 40 deletions

@ -9,9 +9,9 @@ const startHandle = 1000
// opacity and allowing simplification of complex identifiers.
// Based on
// https://github.com/microsoft/vscode-debugadapter-node/blob/master/adapter/src/handles.ts
type handlesMap struct {
type handlesMap[T any] struct {
nextHandle int
handleToVal map[int]interface{}
handleToVal map[int]T
}
type fullyQualifiedVariable struct {
@ -29,47 +29,23 @@ type fullyQualifiedVariable struct {
startIndex int
}
func newHandlesMap() *handlesMap {
return &handlesMap{startHandle, make(map[int]interface{})}
func newHandlesMap[T any]() *handlesMap[T] {
return &handlesMap[T]{startHandle, make(map[int]T)}
}
func (hs *handlesMap) reset() {
func (hs *handlesMap[T]) reset() {
hs.nextHandle = startHandle
hs.handleToVal = make(map[int]interface{})
hs.handleToVal = make(map[int]T)
}
func (hs *handlesMap) create(value interface{}) int {
func (hs *handlesMap[T]) create(value T) int {
next := hs.nextHandle
hs.nextHandle++
hs.handleToVal[next] = value
return next
}
func (hs *handlesMap) get(handle int) (interface{}, bool) {
func (hs *handlesMap[T]) get(handle int) (T, bool) {
v, ok := hs.handleToVal[handle]
return v, ok
}
type variablesHandlesMap struct {
m *handlesMap
}
func newVariablesHandlesMap() *variablesHandlesMap {
return &variablesHandlesMap{newHandlesMap()}
}
func (hs *variablesHandlesMap) create(value *fullyQualifiedVariable) int {
return hs.m.create(value)
}
func (hs *variablesHandlesMap) get(handle int) (*fullyQualifiedVariable, bool) {
v, ok := hs.m.get(handle)
if !ok {
return nil, false
}
return v.(*fullyQualifiedVariable), true
}
func (hs *variablesHandlesMap) reset() {
hs.m.reset()
}

@ -114,11 +114,11 @@ type Session struct {
// stackFrameHandles maps frames of each goroutine to unique ids across all goroutines.
// Reset at every stop.
stackFrameHandles *handlesMap
stackFrameHandles *handlesMap[stackFrame]
// variableHandles maps compound variables to unique references within their stack frame.
// Reset at every stop.
// See also comment for convertVariable.
variableHandles *variablesHandlesMap
variableHandles *handlesMap[*fullyQualifiedVariable]
// args tracks special settings for handling debug session requests.
args launchAttachArgs
// exceptionErr tracks the runtime error that last occurred.
@ -345,8 +345,8 @@ func NewSession(conn io.ReadWriteCloser, config *Config, debugger *debugger.Debu
config: config,
id: sessionCount,
conn: newConnection(conn),
stackFrameHandles: newHandlesMap(),
variableHandles: newVariablesHandlesMap(),
stackFrameHandles: newHandlesMap[stackFrame](),
variableHandles: newHandlesMap[*fullyQualifiedVariable](),
args: defaultArgs,
exceptionErr: nil,
debugger: debugger,
@ -2194,8 +2194,8 @@ func (s *Session) onScopesRequest(request *dap.ScopesRequest) {
return
}
goid := sf.(stackFrame).goroutineID
frame := sf.(stackFrame).frameIndex
goid := sf.goroutineID
frame := sf.frameIndex
// Check if the function is optimized.
fn, err := s.debugger.Function(int64(goid), frame, 0)
@ -2818,8 +2818,8 @@ func (s *Session) onEvaluateRequest(request *dap.EvaluateRequest) {
// no frame is specified (e.g. when stopped on entry or no call stack frame is expanded)
goid, frame := -1, 0
if sf, ok := s.stackFrameHandles.get(request.Arguments.FrameId); ok {
goid = sf.(stackFrame).goroutineID
frame = sf.(stackFrame).frameIndex
goid = sf.goroutineID
frame = sf.frameIndex
}
response := &dap.EvaluateResponse{Response: *newResponse(request.Request)}