Skip to content

Commit

Permalink
service/dap: make handlesMap generic (#3798)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aarzilli authored Sep 3, 2024
1 parent 162959b commit 7857f23
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 40 deletions.
40 changes: 8 additions & 32 deletions service/dap/handles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
16 changes: 8 additions & 8 deletions service/dap/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)}
Expand Down

0 comments on commit 7857f23

Please sign in to comment.