Files
zeekay a6a6264f91 refactor: own the module path (github.com/hanzoai/yaegi)
Per the standing fork rule, a hanzoai fork declares its OWN module path and
is required directly -- never resolved via a `replace` against the upstream
path. Keeping the upstream path meant every import line in every consumer
carried the upstream brand.

  module github.com/traefik/yaegi -> module github.com/hanzoai/yaegi

Mechanical and behaviour-neutral: every occurrence is an import path
(package self-imports, cmd/, _test/ fixtures interpreted at test time, and
the guthib.com/<org>/{fromage,cheese,vin} fake vendor trees under
example/pkg, whose directory components were renamed in lockstep with the
import strings that reference them).

GOPATH-mode source-import tests (interp TestFile) resolve the module by its
path under $GOPATH/src, so the required checkout location moves with the
module path -- as README already documents. Verified green at
$GOPATH/src/github.com/hanzoai/yaegi.

LICENSE is untouched: "Copyright 2020 Traefik Labs SAS" is the Apache-2.0
attribution and is a legal obligation, not a brand leak.

Also drops the inherited CONTRIBUTING.md CLA clause, which named an
upstream agreement this fork does not administer.
2026-07-25 12:26:31 -07:00

71 lines
2.1 KiB
Go

// Package unsafe provides wrapper of standard library unsafe package to be imported natively in Yaegi.
package unsafe
import (
"reflect"
"unsafe"
)
// Symbols stores the map of unsafe package symbols.
var Symbols = map[string]map[string]reflect.Value{}
func init() {
Symbols["github.com/hanzoai/yaegi/stdlib/unsafe/unsafe"] = map[string]reflect.Value{
"Symbols": reflect.ValueOf(Symbols),
}
Symbols["github.com/hanzoai/yaegi/yaegi"] = map[string]reflect.Value{
"convert": reflect.ValueOf(convert),
}
Symbols["unsafe/unsafe"]["Add"] = reflect.ValueOf(add)
// Add builtin functions to unsafe, also implemented in interp/cfg.go.
Symbols["unsafe/unsafe"]["Sizeof"] = reflect.ValueOf(sizeof)
Symbols["unsafe/unsafe"]["Alignof"] = reflect.ValueOf(alignof)
// The following is used for signature check only.
Symbols["unsafe/unsafe"]["Offsetof"] = reflect.ValueOf(func(interface{}) uintptr { return 0 })
}
func convert(from, to reflect.Type) func(src, dest reflect.Value) {
switch {
case to.Kind() == reflect.UnsafePointer && from.Kind() == reflect.Uintptr:
return uintptrToUnsafePtr
case to.Kind() == reflect.UnsafePointer:
return func(src, dest reflect.Value) {
dest.SetPointer(unsafe.Pointer(src.Pointer()))
}
case to.Kind() == reflect.Uintptr && from.Kind() == reflect.UnsafePointer:
return func(src, dest reflect.Value) {
ptr := src.Interface().(unsafe.Pointer)
dest.Set(reflect.ValueOf(uintptr(ptr)))
}
case from.Kind() == reflect.UnsafePointer:
return func(src, dest reflect.Value) {
ptr := src.Interface().(unsafe.Pointer)
v := reflect.NewAt(dest.Type().Elem(), ptr)
dest.Set(v)
}
default:
return nil
}
}
func add(ptr unsafe.Pointer, l int) unsafe.Pointer {
return unsafe.Add(ptr, l)
}
func sizeof(i interface{}) uintptr {
return reflect.ValueOf(i).Type().Size()
}
func alignof(i interface{}) uintptr {
return uintptr(reflect.ValueOf(i).Type().Align())
}
//go:nocheckptr
func uintptrToUnsafePtr(src, dest reflect.Value) {
dest.SetPointer(unsafe.Pointer(src.Interface().(uintptr))) //nolint:govet
}
//go:generate ../../internal/cmd/extract/extract unsafe