OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package reflect | 5 package reflect |
6 | 6 |
7 import ( | 7 import ( |
8 "math" | 8 "math" |
9 "runtime" | 9 "runtime" |
10 "unsafe" | 10 "unsafe" |
(...skipping 23 matching lines...) Expand all Loading... |
34 *(*byte)(addr(dst + i)) = *(*byte)(addr(src + i)) | 34 *(*byte)(addr(dst + i)) = *(*byte)(addr(src + i)) |
35 } | 35 } |
36 default: | 36 default: |
37 // word copy forward | 37 // word copy forward |
38 for i := uintptr(0); i < n; i += ptrSize { | 38 for i := uintptr(0); i < n; i += ptrSize { |
39 *(*uintptr)(addr(dst + i)) = *(*uintptr)(addr(src + i)) | 39 *(*uintptr)(addr(dst + i)) = *(*uintptr)(addr(src + i)) |
40 } | 40 } |
41 } | 41 } |
42 } | 42 } |
43 | 43 |
44 // Value is the common interface to reflection values. | 44 // Value is the reflection interface to a Go value. |
45 // The implementations of Value (e.g., ArrayValue, StructValue) | 45 // |
| 46 // Not all methods apply to all kinds of values. Restrictions, |
| 47 // if any, are noted in the documentation for each method. |
| 48 // Use the Kind method to find out the kind of value before |
| 49 // calling kind-specific methods. Calling a method |
| 50 // inappropriate to the kind of type causes a run time panic. |
| 51 // |
| 52 // The zero Value represents no value. |
| 53 // Its IsValid method returns false, its Kind method returns Invalid, |
| 54 // its String method returns "<invalid Value>", and all other methods panic. |
| 55 // Most functions and methods never return an invalid value. |
| 56 // If one does, its documentation states the conditions explicitly. |
| 57 type Value struct { |
| 58 Internal valueInterface |
| 59 } |
| 60 |
| 61 // TODO(rsc): This implementation of Value is a just a façade |
| 62 // in front of the old implementation, now called valueInterface. |
| 63 // A future CL will change it to a real implementation. |
| 64 // Changing the API is already a big enough step for one CL. |
| 65 |
| 66 // A ValueError occurs when a Value method is invoked on |
| 67 // a Value that does not support it. Such cases are documented |
| 68 // in the description of each method. |
| 69 type ValueError struct { |
| 70 Method string |
| 71 Kind Kind |
| 72 } |
| 73 |
| 74 func (e *ValueError) String() string { |
| 75 if e.Kind == 0 { |
| 76 return "reflect: call of " + e.Method + " on zero Value" |
| 77 } |
| 78 return "reflect: call of " + e.Method + " on " + e.Kind.String() + " Val
ue" |
| 79 } |
| 80 |
| 81 // methodName returns the name of the calling method, |
| 82 // assumed to be two stack frames above. |
| 83 func methodName() string { |
| 84 pc, _, _, _ := runtime.Caller(2) |
| 85 f := runtime.FuncForPC(pc) |
| 86 if f == nil { |
| 87 return "unknown method" |
| 88 } |
| 89 return f.Name() |
| 90 } |
| 91 |
| 92 func (v Value) internal() valueInterface { |
| 93 vi := v.Internal |
| 94 if vi == nil { |
| 95 panic(&ValueError{methodName(), 0}) |
| 96 } |
| 97 return vi |
| 98 } |
| 99 |
| 100 func (v Value) panicIfNot(want Kind) valueInterface { |
| 101 vi := v.Internal |
| 102 if vi == nil { |
| 103 panic(&ValueError{methodName(), 0}) |
| 104 } |
| 105 if k := vi.Kind(); k != want { |
| 106 panic(&ValueError{methodName(), k}) |
| 107 } |
| 108 return vi |
| 109 } |
| 110 |
| 111 func (v Value) panicIfNots(wants []Kind) valueInterface { |
| 112 vi := v.Internal |
| 113 if vi == nil { |
| 114 panic(&ValueError{methodName(), 0}) |
| 115 } |
| 116 k := vi.Kind() |
| 117 for _, want := range wants { |
| 118 if k == want { |
| 119 return vi |
| 120 } |
| 121 } |
| 122 panic(&ValueError{methodName(), k}) |
| 123 } |
| 124 |
| 125 // Addr returns a pointer value representing the address of v. |
| 126 // It panics if CanAddr() returns false. |
| 127 // Addr is typically used to obtain a pointer to a struct field |
| 128 // or slice element in order to call a method that requires a |
| 129 // pointer receiver. |
| 130 func (v Value) Addr() Value { |
| 131 return v.internal().Addr() |
| 132 } |
| 133 |
| 134 // Bool returns v's underlying value. |
| 135 // It panics if v's kind is not Bool. |
| 136 func (v Value) Bool() bool { |
| 137 u := v.panicIfNot(Bool).(*boolValue) |
| 138 return u.Get() |
| 139 } |
| 140 |
| 141 // CanAddr returns true if the value's address can be obtained with Addr. |
| 142 // Such values are called addressable. A value is addressable if it is |
| 143 // an element of a slice, an element of an addressable array, |
| 144 // a field of an addressable struct, the result of dereferencing a pointer, |
| 145 // or the result of a call to NewValue, MakeChan, MakeMap, or Zero. |
| 146 // If CanAddr returns false, calling Addr will panic. |
| 147 func (v Value) CanAddr() bool { |
| 148 return v.internal().CanAddr() |
| 149 } |
| 150 |
| 151 // CanSet returns true if the value of v can be changed. |
| 152 // Values obtained by the use of unexported struct fields |
| 153 // can be read but not set. |
| 154 // If CanSet returns false, calling Set or any type-specific |
| 155 // setter (e.g., SetBool, SetInt64) will panic. |
| 156 func (v Value) CanSet() bool { |
| 157 return v.internal().CanSet() |
| 158 } |
| 159 |
| 160 // Call calls the function v with the input parameters in. |
| 161 // It panics if v's Kind is not Func. |
| 162 // It returns the output parameters as Values. |
| 163 func (v Value) Call(in []Value) []Value { |
| 164 return v.panicIfNot(Func).(*funcValue).Call(in) |
| 165 } |
| 166 |
| 167 var capKinds = []Kind{Array, Chan, Slice} |
| 168 |
| 169 type capper interface { |
| 170 Cap() int |
| 171 } |
| 172 |
| 173 // Cap returns v's capacity. |
| 174 // It panics if v's Kind is not Array, Chan, or Slice. |
| 175 func (v Value) Cap() int { |
| 176 return v.panicIfNots(capKinds).(capper).Cap() |
| 177 } |
| 178 |
| 179 // Close closes the channel v. |
| 180 // It panics if v's Kind is not Chan. |
| 181 func (v Value) Close() { |
| 182 v.panicIfNot(Chan).(*chanValue).Close() |
| 183 } |
| 184 |
| 185 var complexKinds = []Kind{Complex64, Complex128} |
| 186 |
| 187 // Complex returns v's underlying value, as a complex128. |
| 188 // It panics if v's Kind is not Complex64 or Complex128 |
| 189 func (v Value) Complex() complex128 { |
| 190 return v.panicIfNots(complexKinds).(*complexValue).Get() |
| 191 } |
| 192 |
| 193 var interfaceOrPtr = []Kind{Interface, Ptr} |
| 194 |
| 195 type elemer interface { |
| 196 Elem() Value |
| 197 } |
| 198 |
| 199 // Elem returns the value that the interface v contains |
| 200 // or that the pointer v points to. |
| 201 // It panics if v's Kind is not Interface or Ptr. |
| 202 // It returns the zero Value if v is nil. |
| 203 func (v Value) Elem() Value { |
| 204 return v.panicIfNots(interfaceOrPtr).(elemer).Elem() |
| 205 } |
| 206 |
| 207 // Field returns the i'th field of the struct v. |
| 208 // It panics if v's Kind is not Struct. |
| 209 func (v Value) Field(i int) Value { |
| 210 return v.panicIfNot(Struct).(*structValue).Field(i) |
| 211 } |
| 212 |
| 213 // FieldByIndex returns the nested field corresponding to index. |
| 214 // It panics if v's Kind is not struct. |
| 215 func (v Value) FieldByIndex(index []int) Value { |
| 216 return v.panicIfNot(Struct).(*structValue).FieldByIndex(index) |
| 217 } |
| 218 |
| 219 // FieldByName returns the struct field with the given name. |
| 220 // It returns the zero Value if no field was found. |
| 221 // It panics if v's Kind is not struct. |
| 222 func (v Value) FieldByName(name string) Value { |
| 223 return v.panicIfNot(Struct).(*structValue).FieldByName(name) |
| 224 } |
| 225 |
| 226 // FieldByNameFunc returns the struct field with a name |
| 227 // that satisfies the match function. |
| 228 // It panics if v's Kind is not struct. |
| 229 // It returns the zero Value if no field was found. |
| 230 func (v Value) FieldByNameFunc(match func(string) bool) Value { |
| 231 return v.panicIfNot(Struct).(*structValue).FieldByNameFunc(match) |
| 232 } |
| 233 |
| 234 var floatKinds = []Kind{Float32, Float64} |
| 235 |
| 236 // Float returns v's underlying value, as an float64. |
| 237 // It panics if v's Kind is not Float32 or Float64 |
| 238 func (v Value) Float() float64 { |
| 239 return v.panicIfNots(floatKinds).(*floatValue).Get() |
| 240 } |
| 241 |
| 242 var arrayOrSlice = []Kind{Array, Slice} |
| 243 |
| 244 // Index returns v's i'th element. |
| 245 // It panics if v's Kind is not Array or Slice. |
| 246 func (v Value) Index(i int) Value { |
| 247 return v.panicIfNots(arrayOrSlice).(arrayOrSliceValue).Elem(i) |
| 248 } |
| 249 |
| 250 var intKinds = []Kind{Int, Int8, Int16, Int32, Int64} |
| 251 |
| 252 // Int returns v's underlying value, as an int64. |
| 253 // It panics if v's Kind is not a sized or unsized Int kind. |
| 254 func (v Value) Int() int64 { |
| 255 return v.panicIfNots(intKinds).(*intValue).Get() |
| 256 } |
| 257 |
| 258 // Interface returns v's value as an interface{}. |
| 259 // If v is a method obtained by invoking Value.Method |
| 260 // (as opposed to Type.Method), Interface cannot return an |
| 261 // interface value, so it panics. |
| 262 func (v Value) Interface() interface{} { |
| 263 return v.internal().Interface() |
| 264 } |
| 265 |
| 266 // InterfaceData returns the interface v's value as a uintptr pair. |
| 267 // It panics if v's Kind is not Interface. |
| 268 func (v Value) InterfaceData() [2]uintptr { |
| 269 return v.panicIfNot(Interface).(*interfaceValue).Get() |
| 270 } |
| 271 |
| 272 var nilKinds = []Kind{Chan, Func, Interface, Map, Ptr, Slice} |
| 273 |
| 274 type isNiller interface { |
| 275 IsNil() bool |
| 276 } |
| 277 |
| 278 // IsNil returns true if v is a nil value. |
| 279 // It panics if v's Kind is not Chan, Func, Interface, Map, Ptr, or Slice. |
| 280 func (v Value) IsNil() bool { |
| 281 return v.panicIfNots(nilKinds).(isNiller).IsNil() |
| 282 } |
| 283 |
| 284 // IsValid returns true if v represents a value. |
| 285 // It returns false if v is the zero Value. |
| 286 // If IsValid returns false, all other methods except String panic. |
| 287 // Most functions and methods never return an invalid value. |
| 288 // If one does, its documentation states the conditions explicitly. |
| 289 func (v Value) IsValid() bool { |
| 290 return v.Internal != nil |
| 291 } |
| 292 |
| 293 // Kind returns v's Kind. |
| 294 // If v is the zero Value (IsValid returns false), Kind returns Invalid. |
| 295 func (v Value) Kind() Kind { |
| 296 if v.Internal == nil { |
| 297 return Invalid |
| 298 } |
| 299 return v.internal().Kind() |
| 300 } |
| 301 |
| 302 var lenKinds = []Kind{Array, Chan, Map, Slice} |
| 303 |
| 304 type lenner interface { |
| 305 Len() int |
| 306 } |
| 307 |
| 308 // Len returns v's length. |
| 309 // It panics if v's Kind is not Array, Chan, Map, or Slice. |
| 310 func (v Value) Len() int { |
| 311 return v.panicIfNots(lenKinds).(lenner).Len() |
| 312 } |
| 313 |
| 314 // MapIndex returns the value associated with key in the map v. |
| 315 // It panics if v's Kind is not Map. |
| 316 // It returns the zero Value if key is not found in the map. |
| 317 func (v Value) MapIndex(key Value) Value { |
| 318 return v.panicIfNot(Map).(*mapValue).Elem(key) |
| 319 } |
| 320 |
| 321 // MapKeys returns a slice containing all the keys present in the map, |
| 322 // in unspecified order. |
| 323 // It panics if v's Kind is not Map. |
| 324 func (v Value) MapKeys() []Value { |
| 325 return v.panicIfNot(Map).(*mapValue).Keys() |
| 326 } |
| 327 |
| 328 // Method returns a function value corresponding to v's i'th method. |
| 329 // The arguments to a Call on the returned function should not include |
| 330 // a receiver; the returned function will always use v as the receiver. |
| 331 func (v Value) Method(i int) Value { |
| 332 return v.internal().Method(i) |
| 333 } |
| 334 |
| 335 // NumField returns the number of fields in the struct v. |
| 336 // It panics if v's Kind is not Struct. |
| 337 func (v Value) NumField() int { |
| 338 return v.panicIfNot(Struct).(*structValue).NumField() |
| 339 } |
| 340 |
| 341 // OverflowComplex returns true if the complex128 x cannot be represented by v's
type. |
| 342 // It panics if v's Kind is not Complex64 or Complex128. |
| 343 func (v Value) OverflowComplex(x complex128) bool { |
| 344 return v.panicIfNots(complexKinds).(*complexValue).Overflow(x) |
| 345 } |
| 346 |
| 347 // OverflowFloat returns true if the float64 x cannot be represented by v's type
. |
| 348 // It panics if v's Kind is not Float32 or Float64. |
| 349 func (v Value) OverflowFloat(x float64) bool { |
| 350 return v.panicIfNots(floatKinds).(*floatValue).Overflow(x) |
| 351 } |
| 352 |
| 353 // OverflowInt returns true if the int64 x cannot be represented by v's type. |
| 354 // It panics if v's Kind is not a sized or unsized Int kind. |
| 355 func (v Value) OverflowInt(x int64) bool { |
| 356 return v.panicIfNots(intKinds).(*intValue).Overflow(x) |
| 357 } |
| 358 |
| 359 // OverflowUint returns true if the uint64 x cannot be represented by v's type. |
| 360 // It panics if v's Kind is not a sized or unsized Uint kind. |
| 361 func (v Value) OverflowUint(x uint64) bool { |
| 362 return v.panicIfNots(uintKinds).(*uintValue).Overflow(x) |
| 363 } |
| 364 |
| 365 var pointerKinds = []Kind{Chan, Func, Map, Ptr, Slice, UnsafePointer} |
| 366 |
| 367 type uintptrGetter interface { |
| 368 Get() uintptr |
| 369 } |
| 370 |
| 371 // Pointer returns v's value as a uintptr. |
| 372 // It returns uintptr instead of unsafe.Pointer so that |
| 373 // code using reflect cannot obtain unsafe.Pointers |
| 374 // without importing the unsafe package explicitly. |
| 375 // It panics if v's Kind is not Chan, Func, Map, Ptr, Slice, or UnsafePointer. |
| 376 func (v Value) Pointer() uintptr { |
| 377 return v.panicIfNots(pointerKinds).(uintptrGetter).Get() |
| 378 } |
| 379 |
| 380 |
| 381 // Recv receives and returns a value from the channel v. |
| 382 // It panics if v's Kind is not Chan. |
| 383 // The receive blocks until a value is ready. |
| 384 // The boolean value ok is true if the value x corresponds to a send |
| 385 // on the channel, false if it is a zero value received because the channel is c
losed. |
| 386 func (v Value) Recv() (x Value, ok bool) { |
| 387 return v.panicIfNot(Chan).(*chanValue).Recv() |
| 388 } |
| 389 |
| 390 // Send sends x on the channel v. |
| 391 // It panics if v's kind is not Chan or if x's type is not the same type as v's
element type. |
| 392 func (v Value) Send(x Value) { |
| 393 v.panicIfNot(Chan).(*chanValue).Send(x) |
| 394 } |
| 395 |
| 396 // Set assigns x to the value v; x must have the same type as v. |
| 397 // It panics if CanSet() returns false or if x is the zero Value. |
| 398 func (v Value) Set(x Value) { |
| 399 x.internal() |
| 400 v.internal().SetValue(x) |
| 401 } |
| 402 |
| 403 // SetBool sets v's underlying value. |
| 404 // It panics if v's Kind is not Bool or if CanSet() is false. |
| 405 func (v Value) SetBool(x bool) { |
| 406 v.panicIfNot(Bool).(*boolValue).Set(x) |
| 407 } |
| 408 |
| 409 // SetComplex sets v's underlying value to x. |
| 410 // It panics if v's Kind is not Complex64 or Complex128, or if CanSet() is false
. |
| 411 func (v Value) SetComplex(x complex128) { |
| 412 v.panicIfNots(complexKinds).(*complexValue).Set(x) |
| 413 } |
| 414 |
| 415 // SetFloat sets v's underlying value to x. |
| 416 // It panics if v's Kind is not Float32 or Float64, or if CanSet() is false. |
| 417 func (v Value) SetFloat(x float64) { |
| 418 v.panicIfNots(floatKinds).(*floatValue).Set(x) |
| 419 } |
| 420 |
| 421 // SetInt sets v's underlying value to x. |
| 422 // It panics if v's Kind is not a sized or unsized Int kind, or if CanSet() is f
alse. |
| 423 func (v Value) SetInt(x int64) { |
| 424 v.panicIfNots(intKinds).(*intValue).Set(x) |
| 425 } |
| 426 |
| 427 // SetLen sets v's length to n. |
| 428 // It panics if v's Kind is not Slice. |
| 429 func (v Value) SetLen(n int) { |
| 430 v.panicIfNot(Slice).(*sliceValue).SetLen(n) |
| 431 } |
| 432 |
| 433 // SetMapIndex sets the value associated with key in the map v to val. |
| 434 // It panics if v's Kind is not Map. |
| 435 // If val is the zero Value, SetMapIndex deletes the key from the map. |
| 436 func (v Value) SetMapIndex(key, val Value) { |
| 437 v.panicIfNot(Map).(*mapValue).SetElem(key, val) |
| 438 } |
| 439 |
| 440 // SetUint sets v's underlying value to x. |
| 441 // It panics if v's Kind is not a sized or unsized Uint kind, or if CanSet() is
false. |
| 442 func (v Value) SetUint(x uint64) { |
| 443 v.panicIfNots(uintKinds).(*uintValue).Set(x) |
| 444 } |
| 445 |
| 446 // SetPointer sets the unsafe.Pointer value v to x. |
| 447 // It panics if v's Kind is not UnsafePointer. |
| 448 func (v Value) SetPointer(x unsafe.Pointer) { |
| 449 v.panicIfNot(UnsafePointer).(*unsafePointerValue).Set(x) |
| 450 } |
| 451 |
| 452 // SetString sets v's underlying value to x. |
| 453 // It panics if v's Kind is not String or if CanSet() is false. |
| 454 func (v Value) SetString(x string) { |
| 455 v.panicIfNot(String).(*stringValue).Set(x) |
| 456 } |
| 457 |
| 458 // BUG(rsc): Value.Slice should allow slicing arrays. |
| 459 |
| 460 // Slice returns a slice of v. |
| 461 // It panics if v's Kind is not Slice. |
| 462 func (v Value) Slice(beg, end int) Value { |
| 463 return v.panicIfNot(Slice).(*sliceValue).Slice(beg, end) |
| 464 } |
| 465 |
| 466 // String returns the string v's underlying value, as a string. |
| 467 // String is a special case because of Go's String method convention. |
| 468 // Unlike the other getters, it does not panic if v's Kind is not String. |
| 469 // Instead, it returns a string of the form "<T value>" where T is v's type. |
| 470 func (v Value) String() string { |
| 471 vi := v.Internal |
| 472 if vi == nil { |
| 473 return "<invalid Value>" |
| 474 } |
| 475 if vi.Kind() == String { |
| 476 return vi.(*stringValue).Get() |
| 477 } |
| 478 return "<" + vi.Type().String() + " Value>" |
| 479 } |
| 480 |
| 481 // TryRecv attempts to receive a value from the channel v but will not block. |
| 482 // It panics if v's Kind is not Chan. |
| 483 // If the receive cannot finish without blocking, x is the zero Value. |
| 484 // The boolean ok is true if the value x corresponds to a send |
| 485 // on the channel, false if it is a zero value received because the channel is c
losed. |
| 486 func (v Value) TryRecv() (x Value, ok bool) { |
| 487 return v.panicIfNot(Chan).(*chanValue).TryRecv() |
| 488 } |
| 489 |
| 490 // TrySend attempts to send x on the channel v but will not block. |
| 491 // It panics if v's Kind is not Chan. |
| 492 // It returns true if the value was sent, false otherwise. |
| 493 func (v Value) TrySend(x Value) bool { |
| 494 return v.panicIfNot(Chan).(*chanValue).TrySend(x) |
| 495 } |
| 496 |
| 497 // Type returns v's type. |
| 498 func (v Value) Type() Type { |
| 499 return v.internal().Type() |
| 500 } |
| 501 |
| 502 var uintKinds = []Kind{Uint, Uint8, Uint16, Uint32, Uint64, Uintptr} |
| 503 |
| 504 // Uint returns v's underlying value, as a uint64. |
| 505 // It panics if v's Kind is not a sized or unsized Uint kind. |
| 506 func (v Value) Uint() uint64 { |
| 507 return v.panicIfNots(uintKinds).(*uintValue).Get() |
| 508 } |
| 509 |
| 510 // UnsafeAddr returns a pointer to v's data. |
| 511 // It is for advanced clients that also import the "unsafe" package. |
| 512 func (v Value) UnsafeAddr() uintptr { |
| 513 return v.internal().UnsafeAddr() |
| 514 } |
| 515 |
| 516 // valueInterface is the common interface to reflection values. |
| 517 // The implementations of Value (e.g., arrayValue, structValue) |
46 // have additional type-specific methods. | 518 // have additional type-specific methods. |
47 type Value interface { | 519 type valueInterface interface { |
48 // Type returns the value's type. | 520 // Type returns the value's type. |
49 Type() Type | 521 Type() Type |
50 | 522 |
51 // Interface returns the value as an interface{}. | 523 // Interface returns the value as an interface{}. |
52 Interface() interface{} | 524 Interface() interface{} |
53 | 525 |
54 // CanSet returns true if the value can be changed. | 526 // CanSet returns true if the value can be changed. |
55 // Values obtained by the use of non-exported struct fields | 527 // Values obtained by the use of non-exported struct fields |
56 // can be used in Get but not Set. | 528 // can be used in Get but not Set. |
57 // If CanSet returns false, calling the type-specific Set will panic. | 529 // If CanSet returns false, calling the type-specific Set will panic. |
58 CanSet() bool | 530 CanSet() bool |
59 | 531 |
60 // SetValue assigns v to the value; v must have the same type as the val
ue. | 532 // SetValue assigns v to the value; v must have the same type as the val
ue. |
61 SetValue(v Value) | 533 SetValue(v Value) |
62 | 534 |
63 // CanAddr returns true if the value's address can be obtained with Addr
. | 535 // CanAddr returns true if the value's address can be obtained with Addr
. |
64 // Such values are called addressable. A value is addressable if it is | 536 // Such values are called addressable. A value is addressable if it is |
65 // an element of a slice, an element of an addressable array, | 537 // an element of a slice, an element of an addressable array, |
66 // a field of an addressable struct, the result of dereferencing a point
er, | 538 // a field of an addressable struct, the result of dereferencing a point
er, |
67 » // or the result of a call to NewValue, MakeChan, MakeMap, or MakeZero. | 539 » // or the result of a call to NewValue, MakeChan, MakeMap, or Zero. |
68 // If CanAddr returns false, calling Addr will panic. | 540 // If CanAddr returns false, calling Addr will panic. |
69 CanAddr() bool | 541 CanAddr() bool |
70 | 542 |
71 // Addr returns the address of the value. | 543 // Addr returns the address of the value. |
72 // If the value is not addressable, Addr panics. | 544 // If the value is not addressable, Addr panics. |
73 // Addr is typically used to obtain a pointer to a struct field or slice
element | 545 // Addr is typically used to obtain a pointer to a struct field or slice
element |
74 // in order to call a method that requires a pointer receiver. | 546 // in order to call a method that requires a pointer receiver. |
75 » Addr() *PtrValue | 547 » Addr() Value |
76 | 548 |
77 // UnsafeAddr returns a pointer to the underlying data. | 549 // UnsafeAddr returns a pointer to the underlying data. |
78 // It is for advanced clients that also import the "unsafe" package. | 550 // It is for advanced clients that also import the "unsafe" package. |
79 UnsafeAddr() uintptr | 551 UnsafeAddr() uintptr |
80 | 552 |
81 » // Method returns a FuncValue corresponding to the value's i'th method. | 553 » // Method returns a funcValue corresponding to the value's i'th method. |
82 » // The arguments to a Call on the returned FuncValue | 554 » // The arguments to a Call on the returned funcValue |
83 » // should not include a receiver; the FuncValue will use | 555 » // should not include a receiver; the funcValue will use |
84 // the value as the receiver. | 556 // the value as the receiver. |
85 » Method(i int) *FuncValue | 557 » Method(i int) Value |
| 558 |
| 559 » Kind() Kind |
86 | 560 |
87 getAddr() addr | 561 getAddr() addr |
88 } | 562 } |
89 | 563 |
90 // flags for value | 564 // flags for value |
91 const ( | 565 const ( |
92 canSet uint32 = 1 << iota // can set value (write to *v.addr) | 566 canSet uint32 = 1 << iota // can set value (write to *v.addr) |
93 canAddr // can take address of value | 567 canAddr // can take address of value |
94 canStore // can store through value (write to **v.add
r) | 568 canStore // can store through value (write to **v.add
r) |
95 ) | 569 ) |
96 | 570 |
97 // value is the common implementation of most values. | 571 // value is the common implementation of most values. |
98 // It is embedded in other, public struct types, but always | 572 // It is embedded in other, public struct types, but always |
99 // with a unique tag like "uint" or "float" so that the client cannot | 573 // with a unique tag like "uint" or "float" so that the client cannot |
100 // convert from, say, *UintValue to *FloatValue. | 574 // convert from, say, *uintValue to *floatValue. |
101 type value struct { | 575 type value struct { |
102 typ Type | 576 typ Type |
103 addr addr | 577 addr addr |
104 flag uint32 | 578 flag uint32 |
105 } | 579 } |
106 | 580 |
107 func (v *value) Type() Type { return v.typ } | 581 func (v *value) Type() Type { return v.typ } |
108 | 582 |
109 func (v *value) Addr() *PtrValue { | 583 func (v *value) Kind() Kind { return v.typ.Kind() } |
| 584 |
| 585 func (v *value) Addr() Value { |
110 if !v.CanAddr() { | 586 if !v.CanAddr() { |
111 panic("reflect: cannot take address of value") | 587 panic("reflect: cannot take address of value") |
112 } | 588 } |
113 a := v.addr | 589 a := v.addr |
114 flag := canSet | 590 flag := canSet |
115 if v.CanSet() { | 591 if v.CanSet() { |
116 flag |= canStore | 592 flag |= canStore |
117 } | 593 } |
118 // We could safely set canAddr here too - | 594 // We could safely set canAddr here too - |
119 // the caller would get the address of a - | 595 // the caller would get the address of a - |
120 // but it doesn't match the Go model. | 596 // but it doesn't match the Go model. |
121 // The language doesn't let you say &&v. | 597 // The language doesn't let you say &&v. |
122 » return newValue(PtrTo(v.typ), addr(&a), flag).(*PtrValue) | 598 » return newValue(PtrTo(v.typ), addr(&a), flag) |
123 } | 599 } |
124 | 600 |
125 func (v *value) UnsafeAddr() uintptr { return uintptr(v.addr) } | 601 func (v *value) UnsafeAddr() uintptr { return uintptr(v.addr) } |
126 | 602 |
127 func (v *value) getAddr() addr { return v.addr } | 603 func (v *value) getAddr() addr { return v.addr } |
128 | 604 |
129 func (v *value) Interface() interface{} { | 605 func (v *value) Interface() interface{} { |
130 » if typ, ok := v.typ.(*InterfaceType); ok { | 606 » typ := v.typ |
| 607 » if typ.Kind() == Interface { |
131 // There are two different representations of interface values, | 608 // There are two different representations of interface values, |
132 // one if the interface type has methods and one if it doesn't. | 609 // one if the interface type has methods and one if it doesn't. |
133 // These two representations require different expressions | 610 // These two representations require different expressions |
134 // to extract correctly. | 611 // to extract correctly. |
135 if typ.NumMethod() == 0 { | 612 if typ.NumMethod() == 0 { |
136 // Extract as interface value without methods. | 613 // Extract as interface value without methods. |
137 return *(*interface{})(v.addr) | 614 return *(*interface{})(v.addr) |
138 } | 615 } |
139 // Extract from v.addr as interface value with methods. | 616 // Extract from v.addr as interface value with methods. |
140 return *(*interface { | 617 return *(*interface { |
141 m() | 618 m() |
142 })(v.addr) | 619 })(v.addr) |
143 } | 620 } |
144 return unsafe.Unreflect(v.typ, unsafe.Pointer(v.addr)) | 621 return unsafe.Unreflect(v.typ, unsafe.Pointer(v.addr)) |
145 } | 622 } |
146 | 623 |
147 func (v *value) CanSet() bool { return v.flag&canSet != 0 } | 624 func (v *value) CanSet() bool { return v.flag&canSet != 0 } |
148 | 625 |
149 func (v *value) CanAddr() bool { return v.flag&canAddr != 0 } | 626 func (v *value) CanAddr() bool { return v.flag&canAddr != 0 } |
150 | 627 |
151 | 628 |
152 /* | 629 /* |
153 * basic types | 630 * basic types |
154 */ | 631 */ |
155 | 632 |
156 // BoolValue represents a bool value. | 633 // boolValue represents a bool value. |
157 type BoolValue struct { | 634 type boolValue struct { |
158 value "bool" | 635 value "bool" |
159 } | 636 } |
160 | 637 |
161 // Get returns the underlying bool value. | 638 // Get returns the underlying bool value. |
162 func (v *BoolValue) Get() bool { return *(*bool)(v.addr) } | 639 func (v *boolValue) Get() bool { return *(*bool)(v.addr) } |
163 | 640 |
164 // Set sets v to the value x. | 641 // Set sets v to the value x. |
165 func (v *BoolValue) Set(x bool) { | 642 func (v *boolValue) Set(x bool) { |
166 if !v.CanSet() { | 643 if !v.CanSet() { |
167 panic(cannotSet) | 644 panic(cannotSet) |
168 } | 645 } |
169 *(*bool)(v.addr) = x | 646 *(*bool)(v.addr) = x |
170 } | 647 } |
171 | 648 |
172 // Set sets v to the value x. | 649 // Set sets v to the value x. |
173 func (v *BoolValue) SetValue(x Value) { v.Set(x.(*BoolValue).Get()) } | 650 func (v *boolValue) SetValue(x Value) { v.Set(x.Bool()) } |
174 | 651 |
175 // FloatValue represents a float value. | 652 // floatValue represents a float value. |
176 type FloatValue struct { | 653 type floatValue struct { |
177 value "float" | 654 value "float" |
178 } | 655 } |
179 | 656 |
180 // Get returns the underlying int value. | 657 // Get returns the underlying int value. |
181 func (v *FloatValue) Get() float64 { | 658 func (v *floatValue) Get() float64 { |
182 switch v.typ.Kind() { | 659 switch v.typ.Kind() { |
183 case Float32: | 660 case Float32: |
184 return float64(*(*float32)(v.addr)) | 661 return float64(*(*float32)(v.addr)) |
185 case Float64: | 662 case Float64: |
186 return *(*float64)(v.addr) | 663 return *(*float64)(v.addr) |
187 } | 664 } |
188 panic("reflect: invalid float kind") | 665 panic("reflect: invalid float kind") |
189 } | 666 } |
190 | 667 |
191 // Set sets v to the value x. | 668 // Set sets v to the value x. |
192 func (v *FloatValue) Set(x float64) { | 669 func (v *floatValue) Set(x float64) { |
193 if !v.CanSet() { | 670 if !v.CanSet() { |
194 panic(cannotSet) | 671 panic(cannotSet) |
195 } | 672 } |
196 switch v.typ.Kind() { | 673 switch v.typ.Kind() { |
197 default: | 674 default: |
198 panic("reflect: invalid float kind") | 675 panic("reflect: invalid float kind") |
199 case Float32: | 676 case Float32: |
200 *(*float32)(v.addr) = float32(x) | 677 *(*float32)(v.addr) = float32(x) |
201 case Float64: | 678 case Float64: |
202 *(*float64)(v.addr) = x | 679 *(*float64)(v.addr) = x |
203 } | 680 } |
204 } | 681 } |
205 | 682 |
206 // Overflow returns true if x cannot be represented by the type of v. | 683 // Overflow returns true if x cannot be represented by the type of v. |
207 func (v *FloatValue) Overflow(x float64) bool { | 684 func (v *floatValue) Overflow(x float64) bool { |
208 if v.typ.Size() == 8 { | 685 if v.typ.Size() == 8 { |
209 return false | 686 return false |
210 } | 687 } |
211 if x < 0 { | 688 if x < 0 { |
212 x = -x | 689 x = -x |
213 } | 690 } |
214 return math.MaxFloat32 < x && x <= math.MaxFloat64 | 691 return math.MaxFloat32 < x && x <= math.MaxFloat64 |
215 } | 692 } |
216 | 693 |
217 // Set sets v to the value x. | 694 // Set sets v to the value x. |
218 func (v *FloatValue) SetValue(x Value) { v.Set(x.(*FloatValue).Get()) } | 695 func (v *floatValue) SetValue(x Value) { v.Set(x.Float()) } |
219 | 696 |
220 // ComplexValue represents a complex value. | 697 // complexValue represents a complex value. |
221 type ComplexValue struct { | 698 type complexValue struct { |
222 value "complex" | 699 value "complex" |
223 } | 700 } |
224 | 701 |
225 // Get returns the underlying complex value. | 702 // Get returns the underlying complex value. |
226 func (v *ComplexValue) Get() complex128 { | 703 func (v *complexValue) Get() complex128 { |
227 switch v.typ.Kind() { | 704 switch v.typ.Kind() { |
228 case Complex64: | 705 case Complex64: |
229 return complex128(*(*complex64)(v.addr)) | 706 return complex128(*(*complex64)(v.addr)) |
230 case Complex128: | 707 case Complex128: |
231 return *(*complex128)(v.addr) | 708 return *(*complex128)(v.addr) |
232 } | 709 } |
233 panic("reflect: invalid complex kind") | 710 panic("reflect: invalid complex kind") |
234 } | 711 } |
235 | 712 |
236 // Set sets v to the value x. | 713 // Set sets v to the value x. |
237 func (v *ComplexValue) Set(x complex128) { | 714 func (v *complexValue) Set(x complex128) { |
238 if !v.CanSet() { | 715 if !v.CanSet() { |
239 panic(cannotSet) | 716 panic(cannotSet) |
240 } | 717 } |
241 switch v.typ.Kind() { | 718 switch v.typ.Kind() { |
242 default: | 719 default: |
243 panic("reflect: invalid complex kind") | 720 panic("reflect: invalid complex kind") |
244 case Complex64: | 721 case Complex64: |
245 *(*complex64)(v.addr) = complex64(x) | 722 *(*complex64)(v.addr) = complex64(x) |
246 case Complex128: | 723 case Complex128: |
247 *(*complex128)(v.addr) = x | 724 *(*complex128)(v.addr) = x |
248 } | 725 } |
249 } | 726 } |
250 | 727 |
| 728 // How did we forget this one? |
| 729 func (v *complexValue) Overflow(x complex128) bool { |
| 730 if v.typ.Size() == 16 { |
| 731 return false |
| 732 } |
| 733 r := real(x) |
| 734 i := imag(x) |
| 735 if r < 0 { |
| 736 r = -r |
| 737 } |
| 738 if i < 0 { |
| 739 i = -i |
| 740 } |
| 741 return math.MaxFloat32 <= r && r <= math.MaxFloat64 || |
| 742 math.MaxFloat32 <= i && i <= math.MaxFloat64 |
| 743 } |
| 744 |
251 // Set sets v to the value x. | 745 // Set sets v to the value x. |
252 func (v *ComplexValue) SetValue(x Value) { v.Set(x.(*ComplexValue).Get()) } | 746 func (v *complexValue) SetValue(x Value) { v.Set(x.Complex()) } |
253 | 747 |
254 // IntValue represents an int value. | 748 // intValue represents an int value. |
255 type IntValue struct { | 749 type intValue struct { |
256 value "int" | 750 value "int" |
257 } | 751 } |
258 | 752 |
259 // Get returns the underlying int value. | 753 // Get returns the underlying int value. |
260 func (v *IntValue) Get() int64 { | 754 func (v *intValue) Get() int64 { |
261 switch v.typ.Kind() { | 755 switch v.typ.Kind() { |
262 case Int: | 756 case Int: |
263 return int64(*(*int)(v.addr)) | 757 return int64(*(*int)(v.addr)) |
264 case Int8: | 758 case Int8: |
265 return int64(*(*int8)(v.addr)) | 759 return int64(*(*int8)(v.addr)) |
266 case Int16: | 760 case Int16: |
267 return int64(*(*int16)(v.addr)) | 761 return int64(*(*int16)(v.addr)) |
268 case Int32: | 762 case Int32: |
269 return int64(*(*int32)(v.addr)) | 763 return int64(*(*int32)(v.addr)) |
270 case Int64: | 764 case Int64: |
271 return *(*int64)(v.addr) | 765 return *(*int64)(v.addr) |
272 } | 766 } |
273 panic("reflect: invalid int kind") | 767 panic("reflect: invalid int kind") |
274 } | 768 } |
275 | 769 |
276 // Set sets v to the value x. | 770 // Set sets v to the value x. |
277 func (v *IntValue) Set(x int64) { | 771 func (v *intValue) Set(x int64) { |
278 if !v.CanSet() { | 772 if !v.CanSet() { |
279 panic(cannotSet) | 773 panic(cannotSet) |
280 } | 774 } |
281 switch v.typ.Kind() { | 775 switch v.typ.Kind() { |
282 default: | 776 default: |
283 panic("reflect: invalid int kind") | 777 panic("reflect: invalid int kind") |
284 case Int: | 778 case Int: |
285 *(*int)(v.addr) = int(x) | 779 *(*int)(v.addr) = int(x) |
286 case Int8: | 780 case Int8: |
287 *(*int8)(v.addr) = int8(x) | 781 *(*int8)(v.addr) = int8(x) |
288 case Int16: | 782 case Int16: |
289 *(*int16)(v.addr) = int16(x) | 783 *(*int16)(v.addr) = int16(x) |
290 case Int32: | 784 case Int32: |
291 *(*int32)(v.addr) = int32(x) | 785 *(*int32)(v.addr) = int32(x) |
292 case Int64: | 786 case Int64: |
293 *(*int64)(v.addr) = x | 787 *(*int64)(v.addr) = x |
294 } | 788 } |
295 } | 789 } |
296 | 790 |
297 // Set sets v to the value x. | 791 // Set sets v to the value x. |
298 func (v *IntValue) SetValue(x Value) { v.Set(x.(*IntValue).Get()) } | 792 func (v *intValue) SetValue(x Value) { v.Set(x.Int()) } |
299 | 793 |
300 // Overflow returns true if x cannot be represented by the type of v. | 794 // Overflow returns true if x cannot be represented by the type of v. |
301 func (v *IntValue) Overflow(x int64) bool { | 795 func (v *intValue) Overflow(x int64) bool { |
302 bitSize := uint(v.typ.Bits()) | 796 bitSize := uint(v.typ.Bits()) |
303 trunc := (x << (64 - bitSize)) >> (64 - bitSize) | 797 trunc := (x << (64 - bitSize)) >> (64 - bitSize) |
304 return x != trunc | 798 return x != trunc |
305 } | 799 } |
306 | 800 |
307 // StringHeader is the runtime representation of a string. | 801 // StringHeader is the runtime representation of a string. |
308 type StringHeader struct { | 802 type StringHeader struct { |
309 Data uintptr | 803 Data uintptr |
310 Len int | 804 Len int |
311 } | 805 } |
312 | 806 |
313 // StringValue represents a string value. | 807 // stringValue represents a string value. |
314 type StringValue struct { | 808 type stringValue struct { |
315 value "string" | 809 value "string" |
316 } | 810 } |
317 | 811 |
318 // Get returns the underlying string value. | 812 // Get returns the underlying string value. |
319 func (v *StringValue) Get() string { return *(*string)(v.addr) } | 813 func (v *stringValue) Get() string { return *(*string)(v.addr) } |
320 | 814 |
321 // Set sets v to the value x. | 815 // Set sets v to the value x. |
322 func (v *StringValue) Set(x string) { | 816 func (v *stringValue) Set(x string) { |
323 if !v.CanSet() { | 817 if !v.CanSet() { |
324 panic(cannotSet) | 818 panic(cannotSet) |
325 } | 819 } |
326 *(*string)(v.addr) = x | 820 *(*string)(v.addr) = x |
327 } | 821 } |
328 | 822 |
329 // Set sets v to the value x. | 823 // Set sets v to the value x. |
330 func (v *StringValue) SetValue(x Value) { v.Set(x.(*StringValue).Get()) } | 824 func (v *stringValue) SetValue(x Value) { |
| 825 » // Do the kind check explicitly, because x.String() does not. |
| 826 » v.Set(x.panicIfNot(String).(*stringValue).Get()) |
| 827 } |
331 | 828 |
332 // UintValue represents a uint value. | 829 // uintValue represents a uint value. |
333 type UintValue struct { | 830 type uintValue struct { |
334 value "uint" | 831 value "uint" |
335 } | 832 } |
336 | 833 |
337 // Get returns the underlying uuint value. | 834 // Get returns the underlying uuint value. |
338 func (v *UintValue) Get() uint64 { | 835 func (v *uintValue) Get() uint64 { |
339 switch v.typ.Kind() { | 836 switch v.typ.Kind() { |
340 case Uint: | 837 case Uint: |
341 return uint64(*(*uint)(v.addr)) | 838 return uint64(*(*uint)(v.addr)) |
342 case Uint8: | 839 case Uint8: |
343 return uint64(*(*uint8)(v.addr)) | 840 return uint64(*(*uint8)(v.addr)) |
344 case Uint16: | 841 case Uint16: |
345 return uint64(*(*uint16)(v.addr)) | 842 return uint64(*(*uint16)(v.addr)) |
346 case Uint32: | 843 case Uint32: |
347 return uint64(*(*uint32)(v.addr)) | 844 return uint64(*(*uint32)(v.addr)) |
348 case Uint64: | 845 case Uint64: |
349 return *(*uint64)(v.addr) | 846 return *(*uint64)(v.addr) |
350 case Uintptr: | 847 case Uintptr: |
351 return uint64(*(*uintptr)(v.addr)) | 848 return uint64(*(*uintptr)(v.addr)) |
352 } | 849 } |
353 panic("reflect: invalid uint kind") | 850 panic("reflect: invalid uint kind") |
354 } | 851 } |
355 | 852 |
356 // Set sets v to the value x. | 853 // Set sets v to the value x. |
357 func (v *UintValue) Set(x uint64) { | 854 func (v *uintValue) Set(x uint64) { |
358 if !v.CanSet() { | 855 if !v.CanSet() { |
359 panic(cannotSet) | 856 panic(cannotSet) |
360 } | 857 } |
361 switch v.typ.Kind() { | 858 switch v.typ.Kind() { |
362 default: | 859 default: |
363 panic("reflect: invalid uint kind") | 860 panic("reflect: invalid uint kind") |
364 case Uint: | 861 case Uint: |
365 *(*uint)(v.addr) = uint(x) | 862 *(*uint)(v.addr) = uint(x) |
366 case Uint8: | 863 case Uint8: |
367 *(*uint8)(v.addr) = uint8(x) | 864 *(*uint8)(v.addr) = uint8(x) |
368 case Uint16: | 865 case Uint16: |
369 *(*uint16)(v.addr) = uint16(x) | 866 *(*uint16)(v.addr) = uint16(x) |
370 case Uint32: | 867 case Uint32: |
371 *(*uint32)(v.addr) = uint32(x) | 868 *(*uint32)(v.addr) = uint32(x) |
372 case Uint64: | 869 case Uint64: |
373 *(*uint64)(v.addr) = x | 870 *(*uint64)(v.addr) = x |
374 case Uintptr: | 871 case Uintptr: |
375 *(*uintptr)(v.addr) = uintptr(x) | 872 *(*uintptr)(v.addr) = uintptr(x) |
376 } | 873 } |
377 } | 874 } |
378 | 875 |
379 // Overflow returns true if x cannot be represented by the type of v. | 876 // Overflow returns true if x cannot be represented by the type of v. |
380 func (v *UintValue) Overflow(x uint64) bool { | 877 func (v *uintValue) Overflow(x uint64) bool { |
381 bitSize := uint(v.typ.Bits()) | 878 bitSize := uint(v.typ.Bits()) |
382 trunc := (x << (64 - bitSize)) >> (64 - bitSize) | 879 trunc := (x << (64 - bitSize)) >> (64 - bitSize) |
383 return x != trunc | 880 return x != trunc |
384 } | 881 } |
385 | 882 |
386 // Set sets v to the value x. | 883 // Set sets v to the value x. |
387 func (v *UintValue) SetValue(x Value) { v.Set(x.(*UintValue).Get()) } | 884 func (v *uintValue) SetValue(x Value) { v.Set(x.Uint()) } |
388 | 885 |
389 // UnsafePointerValue represents an unsafe.Pointer value. | 886 // unsafePointerValue represents an unsafe.Pointer value. |
390 type UnsafePointerValue struct { | 887 type unsafePointerValue struct { |
391 value "unsafe.Pointer" | 888 value "unsafe.Pointer" |
392 } | 889 } |
393 | 890 |
394 // Get returns the underlying uintptr value. | 891 // Get returns the underlying uintptr value. |
395 // Get returns uintptr, not unsafe.Pointer, so that | 892 // Get returns uintptr, not unsafe.Pointer, so that |
396 // programs that do not import "unsafe" cannot | 893 // programs that do not import "unsafe" cannot |
397 // obtain a value of unsafe.Pointer type from "reflect". | 894 // obtain a value of unsafe.Pointer type from "reflect". |
398 func (v *UnsafePointerValue) Get() uintptr { return uintptr(*(*unsafe.Pointer)(v
.addr)) } | 895 func (v *unsafePointerValue) Get() uintptr { return uintptr(*(*unsafe.Pointer)(v
.addr)) } |
399 | 896 |
400 // Set sets v to the value x. | 897 // Set sets v to the value x. |
401 func (v *UnsafePointerValue) Set(x unsafe.Pointer) { | 898 func (v *unsafePointerValue) Set(x unsafe.Pointer) { |
402 if !v.CanSet() { | 899 if !v.CanSet() { |
403 panic(cannotSet) | 900 panic(cannotSet) |
404 } | 901 } |
405 *(*unsafe.Pointer)(v.addr) = x | 902 *(*unsafe.Pointer)(v.addr) = x |
406 } | 903 } |
407 | 904 |
408 // Set sets v to the value x. | 905 // Set sets v to the value x. |
409 func (v *UnsafePointerValue) SetValue(x Value) { | 906 func (v *unsafePointerValue) SetValue(x Value) { |
410 » v.Set(unsafe.Pointer(x.(*UnsafePointerValue).Get())) | 907 » // Do the kind check explicitly, because x.UnsafePointer |
| 908 » // applies to more than just the UnsafePointer Kind. |
| 909 » v.Set(unsafe.Pointer(x.panicIfNot(UnsafePointer).(*unsafePointerValue).G
et())) |
411 } | 910 } |
412 | 911 |
413 func typesMustMatch(t1, t2 Type) { | 912 func typesMustMatch(t1, t2 Type) { |
414 if t1 != t2 { | 913 if t1 != t2 { |
415 panic("type mismatch: " + t1.String() + " != " + t2.String()) | 914 panic("type mismatch: " + t1.String() + " != " + t2.String()) |
416 } | 915 } |
417 } | 916 } |
418 | 917 |
419 /* | 918 /* |
420 * array | 919 * array |
421 */ | 920 */ |
422 | 921 |
423 // ArrayOrSliceValue is the common interface | 922 // ArrayOrSliceValue is the common interface |
424 // implemented by both ArrayValue and SliceValue. | 923 // implemented by both arrayValue and sliceValue. |
425 type ArrayOrSliceValue interface { | 924 type arrayOrSliceValue interface { |
426 » Value | 925 » valueInterface |
427 Len() int | 926 Len() int |
428 Cap() int | 927 Cap() int |
429 Elem(i int) Value | 928 Elem(i int) Value |
430 addr() addr | 929 addr() addr |
431 } | 930 } |
432 | 931 |
433 // grow grows the slice s so that it can hold extra more values, allocating | 932 // grow grows the slice s so that it can hold extra more values, allocating |
434 // more capacity if needed. It also returns the old and new slice lengths. | 933 // more capacity if needed. It also returns the old and new slice lengths. |
435 func grow(s *SliceValue, extra int) (*SliceValue, int, int) { | 934 func grow(s Value, extra int) (Value, int, int) { |
436 i0 := s.Len() | 935 i0 := s.Len() |
437 i1 := i0 + extra | 936 i1 := i0 + extra |
438 if i1 < i0 { | 937 if i1 < i0 { |
439 panic("append: slice overflow") | 938 panic("append: slice overflow") |
440 } | 939 } |
441 m := s.Cap() | 940 m := s.Cap() |
442 if i1 <= m { | 941 if i1 <= m { |
443 return s.Slice(0, i1), i0, i1 | 942 return s.Slice(0, i1), i0, i1 |
444 } | 943 } |
445 if m == 0 { | 944 if m == 0 { |
446 m = extra | 945 m = extra |
447 } else { | 946 } else { |
448 for m < i1 { | 947 for m < i1 { |
449 if i0 < 1024 { | 948 if i0 < 1024 { |
450 m += m | 949 m += m |
451 } else { | 950 } else { |
452 m += m / 4 | 951 m += m / 4 |
453 } | 952 } |
454 } | 953 } |
455 } | 954 } |
456 » t := MakeSlice(s.Type().(*SliceType), i1, m) | 955 » t := MakeSlice(s.Type(), i1, m) |
457 Copy(t, s) | 956 Copy(t, s) |
458 return t, i0, i1 | 957 return t, i0, i1 |
459 } | 958 } |
460 | 959 |
461 // Append appends the values x to a slice s and returns the resulting slice. | 960 // Append appends the values x to a slice s and returns the resulting slice. |
462 // Each x must have the same type as s' element type. | 961 // Each x must have the same type as s' element type. |
463 func Append(s *SliceValue, x ...Value) *SliceValue { | 962 func Append(s Value, x ...Value) Value { |
464 s, i0, i1 := grow(s, len(x)) | 963 s, i0, i1 := grow(s, len(x)) |
| 964 sa := s.panicIfNot(Slice).(*sliceValue) |
465 for i, j := i0, 0; i < i1; i, j = i+1, j+1 { | 965 for i, j := i0, 0; i < i1; i, j = i+1, j+1 { |
466 » » s.Elem(i).SetValue(x[j]) | 966 » » sa.Elem(i).Set(x[j]) |
467 } | 967 } |
468 return s | 968 return s |
469 } | 969 } |
470 | 970 |
471 // AppendSlice appends a slice t to a slice s and returns the resulting slice. | 971 // AppendSlice appends a slice t to a slice s and returns the resulting slice. |
472 // The slices s and t must have the same element type. | 972 // The slices s and t must have the same element type. |
473 func AppendSlice(s, t *SliceValue) *SliceValue { | 973 func AppendSlice(s, t Value) Value { |
474 s, i0, i1 := grow(s, t.Len()) | 974 s, i0, i1 := grow(s, t.Len()) |
475 Copy(s.Slice(i0, i1), t) | 975 Copy(s.Slice(i0, i1), t) |
476 return s | 976 return s |
477 } | 977 } |
478 | 978 |
479 // Copy copies the contents of src into dst until either | 979 // Copy copies the contents of src into dst until either |
480 // dst has been filled or src has been exhausted. | 980 // dst has been filled or src has been exhausted. |
481 // It returns the number of elements copied. | 981 // It returns the number of elements copied. |
482 // The arrays dst and src must have the same element type. | 982 // Dst and src each must be a slice or array, and they |
483 func Copy(dst, src ArrayOrSliceValue) int { | 983 // must have the same element type. |
| 984 func Copy(dst, src Value) int { |
484 // TODO: This will have to move into the runtime | 985 // TODO: This will have to move into the runtime |
485 // once the real gc goes in. | 986 // once the real gc goes in. |
486 » de := dst.Type().(ArrayOrSliceType).Elem() | 987 » de := dst.Type().Elem() |
487 » se := src.Type().(ArrayOrSliceType).Elem() | 988 » se := src.Type().Elem() |
488 typesMustMatch(de, se) | 989 typesMustMatch(de, se) |
489 n := dst.Len() | 990 n := dst.Len() |
490 if xn := src.Len(); n > xn { | 991 if xn := src.Len(); n > xn { |
491 n = xn | 992 n = xn |
492 } | 993 } |
493 » memmove(dst.addr(), src.addr(), uintptr(n)*de.Size()) | 994 » memmove(dst.panicIfNots(arrayOrSlice).(arrayOrSliceValue).addr(), |
| 995 » » src.panicIfNots(arrayOrSlice).(arrayOrSliceValue).addr(), |
| 996 » » uintptr(n)*de.Size()) |
494 return n | 997 return n |
495 } | 998 } |
496 | 999 |
497 // An ArrayValue represents an array. | 1000 // An arrayValue represents an array. |
498 type ArrayValue struct { | 1001 type arrayValue struct { |
499 value "array" | 1002 value "array" |
500 } | 1003 } |
501 | 1004 |
502 // Len returns the length of the array. | 1005 // Len returns the length of the array. |
503 func (v *ArrayValue) Len() int { return v.typ.(*ArrayType).Len() } | 1006 func (v *arrayValue) Len() int { return v.typ.Len() } |
504 | 1007 |
505 // Cap returns the capacity of the array (equal to Len()). | 1008 // Cap returns the capacity of the array (equal to Len()). |
506 func (v *ArrayValue) Cap() int { return v.typ.(*ArrayType).Len() } | 1009 func (v *arrayValue) Cap() int { return v.typ.Len() } |
507 | 1010 |
508 // addr returns the base address of the data in the array. | 1011 // addr returns the base address of the data in the array. |
509 func (v *ArrayValue) addr() addr { return v.value.addr } | 1012 func (v *arrayValue) addr() addr { return v.value.addr } |
510 | 1013 |
511 // Set assigns x to v. | 1014 // Set assigns x to v. |
512 // The new value x must have the same type as v. | 1015 // The new value x must have the same type as v. |
513 func (v *ArrayValue) Set(x *ArrayValue) { | 1016 func (v *arrayValue) Set(x *arrayValue) { |
514 if !v.CanSet() { | 1017 if !v.CanSet() { |
515 panic(cannotSet) | 1018 panic(cannotSet) |
516 } | 1019 } |
517 typesMustMatch(v.typ, x.typ) | 1020 typesMustMatch(v.typ, x.typ) |
518 » Copy(v, x) | 1021 » Copy(Value{v}, Value{x}) |
519 } | 1022 } |
520 | 1023 |
521 // Set sets v to the value x. | 1024 // Set sets v to the value x. |
522 func (v *ArrayValue) SetValue(x Value) { v.Set(x.(*ArrayValue)) } | 1025 func (v *arrayValue) SetValue(x Value) { |
| 1026 » v.Set(x.panicIfNot(Array).(*arrayValue)) |
| 1027 } |
523 | 1028 |
524 // Elem returns the i'th element of v. | 1029 // Elem returns the i'th element of v. |
525 func (v *ArrayValue) Elem(i int) Value { | 1030 func (v *arrayValue) Elem(i int) Value { |
526 » typ := v.typ.(*ArrayType).Elem() | 1031 » typ := v.typ.Elem() |
527 n := v.Len() | 1032 n := v.Len() |
528 if i < 0 || i >= n { | 1033 if i < 0 || i >= n { |
529 panic("array index out of bounds") | 1034 panic("array index out of bounds") |
530 } | 1035 } |
531 p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size()) | 1036 p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size()) |
532 return newValue(typ, p, v.flag) | 1037 return newValue(typ, p, v.flag) |
533 } | 1038 } |
534 | 1039 |
535 /* | 1040 /* |
536 * slice | 1041 * slice |
537 */ | 1042 */ |
538 | 1043 |
539 // runtime representation of slice | 1044 // runtime representation of slice |
540 type SliceHeader struct { | 1045 type SliceHeader struct { |
541 Data uintptr | 1046 Data uintptr |
542 Len int | 1047 Len int |
543 Cap int | 1048 Cap int |
544 } | 1049 } |
545 | 1050 |
546 // A SliceValue represents a slice. | 1051 // A sliceValue represents a slice. |
547 type SliceValue struct { | 1052 type sliceValue struct { |
548 value "slice" | 1053 value "slice" |
549 } | 1054 } |
550 | 1055 |
551 func (v *SliceValue) slice() *SliceHeader { return (*SliceHeader)(v.value.addr)
} | 1056 func (v *sliceValue) slice() *SliceHeader { return (*SliceHeader)(v.value.addr)
} |
552 | 1057 |
553 // IsNil returns whether v is a nil slice. | 1058 // IsNil returns whether v is a nil slice. |
554 func (v *SliceValue) IsNil() bool { return v.slice().Data == 0 } | 1059 func (v *sliceValue) IsNil() bool { return v.slice().Data == 0 } |
555 | 1060 |
556 // Len returns the length of the slice. | 1061 // Len returns the length of the slice. |
557 func (v *SliceValue) Len() int { return int(v.slice().Len) } | 1062 func (v *sliceValue) Len() int { return int(v.slice().Len) } |
558 | 1063 |
559 // Cap returns the capacity of the slice. | 1064 // Cap returns the capacity of the slice. |
560 func (v *SliceValue) Cap() int { return int(v.slice().Cap) } | 1065 func (v *sliceValue) Cap() int { return int(v.slice().Cap) } |
561 | 1066 |
562 // addr returns the base address of the data in the slice. | 1067 // addr returns the base address of the data in the slice. |
563 func (v *SliceValue) addr() addr { return addr(v.slice().Data) } | 1068 func (v *sliceValue) addr() addr { return addr(v.slice().Data) } |
564 | 1069 |
565 // SetLen changes the length of v. | 1070 // SetLen changes the length of v. |
566 // The new length n must be between 0 and the capacity, inclusive. | 1071 // The new length n must be between 0 and the capacity, inclusive. |
567 func (v *SliceValue) SetLen(n int) { | 1072 func (v *sliceValue) SetLen(n int) { |
568 s := v.slice() | 1073 s := v.slice() |
569 if n < 0 || n > int(s.Cap) { | 1074 if n < 0 || n > int(s.Cap) { |
570 panic("reflect: slice length out of range in SetLen") | 1075 panic("reflect: slice length out of range in SetLen") |
571 } | 1076 } |
572 s.Len = n | 1077 s.Len = n |
573 } | 1078 } |
574 | 1079 |
575 // Set assigns x to v. | 1080 // Set assigns x to v. |
576 // The new value x must have the same type as v. | 1081 // The new value x must have the same type as v. |
577 func (v *SliceValue) Set(x *SliceValue) { | 1082 func (v *sliceValue) Set(x *sliceValue) { |
578 if !v.CanSet() { | 1083 if !v.CanSet() { |
579 panic(cannotSet) | 1084 panic(cannotSet) |
580 } | 1085 } |
581 typesMustMatch(v.typ, x.typ) | 1086 typesMustMatch(v.typ, x.typ) |
582 *v.slice() = *x.slice() | 1087 *v.slice() = *x.slice() |
583 } | 1088 } |
584 | 1089 |
585 // Set sets v to the value x. | 1090 // Set sets v to the value x. |
586 func (v *SliceValue) SetValue(x Value) { v.Set(x.(*SliceValue)) } | 1091 func (v *sliceValue) SetValue(x Value) { |
| 1092 » v.Set(x.panicIfNot(Slice).(*sliceValue)) |
| 1093 } |
587 | 1094 |
588 // Get returns the uintptr address of the v.Cap()'th element. This gives | 1095 // Get returns the uintptr address of the v.Cap()'th element. This gives |
589 // the same result for all slices of the same array. | 1096 // the same result for all slices of the same array. |
590 // It is mainly useful for printing. | 1097 // It is mainly useful for printing. |
591 func (v *SliceValue) Get() uintptr { | 1098 func (v *sliceValue) Get() uintptr { |
592 » typ := v.typ.(*SliceType) | 1099 » typ := v.typ |
593 return uintptr(v.addr()) + uintptr(v.Cap())*typ.Elem().Size() | 1100 return uintptr(v.addr()) + uintptr(v.Cap())*typ.Elem().Size() |
594 } | 1101 } |
595 | 1102 |
596 // Slice returns a sub-slice of the slice v. | 1103 // Slice returns a sub-slice of the slice v. |
597 func (v *SliceValue) Slice(beg, end int) *SliceValue { | 1104 func (v *sliceValue) Slice(beg, end int) Value { |
598 cap := v.Cap() | 1105 cap := v.Cap() |
599 if beg < 0 || end < beg || end > cap { | 1106 if beg < 0 || end < beg || end > cap { |
600 panic("slice index out of bounds") | 1107 panic("slice index out of bounds") |
601 } | 1108 } |
602 » typ := v.typ.(*SliceType) | 1109 » typ := v.typ |
603 s := new(SliceHeader) | 1110 s := new(SliceHeader) |
604 s.Data = uintptr(v.addr()) + uintptr(beg)*typ.Elem().Size() | 1111 s.Data = uintptr(v.addr()) + uintptr(beg)*typ.Elem().Size() |
605 s.Len = end - beg | 1112 s.Len = end - beg |
606 s.Cap = cap - beg | 1113 s.Cap = cap - beg |
607 | 1114 |
608 // Like the result of Addr, we treat Slice as an | 1115 // Like the result of Addr, we treat Slice as an |
609 // unaddressable temporary, so don't set canAddr. | 1116 // unaddressable temporary, so don't set canAddr. |
610 flag := canSet | 1117 flag := canSet |
611 if v.flag&canStore != 0 { | 1118 if v.flag&canStore != 0 { |
612 flag |= canStore | 1119 flag |= canStore |
613 } | 1120 } |
614 » return newValue(typ, addr(s), flag).(*SliceValue) | 1121 » return newValue(typ, addr(s), flag) |
615 } | 1122 } |
616 | 1123 |
617 // Elem returns the i'th element of v. | 1124 // Elem returns the i'th element of v. |
618 func (v *SliceValue) Elem(i int) Value { | 1125 func (v *sliceValue) Elem(i int) Value { |
619 » typ := v.typ.(*SliceType).Elem() | 1126 » typ := v.typ.Elem() |
620 n := v.Len() | 1127 n := v.Len() |
621 if i < 0 || i >= n { | 1128 if i < 0 || i >= n { |
622 panic("reflect: slice index out of range") | 1129 panic("reflect: slice index out of range") |
623 } | 1130 } |
624 p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size()) | 1131 p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size()) |
625 flag := canAddr | 1132 flag := canAddr |
626 if v.flag&canStore != 0 { | 1133 if v.flag&canStore != 0 { |
627 flag |= canSet | canStore | 1134 flag |= canSet | canStore |
628 } | 1135 } |
629 return newValue(typ, p, flag) | 1136 return newValue(typ, p, flag) |
630 } | 1137 } |
631 | 1138 |
632 // MakeSlice creates a new zero-initialized slice value | 1139 // MakeSlice creates a new zero-initialized slice value |
633 // for the specified slice type, length, and capacity. | 1140 // for the specified slice type, length, and capacity. |
634 func MakeSlice(typ *SliceType, len, cap int) *SliceValue { | 1141 func MakeSlice(typ Type, len, cap int) Value { |
| 1142 » if typ.Kind() != Slice { |
| 1143 » » panic("reflect: MakeSlice of non-slice type") |
| 1144 » } |
635 s := &SliceHeader{ | 1145 s := &SliceHeader{ |
636 Data: uintptr(unsafe.NewArray(typ.Elem(), cap)), | 1146 Data: uintptr(unsafe.NewArray(typ.Elem(), cap)), |
637 Len: len, | 1147 Len: len, |
638 Cap: cap, | 1148 Cap: cap, |
639 } | 1149 } |
640 » return newValue(typ, addr(s), canAddr|canSet|canStore).(*SliceValue) | 1150 » return newValue(typ, addr(s), canAddr|canSet|canStore) |
641 } | 1151 } |
642 | 1152 |
643 /* | 1153 /* |
644 * chan | 1154 * chan |
645 */ | 1155 */ |
646 | 1156 |
647 // A ChanValue represents a chan. | 1157 // A chanValue represents a chan. |
648 type ChanValue struct { | 1158 type chanValue struct { |
649 value "chan" | 1159 value "chan" |
650 } | 1160 } |
651 | 1161 |
652 // IsNil returns whether v is a nil channel. | 1162 // IsNil returns whether v is a nil channel. |
653 func (v *ChanValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 } | 1163 func (v *chanValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 } |
654 | 1164 |
655 // Set assigns x to v. | 1165 // Set assigns x to v. |
656 // The new value x must have the same type as v. | 1166 // The new value x must have the same type as v. |
657 func (v *ChanValue) Set(x *ChanValue) { | 1167 func (v *chanValue) Set(x *chanValue) { |
658 if !v.CanSet() { | 1168 if !v.CanSet() { |
659 panic(cannotSet) | 1169 panic(cannotSet) |
660 } | 1170 } |
661 typesMustMatch(v.typ, x.typ) | 1171 typesMustMatch(v.typ, x.typ) |
662 *(*uintptr)(v.addr) = *(*uintptr)(x.addr) | 1172 *(*uintptr)(v.addr) = *(*uintptr)(x.addr) |
663 } | 1173 } |
664 | 1174 |
665 // Set sets v to the value x. | 1175 // Set sets v to the value x. |
666 func (v *ChanValue) SetValue(x Value) { v.Set(x.(*ChanValue)) } | 1176 func (v *chanValue) SetValue(x Value) { |
| 1177 » v.Set(x.panicIfNot(Chan).(*chanValue)) |
| 1178 } |
667 | 1179 |
668 // Get returns the uintptr value of v. | 1180 // Get returns the uintptr value of v. |
669 // It is mainly useful for printing. | 1181 // It is mainly useful for printing. |
670 func (v *ChanValue) Get() uintptr { return *(*uintptr)(v.addr) } | 1182 func (v *chanValue) Get() uintptr { return *(*uintptr)(v.addr) } |
671 | 1183 |
672 // implemented in ../pkg/runtime/reflect.cgo | 1184 // implemented in ../pkg/runtime/reflect.cgo |
673 func makechan(typ *runtime.ChanType, size uint32) (ch *byte) | 1185 func makechan(typ *runtime.ChanType, size uint32) (ch *byte) |
674 func chansend(ch, val *byte, selected *bool) | 1186 func chansend(ch, val *byte, selected *bool) |
675 func chanrecv(ch, val *byte, selected *bool, ok *bool) | 1187 func chanrecv(ch, val *byte, selected *bool, ok *bool) |
676 func chanclose(ch *byte) | 1188 func chanclose(ch *byte) |
677 func chanlen(ch *byte) int32 | 1189 func chanlen(ch *byte) int32 |
678 func chancap(ch *byte) int32 | 1190 func chancap(ch *byte) int32 |
679 | 1191 |
680 // Close closes the channel. | 1192 // Close closes the channel. |
681 func (v *ChanValue) Close() { | 1193 func (v *chanValue) Close() { |
682 ch := *(**byte)(v.addr) | 1194 ch := *(**byte)(v.addr) |
683 chanclose(ch) | 1195 chanclose(ch) |
684 } | 1196 } |
685 | 1197 |
686 func (v *ChanValue) Len() int { | 1198 func (v *chanValue) Len() int { |
687 ch := *(**byte)(v.addr) | 1199 ch := *(**byte)(v.addr) |
688 return int(chanlen(ch)) | 1200 return int(chanlen(ch)) |
689 } | 1201 } |
690 | 1202 |
691 func (v *ChanValue) Cap() int { | 1203 func (v *chanValue) Cap() int { |
692 ch := *(**byte)(v.addr) | 1204 ch := *(**byte)(v.addr) |
693 return int(chancap(ch)) | 1205 return int(chancap(ch)) |
694 } | 1206 } |
695 | 1207 |
696 // internal send; non-blocking if selected != nil | 1208 // internal send; non-blocking if selected != nil |
697 func (v *ChanValue) send(x Value, selected *bool) { | 1209 func (v *chanValue) send(x Value, selected *bool) { |
698 » t := v.Type().(*ChanType) | 1210 » t := v.Type() |
699 » if t.Dir()&SendDir == 0 { | 1211 » if t.ChanDir()&SendDir == 0 { |
700 panic("send on recv-only channel") | 1212 panic("send on recv-only channel") |
701 } | 1213 } |
702 typesMustMatch(t.Elem(), x.Type()) | 1214 typesMustMatch(t.Elem(), x.Type()) |
703 ch := *(**byte)(v.addr) | 1215 ch := *(**byte)(v.addr) |
704 » chansend(ch, (*byte)(x.getAddr()), selected) | 1216 » chansend(ch, (*byte)(x.internal().getAddr()), selected) |
705 } | 1217 } |
706 | 1218 |
707 // internal recv; non-blocking if selected != nil | 1219 // internal recv; non-blocking if selected != nil |
708 func (v *ChanValue) recv(selected *bool) (Value, bool) { | 1220 func (v *chanValue) recv(selected *bool) (Value, bool) { |
709 » t := v.Type().(*ChanType) | 1221 » t := v.Type() |
710 » if t.Dir()&RecvDir == 0 { | 1222 » if t.ChanDir()&RecvDir == 0 { |
711 panic("recv on send-only channel") | 1223 panic("recv on send-only channel") |
712 } | 1224 } |
713 ch := *(**byte)(v.addr) | 1225 ch := *(**byte)(v.addr) |
714 » x := MakeZero(t.Elem()) | 1226 » x := Zero(t.Elem()) |
715 var ok bool | 1227 var ok bool |
716 » chanrecv(ch, (*byte)(x.getAddr()), selected, &ok) | 1228 » chanrecv(ch, (*byte)(x.internal().getAddr()), selected, &ok) |
717 return x, ok | 1229 return x, ok |
718 } | 1230 } |
719 | 1231 |
720 // Send sends x on the channel v. | 1232 // Send sends x on the channel v. |
721 func (v *ChanValue) Send(x Value) { v.send(x, nil) } | 1233 func (v *chanValue) Send(x Value) { v.send(x, nil) } |
722 | 1234 |
723 // Recv receives and returns a value from the channel v. | 1235 // Recv receives and returns a value from the channel v. |
724 // The receive blocks until a value is ready. | 1236 // The receive blocks until a value is ready. |
725 // The boolean value ok is true if the value x corresponds to a send | 1237 // The boolean value ok is true if the value x corresponds to a send |
726 // on the channel, false if it is a zero value received because the channel is c
losed. | 1238 // on the channel, false if it is a zero value received because the channel is c
losed. |
727 func (v *ChanValue) Recv() (x Value, ok bool) { | 1239 func (v *chanValue) Recv() (x Value, ok bool) { |
728 return v.recv(nil) | 1240 return v.recv(nil) |
729 } | 1241 } |
730 | 1242 |
731 // TrySend attempts to sends x on the channel v but will not block. | 1243 // TrySend attempts to sends x on the channel v but will not block. |
732 // It returns true if the value was sent, false otherwise. | 1244 // It returns true if the value was sent, false otherwise. |
733 func (v *ChanValue) TrySend(x Value) bool { | 1245 func (v *chanValue) TrySend(x Value) bool { |
734 var selected bool | 1246 var selected bool |
735 v.send(x, &selected) | 1247 v.send(x, &selected) |
736 return selected | 1248 return selected |
737 } | 1249 } |
738 | 1250 |
739 // TryRecv attempts to receive a value from the channel v but will not block. | 1251 // TryRecv attempts to receive a value from the channel v but will not block. |
740 // If the receive cannot finish without blocking, TryRecv instead returns x == n
il. | 1252 // If the receive cannot finish without blocking, TryRecv instead returns x == n
il. |
741 // If the receive can finish without blocking, TryRecv returns x != nil. | 1253 // If the receive can finish without blocking, TryRecv returns x != nil. |
742 // The boolean value ok is true if the value x corresponds to a send | 1254 // The boolean value ok is true if the value x corresponds to a send |
743 // on the channel, false if it is a zero value received because the channel is c
losed. | 1255 // on the channel, false if it is a zero value received because the channel is c
losed. |
744 func (v *ChanValue) TryRecv() (x Value, ok bool) { | 1256 func (v *chanValue) TryRecv() (x Value, ok bool) { |
745 var selected bool | 1257 var selected bool |
746 x, ok = v.recv(&selected) | 1258 x, ok = v.recv(&selected) |
747 if !selected { | 1259 if !selected { |
748 » » return nil, false | 1260 » » return Value{}, false |
749 } | 1261 } |
750 return x, ok | 1262 return x, ok |
751 } | 1263 } |
752 | 1264 |
753 // MakeChan creates a new channel with the specified type and buffer size. | 1265 // MakeChan creates a new channel with the specified type and buffer size. |
754 func MakeChan(typ *ChanType, buffer int) *ChanValue { | 1266 func MakeChan(typ Type, buffer int) Value { |
| 1267 » if typ.Kind() != Chan { |
| 1268 » » panic("reflect: MakeChan of non-chan type") |
| 1269 » } |
755 if buffer < 0 { | 1270 if buffer < 0 { |
756 panic("MakeChan: negative buffer size") | 1271 panic("MakeChan: negative buffer size") |
757 } | 1272 } |
758 » if typ.Dir() != BothDir { | 1273 » if typ.ChanDir() != BothDir { |
759 panic("MakeChan: unidirectional channel type") | 1274 panic("MakeChan: unidirectional channel type") |
760 } | 1275 } |
761 » v := MakeZero(typ).(*ChanValue) | 1276 » v := Zero(typ) |
762 » *(**byte)(v.addr) = makechan((*runtime.ChanType)(unsafe.Pointer(typ)), u
int32(buffer)) | 1277 » ch := v.panicIfNot(Chan).(*chanValue) |
| 1278 » *(**byte)(ch.addr) = makechan((*runtime.ChanType)(unsafe.Pointer(typ.(*c
ommonType))), uint32(buffer)) |
763 return v | 1279 return v |
764 } | 1280 } |
765 | 1281 |
766 /* | 1282 /* |
767 * func | 1283 * func |
768 */ | 1284 */ |
769 | 1285 |
770 // A FuncValue represents a function value. | 1286 // A funcValue represents a function value. |
771 type FuncValue struct { | 1287 type funcValue struct { |
772 value "func" | 1288 value "func" |
773 first *value | 1289 first *value |
774 isInterface bool | 1290 isInterface bool |
775 } | 1291 } |
776 | 1292 |
777 // IsNil returns whether v is a nil function. | 1293 // IsNil returns whether v is a nil function. |
778 func (v *FuncValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 } | 1294 func (v *funcValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 } |
779 | 1295 |
780 // Get returns the uintptr value of v. | 1296 // Get returns the uintptr value of v. |
781 // It is mainly useful for printing. | 1297 // It is mainly useful for printing. |
782 func (v *FuncValue) Get() uintptr { return *(*uintptr)(v.addr) } | 1298 func (v *funcValue) Get() uintptr { return *(*uintptr)(v.addr) } |
783 | 1299 |
784 // Set assigns x to v. | 1300 // Set assigns x to v. |
785 // The new value x must have the same type as v. | 1301 // The new value x must have the same type as v. |
786 func (v *FuncValue) Set(x *FuncValue) { | 1302 func (v *funcValue) Set(x *funcValue) { |
787 if !v.CanSet() { | 1303 if !v.CanSet() { |
788 panic(cannotSet) | 1304 panic(cannotSet) |
789 } | 1305 } |
790 typesMustMatch(v.typ, x.typ) | 1306 typesMustMatch(v.typ, x.typ) |
791 *(*uintptr)(v.addr) = *(*uintptr)(x.addr) | 1307 *(*uintptr)(v.addr) = *(*uintptr)(x.addr) |
792 } | 1308 } |
793 | 1309 |
794 // Set sets v to the value x. | 1310 // Set sets v to the value x. |
795 func (v *FuncValue) SetValue(x Value) { v.Set(x.(*FuncValue)) } | 1311 func (v *funcValue) SetValue(x Value) { |
| 1312 » v.Set(x.panicIfNot(Func).(*funcValue)) |
| 1313 } |
796 | 1314 |
797 // Method returns a FuncValue corresponding to v's i'th method. | 1315 // Method returns a funcValue corresponding to v's i'th method. |
798 // The arguments to a Call on the returned FuncValue | 1316 // The arguments to a Call on the returned funcValue |
799 // should not include a receiver; the FuncValue will use v | 1317 // should not include a receiver; the funcValue will use v |
800 // as the receiver. | 1318 // as the receiver. |
801 func (v *value) Method(i int) *FuncValue { | 1319 func (v *value) Method(i int) Value { |
802 t := v.Type().uncommon() | 1320 t := v.Type().uncommon() |
803 if t == nil || i < 0 || i >= len(t.methods) { | 1321 if t == nil || i < 0 || i >= len(t.methods) { |
804 » » return nil | 1322 » » panic("reflect: Method index out of range") |
805 } | 1323 } |
806 p := &t.methods[i] | 1324 p := &t.methods[i] |
807 fn := p.tfn | 1325 fn := p.tfn |
808 » fv := &FuncValue{value: value{toType(*p.typ), addr(&fn), 0}, first: v, i
sInterface: false} | 1326 » fv := &funcValue{value: value{toType(p.typ), addr(&fn), 0}, first: v, is
Interface: false} |
809 » return fv | 1327 » return Value{fv} |
810 } | 1328 } |
811 | 1329 |
812 // implemented in ../pkg/runtime/*/asm.s | 1330 // implemented in ../pkg/runtime/*/asm.s |
813 func call(fn, arg *byte, n uint32) | 1331 func call(fn, arg *byte, n uint32) |
814 | 1332 |
815 type tiny struct { | 1333 type tiny struct { |
816 b byte | 1334 b byte |
817 } | 1335 } |
818 | 1336 |
819 // Interface returns the fv as an interface value. | 1337 // Interface returns the fv as an interface value. |
820 // If fv is a method obtained by invoking Value.Method | 1338 // If fv is a method obtained by invoking Value.Method |
821 // (as opposed to Type.Method), Interface cannot return an | 1339 // (as opposed to Type.Method), Interface cannot return an |
822 // interface value, so it panics. | 1340 // interface value, so it panics. |
823 func (fv *FuncValue) Interface() interface{} { | 1341 func (fv *funcValue) Interface() interface{} { |
824 if fv.first != nil { | 1342 if fv.first != nil { |
825 » » panic("FuncValue: cannot create interface value for method with
bound receiver") | 1343 » » panic("funcValue: cannot create interface value for method with
bound receiver") |
826 } | 1344 } |
827 return fv.value.Interface() | 1345 return fv.value.Interface() |
828 } | 1346 } |
829 | 1347 |
830 // Call calls the function fv with input parameters in. | 1348 // Call calls the function fv with input parameters in. |
831 // It returns the function's output parameters as Values. | 1349 // It returns the function's output parameters as Values. |
832 func (fv *FuncValue) Call(in []Value) []Value { | 1350 func (fv *funcValue) Call(in []Value) []Value { |
833 » t := fv.Type().(*FuncType) | 1351 » t := fv.Type() |
834 nin := len(in) | 1352 nin := len(in) |
835 if fv.first != nil && !fv.isInterface { | 1353 if fv.first != nil && !fv.isInterface { |
836 nin++ | 1354 nin++ |
837 } | 1355 } |
838 if nin != t.NumIn() { | 1356 if nin != t.NumIn() { |
839 » » panic("FuncValue: wrong argument count") | 1357 » » panic("funcValue: wrong argument count") |
840 } | 1358 } |
841 nout := t.NumOut() | 1359 nout := t.NumOut() |
842 | 1360 |
843 // Compute arg size & allocate. | 1361 // Compute arg size & allocate. |
844 // This computation is 6g/8g-dependent | 1362 // This computation is 6g/8g-dependent |
845 // and probably wrong for gccgo, but so | 1363 // and probably wrong for gccgo, but so |
846 // is most of this function. | 1364 // is most of this function. |
847 size := uintptr(0) | 1365 size := uintptr(0) |
848 if fv.isInterface { | 1366 if fv.isInterface { |
849 // extra word for interface value | 1367 // extra word for interface value |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 off = n | 1417 off = n |
900 delta = 1 | 1418 delta = 1 |
901 } | 1419 } |
902 } | 1420 } |
903 for i, v := range in { | 1421 for i, v := range in { |
904 tv := v.Type() | 1422 tv := v.Type() |
905 typesMustMatch(t.In(i+delta), tv) | 1423 typesMustMatch(t.In(i+delta), tv) |
906 a := uintptr(tv.Align()) | 1424 a := uintptr(tv.Align()) |
907 off = (off + a - 1) &^ (a - 1) | 1425 off = (off + a - 1) &^ (a - 1) |
908 n := tv.Size() | 1426 n := tv.Size() |
909 » » memmove(addr(ptr+off), v.getAddr(), n) | 1427 » » memmove(addr(ptr+off), v.internal().getAddr(), n) |
910 off += n | 1428 off += n |
911 } | 1429 } |
912 off = (off + ptrSize - 1) &^ (ptrSize - 1) | 1430 off = (off + ptrSize - 1) &^ (ptrSize - 1) |
913 | 1431 |
914 // Call | 1432 // Call |
915 call(*(**byte)(fv.addr), (*byte)(addr(ptr)), uint32(size)) | 1433 call(*(**byte)(fv.addr), (*byte)(addr(ptr)), uint32(size)) |
916 | 1434 |
917 // Copy return values out of args. | 1435 // Copy return values out of args. |
918 // | 1436 // |
919 // TODO(rsc): revisit like above. | 1437 // TODO(rsc): revisit like above. |
920 ret := make([]Value, nout) | 1438 ret := make([]Value, nout) |
921 for i := 0; i < nout; i++ { | 1439 for i := 0; i < nout; i++ { |
922 tv := t.Out(i) | 1440 tv := t.Out(i) |
923 a := uintptr(tv.Align()) | 1441 a := uintptr(tv.Align()) |
924 off = (off + a - 1) &^ (a - 1) | 1442 off = (off + a - 1) &^ (a - 1) |
925 » » v := MakeZero(tv) | 1443 » » v := Zero(tv) |
926 n := tv.Size() | 1444 n := tv.Size() |
927 » » memmove(v.getAddr(), addr(ptr+off), n) | 1445 » » memmove(v.internal().getAddr(), addr(ptr+off), n) |
928 ret[i] = v | 1446 ret[i] = v |
929 off += n | 1447 off += n |
930 } | 1448 } |
931 | 1449 |
932 return ret | 1450 return ret |
933 } | 1451 } |
934 | 1452 |
935 /* | 1453 /* |
936 * interface | 1454 * interface |
937 */ | 1455 */ |
938 | 1456 |
939 // An InterfaceValue represents an interface value. | 1457 // An interfaceValue represents an interface value. |
940 type InterfaceValue struct { | 1458 type interfaceValue struct { |
941 value "interface" | 1459 value "interface" |
942 } | 1460 } |
943 | 1461 |
944 // IsNil returns whether v is a nil interface value. | 1462 // IsNil returns whether v is a nil interface value. |
945 func (v *InterfaceValue) IsNil() bool { return v.Interface() == nil } | 1463 func (v *interfaceValue) IsNil() bool { return v.Interface() == nil } |
946 | 1464 |
947 // No single uinptr Get because v.Interface() is available. | 1465 // No single uinptr Get because v.Interface() is available. |
948 | 1466 |
949 // Get returns the two words that represent an interface in the runtime. | 1467 // Get returns the two words that represent an interface in the runtime. |
950 // Those words are useful only when playing unsafe games. | 1468 // Those words are useful only when playing unsafe games. |
951 func (v *InterfaceValue) Get() [2]uintptr { | 1469 func (v *interfaceValue) Get() [2]uintptr { |
952 return *(*[2]uintptr)(v.addr) | 1470 return *(*[2]uintptr)(v.addr) |
953 } | 1471 } |
954 | 1472 |
955 // Elem returns the concrete value stored in the interface value v. | 1473 // Elem returns the concrete value stored in the interface value v. |
956 func (v *InterfaceValue) Elem() Value { return NewValue(v.Interface()) } | 1474 func (v *interfaceValue) Elem() Value { return NewValue(v.Interface()) } |
957 | 1475 |
958 // ../runtime/reflect.cgo | 1476 // ../runtime/reflect.cgo |
959 func setiface(typ *InterfaceType, x *interface{}, addr addr) | 1477 func setiface(typ *interfaceType, x *interface{}, addr addr) |
960 | 1478 |
961 // Set assigns x to v. | 1479 // Set assigns x to v. |
962 func (v *InterfaceValue) Set(x Value) { | 1480 func (v *interfaceValue) Set(x Value) { |
963 » var i interface{} | 1481 » i := x.Interface() |
964 » if x != nil { | |
965 » » i = x.Interface() | |
966 » } | |
967 if !v.CanSet() { | 1482 if !v.CanSet() { |
968 panic(cannotSet) | 1483 panic(cannotSet) |
969 } | 1484 } |
970 // Two different representations; see comment in Get. | 1485 // Two different representations; see comment in Get. |
971 // Empty interface is easy. | 1486 // Empty interface is easy. |
972 » t := v.typ.(*InterfaceType) | 1487 » t := (*interfaceType)(unsafe.Pointer(v.typ.(*commonType))) |
973 if t.NumMethod() == 0 { | 1488 if t.NumMethod() == 0 { |
974 *(*interface{})(v.addr) = i | 1489 *(*interface{})(v.addr) = i |
975 return | 1490 return |
976 } | 1491 } |
977 | 1492 |
978 // Non-empty interface requires a runtime check. | 1493 // Non-empty interface requires a runtime check. |
979 setiface(t, &i, v.addr) | 1494 setiface(t, &i, v.addr) |
980 } | 1495 } |
981 | 1496 |
982 // Set sets v to the value x. | 1497 // Set sets v to the value x. |
983 func (v *InterfaceValue) SetValue(x Value) { v.Set(x) } | 1498 func (v *interfaceValue) SetValue(x Value) { v.Set(x) } |
984 | 1499 |
985 // Method returns a FuncValue corresponding to v's i'th method. | 1500 // Method returns a funcValue corresponding to v's i'th method. |
986 // The arguments to a Call on the returned FuncValue | 1501 // The arguments to a Call on the returned funcValue |
987 // should not include a receiver; the FuncValue will use v | 1502 // should not include a receiver; the funcValue will use v |
988 // as the receiver. | 1503 // as the receiver. |
989 func (v *InterfaceValue) Method(i int) *FuncValue { | 1504 func (v *interfaceValue) Method(i int) Value { |
990 » t := v.Type().(*InterfaceType) | 1505 » t := (*interfaceType)(unsafe.Pointer(v.Type().(*commonType))) |
991 if t == nil || i < 0 || i >= len(t.methods) { | 1506 if t == nil || i < 0 || i >= len(t.methods) { |
992 » » return nil | 1507 » » panic("reflect: Method index out of range") |
993 } | 1508 } |
994 p := &t.methods[i] | 1509 p := &t.methods[i] |
995 | 1510 |
996 // Interface is two words: itable, data. | 1511 // Interface is two words: itable, data. |
997 tab := *(**runtime.Itable)(v.addr) | 1512 tab := *(**runtime.Itable)(v.addr) |
998 data := &value{Typeof((*byte)(nil)), addr(uintptr(v.addr) + ptrSize), 0} | 1513 data := &value{Typeof((*byte)(nil)), addr(uintptr(v.addr) + ptrSize), 0} |
999 | 1514 |
1000 // Function pointer is at p.perm in the table. | 1515 // Function pointer is at p.perm in the table. |
1001 fn := tab.Fn[i] | 1516 fn := tab.Fn[i] |
1002 » fv := &FuncValue{value: value{toType(*p.typ), addr(&fn), 0}, first: data
, isInterface: true} | 1517 » fv := &funcValue{value: value{toType(p.typ), addr(&fn), 0}, first: data,
isInterface: true} |
1003 » return fv | 1518 » return Value{fv} |
1004 } | 1519 } |
1005 | 1520 |
1006 /* | 1521 /* |
1007 * map | 1522 * map |
1008 */ | 1523 */ |
1009 | 1524 |
1010 // A MapValue represents a map value. | 1525 // A mapValue represents a map value. |
1011 type MapValue struct { | 1526 type mapValue struct { |
1012 value "map" | 1527 value "map" |
1013 } | 1528 } |
1014 | 1529 |
1015 // IsNil returns whether v is a nil map value. | 1530 // IsNil returns whether v is a nil map value. |
1016 func (v *MapValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 } | 1531 func (v *mapValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 } |
1017 | 1532 |
1018 // Set assigns x to v. | 1533 // Set assigns x to v. |
1019 // The new value x must have the same type as v. | 1534 // The new value x must have the same type as v. |
1020 func (v *MapValue) Set(x *MapValue) { | 1535 func (v *mapValue) Set(x *mapValue) { |
1021 if !v.CanSet() { | 1536 if !v.CanSet() { |
1022 panic(cannotSet) | 1537 panic(cannotSet) |
1023 } | 1538 } |
1024 if x == nil { | 1539 if x == nil { |
1025 *(**uintptr)(v.addr) = nil | 1540 *(**uintptr)(v.addr) = nil |
1026 return | 1541 return |
1027 } | 1542 } |
1028 typesMustMatch(v.typ, x.typ) | 1543 typesMustMatch(v.typ, x.typ) |
1029 *(*uintptr)(v.addr) = *(*uintptr)(x.addr) | 1544 *(*uintptr)(v.addr) = *(*uintptr)(x.addr) |
1030 } | 1545 } |
1031 | 1546 |
1032 // Set sets v to the value x. | 1547 // Set sets v to the value x. |
1033 func (v *MapValue) SetValue(x Value) { | 1548 func (v *mapValue) SetValue(x Value) { |
1034 » if x == nil { | 1549 » v.Set(x.panicIfNot(Map).(*mapValue)) |
1035 » » v.Set(nil) | |
1036 » » return | |
1037 » } | |
1038 » v.Set(x.(*MapValue)) | |
1039 } | 1550 } |
1040 | 1551 |
1041 // Get returns the uintptr value of v. | 1552 // Get returns the uintptr value of v. |
1042 // It is mainly useful for printing. | 1553 // It is mainly useful for printing. |
1043 func (v *MapValue) Get() uintptr { return *(*uintptr)(v.addr) } | 1554 func (v *mapValue) Get() uintptr { return *(*uintptr)(v.addr) } |
1044 | 1555 |
1045 // implemented in ../pkg/runtime/reflect.cgo | 1556 // implemented in ../pkg/runtime/reflect.cgo |
1046 func mapaccess(m, key, val *byte) bool | 1557 func mapaccess(m, key, val *byte) bool |
1047 func mapassign(m, key, val *byte) | 1558 func mapassign(m, key, val *byte) |
1048 func maplen(m *byte) int32 | 1559 func maplen(m *byte) int32 |
1049 func mapiterinit(m *byte) *byte | 1560 func mapiterinit(m *byte) *byte |
1050 func mapiternext(it *byte) | 1561 func mapiternext(it *byte) |
1051 func mapiterkey(it *byte, key *byte) bool | 1562 func mapiterkey(it *byte, key *byte) bool |
1052 func makemap(t *runtime.MapType) *byte | 1563 func makemap(t *runtime.MapType) *byte |
1053 | 1564 |
1054 // Elem returns the value associated with key in the map v. | 1565 // Elem returns the value associated with key in the map v. |
1055 // It returns nil if key is not found in the map. | 1566 // It returns nil if key is not found in the map. |
1056 func (v *MapValue) Elem(key Value) Value { | 1567 func (v *mapValue) Elem(key Value) Value { |
1057 » t := v.Type().(*MapType) | 1568 » t := v.Type() |
1058 typesMustMatch(t.Key(), key.Type()) | 1569 typesMustMatch(t.Key(), key.Type()) |
1059 m := *(**byte)(v.addr) | 1570 m := *(**byte)(v.addr) |
1060 if m == nil { | 1571 if m == nil { |
1061 » » return nil | 1572 » » return Value{} |
1062 } | 1573 } |
1063 » newval := MakeZero(t.Elem()) | 1574 » newval := Zero(t.Elem()) |
1064 » if !mapaccess(m, (*byte)(key.getAddr()), (*byte)(newval.getAddr())) { | 1575 » if !mapaccess(m, (*byte)(key.internal().getAddr()), (*byte)(newval.inter
nal().getAddr())) { |
1065 » » return nil | 1576 » » return Value{} |
1066 } | 1577 } |
1067 return newval | 1578 return newval |
1068 } | 1579 } |
1069 | 1580 |
1070 // SetElem sets the value associated with key in the map v to val. | 1581 // SetElem sets the value associated with key in the map v to val. |
1071 // If val is nil, Put deletes the key from map. | 1582 // If val is nil, Put deletes the key from map. |
1072 func (v *MapValue) SetElem(key, val Value) { | 1583 func (v *mapValue) SetElem(key, val Value) { |
1073 » t := v.Type().(*MapType) | 1584 » t := v.Type() |
1074 typesMustMatch(t.Key(), key.Type()) | 1585 typesMustMatch(t.Key(), key.Type()) |
1075 var vaddr *byte | 1586 var vaddr *byte |
1076 » if val != nil { | 1587 » if val.IsValid() { |
1077 typesMustMatch(t.Elem(), val.Type()) | 1588 typesMustMatch(t.Elem(), val.Type()) |
1078 » » vaddr = (*byte)(val.getAddr()) | 1589 » » vaddr = (*byte)(val.internal().getAddr()) |
1079 } | 1590 } |
1080 m := *(**byte)(v.addr) | 1591 m := *(**byte)(v.addr) |
1081 » mapassign(m, (*byte)(key.getAddr()), vaddr) | 1592 » mapassign(m, (*byte)(key.internal().getAddr()), vaddr) |
1082 } | 1593 } |
1083 | 1594 |
1084 // Len returns the number of keys in the map v. | 1595 // Len returns the number of keys in the map v. |
1085 func (v *MapValue) Len() int { | 1596 func (v *mapValue) Len() int { |
1086 m := *(**byte)(v.addr) | 1597 m := *(**byte)(v.addr) |
1087 if m == nil { | 1598 if m == nil { |
1088 return 0 | 1599 return 0 |
1089 } | 1600 } |
1090 return int(maplen(m)) | 1601 return int(maplen(m)) |
1091 } | 1602 } |
1092 | 1603 |
1093 // Keys returns a slice containing all the keys present in the map, | 1604 // Keys returns a slice containing all the keys present in the map, |
1094 // in unspecified order. | 1605 // in unspecified order. |
1095 func (v *MapValue) Keys() []Value { | 1606 func (v *mapValue) Keys() []Value { |
1096 » tk := v.Type().(*MapType).Key() | 1607 » tk := v.Type().Key() |
1097 m := *(**byte)(v.addr) | 1608 m := *(**byte)(v.addr) |
1098 mlen := int32(0) | 1609 mlen := int32(0) |
1099 if m != nil { | 1610 if m != nil { |
1100 mlen = maplen(m) | 1611 mlen = maplen(m) |
1101 } | 1612 } |
1102 it := mapiterinit(m) | 1613 it := mapiterinit(m) |
1103 a := make([]Value, mlen) | 1614 a := make([]Value, mlen) |
1104 var i int | 1615 var i int |
1105 for i = 0; i < len(a); i++ { | 1616 for i = 0; i < len(a); i++ { |
1106 » » k := MakeZero(tk) | 1617 » » k := Zero(tk) |
1107 » » if !mapiterkey(it, (*byte)(k.getAddr())) { | 1618 » » if !mapiterkey(it, (*byte)(k.internal().getAddr())) { |
1108 break | 1619 break |
1109 } | 1620 } |
1110 a[i] = k | 1621 a[i] = k |
1111 mapiternext(it) | 1622 mapiternext(it) |
1112 } | 1623 } |
1113 return a[0:i] | 1624 return a[0:i] |
1114 } | 1625 } |
1115 | 1626 |
1116 // MakeMap creates a new map of the specified type. | 1627 // MakeMap creates a new map of the specified type. |
1117 func MakeMap(typ *MapType) *MapValue { | 1628 func MakeMap(typ Type) Value { |
1118 » v := MakeZero(typ).(*MapValue) | 1629 » if typ.Kind() != Map { |
1119 » *(**byte)(v.addr) = makemap((*runtime.MapType)(unsafe.Pointer(typ))) | 1630 » » panic("reflect: MakeMap of non-map type") |
| 1631 » } |
| 1632 » v := Zero(typ) |
| 1633 » m := v.panicIfNot(Map).(*mapValue) |
| 1634 » *(**byte)(m.addr) = makemap((*runtime.MapType)(unsafe.Pointer(typ.(*comm
onType)))) |
1120 return v | 1635 return v |
1121 } | 1636 } |
1122 | 1637 |
1123 /* | 1638 /* |
1124 * ptr | 1639 * ptr |
1125 */ | 1640 */ |
1126 | 1641 |
1127 // A PtrValue represents a pointer. | 1642 // A ptrValue represents a pointer. |
1128 type PtrValue struct { | 1643 type ptrValue struct { |
1129 value "ptr" | 1644 value "ptr" |
1130 } | 1645 } |
1131 | 1646 |
1132 // IsNil returns whether v is a nil pointer. | 1647 // IsNil returns whether v is a nil pointer. |
1133 func (v *PtrValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 } | 1648 func (v *ptrValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 } |
1134 | 1649 |
1135 // Get returns the uintptr value of v. | 1650 // Get returns the uintptr value of v. |
1136 // It is mainly useful for printing. | 1651 // It is mainly useful for printing. |
1137 func (v *PtrValue) Get() uintptr { return *(*uintptr)(v.addr) } | 1652 func (v *ptrValue) Get() uintptr { return *(*uintptr)(v.addr) } |
1138 | 1653 |
1139 // Set assigns x to v. | 1654 // Set assigns x to v. |
1140 // The new value x must have the same type as v, and x.Elem().CanSet() must be t
rue. | 1655 // The new value x must have the same type as v, and x.Elem().CanSet() must be t
rue. |
1141 func (v *PtrValue) Set(x *PtrValue) { | 1656 func (v *ptrValue) Set(x *ptrValue) { |
1142 if x == nil { | 1657 if x == nil { |
1143 *(**uintptr)(v.addr) = nil | 1658 *(**uintptr)(v.addr) = nil |
1144 return | 1659 return |
1145 } | 1660 } |
1146 if !v.CanSet() { | 1661 if !v.CanSet() { |
1147 panic(cannotSet) | 1662 panic(cannotSet) |
1148 } | 1663 } |
1149 if x.flag&canStore == 0 { | 1664 if x.flag&canStore == 0 { |
1150 panic("cannot copy pointer obtained from unexported struct field
") | 1665 panic("cannot copy pointer obtained from unexported struct field
") |
1151 } | 1666 } |
1152 typesMustMatch(v.typ, x.typ) | 1667 typesMustMatch(v.typ, x.typ) |
1153 // TODO: This will have to move into the runtime | 1668 // TODO: This will have to move into the runtime |
1154 // once the new gc goes in | 1669 // once the new gc goes in |
1155 *(*uintptr)(v.addr) = *(*uintptr)(x.addr) | 1670 *(*uintptr)(v.addr) = *(*uintptr)(x.addr) |
1156 } | 1671 } |
1157 | 1672 |
1158 // Set sets v to the value x. | 1673 // Set sets v to the value x. |
1159 func (v *PtrValue) SetValue(x Value) { | 1674 func (v *ptrValue) SetValue(x Value) { |
1160 » if x == nil { | 1675 » v.Set(x.panicIfNot(Ptr).(*ptrValue)) |
1161 » » v.Set(nil) | |
1162 » » return | |
1163 » } | |
1164 » v.Set(x.(*PtrValue)) | |
1165 } | 1676 } |
1166 | 1677 |
1167 // PointTo changes v to point to x. | 1678 // PointTo changes v to point to x. |
1168 // If x is a nil Value, PointTo sets v to nil. | 1679 // If x is a nil Value, PointTo sets v to nil. |
1169 func (v *PtrValue) PointTo(x Value) { | 1680 func (v *ptrValue) PointTo(x Value) { |
1170 » if x == nil { | 1681 » if !x.IsValid() { |
1171 *(**uintptr)(v.addr) = nil | 1682 *(**uintptr)(v.addr) = nil |
1172 return | 1683 return |
1173 } | 1684 } |
1174 if !x.CanSet() { | 1685 if !x.CanSet() { |
1175 panic("cannot set x; cannot point to x") | 1686 panic("cannot set x; cannot point to x") |
1176 } | 1687 } |
1177 » typesMustMatch(v.typ.(*PtrType).Elem(), x.Type()) | 1688 » typesMustMatch(v.typ.Elem(), x.Type()) |
1178 // TODO: This will have to move into the runtime | 1689 // TODO: This will have to move into the runtime |
1179 // once the new gc goes in. | 1690 // once the new gc goes in. |
1180 *(*uintptr)(v.addr) = x.UnsafeAddr() | 1691 *(*uintptr)(v.addr) = x.UnsafeAddr() |
1181 } | 1692 } |
1182 | 1693 |
1183 // Elem returns the value that v points to. | 1694 // Elem returns the value that v points to. |
1184 // If v is a nil pointer, Elem returns a nil Value. | 1695 // If v is a nil pointer, Elem returns a nil Value. |
1185 func (v *PtrValue) Elem() Value { | 1696 func (v *ptrValue) Elem() Value { |
1186 if v.IsNil() { | 1697 if v.IsNil() { |
1187 » » return nil | 1698 » » return Value{} |
1188 } | 1699 } |
1189 flag := canAddr | 1700 flag := canAddr |
1190 if v.flag&canStore != 0 { | 1701 if v.flag&canStore != 0 { |
1191 flag |= canSet | canStore | 1702 flag |= canSet | canStore |
1192 } | 1703 } |
1193 » return newValue(v.typ.(*PtrType).Elem(), *(*addr)(v.addr), flag) | 1704 » return newValue(v.typ.Elem(), *(*addr)(v.addr), flag) |
1194 } | 1705 } |
1195 | 1706 |
1196 // Indirect returns the value that v points to. | 1707 // Indirect returns the value that v points to. |
1197 // If v is a nil pointer, Indirect returns a nil Value. | 1708 // If v is a nil pointer, Indirect returns a nil Value. |
1198 // If v is not a pointer, Indirect returns v. | 1709 // If v is not a pointer, Indirect returns v. |
1199 func Indirect(v Value) Value { | 1710 func Indirect(v Value) Value { |
1200 » if pv, ok := v.(*PtrValue); ok { | 1711 » if v.Kind() != Ptr { |
1201 » » return pv.Elem() | 1712 » » return v |
1202 } | 1713 } |
1203 » return v | 1714 » return v.panicIfNot(Ptr).(*ptrValue).Elem() |
1204 } | 1715 } |
1205 | 1716 |
1206 /* | 1717 /* |
1207 * struct | 1718 * struct |
1208 */ | 1719 */ |
1209 | 1720 |
1210 // A StructValue represents a struct value. | 1721 // A structValue represents a struct value. |
1211 type StructValue struct { | 1722 type structValue struct { |
1212 value "struct" | 1723 value "struct" |
1213 } | 1724 } |
1214 | 1725 |
1215 // Set assigns x to v. | 1726 // Set assigns x to v. |
1216 // The new value x must have the same type as v. | 1727 // The new value x must have the same type as v. |
1217 func (v *StructValue) Set(x *StructValue) { | 1728 func (v *structValue) Set(x *structValue) { |
1218 // TODO: This will have to move into the runtime | 1729 // TODO: This will have to move into the runtime |
1219 // once the gc goes in. | 1730 // once the gc goes in. |
1220 if !v.CanSet() { | 1731 if !v.CanSet() { |
1221 panic(cannotSet) | 1732 panic(cannotSet) |
1222 } | 1733 } |
1223 typesMustMatch(v.typ, x.typ) | 1734 typesMustMatch(v.typ, x.typ) |
1224 memmove(v.addr, x.addr, v.typ.Size()) | 1735 memmove(v.addr, x.addr, v.typ.Size()) |
1225 } | 1736 } |
1226 | 1737 |
1227 // Set sets v to the value x. | 1738 // Set sets v to the value x. |
1228 func (v *StructValue) SetValue(x Value) { v.Set(x.(*StructValue)) } | 1739 func (v *structValue) SetValue(x Value) { |
| 1740 » v.Set(x.panicIfNot(Struct).(*structValue)) |
| 1741 } |
1229 | 1742 |
1230 // Field returns the i'th field of the struct. | 1743 // Field returns the i'th field of the struct. |
1231 func (v *StructValue) Field(i int) Value { | 1744 func (v *structValue) Field(i int) Value { |
1232 » t := v.typ.(*StructType) | 1745 » t := v.typ |
1233 if i < 0 || i >= t.NumField() { | 1746 if i < 0 || i >= t.NumField() { |
1234 » » return nil | 1747 » » panic("reflect: Field index out of range") |
1235 } | 1748 } |
1236 f := t.Field(i) | 1749 f := t.Field(i) |
1237 flag := v.flag | 1750 flag := v.flag |
1238 if f.PkgPath != "" { | 1751 if f.PkgPath != "" { |
1239 // unexported field | 1752 // unexported field |
1240 flag &^= canSet | canStore | 1753 flag &^= canSet | canStore |
1241 } | 1754 } |
1242 return newValue(f.Type, addr(uintptr(v.addr)+f.Offset), flag) | 1755 return newValue(f.Type, addr(uintptr(v.addr)+f.Offset), flag) |
1243 } | 1756 } |
1244 | 1757 |
1245 // FieldByIndex returns the nested field corresponding to index. | 1758 // FieldByIndex returns the nested field corresponding to index. |
1246 func (t *StructValue) FieldByIndex(index []int) (v Value) { | 1759 func (t *structValue) FieldByIndex(index []int) (v Value) { |
1247 » v = t | 1760 » v = Value{t} |
1248 for i, x := range index { | 1761 for i, x := range index { |
1249 if i > 0 { | 1762 if i > 0 { |
1250 » » » if p, ok := v.(*PtrValue); ok { | 1763 » » » if v.Kind() == Ptr { |
1251 » » » » v = p.Elem() | 1764 » » » » v = v.Elem() |
1252 } | 1765 } |
1253 » » » if s, ok := v.(*StructValue); ok { | 1766 » » » if v.Kind() != Struct { |
1254 » » » » t = s | 1767 » » » » return Value{} |
1255 » » » } else { | |
1256 » » » » v = nil | |
1257 » » » » return | |
1258 } | 1768 } |
1259 } | 1769 } |
1260 » » v = t.Field(x) | 1770 » » v = v.Field(x) |
1261 } | 1771 } |
1262 return | 1772 return |
1263 } | 1773 } |
1264 | 1774 |
1265 // FieldByName returns the struct field with the given name. | 1775 // FieldByName returns the struct field with the given name. |
1266 // The result is nil if no field was found. | 1776 // The result is nil if no field was found. |
1267 func (t *StructValue) FieldByName(name string) Value { | 1777 func (t *structValue) FieldByName(name string) Value { |
1268 » if f, ok := t.Type().(*StructType).FieldByName(name); ok { | 1778 » if f, ok := t.Type().FieldByName(name); ok { |
1269 return t.FieldByIndex(f.Index) | 1779 return t.FieldByIndex(f.Index) |
1270 } | 1780 } |
1271 » return nil | 1781 » return Value{} |
1272 } | 1782 } |
1273 | 1783 |
1274 // FieldByNameFunc returns the struct field with a name that satisfies the | 1784 // FieldByNameFunc returns the struct field with a name that satisfies the |
1275 // match function. | 1785 // match function. |
1276 // The result is nil if no field was found. | 1786 // The result is nil if no field was found. |
1277 func (t *StructValue) FieldByNameFunc(match func(string) bool) Value { | 1787 func (t *structValue) FieldByNameFunc(match func(string) bool) Value { |
1278 » if f, ok := t.Type().(*StructType).FieldByNameFunc(match); ok { | 1788 » if f, ok := t.Type().FieldByNameFunc(match); ok { |
1279 return t.FieldByIndex(f.Index) | 1789 return t.FieldByIndex(f.Index) |
1280 } | 1790 } |
1281 » return nil | 1791 » return Value{} |
1282 } | 1792 } |
1283 | 1793 |
1284 // NumField returns the number of fields in the struct. | 1794 // NumField returns the number of fields in the struct. |
1285 func (v *StructValue) NumField() int { return v.typ.(*StructType).NumField() } | 1795 func (v *structValue) NumField() int { return v.typ.NumField() } |
1286 | 1796 |
1287 /* | 1797 /* |
1288 * constructors | 1798 * constructors |
1289 */ | 1799 */ |
1290 | 1800 |
1291 // NewValue returns a new Value initialized to the concrete value | 1801 // NewValue returns a new Value initialized to the concrete value |
1292 // stored in the interface i. NewValue(nil) returns nil. | 1802 // stored in the interface i. NewValue(nil) returns the zero Value. |
1293 func NewValue(i interface{}) Value { | 1803 func NewValue(i interface{}) Value { |
1294 if i == nil { | 1804 if i == nil { |
1295 » » return nil | 1805 » » return Value{} |
1296 } | 1806 } |
1297 » t, a := unsafe.Reflect(i) | 1807 » _, a := unsafe.Reflect(i) |
1298 » return newValue(toType(t), addr(a), canSet|canAddr|canStore) | 1808 » return newValue(Typeof(i), addr(a), canSet|canAddr|canStore) |
1299 } | 1809 } |
1300 | 1810 |
1301 func newValue(typ Type, addr addr, flag uint32) Value { | 1811 func newValue(typ Type, addr addr, flag uint32) Value { |
1302 v := value{typ, addr, flag} | 1812 v := value{typ, addr, flag} |
1303 » switch typ.(type) { | 1813 » switch typ.Kind() { |
1304 » case *ArrayType: | 1814 » case Array: |
1305 » » return &ArrayValue{v} | 1815 » » return Value{&arrayValue{v}} |
1306 » case *BoolType: | 1816 » case Bool: |
1307 » » return &BoolValue{v} | 1817 » » return Value{&boolValue{v}} |
1308 » case *ChanType: | 1818 » case Chan: |
1309 » » return &ChanValue{v} | 1819 » » return Value{&chanValue{v}} |
1310 » case *FloatType: | 1820 » case Float32, Float64: |
1311 » » return &FloatValue{v} | 1821 » » return Value{&floatValue{v}} |
1312 » case *FuncType: | 1822 » case Func: |
1313 » » return &FuncValue{value: v} | 1823 » » return Value{&funcValue{value: v}} |
1314 » case *ComplexType: | 1824 » case Complex64, Complex128: |
1315 » » return &ComplexValue{v} | 1825 » » return Value{&complexValue{v}} |
1316 » case *IntType: | 1826 » case Int, Int8, Int16, Int32, Int64: |
1317 » » return &IntValue{v} | 1827 » » return Value{&intValue{v}} |
1318 » case *InterfaceType: | 1828 » case Interface: |
1319 » » return &InterfaceValue{v} | 1829 » » return Value{&interfaceValue{v}} |
1320 » case *MapType: | 1830 » case Map: |
1321 » » return &MapValue{v} | 1831 » » return Value{&mapValue{v}} |
1322 » case *PtrType: | 1832 » case Ptr: |
1323 » » return &PtrValue{v} | 1833 » » return Value{&ptrValue{v}} |
1324 » case *SliceType: | 1834 » case Slice: |
1325 » » return &SliceValue{v} | 1835 » » return Value{&sliceValue{v}} |
1326 » case *StringType: | 1836 » case String: |
1327 » » return &StringValue{v} | 1837 » » return Value{&stringValue{v}} |
1328 » case *StructType: | 1838 » case Struct: |
1329 » » return &StructValue{v} | 1839 » » return Value{&structValue{v}} |
1330 » case *UintType: | 1840 » case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr: |
1331 » » return &UintValue{v} | 1841 » » return Value{&uintValue{v}} |
1332 » case *UnsafePointerType: | 1842 » case UnsafePointer: |
1333 » » return &UnsafePointerValue{v} | 1843 » » return Value{&unsafePointerValue{v}} |
1334 } | 1844 } |
1335 panic("newValue" + typ.String()) | 1845 panic("newValue" + typ.String()) |
1336 } | 1846 } |
1337 | 1847 |
1338 // MakeZero returns a zero Value for the specified Type. | 1848 // Zero returns a Value representing a zero value for the specified type. |
1339 func MakeZero(typ Type) Value { | 1849 // The result is different from the zero value of the Value struct, |
| 1850 // which represents no value at all. |
| 1851 // For example, Zero(Typeof(42)) returns a Value with Kind Int and value 0. |
| 1852 func Zero(typ Type) Value { |
1340 if typ == nil { | 1853 if typ == nil { |
1341 » » return nil | 1854 » » panic("reflect: Zero(nil)") |
1342 } | 1855 } |
1343 return newValue(typ, addr(unsafe.New(typ)), canSet|canAddr|canStore) | 1856 return newValue(typ, addr(unsafe.New(typ)), canSet|canAddr|canStore) |
1344 } | 1857 } |
OLD | NEW |