121 lines
3.9 KiB
Go
121 lines
3.9 KiB
Go
|
|
/*
|
||
|
|
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||
|
|
*
|
||
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
|
* you may not use this file except in compliance with the License.
|
||
|
|
* You may obtain a copy of the License at
|
||
|
|
*
|
||
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
*
|
||
|
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
|
* See the License for the specific language governing permissions and
|
||
|
|
* limitations under the License.
|
||
|
|
*/
|
||
|
|
|
||
|
|
package reflect
|
||
|
|
|
||
|
|
import (
|
||
|
|
"unsafe"
|
||
|
|
|
||
|
|
"github.com/goplus/llgo/internal/abi"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Value is the reflection interface to a Go value.
|
||
|
|
//
|
||
|
|
// Not all methods apply to all kinds of values. Restrictions,
|
||
|
|
// if any, are noted in the documentation for each method.
|
||
|
|
// Use the Kind method to find out the kind of value before
|
||
|
|
// calling kind-specific methods. Calling a method
|
||
|
|
// inappropriate to the kind of type causes a run time panic.
|
||
|
|
//
|
||
|
|
// The zero Value represents no value.
|
||
|
|
// Its IsValid method returns false, its Kind method returns Invalid,
|
||
|
|
// its String method returns "<invalid Value>", and all other methods panic.
|
||
|
|
// Most functions and methods never return an invalid value.
|
||
|
|
// If one does, its documentation states the conditions explicitly.
|
||
|
|
//
|
||
|
|
// A Value can be used concurrently by multiple goroutines provided that
|
||
|
|
// the underlying Go value can be used concurrently for the equivalent
|
||
|
|
// direct operations.
|
||
|
|
//
|
||
|
|
// To compare two Values, compare the results of the Interface method.
|
||
|
|
// Using == on two Values does not compare the underlying values
|
||
|
|
// they represent.
|
||
|
|
type Value struct {
|
||
|
|
// typ_ holds the type of the value represented by a Value.
|
||
|
|
// Access using the typ method to avoid escape of v.
|
||
|
|
typ_ *abi.Type
|
||
|
|
|
||
|
|
// Pointer-valued data or, if flagIndir is set, pointer to data.
|
||
|
|
// Valid when either flagIndir is set or typ.pointers() is true.
|
||
|
|
ptr unsafe.Pointer
|
||
|
|
|
||
|
|
// flag holds metadata about the value.
|
||
|
|
//
|
||
|
|
// The lowest five bits give the Kind of the value, mirroring typ.Kind().
|
||
|
|
//
|
||
|
|
// The next set of bits are flag bits:
|
||
|
|
// - flagStickyRO: obtained via unexported not embedded field, so read-only
|
||
|
|
// - flagEmbedRO: obtained via unexported embedded field, so read-only
|
||
|
|
// - flagIndir: val holds a pointer to the data
|
||
|
|
// - flagAddr: v.CanAddr is true (implies flagIndir and ptr is non-nil)
|
||
|
|
// - flagMethod: v is a method value.
|
||
|
|
// If ifaceIndir(typ), code can assume that flagIndir is set.
|
||
|
|
//
|
||
|
|
// The remaining 22+ bits give a method number for method values.
|
||
|
|
// If flag.kind() != Func, code can assume that flagMethod is unset.
|
||
|
|
flag
|
||
|
|
|
||
|
|
// A method value represents a curried method invocation
|
||
|
|
// like r.Read for some receiver r. The typ+val+flag bits describe
|
||
|
|
// the receiver r, but the flag's Kind bits say Func (methods are
|
||
|
|
// functions), and the top bits of the flag give the method number
|
||
|
|
// in r's type's method table.
|
||
|
|
}
|
||
|
|
|
||
|
|
type flag uintptr
|
||
|
|
|
||
|
|
const (
|
||
|
|
flagKindWidth = 5 // there are 27 kinds
|
||
|
|
flagKindMask flag = 1<<flagKindWidth - 1
|
||
|
|
flagStickyRO flag = 1 << 5
|
||
|
|
flagEmbedRO flag = 1 << 6
|
||
|
|
flagIndir flag = 1 << 7
|
||
|
|
flagAddr flag = 1 << 8
|
||
|
|
flagMethod flag = 1 << 9
|
||
|
|
flagMethodShift = 10
|
||
|
|
flagRO flag = flagStickyRO | flagEmbedRO
|
||
|
|
)
|
||
|
|
|
||
|
|
func (f flag) kind() Kind {
|
||
|
|
return Kind(f & flagKindMask)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (f flag) ro() flag {
|
||
|
|
if f&flagRO != 0 {
|
||
|
|
return flagStickyRO
|
||
|
|
}
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
// emptyInterface is the header for an interface{} value.
|
||
|
|
type emptyInterface struct {
|
||
|
|
typ *abi.Type
|
||
|
|
word unsafe.Pointer
|
||
|
|
}
|
||
|
|
|
||
|
|
// nonEmptyInterface is the header for an interface value with methods.
|
||
|
|
type nonEmptyInterface struct {
|
||
|
|
// see ../runtime/iface.go:/Itab
|
||
|
|
itab *struct {
|
||
|
|
ityp *abi.Type // static interface type
|
||
|
|
typ *abi.Type // dynamic concrete type
|
||
|
|
hash uint32 // copy of typ.hash
|
||
|
|
_ [4]byte
|
||
|
|
fun [100000]unsafe.Pointer // method table
|
||
|
|
}
|
||
|
|
word unsafe.Pointer
|
||
|
|
}
|