-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interp: create real recursive types with unsafe type swapping
As the unsafe and pointer methods in `reflect` are to be depreciated, and seeing no replacement functions, it is now forced that some unsafe is needed to replace this as when and interface is dereferenced it is made unsettable by reflect. With this in mind, this adds real recursive types by hot swapping the struct field type on the fly. This removes a lot of compensation code, simplifying all previous cases. **Note:** While the struct field type is swapped for the real type, the type string is not changed. Due to this, unsafe will recreate the same type.
- Loading branch information
Showing
6 changed files
with
167 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package unsafe2 | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
type dummy struct{} | ||
|
||
// DummyType represents a stand-in for a recursive type. | ||
var DummyType = reflect.TypeOf(dummy{}) | ||
|
||
type rtype struct { | ||
_ [48]byte | ||
} | ||
|
||
type emptyInterface struct { | ||
typ *rtype | ||
_ unsafe.Pointer | ||
} | ||
|
||
type structField struct { | ||
_ int64 | ||
typ *rtype | ||
_ uintptr | ||
} | ||
|
||
type structType struct { | ||
rtype | ||
_ int64 | ||
fields []structField | ||
} | ||
|
||
// SwapFieldType swaps the type of the struct field with the given type. | ||
// | ||
// The struct type must have been created at runtime. This is very unsafe. | ||
func SwapFieldType(s reflect.Type, idx int, t reflect.Type) { | ||
if s.Kind() != reflect.Struct || idx >= s.NumField() { | ||
return | ||
} | ||
|
||
rtyp := unpackType(s) | ||
styp := (*structType)(unsafe.Pointer(rtyp)) | ||
f := styp.fields[idx] | ||
f.typ = unpackType(t) | ||
styp.fields[idx] = f | ||
} | ||
|
||
func unpackType(t reflect.Type) *rtype { | ||
v := reflect.New(t).Elem().Interface() | ||
return (*emptyInterface)(unsafe.Pointer(&v)).typ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package unsafe2_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/traefik/yaegi/internal/unsafe2" | ||
) | ||
|
||
func TestSwapFieldType(t *testing.T) { | ||
f := []reflect.StructField{ | ||
{ | ||
Name: "A", | ||
Type: reflect.TypeOf(int(0)), | ||
}, | ||
{ | ||
Name: "B", | ||
Type: reflect.PtrTo(unsafe2.DummyType), | ||
}, | ||
{ | ||
Name: "C", | ||
Type: reflect.TypeOf(int64(0)), | ||
}, | ||
} | ||
typ := reflect.StructOf(f) | ||
ntyp := reflect.PtrTo(typ) | ||
|
||
unsafe2.SwapFieldType(typ, 1, ntyp) | ||
|
||
if typ.Field(1).Type != ntyp { | ||
t.Fatalf("unexpected field type: want %s; got %s", ntyp, typ.Field(1).Type) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.