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-03-09 18:47:15 -06:00
|
|
|
// Addr is shorthand for unsafe.Pointer and is used to represent the address of Values.
|
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 {
|
2009-07-01 17:45:09 -06:00
|
|
|
return a.Kind() == b.Kind() && a.String() == b.String()
|
2008-12-10 16:55:59 -07:00
|
|
|
}
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// Value is the generic interface to reflection values. Once its Kind is known,
|
|
|
|
// such as BoolKind, the Value can be narrowed to the appropriate, more
|
|
|
|
// specific interface, such as BoolValue. Such narrowed values still implement
|
|
|
|
// the Value interface.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Value interface {
|
2009-03-09 18:47:15 -06:00
|
|
|
// The kind of thing described: ArrayKind, BoolKind, etc.
|
2008-10-22 12:02:56 -06:00
|
|
|
Kind() int;
|
2009-03-09 18:47:15 -06:00
|
|
|
// The reflection Type of the value.
|
2008-10-22 12:02:56 -06:00
|
|
|
Type() Type;
|
2009-03-09 18:47:15 -06:00
|
|
|
// The address of the value.
|
2008-11-12 15:19:39 -07:00
|
|
|
Addr() Addr;
|
2009-03-09 18:47:15 -06:00
|
|
|
// The value itself is the dynamic value of an empty interface.
|
2008-11-24 15:51:33 -07:00
|
|
|
Interface() interface {};
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-04-06 22:28:04 -06:00
|
|
|
func NewValue(e interface{}) Value;
|
|
|
|
|
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 {};
|
2009-04-14 20:03:57 -06:00
|
|
|
switch {
|
|
|
|
case c.typ.Kind() == InterfaceKind:
|
2009-05-21 12:50:20 -06:00
|
|
|
panic("not reached"); // InterfaceValue overrides this method
|
2009-06-04 22:09:06 -06:00
|
|
|
case c.typ.Size() > unsafe.Sizeof(uintptr(0)):
|
2009-05-08 15:57:56 -06:00
|
|
|
i = unsafe.Unreflect(uint64(uintptr(c.addr)), c.typ.String(), true);
|
2009-04-14 20:03:57 -06:00
|
|
|
default:
|
2009-01-09 01:17:46 -07:00
|
|
|
if uintptr(c.addr) == 0 {
|
|
|
|
panicln("reflect: address 0 for", c.typ.String());
|
|
|
|
}
|
2009-05-08 15:57:56 -06:00
|
|
|
i = unsafe.Unreflect(uint64(uintptr(*(*Addr)(c.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-03-09 18:47:15 -06:00
|
|
|
// MissingValue represents a value whose type is not known. It usually
|
|
|
|
// indicates an error.
|
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-03-03 09:39:12 -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-03-09 18:47:15 -06:00
|
|
|
// IntValue represents an int value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type IntValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() int; // Get the underlying int.
|
|
|
|
Set(int); // Set the underlying 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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*int)(v.addr)
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *intValueStruct) Set(i int) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*int)(v.addr) = i
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
// -- Int8
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// Int8Value represents an int8 value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Int8Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() int8; // Get the underlying int8.
|
|
|
|
Set(int8); // Set the underlying 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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*int8)(v.addr)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *int8ValueStruct) Set(i int8) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*int8)(v.addr) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Int16
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// Int16Value represents an int16 value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Int16Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() int16; // Get the underlying int16.
|
|
|
|
Set(int16); // Set the underlying 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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*int16)(v.addr)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *int16ValueStruct) Set(i int16) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*int16)(v.addr) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Int32
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// Int32Value represents an int32 value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Int32Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() int32; // Get the underlying int32.
|
|
|
|
Set(int32); // Set the underlying 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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*int32)(v.addr)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *int32ValueStruct) Set(i int32) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*int32)(v.addr) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Int64
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// Int64Value represents an int64 value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Int64Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() int64; // Get the underlying int64.
|
|
|
|
Set(int64); // Set the underlying 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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*int64)(v.addr)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *int64ValueStruct) Set(i int64) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*int64)(v.addr) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-29 16:31:02 -06:00
|
|
|
// -- Uint
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// UintValue represents a uint value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type UintValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() uint; // Get the underlying uint.
|
|
|
|
Set(uint); // Set the underlying 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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*uint)(v.addr)
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uintValueStruct) Set(i uint) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*uint)(v.addr) = i
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
// -- Uint8
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// Uint8Value represents a uint8 value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Uint8Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() uint8; // Get the underlying uint8.
|
|
|
|
Set(uint8); // Set the underlying 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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*uint8)(v.addr)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uint8ValueStruct) Set(i uint8) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*uint8)(v.addr) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Uint16
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// Uint16Value represents a uint16 value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Uint16Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() uint16; // Get the underlying uint16.
|
|
|
|
Set(uint16); // Set the underlying 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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*uint16)(v.addr)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uint16ValueStruct) Set(i uint16) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*uint16)(v.addr) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Uint32
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// Uint32Value represents a uint32 value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Uint32Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() uint32; // Get the underlying uint32.
|
|
|
|
Set(uint32); // Set the underlying 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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*uint32)(v.addr)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uint32ValueStruct) Set(i uint32) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*uint32)(v.addr) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Uint64
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// Uint64Value represents a uint64 value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Uint64Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() uint64; // Get the underlying uint64.
|
|
|
|
Set(uint64); // Set the underlying 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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*uint64)(v.addr)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uint64ValueStruct) Set(i uint64) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*uint64)(v.addr) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-12-11 15:41:12 -07:00
|
|
|
// -- Uintptr
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// UintptrValue represents a uintptr value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type UintptrValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() uintptr; // Get the underlying uintptr.
|
|
|
|
Set(uintptr); // Set the underlying uintptr.
|
2008-12-11 15:41:12 -07:00
|
|
|
}
|
|
|
|
|
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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*uintptr)(v.addr)
|
2008-12-11 15:41:12 -07:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *uintptrValueStruct) Set(i uintptr) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*uintptr)(v.addr) = i
|
2008-12-11 15:41:12 -07:00
|
|
|
}
|
|
|
|
|
2008-10-29 16:31:02 -06:00
|
|
|
// -- Float
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// FloatValue represents a float value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type FloatValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() float; // Get the underlying float.
|
|
|
|
Set(float); // Get the underlying 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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*float)(v.addr)
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *floatValueStruct) Set(f float) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*float)(v.addr) = f
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
// -- Float32
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// Float32Value represents a float32 value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Float32Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() float32; // Get the underlying float32.
|
|
|
|
Set(float32); // Get the underlying 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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*float32)(v.addr)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *float32ValueStruct) Set(f float32) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*float32)(v.addr) = f
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Float64
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// Float64Value represents a float64 value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Float64Value interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() float64; // Get the underlying float64.
|
|
|
|
Set(float64); // Get the underlying 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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*float64)(v.addr)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *float64ValueStruct) Set(f float64) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*float64)(v.addr) = f
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- String
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// StringValue represents a string value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type StringValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() string; // Get the underlying string value.
|
|
|
|
Set(string); // Set the underlying string value.
|
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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*string)(v.addr)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *stringValueStruct) Set(s string) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*string)(v.addr) = s
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-31 17:34:47 -06:00
|
|
|
// -- Bool
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// BoolValue represents a bool value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type BoolValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() bool; // Get the underlying bool value.
|
|
|
|
Set(bool); // Set the underlying bool value.
|
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-03-03 09:39:12 -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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*bool)(v.addr)
|
2008-10-31 17:34:47 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *boolValueStruct) Set(b bool) {
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*bool)(v.addr) = b
|
2008-10-31 17:34:47 -06:00
|
|
|
}
|
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
// -- Pointer
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// PtrValue represents a pointer value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type PtrValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Sub() Value; // The Value pointed to.
|
|
|
|
Get() Addr; // Get the address stored in the pointer.
|
|
|
|
SetSub(Value); // Set the the pointed-to Value.
|
2009-05-12 15:57:44 -06:00
|
|
|
IsNil() bool;
|
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 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return *(*Addr)(v.addr)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2009-06-15 19:35:04 -06:00
|
|
|
func (v *ptrValueStruct) IsNil() bool {
|
|
|
|
return uintptr(*(*Addr)(v.addr)) == 0
|
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func (v *ptrValueStruct) Sub() Value {
|
2009-06-15 19:35:04 -06:00
|
|
|
if v.IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
2009-01-16 13:48:07 -07:00
|
|
|
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
|
|
|
}
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*Addr)(v.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-03-03 09:39:12 -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-03-09 18:47:15 -06:00
|
|
|
// ArrayValue represents an array or slice value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type ArrayValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
IsSlice() bool; // Is this a slice (true) or array (false)?
|
|
|
|
Len() int; // The length of the array/slice.
|
|
|
|
Cap() int; // The capacity of the array/slice (==Len() for arrays).
|
|
|
|
Elem(i int) Value; // The Value of the i'th element.
|
|
|
|
SetLen(len int); // Set the length; slice only.
|
|
|
|
Set(src ArrayValue); // Set the underlying Value; slice only for src and dest both.
|
2009-05-12 15:57:44 -06:00
|
|
|
CopyFrom(src ArrayValue, n int); // Copy the elements from src; lengths must match.
|
|
|
|
IsNil() bool;
|
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-03-09 18:47:15 -06:00
|
|
|
panic("can't set slice from array");
|
2009-01-23 13:40:55 -07:00
|
|
|
}
|
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-05-12 15:57:44 -06:00
|
|
|
func (v *sliceValueStruct) IsNil() bool {
|
|
|
|
return uintptr(v.slice.data) == 0
|
|
|
|
}
|
|
|
|
|
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) {
|
2009-03-09 18:47:15 -06:00
|
|
|
panicln("can't set len of array");
|
2008-11-13 14:42:59 -07:00
|
|
|
}
|
|
|
|
|
2009-01-23 16:56:04 -07:00
|
|
|
func (v *arrayValueStruct) Set(src ArrayValue) {
|
2009-03-09 18:47:15 -06:00
|
|
|
panicln("can't set array");
|
2009-01-23 13:40:55 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-05-12 15:57:44 -06:00
|
|
|
func (v *arrayValueStruct) IsNil() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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-03-03 09:39:12 -07:00
|
|
|
v.slice = (*runtimeSlice)(addr);
|
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-03-09 18:47:15 -06:00
|
|
|
// MapValue represents a map value.
|
|
|
|
// Its implementation is incomplete.
|
2009-01-20 15:40:40 -07:00
|
|
|
type MapValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Len() int; // The number of elements; currently always returns 0.
|
|
|
|
Elem(key Value) Value; // The value indexed by key; unimplemented.
|
2009-05-12 15:57:44 -06:00
|
|
|
IsNil() bool;
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
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-03-03 09:39:12 -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-05-12 15:57:44 -06:00
|
|
|
func (v *mapValueStruct) IsNil() bool {
|
|
|
|
return false // TODO: implement this properly
|
|
|
|
}
|
|
|
|
|
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-03-09 18:47:15 -06:00
|
|
|
// ChanValue represents a chan value.
|
|
|
|
// Its implementation is incomplete.
|
2009-01-20 15:40:40 -07:00
|
|
|
type ChanValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-05-12 15:57:44 -06:00
|
|
|
IsNil() bool;
|
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-05-12 15:57:44 -06:00
|
|
|
func (v *chanValueStruct) IsNil() bool {
|
|
|
|
return false // TODO: implement this properly
|
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func chanCreator(typ Type, addr Addr) Value {
|
2009-03-03 09:39:12 -07:00
|
|
|
return &chanValueStruct{ commonValue{ChanKind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Struct
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// StructValue represents a struct value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type StructValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Len() int; // The number of fields.
|
|
|
|
Field(i int) Value; // The Value of field i.
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
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-03-03 09:39:12 -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-03-09 18:47:15 -06:00
|
|
|
// InterfaceValue represents an interface value.
|
2009-01-20 15:40:40 -07:00
|
|
|
type InterfaceValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-03-09 18:47:15 -06:00
|
|
|
Get() interface {}; // Get the underlying interface{} value.
|
2009-04-06 22:28:04 -06:00
|
|
|
Value() Value;
|
2009-05-12 15:57:44 -06:00
|
|
|
IsNil() bool;
|
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{} {
|
2009-05-12 17:08:16 -06:00
|
|
|
// There are two different representations of interface values,
|
|
|
|
// one if the interface type has methods and one if it doesn't.
|
|
|
|
// These two representations require different expressions
|
|
|
|
// to extract correctly.
|
|
|
|
if v.Type().(InterfaceType).Len() == 0 {
|
|
|
|
// Extract as interface value without methods.
|
|
|
|
return *(*interface{})(v.addr)
|
|
|
|
}
|
|
|
|
// Extract from v.addr as interface value with methods.
|
|
|
|
return *(*interface{ m() })(v.addr)
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
2009-05-21 12:50:20 -06:00
|
|
|
func (v *interfaceValueStruct) Interface() interface{} {
|
|
|
|
return v.Get();
|
|
|
|
}
|
|
|
|
|
2009-04-06 22:28:04 -06:00
|
|
|
func (v *interfaceValueStruct) Value() Value {
|
|
|
|
i := v.Get();
|
|
|
|
if i == nil {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return NewValue(i);
|
|
|
|
}
|
|
|
|
|
2009-05-12 15:57:44 -06:00
|
|
|
func (v *interfaceValueStruct) IsNil() bool {
|
|
|
|
return *(*interface{})(v.addr) == nil
|
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func interfaceCreator(typ Type, addr Addr) Value {
|
2009-03-03 09:39:12 -07:00
|
|
|
return &interfaceValueStruct{ commonValue{InterfaceKind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Func
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
|
|
|
|
// FuncValue represents a func value.
|
|
|
|
// Its implementation is incomplete.
|
2009-01-20 15:40:40 -07:00
|
|
|
type FuncValue interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Value;
|
2009-04-14 07:46:01 -06:00
|
|
|
Get() Addr; // The address of the function.
|
2009-05-12 15:57:44 -06:00
|
|
|
IsNil() bool;
|
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-04-14 07:46:01 -06:00
|
|
|
func (v *funcValueStruct) Get() Addr {
|
|
|
|
return *(*Addr)(v.addr)
|
|
|
|
}
|
|
|
|
|
2009-05-12 15:57:44 -06:00
|
|
|
func (v *funcValueStruct) IsNil() bool {
|
|
|
|
return *(*Addr)(v.addr) == nil
|
|
|
|
}
|
|
|
|
|
2009-01-16 13:48:07 -07:00
|
|
|
func funcCreator(typ Type, addr Addr) Value {
|
2009-03-03 09:39:12 -07:00
|
|
|
return &funcValueStruct{ commonValue{FuncKind, typ, addr} }
|
2009-01-16 13:48:07 -07:00
|
|
|
}
|
|
|
|
|
2009-03-03 09:39:12 -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,
|
|
|
|
StringKind : stringCreator,
|
|
|
|
BoolKind : boolCreator,
|
|
|
|
PtrKind : ptrCreator,
|
|
|
|
ArrayKind : arrayCreator,
|
|
|
|
MapKind : mapCreator,
|
|
|
|
ChanKind : chanCreator,
|
|
|
|
StructKind : structCreator,
|
|
|
|
InterfaceKind : interfaceCreator,
|
|
|
|
FuncKind : funcCreator,
|
2009-03-03 09:39:12 -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 {
|
2009-06-04 22:09:06 -06:00
|
|
|
panicln("no creator for type" , typ.String());
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
return c(typ, addr);
|
|
|
|
}
|
|
|
|
|
2009-05-12 17:16:52 -06:00
|
|
|
// NewZeroValue creates a new, zero-initialized Value for the specified Type.
|
|
|
|
func NewZeroValue(typ Type) Value {
|
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-03-09 18:47:15 -06:00
|
|
|
// NewSliceValue creates a new, zero-initialized slice value (ArrayValue) for the specified
|
|
|
|
// slice type (ArrayType), length, and capacity.
|
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);
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*uint64)(di) = *(*uint64)(si);
|
2008-12-10 16:55:59 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := uintptr(0); i < end; i++ {
|
|
|
|
di := Addr(dstp + i);
|
|
|
|
si := Addr(srcp + i);
|
2009-03-03 09:39:12 -07:00
|
|
|
*(*byte)(di) = *(*byte)(si);
|
2008-12-10 16:55:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-25 15:25:38 -06:00
|
|
|
func typeof(typestring string) Type {
|
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);
|
2009-06-04 22:09:06 -06:00
|
|
|
if typ.Kind() == MissingKind {
|
|
|
|
// This can not happen: unsafe.Reflect should only
|
|
|
|
// ever tell us the names of types that exist.
|
|
|
|
// Of course it does happen, and when it does
|
|
|
|
// it is more helpful to catch it in action here than
|
|
|
|
// to see $missing$ in a later print.
|
|
|
|
panicln("missing type for", typestring);
|
|
|
|
}
|
2009-01-09 01:17:46 -07:00
|
|
|
typecache[typestring] = typ;
|
2008-11-10 15:53:40 -07:00
|
|
|
}
|
2009-06-25 15:25:38 -06:00
|
|
|
return typ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewValue creates a new Value from the interface{} object provided.
|
|
|
|
func NewValue(e interface {}) Value {
|
|
|
|
value, typestring, indir := unsafe.Reflect(e);
|
|
|
|
typ := typeof(typestring);
|
2009-05-21 15:06:43 -06:00
|
|
|
var ap Addr;
|
2009-01-09 01:17:46 -07:00
|
|
|
if indir {
|
2009-05-21 15:06:43 -06:00
|
|
|
// Content of interface is large and didn't
|
|
|
|
// fit, so it's a pointer to the actual content.
|
|
|
|
// We have an address, but we need to
|
|
|
|
// make a copy to avoid letting the caller
|
|
|
|
// edit the content inside the interface.
|
|
|
|
n := uintptr(typ.Size());
|
|
|
|
data := make([]byte, n);
|
|
|
|
p1 := uintptr(Addr(&data[0]));
|
|
|
|
p2 := uintptr(value);
|
|
|
|
for i := uintptr(0); i < n; i++ {
|
|
|
|
*(*byte)(Addr(p1+i)) = *(*byte)(Addr(p2+i));
|
|
|
|
}
|
|
|
|
ap = Addr(&data[0]);
|
|
|
|
} else {
|
|
|
|
// Content of interface is small and stored
|
|
|
|
// inside the interface. Make a copy so we
|
|
|
|
// can take its address.
|
|
|
|
x := new(uint64);
|
|
|
|
*x = value;
|
|
|
|
ap = Addr(x);
|
2009-01-09 01:17:46 -07:00
|
|
|
}
|
2009-05-21 15:06:43 -06:00
|
|
|
return newValueAddr(typ, ap);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
2009-04-09 00:33:31 -06:00
|
|
|
|
2009-06-25 15:25:38 -06:00
|
|
|
// Typeof returns the type of the value in the interface{} object provided.
|
|
|
|
func Typeof(e interface{}) Type {
|
|
|
|
value, typestring, indir := unsafe.Reflect(e);
|
|
|
|
return typeof(typestring);
|
|
|
|
}
|
|
|
|
|
2009-04-09 00:33:31 -06:00
|
|
|
// Indirect indirects one level through a value, if it is a pointer.
|
|
|
|
// If not a pointer, the value is returned unchanged.
|
|
|
|
// Useful when walking arbitrary data structures.
|
|
|
|
func Indirect(v Value) Value {
|
|
|
|
if v.Kind() == PtrKind {
|
|
|
|
p := v.(PtrValue);
|
|
|
|
if p.Get() == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
v = p.Sub()
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|