From 9344fa5b9567b413ff44a874135999cf240a9c99 Mon Sep 17 00:00:00 2001 From: Anonymous Date: Tue, 2 Jan 2024 09:35:54 +0000 Subject: [PATCH] Change API of ConvertTypesAcrossModules to make it clearer that old value gets converted to new type --- convert.go | 15 +++++++-------- jit/jit_test.go | 8 ++++---- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/convert.go b/convert.go index 9cb7fa0f..a95c490d 100644 --- a/convert.go +++ b/convert.go @@ -16,14 +16,14 @@ import ( "unsafe" ) -func CanAttemptConversion(oldValue, newValue interface{}) bool { +func CanAttemptConversion(oldValue interface{}, newType reflect.Type) bool { oldT := efaceOf(&oldValue)._type - newT := efaceOf(&newValue)._type + newT := fromRType(newType) seen := map[_typePair]struct{}{} return typesEqual(oldT, newT, seen) } -func ConvertTypesAcrossModules(oldModule, newModule *CodeModule, oldValue, newValue interface{}) (res interface{}, err error) { +func ConvertTypesAcrossModules(oldModule, newModule *CodeModule, oldValue interface{}, newType reflect.Type) (res interface{}, err error) { defer func() { if v := recover(); v != nil { err = fmt.Errorf("unexpected panic (this is a bug): %v\n stack trace: %s", v, debug.Stack()) @@ -35,15 +35,14 @@ func ConvertTypesAcrossModules(oldModule, newModule *CodeModule, oldValue, newVa // So we need to recurse over the entire structure, and find any itabs and replace them with the equivalent from the new module oldT := efaceOf(&oldValue)._type - newT := efaceOf(&newValue)._type + newT := fromRType(newType) seen := map[_typePair]struct{}{} if !typesEqual(oldT, newT, seen) { - return nil, fmt.Errorf("old type %T and new type %T are not equal", oldValue, newValue) + return nil, fmt.Errorf("old type %T and new type %s are not equal", oldValue, newType) } // Need to take data in old value and copy into new value one field at a time, but check that // the type is either shared (first module) or translated from the old to the new modules - newV := Indirect(ValueOf(&newValue)).Elem() oldV := Indirect(ValueOf(&oldValue)).Elem() cycleDetector := map[uintptr]*Value{} @@ -51,9 +50,9 @@ func ConvertTypesAcrossModules(oldModule, newModule *CodeModule, oldValue, newVa buildModuleTypeHash(activeModules()[0], typeHash) buildModuleTypeHash(newModule.module, typeHash) - cvt(oldModule, newModule, Value{oldV}, newV.Type(), nil, cycleDetector, typeHash) + cvt(oldModule, newModule, Value{oldV}, AsType(newT), nil, cycleDetector, typeHash) - return oldV.ConvertWithInterface(newV.Type()).Interface(), err + return oldV.ConvertWithInterface(AsType(newT)).Interface(), err } func toType(t Type) *_type { diff --git a/jit/jit_test.go b/jit/jit_test.go index 8d573429..df3cb455 100644 --- a/jit/jit_test.go +++ b/jit/jit_test.go @@ -1053,7 +1053,7 @@ func TestConvertOldAndNewTypes(t *testing.T) { t.Fatalf("expected current to be the same as input: %d %d", current, input) } - newThing2, err := goloader.ConvertTypesAcrossModules(module1, module2, thing1, thing2) + newThing2, err := goloader.ConvertTypesAcrossModules(module1, module2, thing1, reflect.TypeOf(thing2)) if err != nil { t.Fatal(err) } @@ -1072,7 +1072,7 @@ func TestConvertOldAndNewTypes(t *testing.T) { ifaceCurrentComplex12 := ifaceOut12.Val2["complex"].(map[interface{}]interface{}) _ = thingIface1.Method2(nil) - newThingIface2, err := goloader.ConvertTypesAcrossModules(module1, module2, thingIface1, thingIface2) + newThingIface2, err := goloader.ConvertTypesAcrossModules(module1, module2, thingIface1, reflect.TypeOf(thingIface2)) if err != nil { t.Fatal(err) } @@ -1192,7 +1192,7 @@ func TestStatefulHttpServer(t *testing.T) { makeHandler2 := symbols2["MakeServer"].(func() http.Handler) handler2 := makeHandler2() - newHandler, err := goloader.ConvertTypesAcrossModules(module, module2, handler, handler2) + newHandler, err := goloader.ConvertTypesAcrossModules(module, module2, handler, reflect.TypeOf(handler2)) if err != nil { t.Fatal(err) } @@ -1287,7 +1287,7 @@ func TestCloneConnection(t *testing.T) { t.Fatal(err) } - newDialer2, err := goloader.ConvertTypesAcrossModules(module1, module2, dialer1, dialer2) + newDialer2, err := goloader.ConvertTypesAcrossModules(module1, module2, dialer1, reflect.TypeOf(dialer2)) if err != nil { t.Fatal(err) }