aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/reflect/value.go
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2019-01-20 17:30:27 +0100
committerAyke van Laethem <[email protected]>2019-02-05 17:11:09 +0100
commitdfef16813927fa41b40b50e0722bc666f98f71d6 (patch)
treef5c4285bf0f7174f9e471b5e4c1fe4013a3a9d64 /src/reflect/value.go
parent101f2e519baca892940112a9711ec669250ea2f4 (diff)
downloadtinygo-dfef16813927fa41b40b50e0722bc666f98f71d6.tar.gz
tinygo-dfef16813927fa41b40b50e0722bc666f98f71d6.zip
reflect: add limited support for all type kinds
This commit makes sure all Go types can be encoded in the interface type code, so that Type.Kind() always returns a proper type kind for any non-nil interface.
Diffstat (limited to 'src/reflect/value.go')
-rw-r--r--src/reflect/value.go35
1 files changed, 31 insertions, 4 deletions
diff --git a/src/reflect/value.go b/src/reflect/value.go
index d45f0b5c5..8d8d59c9d 100644
--- a/src/reflect/value.go
+++ b/src/reflect/value.go
@@ -31,16 +31,38 @@ func (v Value) Kind() Kind {
}
func (v Value) IsNil() bool {
- panic("unimplemented: (reflect.Value).IsNil()")
+ switch v.Kind() {
+ case Chan, Map, Ptr:
+ return v.value == nil
+ case Func:
+ if v.value == nil {
+ return true
+ }
+ fn := (*funcHeader)(v.value)
+ return fn.Code == nil
+ case Slice:
+ if v.value == nil {
+ return true
+ }
+ slice := (*SliceHeader)(v.value)
+ return slice.Data == 0
+ case Interface:
+ panic("unimplemented: (reflect.Value).IsNil()")
+ default:
+ panic(&ValueError{"IsNil"})
+ }
}
func (v Value) Pointer() uintptr {
switch v.Kind() {
- case UnsafePointer:
+ case Chan, Map, Ptr, UnsafePointer:
return uintptr(v.value)
- case Chan, Func, Map, Ptr, Slice:
+ case Slice:
+ slice := (*SliceHeader)(v.value)
+ return slice.Data
+ case Func:
panic("unimplemented: (reflect.Value).Pointer()")
- default:
+ default: // not implemented: Func
panic(&ValueError{"Pointer"})
}
}
@@ -322,6 +344,11 @@ func MakeSlice(typ Type, len, cap int) Value {
panic("unimplemented: reflect.MakeSlice()")
}
+type funcHeader struct {
+ Context unsafe.Pointer
+ Code unsafe.Pointer
+}
+
type SliceHeader struct {
Data uintptr
Len uintptr