mirror of
https://github.com/hanzoai/yaegi.git
synced 2026-10-01 00:49:47 +00:00
* feat: generate go1.21 files * chore: update CI * feat: add support for generic symbols in standard library packages This is necessary to fully support go1.21 and beyond, which now provide some generic packages such as `cmp`, `maps` or `slices` in the standard library. The principle is to embed the generic symbols in source form (as strings) so they can be instantiated as required during interpretation. Extract() has been modified to skip the generic types, functions and constraint interfaces which can't be represented as reflect.Values. A new stdlib/generic package has been added to provide the corresponding source files as embedded strings. The `Use()` function has been changed to pre-parse generic symbols as doing lazy parsing was causing cyclic dependencies issues at compiling. This is something we may improve in the future. A unit test using `cmp` has been added. For now, there are still some issues with generic stdlib packages inter-dependencies, for example `slices` importing `cmp`, or when generic types or function signatures depends on pre-compiled types in the same package, which we will support shortly. * fixup * fixup * fixup * fixup * fixup * fixup * fixes for go1.20 * fix previous * update unsafe2 for go1.21, skip faky tests In go1.21, the reflect rtype definition has been move to internal/abi. We follow this change for maintainability, even if there is no layout change (the go1.20 unsafe2 is compatible with go1.21). We have isolated a few problematic tests which are failing sometimes in go1.21, but work in go1.20, and also in go1.22. Those tests are skipped if in go1.21. A preliminary investigation can not confirm that something is wrong in yaegi, and the problem disappears with go1.22. * add new wrapper for go1.21 package testing/slogtest * add missing wrapper for go/doc/comment * add support for slices generic package --------- Co-authored-by: Marc Vertes <[email protected]>
67 lines
1.8 KiB
Plaintext
67 lines
1.8 KiB
Plaintext
// 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.
|
|
|
|
// Package maps defines various functions useful with maps of any type.
|
|
package maps
|
|
|
|
// Equal reports whether two maps contain the same key/value pairs.
|
|
// Values are compared using ==.
|
|
func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool {
|
|
if len(m1) != len(m2) {
|
|
return false
|
|
}
|
|
for k, v1 := range m1 {
|
|
if v2, ok := m2[k]; !ok || v1 != v2 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// EqualFunc is like Equal, but compares values using eq.
|
|
// Keys are still compared with ==.
|
|
func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool {
|
|
if len(m1) != len(m2) {
|
|
return false
|
|
}
|
|
for k, v1 := range m1 {
|
|
if v2, ok := m2[k]; !ok || !eq(v1, v2) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// clone is implemented in the runtime package.
|
|
func clone(m any) any { return m }
|
|
|
|
// Clone returns a copy of m. This is a shallow clone:
|
|
// the new keys and values are set using ordinary assignment.
|
|
func Clone[M ~map[K]V, K comparable, V any](m M) M {
|
|
// Preserve nil in case it matters.
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
return clone(m).(M)
|
|
}
|
|
|
|
// Copy copies all key/value pairs in src adding them to dst.
|
|
// When a key in src is already present in dst,
|
|
// the value in dst will be overwritten by the value associated
|
|
// with the key in src.
|
|
func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2) {
|
|
for k, v := range src {
|
|
dst[k] = v
|
|
}
|
|
}
|
|
|
|
// DeleteFunc deletes any key/value pairs from m for which del returns true.
|
|
func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool) {
|
|
for k, v := range m {
|
|
if del(k, v) {
|
|
delete(m, k)
|
|
}
|
|
}
|
|
}
|