github.com/aykevl/tinygo@v0.5.0/src/reflect/value.go (about) 1 package reflect 2 3 import ( 4 "unsafe" 5 ) 6 7 type Value struct { 8 typecode Type 9 value unsafe.Pointer 10 indirect bool 11 } 12 13 func Indirect(v Value) Value { 14 if v.Kind() != Ptr { 15 return v 16 } 17 return v.Elem() 18 } 19 20 func ValueOf(i interface{}) Value { 21 v := (*interfaceHeader)(unsafe.Pointer(&i)) 22 return Value{ 23 typecode: v.typecode, 24 value: v.value, 25 } 26 } 27 28 func (v Value) Interface() interface{} { 29 i := interfaceHeader{ 30 typecode: v.typecode, 31 value: v.value, 32 } 33 if v.indirect && v.Type().Size() <= unsafe.Sizeof(uintptr(0)) { 34 // Value was indirect but must be put back directly in the interface 35 // value. 36 var value uintptr 37 for j := v.Type().Size(); j != 0; j-- { 38 value = (value << 8) | uintptr(*(*uint8)(unsafe.Pointer(uintptr(v.value) + j - 1))) 39 } 40 i.value = unsafe.Pointer(value) 41 } 42 return *(*interface{})(unsafe.Pointer(&i)) 43 } 44 45 func (v Value) Type() Type { 46 return v.typecode 47 } 48 49 func (v Value) Kind() Kind { 50 return v.Type().Kind() 51 } 52 53 func (v Value) IsNil() bool { 54 switch v.Kind() { 55 case Chan, Map, Ptr: 56 return v.value == nil 57 case Func: 58 if v.value == nil { 59 return true 60 } 61 fn := (*funcHeader)(v.value) 62 return fn.Code == nil 63 case Slice: 64 if v.value == nil { 65 return true 66 } 67 slice := (*SliceHeader)(v.value) 68 return slice.Data == 0 69 case Interface: 70 if v.value == nil { 71 return true 72 } 73 itf := (*interfaceHeader)(v.value) 74 return itf.value == nil 75 default: 76 panic(&ValueError{"IsNil"}) 77 } 78 } 79 80 func (v Value) Pointer() uintptr { 81 switch v.Kind() { 82 case Chan, Map, Ptr, UnsafePointer: 83 return uintptr(v.value) 84 case Slice: 85 slice := (*SliceHeader)(v.value) 86 return slice.Data 87 case Func: 88 panic("unimplemented: (reflect.Value).Pointer()") 89 default: // not implemented: Func 90 panic(&ValueError{"Pointer"}) 91 } 92 } 93 94 func (v Value) IsValid() bool { 95 panic("unimplemented: (reflect.Value).IsValid()") 96 } 97 98 func (v Value) CanInterface() bool { 99 // No Value types of private data can be constructed at the moment. 100 return true 101 } 102 103 func (v Value) CanAddr() bool { 104 panic("unimplemented: (reflect.Value).CanAddr()") 105 } 106 107 func (v Value) Addr() Value { 108 panic("unimplemented: (reflect.Value).Addr()") 109 } 110 111 func (v Value) CanSet() bool { 112 return v.indirect 113 } 114 115 func (v Value) Bool() bool { 116 switch v.Kind() { 117 case Bool: 118 if v.indirect { 119 return *((*bool)(v.value)) 120 } else { 121 return uintptr(v.value) != 0 122 } 123 default: 124 panic(&ValueError{"Bool"}) 125 } 126 } 127 128 func (v Value) Int() int64 { 129 switch v.Kind() { 130 case Int: 131 if v.indirect || unsafe.Sizeof(int(0)) > unsafe.Sizeof(uintptr(0)) { 132 return int64(*(*int)(v.value)) 133 } else { 134 return int64(int(uintptr(v.value))) 135 } 136 case Int8: 137 if v.indirect { 138 return int64(*(*int8)(v.value)) 139 } else { 140 return int64(int8(uintptr(v.value))) 141 } 142 case Int16: 143 if v.indirect { 144 return int64(*(*int16)(v.value)) 145 } else { 146 return int64(int16(uintptr(v.value))) 147 } 148 case Int32: 149 if v.indirect || unsafe.Sizeof(int32(0)) > unsafe.Sizeof(uintptr(0)) { 150 return int64(*(*int32)(v.value)) 151 } else { 152 return int64(int32(uintptr(v.value))) 153 } 154 case Int64: 155 if v.indirect || unsafe.Sizeof(int64(0)) > unsafe.Sizeof(uintptr(0)) { 156 return int64(*(*int64)(v.value)) 157 } else { 158 return int64(int64(uintptr(v.value))) 159 } 160 default: 161 panic(&ValueError{"Int"}) 162 } 163 } 164 165 func (v Value) Uint() uint64 { 166 switch v.Kind() { 167 case Uintptr: 168 if v.indirect { 169 return uint64(*(*uintptr)(v.value)) 170 } else { 171 return uint64(uintptr(v.value)) 172 } 173 case Uint8: 174 if v.indirect { 175 return uint64(*(*uint8)(v.value)) 176 } else { 177 return uint64(uintptr(v.value)) 178 } 179 case Uint16: 180 if v.indirect { 181 return uint64(*(*uint16)(v.value)) 182 } else { 183 return uint64(uintptr(v.value)) 184 } 185 case Uint: 186 if v.indirect || unsafe.Sizeof(uint(0)) > unsafe.Sizeof(uintptr(0)) { 187 return uint64(*(*uint)(v.value)) 188 } else { 189 return uint64(uintptr(v.value)) 190 } 191 case Uint32: 192 if v.indirect || unsafe.Sizeof(uint32(0)) > unsafe.Sizeof(uintptr(0)) { 193 return uint64(*(*uint32)(v.value)) 194 } else { 195 return uint64(uintptr(v.value)) 196 } 197 case Uint64: 198 if v.indirect || unsafe.Sizeof(uint64(0)) > unsafe.Sizeof(uintptr(0)) { 199 return uint64(*(*uint64)(v.value)) 200 } else { 201 return uint64(uintptr(v.value)) 202 } 203 default: 204 panic(&ValueError{"Uint"}) 205 } 206 } 207 208 func (v Value) Float() float64 { 209 switch v.Kind() { 210 case Float32: 211 if v.indirect || unsafe.Sizeof(float32(0)) > unsafe.Sizeof(uintptr(0)) { 212 // The float is stored as an external value on systems with 16-bit 213 // pointers. 214 return float64(*(*float32)(v.value)) 215 } else { 216 // The float is directly stored in the interface value on systems 217 // with 32-bit and 64-bit pointers. 218 return float64(*(*float32)(unsafe.Pointer(&v.value))) 219 } 220 case Float64: 221 if v.indirect || unsafe.Sizeof(float64(0)) > unsafe.Sizeof(uintptr(0)) { 222 // For systems with 16-bit and 32-bit pointers. 223 return *(*float64)(v.value) 224 } else { 225 // The float is directly stored in the interface value on systems 226 // with 64-bit pointers. 227 return *(*float64)(unsafe.Pointer(&v.value)) 228 } 229 default: 230 panic(&ValueError{"Float"}) 231 } 232 } 233 234 func (v Value) Complex() complex128 { 235 switch v.Kind() { 236 case Complex64: 237 if v.indirect || unsafe.Sizeof(complex64(0)) > unsafe.Sizeof(uintptr(0)) { 238 // The complex number is stored as an external value on systems with 239 // 16-bit and 32-bit pointers. 240 return complex128(*(*complex64)(v.value)) 241 } else { 242 // The complex number is directly stored in the interface value on 243 // systems with 64-bit pointers. 244 return complex128(*(*complex64)(unsafe.Pointer(&v.value))) 245 } 246 case Complex128: 247 // This is a 128-bit value, which is always stored as an external value. 248 // It may be stored in the pointer directly on very uncommon 249 // architectures with 128-bit pointers, however. 250 return *(*complex128)(v.value) 251 default: 252 panic(&ValueError{"Complex"}) 253 } 254 } 255 256 func (v Value) String() string { 257 switch v.Kind() { 258 case String: 259 // A string value is always bigger than a pointer as it is made of a 260 // pointer and a length. 261 return *(*string)(v.value) 262 default: 263 // Special case because of the special treatment of .String() in Go. 264 return "<T>" 265 } 266 } 267 268 func (v Value) Bytes() []byte { 269 panic("unimplemented: (reflect.Value).Bytes()") 270 } 271 272 func (v Value) Slice(i, j int) Value { 273 panic("unimplemented: (reflect.Value).Slice()") 274 } 275 276 func (v Value) Len() int { 277 t := v.Type() 278 switch t.Kind() { 279 case Slice: 280 return int((*SliceHeader)(v.value).Len) 281 case String: 282 return int((*StringHeader)(v.value).Len) 283 default: // Array, Chan, Map 284 panic("unimplemented: (reflect.Value).Len()") 285 } 286 } 287 288 func (v Value) Cap() int { 289 t := v.Type() 290 switch t.Kind() { 291 case Slice: 292 return int((*SliceHeader)(v.value).Cap) 293 default: // Array, Chan 294 panic("unimplemented: (reflect.Value).Cap()") 295 } 296 } 297 298 func (v Value) NumField() int { 299 panic("unimplemented: (reflect.Value).NumField()") 300 } 301 302 func (v Value) Elem() Value { 303 switch v.Kind() { 304 case Ptr: 305 ptr := v.value 306 if v.indirect { 307 ptr = *(*unsafe.Pointer)(ptr) 308 } 309 if ptr == nil { 310 return Value{} 311 } 312 return Value{ 313 typecode: v.Type().Elem(), 314 value: ptr, 315 indirect: true, 316 } 317 default: // not implemented: Interface 318 panic(&ValueError{"Elem"}) 319 } 320 } 321 322 func (v Value) Field(i int) Value { 323 panic("unimplemented: (reflect.Value).Field()") 324 } 325 326 func (v Value) Index(i int) Value { 327 switch v.Kind() { 328 case Slice: 329 // Extract an element from the slice. 330 slice := *(*SliceHeader)(v.value) 331 if uint(i) >= uint(slice.Len) { 332 panic("reflect: slice index out of range") 333 } 334 elem := Value{ 335 typecode: v.Type().Elem(), 336 indirect: true, 337 } 338 addr := uintptr(slice.Data) + elem.Type().Size()*uintptr(i) // pointer to new value 339 elem.value = unsafe.Pointer(addr) 340 return elem 341 case String: 342 // Extract a character from a string. 343 // A string is never stored directly in the interface, but always as a 344 // pointer to the string value. 345 s := *(*StringHeader)(v.value) 346 if uint(i) >= uint(s.Len) { 347 panic("reflect: string index out of range") 348 } 349 return Value{ 350 typecode: Uint8.basicType(), 351 value: unsafe.Pointer(uintptr(*(*uint8)(unsafe.Pointer(s.Data + uintptr(i))))), 352 } 353 case Array: 354 panic("unimplemented: (reflect.Value).Index()") 355 default: 356 panic(&ValueError{"Index"}) 357 } 358 } 359 360 func (v Value) MapKeys() []Value { 361 panic("unimplemented: (reflect.Value).MapKeys()") 362 } 363 364 func (v Value) MapIndex(key Value) Value { 365 panic("unimplemented: (reflect.Value).MapIndex()") 366 } 367 368 func (v Value) MapRange() *MapIter { 369 panic("unimplemented: (reflect.Value).MapRange()") 370 } 371 372 type MapIter struct { 373 } 374 375 func (it *MapIter) Key() Value { 376 panic("unimplemented: (*reflect.MapIter).Key()") 377 } 378 379 func (it *MapIter) Value() Value { 380 panic("unimplemented: (*reflect.MapIter).Value()") 381 } 382 383 func (it *MapIter) Next() bool { 384 panic("unimplemented: (*reflect.MapIter).Next()") 385 } 386 387 func (v Value) Set(x Value) { 388 if !v.indirect { 389 panic("reflect: value is not addressable") 390 } 391 if v.Type() != x.Type() { 392 if v.Kind() == Interface { 393 panic("reflect: unimplemented: assigning to interface of different type") 394 } else { 395 panic("reflect: cannot assign") 396 } 397 } 398 size := v.Type().Size() 399 xptr := x.value 400 if size <= unsafe.Sizeof(uintptr(0)) && !x.indirect { 401 value := x.value 402 xptr = unsafe.Pointer(&value) 403 } 404 memcpy(v.value, xptr, size) 405 } 406 407 func (v Value) SetBool(x bool) { 408 if !v.indirect { 409 panic("reflect: value is not addressable") 410 } 411 switch v.Kind() { 412 case Bool: 413 *(*bool)(v.value) = x 414 default: 415 panic(&ValueError{"SetBool"}) 416 } 417 } 418 419 func (v Value) SetInt(x int64) { 420 if !v.indirect { 421 panic("reflect: value is not addressable") 422 } 423 switch v.Kind() { 424 case Int: 425 *(*int)(v.value) = int(x) 426 case Int8: 427 *(*int8)(v.value) = int8(x) 428 case Int16: 429 *(*int16)(v.value) = int16(x) 430 case Int32: 431 *(*int32)(v.value) = int32(x) 432 case Int64: 433 *(*int64)(v.value) = x 434 default: 435 panic(&ValueError{"SetInt"}) 436 } 437 } 438 439 func (v Value) SetUint(x uint64) { 440 if !v.indirect { 441 panic("reflect: value is not addressable") 442 } 443 switch v.Kind() { 444 case Uint: 445 *(*uint)(v.value) = uint(x) 446 case Uint8: 447 *(*uint8)(v.value) = uint8(x) 448 case Uint16: 449 *(*uint16)(v.value) = uint16(x) 450 case Uint32: 451 *(*uint32)(v.value) = uint32(x) 452 case Uint64: 453 *(*uint64)(v.value) = x 454 case Uintptr: 455 *(*uintptr)(v.value) = uintptr(x) 456 default: 457 panic(&ValueError{"SetUint"}) 458 } 459 } 460 461 func (v Value) SetFloat(x float64) { 462 if !v.indirect { 463 panic("reflect: value is not addressable") 464 } 465 switch v.Kind() { 466 case Float32: 467 *(*float32)(v.value) = float32(x) 468 case Float64: 469 *(*float64)(v.value) = x 470 default: 471 panic(&ValueError{"SetFloat"}) 472 } 473 } 474 475 func (v Value) SetComplex(x complex128) { 476 if !v.indirect { 477 panic("reflect: value is not addressable") 478 } 479 switch v.Kind() { 480 case Complex64: 481 *(*complex64)(v.value) = complex64(x) 482 case Complex128: 483 *(*complex128)(v.value) = x 484 default: 485 panic(&ValueError{"SetComplex"}) 486 } 487 } 488 489 func (v Value) SetString(x string) { 490 if !v.indirect { 491 panic("reflect: value is not addressable") 492 } 493 switch v.Kind() { 494 case String: 495 *(*string)(v.value) = x 496 default: 497 panic(&ValueError{"SetString"}) 498 } 499 } 500 501 func MakeSlice(typ Type, len, cap int) Value { 502 panic("unimplemented: reflect.MakeSlice()") 503 } 504 505 type funcHeader struct { 506 Context unsafe.Pointer 507 Code unsafe.Pointer 508 } 509 510 // This is the same thing as an interface{}. 511 type interfaceHeader struct { 512 typecode Type 513 value unsafe.Pointer 514 } 515 516 type SliceHeader struct { 517 Data uintptr 518 Len uintptr 519 Cap uintptr 520 } 521 522 type StringHeader struct { 523 Data uintptr 524 Len uintptr 525 } 526 527 type ValueError struct { 528 Method string 529 } 530 531 func (e *ValueError) Error() string { 532 return "reflect: call of reflect.Value." + e.Method + " on invalid type" 533 } 534 535 //go:linkname memcpy runtime.memcpy 536 func memcpy(dst, src unsafe.Pointer, size uintptr)