compiler: build separation runtime with clite
This commit is contained in:
31
runtime/internal/lib/internal/abi/abi.go
Normal file
31
runtime/internal/lib/internal/abi/abi.go
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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 abi
|
||||
|
||||
// llgo:skipall
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/runtime/abi"
|
||||
)
|
||||
|
||||
type InterfaceType = abi.InterfaceType
|
||||
|
||||
func NoEscape(p unsafe.Pointer) unsafe.Pointer {
|
||||
x := uintptr(p)
|
||||
return unsafe.Pointer(x ^ 0)
|
||||
}
|
||||
148
runtime/internal/lib/internal/bytealg/bytealg.go
Normal file
148
runtime/internal/lib/internal/bytealg/bytealg.go
Normal file
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* 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 bytealg
|
||||
|
||||
// llgo:skip init CompareString
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
c "github.com/goplus/llgo/runtime/internal/clite"
|
||||
"github.com/goplus/llgo/runtime/internal/runtime"
|
||||
)
|
||||
|
||||
func IndexByte(b []byte, ch byte) int {
|
||||
ptr := unsafe.Pointer(unsafe.SliceData(b))
|
||||
ret := c.Memchr(ptr, c.Int(ch), uintptr(len(b)))
|
||||
if ret != nil {
|
||||
return int(uintptr(ret) - uintptr(ptr))
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func IndexByteString(s string, ch byte) int {
|
||||
ptr := unsafe.Pointer(unsafe.StringData(s))
|
||||
ret := c.Memchr(ptr, c.Int(ch), uintptr(len(s)))
|
||||
if ret != nil {
|
||||
return int(uintptr(ret) - uintptr(ptr))
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func Count(b []byte, c byte) (n int) {
|
||||
for _, x := range b {
|
||||
if x == c {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func CountString(s string, c byte) (n int) {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == c {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Index returns the index of the first instance of b in a, or -1 if b is not present in a.
|
||||
// Requires 2 <= len(b) <= MaxLen.
|
||||
func Index(a, b []byte) int {
|
||||
for i := 0; i <= len(a)-len(b); i++ {
|
||||
if equal(a[i:i+len(b)], b) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func equal(a, b []byte) bool {
|
||||
if n := len(a); n == len(b) {
|
||||
return c.Memcmp(unsafe.Pointer(unsafe.SliceData(a)), unsafe.Pointer(unsafe.SliceData(b)), uintptr(n)) == 0
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IndexString returns the index of the first instance of b in a, or -1 if b is not present in a.
|
||||
// Requires 2 <= len(b) <= MaxLen.
|
||||
func IndexString(a, b string) int {
|
||||
for i := 0; i <= len(a)-len(b); i++ {
|
||||
if a[i:i+len(b)] == b {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// MakeNoZero makes a slice of length and capacity n without zeroing the bytes.
|
||||
// It is the caller's responsibility to ensure uninitialized bytes
|
||||
// do not leak to the end user.
|
||||
func MakeNoZero(n int) (r []byte) {
|
||||
s := (*sliceHead)(unsafe.Pointer(&r))
|
||||
s.data = runtime.AllocU(uintptr(n))
|
||||
s.len = n
|
||||
s.cap = n
|
||||
return
|
||||
}
|
||||
|
||||
type sliceHead struct {
|
||||
data unsafe.Pointer
|
||||
len int
|
||||
cap int
|
||||
}
|
||||
|
||||
func LastIndexByte(s []byte, c byte) int {
|
||||
for i := len(s) - 1; i >= 0; i-- {
|
||||
if s[i] == c {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func LastIndexByteString(s string, c byte) int {
|
||||
for i := len(s) - 1; i >= 0; i-- {
|
||||
if s[i] == c {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func CompareString(a, b string) int {
|
||||
l := len(a)
|
||||
if len(b) < l {
|
||||
l = len(b)
|
||||
}
|
||||
for i := 0; i < l; i++ {
|
||||
c1, c2 := a[i], b[i]
|
||||
if c1 < c2 {
|
||||
return -1
|
||||
}
|
||||
if c1 > c2 {
|
||||
return +1
|
||||
}
|
||||
}
|
||||
if len(a) < len(b) {
|
||||
return -1
|
||||
}
|
||||
if len(a) > len(b) {
|
||||
return +1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package filepathlite
|
||||
220
runtime/internal/lib/internal/fmtsort/sort.go
Normal file
220
runtime/internal/lib/internal/fmtsort/sort.go
Normal file
@@ -0,0 +1,220 @@
|
||||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package fmtsort provides a general stable ordering mechanism
|
||||
// for maps, on behalf of the fmt and text/template packages.
|
||||
// It is not guaranteed to be efficient and works only for types
|
||||
// that are valid map keys.
|
||||
package fmtsort
|
||||
|
||||
// llgo:skipall
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Note: Throughout this package we avoid calling reflect.Value.Interface as
|
||||
// it is not always legal to do so and it's easier to avoid the issue than to face it.
|
||||
|
||||
// SortedMap represents a map's keys and values. The keys and values are
|
||||
// aligned in index order: Value[i] is the value in the map corresponding to Key[i].
|
||||
type SortedMap struct {
|
||||
Key []reflect.Value
|
||||
Value []reflect.Value
|
||||
}
|
||||
|
||||
func (o *SortedMap) Len() int { return len(o.Key) }
|
||||
func (o *SortedMap) Less(i, j int) bool { return compare(o.Key[i], o.Key[j]) < 0 }
|
||||
func (o *SortedMap) Swap(i, j int) {
|
||||
o.Key[i], o.Key[j] = o.Key[j], o.Key[i]
|
||||
o.Value[i], o.Value[j] = o.Value[j], o.Value[i]
|
||||
}
|
||||
|
||||
// Sort accepts a map and returns a SortedMap that has the same keys and
|
||||
// values but in a stable sorted order according to the keys, modulo issues
|
||||
// raised by unorderable key values such as NaNs.
|
||||
//
|
||||
// The ordering rules are more general than with Go's < operator:
|
||||
//
|
||||
// - when applicable, nil compares low
|
||||
// - ints, floats, and strings order by <
|
||||
// - NaN compares less than non-NaN floats
|
||||
// - bool compares false before true
|
||||
// - complex compares real, then imag
|
||||
// - pointers compare by machine address
|
||||
// - channel values compare by machine address
|
||||
// - structs compare each field in turn
|
||||
// - arrays compare each element in turn.
|
||||
// Otherwise identical arrays compare by length.
|
||||
// - interface values compare first by reflect.Type describing the concrete type
|
||||
// and then by concrete value as described in the previous rules.
|
||||
func Sort(mapValue reflect.Value) *SortedMap {
|
||||
if mapValue.Type().Kind() != reflect.Map {
|
||||
return nil
|
||||
}
|
||||
// Note: this code is arranged to not panic even in the presence
|
||||
// of a concurrent map update. The runtime is responsible for
|
||||
// yelling loudly if that happens. See issue 33275.
|
||||
n := mapValue.Len()
|
||||
key := make([]reflect.Value, 0, n)
|
||||
value := make([]reflect.Value, 0, n)
|
||||
iter := mapValue.MapRange()
|
||||
for iter.Next() {
|
||||
key = append(key, iter.Key())
|
||||
value = append(value, iter.Value())
|
||||
}
|
||||
sorted := &SortedMap{
|
||||
Key: key,
|
||||
Value: value,
|
||||
}
|
||||
sort.Stable(sorted)
|
||||
return sorted
|
||||
}
|
||||
|
||||
// compare compares two values of the same type. It returns -1, 0, 1
|
||||
// according to whether a > b (1), a == b (0), or a < b (-1).
|
||||
// If the types differ, it returns -1.
|
||||
// See the comment on Sort for the comparison rules.
|
||||
func compare(aVal, bVal reflect.Value) int {
|
||||
aType, bType := aVal.Type(), bVal.Type()
|
||||
if aType != bType {
|
||||
return -1 // No good answer possible, but don't return 0: they're not equal.
|
||||
}
|
||||
switch aVal.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
a, b := aVal.Int(), bVal.Int()
|
||||
switch {
|
||||
case a < b:
|
||||
return -1
|
||||
case a > b:
|
||||
return 1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
a, b := aVal.Uint(), bVal.Uint()
|
||||
switch {
|
||||
case a < b:
|
||||
return -1
|
||||
case a > b:
|
||||
return 1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
case reflect.String:
|
||||
a, b := aVal.String(), bVal.String()
|
||||
switch {
|
||||
case a < b:
|
||||
return -1
|
||||
case a > b:
|
||||
return 1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return floatCompare(aVal.Float(), bVal.Float())
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
a, b := aVal.Complex(), bVal.Complex()
|
||||
if c := floatCompare(real(a), real(b)); c != 0 {
|
||||
return c
|
||||
}
|
||||
return floatCompare(imag(a), imag(b))
|
||||
case reflect.Bool:
|
||||
a, b := aVal.Bool(), bVal.Bool()
|
||||
switch {
|
||||
case a == b:
|
||||
return 0
|
||||
case a:
|
||||
return 1
|
||||
default:
|
||||
return -1
|
||||
}
|
||||
case reflect.Pointer, reflect.UnsafePointer:
|
||||
a, b := aVal.Pointer(), bVal.Pointer()
|
||||
switch {
|
||||
case a < b:
|
||||
return -1
|
||||
case a > b:
|
||||
return 1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
case reflect.Chan:
|
||||
if c, ok := nilCompare(aVal, bVal); ok {
|
||||
return c
|
||||
}
|
||||
ap, bp := aVal.Pointer(), bVal.Pointer()
|
||||
switch {
|
||||
case ap < bp:
|
||||
return -1
|
||||
case ap > bp:
|
||||
return 1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
case reflect.Struct:
|
||||
for i := 0; i < aVal.NumField(); i++ {
|
||||
if c := compare(aVal.Field(i), bVal.Field(i)); c != 0 {
|
||||
return c
|
||||
}
|
||||
}
|
||||
return 0
|
||||
case reflect.Array:
|
||||
for i := 0; i < aVal.Len(); i++ {
|
||||
if c := compare(aVal.Index(i), bVal.Index(i)); c != 0 {
|
||||
return c
|
||||
}
|
||||
}
|
||||
return 0
|
||||
case reflect.Interface:
|
||||
if c, ok := nilCompare(aVal, bVal); ok {
|
||||
return c
|
||||
}
|
||||
c := compare(reflect.ValueOf(aVal.Elem().Type()), reflect.ValueOf(bVal.Elem().Type()))
|
||||
if c != 0 {
|
||||
return c
|
||||
}
|
||||
return compare(aVal.Elem(), bVal.Elem())
|
||||
default:
|
||||
// Certain types cannot appear as keys (maps, funcs, slices), but be explicit.
|
||||
panic("bad type in compare: " + aType.String())
|
||||
}
|
||||
}
|
||||
|
||||
// nilCompare checks whether either value is nil. If not, the boolean is false.
|
||||
// If either value is nil, the boolean is true and the integer is the comparison
|
||||
// value. The comparison is defined to be 0 if both are nil, otherwise the one
|
||||
// nil value compares low. Both arguments must represent a chan, func,
|
||||
// interface, map, pointer, or slice.
|
||||
func nilCompare(aVal, bVal reflect.Value) (int, bool) {
|
||||
if aVal.IsNil() {
|
||||
if bVal.IsNil() {
|
||||
return 0, true
|
||||
}
|
||||
return -1, true
|
||||
}
|
||||
if bVal.IsNil() {
|
||||
return 1, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// floatCompare compares two floating-point values. NaNs compare low.
|
||||
func floatCompare(a, b float64) int {
|
||||
switch {
|
||||
case isNaN(a):
|
||||
return -1 // No good answer if b is a NaN so don't bother checking.
|
||||
case isNaN(b):
|
||||
return 1
|
||||
case a < b:
|
||||
return -1
|
||||
case a > b:
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func isNaN(a float64) bool {
|
||||
return a != a
|
||||
}
|
||||
34
runtime/internal/lib/internal/itoa/itoa.go
Normal file
34
runtime/internal/lib/internal/itoa/itoa.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Simple conversions to avoid depending on strconv.
|
||||
|
||||
// llgo:skipall
|
||||
package itoa
|
||||
|
||||
// Itoa converts val to a decimal string.
|
||||
func Itoa(val int) string {
|
||||
if val < 0 {
|
||||
return "-" + Uitoa(uint(-val))
|
||||
}
|
||||
return Uitoa(uint(val))
|
||||
}
|
||||
|
||||
// Uitoa converts val to a decimal string.
|
||||
func Uitoa(val uint) string {
|
||||
if val == 0 { // avoid string allocation
|
||||
return "0"
|
||||
}
|
||||
var buf [20]byte // big enough for 64bit value base 10
|
||||
i := len(buf) - 1
|
||||
for val >= 10 {
|
||||
q := val / 10
|
||||
buf[i] = byte('0' + val - q*10)
|
||||
i--
|
||||
val = q
|
||||
}
|
||||
// val < 10
|
||||
buf[i] = byte('0' + val)
|
||||
return string(buf[i:])
|
||||
}
|
||||
19
runtime/internal/lib/internal/oserror/errors.go
Normal file
19
runtime/internal/lib/internal/oserror/errors.go
Normal file
@@ -0,0 +1,19 @@
|
||||
// Copyright 2019 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package oserror defines errors values used in the os package.
|
||||
//
|
||||
// These types are defined here to permit the syscall package to reference them.
|
||||
package oserror
|
||||
|
||||
// llgo:skipall
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrInvalid = errors.New("invalid argument")
|
||||
ErrPermission = errors.New("permission denied")
|
||||
ErrExist = errors.New("file already exists")
|
||||
ErrNotExist = errors.New("file does not exist")
|
||||
ErrClosed = errors.New("file already closed")
|
||||
)
|
||||
1
runtime/internal/lib/internal/race/race.go
Normal file
1
runtime/internal/lib/internal/race/race.go
Normal file
@@ -0,0 +1 @@
|
||||
package race
|
||||
22
runtime/internal/lib/internal/reflectlite/reflectlite.go
Normal file
22
runtime/internal/lib/internal/reflectlite/reflectlite.go
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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 reflectlite
|
||||
|
||||
// llgo:skipall
|
||||
import (
|
||||
_ "unsafe"
|
||||
)
|
||||
80
runtime/internal/lib/internal/reflectlite/swapper.go
Normal file
80
runtime/internal/lib/internal/reflectlite/swapper.go
Normal file
@@ -0,0 +1,80 @@
|
||||
// Copyright 2016 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package reflectlite
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
goarchPtrSize = unsafe.Sizeof(uintptr(0))
|
||||
)
|
||||
|
||||
// Swapper returns a function that swaps the elements in the provided
|
||||
// slice.
|
||||
//
|
||||
// Swapper panics if the provided interface is not a slice.
|
||||
func Swapper(slice any) func(i, j int) {
|
||||
v := ValueOf(slice)
|
||||
if v.Kind() != Slice {
|
||||
panic(&ValueError{Method: "Swapper", Kind: v.Kind()})
|
||||
}
|
||||
// Fast path for slices of size 0 and 1. Nothing to swap.
|
||||
switch v.Len() {
|
||||
case 0:
|
||||
return func(i, j int) { panic("reflect: slice index out of range") }
|
||||
case 1:
|
||||
return func(i, j int) {
|
||||
if i != 0 || j != 0 {
|
||||
panic("reflect: slice index out of range")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typ := v.Type().Elem().common()
|
||||
size := typ.Size()
|
||||
hasPtr := typ.PtrBytes != 0
|
||||
|
||||
// Some common & small cases, without using memmove:
|
||||
if hasPtr {
|
||||
if size == goarchPtrSize {
|
||||
ps := *(*[]unsafe.Pointer)(v.ptr)
|
||||
return func(i, j int) { ps[i], ps[j] = ps[j], ps[i] }
|
||||
}
|
||||
if typ.Kind() == String {
|
||||
ss := *(*[]string)(v.ptr)
|
||||
return func(i, j int) { ss[i], ss[j] = ss[j], ss[i] }
|
||||
}
|
||||
} else {
|
||||
switch size {
|
||||
case 8:
|
||||
is := *(*[]int64)(v.ptr)
|
||||
return func(i, j int) { is[i], is[j] = is[j], is[i] }
|
||||
case 4:
|
||||
is := *(*[]int32)(v.ptr)
|
||||
return func(i, j int) { is[i], is[j] = is[j], is[i] }
|
||||
case 2:
|
||||
is := *(*[]int16)(v.ptr)
|
||||
return func(i, j int) { is[i], is[j] = is[j], is[i] }
|
||||
case 1:
|
||||
is := *(*[]int8)(v.ptr)
|
||||
return func(i, j int) { is[i], is[j] = is[j], is[i] }
|
||||
}
|
||||
}
|
||||
|
||||
s := (*unsafeheaderSlice)(v.ptr)
|
||||
tmp := unsafe_New(typ) // swap scratch space
|
||||
|
||||
return func(i, j int) {
|
||||
if uint(i) >= uint(s.Len) || uint(j) >= uint(s.Len) {
|
||||
panic("reflect: slice index out of range")
|
||||
}
|
||||
val1 := arrayAt(s.Data, i, size, "i < s.Len")
|
||||
val2 := arrayAt(s.Data, j, size, "j < s.Len")
|
||||
typedmemmove(typ, tmp, val1)
|
||||
typedmemmove(typ, val1, val2)
|
||||
typedmemmove(typ, val2, tmp)
|
||||
}
|
||||
}
|
||||
565
runtime/internal/lib/internal/reflectlite/type.go
Normal file
565
runtime/internal/lib/internal/reflectlite/type.go
Normal file
@@ -0,0 +1,565 @@
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package reflectlite implements lightweight version of reflect, not using
|
||||
// any package except for "runtime", "unsafe", and "internal/abi"
|
||||
package reflectlite
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/runtime/abi"
|
||||
)
|
||||
|
||||
// Type is the representation of a Go type.
|
||||
//
|
||||
// Not all methods apply to all kinds of types. Restrictions,
|
||||
// if any, are noted in the documentation for each method.
|
||||
// Use the Kind method to find out the kind of type before
|
||||
// calling kind-specific methods. Calling a method
|
||||
// inappropriate to the kind of type causes a run-time panic.
|
||||
//
|
||||
// Type values are comparable, such as with the == operator,
|
||||
// so they can be used as map keys.
|
||||
// Two Type values are equal if they represent identical types.
|
||||
type Type interface {
|
||||
// Methods applicable to all types.
|
||||
|
||||
// Name returns the type's name within its package for a defined type.
|
||||
// For other (non-defined) types it returns the empty string.
|
||||
Name() string
|
||||
|
||||
// PkgPath returns a defined type's package path, that is, the import path
|
||||
// that uniquely identifies the package, such as "encoding/base64".
|
||||
// If the type was predeclared (string, error) or not defined (*T, struct{},
|
||||
// []int, or A where A is an alias for a non-defined type), the package path
|
||||
// will be the empty string.
|
||||
PkgPath() string
|
||||
|
||||
// Size returns the number of bytes needed to store
|
||||
// a value of the given type; it is analogous to unsafe.Sizeof.
|
||||
Size() uintptr
|
||||
|
||||
// Kind returns the specific kind of this type.
|
||||
Kind() Kind
|
||||
|
||||
// Implements reports whether the type implements the interface type u.
|
||||
Implements(u Type) bool
|
||||
|
||||
// AssignableTo reports whether a value of the type is assignable to type u.
|
||||
AssignableTo(u Type) bool
|
||||
|
||||
// Comparable reports whether values of this type are comparable.
|
||||
Comparable() bool
|
||||
|
||||
// String returns a string representation of the type.
|
||||
// The string representation may use shortened package names
|
||||
// (e.g., base64 instead of "encoding/base64") and is not
|
||||
// guaranteed to be unique among types. To test for type identity,
|
||||
// compare the Types directly.
|
||||
String() string
|
||||
|
||||
// Elem returns a type's element type.
|
||||
// It panics if the type's Kind is not Ptr.
|
||||
Elem() Type
|
||||
|
||||
common() *abi.Type
|
||||
uncommon() *uncommonType
|
||||
}
|
||||
|
||||
/*
|
||||
* These data structures are known to the compiler (../../cmd/internal/reflectdata/reflect.go).
|
||||
* A few are known to ../runtime/type.go to convey to debuggers.
|
||||
* They are also known to ../runtime/type.go.
|
||||
*/
|
||||
|
||||
// A Kind represents the specific kind of type that a Type represents.
|
||||
// The zero Kind is not a valid kind.
|
||||
type Kind = abi.Kind
|
||||
|
||||
const Ptr = abi.Pointer
|
||||
|
||||
const (
|
||||
// Import-and-export these constants as necessary
|
||||
Interface = abi.Interface
|
||||
Slice = abi.Slice
|
||||
String = abi.String
|
||||
Struct = abi.Struct
|
||||
)
|
||||
|
||||
type rtype struct {
|
||||
*abi.Type
|
||||
}
|
||||
|
||||
// uncommonType is present only for defined types or types with methods
|
||||
// (if T is a defined type, the uncommonTypes for T and *T have methods).
|
||||
// Using a pointer to this struct reduces the overall size required
|
||||
// to describe a non-defined type with no methods.
|
||||
type uncommonType = abi.UncommonType
|
||||
|
||||
// arrayType represents a fixed array type.
|
||||
type arrayType = abi.ArrayType
|
||||
|
||||
// chanType represents a channel type.
|
||||
type chanType = abi.ChanType
|
||||
|
||||
type funcType = abi.FuncType
|
||||
|
||||
type interfaceType = abi.InterfaceType
|
||||
|
||||
// mapType represents a map type.
|
||||
type mapType struct {
|
||||
rtype
|
||||
Key *abi.Type // map key type
|
||||
Elem *abi.Type // map element (value) type
|
||||
Bucket *abi.Type // internal bucket structure
|
||||
// function for hashing keys (ptr to key, seed) -> hash
|
||||
Hasher func(unsafe.Pointer, uintptr) uintptr
|
||||
KeySize uint8 // size of key slot
|
||||
ValueSize uint8 // size of value slot
|
||||
BucketSize uint16 // size of bucket
|
||||
Flags uint32
|
||||
}
|
||||
|
||||
// ptrType represents a pointer type.
|
||||
type ptrType = abi.PtrType
|
||||
|
||||
// sliceType represents a slice type.
|
||||
type sliceType = abi.SliceType
|
||||
|
||||
// structType represents a struct type.
|
||||
type structType = abi.StructType
|
||||
|
||||
func (t rtype) uncommon() *uncommonType {
|
||||
return t.Uncommon()
|
||||
}
|
||||
|
||||
func (t rtype) String() string {
|
||||
return t.Type.String()
|
||||
}
|
||||
|
||||
func (t rtype) common() *abi.Type { return t.Type }
|
||||
|
||||
func (t rtype) exportedMethods() []abi.Method {
|
||||
ut := t.uncommon()
|
||||
if ut == nil {
|
||||
return nil
|
||||
}
|
||||
return ut.ExportedMethods()
|
||||
}
|
||||
|
||||
func (t rtype) NumMethod() int {
|
||||
/*
|
||||
tt := t.Type.InterfaceType()
|
||||
if tt != nil {
|
||||
return tt.NumMethod()
|
||||
}
|
||||
return len(t.exportedMethods())
|
||||
*/
|
||||
panic("todo: reflectlite.rtype.NumMethod")
|
||||
}
|
||||
|
||||
func (t rtype) PkgPath() string {
|
||||
/*
|
||||
if t.TFlag&abi.TFlagNamed == 0 {
|
||||
return ""
|
||||
}
|
||||
ut := t.uncommon()
|
||||
if ut == nil {
|
||||
return ""
|
||||
}
|
||||
return t.nameOff(ut.PkgPath).Name()
|
||||
*/
|
||||
panic("todo: reflectlite.rtype.PkgPath")
|
||||
}
|
||||
|
||||
func (t rtype) Name() string {
|
||||
/*
|
||||
if !t.HasName() {
|
||||
return ""
|
||||
}
|
||||
s := t.String()
|
||||
i := len(s) - 1
|
||||
sqBrackets := 0
|
||||
for i >= 0 && (s[i] != '.' || sqBrackets != 0) {
|
||||
switch s[i] {
|
||||
case ']':
|
||||
sqBrackets++
|
||||
case '[':
|
||||
sqBrackets--
|
||||
}
|
||||
i--
|
||||
}
|
||||
return s[i+1:]
|
||||
*/
|
||||
panic("todo: reflectlite.rtype.Name")
|
||||
}
|
||||
|
||||
func toRType(t *abi.Type) rtype {
|
||||
return rtype{t}
|
||||
}
|
||||
|
||||
func elem(t *abi.Type) *abi.Type {
|
||||
et := t.Elem()
|
||||
if et != nil {
|
||||
return et
|
||||
}
|
||||
panic("reflect: Elem of invalid type " + toRType(t).String())
|
||||
}
|
||||
|
||||
func (t rtype) Elem() Type {
|
||||
return toType(elem(t.common()))
|
||||
}
|
||||
|
||||
func (t rtype) In(i int) Type {
|
||||
/*
|
||||
tt := t.Type.FuncType()
|
||||
if tt == nil {
|
||||
panic("reflect: In of non-func type")
|
||||
}
|
||||
return toType(tt.InSlice()[i])
|
||||
*/
|
||||
panic("todo: reflectlite.rtype.In")
|
||||
}
|
||||
|
||||
func (t rtype) Key() Type {
|
||||
tt := t.Type.MapType()
|
||||
if tt == nil {
|
||||
panic("reflect: Key of non-map type")
|
||||
}
|
||||
return toType(tt.Key)
|
||||
}
|
||||
|
||||
func (t rtype) Len() int {
|
||||
tt := t.Type.ArrayType()
|
||||
if tt == nil {
|
||||
panic("reflect: Len of non-array type")
|
||||
}
|
||||
return int(tt.Len)
|
||||
}
|
||||
|
||||
func (t rtype) NumField() int {
|
||||
tt := t.Type.StructType()
|
||||
if tt == nil {
|
||||
panic("reflect: NumField of non-struct type")
|
||||
}
|
||||
return len(tt.Fields)
|
||||
}
|
||||
|
||||
func (t rtype) NumIn() int {
|
||||
/*
|
||||
tt := t.Type.FuncType()
|
||||
if tt == nil {
|
||||
panic("reflect: NumIn of non-func type")
|
||||
}
|
||||
return int(tt.InCount)
|
||||
*/
|
||||
panic("todo: reflectlite.rtype.NumIn")
|
||||
}
|
||||
|
||||
func (t rtype) NumOut() int {
|
||||
/*
|
||||
tt := t.Type.FuncType()
|
||||
if tt == nil {
|
||||
panic("reflect: NumOut of non-func type")
|
||||
}
|
||||
return tt.NumOut()
|
||||
*/
|
||||
panic("todo: reflectlite.rtype.NumOut")
|
||||
}
|
||||
|
||||
func (t rtype) Out(i int) Type {
|
||||
/*
|
||||
tt := t.Type.FuncType()
|
||||
if tt == nil {
|
||||
panic("reflect: Out of non-func type")
|
||||
}
|
||||
return toType(tt.OutSlice()[i])
|
||||
*/
|
||||
panic("todo: reflectlite.rtype.Out")
|
||||
}
|
||||
|
||||
// add returns p+x.
|
||||
//
|
||||
// The whySafe string is ignored, so that the function still inlines
|
||||
// as efficiently as p+x, but all call sites should use the string to
|
||||
// record why the addition is safe, which is to say why the addition
|
||||
// does not cause x to advance to the very end of p's allocation
|
||||
// and therefore point incorrectly at the next block in memory.
|
||||
func add(p unsafe.Pointer, x uintptr, whySafe string) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(p) + x)
|
||||
}
|
||||
|
||||
// TypeOf returns the reflection Type that represents the dynamic type of i.
|
||||
// If i is a nil interface value, TypeOf returns nil.
|
||||
func TypeOf(i any) Type {
|
||||
eface := *(*emptyInterface)(unsafe.Pointer(&i))
|
||||
return toType(eface.typ)
|
||||
}
|
||||
|
||||
func (t rtype) Implements(u Type) bool {
|
||||
if u == nil {
|
||||
panic("reflect: nil type passed to Type.Implements")
|
||||
}
|
||||
if u.Kind() != Interface {
|
||||
panic("reflect: non-interface type passed to Type.Implements")
|
||||
}
|
||||
return implements(u.common(), t.common())
|
||||
}
|
||||
|
||||
func (t rtype) AssignableTo(u Type) bool {
|
||||
if u == nil {
|
||||
panic("reflect: nil type passed to Type.AssignableTo")
|
||||
}
|
||||
uu := u.common()
|
||||
tt := t.common()
|
||||
return directlyAssignable(uu, tt) || implements(uu, tt)
|
||||
}
|
||||
|
||||
func (t rtype) Comparable() bool {
|
||||
return t.Equal != nil
|
||||
}
|
||||
|
||||
// implements reports whether the type V implements the interface type T.
|
||||
func implements(T, V *abi.Type) bool {
|
||||
/*
|
||||
t := T.InterfaceType()
|
||||
if t == nil {
|
||||
return false
|
||||
}
|
||||
if len(t.Methods) == 0 {
|
||||
return true
|
||||
}
|
||||
rT := toRType(T)
|
||||
rV := toRType(V)
|
||||
|
||||
// The same algorithm applies in both cases, but the
|
||||
// method tables for an interface type and a concrete type
|
||||
// are different, so the code is duplicated.
|
||||
// In both cases the algorithm is a linear scan over the two
|
||||
// lists - T's methods and V's methods - simultaneously.
|
||||
// Since method tables are stored in a unique sorted order
|
||||
// (alphabetical, with no duplicate method names), the scan
|
||||
// through V's methods must hit a match for each of T's
|
||||
// methods along the way, or else V does not implement T.
|
||||
// This lets us run the scan in overall linear time instead of
|
||||
// the quadratic time a naive search would require.
|
||||
// See also ../runtime/iface.go.
|
||||
if V.Kind() == Interface {
|
||||
v := (*interfaceType)(unsafe.Pointer(V))
|
||||
i := 0
|
||||
for j := 0; j < len(v.Methods); j++ {
|
||||
tm := &t.Methods[i]
|
||||
tmName := rT.nameOff(tm.Name)
|
||||
vm := &v.Methods[j]
|
||||
vmName := rV.nameOff(vm.Name)
|
||||
if vmName.Name() == tmName.Name() && rV.typeOff(vm.Typ) == rT.typeOff(tm.Typ) {
|
||||
if !tmName.IsExported() {
|
||||
tmPkgPath := pkgPath(tmName)
|
||||
if tmPkgPath == "" {
|
||||
tmPkgPath = t.PkgPath.Name()
|
||||
}
|
||||
vmPkgPath := pkgPath(vmName)
|
||||
if vmPkgPath == "" {
|
||||
vmPkgPath = v.PkgPath.Name()
|
||||
}
|
||||
if tmPkgPath != vmPkgPath {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if i++; i >= len(t.Methods) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
v := V.Uncommon()
|
||||
if v == nil {
|
||||
return false
|
||||
}
|
||||
i := 0
|
||||
vmethods := v.Methods()
|
||||
for j := 0; j < int(v.Mcount); j++ {
|
||||
tm := &t.Methods[i]
|
||||
tmName := rT.nameOff(tm.Name)
|
||||
vm := vmethods[j]
|
||||
vmName := rV.nameOff(vm.Name)
|
||||
if vmName.Name() == tmName.Name() && rV.typeOff(vm.Mtyp) == rT.typeOff(tm.Typ) {
|
||||
if !tmName.IsExported() {
|
||||
tmPkgPath := pkgPath(tmName)
|
||||
if tmPkgPath == "" {
|
||||
tmPkgPath = t.PkgPath.Name()
|
||||
}
|
||||
vmPkgPath := pkgPath(vmName)
|
||||
if vmPkgPath == "" {
|
||||
vmPkgPath = rV.nameOff(v.PkgPath).Name()
|
||||
}
|
||||
if tmPkgPath != vmPkgPath {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if i++; i >= len(t.Methods) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
*/
|
||||
panic("todo: reflectlite.implements")
|
||||
}
|
||||
|
||||
// directlyAssignable reports whether a value x of type V can be directly
|
||||
// assigned (using memmove) to a value of type T.
|
||||
// https://golang.org/doc/go_spec.html#Assignability
|
||||
// Ignoring the interface rules (implemented elsewhere)
|
||||
// and the ideal constant rules (no ideal constants at run time).
|
||||
func directlyAssignable(T, V *abi.Type) bool {
|
||||
// x's type V is identical to T?
|
||||
if T == V {
|
||||
return true
|
||||
}
|
||||
|
||||
// Otherwise at least one of T and V must not be defined
|
||||
// and they must have the same kind.
|
||||
if T.HasName() && V.HasName() || T.Kind() != V.Kind() {
|
||||
return false
|
||||
}
|
||||
|
||||
// x's type T and V must have identical underlying types.
|
||||
return haveIdenticalUnderlyingType(T, V, true)
|
||||
}
|
||||
|
||||
func haveIdenticalType(T, V *abi.Type, cmpTags bool) bool {
|
||||
if cmpTags {
|
||||
return T == V
|
||||
}
|
||||
|
||||
if toRType(T).Name() != toRType(V).Name() || T.Kind() != V.Kind() {
|
||||
return false
|
||||
}
|
||||
|
||||
return haveIdenticalUnderlyingType(T, V, false)
|
||||
}
|
||||
|
||||
func haveIdenticalUnderlyingType(T, V *abi.Type, cmpTags bool) bool {
|
||||
if T == V {
|
||||
return true
|
||||
}
|
||||
|
||||
kind := T.Kind()
|
||||
if kind != V.Kind() {
|
||||
return false
|
||||
}
|
||||
|
||||
// Non-composite types of equal kind have same underlying type
|
||||
// (the predefined instance of the type).
|
||||
if abi.Bool <= kind && kind <= abi.Complex128 || kind == abi.String || kind == abi.UnsafePointer {
|
||||
return true
|
||||
}
|
||||
|
||||
/*
|
||||
// Composite types.
|
||||
switch kind {
|
||||
case abi.Array:
|
||||
return T.Len() == V.Len() && haveIdenticalType(T.Elem(), V.Elem(), cmpTags)
|
||||
|
||||
case abi.Chan:
|
||||
// Special case:
|
||||
// x is a bidirectional channel value, T is a channel type,
|
||||
// and x's type V and T have identical element types.
|
||||
if V.ChanDir() == abi.BothDir && haveIdenticalType(T.Elem(), V.Elem(), cmpTags) {
|
||||
return true
|
||||
}
|
||||
|
||||
// Otherwise continue test for identical underlying type.
|
||||
return V.ChanDir() == T.ChanDir() && haveIdenticalType(T.Elem(), V.Elem(), cmpTags)
|
||||
|
||||
case abi.Func:
|
||||
t := (*funcType)(unsafe.Pointer(T))
|
||||
v := (*funcType)(unsafe.Pointer(V))
|
||||
if t.OutCount != v.OutCount || t.InCount != v.InCount {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < t.NumIn(); i++ {
|
||||
if !haveIdenticalType(t.In(i), v.In(i), cmpTags) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for i := 0; i < t.NumOut(); i++ {
|
||||
if !haveIdenticalType(t.Out(i), v.Out(i), cmpTags) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
|
||||
case Interface:
|
||||
t := (*interfaceType)(unsafe.Pointer(T))
|
||||
v := (*interfaceType)(unsafe.Pointer(V))
|
||||
if len(t.Methods) == 0 && len(v.Methods) == 0 {
|
||||
return true
|
||||
}
|
||||
// Might have the same methods but still
|
||||
// need a run time conversion.
|
||||
return false
|
||||
|
||||
case abi.Map:
|
||||
return haveIdenticalType(T.Key(), V.Key(), cmpTags) && haveIdenticalType(T.Elem(), V.Elem(), cmpTags)
|
||||
|
||||
case Ptr, abi.Slice:
|
||||
return haveIdenticalType(T.Elem(), V.Elem(), cmpTags)
|
||||
|
||||
case abi.Struct:
|
||||
t := (*structType)(unsafe.Pointer(T))
|
||||
v := (*structType)(unsafe.Pointer(V))
|
||||
if len(t.Fields) != len(v.Fields) {
|
||||
return false
|
||||
}
|
||||
if t.PkgPath.Name() != v.PkgPath.Name() {
|
||||
return false
|
||||
}
|
||||
for i := range t.Fields {
|
||||
tf := &t.Fields[i]
|
||||
vf := &v.Fields[i]
|
||||
if tf.Name.Name() != vf.Name.Name() {
|
||||
return false
|
||||
}
|
||||
if !haveIdenticalType(tf.Typ, vf.Typ, cmpTags) {
|
||||
return false
|
||||
}
|
||||
if cmpTags && tf.Name.Tag() != vf.Name.Tag() {
|
||||
return false
|
||||
}
|
||||
if tf.Offset != vf.Offset {
|
||||
return false
|
||||
}
|
||||
if tf.Embedded() != vf.Embedded() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
*/
|
||||
panic("todo: reflectlite.haveIdenticalUnderlyingType")
|
||||
}
|
||||
|
||||
// toType converts from a *rtype to a Type that can be returned
|
||||
// to the client of package reflect. In gc, the only concern is that
|
||||
// a nil *rtype must be replaced by a nil Type, but in gccgo this
|
||||
// function takes care of ensuring that multiple *rtype for the same
|
||||
// type are coalesced into a single Type.
|
||||
func toType(t *abi.Type) Type {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
return toRType(t)
|
||||
}
|
||||
|
||||
// ifaceIndir reports whether t is stored indirectly in an interface value.
|
||||
func ifaceIndir(t *abi.Type) bool {
|
||||
return t.Kind_&abi.KindDirectIface == 0
|
||||
}
|
||||
32
runtime/internal/lib/internal/reflectlite/unsafeheader.go
Normal file
32
runtime/internal/lib/internal/reflectlite/unsafeheader.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// Copyright 2020 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package reflectlite
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// unsafeheaderSlice is the runtime representation of a slice.
|
||||
// It cannot be used safely or portably and its representation may
|
||||
// change in a later release.
|
||||
//
|
||||
// Unlike reflect.SliceHeader, its Data field is sufficient to guarantee the
|
||||
// data it references will not be garbage collected.
|
||||
type unsafeheaderSlice struct {
|
||||
Data unsafe.Pointer
|
||||
Len int
|
||||
Cap int
|
||||
}
|
||||
|
||||
// unsafeheaderString is the runtime representation of a string.
|
||||
// It cannot be used safely or portably and its representation may
|
||||
// change in a later release.
|
||||
//
|
||||
// Unlike reflect.StringHeader, its Data field is sufficient to guarantee the
|
||||
// data it references will not be garbage collected.
|
||||
type unsafeheaderString struct {
|
||||
Data unsafe.Pointer
|
||||
Len int
|
||||
}
|
||||
477
runtime/internal/lib/internal/reflectlite/value.go
Normal file
477
runtime/internal/lib/internal/reflectlite/value.go
Normal file
@@ -0,0 +1,477 @@
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package reflectlite
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/runtime/abi"
|
||||
_ "github.com/goplus/llgo/runtime/internal/runtime"
|
||||
)
|
||||
|
||||
// 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.
|
||||
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 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)
|
||||
// Value cannot represent method values.
|
||||
// The next five bits give the Kind of the value.
|
||||
// This repeats typ.Kind() except for method values.
|
||||
// The remaining 23+ bits give a method number for method values.
|
||||
// If flag.kind() != Func, code can assume that flagMethod is unset.
|
||||
// If ifaceIndir(typ), code can assume that flagIndir is set.
|
||||
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
|
||||
}
|
||||
|
||||
// pointer returns the underlying pointer represented by v.
|
||||
// v.Kind() must be Pointer, Map, Chan, Func, or UnsafePointer
|
||||
func (v Value) pointer() unsafe.Pointer {
|
||||
/*
|
||||
if v.typ.Size() != goarch.PtrSize || !v.typ.Pointers() {
|
||||
panic("can't call pointer on a non-pointer Value")
|
||||
}
|
||||
if v.flag&flagIndir != 0 {
|
||||
return *(*unsafe.Pointer)(v.ptr)
|
||||
}
|
||||
return v.ptr
|
||||
*/
|
||||
panic("todo: reflectlite.Value.pointer")
|
||||
}
|
||||
|
||||
// packEface converts v to the empty interface.
|
||||
func packEface(v Value) any {
|
||||
t := v.typ
|
||||
var i any
|
||||
e := (*emptyInterface)(unsafe.Pointer(&i))
|
||||
// First, fill in the data portion of the interface.
|
||||
switch {
|
||||
case ifaceIndir(t):
|
||||
if v.flag&flagIndir == 0 {
|
||||
panic("bad indir")
|
||||
}
|
||||
// Value is indirect, and so is the interface we're making.
|
||||
ptr := v.ptr
|
||||
if v.flag&flagAddr != 0 {
|
||||
// TODO: pass safe boolean from valueInterface so
|
||||
// we don't need to copy if safe==true?
|
||||
c := unsafe_New(t)
|
||||
typedmemmove(t, c, ptr)
|
||||
ptr = c
|
||||
}
|
||||
e.word = ptr
|
||||
case v.flag&flagIndir != 0:
|
||||
// Value is indirect, but interface is direct. We need
|
||||
// to load the data at v.ptr into the interface data word.
|
||||
e.word = *(*unsafe.Pointer)(v.ptr)
|
||||
default:
|
||||
// Value is direct, and so is the interface.
|
||||
e.word = v.ptr
|
||||
}
|
||||
// Now, fill in the type portion. We're very careful here not
|
||||
// to have any operation between the e.word and e.typ assignments
|
||||
// that would let the garbage collector observe the partially-built
|
||||
// interface value.
|
||||
e.typ = t
|
||||
return i
|
||||
}
|
||||
|
||||
// unpackEface converts the empty interface i to a Value.
|
||||
func unpackEface(i any) Value {
|
||||
e := (*emptyInterface)(unsafe.Pointer(&i))
|
||||
// NOTE: don't read e.word until we know whether it is really a pointer or not.
|
||||
t := e.typ
|
||||
if t == nil {
|
||||
return Value{}
|
||||
}
|
||||
f := flag(t.Kind())
|
||||
if ifaceIndir(t) {
|
||||
f |= flagIndir
|
||||
}
|
||||
return Value{t, e.word, f}
|
||||
}
|
||||
|
||||
// A ValueError occurs when a Value method is invoked on
|
||||
// a Value that does not support it. Such cases are documented
|
||||
// in the description of each method.
|
||||
type ValueError struct {
|
||||
Method string
|
||||
Kind Kind
|
||||
}
|
||||
|
||||
func (e *ValueError) Error() string {
|
||||
if e.Kind == 0 {
|
||||
return "reflect: call of " + e.Method + " on zero Value"
|
||||
}
|
||||
return "reflect: call of " + e.Method + " on " + e.Kind.String() + " Value"
|
||||
}
|
||||
|
||||
// methodName returns the name of the calling method,
|
||||
// assumed to be two stack frames above.
|
||||
func methodName() string {
|
||||
/* TODO(xsw):
|
||||
pc, _, _, _ := runtime.Caller(2)
|
||||
f := runtime.FuncForPC(pc)
|
||||
if f == nil {
|
||||
return "unknown method"
|
||||
}
|
||||
return f.Name()
|
||||
*/
|
||||
return "unknown method"
|
||||
}
|
||||
|
||||
// emptyInterface is the header for an interface{} value.
|
||||
type emptyInterface struct {
|
||||
typ *abi.Type
|
||||
word unsafe.Pointer
|
||||
}
|
||||
|
||||
// mustBeExported panics if f records that the value was obtained using
|
||||
// an unexported field.
|
||||
func (f flag) mustBeExported() {
|
||||
if f == 0 {
|
||||
panic(&ValueError{methodName(), 0})
|
||||
}
|
||||
if f&flagRO != 0 {
|
||||
panic("reflect: " + methodName() + " using value obtained using unexported field")
|
||||
}
|
||||
}
|
||||
|
||||
// mustBeAssignable panics if f records that the value is not assignable,
|
||||
// which is to say that either it was obtained using an unexported field
|
||||
// or it is not addressable.
|
||||
func (f flag) mustBeAssignable() {
|
||||
if f == 0 {
|
||||
panic(&ValueError{methodName(), abi.Invalid})
|
||||
}
|
||||
// Assignable if addressable and not read-only.
|
||||
if f&flagRO != 0 {
|
||||
panic("reflect: " + methodName() + " using value obtained using unexported field")
|
||||
}
|
||||
if f&flagAddr == 0 {
|
||||
panic("reflect: " + methodName() + " using unaddressable value")
|
||||
}
|
||||
}
|
||||
|
||||
// CanSet reports whether the value of v can be changed.
|
||||
// A Value can be changed only if it is addressable and was not
|
||||
// obtained by the use of unexported struct fields.
|
||||
// If CanSet returns false, calling Set or any type-specific
|
||||
// setter (e.g., SetBool, SetInt) will panic.
|
||||
func (v Value) CanSet() bool {
|
||||
return v.flag&(flagAddr|flagRO) == flagAddr
|
||||
}
|
||||
|
||||
// Elem returns the value that the interface v contains
|
||||
// or that the pointer v points to.
|
||||
// It panics if v's Kind is not Interface or Pointer.
|
||||
// It returns the zero Value if v is nil.
|
||||
func (v Value) Elem() Value {
|
||||
/*
|
||||
k := v.kind()
|
||||
switch k {
|
||||
case abi.Interface:
|
||||
var eface any
|
||||
if v.typ.NumMethod() == 0 {
|
||||
eface = *(*any)(v.ptr)
|
||||
} else {
|
||||
eface = (any)(*(*interface {
|
||||
M()
|
||||
})(v.ptr))
|
||||
}
|
||||
x := unpackEface(eface)
|
||||
if x.flag != 0 {
|
||||
x.flag |= v.flag.ro()
|
||||
}
|
||||
return x
|
||||
case abi.Pointer:
|
||||
ptr := v.ptr
|
||||
if v.flag&flagIndir != 0 {
|
||||
ptr = *(*unsafe.Pointer)(ptr)
|
||||
}
|
||||
// The returned value's address is v's value.
|
||||
if ptr == nil {
|
||||
return Value{}
|
||||
}
|
||||
tt := (*ptrType)(unsafe.Pointer(v.typ))
|
||||
typ := tt.Elem
|
||||
fl := v.flag&flagRO | flagIndir | flagAddr
|
||||
fl |= flag(typ.Kind())
|
||||
return Value{typ, ptr, fl}
|
||||
}
|
||||
panic(&ValueError{"reflectlite.Value.Elem", v.kind()})
|
||||
*/
|
||||
panic("todo: reflectlite.Value.Elem")
|
||||
}
|
||||
|
||||
func valueInterface(v Value) any {
|
||||
if v.flag == 0 {
|
||||
panic(&ValueError{"reflectlite.Value.Interface", 0})
|
||||
}
|
||||
|
||||
if v.kind() == abi.Interface {
|
||||
// Special case: return the element inside the interface.
|
||||
// Empty interface has one layout, all interfaces with
|
||||
// methods have a second layout.
|
||||
if v.numMethod() == 0 {
|
||||
return *(*any)(v.ptr)
|
||||
}
|
||||
return *(*interface {
|
||||
M()
|
||||
})(v.ptr)
|
||||
}
|
||||
|
||||
// TODO: pass safe to packEface so we don't need to copy if safe==true?
|
||||
return packEface(v)
|
||||
}
|
||||
|
||||
// IsNil reports whether its argument v is nil. The argument must be
|
||||
// a chan, func, interface, map, pointer, or slice value; if it is
|
||||
// not, IsNil panics. Note that IsNil is not always equivalent to a
|
||||
// regular comparison with nil in Go. For example, if v was created
|
||||
// by calling ValueOf with an uninitialized interface variable i,
|
||||
// i==nil will be true but v.IsNil will panic as v will be the zero
|
||||
// Value.
|
||||
func (v Value) IsNil() bool {
|
||||
k := v.kind()
|
||||
switch k {
|
||||
case abi.Chan, abi.Func, abi.Map, abi.Pointer, abi.UnsafePointer:
|
||||
// if v.flag&flagMethod != 0 {
|
||||
// return false
|
||||
// }
|
||||
ptr := v.ptr
|
||||
if v.flag&flagIndir != 0 {
|
||||
ptr = *(*unsafe.Pointer)(ptr)
|
||||
}
|
||||
return ptr == nil
|
||||
case abi.Interface, abi.Slice:
|
||||
// Both interface and slice are nil if first word is 0.
|
||||
// Both are always bigger than a word; assume flagIndir.
|
||||
return *(*unsafe.Pointer)(v.ptr) == nil
|
||||
}
|
||||
panic(&ValueError{"reflectlite.Value.IsNil", v.kind()})
|
||||
}
|
||||
|
||||
// IsValid reports whether v represents a value.
|
||||
// It returns false if v is the zero Value.
|
||||
// If IsValid returns false, all other methods except String panic.
|
||||
// Most functions and methods never return an invalid Value.
|
||||
// If one does, its documentation states the conditions explicitly.
|
||||
func (v Value) IsValid() bool {
|
||||
return v.flag != 0
|
||||
}
|
||||
|
||||
// Kind returns v's Kind.
|
||||
// If v is the zero Value (IsValid returns false), Kind returns Invalid.
|
||||
func (v Value) Kind() Kind {
|
||||
return v.kind()
|
||||
}
|
||||
|
||||
/* TODO(xsw):
|
||||
// implemented in runtime:
|
||||
func chanlen(unsafe.Pointer) int
|
||||
func maplen(unsafe.Pointer) int
|
||||
*/
|
||||
|
||||
// Len returns v's length.
|
||||
// It panics if v's Kind is not Array, Chan, Map, Slice, or String.
|
||||
func (v Value) Len() int {
|
||||
k := v.kind()
|
||||
switch k {
|
||||
case abi.Slice:
|
||||
// Slice is bigger than a word; assume flagIndir.
|
||||
return (*unsafeheaderSlice)(v.ptr).Len
|
||||
case abi.String:
|
||||
// String is bigger than a word; assume flagIndir.
|
||||
return (*unsafeheaderString)(v.ptr).Len
|
||||
case abi.Array:
|
||||
tt := (*arrayType)(unsafe.Pointer(v.typ))
|
||||
return int(tt.Len)
|
||||
/* TODO(xsw):
|
||||
case abi.Chan:
|
||||
return chanlen(v.pointer())
|
||||
case abi.Map:
|
||||
return maplen(v.pointer())
|
||||
*/
|
||||
}
|
||||
panic(&ValueError{"reflect.Value.Len", v.kind()})
|
||||
}
|
||||
|
||||
// NumMethod returns the number of exported methods in the value's method set.
|
||||
func (v Value) numMethod() int {
|
||||
/*
|
||||
if v.typ == nil {
|
||||
panic(&ValueError{"reflectlite.Value.NumMethod", abi.Invalid})
|
||||
}
|
||||
return v.typ.NumMethod()
|
||||
*/
|
||||
panic("todo: reflectlite.Value.numMethod")
|
||||
}
|
||||
|
||||
// Set assigns x to the value v.
|
||||
// It panics if CanSet returns false.
|
||||
// As in Go, x's value must be assignable to v's type.
|
||||
func (v Value) Set(x Value) {
|
||||
v.mustBeAssignable()
|
||||
x.mustBeExported() // do not let unexported x leak
|
||||
var target unsafe.Pointer
|
||||
if v.kind() == abi.Interface {
|
||||
target = v.ptr
|
||||
}
|
||||
x = x.assignTo("reflectlite.Set", v.typ, target)
|
||||
if x.flag&flagIndir != 0 {
|
||||
typedmemmove(v.typ, v.ptr, x.ptr)
|
||||
} else {
|
||||
*(*unsafe.Pointer)(v.ptr) = x.ptr
|
||||
}
|
||||
}
|
||||
|
||||
// Type returns v's type.
|
||||
func (v Value) Type() Type {
|
||||
f := v.flag
|
||||
if f == 0 {
|
||||
panic(&ValueError{"reflectlite.Value.Type", abi.Invalid})
|
||||
}
|
||||
// Method values not supported.
|
||||
return toRType(v.typ)
|
||||
}
|
||||
|
||||
/*
|
||||
* constructors
|
||||
*/
|
||||
|
||||
//go:linkname unsafe_New github.com/goplus/llgo/runtime/internal/runtime.New
|
||||
func unsafe_New(*abi.Type) unsafe.Pointer
|
||||
|
||||
// ValueOf returns a new Value initialized to the concrete value
|
||||
// stored in the interface i. ValueOf(nil) returns the zero Value.
|
||||
func ValueOf(i any) Value {
|
||||
if i == nil {
|
||||
return Value{}
|
||||
}
|
||||
|
||||
return unpackEface(i)
|
||||
}
|
||||
|
||||
// assignTo returns a value v that can be assigned directly to typ.
|
||||
// It panics if v is not assignable to typ.
|
||||
// For a conversion to an interface type, target is a suggested scratch space to use.
|
||||
func (v Value) assignTo(context string, dst *abi.Type, target unsafe.Pointer) Value {
|
||||
// if v.flag&flagMethod != 0 {
|
||||
// v = makeMethodValue(context, v)
|
||||
// }
|
||||
|
||||
switch {
|
||||
case directlyAssignable(dst, v.typ):
|
||||
// Overwrite type so that they match.
|
||||
// Same memory layout, so no harm done.
|
||||
fl := v.flag&(flagAddr|flagIndir) | v.flag.ro()
|
||||
fl |= flag(dst.Kind())
|
||||
return Value{dst, v.ptr, fl}
|
||||
|
||||
case implements(dst, v.typ):
|
||||
if target == nil {
|
||||
target = unsafe_New(dst)
|
||||
}
|
||||
if v.Kind() == abi.Interface && v.IsNil() {
|
||||
// A nil ReadWriter passed to nil Reader is OK,
|
||||
// but using ifaceE2I below will panic.
|
||||
// Avoid the panic by returning a nil dst (e.g., Reader) explicitly.
|
||||
return Value{dst, nil, flag(abi.Interface)}
|
||||
}
|
||||
/* TODO(xsw):
|
||||
x := valueInterface(v)
|
||||
if dst.NumMethod() == 0 {
|
||||
*(*any)(target) = x
|
||||
} else {
|
||||
ifaceE2I(dst, x, target)
|
||||
}
|
||||
return Value{dst, target, flagIndir | flag(abi.Interface)}
|
||||
*/
|
||||
}
|
||||
|
||||
// Failed.
|
||||
// TODO(xsw):
|
||||
// panic(context + ": value of type " + toRType(v.typ).String() + " is not assignable to type " + toRType(dst).String())
|
||||
panic("todo: reflectlite.Value.assignTo")
|
||||
}
|
||||
|
||||
// arrayAt returns the i-th element of p,
|
||||
// an array whose elements are eltSize bytes wide.
|
||||
// The array pointed at by p must have at least i+1 elements:
|
||||
// it is invalid (but impossible to check here) to pass i >= len,
|
||||
// because then the result will point outside the array.
|
||||
// whySafe must explain why i < len. (Passing "i < len" is fine;
|
||||
// the benefit is to surface this assumption at the call site.)
|
||||
func arrayAt(p unsafe.Pointer, i int, eltSize uintptr, whySafe string) unsafe.Pointer {
|
||||
return add(p, uintptr(i)*eltSize, "i < len")
|
||||
}
|
||||
|
||||
// func ifaceE2I(t *abi.Type, src any, dst unsafe.Pointer)
|
||||
|
||||
// typedmemmove copies a value of type t to dst from src.
|
||||
//
|
||||
//go:linkname typedmemmove github.com/goplus/llgo/runtime/internal/runtime.Typedmemmove
|
||||
func typedmemmove(t *abi.Type, dst, src unsafe.Pointer)
|
||||
1
runtime/internal/lib/internal/stringslite/strings.go
Normal file
1
runtime/internal/lib/internal/stringslite/strings.go
Normal file
@@ -0,0 +1 @@
|
||||
package stringslite
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2020 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !windows
|
||||
|
||||
package execenv
|
||||
|
||||
// llgo:skipall
|
||||
import "syscall"
|
||||
|
||||
// Default will return the default environment
|
||||
// variables based on the process attributes
|
||||
// provided.
|
||||
//
|
||||
// Defaults to syscall.Environ() on all platforms
|
||||
// other than Windows.
|
||||
func Default(sys *syscall.SysProcAttr) ([]string, error) {
|
||||
return syscall.Environ(), nil
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// Copyright 2020 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build windows
|
||||
|
||||
package execenv
|
||||
|
||||
// llgo:skipall
|
||||
import (
|
||||
"internal/syscall/windows"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Default will return the default environment
|
||||
// variables based on the process attributes
|
||||
// provided.
|
||||
//
|
||||
// If the process attributes contain a token, then
|
||||
// the environment variables will be sourced from
|
||||
// the defaults for that user token, otherwise they
|
||||
// will be sourced from syscall.Environ().
|
||||
func Default(sys *syscall.SysProcAttr) (env []string, err error) {
|
||||
if sys == nil || sys.Token == 0 {
|
||||
return syscall.Environ(), nil
|
||||
}
|
||||
var blockp *uint16
|
||||
err = windows.CreateEnvironmentBlock(&blockp, sys.Token, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer windows.DestroyEnvironmentBlock(blockp)
|
||||
|
||||
const size = unsafe.Sizeof(*blockp)
|
||||
for *blockp != 0 { // environment block ends with empty string
|
||||
// find NUL terminator
|
||||
end := unsafe.Add(unsafe.Pointer(blockp), size)
|
||||
for *(*uint16)(end) != 0 {
|
||||
end = unsafe.Add(end, size)
|
||||
}
|
||||
|
||||
entry := unsafe.Slice(blockp, (uintptr(end)-uintptr(unsafe.Pointer(blockp)))/2)
|
||||
env = append(env, syscall.UTF16ToString(entry))
|
||||
blockp = (*uint16)(unsafe.Add(end, size))
|
||||
}
|
||||
return
|
||||
}
|
||||
15
runtime/internal/lib/internal/syscall/unix/nonblocking_js.go
Normal file
15
runtime/internal/lib/internal/syscall/unix/nonblocking_js.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build js && wasm
|
||||
|
||||
package unix
|
||||
|
||||
func IsNonblock(fd int) (nonblocking bool, err error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func HasNonblockFlag(flag int) bool {
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build unix
|
||||
|
||||
package unix
|
||||
|
||||
import "github.com/goplus/llgo/runtime/internal/clite/syscall"
|
||||
|
||||
/* TODO(xsw):
|
||||
func IsNonblock(fd int) (nonblocking bool, err error) {
|
||||
flag, e1 := Fcntl(fd, syscall.F_GETFL, 0)
|
||||
if e1 != nil {
|
||||
return false, e1
|
||||
}
|
||||
return flag&syscall.O_NONBLOCK != 0, nil
|
||||
}
|
||||
*/
|
||||
|
||||
func HasNonblockFlag(flag int) bool {
|
||||
return flag&syscall.O_NONBLOCK != 0
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright 2023 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build wasip1
|
||||
|
||||
package unix
|
||||
|
||||
import "github.com/goplus/llgo/runtime/internal/clite/syscall"
|
||||
|
||||
/* TODO(xsw):
|
||||
import (
|
||||
"syscall"
|
||||
_ "unsafe" // for go:linkname
|
||||
)
|
||||
|
||||
func IsNonblock(fd int) (nonblocking bool, err error) {
|
||||
flags, e1 := fd_fdstat_get_flags(fd)
|
||||
if e1 != nil {
|
||||
return false, e1
|
||||
}
|
||||
return flags&syscall.FDFLAG_NONBLOCK != 0, nil
|
||||
}
|
||||
*/
|
||||
|
||||
func HasNonblockFlag(flag int) bool {
|
||||
return flag&syscall.FDFLAG_NONBLOCK != 0
|
||||
}
|
||||
|
||||
/* TODO(xsw):
|
||||
// This helper is implemented in the syscall package. It means we don't have
|
||||
// to redefine the fd_fdstat_get host import or the fdstat struct it
|
||||
// populates.
|
||||
//
|
||||
//-go:linkname fd_fdstat_get_flags syscall.fd_fdstat_get_flags
|
||||
func fd_fdstat_get_flags(fd int) (uint32, error)
|
||||
*/
|
||||
22
runtime/internal/lib/internal/syscall/unix/unix.go
Normal file
22
runtime/internal/lib/internal/syscall/unix/unix.go
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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 unix
|
||||
|
||||
// llgo:skipall
|
||||
import (
|
||||
_ "unsafe"
|
||||
)
|
||||
Reference in New Issue
Block a user