/* Emit a signal with the given parameters */ func Emit(signal Signal, params ...Object) { signalID := signal.id if registeredSlot[signalID] == nil { registeredSlot[signalID] = []SlotVal{} } slots := registeredSlot[signalID] for _, slot := range slots { slottype := reflect.Value(slot).Type() paramlist := []reflect.Value{} /* Try to match number of parameters */ if slottype.IsVariadic() { for _, param := range params { paramlist = append(paramlist, reflect.ValueOf(param)) } } else { for i := 0; i < slottype.NumIn(); i++ { paramlist = append(paramlist, reflect.ValueOf(params[i])) } } go func(slot SlotVal, paramlist []reflect.Value) { defer crashHandler("Fatal: Cannot call slot/function") reflect.Value(slot).Call(paramlist) }(slot, paramlist) } }
// create one client instance func (zt *ZooThrift) createClientFactory() (interface{}, error) { transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory()) protocolFactory := thrift.NewTBinaryProtocolFactoryDefault() address := zt.provider.Selector() if address != "" { transport, err := thrift.NewTSocket(address) if err != nil { return nil, err } useTransport := transportFactory.GetTransport(transport) client := reflect.ValueOf(zt.Service) mutable := client.Elem() mutable.FieldByName("Transport").Set(reflect.Value(reflect.ValueOf(useTransport))) mutable.FieldByName("ProtocolFactory").Set(reflect.Value(reflect.ValueOf(protocolFactory))) mutable.FieldByName("InputProtocol").Set(reflect.Value(reflect.ValueOf(protocolFactory.GetProtocol(useTransport)))) mutable.FieldByName("OutputProtocol").Set(reflect.Value(reflect.ValueOf(protocolFactory.GetProtocol(useTransport)))) mutable.FieldByName("SeqId").SetInt(0) if err := transport.Open(); err != nil { return nil, err } return zt.Service, nil } else { return nil, ErrSerAddress } }
func TypeOf(expr Sexp) SexpStr { v := "" switch e := expr.(type) { case SexpRaw: v = "raw" case *SexpArray: v = "array" case *SexpInt: v = "int64" case SexpStr: v = "string" case SexpChar: v = "char" case SexpFloat: v = "float64" case *SexpHash: v = e.TypeName case SexpPair: v = "list" case SexpSymbol: v = "symbol" case *SexpFunction: v = "func" case SexpSentinel: v = "nil" case SexpTime: v = "time.Time" case *RegisteredType: v = "regtype" case *SexpPointer: v = e.MyType.RegisteredName case SexpReflect: rt := expr.Type() if rt != nil { return SexpStr{S: rt.RegisteredName} } //v = reflect.Value(e).Type().Name() //if v == "Ptr" { // v = reflect.Value(e).Type().Elem().Kind().String() //} kind := reflect.Value(e).Type().Kind() if kind == reflect.Ptr { v = reflect.Value(e).Elem().Type().Name() } else { P("kind = %v", kind) v = "reflect.Value" } default: fmt.Printf("\n error: unknown type: %T in '%#v'\n", e, e) } return SexpStr{S: v} }
// Returns a printable interface{} which replaces constant expressions with their constants func simplifyBinaryChildExpr(parent *BinaryExpr, expr, other Expr) interface{} { op := parent.Op() if expr.IsConst() { // This mess is due to automatic type conversions in binary expressions. // It will disappear once the AST is retyped as a first step v := expr.Const() eT := expr.KnownType()[0] oT := other.KnownType() if len(oT) == 1 { ct, eok := eT.(ConstType) if eok { // Separate case for shifts if op == token.SHL || op == token.SHR { // Don't touch the shifted operand if parent.X == expr { return expr } else if isUnsignedInt(eT) { c, _ := promoteConstToTyped(ct, constValue(expr.Const()), uintType, expr) return reflect.Value(c).Interface() } } else if _, ook := oT[0].(ConstType); !ook { if eT == ConstNil { return "nil" } c, _ := promoteConstToTyped(ct, constValue(expr.Const()), oT[0], expr) if reflect.Value(c).IsValid() { eT = oT[0] v = reflect.Value(c) } else if _, ok := eT.(ConstRuneType); ok { // For mismatched type errors eT = RuneType } } } } return sprintConstValue(eT, v, false) } expr = skipSuperfluousParens(expr) if p, ok := expr.(*ParenExpr); ok { // Remove parens all together from 1 + (2 * 3) if b, ok := p.X.(*BinaryExpr); ok { op := b.Op() if op.Precedence() > op.Precedence() { return p.X } } } return expr }
func checkArrayIndex(expr ast.Expr, env Env) (aexpr Expr, i int, ok bool, checkErrs []error) { aexpr, checkErrs = CheckExpr(expr, env) if !aexpr.IsConst() { return aexpr, 0, false, checkErrs } t := aexpr.KnownType()[0] var ii int64 if ct, ok := t.(ConstType); ok { c, moreErrs := promoteConstToTyped(ct, constValue(aexpr.Const()), intType, aexpr) if moreErrs != nil { checkErrs = append(checkErrs, moreErrs...) } v := reflect.Value(c) if v.IsValid() { ii = v.Int() } else { return aexpr, 0, false, checkErrs } } else { switch t.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: ii = aexpr.Const().Int() case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: ii = int64(aexpr.Const().Uint()) default: return aexpr, 0, false, checkErrs } } // The limit of 2^31-1 is derived from the gc implementation, // which seems to use this definition whilst type checking. // The actual definition is the "largest value representable by an int" return aexpr, int(ii), 0 <= ii && ii <= 0x7fffffff, checkErrs }
// Eval a node and cast it to an int. expr must be a *ConstNumber or integral type func evalInteger(expr Expr, env Env) (int, error) { if expr.IsConst() { x := expr.Const() if ct, ok := expr.KnownType()[0].(ConstType); ok { cx, _ := promoteConstToTyped(ct, constValue(x), intType, expr) return int(reflect.Value(cx).Int()), nil } else { panic(dytc("const bool or string evaluated as int")) } } else { xs, err := EvalExpr(expr, env) if err != nil { return 0, err } x := xs[0] switch x.Type().Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return int(x.Int()), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: return int(x.Uint()), nil default: panic(dytc("non-integral type evaluated as int")) } } }
func (s intSlice) Swap(i, j int) { v := reflect.Value(s) ii := v.Index(i).Uint() jj := v.Index(j).Uint() v.Index(i).SetUint(jj) v.Index(j).SetUint(ii) }
func parseInteger(v reflect.Value, b []byte) error { switch v.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: default: return &TypeError{TagInteger, v.Kind()} } if len(b) > 8 { return errors.New("value does not fit in a int64") } var n int64 for i, v := range b { shift := uint((len(b) - i - 1) * 8) if i == 0 { if v&0x80 != 0 { n -= 0x80 << shift v &= 0x7f } } n += int64(v) << shift } reflect.Value(v).SetInt(n) return nil }
func checkCompositeLitMap(ctx *Ctx, lit *CompositeLit, t reflect.Type, env *Env) (*CompositeLit, []error) { var errs, moreErrs []error kT := t.Key() // Don't check for duplicate interface{} keys. This is a gc bug // http://code.google.com/p/go/issues/detail?id=7214 var seen map[interface{}]bool if kT.Kind() != reflect.Interface { seen = make(map[interface{}]bool, len(lit.Elts)) } eltT := t.Elem() for i := range lit.Elts { if kv, ok := lit.Elts[i].(*ast.KeyValueExpr); !ok { lit.Elts[i], moreErrs = CheckExpr(ctx, lit.Elts[i], env) if moreErrs != nil { errs = append(errs, moreErrs...) } errs = append(errs, ErrMissingMapKey{at(ctx, lit.Elts[i])}) } else { lit.Elts[i] = &KeyValueExpr{KeyValueExpr: kv} k, ok, moreErrs := checkExprAssignableTo(ctx, kv.Key, kT, env) if !ok { if len(k.KnownType()) != 0 { kF := fakeCheckExpr(kv.Key, env) kF.setKnownType(knownType(k.KnownType())) errs = append(errs, ErrBadMapKey{at(ctx, kF), kT}) } } else { errs = append(errs, moreErrs...) } kv.Key = k if seen != nil && k.IsConst() { var constKey interface{} if k.KnownType()[0] == ConstNil { constKey = nil } else if cT, ok := k.KnownType()[0].(ConstType); ok { c, _ := promoteConstToTyped(ctx, cT, constValue(k.Const()), cT.DefaultPromotion(), k) constKey = reflect.Value(c).Interface() } else { constKey = k.Const().Interface() } if seen[constKey] { errs = append(errs, ErrDuplicateMapKey{at(ctx, kv.Key)}) } seen[constKey] = true } v, moreErrs := checkMapValue(ctx, kv.Value, eltT, env) if moreErrs != nil { errs = append(errs, moreErrs...) } kv.Value = v } } return lit, errs }
func evalConstTypedBinaryExpr(binary *BinaryExpr, xexpr, yexpr Expr) (constValue, []error) { // These are known not to be ConstTypes xt := xexpr.KnownType()[0] yt := yexpr.KnownType()[0] op := binary.Op() // Check that the types are compatible, handling the special alias type for runes // For the sake of error messages, for expressions involving int32 and rune, the // resulting type is that of the left operand var zt reflect.Type if xt == yt { zt = xt } else if xt == RuneType && yt == RuneType.Type || xt == ByteType && yt == ByteType.Type { zt = RuneType } else if yt == RuneType && xt == RuneType.Type || yt == ByteType && yt == ByteType.Type { zt = xt } else { return constValue{}, []error{ErrInvalidBinaryOperation{binary}} } x, xok := convertTypedToConstNumber(xexpr.Const()) y, yok := convertTypedToConstNumber(yexpr.Const()) if xok && yok { z, errs := evalConstBinaryNumericExpr(binary, x, y) if isBooleanOp(op) { return constValue(z), errs } if errs != nil { if _, ok := errs[0].(ErrInvalidBinaryOperation); ok { // This happens if the operator is not defined on x and y return constValue(z), errs } } from := reflect.Value(z).Interface().(*ConstNumber).Type r, moreErrs := promoteConstToTyped(from, z, zt, binary) return constValue(r), append(errs, moreErrs...) } else if !xok && !yok { switch zt.Kind() { case reflect.String: xstring := xexpr.Const().String() ystring := yexpr.Const().String() z, errs := evalConstBinaryStringExpr(binary, xstring, ystring) if isBooleanOp(op) { return constValue(z), errs } r, moreErrs := promoteConstToTyped(ConstString, z, zt, binary) return constValue(r), append(errs, moreErrs...) case reflect.Bool: xbool := xexpr.Const().Bool() ybool := yexpr.Const().Bool() z, errs := evalConstBinaryBoolExpr(binary, xbool, ybool) return constValue(z), errs } } panic("go-interactive: impossible") }
// TODO: document me // func GetByteArrayLen(v reflect.Value) (len int, ok bool) { switch v.Kind() { case reflect.Array, reflect.Slice: aosv := reflect.Value(v) return aosv.Len(), true } return }
func (a structAdaptor) tagOf(key string) reflect.StructTag { v := reflect.Value(a) field, ok := v.Type().FieldByName(key) if ok { return field.Tag } return "" }
func (r *SexpReflect) SexpString(ps *PrintState) string { Q("in SexpReflect.SexpString(indent); top; type = %T", r) if reflect.Value(r.Val).Type().Kind() == reflect.Ptr { iface := reflect.Value(r.Val).Interface() switch iface.(type) { case *string: return fmt.Sprintf("`%v`", reflect.Value(r.Val).Elem().Interface()) default: return fmt.Sprintf("%v", reflect.Value(r.Val).Elem().Interface()) } } iface := reflect.Value(r.Val).Interface() Q("in SexpReflect.SexpString(indent); type = %T", iface) switch iface.(type) { default: return fmt.Sprintf("%v", iface) } }
func (a structAdaptor) keys() []string { v := reflect.Value(a) t := v.Type() keys := make([]string, t.NumField()) for i := range keys { keys[i] = t.Field(i).Name } return keys }
// For display purposes only, display untyped const nodes as they would be // displayed as a typed const node. func sprintUntypedConstAsTyped(expr Expr) string { if !expr.IsConst() { return expr.String() } switch expr.KnownType()[0].(type) { case ConstRuneType: return sprintConstValue(RuneType, reflect.Value(expr.Const()), false) default: return expr.String() } }
func (me Method) Call(ret_reciever interface{}, params ...interface{}) error { subj_in := []reflect.Value{} for _, v := range params { subj_in = append(subj_in, reflect.ValueOf(v)) } subj_out := reflect.Value(me).Call(subj_in) if ret_reciever != nil { reflect.ValueOf(ret_reciever).Call(subj_out) } return nil }
func (r *SexpReflect) Type() *RegisteredType { k := reflectName(reflect.Value(r.Val)) Q("SexpReflect.Type() looking up type named '%s'", k) ty, ok := GoStructRegistry.Registry[k] if !ok { Q("SexpReflect.Type(): type named '%s' not found", k) return nil } Q("SexpReflect.Type(): type named '%s' found as regtype '%v'", k, ty.SexpString(nil)) return ty }
// TODO: document me // func GetByteArray(v reflect.Value) (arr []byte, ok bool) { switch v.Kind() { case reflect.Array, reflect.Slice: aosv := reflect.Value(v) arr = make([]byte, aosv.Len()) for i := 0; i < aosv.Len(); i++ { arr[i] = uint8(aosv.Index(i).Uint()) } return arr, true } return }
func (a arrayValue) String() string { v := reflect.Value(a.reflectValue) str := "[" for i := 0; i < v.Len(); i++ { if i > 0 { str += ", " } str1 := quoteString(refToVal(v.Index(i))) str += str1 } str += "]" return str }
func (i Instance) MethodNames() []string { t := reflect.TypeOf(reflect.Value(i).Interface()) names := []string{} num := t.NumMethod() for i := 0; i < num; i++ { mname := t.Method(i).Name r, _ := utf8.DecodeRuneInString(mname) if unicode.IsUpper(r) { names = append(names, mname) } } return names }
func NewPairSlice(I interface{}) PairSlice { V := reflect.ValueOf(I) keys := V.MapKeys() ps := PairSlice{} for _, k := range keys { m := MapPair{ Key: reflect.Value(k), Value: V.MapIndex(k), } ps = append(ps, m) } return ps }
func (a stringMapAdaptor) keys() []string { v := reflect.Value(a) keys := make([]string, v.Len()) for i, v := range v.MapKeys() { if v.IsNil() { continue } switch t := v.Interface().(type) { case string: keys[i] = t } } return keys }
func evalConstTypedUnaryExpr(ctx *Ctx, unary *UnaryExpr, x Expr) (constValue, []error) { t := x.KnownType()[0] if xx, ok := convertTypedToConstNumber(x.Const()); ok { zz, errs := evalConstUnaryNumericExpr(ctx, unary, xx) if !reflect.Value(zz).IsValid() { return constValue{}, errs } rr, moreErrs := promoteConstToTyped(ctx, xx.Type, zz, t, x) return rr, append(errs, moreErrs...) } else if t.Kind() == reflect.Bool { return evalConstUnaryBoolExpr(ctx, unary, x.Const().Bool()) } return constValue{}, []error{ErrInvalidUnaryOperation{at(ctx, unary)}} }
// Evals an expression with a known result type. If the node is an // untyped constant, it is converted to type t. This function assumes // the input is successfully type checked, and therefore is undefined // incorrectly typed inputs. func evalTypedExpr(expr Expr, t knownType, env Env) (xs []reflect.Value, err error) { if expr.IsConst() { x := expr.Const() if ct, ok := expr.KnownType()[0].(ConstType); ok { cx, _ := promoteConstToTyped(ct, constValue(x), t[0], expr) xs = []reflect.Value{reflect.Value(cx)} } else { xs = []reflect.Value{x} } } else { xs, err = EvalExpr(expr, env) } return xs, err }
func getParams(req *jsonRequest, argTypes []reflect.Type) ([]reflect.Value, error) { params := make([]*json.RawMessage, 0) err := json.Unmarshal(*req.Params, ¶ms) if err != nil { return nil, err } if len(params) != len(argTypes) { return nil, errors.New("Incorrect number of parameters.") } args := make([]reflect.Value, 0, len(argTypes)) for i, argType := range argTypes { argPointerType := argType if argPointerType.Kind() == reflect.Ptr { argPointer := reflect.New(argType) err := json.Unmarshal(*params[i], argPointer.Interface()) if err != nil { return nil, errors.New("Type mismatch parameter " + strconv.Itoa(i+1) + ".") } args = append(args, reflect.Indirect(argPointer)) } else { var arg reflect.Value var v interface{} err := json.Unmarshal(*params[i], &v) if err != nil { return nil, errors.New("Type mismatch parameter " + strconv.Itoa(i+1) + ".") } value := reflect.ValueOf(v) if value.Type() == argType { arg = reflect.ValueOf(v) } else if value.Type().Kind() == reflect.Float32 || value.Type().Kind() == reflect.Float64 { if argType.Kind() == reflect.Int || argType.Kind() == reflect.Int8 || argType.Kind() == reflect.Int16 || argType.Kind() == reflect.Int32 || argType.Kind() == reflect.Int64 { arg = reflect.ValueOf(int(v.(float64))) } else { return nil, errors.New("Type mismatch parameter " + strconv.Itoa(i+1) + ".") } } else { return nil, errors.New("Type mismatch parameter " + strconv.Itoa(i+1) + ".") } args = append(args, reflect.Value(arg)) } } return args, nil }
func compareString(s *SexpStr, expr Sexp) (int, error) { switch e := expr.(type) { case *SexpStr: return bytes.Compare([]byte(s.S), []byte(e.S)), nil case *SexpReflect: r := reflect.Value(e.Val) ifa := r.Interface() switch z := ifa.(type) { case *string: return bytes.Compare([]byte(s.S), []byte(*z)), nil } } errmsg := fmt.Sprintf("err 94: cannot compare %T to %T", s, expr) return 0, errors.New(errmsg) }
// Determine if the result of from expr is assignable to type to. to must be a vanilla reflect.Type. // from must have a KnownType() of length 1. Const types that raise overflow and truncation // errors will still return true, but the errors will be reflected in the []error slice. func exprAssignableTo(from Expr, to reflect.Type) (bool, []error) { if len(from.KnownType()) != 1 { panic("go-eval: assignableTo called with from.KnownType() != 1") } fromType := from.KnownType()[0] // Check that consts can be converted if c, ok := fromType.(ConstType); ok && from.IsConst() { // If cv is a valid value, then the types are assignable even if // other conversion errors, such as overflows, are present. cv, errs := promoteConstToTyped(c, constValue(from.Const()), to, from) return reflect.Value(cv).IsValid(), errs } return typeAssignableTo(fromType, to), nil }
func (m mapValue) String() string { v := reflect.Value(m.reflectValue) keys := v.MapKeys() str := "{" for i, key := range keys { if i > 0 { str += ", " } v1 := refToVal(key) str1 := quoteString(v1) str += str1 str += ": " v1 = refToVal(v.MapIndex(key)) str1 = quoteString(v1) str += str1 } str += "}" return str }
// Type check an integral node. Returns the type checked node, the // integer value if constant, ok if the node was indeed integral, // and checkErrs which occur during the type check. It is possible // that checkErrs will be non-nil yet ok is still true. In this case // the errors are non-fatal, such as integer truncation. func checkInteger(expr ast.Expr, env Env) (aexpr Expr, i int, ok bool, checkErrs []error) { aexpr, checkErrs = CheckExpr(expr, env) if checkErrs != nil && !aexpr.IsConst() { return aexpr, 0, false, checkErrs } t, err := expectSingleType(aexpr) if err != nil { return aexpr, 0, false, append(checkErrs, err) } var ii int64 if ct, ok := t.(ConstType); ok { c, moreErrs := promoteConstToTyped(ct, constValue(aexpr.Const()), intType, aexpr) if moreErrs != nil { checkErrs = append(checkErrs, moreErrs...) } v := reflect.Value(c) if v.IsValid() { ii = v.Int() } else { return aexpr, 0, false, checkErrs } } else { switch t.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: if aexpr.IsConst() { ii = aexpr.Const().Int() } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: if aexpr.IsConst() { ii = int64(aexpr.Const().Uint()) } default: return aexpr, 0, false, checkErrs } } return aexpr, int(ii), true, checkErrs }
func (f reflectField) Set(value interface{}) error { v := reflect.Value(f) if f64, ok := value.(float64); ok { switch v.Kind() { case reflect.Float32, reflect.Float64: v.SetFloat(f64) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: v.SetInt(int64(f64)) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: v.SetUint(uint64(f64)) default: return newError(fmt.Sprintf("Can't convert float64 to %s.", v.Type().Name())) } return nil } v.Set(reflect.ValueOf(value)) return nil }