diff options
| author | Russ Cox <rsc@golang.org> | 2008-11-24 13:04:27 -0800 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2008-11-24 13:04:27 -0800 |
| commit | b65a930453232646b1511414bcdbc6e05b9db476 (patch) | |
| tree | dcbf89c2abddd807510ee54812d5493c6f3c182b /src/lib/reflect | |
| parent | 508277debe6230ef210cf0165c393e6ff2fd0d4b (diff) | |
| download | go-b65a930453232646b1511414bcdbc6e05b9db476.tar.xz | |
utf8: add InString routines for decoding in strings
reflect: add InterfaceValue.Get(), remove Empty
strconv: add Quote, CanBackquote
fmt:
* %q go-quoted " string
* %#q go-quoted ` string if possible, " string otherwise
* %x hexadecimal string
* anywhere a string is okay, *[]byte is okay
* flags # 0 - + space
* print value inside interface, not interface itself
* tests
R=r
DELTA=756 (597 added, 121 deleted, 38 changed)
OCL=19888
CL=19916
Diffstat (limited to 'src/lib/reflect')
| -rw-r--r-- | src/lib/reflect/all_test.go | 11 | ||||
| -rw-r--r-- | src/lib/reflect/cast_amd64.s | 10 | ||||
| -rwxr-xr-x | src/lib/reflect/gencast.sh | 1 | ||||
| -rw-r--r-- | src/lib/reflect/value.go | 14 |
4 files changed, 31 insertions, 5 deletions
diff --git a/src/lib/reflect/all_test.go b/src/lib/reflect/all_test.go index 203413e552..a6ac1a7c79 100644 --- a/src/lib/reflect/all_test.go +++ b/src/lib/reflect/all_test.go @@ -283,3 +283,14 @@ export func TestAll(tt *testing.T) { // TODO(r): wrap up better println(a[i]); } } + +export func TestInterfaceGet(t *testing.T) { + var inter struct { e interface{ } }; + inter.e = 123.456; + v1 := reflect.NewValue(&inter); + v2 := v1.(reflect.PtrValue).Sub().(reflect.StructValue).Field(0); + assert(v2.Type().String(), "interface { }"); + i2 := v2.(reflect.InterfaceValue).Get(); + v3 := reflect.NewValue(i2); + assert(v3.Type().String(), "float"); +} diff --git a/src/lib/reflect/cast_amd64.s b/src/lib/reflect/cast_amd64.s index a1363718ca..d0e97a3c2c 100644 --- a/src/lib/reflect/cast_amd64.s +++ b/src/lib/reflect/cast_amd64.s @@ -181,3 +181,13 @@ TEXT reflect·PtrRuntimeArrayToAddr(SB),7,$-8 MOVQ AX, 16(SP) RET +TEXT reflect·AddrToPtrInterface(SB),7,$-8 + MOVQ 8(SP), AX + MOVQ AX, 16(SP) + RET + +TEXT reflect·PtrInterfaceToAddr(SB),7,$-8 + MOVQ 8(SP), AX + MOVQ AX, 16(SP) + RET + diff --git a/src/lib/reflect/gencast.sh b/src/lib/reflect/gencast.sh index af90d8df2f..afb60de1a4 100755 --- a/src/lib/reflect/gencast.sh +++ b/src/lib/reflect/gencast.sh @@ -38,4 +38,5 @@ Float80 String Bool RuntimeArray +Interface ! diff --git a/src/lib/reflect/value.go b/src/lib/reflect/value.go index ef6ddce7ae..65d4b5ca97 100644 --- a/src/lib/reflect/value.go +++ b/src/lib/reflect/value.go @@ -36,14 +36,13 @@ func AddrToPtrString(Addr) *string func AddrToPtrBool(Addr) *bool func AddrToPtrRuntimeArray(Addr) *RuntimeArray func PtrRuntimeArrayToAddr(*RuntimeArray) Addr - -export type Empty interface {} // TODO(r): Delete when no longer needed? +func AddrToPtrInterface(Addr) *interface{} export type Value interface { Kind() int; Type() Type; Addr() Addr; - Interface() Empty; + Interface() interface {}; } // Common fields and functionality for all values @@ -66,7 +65,7 @@ func (c *Common) Addr() Addr { return c.addr } -func (c *Common) Interface() Empty { +func (c *Common) Interface() interface {} { return sys.unreflect(*AddrToPtrAddr(c.addr), c.typ.String()); } @@ -714,12 +713,17 @@ func StructCreator(typ Type, addr Addr) Value { export type InterfaceValue interface { Kind() int; Type() Type; + Get() interface {}; } type InterfaceValueStruct struct { Common } +func (v *InterfaceValueStruct) Get() interface{} { + return *AddrToPtrInterface(v.addr); +} + func InterfaceCreator(typ Type, addr Addr) Value { return &InterfaceValueStruct{ Common{InterfaceKind, typ, addr} } } @@ -824,7 +828,7 @@ export func NewOpenArrayValue(typ ArrayType, len, cap int) ArrayValue { return NewValueAddr(typ, PtrRuntimeArrayToAddr(array)); } -export func NewValue(e Empty) Value { +export func NewValue(e interface {}) Value { value, typestring := sys.reflect(e); p, ok := typecache[typestring]; if !ok { |
