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:
parent
162959baee
commit
7857f233ad
@ -9,9 +9,9 @@ const startHandle = 1000
|
|||||||
// opacity and allowing simplification of complex identifiers.
|
// opacity and allowing simplification of complex identifiers.
|
||||||
// Based on
|
// Based on
|
||||||
// https://github.com/microsoft/vscode-debugadapter-node/blob/master/adapter/src/handles.ts
|
// https://github.com/microsoft/vscode-debugadapter-node/blob/master/adapter/src/handles.ts
|
||||||
type handlesMap struct {
|
type handlesMap[T any] struct {
|
||||||
nextHandle int
|
nextHandle int
|
||||||
handleToVal map[int]interface{}
|
handleToVal map[int]T
|
||||||
}
|
}
|
||||||
|
|
||||||
type fullyQualifiedVariable struct {
|
type fullyQualifiedVariable struct {
|
||||||
@ -29,47 +29,23 @@ type fullyQualifiedVariable struct {
|
|||||||
startIndex int
|
startIndex int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHandlesMap() *handlesMap {
|
func newHandlesMap[T any]() *handlesMap[T] {
|
||||||
return &handlesMap{startHandle, make(map[int]interface{})}
|
return &handlesMap[T]{startHandle, make(map[int]T)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hs *handlesMap) reset() {
|
func (hs *handlesMap[T]) reset() {
|
||||||
hs.nextHandle = startHandle
|
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
|
next := hs.nextHandle
|
||||||
hs.nextHandle++
|
hs.nextHandle++
|
||||||
hs.handleToVal[next] = value
|
hs.handleToVal[next] = value
|
||||||
return next
|
return next
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hs *handlesMap) get(handle int) (interface{}, bool) {
|
func (hs *handlesMap[T]) get(handle int) (T, bool) {
|
||||||
v, ok := hs.handleToVal[handle]
|
v, ok := hs.handleToVal[handle]
|
||||||
return v, ok
|
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.
|
// stackFrameHandles maps frames of each goroutine to unique ids across all goroutines.
|
||||||
// Reset at every stop.
|
// Reset at every stop.
|
||||||
stackFrameHandles *handlesMap
|
stackFrameHandles *handlesMap[stackFrame]
|
||||||
// variableHandles maps compound variables to unique references within their stack frame.
|
// variableHandles maps compound variables to unique references within their stack frame.
|
||||||
// Reset at every stop.
|
// Reset at every stop.
|
||||||
// See also comment for convertVariable.
|
// See also comment for convertVariable.
|
||||||
variableHandles *variablesHandlesMap
|
variableHandles *handlesMap[*fullyQualifiedVariable]
|
||||||
// args tracks special settings for handling debug session requests.
|
// args tracks special settings for handling debug session requests.
|
||||||
args launchAttachArgs
|
args launchAttachArgs
|
||||||
// exceptionErr tracks the runtime error that last occurred.
|
// exceptionErr tracks the runtime error that last occurred.
|
||||||
@ -345,8 +345,8 @@ func NewSession(conn io.ReadWriteCloser, config *Config, debugger *debugger.Debu
|
|||||||
config: config,
|
config: config,
|
||||||
id: sessionCount,
|
id: sessionCount,
|
||||||
conn: newConnection(conn),
|
conn: newConnection(conn),
|
||||||
stackFrameHandles: newHandlesMap(),
|
stackFrameHandles: newHandlesMap[stackFrame](),
|
||||||
variableHandles: newVariablesHandlesMap(),
|
variableHandles: newHandlesMap[*fullyQualifiedVariable](),
|
||||||
args: defaultArgs,
|
args: defaultArgs,
|
||||||
exceptionErr: nil,
|
exceptionErr: nil,
|
||||||
debugger: debugger,
|
debugger: debugger,
|
||||||
@ -2194,8 +2194,8 @@ func (s *Session) onScopesRequest(request *dap.ScopesRequest) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
goid := sf.(stackFrame).goroutineID
|
goid := sf.goroutineID
|
||||||
frame := sf.(stackFrame).frameIndex
|
frame := sf.frameIndex
|
||||||
|
|
||||||
// Check if the function is optimized.
|
// Check if the function is optimized.
|
||||||
fn, err := s.debugger.Function(int64(goid), frame, 0)
|
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)
|
// no frame is specified (e.g. when stopped on entry or no call stack frame is expanded)
|
||||||
goid, frame := -1, 0
|
goid, frame := -1, 0
|
||||||
if sf, ok := s.stackFrameHandles.get(request.Arguments.FrameId); ok {
|
if sf, ok := s.stackFrameHandles.get(request.Arguments.FrameId); ok {
|
||||||
goid = sf.(stackFrame).goroutineID
|
goid = sf.goroutineID
|
||||||
frame = sf.(stackFrame).frameIndex
|
frame = sf.frameIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
response := &dap.EvaluateResponse{Response: *newResponse(request.Request)}
|
response := &dap.EvaluateResponse{Response: *newResponse(request.Request)}
|
||||||
|
Loading…
Reference in New Issue
Block a user