Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service/dap: make handlesMap generic #3798

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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