github.com/goplusjs/gopherjs@v1.2.6-0.20211206034512-f187917453b8/compiler/natives/src/internal/reflectlite/utils.go (about)

     1  // +build js
     2  
     3  package reflectlite
     4  
     5  import (
     6  	"unsafe"
     7  )
     8  
     9  type ChanDir int
    10  
    11  const (
    12  	RecvDir ChanDir             = 1 << iota // <-chan
    13  	SendDir                                 // chan<-
    14  	BothDir = RecvDir | SendDir             // chan
    15  )
    16  
    17  type errorString struct {
    18  	s string
    19  }
    20  
    21  func (e *errorString) Error() string {
    22  	return e.s
    23  }
    24  
    25  var (
    26  	ErrSyntax = &errorString{"invalid syntax"}
    27  )
    28  
    29  func unquote(s string) (string, error) {
    30  	if len(s) < 2 {
    31  		return s, nil
    32  	}
    33  	if s[0] == '\'' || s[0] == '"' {
    34  		if s[len(s)-1] == s[0] {
    35  			return s[1 : len(s)-1], nil
    36  		}
    37  		return "", ErrSyntax
    38  	}
    39  	return s, nil
    40  }
    41  
    42  // Method represents a single method.
    43  type Method struct {
    44  	// Name is the method name.
    45  	// PkgPath is the package path that qualifies a lower case (unexported)
    46  	// method name. It is empty for upper case (exported) method names.
    47  	// The combination of PkgPath and Name uniquely identifies a method
    48  	// in a method set.
    49  	// See https://golang.org/ref/spec#Uniqueness_of_identifiers
    50  	Name    string
    51  	PkgPath string
    52  
    53  	Type  Type  // method type
    54  	Func  Value // func with receiver as first argument
    55  	Index int   // index for Type.Method
    56  }
    57  
    58  // A SelectDir describes the communication direction of a select case.
    59  type SelectDir int
    60  
    61  // NOTE: These values must match ../runtime/select.go:/selectDir.
    62  
    63  const (
    64  	_             SelectDir = iota
    65  	SelectSend              // case Chan <- Send
    66  	SelectRecv              // case <-Chan:
    67  	SelectDefault           // default
    68  )
    69  
    70  // A runtimeSelect is a single case passed to rselect.
    71  // This must match ../runtime/select.go:/runtimeSelect
    72  type runtimeSelect struct {
    73  	dir SelectDir      // SelectSend, SelectRecv or SelectDefault
    74  	typ *rtype         // channel type
    75  	ch  unsafe.Pointer // channel
    76  	val unsafe.Pointer // ptr to data (SendDir) or ptr to receive buffer (RecvDir)
    77  }
    78  
    79  func (f flag) mustBe(expected Kind) {
    80  	// TODO(mvdan): use f.kind() again once mid-stack inlining gets better
    81  	if Kind(f&flagKindMask) != expected {
    82  		panic(&ValueError{methodName(), f.kind()})
    83  	}
    84  }
    85  
    86  // A StructTag is the tag string in a struct field.
    87  //
    88  // By convention, tag strings are a concatenation of
    89  // optionally space-separated key:"value" pairs.
    90  // Each key is a non-empty string consisting of non-control
    91  // characters other than space (U+0020 ' '), quote (U+0022 '"'),
    92  // and colon (U+003A ':').  Each value is quoted using U+0022 '"'
    93  // characters and Go string literal syntax.
    94  type StructTag string
    95  
    96  func typesMustMatch(what string, t1, t2 Type) {
    97  	if t1 != t2 {
    98  		panic(what + ": " + t1.String() + " != " + t2.String())
    99  	}
   100  }