2008-10-22 12:02:56 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Reflection library.
|
|
|
|
// Handling values.
|
|
|
|
|
|
|
|
package reflect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect";
|
2008-12-09 16:41:21 -07:00
|
|
|
"unsafe";
|
2008-10-22 12:02:56 -06:00
|
|
|
)
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Addr unsafe.Pointer
|
2008-11-04 23:54:11 -07:00
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func equalType(a, b Type) bool {
|
2008-12-10 16:55:59 -07:00
|
|
|
return a.String() == b.String()
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Value interface {
|
2008-10-22 12:02:56 -06:00
|
|
|
Kind() int;
|
|
|
|
Type() Type;
|
2008-11-12 15:19:39 -07:00
|
|
|
Addr() Addr;
|
2008-11-24 15:51:33 -07:00
|
|
|
Interface() interface {};
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 17:16:42 -07:00
|
|
|
// commonValue fields and functionality for all values
|
2008-10-26 09:28:33 -06:00
|
|
|
|
2009-01-15 17:16:42 -07:00
|
|
|
type commonValue struct {
|
2008-10-26 09:28:33 -06:00
|
|
|
kind int;
|
|
|
|
typ Type;
|
|
|
|
addr Addr;
|
|
|
|
}
|
|
|
|
|
2009-01-15 17:16:42 -07:00
|
|
|
func (c *commonValue) Kind() int {
|
2008-10-26 09:28:33 -06:00
|
|
|
return c.kind
|
|
|
|
}
|
|
|
|
|
2009-01-15 17:16:42 -07:00
|
|
|
func (c *commonValue) Type() Type {
|
2008-10-26 09:28:33 -06:00
|
|
|
return c.typ
|
|
|
|
}
|
|
|
|
|
2009-01-15 17:16:42 -07:00
|
|
|
func (c *commonValue) Addr() Addr {
|
2008-11-12 15:19:39 -07:00
|
|
|
return c.addr
|
|
|
|
}
|
|
|
|
|
2009-01-15 17:16:42 -07:00
|
|
|
func (c *commonValue) Interface() interface {} {
|
2009-01-09 01:17:46 -07:00
|
|
|
var i interface {};
|
|
|
|
if c.typ.Size() > 8 { // TODO(rsc): how do we know it is 8?
|
2009-01-16 15:58:14 -07:00
|
|
|
i = sys.Unreflect(c.addr.(uintptr).(uint64), c.typ.String(), true);
|
2009-01-09 01:17:46 -07:00
|
|
|
} else {
|
|
|
|
if uintptr(c.addr) == 0 {
|
|
|
|
panicln("reflect: address 0 for", c.typ.String());
|
|
|
|
}
|
2009-01-16 15:58:14 -07:00
|
|
|
i = sys.Unreflect(uint64(uintptr(*c.addr.(*Addr))), c.typ.String(), false);
|
2008-12-19 04:06:19 -07:00
|
|
|
}
|
2009-01-09 01:17:46 -07:00
|
|
|
return i;
|
2008-11-04 23:54:11 -07:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func newValueAddr(typ Type, addr Addr) Value
|
2008-10-22 12:02:56 -06:00
|
|
|
|
2009-01-30 15:39:31 -07:00
|
|
|
type creatorFn func(typ Type, addr Addr) Value
|
2008-10-22 12:02:56 -06:00
|
|
|
|
|
|
|
|
2008-11-02 13:32:14 -07:00
|
|
|
// -- Missing
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type MissingValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-11-02 13:32:14 -07:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type missingValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-11-02 13:32:14 -07:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func missingCreator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &missingValueStruct( commonValue(MissingKind, typ, addr) )
|
2008-11-02 13:32:14 -07:00
|
|
|
}
|
|
|
|
|
2008-10-29 16:31:02 -06:00
|
|
|
// -- Int
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type IntValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-29 16:31:02 -06:00
|
|
|
Get() int;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(int);
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type intValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func intCreator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &intValueStruct( commonValue(IntKind, typ, addr) )
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *intValueStruct) Get() int {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*int)
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *intValueStruct) Set(i int) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*int) = i
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
// -- Int8
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Int8Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
Get() int8;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(int8);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type int8ValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func int8Creator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &int8ValueStruct( commonValue(Int8Kind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *int8ValueStruct) Get() int8 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*int8)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *int8ValueStruct) Set(i int8) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*int8) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Int16
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Int16Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
Get() int16;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(int16);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type int16ValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func int16Creator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &int16ValueStruct( commonValue(Int16Kind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *int16ValueStruct) Get() int16 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*int16)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *int16ValueStruct) Set(i int16) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*int16) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Int32
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Int32Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
Get() int32;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(int32);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type int32ValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func int32Creator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &int32ValueStruct( commonValue(Int32Kind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *int32ValueStruct) Get() int32 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*int32)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *int32ValueStruct) Set(i int32) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*int32) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Int64
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Int64Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
Get() int64;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(int64);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type int64ValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func int64Creator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &int64ValueStruct( commonValue(Int64Kind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *int64ValueStruct) Get() int64 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*int64)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *int64ValueStruct) Set(i int64) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*int64) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-29 16:31:02 -06:00
|
|
|
// -- Uint
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type UintValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-29 16:31:02 -06:00
|
|
|
Get() uint;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(uint);
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type uintValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func uintCreator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &uintValueStruct( commonValue(UintKind, typ, addr) )
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uintValueStruct) Get() uint {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*uint)
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uintValueStruct) Set(i uint) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*uint) = i
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
// -- Uint8
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Uint8Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
Get() uint8;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(uint8);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type uint8ValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func uint8Creator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &uint8ValueStruct( commonValue(Uint8Kind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uint8ValueStruct) Get() uint8 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*uint8)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uint8ValueStruct) Set(i uint8) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*uint8) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Uint16
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Uint16Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
Get() uint16;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(uint16);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type uint16ValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func uint16Creator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &uint16ValueStruct( commonValue(Uint16Kind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uint16ValueStruct) Get() uint16 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*uint16)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uint16ValueStruct) Set(i uint16) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*uint16) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Uint32
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Uint32Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
Get() uint32;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(uint32);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type uint32ValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func uint32Creator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &uint32ValueStruct( commonValue(Uint32Kind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uint32ValueStruct) Get() uint32 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*uint32)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uint32ValueStruct) Set(i uint32) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*uint32) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Uint64
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Uint64Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
Get() uint64;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(uint64);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type uint64ValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func uint64Creator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &uint64ValueStruct( commonValue(Uint64Kind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uint64ValueStruct) Get() uint64 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*uint64)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uint64ValueStruct) Set(i uint64) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*uint64) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-12-11 15:41:12 -07:00
|
|
|
// -- Uintptr
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type UintptrValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-12-11 15:41:12 -07:00
|
|
|
Get() uintptr;
|
|
|
|
Set(uintptr);
|
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type uintptrValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-12-11 15:41:12 -07:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func uintptrCreator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &uintptrValueStruct( commonValue(UintptrKind, typ, addr) )
|
2008-12-11 15:41:12 -07:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uintptrValueStruct) Get() uintptr {
|
2008-12-11 15:41:12 -07:00
|
|
|
return *v.addr.(*uintptr)
|
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uintptrValueStruct) Set(i uintptr) {
|
2008-12-11 15:41:12 -07:00
|
|
|
*v.addr.(*uintptr) = i
|
|
|
|
}
|
|
|
|
|
2008-10-29 16:31:02 -06:00
|
|
|
// -- Float
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type FloatValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-29 16:31:02 -06:00
|
|
|
Get() float;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(float);
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type floatValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func floatCreator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &floatValueStruct( commonValue(FloatKind, typ, addr) )
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *floatValueStruct) Get() float {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*float)
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *floatValueStruct) Set(f float) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*float) = f
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
// -- Float32
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Float32Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
Get() float32;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(float32);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type float32ValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func float32Creator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &float32ValueStruct( commonValue(Float32Kind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *float32ValueStruct) Get() float32 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*float32)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *float32ValueStruct) Set(f float32) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*float32) = f
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Float64
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Float64Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
Get() float64;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(float64);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type float64ValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func float64Creator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &float64ValueStruct( commonValue(Float64Kind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *float64ValueStruct) Get() float64 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*float64)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *float64ValueStruct) Set(f float64) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*float64) = f
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Float80
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Float80Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
Get() float80;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(float80);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type float80ValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func float80Creator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &float80ValueStruct( commonValue(Float80Kind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
BUG: can't gen code for float80s
|
|
|
|
func (v *Float80ValueStruct) Get() float80 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*float80)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *Float80ValueStruct) Set(f float80) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*float80) = f
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// -- String
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type StringValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
Get() string;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(string);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type stringValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func stringCreator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &stringValueStruct( commonValue(StringKind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *stringValueStruct) Get() string {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*string)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *stringValueStruct) Set(s string) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*string) = s
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-31 17:34:47 -06:00
|
|
|
// -- Bool
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type BoolValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-31 17:34:47 -06:00
|
|
|
Get() bool;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(bool);
|
2008-10-31 17:34:47 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type boolValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-31 17:34:47 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func boolCreator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &boolValueStruct( commonValue(BoolKind, typ, addr) )
|
2008-10-31 17:34:47 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *boolValueStruct) Get() bool {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*bool)
|
2008-10-31 17:34:47 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *boolValueStruct) Set(b bool) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*bool) = b
|
2008-10-31 17:34:47 -06:00
|
|
|
}
|
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
// -- Pointer
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type PtrValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-24 17:33:29 -06:00
|
|
|
Sub() Value;
|
|
|
|
Get() Addr;
|
2008-11-12 15:19:39 -07:00
|
|
|
SetSub(Value);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type ptrValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *ptrValueStruct) Get() Addr {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*Addr)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *ptrValueStruct) Sub() Value {
|
|
|
|
return newValueAddr(v.typ.(PtrType).Sub(), v.Get());
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *ptrValueStruct) SetSub(subv Value) {
|
2008-12-10 16:55:59 -07:00
|
|
|
a := v.typ.(PtrType).Sub();
|
|
|
|
b := subv.Type();
|
2009-01-16 13:48:07 -07:00
|
|
|
if !equalType(a, b) {
|
2008-12-10 16:55:59 -07:00
|
|
|
panicln("reflect: incompatible types in PtrValue.SetSub:",
|
|
|
|
a.String(), b.String());
|
2008-11-12 20:05:05 -07:00
|
|
|
}
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*Addr) = subv.Addr();
|
2008-11-12 15:19:39 -07:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func ptrCreator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &ptrValueStruct( commonValue(PtrKind, typ, addr) );
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-26 09:28:33 -06:00
|
|
|
// -- Array
|
2009-01-23 16:56:04 -07:00
|
|
|
// Slices and arrays are represented by the same interface.
|
2008-10-22 12:02:56 -06:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type ArrayValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-01-23 16:56:04 -07:00
|
|
|
IsSlice() bool;
|
2008-11-05 11:17:38 -07:00
|
|
|
Len() int;
|
2008-11-13 14:42:59 -07:00
|
|
|
Cap() int;
|
2008-11-05 11:17:38 -07:00
|
|
|
Elem(i int) Value;
|
2008-11-13 14:42:59 -07:00
|
|
|
SetLen(len int);
|
2009-01-23 13:40:55 -07:00
|
|
|
Set(src ArrayValue);
|
2009-01-21 16:45:54 -07:00
|
|
|
CopyFrom(src ArrayValue, n int)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
2008-10-26 09:28:33 -06:00
|
|
|
|
2009-01-21 16:45:54 -07:00
|
|
|
func copyArray(dst ArrayValue, src ArrayValue, n int);
|
|
|
|
|
2008-10-22 17:48:17 -06:00
|
|
|
/*
|
2009-01-23 16:56:04 -07:00
|
|
|
Run-time representation of slices looks like this:
|
|
|
|
struct Slice {
|
2008-10-22 17:48:17 -06:00
|
|
|
byte* array; // actual data
|
|
|
|
uint32 nel; // number of elements
|
2008-11-13 14:42:59 -07:00
|
|
|
uint32 cap;
|
2008-10-22 17:48:17 -06:00
|
|
|
};
|
|
|
|
*/
|
2009-01-23 16:56:04 -07:00
|
|
|
type runtimeSlice struct {
|
2008-11-13 14:42:59 -07:00
|
|
|
data Addr;
|
|
|
|
len uint32;
|
|
|
|
cap uint32;
|
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
type sliceValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue;
|
2008-11-13 14:42:59 -07:00
|
|
|
elemtype Type;
|
|
|
|
elemsize int;
|
2009-01-23 16:56:04 -07:00
|
|
|
slice *runtimeSlice;
|
2008-11-13 14:42:59 -07:00
|
|
|
}
|
2008-10-22 12:02:56 -06:00
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *sliceValueStruct) IsSlice() bool {
|
2008-10-22 12:02:56 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *sliceValueStruct) Len() int {
|
|
|
|
return int(v.slice.len);
|
2008-11-13 14:42:59 -07:00
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *sliceValueStruct) Cap() int {
|
|
|
|
return int(v.slice.cap);
|
2008-11-13 14:42:59 -07:00
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *sliceValueStruct) SetLen(len int) {
|
2008-11-13 14:42:59 -07:00
|
|
|
if len > v.Cap() {
|
2009-01-23 16:56:04 -07:00
|
|
|
panicln("reflect: sliceValueStruct.SetLen", len, v.Cap());
|
2008-11-13 14:42:59 -07:00
|
|
|
}
|
2009-01-23 16:56:04 -07:00
|
|
|
v.slice.len = uint32(len);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *sliceValueStruct) Set(src ArrayValue) {
|
|
|
|
if !src.IsSlice() {
|
2009-01-23 13:40:55 -07:00
|
|
|
panic("can't set from fixed array");
|
|
|
|
}
|
2009-01-23 16:56:04 -07:00
|
|
|
s := src.(*sliceValueStruct);
|
2009-01-23 13:40:55 -07:00
|
|
|
if !equalType(v.typ, s.typ) {
|
2009-01-23 16:56:04 -07:00
|
|
|
panicln("incompatible types in ArrayValue.Set()");
|
2009-01-23 13:40:55 -07:00
|
|
|
}
|
2009-01-23 16:56:04 -07:00
|
|
|
*v.slice = *s.slice;
|
2009-01-23 13:40:55 -07:00
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *sliceValueStruct) Elem(i int) Value {
|
|
|
|
data_uint := uintptr(v.slice.data) + uintptr(i * v.elemsize);
|
2009-01-16 13:48:07 -07:00
|
|
|
return newValueAddr(v.elemtype, Addr(data_uint));
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *sliceValueStruct) CopyFrom(src ArrayValue, n int) {
|
2009-01-21 16:45:54 -07:00
|
|
|
copyArray(v, src, n);
|
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
type arrayValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue;
|
2008-10-22 12:02:56 -06:00
|
|
|
elemtype Type;
|
2008-11-05 11:17:38 -07:00
|
|
|
elemsize int;
|
|
|
|
len int;
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *arrayValueStruct) IsSlice() bool {
|
2008-10-22 12:02:56 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *arrayValueStruct) Len() int {
|
2008-10-22 12:02:56 -06:00
|
|
|
return v.len
|
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *arrayValueStruct) Cap() int {
|
2008-11-13 14:42:59 -07:00
|
|
|
return v.len
|
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *arrayValueStruct) SetLen(len int) {
|
2008-11-13 14:42:59 -07:00
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *arrayValueStruct) Set(src ArrayValue) {
|
2009-01-23 13:40:55 -07:00
|
|
|
panicln("can't set fixed array");
|
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *arrayValueStruct) Elem(i int) Value {
|
2008-12-09 16:41:21 -07:00
|
|
|
data_uint := uintptr(v.addr) + uintptr(i * v.elemsize);
|
2009-01-16 13:48:07 -07:00
|
|
|
return newValueAddr(v.elemtype, Addr(data_uint));
|
2008-10-22 12:02:56 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *arrayValueStruct) CopyFrom(src ArrayValue, n int) {
|
2009-01-21 16:45:54 -07:00
|
|
|
copyArray(v, src, n);
|
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func arrayCreator(typ Type, addr Addr) Value {
|
2008-10-22 12:02:56 -06:00
|
|
|
arraytype := typ.(ArrayType);
|
2009-01-23 16:56:04 -07:00
|
|
|
if arraytype.IsSlice() {
|
|
|
|
v := new(sliceValueStruct);
|
2008-10-26 09:28:33 -06:00
|
|
|
v.kind = ArrayKind;
|
2008-10-22 17:48:17 -06:00
|
|
|
v.addr = addr;
|
2008-10-22 12:02:56 -06:00
|
|
|
v.typ = typ;
|
2008-10-22 17:48:17 -06:00
|
|
|
v.elemtype = arraytype.Elem();
|
|
|
|
v.elemsize = v.elemtype.Size();
|
2009-01-23 16:56:04 -07:00
|
|
|
v.slice = addr.(*runtimeSlice);
|
2008-10-22 12:02:56 -06:00
|
|
|
return v;
|
|
|
|
}
|
2009-01-23 16:56:04 -07:00
|
|
|
v := new(arrayValueStruct);
|
2008-10-26 09:28:33 -06:00
|
|
|
v.kind = ArrayKind;
|
2008-10-22 17:48:17 -06:00
|
|
|
v.addr = addr;
|
2008-10-22 12:02:56 -06:00
|
|
|
v.typ = typ;
|
|
|
|
v.elemtype = arraytype.Elem();
|
2008-10-22 17:48:17 -06:00
|
|
|
v.elemsize = v.elemtype.Size();
|
|
|
|
v.len = arraytype.Len();
|
2008-10-22 12:02:56 -06:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Map TODO: finish and test
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type MapValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
Len() int;
|
|
|
|
Elem(key Value) Value;
|
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type mapValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-23 13:41:06 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func mapCreator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &mapValueStruct( commonValue(MapKind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *mapValueStruct) Len() int {
|
2008-10-23 13:41:06 -06:00
|
|
|
return 0 // TODO: probably want this to be dynamic
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *mapValueStruct) Elem(key Value) Value {
|
2008-10-22 12:02:56 -06:00
|
|
|
panic("map value element");
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Chan
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type ChanValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type chanValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-23 13:41:06 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func chanCreator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &chanValueStruct( commonValue(ChanKind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Struct
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type StructValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
Len() int;
|
|
|
|
Field(i int) Value;
|
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type structValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue;
|
2008-12-18 23:37:22 -07:00
|
|
|
field []Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *structValueStruct) Len() int {
|
2008-10-22 12:02:56 -06:00
|
|
|
return len(v.field)
|
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *structValueStruct) Field(i int) Value {
|
2008-10-22 12:02:56 -06:00
|
|
|
return v.field[i]
|
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func structCreator(typ Type, addr Addr) Value {
|
2008-10-22 12:02:56 -06:00
|
|
|
t := typ.(StructType);
|
|
|
|
nfield := t.Len();
|
2009-02-13 15:48:32 -07:00
|
|
|
v := &structValueStruct( commonValue(StructKind, typ, addr), make([]Value, nfield) );
|
2008-10-22 12:02:56 -06:00
|
|
|
for i := 0; i < nfield; i++ {
|
2008-10-30 18:29:53 -06:00
|
|
|
name, ftype, str, offset := t.Field(i);
|
2008-12-09 16:41:21 -07:00
|
|
|
addr_uint := uintptr(addr) + uintptr(offset);
|
2009-01-16 13:48:07 -07:00
|
|
|
v.field[i] = newValueAddr(ftype, Addr(addr_uint));
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
v.typ = typ;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Interface
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type InterfaceValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-11-24 15:51:33 -07:00
|
|
|
Get() interface {};
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type interfaceValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *interfaceValueStruct) Get() interface{} {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*interface{})
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func interfaceCreator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &interfaceValueStruct( commonValue(InterfaceKind, typ, addr) )
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Func
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type FuncValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
type funcValueStruct struct {
|
2009-01-15 17:16:42 -07:00
|
|
|
commonValue
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func funcCreator(typ Type, addr Addr) Value {
|
2009-02-13 15:48:32 -07:00
|
|
|
return &funcValueStruct( commonValue(FuncKind, typ, addr) )
|
2009-01-16 13:48:07 -07:00
|
|
|
}
|
|
|
|
|
2009-02-13 15:48:32 -07:00
|
|
|
var creator = map[int] creatorFn (
|
2009-01-30 15:39:31 -07:00
|
|
|
MissingKind : missingCreator,
|
|
|
|
IntKind : intCreator,
|
|
|
|
Int8Kind : int8Creator,
|
|
|
|
Int16Kind : int16Creator,
|
|
|
|
Int32Kind : int32Creator,
|
|
|
|
Int64Kind : int64Creator,
|
|
|
|
UintKind : uintCreator,
|
|
|
|
Uint8Kind : uint8Creator,
|
|
|
|
Uint16Kind : uint16Creator,
|
|
|
|
Uint32Kind : uint32Creator,
|
|
|
|
Uint64Kind : uint64Creator,
|
|
|
|
UintptrKind : uintptrCreator,
|
|
|
|
FloatKind : floatCreator,
|
|
|
|
Float32Kind : float32Creator,
|
|
|
|
Float64Kind : float64Creator,
|
|
|
|
Float80Kind : float80Creator,
|
|
|
|
StringKind : stringCreator,
|
|
|
|
BoolKind : boolCreator,
|
|
|
|
PtrKind : ptrCreator,
|
|
|
|
ArrayKind : arrayCreator,
|
|
|
|
MapKind : mapCreator,
|
|
|
|
ChanKind : chanCreator,
|
|
|
|
StructKind : structCreator,
|
|
|
|
InterfaceKind : interfaceCreator,
|
|
|
|
FuncKind : funcCreator,
|
2009-02-13 15:48:32 -07:00
|
|
|
)
|
2008-12-11 15:41:12 -07:00
|
|
|
|
2009-01-09 01:17:46 -07:00
|
|
|
var typecache = make(map[string] Type);
|
2008-10-22 12:02:56 -06:00
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func newValueAddr(typ Type, addr Addr) Value {
|
2008-10-22 12:02:56 -06:00
|
|
|
c, ok := creator[typ.Kind()];
|
|
|
|
if !ok {
|
|
|
|
panicln("no creator for type" , typ.Kind());
|
|
|
|
}
|
|
|
|
return c(typ, addr);
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func NewInitValue(typ Type) Value {
|
2008-10-22 17:48:17 -06:00
|
|
|
// Some values cannot be made this way.
|
|
|
|
switch typ.Kind() {
|
2008-12-19 04:06:19 -07:00
|
|
|
case FuncKind: // must be pointers, at least for now (TODO?)
|
2008-10-22 17:48:17 -06:00
|
|
|
return nil;
|
|
|
|
case ArrayKind:
|
2009-01-23 16:56:04 -07:00
|
|
|
if typ.(ArrayType).IsSlice() {
|
2008-10-22 17:48:17 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2008-10-22 12:02:56 -06:00
|
|
|
size := typ.Size();
|
|
|
|
if size == 0 {
|
|
|
|
size = 1;
|
|
|
|
}
|
2009-01-06 16:19:02 -07:00
|
|
|
data := make([]uint8, size);
|
2009-01-16 13:48:07 -07:00
|
|
|
return newValueAddr(typ, Addr(&data[0]));
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func NewSliceValue(typ ArrayType, len, cap int) ArrayValue {
|
|
|
|
if !typ.IsSlice() {
|
2008-11-13 14:42:59 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
array := new(runtimeSlice);
|
2008-11-13 14:42:59 -07:00
|
|
|
size := typ.Elem().Size() * cap;
|
|
|
|
if size == 0 {
|
|
|
|
size = 1;
|
|
|
|
}
|
2009-01-06 16:19:02 -07:00
|
|
|
data := make([]uint8, size);
|
2008-12-09 16:41:21 -07:00
|
|
|
array.data = Addr(&data[0]);
|
2008-11-13 14:42:59 -07:00
|
|
|
array.len = uint32(len);
|
|
|
|
array.cap = uint32(cap);
|
|
|
|
|
2009-02-11 18:55:16 -07:00
|
|
|
return newValueAddr(typ, Addr(array)).(ArrayValue);
|
2008-11-13 14:42:59 -07:00
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
// Works on both slices and arrays
|
2009-01-21 16:45:54 -07:00
|
|
|
func copyArray(dst ArrayValue, src ArrayValue, n int) {
|
2008-12-10 16:55:59 -07:00
|
|
|
if n == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dt := dst.Type().(ArrayType).Elem();
|
|
|
|
st := src.Type().(ArrayType).Elem();
|
2009-01-16 13:48:07 -07:00
|
|
|
if !equalType(dt, st) {
|
2008-12-10 16:55:59 -07:00
|
|
|
panicln("reflect: incompatible types in CopyArray:",
|
|
|
|
dt.String(), st.String());
|
|
|
|
}
|
|
|
|
if n < 0 || n > dst.Len() || n > src.Len() {
|
|
|
|
panicln("reflect: CopyArray: invalid count", n);
|
|
|
|
}
|
|
|
|
dstp := uintptr(dst.Elem(0).Addr());
|
|
|
|
srcp := uintptr(src.Elem(0).Addr());
|
|
|
|
end := uintptr(n)*uintptr(dt.Size());
|
2008-12-18 23:37:22 -07:00
|
|
|
if end % 8 == 0 {
|
2008-12-10 16:55:59 -07:00
|
|
|
for i := uintptr(0); i < end; i += 8{
|
|
|
|
di := Addr(dstp + i);
|
|
|
|
si := Addr(srcp + i);
|
|
|
|
*di.(*uint64) = *si.(*uint64);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := uintptr(0); i < end; i++ {
|
|
|
|
di := Addr(dstp + i);
|
|
|
|
si := Addr(srcp + i);
|
|
|
|
*di.(*byte) = *si.(*byte);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func NewValue(e interface {}) Value {
|
2009-01-16 15:58:14 -07:00
|
|
|
value, typestring, indir := sys.Reflect(e);
|
2009-01-09 01:17:46 -07:00
|
|
|
typ, ok := typecache[typestring];
|
2008-11-10 15:53:40 -07:00
|
|
|
if !ok {
|
2009-01-09 01:17:46 -07:00
|
|
|
typ = ParseTypeString("", typestring);
|
|
|
|
typecache[typestring] = typ;
|
2008-11-10 15:53:40 -07:00
|
|
|
}
|
2009-01-09 01:17:46 -07:00
|
|
|
|
|
|
|
if indir {
|
|
|
|
// Content of interface is a pointer.
|
2009-01-16 13:48:07 -07:00
|
|
|
return newValueAddr(typ, value.(uintptr).(Addr));
|
2009-01-09 01:17:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Content of interface is a value;
|
|
|
|
// need a permanent copy to take its address.
|
2009-01-06 16:19:02 -07:00
|
|
|
ap := new(uint64);
|
2008-10-22 17:48:17 -06:00
|
|
|
*ap = value;
|
2009-01-16 13:48:07 -07:00
|
|
|
return newValueAddr(typ, ap.(Addr));
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|