v := reflect.NewInitValue(typ);
switch v.Kind() {
case reflect.IntKind:
- v.(reflect.IntValue).Put(132);
+ v.(reflect.IntValue).Set(132);
case reflect.Int8Kind:
- v.(reflect.Int8Value).Put(8);
+ v.(reflect.Int8Value).Set(8);
case reflect.Int16Kind:
- v.(reflect.Int16Value).Put(16);
+ v.(reflect.Int16Value).Set(16);
case reflect.Int32Kind:
- v.(reflect.Int32Value).Put(32);
+ v.(reflect.Int32Value).Set(32);
case reflect.Int64Kind:
- v.(reflect.Int64Value).Put(64);
+ v.(reflect.Int64Value).Set(64);
case reflect.UintKind:
- v.(reflect.UintValue).Put(132);
+ v.(reflect.UintValue).Set(132);
case reflect.Uint8Kind:
- v.(reflect.Uint8Value).Put(8);
+ v.(reflect.Uint8Value).Set(8);
case reflect.Uint16Kind:
- v.(reflect.Uint16Value).Put(16);
+ v.(reflect.Uint16Value).Set(16);
case reflect.Uint32Kind:
- v.(reflect.Uint32Value).Put(32);
+ v.(reflect.Uint32Value).Set(32);
case reflect.Uint64Kind:
- v.(reflect.Uint64Value).Put(64);
+ v.(reflect.Uint64Value).Set(64);
case reflect.FloatKind:
- v.(reflect.FloatValue).Put(3200.0);
+ v.(reflect.FloatValue).Set(3200.0);
case reflect.Float32Kind:
- v.(reflect.Float32Value).Put(32.0);
+ v.(reflect.Float32Value).Set(32.0);
case reflect.Float64Kind:
- v.(reflect.Float64Value).Put(64.0);
+ v.(reflect.Float64Value).Set(64.0);
case reflect.StringKind:
- v.(reflect.StringValue).Put("stringy cheese");
+ v.(reflect.StringValue).Set("stringy cheese");
case reflect.BoolKind:
- v.(reflect.BoolValue).Put(true);
+ v.(reflect.BoolValue).Set(true);
}
assert(reflect.ValueToString(v), t);
}
var tmp A = A{1,2,3,4,5,6,7,8,9,10};
value := reflect.NewValue(&tmp);
assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.A·test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}");
- value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.IntValue).Put(123);
+ value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.IntValue).Set(123);
assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.A·test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
}
{
var tmp *AA = &tmp1;
value := reflect.NewValue(tmp);
assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.AA·test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}");
- value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.IntValue).Put(123);
+ value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.IntValue).Set(123);
assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.AA·test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
}
st = t.(reflect.StructType);
name, typ, tag, offset = st.Field(0);
assert(typ.String(), "*[]uint32");
+
+ t = reflect.ParseTypeString("", "[]int32");
+ v := reflect.NewOpenArrayValue(t, 5, 10);
+ t1 := reflect.ParseTypeString("", "*[]int32");
+ v1 := reflect.NewInitValue(t1);
+ v1.(reflect.PtrValue).SetSub(v);
+ a := v1.Interface().(*[]int32);
+ println(a, len(a), cap(a));
+ for i := 0; i < len(a); i++ {
+ v.Elem(i).(reflect.Int32Value).Set(int32(i));
+ }
+ for i := 0; i < len(a); i++ {
+ println(a[i]);
+ }
}
type Addr uint64 // TODO: where are ptrint/intptr etc?
// Conversion functions, implemented in assembler
+type RuntimeArray struct
func AddrToPtrAddr(Addr) *Addr
func AddrToPtrInt(Addr) *int
func AddrToPtrInt8(Addr) *int8
func AddrToPtrFloat80(Addr) *float80
func AddrToPtrString(Addr) *string
func AddrToPtrBool(Addr) *bool
+func AddrToPtrRuntimeArray(Addr) *RuntimeArray
+func PtrRuntimeArrayToAddr(*RuntimeArray) Addr
export type Empty interface {} // TODO(r): Delete when no longer needed?
export type IntValue interface {
Kind() int;
Get() int;
- Put(int);
+ Set(int);
Type() Type;
}
return *AddrToPtrInt(v.addr)
}
-func (v *IntValueStruct) Put(i int) {
+func (v *IntValueStruct) Set(i int) {
*AddrToPtrInt(v.addr) = i
}
export type Int8Value interface {
Kind() int;
Get() int8;
- Put(int8);
+ Set(int8);
Type() Type;
}
return *AddrToPtrInt8(v.addr)
}
-func (v *Int8ValueStruct) Put(i int8) {
+func (v *Int8ValueStruct) Set(i int8) {
*AddrToPtrInt8(v.addr) = i
}
export type Int16Value interface {
Kind() int;
Get() int16;
- Put(int16);
+ Set(int16);
Type() Type;
}
return *AddrToPtrInt16(v.addr)
}
-func (v *Int16ValueStruct) Put(i int16) {
+func (v *Int16ValueStruct) Set(i int16) {
*AddrToPtrInt16(v.addr) = i
}
export type Int32Value interface {
Kind() int;
Get() int32;
- Put(int32);
+ Set(int32);
Type() Type;
}
return *AddrToPtrInt32(v.addr)
}
-func (v *Int32ValueStruct) Put(i int32) {
+func (v *Int32ValueStruct) Set(i int32) {
*AddrToPtrInt32(v.addr) = i
}
export type Int64Value interface {
Kind() int;
Get() int64;
- Put(int64);
+ Set(int64);
Type() Type;
}
return *AddrToPtrInt64(v.addr)
}
-func (v *Int64ValueStruct) Put(i int64) {
+func (v *Int64ValueStruct) Set(i int64) {
*AddrToPtrInt64(v.addr) = i
}
export type UintValue interface {
Kind() int;
Get() uint;
- Put(uint);
+ Set(uint);
Type() Type;
}
return *AddrToPtrUint(v.addr)
}
-func (v *UintValueStruct) Put(i uint) {
+func (v *UintValueStruct) Set(i uint) {
*AddrToPtrUint(v.addr) = i
}
export type Uint8Value interface {
Kind() int;
Get() uint8;
- Put(uint8);
+ Set(uint8);
Type() Type;
}
return *AddrToPtrUint8(v.addr)
}
-func (v *Uint8ValueStruct) Put(i uint8) {
+func (v *Uint8ValueStruct) Set(i uint8) {
*AddrToPtrUint8(v.addr) = i
}
export type Uint16Value interface {
Kind() int;
Get() uint16;
- Put(uint16);
+ Set(uint16);
Type() Type;
}
return *AddrToPtrUint16(v.addr)
}
-func (v *Uint16ValueStruct) Put(i uint16) {
+func (v *Uint16ValueStruct) Set(i uint16) {
*AddrToPtrUint16(v.addr) = i
}
export type Uint32Value interface {
Kind() int;
Get() uint32;
- Put(uint32);
+ Set(uint32);
Type() Type;
}
return *AddrToPtrUint32(v.addr)
}
-func (v *Uint32ValueStruct) Put(i uint32) {
+func (v *Uint32ValueStruct) Set(i uint32) {
*AddrToPtrUint32(v.addr) = i
}
export type Uint64Value interface {
Kind() int;
Get() uint64;
- Put(uint64);
+ Set(uint64);
Type() Type;
}
return *AddrToPtrUint64(v.addr)
}
-func (v *Uint64ValueStruct) Put(i uint64) {
+func (v *Uint64ValueStruct) Set(i uint64) {
*AddrToPtrUint64(v.addr) = i
}
export type FloatValue interface {
Kind() int;
Get() float;
- Put(float);
+ Set(float);
Type() Type;
}
return *AddrToPtrFloat(v.addr)
}
-func (v *FloatValueStruct) Put(f float) {
+func (v *FloatValueStruct) Set(f float) {
*AddrToPtrFloat(v.addr) = f
}
export type Float32Value interface {
Kind() int;
Get() float32;
- Put(float32);
+ Set(float32);
Type() Type;
}
return *AddrToPtrFloat32(v.addr)
}
-func (v *Float32ValueStruct) Put(f float32) {
+func (v *Float32ValueStruct) Set(f float32) {
*AddrToPtrFloat32(v.addr) = f
}
export type Float64Value interface {
Kind() int;
Get() float64;
- Put(float64);
+ Set(float64);
Type() Type;
}
return *AddrToPtrFloat64(v.addr)
}
-func (v *Float64ValueStruct) Put(f float64) {
+func (v *Float64ValueStruct) Set(f float64) {
*AddrToPtrFloat64(v.addr) = f
}
export type Float80Value interface {
Kind() int;
Get() float80;
- Put(float80);
+ Set(float80);
Type() Type;
}
return 0;
}
-func (v *Float80ValueStruct) Put(f float80) {
+func (v *Float80ValueStruct) Set(f float80) {
*AddrToPtrFloat80(v.addr) = f
}
*/
export type StringValue interface {
Kind() int;
Get() string;
- Put(string);
+ Set(string);
Type() Type;
}
return *AddrToPtrString(v.addr)
}
-func (v *StringValueStruct) Put(s string) {
+func (v *StringValueStruct) Set(s string) {
*AddrToPtrString(v.addr) = s
}
export type BoolValue interface {
Kind() int;
Get() bool;
- Put(bool);
+ Set(bool);
Type() Type;
}
return *AddrToPtrBool(v.addr)
}
-func (v *BoolValueStruct) Put(b bool) {
+func (v *BoolValueStruct) Set(b bool) {
*AddrToPtrBool(v.addr) = b
}
Type() Type;
Open() bool;
Len() int;
+ Cap() int;
Elem(i int) Value;
-}
-
-type OpenArrayValueStruct struct {
- Common;
- elemtype Type;
- elemsize int;
+ SetLen(len int);
}
/*
struct Array {
byte* array; // actual data
uint32 nel; // number of elements
+ uint32 cap;
};
*/
+type RuntimeArray struct {
+ data Addr;
+ len uint32;
+ cap uint32;
+}
+
+type OpenArrayValueStruct struct {
+ Common;
+ elemtype Type;
+ elemsize int;
+ array *RuntimeArray;
+}
func (v *OpenArrayValueStruct) Open() bool {
return true
}
func (v *OpenArrayValueStruct) Len() int {
- return int(*AddrToPtrInt32(v.addr+8));
+ return int(v.array.len);
+}
+
+func (v *OpenArrayValueStruct) Cap() int {
+ return int(v.array.cap);
+}
+
+func (v *OpenArrayValueStruct) SetLen(len int) {
+ if len > v.Cap() {
+ panicln("reflect: OpenArrayValueStruct.SetLen", len, v.Cap());
+ }
+ v.array.len = uint32(len);
}
func (v *OpenArrayValueStruct) Elem(i int) Value {
- base := *AddrToPtrAddr(v.addr);
- return NewValueAddr(v.elemtype, base + Addr(i * v.elemsize));
+ return NewValueAddr(v.elemtype, v.array.data + Addr(i * v.elemsize));
}
type FixedArrayValueStruct struct {
return v.len
}
+func (v *FixedArrayValueStruct) Cap() int {
+ return v.len
+}
+
+func (v *FixedArrayValueStruct) SetLen(len int) {
+}
+
func (v *FixedArrayValueStruct) Elem(i int) Value {
return NewValueAddr(v.elemtype, v.addr + Addr(i * v.elemsize));
return nil
v.typ = typ;
v.elemtype = arraytype.Elem();
v.elemsize = v.elemtype.Size();
+ v.array = AddrToPtrRuntimeArray(addr);
return v;
}
v := new(FixedArrayValueStruct);
return NewValueAddr(typ, PtrUint8ToAddr(&data[0]));
}
+/*
+ Run-time representation of open arrays looks like this:
+ struct Array {
+ byte* array; // actual data
+ uint32 nel; // number of elements
+ uint32 cap; // allocated number of elements
+ };
+*/
+export func NewOpenArrayValue(typ ArrayType, len, cap int) ArrayValue {
+ if !typ.Open() {
+ return nil
+ }
+
+ array := new(RuntimeArray);
+ size := typ.Elem().Size() * cap;
+ if size == 0 {
+ size = 1;
+ }
+ data := new([]uint8, size);
+ array.data = PtrUint8ToAddr(&data[0]);
+ array.len = uint32(len);
+ array.cap = uint32(cap);
+
+ return NewValueAddr(typ, PtrRuntimeArrayToAddr(array));
+}
+
export func NewValue(e Empty) Value {
value, typestring := sys.reflect(e);
p, ok := typecache[typestring];