| 1 | package interp |
| 2 | |
| 3 | //go:generate go run ../internal/cmd/genop/genop.go |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "go/constant" |
| 9 | "reflect" |
| 10 | "regexp" |
| 11 | "strings" |
| 12 | ) |
| 13 | |
| 14 | // bltn type defines functions which run at CFG execution. |
| 15 | type bltn func(f *frame) bltn |
| 16 | |
| 17 | // bltnGenerator type defines a builtin generator function. |
| 18 | type bltnGenerator func(n *node) |
| 19 | |
| 20 | var builtin = [...]bltnGenerator{ |
| 21 | aNop: nop, |
| 22 | aAddr: addr, |
| 23 | aAssign: assign, |
| 24 | aAdd: add, |
| 25 | aAddAssign: addAssign, |
| 26 | aAnd: and, |
| 27 | aAndAssign: andAssign, |
| 28 | aAndNot: andNot, |
| 29 | aAndNotAssign: andNotAssign, |
| 30 | aBitNot: bitNot, |
| 31 | aCall: call, |
| 32 | aCallSlice: call, |
| 33 | aCase: _case, |
| 34 | aCompositeLit: arrayLit, |
| 35 | aDec: dec, |
| 36 | aEqual: equal, |
| 37 | aGetFunc: getFunc, |
| 38 | aGreater: greater, |
| 39 | aGreaterEqual: greaterEqual, |
| 40 | aInc: inc, |
| 41 | aLand: land, |
| 42 | aLor: lor, |
| 43 | aLower: lower, |
| 44 | aLowerEqual: lowerEqual, |
| 45 | aMul: mul, |
| 46 | aMulAssign: mulAssign, |
| 47 | aNeg: neg, |
| 48 | aNot: not, |
| 49 | aNotEqual: notEqual, |
| 50 | aOr: or, |
| 51 | aOrAssign: orAssign, |
| 52 | aPos: pos, |
| 53 | aQuo: quo, |
| 54 | aQuoAssign: quoAssign, |
| 55 | aRange: _range, |
| 56 | aRecv: recv, |
| 57 | aRem: rem, |
| 58 | aRemAssign: remAssign, |
| 59 | aReturn: _return, |
| 60 | aSend: send, |
| 61 | aShl: shl, |
| 62 | aShlAssign: shlAssign, |
| 63 | aShr: shr, |
| 64 | aShrAssign: shrAssign, |
| 65 | aSlice: slice, |
| 66 | aSlice0: slice0, |
| 67 | aStar: deref, |
| 68 | aSub: sub, |
| 69 | aSubAssign: subAssign, |
| 70 | aTypeAssert: typeAssertShort, |
| 71 | aXor: xor, |
| 72 | aXorAssign: xorAssign, |
| 73 | } |
| 74 | |
| 75 | var receiverStripperRxp *regexp.Regexp |
| 76 | |
| 77 | func init() { |
| 78 | re := `func\(((.*?(, |\)))(.*))` |
| 79 | var err error |
| 80 | receiverStripperRxp, err = regexp.Compile(re) |
| 81 | if err != nil { |
| 82 | panic(err) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | type valueInterface struct { |
| 87 | node *node |
| 88 | value reflect.Value |
| 89 | } |
| 90 | |
| 91 | var floatType, complexType reflect.Type |
| 92 | |
| 93 | func init() { |
| 94 | floatType = reflect.ValueOf(0.0).Type() |
| 95 | complexType = reflect.ValueOf(complex(0, 0)).Type() |
| 96 | } |
| 97 | |
| 98 | func (interp *Interpreter) run(n *node, cf *frame) { |
| 99 | if n == nil { |
| 100 | return |
| 101 | } |
| 102 | var f *frame |
| 103 | if cf == nil { |
| 104 | f = interp.frame |
| 105 | } else { |
| 106 | f = newFrame(cf, len(n.types), interp.runid()) |
| 107 | } |
| 108 | interp.mutex.RLock() |
| 109 | c := reflect.ValueOf(interp.done) |
| 110 | interp.mutex.RUnlock() |
| 111 | |
| 112 | f.mutex.Lock() |
| 113 | f.done = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: c} |
| 114 | f.mutex.Unlock() |
| 115 | |
| 116 | for i, t := range n.types { |
| 117 | f.data[i] = reflect.New(t).Elem() |
| 118 | } |
| 119 | runCfg(n.start, f, n, nil) |
| 120 | } |
| 121 | |
| 122 | func isExecNode(n *node, exec bltn) bool { |
| 123 | if n == nil || n.exec == nil || exec == nil { |
| 124 | return false |
| 125 | } |
| 126 | |
| 127 | a1 := reflect.ValueOf(n.exec).Pointer() |
| 128 | a2 := reflect.ValueOf(exec).Pointer() |
| 129 | return a1 == a2 |
| 130 | } |
| 131 | |
| 132 | // originalExecNode looks in the tree of nodes for the node which has exec, |
| 133 | // aside from n, in order to know where n "inherited" that exec from. |
| 134 | func originalExecNode(n *node, exec bltn) *node { |
| 135 | execAddr := reflect.ValueOf(exec).Pointer() |
| 136 | var originalNode *node |
| 137 | seen := make(map[int64]struct{}) |
| 138 | root := n |
| 139 | for { |
| 140 | root = root.anc |
| 141 | if root == nil { |
| 142 | break |
| 143 | } |
| 144 | if _, ok := seen[root.index]; ok { |
| 145 | continue |
| 146 | } |
| 147 | |
| 148 | root.Walk(func(wn *node) bool { |
| 149 | if _, ok := seen[wn.index]; ok { |
| 150 | return true |
| 151 | } |
| 152 | seen[wn.index] = struct{}{} |
| 153 | if wn.index == n.index { |
| 154 | return true |
| 155 | } |
| 156 | if wn.exec == nil { |
| 157 | return true |
| 158 | } |
| 159 | if reflect.ValueOf(wn.exec).Pointer() == execAddr { |
| 160 | originalNode = wn |
| 161 | return false |
| 162 | } |
| 163 | return true |
| 164 | }, nil) |
| 165 | |
| 166 | if originalNode != nil { |
| 167 | break |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return originalNode |
| 172 | } |
| 173 | |
| 174 | // cloned from net/http/server.go , so we can enforce a similar behavior: |
| 175 | // in the stdlib, this error is used as sentinel in panic triggered e.g. on |
| 176 | // request cancellation, in order to catch it and suppress it in a following defer. |
| 177 | // in yaegi, we use it to suppress a "panic" log message that happens in the |
| 178 | // same circumstances. |
| 179 | var errAbortHandler = errors.New("net/http: abort Handler") |
| 180 | |
| 181 | // Functions set to run during execution of CFG. |
| 182 | |
| 183 | func panicFunc(s *scope) string { |
| 184 | if s == nil { |
| 185 | return "" |
| 186 | } |
| 187 | def := s.def |
| 188 | if def == nil { |
| 189 | return s.pkgID |
| 190 | } |
| 191 | switch def.kind { |
| 192 | case funcDecl: |
| 193 | if c := def.child[1]; c.kind == identExpr { |
| 194 | return s.pkgID + "." + c.ident |
| 195 | } |
| 196 | case funcLit: |
| 197 | if def.anc != nil { |
| 198 | return panicFunc(def.anc.scope) + ".func" |
| 199 | } |
| 200 | } |
| 201 | return s.pkgID |
| 202 | } |
| 203 | |
| 204 | // runCfg executes a node AST by walking its CFG and running node builtin at each step. |
| 205 | func runCfg(n *node, f *frame, funcNode, callNode *node) { |
| 206 | var exec bltn |
| 207 | defer func() { |
| 208 | f.mutex.Lock() |
| 209 | f.recovered = recover() |
| 210 | for _, val := range f.deferred { |
| 211 | val[0].Call(val[1:]) |
| 212 | } |
| 213 | if f.recovered != nil { |
| 214 | oNode := originalExecNode(n, exec) |
| 215 | if oNode == nil { |
| 216 | oNode = n |
| 217 | } |
| 218 | errorer, ok := f.recovered.(error) |
| 219 | // in this specific case, the stdlib would/will suppress the panic, so we |
| 220 | // suppress the logging here accordingly, to get a similar and consistent |
| 221 | // behavior. |
| 222 | if !ok || errorer.Error() != errAbortHandler.Error() { |
| 223 | fmt.Fprintln(n.interp.stderr, oNode.cfgErrorf("panic: %s(...)", panicFunc(oNode.scope))) |
| 224 | } |
| 225 | f.mutex.Unlock() |
| 226 | panic(f.recovered) |
| 227 | } |
| 228 | f.mutex.Unlock() |
| 229 | }() |
| 230 | |
| 231 | dbg := n.interp.debugger |
| 232 | if dbg == nil { |
| 233 | for exec := n.exec; exec != nil && f.runid() == n.interp.runid(); { |
| 234 | exec = exec(f) |
| 235 | } |
| 236 | return |
| 237 | } |
| 238 | |
| 239 | if n.exec == nil { |
| 240 | return |
| 241 | } |
| 242 | |
| 243 | dbg.enterCall(funcNode, callNode, f) |
| 244 | defer dbg.exitCall(funcNode, callNode, f) |
| 245 | |
| 246 | for m, exec := n, n.exec; f.runid() == n.interp.runid(); { |
| 247 | if dbg.exec(m, f) { |
| 248 | break |
| 249 | } |
| 250 | |
| 251 | exec = exec(f) |
| 252 | if exec == nil { |
| 253 | break |
| 254 | } |
| 255 | |
| 256 | if m == nil { |
| 257 | m = originalExecNode(n, exec) |
| 258 | continue |
| 259 | } |
| 260 | |
| 261 | switch { |
| 262 | case isExecNode(m.tnext, exec): |
| 263 | m = m.tnext |
| 264 | case isExecNode(m.fnext, exec): |
| 265 | m = m.fnext |
| 266 | default: |
| 267 | m = originalExecNode(m, exec) |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | func stripReceiverFromArgs(signature string) (string, error) { |
| 273 | fields := receiverStripperRxp.FindStringSubmatch(signature) |
| 274 | if len(fields) < 5 { |
| 275 | return "", errors.New("error while matching method signature") |
| 276 | } |
| 277 | if fields[3] == ")" { |
| 278 | return fmt.Sprintf("func()%s", fields[4]), nil |
| 279 | } |
| 280 | return fmt.Sprintf("func(%s", fields[4]), nil |
| 281 | } |
| 282 | |
| 283 | func typeAssertShort(n *node) { |
| 284 | typeAssert(n, true, false) |
| 285 | } |
| 286 | |
| 287 | func typeAssertLong(n *node) { |
| 288 | typeAssert(n, true, true) |
| 289 | } |
| 290 | |
| 291 | func typeAssertStatus(n *node) { |
| 292 | typeAssert(n, false, true) |
| 293 | } |
| 294 | |
| 295 | func typeAssert(n *node, withResult, withOk bool) { |
| 296 | c0, c1 := n.child[0], n.child[1] |
| 297 | value := genValue(c0) // input value |
| 298 | var value0, value1 func(*frame) reflect.Value |
| 299 | setStatus := false |
| 300 | switch { |
| 301 | case withResult && withOk: |
| 302 | value0 = genValue(n.anc.child[0]) // returned result |
| 303 | value1 = genValue(n.anc.child[1]) // returned status |
| 304 | setStatus = n.anc.child[1].ident != "_" // do not assign status to "_" |
| 305 | case withResult && !withOk: |
| 306 | value0 = genValue(n) // returned result |
| 307 | case !withResult && withOk: |
| 308 | value1 = genValue(n.anc.child[1]) // returned status |
| 309 | setStatus = n.anc.child[1].ident != "_" // do not assign status to "_" |
| 310 | } |
| 311 | |
| 312 | typ := c1.typ // type to assert or convert to |
| 313 | typID := typ.id() |
| 314 | rtype := typ.refType(nil) // type to assert |
| 315 | next := getExec(n.tnext) |
| 316 | |
| 317 | switch { |
| 318 | case isInterfaceSrc(typ): |
| 319 | n.exec = func(f *frame) bltn { |
| 320 | valf := value(f) |
| 321 | v, ok := valf.Interface().(valueInterface) |
| 322 | if setStatus { |
| 323 | defer func() { |
| 324 | value1(f).SetBool(ok) |
| 325 | }() |
| 326 | } |
| 327 | if !ok { |
| 328 | if !withOk { |
| 329 | panic(n.cfgErrorf("interface conversion: nil is not %v", typID)) |
| 330 | } |
| 331 | return next |
| 332 | } |
| 333 | if c0.typ.cat == valueT { |
| 334 | valf = reflect.ValueOf(v) |
| 335 | } |
| 336 | if v.node.typ.id() == typID { |
| 337 | if withResult { |
| 338 | value0(f).Set(valf) |
| 339 | } |
| 340 | return next |
| 341 | } |
| 342 | m0 := v.node.typ.methods() |
| 343 | m1 := typ.methods() |
| 344 | if len(m0) < len(m1) { |
| 345 | ok = false |
| 346 | if !withOk { |
| 347 | panic(n.cfgErrorf("interface conversion: %v is not %v", v.node.typ.id(), typID)) |
| 348 | } |
| 349 | return next |
| 350 | } |
| 351 | |
| 352 | for k, meth1 := range m1 { |
| 353 | var meth0 string |
| 354 | meth0, ok = m0[k] |
| 355 | if !ok { |
| 356 | return next |
| 357 | } |
| 358 | // As far as we know this equality check can fail because they are two ways to |
| 359 | // represent the signature of a method: one where the receiver appears before the |
| 360 | // func keyword, and one where it is just a func signature, and the receiver is |
| 361 | // seen as the first argument. That's why if that equality fails, we try harder to |
| 362 | // compare them afterwards. Hopefully that is the only reason this equality can fail. |
| 363 | if meth0 == meth1 { |
| 364 | continue |
| 365 | } |
| 366 | tm := lookupFieldOrMethod(v.node.typ, k) |
| 367 | if tm == nil { |
| 368 | ok = false |
| 369 | return next |
| 370 | } |
| 371 | |
| 372 | var err error |
| 373 | meth0, err = stripReceiverFromArgs(meth0) |
| 374 | if err != nil { |
| 375 | ok = false |
| 376 | return next |
| 377 | } |
| 378 | |
| 379 | if meth0 != meth1 { |
| 380 | ok = false |
| 381 | return next |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if withResult { |
| 386 | value0(f).Set(valf) |
| 387 | } |
| 388 | return next |
| 389 | } |
| 390 | case isInterface(typ): |
| 391 | n.exec = func(f *frame) bltn { |
| 392 | var leftType reflect.Type |
| 393 | v := value(f) |
| 394 | val, ok := v.Interface().(valueInterface) |
| 395 | if setStatus { |
| 396 | defer func() { |
| 397 | value1(f).SetBool(ok) |
| 398 | }() |
| 399 | } |
| 400 | if ok && val.node.typ.cat != valueT { |
| 401 | m0 := val.node.typ.methods() |
| 402 | m1 := typ.methods() |
| 403 | if len(m0) < len(m1) { |
| 404 | ok = false |
| 405 | return next |
| 406 | } |
| 407 | |
| 408 | for k, meth1 := range m1 { |
| 409 | var meth0 string |
| 410 | meth0, ok = m0[k] |
| 411 | if !ok { |
| 412 | return next |
| 413 | } |
| 414 | if meth0 != meth1 { |
| 415 | ok = false |
| 416 | return next |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | if withResult { |
| 421 | value0(f).Set(genInterfaceWrapper(val.node, rtype)(f)) |
| 422 | } |
| 423 | ok = true |
| 424 | return next |
| 425 | } |
| 426 | |
| 427 | if ok { |
| 428 | v = val.value |
| 429 | leftType = val.node.typ.rtype |
| 430 | } else { |
| 431 | v = v.Elem() |
| 432 | leftType = v.Type() |
| 433 | ok = true |
| 434 | } |
| 435 | ok = v.IsValid() |
| 436 | if !ok { |
| 437 | if !withOk { |
| 438 | panic(n.cfgErrorf("interface conversion: interface {} is nil, not %s", rtype.String())) |
| 439 | } |
| 440 | return next |
| 441 | } |
| 442 | ok = canAssertTypes(leftType, rtype) |
| 443 | if !ok { |
| 444 | if !withOk { |
| 445 | method := firstMissingMethod(leftType, rtype) |
| 446 | panic(n.cfgErrorf("interface conversion: %s is not %s: missing method %s", leftType.String(), rtype.String(), method)) |
| 447 | } |
| 448 | return next |
| 449 | } |
| 450 | if withResult { |
| 451 | value0(f).Set(v) |
| 452 | } |
| 453 | return next |
| 454 | } |
| 455 | case isEmptyInterface(n.child[0].typ): |
| 456 | n.exec = func(f *frame) bltn { |
| 457 | var ok bool |
| 458 | if setStatus { |
| 459 | defer func() { |
| 460 | value1(f).SetBool(ok) |
| 461 | }() |
| 462 | } |
| 463 | val := value(f) |
| 464 | concrete := val.Interface() |
| 465 | ctyp := reflect.TypeOf(concrete) |
| 466 | |
| 467 | if vv, ok := concrete.(valueInterface); ok { |
| 468 | ctyp = vv.value.Type() |
| 469 | concrete = vv.value.Interface() |
| 470 | } |
| 471 | ok = canAssertTypes(ctyp, rtype) |
| 472 | if !ok { |
| 473 | if !withOk { |
| 474 | // TODO(mpl): think about whether this should ever happen. |
| 475 | if ctyp == nil { |
| 476 | panic(n.cfgErrorf("interface conversion: interface {} is nil, not %s", rtype.String())) |
| 477 | } |
| 478 | panic(n.cfgErrorf("interface conversion: interface {} is %s, not %s", ctyp.String(), rtype.String())) |
| 479 | } |
| 480 | return next |
| 481 | } |
| 482 | if withResult { |
| 483 | if isInterfaceSrc(typ) { |
| 484 | // TODO(mpl): this requires more work. the wrapped node is not complete enough. |
| 485 | value0(f).Set(reflect.ValueOf(valueInterface{n.child[0], reflect.ValueOf(concrete)})) |
| 486 | } else { |
| 487 | value0(f).Set(reflect.ValueOf(concrete)) |
| 488 | } |
| 489 | } |
| 490 | return next |
| 491 | } |
| 492 | case n.child[0].typ.cat == valueT || n.child[0].typ.cat == errorT: |
| 493 | n.exec = func(f *frame) bltn { |
| 494 | v := value(f).Elem() |
| 495 | ok := v.IsValid() |
| 496 | if setStatus { |
| 497 | defer func() { |
| 498 | value1(f).SetBool(ok) |
| 499 | }() |
| 500 | } |
| 501 | if !ok { |
| 502 | if !withOk { |
| 503 | panic(n.cfgErrorf("interface conversion: interface {} is nil, not %s", rtype.String())) |
| 504 | } |
| 505 | return next |
| 506 | } |
| 507 | v = valueInterfaceValue(v) |
| 508 | if vt := v.Type(); vt.Kind() == reflect.Struct && vt.Field(0).Name == "IValue" { |
| 509 | // Value is retrieved from an interface wrapper. |
| 510 | v = v.Field(0).Elem() |
| 511 | } |
| 512 | ok = canAssertTypes(v.Type(), rtype) |
| 513 | if !ok { |
| 514 | if !withOk { |
| 515 | method := firstMissingMethod(v.Type(), rtype) |
| 516 | panic(n.cfgErrorf("interface conversion: %s is not %s: missing method %s", v.Type().String(), rtype.String(), method)) |
| 517 | } |
| 518 | return next |
| 519 | } |
| 520 | if withResult { |
| 521 | value0(f).Set(v) |
| 522 | } |
| 523 | return next |
| 524 | } |
| 525 | default: |
| 526 | n.exec = func(f *frame) bltn { |
| 527 | v, ok := value(f).Interface().(valueInterface) |
| 528 | if setStatus { |
| 529 | defer func() { |
| 530 | value1(f).SetBool(ok) |
| 531 | }() |
| 532 | } |
| 533 | if !ok || !v.value.IsValid() { |
| 534 | ok = false |
| 535 | if !withOk { |
| 536 | panic(n.cfgErrorf("interface conversion: interface {} is nil, not %s", rtype.String())) |
| 537 | } |
| 538 | return next |
| 539 | } |
| 540 | |
| 541 | ok = canAssertTypes(v.value.Type(), rtype) |
| 542 | if !ok { |
| 543 | if !withOk { |
| 544 | panic(n.cfgErrorf("interface conversion: interface {} is %s, not %s", v.value.Type().String(), rtype.String())) |
| 545 | } |
| 546 | return next |
| 547 | } |
| 548 | if withResult { |
| 549 | value0(f).Set(v.value) |
| 550 | } |
| 551 | return next |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | func canAssertTypes(src, dest reflect.Type) bool { |
| 557 | if dest == nil { |
| 558 | return false |
| 559 | } |
| 560 | if src == dest { |
| 561 | return true |
| 562 | } |
| 563 | if dest.Kind() == reflect.Interface && src.Implements(dest) { |
| 564 | return true |
| 565 | } |
| 566 | if src == nil { |
| 567 | return false |
| 568 | } |
| 569 | if src.AssignableTo(dest) { |
| 570 | return true |
| 571 | } |
| 572 | return false |
| 573 | } |
| 574 | |
| 575 | func firstMissingMethod(src, dest reflect.Type) string { |
| 576 | for i := 0; i < dest.NumMethod(); i++ { |
| 577 | m := dest.Method(i).Name |
| 578 | if _, ok := src.MethodByName(m); !ok { |
| 579 | return m |
| 580 | } |
| 581 | } |
| 582 | return "" |
| 583 | } |
| 584 | |
| 585 | func convert(n *node) { |
| 586 | dest := genValue(n) |
| 587 | c := n.child[1] |
| 588 | typ := n.child[0].typ.frameType() |
| 589 | next := getExec(n.tnext) |
| 590 | |
| 591 | if c.isNil() { // convert nil to type |
| 592 | // TODO(mpl): Try to completely remove, as maybe frameType already does the job for interfaces. |
| 593 | if isInterfaceSrc(n.child[0].typ) && !isEmptyInterface(n.child[0].typ) { |
| 594 | typ = valueInterfaceType |
| 595 | } |
| 596 | n.exec = func(f *frame) bltn { |
| 597 | dest(f).Set(reflect.New(typ).Elem()) |
| 598 | return next |
| 599 | } |
| 600 | return |
| 601 | } |
| 602 | |
| 603 | doConvert := true |
| 604 | var value func(*frame) reflect.Value |
| 605 | switch { |
| 606 | case isFuncSrc(c.typ): |
| 607 | value = genFunctionWrapper(c) |
| 608 | default: |
| 609 | value = genValue(c) |
| 610 | } |
| 611 | |
| 612 | for _, con := range n.interp.hooks.convert { |
| 613 | if c.typ.rtype == nil { |
| 614 | continue |
| 615 | } |
| 616 | |
| 617 | fn := con(c.typ.rtype, typ) |
| 618 | if fn == nil { |
| 619 | continue |
| 620 | } |
| 621 | n.exec = func(f *frame) bltn { |
| 622 | fn(value(f), dest(f)) |
| 623 | return next |
| 624 | } |
| 625 | return |
| 626 | } |
| 627 | |
| 628 | n.exec = func(f *frame) bltn { |
| 629 | if doConvert { |
| 630 | dest(f).Set(value(f).Convert(typ)) |
| 631 | } else { |
| 632 | dest(f).Set(value(f)) |
| 633 | } |
| 634 | return next |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | // assignFromCall assigns values from a function call. |
| 639 | func assignFromCall(n *node) { |
| 640 | ncall := n.lastChild() |
| 641 | l := len(n.child) - 1 |
| 642 | if n.anc.kind == varDecl && n.child[l-1].isType(n.scope) { |
| 643 | // Ignore the type in the assignment if it is part of a variable declaration. |
| 644 | l-- |
| 645 | } |
| 646 | dvalue := make([]func(*frame) reflect.Value, l) |
| 647 | for i := range dvalue { |
| 648 | if n.child[i].ident == "_" { |
| 649 | continue |
| 650 | } |
| 651 | dvalue[i] = genValue(n.child[i]) |
| 652 | } |
| 653 | next := getExec(n.tnext) |
| 654 | n.exec = func(f *frame) bltn { |
| 655 | for i, v := range dvalue { |
| 656 | if v == nil { |
| 657 | continue |
| 658 | } |
| 659 | s := f.data[ncall.findex+i] |
| 660 | c := n.child[i] |
| 661 | if n.kind == defineXStmt && !c.redeclared { |
| 662 | // Recreate destination value in case of define statement, |
| 663 | // to preserve previous value possibly in use by a closure. |
| 664 | data := getFrame(f, c.level).data |
| 665 | data[c.findex] = reflect.New(data[c.findex].Type()).Elem() |
| 666 | data[c.findex].Set(s) |
| 667 | continue |
| 668 | } |
| 669 | v(f).Set(s) |
| 670 | } |
| 671 | return next |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | func assign(n *node) { |
| 676 | next := getExec(n.tnext) |
| 677 | dvalue := make([]func(*frame) reflect.Value, n.nleft) |
| 678 | ivalue := make([]func(*frame) reflect.Value, n.nleft) |
| 679 | svalue := make([]func(*frame) reflect.Value, n.nleft) |
| 680 | var sbase int |
| 681 | if n.nright > 0 { |
| 682 | sbase = len(n.child) - n.nright |
| 683 | } |
| 684 | |
| 685 | for i := 0; i < n.nleft; i++ { |
| 686 | dest, src := n.child[i], n.child[sbase+i] |
| 687 | if isNamedFuncSrc(src.typ) { |
| 688 | svalue[i] = genFuncValue(src) |
| 689 | } else { |
| 690 | svalue[i] = genDestValue(dest.typ, src) |
| 691 | } |
| 692 | if isMapEntry(dest) { |
| 693 | if isInterfaceSrc(dest.child[1].typ) { // key |
| 694 | ivalue[i] = genValueInterface(dest.child[1]) |
| 695 | } else { |
| 696 | ivalue[i] = genValue(dest.child[1]) |
| 697 | } |
| 698 | dvalue[i] = genValue(dest.child[0]) |
| 699 | } else { |
| 700 | dvalue[i] = genValue(dest) |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | if n.nleft == 1 { |
| 705 | // Single assign operation. |
| 706 | switch s, d, i := svalue[0], dvalue[0], ivalue[0]; { |
| 707 | case n.child[0].ident == "_": |
| 708 | n.exec = func(f *frame) bltn { |
| 709 | return next |
| 710 | } |
| 711 | case i != nil: |
| 712 | n.exec = func(f *frame) bltn { |
| 713 | d(f).SetMapIndex(i(f), s(f)) |
| 714 | return next |
| 715 | } |
| 716 | case n.kind == defineStmt: |
| 717 | l := n.level |
| 718 | ind := n.findex |
| 719 | n.exec = func(f *frame) bltn { |
| 720 | data := getFrame(f, l).data |
| 721 | data[ind] = reflect.New(data[ind].Type()).Elem() |
| 722 | data[ind].Set(s(f)) |
| 723 | return next |
| 724 | } |
| 725 | default: |
| 726 | n.exec = func(f *frame) bltn { |
| 727 | d(f).Set(s(f)) |
| 728 | return next |
| 729 | } |
| 730 | } |
| 731 | return |
| 732 | } |
| 733 | |
| 734 | // Multi assign operation. |
| 735 | types := make([]reflect.Type, n.nright) |
| 736 | index := make([]int, n.nright) |
| 737 | level := make([]int, n.nright) |
| 738 | |
| 739 | for i := range types { |
| 740 | var t reflect.Type |
| 741 | switch typ := n.child[sbase+i].typ; { |
| 742 | case isInterfaceSrc(typ): |
| 743 | t = valueInterfaceType |
| 744 | default: |
| 745 | t = typ.TypeOf() |
| 746 | } |
| 747 | types[i] = t |
| 748 | index[i] = n.child[i].findex |
| 749 | level[i] = n.child[i].level |
| 750 | } |
| 751 | |
| 752 | if n.kind == defineStmt { |
| 753 | // Handle a multiple var declararation / assign. It cannot be a swap. |
| 754 | n.exec = func(f *frame) bltn { |
| 755 | for i, s := range svalue { |
| 756 | if n.child[i].ident == "_" { |
| 757 | continue |
| 758 | } |
| 759 | data := getFrame(f, level[i]).data |
| 760 | j := index[i] |
| 761 | data[j] = reflect.New(data[j].Type()).Elem() |
| 762 | data[j].Set(s(f)) |
| 763 | } |
| 764 | return next |
| 765 | } |
| 766 | return |
| 767 | } |
| 768 | |
| 769 | // To handle possible swap in multi-assign: |
| 770 | // evaluate and copy all values in assign right hand side into temporary |
| 771 | // then evaluate assign left hand side and copy temporary into it |
| 772 | n.exec = func(f *frame) bltn { |
| 773 | t := make([]reflect.Value, len(svalue)) |
| 774 | for i, s := range svalue { |
| 775 | if n.child[i].ident == "_" { |
| 776 | continue |
| 777 | } |
| 778 | t[i] = reflect.New(types[i]).Elem() |
| 779 | t[i].Set(s(f)) |
| 780 | } |
| 781 | for i, d := range dvalue { |
| 782 | if n.child[i].ident == "_" { |
| 783 | continue |
| 784 | } |
| 785 | if j := ivalue[i]; j != nil { |
| 786 | d(f).SetMapIndex(j(f), t[i]) // Assign a map entry |
| 787 | } else { |
| 788 | d(f).Set(t[i]) // Assign a var or array/slice entry |
| 789 | } |
| 790 | } |
| 791 | return next |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | func not(n *node) { |
| 796 | dest := genValue(n) |
| 797 | value := genValue(n.child[0]) |
| 798 | tnext := getExec(n.tnext) |
| 799 | |
| 800 | if n.fnext != nil { |
| 801 | fnext := getExec(n.fnext) |
| 802 | n.exec = func(f *frame) bltn { |
| 803 | if !value(f).Bool() { |
| 804 | dest(f).SetBool(true) |
| 805 | return tnext |
| 806 | } |
| 807 | dest(f).SetBool(false) |
| 808 | return fnext |
| 809 | } |
| 810 | } else { |
| 811 | n.exec = func(f *frame) bltn { |
| 812 | dest(f).SetBool(!value(f).Bool()) |
| 813 | return tnext |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | func addr(n *node) { |
| 819 | dest := genValue(n) |
| 820 | next := getExec(n.tnext) |
| 821 | c0 := n.child[0] |
| 822 | value := genValue(c0) |
| 823 | |
| 824 | if isInterfaceSrc(c0.typ) || isPtrSrc(c0.typ) { |
| 825 | i := n.findex |
| 826 | l := n.level |
| 827 | n.exec = func(f *frame) bltn { |
| 828 | getFrame(f, l).data[i] = value(f).Addr() |
| 829 | return next |
| 830 | } |
| 831 | return |
| 832 | } |
| 833 | |
| 834 | n.exec = func(f *frame) bltn { |
| 835 | dest(f).Set(value(f).Addr()) |
| 836 | return next |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | func deref(n *node) { |
| 841 | value := genValue(n.child[0]) |
| 842 | tnext := getExec(n.tnext) |
| 843 | i := n.findex |
| 844 | l := n.level |
| 845 | |
| 846 | if n.fnext != nil { |
| 847 | fnext := getExec(n.fnext) |
| 848 | n.exec = func(f *frame) bltn { |
| 849 | r := value(f).Elem() |
| 850 | if r.Bool() { |
| 851 | getFrame(f, l).data[i] = r |
| 852 | return tnext |
| 853 | } |
| 854 | return fnext |
| 855 | } |
| 856 | } else { |
| 857 | n.exec = func(f *frame) bltn { |
| 858 | getFrame(f, l).data[i] = value(f).Elem() |
| 859 | return tnext |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | func _print(n *node) { |
| 865 | child := n.child[1:] |
| 866 | values := make([]func(*frame) reflect.Value, len(child)) |
| 867 | for i, c := range child { |
| 868 | values[i] = genValue(c) |
| 869 | } |
| 870 | out := n.interp.stdout |
| 871 | |
| 872 | genBuiltinDeferWrapper(n, values, nil, func(args []reflect.Value) []reflect.Value { |
| 873 | for i, value := range args { |
| 874 | if i > 0 { |
| 875 | fmt.Fprintf(out, " ") |
| 876 | } |
| 877 | fmt.Fprintf(out, "%v", value) |
| 878 | } |
| 879 | return nil |
| 880 | }) |
| 881 | } |
| 882 | |
| 883 | func _println(n *node) { |
| 884 | child := n.child[1:] |
| 885 | values := make([]func(*frame) reflect.Value, len(child)) |
| 886 | for i, c := range child { |
| 887 | values[i] = genValue(c) |
| 888 | } |
| 889 | out := n.interp.stdout |
| 890 | |
| 891 | genBuiltinDeferWrapper(n, values, nil, func(args []reflect.Value) []reflect.Value { |
| 892 | for i, value := range args { |
| 893 | if i > 0 { |
| 894 | fmt.Fprintf(out, " ") |
| 895 | } |
| 896 | fmt.Fprintf(out, "%v", value) |
| 897 | } |
| 898 | fmt.Fprintln(out, "") |
| 899 | return nil |
| 900 | }) |
| 901 | } |
| 902 | |
| 903 | func _recover(n *node) { |
| 904 | tnext := getExec(n.tnext) |
| 905 | dest := genValue(n) |
| 906 | |
| 907 | n.exec = func(f *frame) bltn { |
| 908 | if f.anc.recovered == nil { |
| 909 | // TODO(mpl): maybe we don't need that special case, and we're just forgetting to unwrap the valueInterface somewhere else. |
| 910 | if isEmptyInterface(n.typ) { |
| 911 | return tnext |
| 912 | } |
| 913 | dest(f).Set(reflect.ValueOf(valueInterface{})) |
| 914 | return tnext |
| 915 | } |
| 916 | |
| 917 | if isEmptyInterface(n.typ) { |
| 918 | dest(f).Set(reflect.ValueOf(f.anc.recovered)) |
| 919 | } else { |
| 920 | dest(f).Set(reflect.ValueOf(valueInterface{n, reflect.ValueOf(f.anc.recovered)})) |
| 921 | } |
| 922 | f.anc.recovered = nil |
| 923 | return tnext |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | func _panic(n *node) { |
| 928 | value := genValue(n.child[1]) |
| 929 | |
| 930 | n.exec = func(f *frame) bltn { |
| 931 | panic(value(f)) |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | func genBuiltinDeferWrapper(n *node, in, out []func(*frame) reflect.Value, fn func([]reflect.Value) []reflect.Value) { |
| 936 | next := getExec(n.tnext) |
| 937 | |
| 938 | if n.anc.kind == deferStmt { |
| 939 | n.exec = func(f *frame) bltn { |
| 940 | val := make([]reflect.Value, len(in)+1) |
| 941 | inTypes := make([]reflect.Type, len(in)) |
| 942 | for i, v := range in { |
| 943 | val[i+1] = v(f) |
| 944 | inTypes[i] = val[i+1].Type() |
| 945 | } |
| 946 | outTypes := make([]reflect.Type, len(out)) |
| 947 | for i, v := range out { |
| 948 | outTypes[i] = v(f).Type() |
| 949 | } |
| 950 | |
| 951 | funcType := reflect.FuncOf(inTypes, outTypes, false) |
| 952 | val[0] = reflect.MakeFunc(funcType, fn) |
| 953 | f.deferred = append([][]reflect.Value{val}, f.deferred...) |
| 954 | return next |
| 955 | } |
| 956 | return |
| 957 | } |
| 958 | |
| 959 | n.exec = func(f *frame) bltn { |
| 960 | val := make([]reflect.Value, len(in)) |
| 961 | for i, v := range in { |
| 962 | val[i] = v(f) |
| 963 | } |
| 964 | |
| 965 | dests := fn(val) |
| 966 | |
| 967 | for i, dest := range dests { |
| 968 | out[i](f).Set(dest) |
| 969 | } |
| 970 | return next |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | func genFunctionWrapper(n *node) func(*frame) reflect.Value { |
| 975 | var def *node |
| 976 | var ok bool |
| 977 | |
| 978 | if def, ok = n.val.(*node); !ok { |
| 979 | return genValueAsFunctionWrapper(n) |
| 980 | } |
| 981 | start := def.child[3].start |
| 982 | numRet := len(def.typ.ret) |
| 983 | var rcvr func(*frame) reflect.Value |
| 984 | |
| 985 | if n.recv != nil { |
| 986 | rcvr = genValueRecv(n) |
| 987 | } |
| 988 | funcType := n.typ.TypeOf() |
| 989 | |
| 990 | value := genValue(n) |
| 991 | var isDefer bool |
| 992 | if n.anc != nil && n.anc.anc != nil && n.anc.anc.kind == deferStmt { |
| 993 | isDefer = true |
| 994 | } |
| 995 | |
| 996 | return func(f *frame) reflect.Value { |
| 997 | v := value(f) |
| 998 | if !isDefer && v.Kind() == reflect.Func { |
| 999 | // fixes #1634, if v is already a func, then don't re-wrap |
| 1000 | // because original wrapping cloned the frame but this doesn't |
| 1001 | return v |
| 1002 | } |
| 1003 | |
| 1004 | return reflect.MakeFunc(funcType, func(in []reflect.Value) []reflect.Value { |
| 1005 | // Allocate and init local frame. All values to be settable and addressable. |
| 1006 | fr := newFrame(f, len(def.types), f.runid()) |
| 1007 | d := fr.data |
| 1008 | for i, t := range def.types { |
| 1009 | d[i] = reflect.New(t).Elem() |
| 1010 | } |
| 1011 | |
| 1012 | if rcvr == nil { |
| 1013 | d = d[numRet:] |
| 1014 | } else { |
| 1015 | // Copy method receiver as first argument. |
| 1016 | src, dest := rcvr(f), d[numRet] |
| 1017 | sk, dk := src.Kind(), dest.Kind() |
| 1018 | for { |
| 1019 | vs, ok := src.Interface().(valueInterface) |
| 1020 | if !ok { |
| 1021 | break |
| 1022 | } |
| 1023 | src = vs.value |
| 1024 | sk = src.Kind() |
| 1025 | } |
| 1026 | switch { |
| 1027 | case sk == reflect.Ptr && dk != reflect.Ptr: |
| 1028 | dest.Set(src.Elem()) |
| 1029 | case sk != reflect.Ptr && dk == reflect.Ptr: |
| 1030 | dest.Set(src.Addr()) |
| 1031 | default: |
| 1032 | dest.Set(src) |
| 1033 | } |
| 1034 | d = d[numRet+1:] |
| 1035 | } |
| 1036 | |
| 1037 | // Copy function input arguments in local frame. |
| 1038 | for i, arg := range in { |
| 1039 | if i >= len(d) { |
| 1040 | // In case of unused arg, there may be not even a frame entry allocated, just skip. |
| 1041 | break |
| 1042 | } |
| 1043 | typ := def.typ.arg[i] |
| 1044 | switch { |
| 1045 | case isEmptyInterface(typ) || typ.TypeOf() == valueInterfaceType: |
| 1046 | d[i].Set(arg) |
| 1047 | case isInterfaceSrc(typ): |
| 1048 | d[i].Set(reflect.ValueOf(valueInterface{value: arg.Elem()})) |
| 1049 | default: |
| 1050 | d[i].Set(arg) |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | // Interpreter code execution. |
| 1055 | runCfg(start, fr, def, n) |
| 1056 | |
| 1057 | return fr.data[:numRet] |
| 1058 | }) |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | func genInterfaceWrapper(n *node, typ reflect.Type) func(*frame) reflect.Value { |
| 1063 | value := genValue(n) |
| 1064 | if typ == nil || typ.Kind() != reflect.Interface || typ.NumMethod() == 0 || n.typ.cat == valueT { |
| 1065 | return value |
| 1066 | } |
| 1067 | tc := n.typ.cat |
| 1068 | if tc != structT { |
| 1069 | // Always force wrapper generation for struct types, as they may contain |
| 1070 | // embedded interface fields which require wrapping, even if reported as |
| 1071 | // implementing typ by reflect. |
| 1072 | if nt := n.typ.frameType(); nt != nil && nt.Implements(typ) { |
| 1073 | return value |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | // Retrieve methods from the interface wrapper, which is a struct where all fields |
| 1078 | // except the first define the methods to implement. |
| 1079 | // As the field name was generated with a prefixed first character (in order to avoid |
| 1080 | // collisions with method names), this first character is ignored in comparisons. |
| 1081 | wrap := getWrapper(n, typ) |
| 1082 | mn := wrap.NumField() - 1 |
| 1083 | names := make([]string, mn) |
| 1084 | methods := make([]*node, mn) |
| 1085 | indexes := make([][]int, mn) |
| 1086 | for i := 0; i < mn; i++ { |
| 1087 | names[i] = wrap.Field(i + 1).Name[1:] |
| 1088 | methods[i], indexes[i] = n.typ.lookupMethod(names[i]) |
| 1089 | if methods[i] == nil && n.typ.cat != nilT { |
| 1090 | // interpreted method not found, look for binary method, possibly embedded |
| 1091 | _, indexes[i], _, _ = n.typ.lookupBinMethod(names[i]) |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | return func(f *frame) reflect.Value { |
| 1096 | v := value(f) |
| 1097 | if tc != structT && v.Type().Implements(typ) { |
| 1098 | return v |
| 1099 | } |
| 1100 | switch v.Kind() { |
| 1101 | case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: |
| 1102 | if v.IsNil() { |
| 1103 | return reflect.New(typ).Elem() |
| 1104 | } |
| 1105 | } |
| 1106 | var n2 *node |
| 1107 | if vi, ok := v.Interface().(valueInterface); ok { |
| 1108 | n2 = vi.node |
| 1109 | } |
| 1110 | v = getConcreteValue(v) |
| 1111 | w := reflect.New(wrap).Elem() |
| 1112 | w.Field(0).Set(v) |
| 1113 | for i, m := range methods { |
| 1114 | if m == nil { |
| 1115 | // First direct method lookup on field. |
| 1116 | if r := methodByName(v, names[i], indexes[i]); r.IsValid() { |
| 1117 | w.Field(i + 1).Set(r) |
| 1118 | continue |
| 1119 | } |
| 1120 | if n2 == nil { |
| 1121 | panic(n.cfgErrorf("method not found: %s", names[i])) |
| 1122 | } |
| 1123 | // Method lookup in embedded valueInterface. |
| 1124 | m2, i2 := n2.typ.lookupMethod(names[i]) |
| 1125 | if m2 != nil { |
| 1126 | nod := *m2 |
| 1127 | nod.recv = &receiver{n, v, i2} |
| 1128 | w.Field(i + 1).Set(genFunctionWrapper(&nod)(f)) |
| 1129 | continue |
| 1130 | } |
| 1131 | panic(n.cfgErrorf("method not found: %s", names[i])) |
| 1132 | } |
| 1133 | nod := *m |
| 1134 | nod.recv = &receiver{n, v, indexes[i]} |
| 1135 | w.Field(i + 1).Set(genFunctionWrapper(&nod)(f)) |
| 1136 | } |
| 1137 | return w |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | // methodByName returns the method corresponding to name on value, or nil if not found. |
| 1142 | // The search is extended on valueInterface wrapper if present. |
| 1143 | // If valid, the returned value is a method function with the receiver already set |
| 1144 | // (no need to pass it at call). |
| 1145 | func methodByName(value reflect.Value, name string, index []int) (v reflect.Value) { |
| 1146 | if vi, ok := value.Interface().(valueInterface); ok { |
| 1147 | if v = getConcreteValue(vi.value).MethodByName(name); v.IsValid() { |
| 1148 | return |
| 1149 | } |
| 1150 | } |
| 1151 | if v = value.MethodByName(name); v.IsValid() { |
| 1152 | return |
| 1153 | } |
| 1154 | for value.Kind() == reflect.Ptr { |
| 1155 | value = value.Elem() |
| 1156 | if checkFieldIndex(value.Type(), index) { |
| 1157 | value = value.FieldByIndex(index) |
| 1158 | } |
| 1159 | if v = value.MethodByName(name); v.IsValid() { |
| 1160 | return |
| 1161 | } |
| 1162 | } |
| 1163 | return |
| 1164 | } |
| 1165 | |
| 1166 | func checkFieldIndex(typ reflect.Type, index []int) bool { |
| 1167 | if len(index) == 0 { |
| 1168 | return false |
| 1169 | } |
| 1170 | t := typ |
| 1171 | for t.Kind() == reflect.Ptr { |
| 1172 | t = t.Elem() |
| 1173 | } |
| 1174 | if t.Kind() != reflect.Struct { |
| 1175 | return false |
| 1176 | } |
| 1177 | i := index[0] |
| 1178 | if i >= t.NumField() { |
| 1179 | return false |
| 1180 | } |
| 1181 | if len(index) > 1 { |
| 1182 | return checkFieldIndex(t.Field(i).Type, index[1:]) |
| 1183 | } |
| 1184 | return true |
| 1185 | } |
| 1186 | |
| 1187 | func call(n *node) { |
| 1188 | goroutine := n.anc.kind == goStmt |
| 1189 | c0 := n.child[0] |
| 1190 | value := genValue(c0) |
| 1191 | var values []func(*frame) reflect.Value |
| 1192 | |
| 1193 | numRet := len(c0.typ.ret) |
| 1194 | variadic := variadicPos(n) |
| 1195 | child := n.child[1:] |
| 1196 | tnext := getExec(n.tnext) |
| 1197 | fnext := getExec(n.fnext) |
| 1198 | hasVariadicArgs := n.action == aCallSlice // callSlice implies variadic call with ellipsis. |
| 1199 | |
| 1200 | // Compute input argument value functions. |
| 1201 | for i, c := range child { |
| 1202 | var arg *itype |
| 1203 | if variadic >= 0 && i >= variadic { |
| 1204 | arg = c0.typ.arg[variadic].val |
| 1205 | } else { |
| 1206 | arg = c0.typ.arg[i] |
| 1207 | } |
| 1208 | switch { |
| 1209 | case isBinCall(c, c.scope): |
| 1210 | // Handle nested function calls: pass returned values as arguments. |
| 1211 | numOut := c.child[0].typ.rtype.NumOut() |
| 1212 | for j := 0; j < numOut; j++ { |
| 1213 | ind := c.findex + j |
| 1214 | if hasVariadicArgs || !isInterfaceSrc(arg) || isEmptyInterface(arg) { |
| 1215 | values = append(values, func(f *frame) reflect.Value { return f.data[ind] }) |
| 1216 | continue |
| 1217 | } |
| 1218 | values = append(values, func(f *frame) reflect.Value { |
| 1219 | return reflect.ValueOf(valueInterface{value: f.data[ind]}) |
| 1220 | }) |
| 1221 | } |
| 1222 | case isRegularCall(c): |
| 1223 | // Arguments are return values of a nested function call. |
| 1224 | cc0 := c.child[0] |
| 1225 | for j := range cc0.typ.ret { |
| 1226 | ind := c.findex + j |
| 1227 | if hasVariadicArgs || !isInterfaceSrc(arg) || isEmptyInterface(arg) { |
| 1228 | values = append(values, func(f *frame) reflect.Value { return f.data[ind] }) |
| 1229 | continue |
| 1230 | } |
| 1231 | values = append(values, func(f *frame) reflect.Value { |
| 1232 | return reflect.ValueOf(valueInterface{node: cc0.typ.ret[j].node, value: f.data[ind]}) |
| 1233 | }) |
| 1234 | } |
| 1235 | default: |
| 1236 | if c.kind == basicLit || c.rval.IsValid() { |
| 1237 | argType := arg.TypeOf() |
| 1238 | convertLiteralValue(c, argType) |
| 1239 | } |
| 1240 | switch { |
| 1241 | case hasVariadicArgs: |
| 1242 | values = append(values, genValue(c)) |
| 1243 | case isInterfaceSrc(arg) && (!isEmptyInterface(arg) || len(c.typ.method) > 0): |
| 1244 | values = append(values, genValueInterface(c)) |
| 1245 | case isInterfaceBin(arg): |
| 1246 | values = append(values, genInterfaceWrapper(c, arg.rtype)) |
| 1247 | case isFuncSrc(arg): |
| 1248 | values = append(values, genFuncValue(c)) |
| 1249 | default: |
| 1250 | values = append(values, genValue(c)) |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | // Compute output argument value functions. |
| 1256 | rtypes := c0.typ.ret |
| 1257 | rvalues := make([]func(*frame) reflect.Value, len(rtypes)) |
| 1258 | switch n.anc.kind { |
| 1259 | case defineXStmt, assignXStmt: |
| 1260 | l := n.level |
| 1261 | for i := range rvalues { |
| 1262 | c := n.anc.child[i] |
| 1263 | switch { |
| 1264 | case c.ident == "_": |
| 1265 | // Skip assigning return value to blank var. |
| 1266 | case isInterfaceSrc(c.typ) && !isEmptyInterface(c.typ) && !isInterfaceSrc(rtypes[i]): |
| 1267 | rvalues[i] = genValueInterfaceValue(c) |
| 1268 | default: |
| 1269 | j := n.findex + i |
| 1270 | rvalues[i] = func(f *frame) reflect.Value { return getFrame(f, l).data[j] } |
| 1271 | } |
| 1272 | } |
| 1273 | case returnStmt: |
| 1274 | // Function call from a return statement: forward return values (always at frame start). |
| 1275 | for i := range rtypes { |
| 1276 | j := n.findex + i |
| 1277 | // Set the return value location in return value of caller frame. |
| 1278 | rvalues[i] = func(f *frame) reflect.Value { return f.data[j] } |
| 1279 | } |
| 1280 | default: |
| 1281 | // Multiple return values frame index are indexed from the node frame index. |
| 1282 | l := n.level |
| 1283 | for i := range rtypes { |
| 1284 | j := n.findex + i |
| 1285 | rvalues[i] = func(f *frame) reflect.Value { return getFrame(f, l).data[j] } |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | if n.anc.kind == deferStmt { |
| 1290 | // Store function call in frame for deferred execution. |
| 1291 | value = genFunctionWrapper(c0) |
| 1292 | n.exec = func(f *frame) bltn { |
| 1293 | val := make([]reflect.Value, len(values)+1) |
| 1294 | val[0] = value(f) |
| 1295 | for i, v := range values { |
| 1296 | val[i+1] = v(f) |
| 1297 | } |
| 1298 | f.deferred = append([][]reflect.Value{val}, f.deferred...) |
| 1299 | return tnext |
| 1300 | } |
| 1301 | return |
| 1302 | } |
| 1303 | |
| 1304 | n.exec = func(f *frame) bltn { |
| 1305 | f.mutex.Lock() |
| 1306 | bf := value(f) |
| 1307 | def, ok := bf.Interface().(*node) |
| 1308 | if ok { |
| 1309 | bf = def.rval |
| 1310 | } |
| 1311 | f.mutex.Unlock() |
| 1312 | |
| 1313 | // Call bin func if defined |
| 1314 | if bf.IsValid() { |
| 1315 | var callf func([]reflect.Value) []reflect.Value |
| 1316 | |
| 1317 | // Lambda definitions are necessary here. Due to reflect internals, |
| 1318 | // having `callf = bf.Call` or `callf = bf.CallSlice` does not work. |
| 1319 | //nolint:gocritic |
| 1320 | if hasVariadicArgs { |
| 1321 | callf = func(in []reflect.Value) []reflect.Value { return bf.CallSlice(in) } |
| 1322 | } else { |
| 1323 | callf = func(in []reflect.Value) []reflect.Value { return bf.Call(in) } |
| 1324 | } |
| 1325 | |
| 1326 | if goroutine { |
| 1327 | // Goroutine's arguments should be copied. |
| 1328 | in := make([]reflect.Value, len(values)) |
| 1329 | for i, v := range values { |
| 1330 | value := v(f) |
| 1331 | in[i] = reflect.New(value.Type()).Elem() |
| 1332 | in[i].Set(value) |
| 1333 | } |
| 1334 | |
| 1335 | go callf(in) |
| 1336 | return tnext |
| 1337 | } |
| 1338 | |
| 1339 | in := make([]reflect.Value, len(values)) |
| 1340 | for i, v := range values { |
| 1341 | in[i] = v(f) |
| 1342 | } |
| 1343 | out := callf(in) |
| 1344 | for i, v := range rvalues { |
| 1345 | if v != nil { |
| 1346 | v(f).Set(out[i]) |
| 1347 | } |
| 1348 | } |
| 1349 | if fnext != nil && !out[0].Bool() { |
| 1350 | return fnext |
| 1351 | } |
| 1352 | return tnext |
| 1353 | } |
| 1354 | |
| 1355 | nf := newFrame(f, len(def.types), f.runid()) |
| 1356 | var vararg reflect.Value |
| 1357 | |
| 1358 | // Init return values |
| 1359 | for i, v := range rvalues { |
| 1360 | if v != nil { |
| 1361 | nf.data[i] = v(f) |
| 1362 | } else { |
| 1363 | nf.data[i] = reflect.New(def.types[i]).Elem() |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | // Init local frame values |
| 1368 | for i, t := range def.types[numRet:] { |
| 1369 | nf.data[numRet+i] = reflect.New(t).Elem() |
| 1370 | } |
| 1371 | |
| 1372 | // Init variadic argument vector |
| 1373 | if variadic >= 0 { |
| 1374 | vararg = nf.data[numRet+variadic] |
| 1375 | } |
| 1376 | |
| 1377 | // Copy input parameters from caller |
| 1378 | if dest := nf.data[numRet:]; len(dest) > 0 { |
| 1379 | for i, v := range values { |
| 1380 | switch { |
| 1381 | case variadic >= 0 && i >= variadic: |
| 1382 | if v(f).Type() == vararg.Type() { |
| 1383 | vararg.Set(v(f)) |
| 1384 | } else { |
| 1385 | vararg.Set(reflect.Append(vararg, v(f))) |
| 1386 | } |
| 1387 | default: |
| 1388 | val := v(f) |
| 1389 | if val.IsZero() && dest[i].Kind() != reflect.Interface { |
| 1390 | // Work around a recursive struct zero interface issue. |
| 1391 | // Once there is a better way to handle this case, the dest can just be set. |
| 1392 | continue |
| 1393 | } |
| 1394 | if nod, ok := val.Interface().(*node); ok && nod.recv != nil { |
| 1395 | // An interpreted method is passed as value in a function call. |
| 1396 | // It must be wrapped now, otherwise the receiver will be missing |
| 1397 | // at the method call (#1332). |
| 1398 | // TODO (marc): wrapping interpreted functions should be always done |
| 1399 | // everywhere at runtime to simplify the whole code, |
| 1400 | // but it requires deeper refactoring. |
| 1401 | dest[i] = genFunctionWrapper(nod)(f) |
| 1402 | continue |
| 1403 | } |
| 1404 | dest[i].Set(val) |
| 1405 | } |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | // Execute function body |
| 1410 | if goroutine { |
| 1411 | go runCfg(def.child[3].start, nf, def, n) |
| 1412 | return tnext |
| 1413 | } |
| 1414 | runCfg(def.child[3].start, nf, def, n) |
| 1415 | |
| 1416 | // Set return values |
| 1417 | for i, v := range rvalues { |
| 1418 | if v != nil { |
| 1419 | v(f).Set(nf.data[i]) |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | // Handle branching according to boolean result |
| 1424 | if fnext != nil && !nf.data[0].Bool() { |
| 1425 | return fnext |
| 1426 | } |
| 1427 | return tnext |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | func getFrame(f *frame, l int) *frame { |
| 1432 | switch l { |
| 1433 | case globalFrame: |
| 1434 | return f.root |
| 1435 | case 0: |
| 1436 | return f |
| 1437 | case 1: |
| 1438 | return f.anc |
| 1439 | case 2: |
| 1440 | return f.anc.anc |
| 1441 | } |
| 1442 | for ; l > 0; l-- { |
| 1443 | f = f.anc |
| 1444 | } |
| 1445 | return f |
| 1446 | } |
| 1447 | |
| 1448 | // Callbin calls a function from a bin import, accessible through reflect. |
| 1449 | func callBin(n *node) { |
| 1450 | tnext := getExec(n.tnext) |
| 1451 | fnext := getExec(n.fnext) |
| 1452 | child := n.child[1:] |
| 1453 | c0 := n.child[0] |
| 1454 | value := genValue(c0) |
| 1455 | var values []func(*frame) reflect.Value |
| 1456 | funcType := c0.typ.rtype |
| 1457 | wt := wrappedType(c0) |
| 1458 | variadic := -1 |
| 1459 | if funcType.IsVariadic() { |
| 1460 | variadic = funcType.NumIn() - 1 |
| 1461 | } |
| 1462 | // A method signature obtained from reflect.Type includes receiver as 1st arg, except for interface types. |
| 1463 | rcvrOffset := 0 |
| 1464 | if recv := c0.recv; recv != nil && !isInterface(recv.node.typ) { |
| 1465 | if variadic > 0 || funcType.NumIn() > len(child) { |
| 1466 | rcvrOffset = 1 |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | // getMapType returns a reflect type suitable for interface wrapper for functions |
| 1471 | // with some special processing in case of interface{} argument, i.e. fmt.Printf. |
| 1472 | var getMapType func(*itype) reflect.Type |
| 1473 | if lr, ok := n.interp.mapTypes[c0.rval]; ok { |
| 1474 | getMapType = func(typ *itype) reflect.Type { |
| 1475 | for _, rt := range lr { |
| 1476 | if typ.implements(&itype{cat: valueT, rtype: rt}) { |
| 1477 | return rt |
| 1478 | } |
| 1479 | } |
| 1480 | return nil |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | // Determine if we should use `Call` or `CallSlice` on the function Value. |
| 1485 | callFn := func(v reflect.Value, in []reflect.Value) []reflect.Value { return v.Call(in) } |
| 1486 | if n.action == aCallSlice { |
| 1487 | callFn = func(v reflect.Value, in []reflect.Value) []reflect.Value { return v.CallSlice(in) } |
| 1488 | } |
| 1489 | |
| 1490 | for i, c := range child { |
| 1491 | switch { |
| 1492 | case isBinCall(c, c.scope): |
| 1493 | // Handle nested function calls: pass returned values as arguments |
| 1494 | numOut := c.child[0].typ.rtype.NumOut() |
| 1495 | for j := 0; j < numOut; j++ { |
| 1496 | ind := c.findex + j |
| 1497 | values = append(values, func(f *frame) reflect.Value { return valueInterfaceValue(f.data[ind]) }) |
| 1498 | } |
| 1499 | case isRegularCall(c): |
| 1500 | // Handle nested function calls: pass returned values as arguments |
| 1501 | for j := range c.child[0].typ.ret { |
| 1502 | ind := c.findex + j |
| 1503 | values = append(values, func(f *frame) reflect.Value { return valueInterfaceValue(f.data[ind]) }) |
| 1504 | } |
| 1505 | default: |
| 1506 | if c.kind == basicLit || c.rval.IsValid() { |
| 1507 | // Convert literal value (untyped) to function argument type (if not an interface{}) |
| 1508 | var argType reflect.Type |
| 1509 | if variadic >= 0 && i+rcvrOffset >= variadic { |
| 1510 | argType = funcType.In(variadic).Elem() |
| 1511 | } else { |
| 1512 | argType = funcType.In(i + rcvrOffset) |
| 1513 | } |
| 1514 | convertLiteralValue(c, argType) |
| 1515 | if !reflect.ValueOf(c.val).IsValid() { // Handle "nil" |
| 1516 | c.val = reflect.Zero(argType) |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | if wt != nil && isInterfaceSrc(wt.arg[i]) { |
| 1521 | values = append(values, genValueInterface(c)) |
| 1522 | break |
| 1523 | } |
| 1524 | |
| 1525 | // defType is the target type for a potential interface wrapper. |
| 1526 | var defType reflect.Type |
| 1527 | if variadic >= 0 && i+rcvrOffset >= variadic { |
| 1528 | defType = funcType.In(variadic) |
| 1529 | } else { |
| 1530 | defType = funcType.In(rcvrOffset + i) |
| 1531 | } |
| 1532 | if getMapType != nil { |
| 1533 | if rt := getMapType(c.typ); rt != nil { |
| 1534 | defType = rt |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | switch { |
| 1539 | case isEmptyInterface(c.typ): |
| 1540 | values = append(values, genValue(c)) |
| 1541 | case isInterfaceSrc(c.typ): |
| 1542 | values = append(values, genValueInterfaceValue(c)) |
| 1543 | case isFuncSrc(c.typ): |
| 1544 | values = append(values, genFunctionWrapper(c)) |
| 1545 | case c.typ.cat == arrayT || c.typ.cat == variadicT: |
| 1546 | if isEmptyInterface(c.typ.val) { |
| 1547 | values = append(values, genValueArray(c)) |
| 1548 | } else { |
| 1549 | values = append(values, genInterfaceWrapper(c, defType)) |
| 1550 | } |
| 1551 | case isPtrSrc(c.typ): |
| 1552 | if c.typ.val.cat == valueT { |
| 1553 | values = append(values, genValue(c)) |
| 1554 | } else { |
| 1555 | values = append(values, genInterfaceWrapper(c, defType)) |
| 1556 | } |
| 1557 | case c.typ.cat == valueT: |
| 1558 | values = append(values, genValue(c)) |
| 1559 | default: |
| 1560 | values = append(values, genInterfaceWrapper(c, defType)) |
| 1561 | } |
| 1562 | } |
| 1563 | } |
| 1564 | l := len(values) |
| 1565 | |
| 1566 | switch { |
| 1567 | case n.anc.kind == deferStmt: |
| 1568 | // Store function call in frame for deferred execution. |
| 1569 | n.exec = func(f *frame) bltn { |
| 1570 | val := make([]reflect.Value, l+1) |
| 1571 | val[0] = value(f) |
| 1572 | for i, v := range values { |
| 1573 | val[i+1] = getBinValue(getMapType, v, f) |
| 1574 | } |
| 1575 | f.deferred = append([][]reflect.Value{val}, f.deferred...) |
| 1576 | return tnext |
| 1577 | } |
| 1578 | case n.anc.kind == goStmt: |
| 1579 | // Execute function in a goroutine, discard results. |
| 1580 | n.exec = func(f *frame) bltn { |
| 1581 | in := make([]reflect.Value, l) |
| 1582 | for i, v := range values { |
| 1583 | in[i] = getBinValue(getMapType, v, f) |
| 1584 | } |
| 1585 | go callFn(value(f), in) |
| 1586 | return tnext |
| 1587 | } |
| 1588 | case fnext != nil: |
| 1589 | // Handle branching according to boolean result. |
| 1590 | index := n.findex |
| 1591 | level := n.level |
| 1592 | n.exec = func(f *frame) bltn { |
| 1593 | in := make([]reflect.Value, l) |
| 1594 | for i, v := range values { |
| 1595 | in[i] = getBinValue(getMapType, v, f) |
| 1596 | } |
| 1597 | res := callFn(value(f), in) |
| 1598 | b := res[0].Bool() |
| 1599 | getFrame(f, level).data[index].SetBool(b) |
| 1600 | if b { |
| 1601 | return tnext |
| 1602 | } |
| 1603 | return fnext |
| 1604 | } |
| 1605 | default: |
| 1606 | switch n.anc.action { |
| 1607 | case aAssignX: |
| 1608 | // The function call is part of an assign expression, store results direcly |
| 1609 | // to assigned location, to avoid an additional frame copy. |
| 1610 | // The optimization of aAssign is handled in assign(), and should not |
| 1611 | // be handled here. |
| 1612 | rvalues := make([]func(*frame) reflect.Value, funcType.NumOut()) |
| 1613 | for i := range rvalues { |
| 1614 | c := n.anc.child[i] |
| 1615 | if c.ident == "_" { |
| 1616 | continue |
| 1617 | } |
| 1618 | if isInterfaceSrc(c.typ) { |
| 1619 | rvalues[i] = genValueInterfaceValue(c) |
| 1620 | } else { |
| 1621 | rvalues[i] = genValue(c) |
| 1622 | } |
| 1623 | } |
| 1624 | n.exec = func(f *frame) bltn { |
| 1625 | in := make([]reflect.Value, l) |
| 1626 | for i, v := range values { |
| 1627 | in[i] = getBinValue(getMapType, v, f) |
| 1628 | } |
| 1629 | out := callFn(value(f), in) |
| 1630 | for i, v := range rvalues { |
| 1631 | if v == nil { |
| 1632 | continue // Skip assign "_". |
| 1633 | } |
| 1634 | c := n.anc.child[i] |
| 1635 | if n.anc.kind == defineXStmt && !c.redeclared { |
| 1636 | // In case of a define statement, the destination value in the frame |
| 1637 | // must be recreated. This is necessary to preserve the previous value |
| 1638 | // which may be still used in a separate closure. |
| 1639 | data := getFrame(f, c.level).data |
| 1640 | data[c.findex] = reflect.New(data[c.findex].Type()).Elem() |
| 1641 | data[c.findex].Set(out[i]) |
| 1642 | continue |
| 1643 | } |
| 1644 | v(f).Set(out[i]) |
| 1645 | } |
| 1646 | return tnext |
| 1647 | } |
| 1648 | case aReturn: |
| 1649 | // The function call is part of a return statement, store output results |
| 1650 | // directly in the frame location of outputs of the current function. |
| 1651 | b := childPos(n) |
| 1652 | n.exec = func(f *frame) bltn { |
| 1653 | in := make([]reflect.Value, l) |
| 1654 | for i, v := range values { |
| 1655 | in[i] = getBinValue(getMapType, v, f) |
| 1656 | } |
| 1657 | out := callFn(value(f), in) |
| 1658 | for i, v := range out { |
| 1659 | dest := f.data[b+i] |
| 1660 | if _, ok := dest.Interface().(valueInterface); ok { |
| 1661 | v = reflect.ValueOf(valueInterface{value: v}) |
| 1662 | } |
| 1663 | dest.Set(v) |
| 1664 | } |
| 1665 | return tnext |
| 1666 | } |
| 1667 | default: |
| 1668 | n.exec = func(f *frame) bltn { |
| 1669 | in := make([]reflect.Value, l) |
| 1670 | for i, v := range values { |
| 1671 | in[i] = getBinValue(getMapType, v, f) |
| 1672 | } |
| 1673 | out := callFn(value(f), in) |
| 1674 | for i := 0; i < len(out); i++ { |
| 1675 | r := out[i] |
| 1676 | if r.Kind() == reflect.Func { |
| 1677 | getFrame(f, n.level).data[n.findex+i] = r |
| 1678 | continue |
| 1679 | } |
| 1680 | dest := getFrame(f, n.level).data[n.findex+i] |
| 1681 | if _, ok := dest.Interface().(valueInterface); ok { |
| 1682 | r = reflect.ValueOf(valueInterface{value: r}) |
| 1683 | } |
| 1684 | dest.Set(r) |
| 1685 | } |
| 1686 | return tnext |
| 1687 | } |
| 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | func getIndexBinMethod(n *node) { |
| 1693 | // dest := genValue(n) |
| 1694 | i := n.findex |
| 1695 | l := n.level |
| 1696 | m := n.val.(int) |
| 1697 | value := genValue(n.child[0]) |
| 1698 | next := getExec(n.tnext) |
| 1699 | |
| 1700 | n.exec = func(f *frame) bltn { |
| 1701 | // Can not use .Set() because dest type contains the receiver and source not |
| 1702 | // dest(f).Set(value(f).Method(m)) |
| 1703 | getFrame(f, l).data[i] = value(f).Method(m) |
| 1704 | return next |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | func getIndexBinElemMethod(n *node) { |
| 1709 | i := n.findex |
| 1710 | l := n.level |
| 1711 | m := n.val.(int) |
| 1712 | value := genValue(n.child[0]) |
| 1713 | next := getExec(n.tnext) |
| 1714 | |
| 1715 | n.exec = func(f *frame) bltn { |
| 1716 | // Can not use .Set() because dest type contains the receiver and source not |
| 1717 | getFrame(f, l).data[i] = value(f).Elem().Method(m) |
| 1718 | return next |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | func getIndexBinPtrMethod(n *node) { |
| 1723 | i := n.findex |
| 1724 | l := n.level |
| 1725 | m := n.val.(int) |
| 1726 | value := genValue(n.child[0]) |
| 1727 | next := getExec(n.tnext) |
| 1728 | |
| 1729 | n.exec = func(f *frame) bltn { |
| 1730 | // Can not use .Set() because dest type contains the receiver and source not |
| 1731 | getFrame(f, l).data[i] = value(f).Addr().Method(m) |
| 1732 | return next |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | // getIndexArray returns array value from index. |
| 1737 | func getIndexArray(n *node) { |
| 1738 | tnext := getExec(n.tnext) |
| 1739 | value0 := genValueArray(n.child[0]) // array |
| 1740 | i := n.findex |
| 1741 | l := n.level |
| 1742 | |
| 1743 | if n.child[1].rval.IsValid() { // constant array index |
| 1744 | ai := int(vInt(n.child[1].rval)) |
| 1745 | if n.fnext != nil { |
| 1746 | fnext := getExec(n.fnext) |
| 1747 | n.exec = func(f *frame) bltn { |
| 1748 | r := value0(f).Index(ai) |
| 1749 | getFrame(f, l).data[i] = r |
| 1750 | if r.Bool() { |
| 1751 | return tnext |
| 1752 | } |
| 1753 | return fnext |
| 1754 | } |
| 1755 | } else { |
| 1756 | n.exec = func(f *frame) bltn { |
| 1757 | getFrame(f, l).data[i] = value0(f).Index(ai) |
| 1758 | return tnext |
| 1759 | } |
| 1760 | } |
| 1761 | } else { |
| 1762 | value1 := genValueInt(n.child[1]) // array index |
| 1763 | |
| 1764 | if n.fnext != nil { |
| 1765 | fnext := getExec(n.fnext) |
| 1766 | n.exec = func(f *frame) bltn { |
| 1767 | _, vi := value1(f) |
| 1768 | r := value0(f).Index(int(vi)) |
| 1769 | getFrame(f, l).data[i] = r |
| 1770 | if r.Bool() { |
| 1771 | return tnext |
| 1772 | } |
| 1773 | return fnext |
| 1774 | } |
| 1775 | } else { |
| 1776 | n.exec = func(f *frame) bltn { |
| 1777 | _, vi := value1(f) |
| 1778 | getFrame(f, l).data[i] = value0(f).Index(int(vi)) |
| 1779 | return tnext |
| 1780 | } |
| 1781 | } |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | // getIndexMap retrieves map value from index. |
| 1786 | func getIndexMap(n *node) { |
| 1787 | dest := genValue(n) |
| 1788 | value0 := genValue(n.child[0]) // map |
| 1789 | tnext := getExec(n.tnext) |
| 1790 | z := reflect.New(n.child[0].typ.frameType().Elem()).Elem() |
| 1791 | |
| 1792 | if n.child[1].rval.IsValid() { // constant map index |
| 1793 | mi := n.child[1].rval |
| 1794 | |
| 1795 | switch { |
| 1796 | case n.fnext != nil: |
| 1797 | fnext := getExec(n.fnext) |
| 1798 | n.exec = func(f *frame) bltn { |
| 1799 | if v := value0(f).MapIndex(mi); v.IsValid() && v.Bool() { |
| 1800 | dest(f).SetBool(true) |
| 1801 | return tnext |
| 1802 | } |
| 1803 | dest(f).Set(z) |
| 1804 | return fnext |
| 1805 | } |
| 1806 | default: |
| 1807 | n.exec = func(f *frame) bltn { |
| 1808 | if v := value0(f).MapIndex(mi); v.IsValid() { |
| 1809 | dest(f).Set(v) |
| 1810 | } else { |
| 1811 | dest(f).Set(z) |
| 1812 | } |
| 1813 | return tnext |
| 1814 | } |
| 1815 | } |
| 1816 | } else { |
| 1817 | value1 := genValue(n.child[1]) // map index |
| 1818 | |
| 1819 | switch { |
| 1820 | case n.fnext != nil: |
| 1821 | fnext := getExec(n.fnext) |
| 1822 | n.exec = func(f *frame) bltn { |
| 1823 | if v := value0(f).MapIndex(value1(f)); v.IsValid() && v.Bool() { |
| 1824 | dest(f).SetBool(true) |
| 1825 | return tnext |
| 1826 | } |
| 1827 | dest(f).Set(z) |
| 1828 | return fnext |
| 1829 | } |
| 1830 | default: |
| 1831 | n.exec = func(f *frame) bltn { |
| 1832 | if v := value0(f).MapIndex(value1(f)); v.IsValid() { |
| 1833 | dest(f).Set(v) |
| 1834 | } else { |
| 1835 | dest(f).Set(z) |
| 1836 | } |
| 1837 | return tnext |
| 1838 | } |
| 1839 | } |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | // getIndexMap2 retrieves map value from index and set status. |
| 1844 | func getIndexMap2(n *node) { |
| 1845 | dest := genValue(n.anc.child[0]) // result |
| 1846 | value0 := genValue(n.child[0]) // map |
| 1847 | value2 := genValue(n.anc.child[1]) // status |
| 1848 | next := getExec(n.tnext) |
| 1849 | doValue := n.anc.child[0].ident != "_" |
| 1850 | doStatus := n.anc.child[1].ident != "_" |
| 1851 | |
| 1852 | if !doValue && !doStatus { |
| 1853 | nop(n) |
| 1854 | return |
| 1855 | } |
| 1856 | if n.child[1].rval.IsValid() { // constant map index |
| 1857 | mi := n.child[1].rval |
| 1858 | switch { |
| 1859 | case !doValue: |
| 1860 | n.exec = func(f *frame) bltn { |
| 1861 | v := value0(f).MapIndex(mi) |
| 1862 | value2(f).SetBool(v.IsValid()) |
| 1863 | return next |
| 1864 | } |
| 1865 | default: |
| 1866 | n.exec = func(f *frame) bltn { |
| 1867 | v := value0(f).MapIndex(mi) |
| 1868 | if v.IsValid() { |
| 1869 | dest(f).Set(v) |
| 1870 | } |
| 1871 | if doStatus { |
| 1872 | value2(f).SetBool(v.IsValid()) |
| 1873 | } |
| 1874 | return next |
| 1875 | } |
| 1876 | } |
| 1877 | } else { |
| 1878 | value1 := genValue(n.child[1]) // map index |
| 1879 | switch { |
| 1880 | case !doValue: |
| 1881 | n.exec = func(f *frame) bltn { |
| 1882 | v := value0(f).MapIndex(value1(f)) |
| 1883 | value2(f).SetBool(v.IsValid()) |
| 1884 | return next |
| 1885 | } |
| 1886 | default: |
| 1887 | n.exec = func(f *frame) bltn { |
| 1888 | v := value0(f).MapIndex(value1(f)) |
| 1889 | if v.IsValid() { |
| 1890 | dest(f).Set(v) |
| 1891 | } |
| 1892 | if doStatus { |
| 1893 | value2(f).SetBool(v.IsValid()) |
| 1894 | } |
| 1895 | return next |
| 1896 | } |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | // getFunc compiles a closure function generator for anonymous functions. |
| 1902 | func getFunc(n *node) { |
| 1903 | i := n.findex |
| 1904 | l := n.level |
| 1905 | next := getExec(n.tnext) |
| 1906 | numRet := len(n.typ.ret) |
| 1907 | |
| 1908 | n.exec = func(f *frame) bltn { |
| 1909 | fr := f.clone() |
| 1910 | o := getFrame(f, l).data[i] |
| 1911 | |
| 1912 | fct := reflect.MakeFunc(n.typ.TypeOf(), func(in []reflect.Value) []reflect.Value { |
| 1913 | // Allocate and init local frame. All values to be settable and addressable. |
| 1914 | fr2 := newFrame(fr, len(n.types), fr.runid()) |
| 1915 | d := fr2.data |
| 1916 | for i, t := range n.types { |
| 1917 | d[i] = reflect.New(t).Elem() |
| 1918 | } |
| 1919 | d = d[numRet:] |
| 1920 | |
| 1921 | // Copy function input arguments in local frame. |
| 1922 | for i, arg := range in { |
| 1923 | if i >= len(d) { |
| 1924 | // In case of unused arg, there may be not even a frame entry allocated, just skip. |
| 1925 | break |
| 1926 | } |
| 1927 | typ := n.typ.arg[i] |
| 1928 | switch { |
| 1929 | case isEmptyInterface(typ) || typ.TypeOf() == valueInterfaceType: |
| 1930 | d[i].Set(arg) |
| 1931 | case isInterfaceSrc(typ): |
| 1932 | d[i].Set(reflect.ValueOf(valueInterface{value: arg.Elem()})) |
| 1933 | default: |
| 1934 | d[i].Set(arg) |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | // Interpreter code execution. |
| 1939 | runCfg(n.child[3].start, fr2, n, n) |
| 1940 | |
| 1941 | f.mutex.Lock() |
| 1942 | getFrame(f, l).data[i] = o |
| 1943 | f.mutex.Unlock() |
| 1944 | |
| 1945 | return fr2.data[:numRet] |
| 1946 | }) |
| 1947 | |
| 1948 | f.mutex.Lock() |
| 1949 | getFrame(f, l).data[i] = fct |
| 1950 | f.mutex.Unlock() |
| 1951 | |
| 1952 | return next |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | func getMethod(n *node) { |
| 1957 | i := n.findex |
| 1958 | l := n.level |
| 1959 | next := getExec(n.tnext) |
| 1960 | |
| 1961 | n.exec = func(f *frame) bltn { |
| 1962 | nod := *(n.val.(*node)) |
| 1963 | nod.val = &nod |
| 1964 | nod.recv = n.recv |
| 1965 | getFrame(f, l).data[i] = genFuncValue(&nod)(f) |
| 1966 | return next |
| 1967 | } |
| 1968 | } |
| 1969 | |
| 1970 | func getMethodByName(n *node) { |
| 1971 | next := getExec(n.tnext) |
| 1972 | value0 := genValue(n.child[0]) |
| 1973 | name := n.child[1].ident |
| 1974 | i := n.findex |
| 1975 | l := n.level |
| 1976 | |
| 1977 | n.exec = func(f *frame) bltn { |
| 1978 | // The interface object must be directly accessible, or embedded in a struct (exported anonymous field). |
| 1979 | val0 := value0(f) |
| 1980 | val, ok := value0(f).Interface().(valueInterface) |
| 1981 | if !ok { |
| 1982 | // Search the first embedded valueInterface. |
| 1983 | for val0.Kind() == reflect.Ptr { |
| 1984 | val0 = val0.Elem() |
| 1985 | } |
| 1986 | for i := 0; i < val0.NumField(); i++ { |
| 1987 | fld := val0.Type().Field(i) |
| 1988 | if !fld.Anonymous || !fld.IsExported() { |
| 1989 | continue |
| 1990 | } |
| 1991 | if val, ok = val0.Field(i).Interface().(valueInterface); ok { |
| 1992 | break |
| 1993 | // TODO: should we keep track of all the vals that are indeed valueInterface, |
| 1994 | // so that later on we can call MethodByName on all of them until one matches? |
| 1995 | } |
| 1996 | } |
| 1997 | if !ok { |
| 1998 | panic(n.cfgErrorf("invalid interface value %v", val0)) |
| 1999 | } |
| 2000 | } |
| 2001 | // Traverse nested interface values to get the concrete value. |
| 2002 | for { |
| 2003 | v, ok := val.value.Interface().(valueInterface) |
| 2004 | if !ok { |
| 2005 | break |
| 2006 | } |
| 2007 | val = v |
| 2008 | } |
| 2009 | |
| 2010 | if met := val.value.MethodByName(name); met.IsValid() { |
| 2011 | getFrame(f, l).data[i] = met |
| 2012 | return next |
| 2013 | } |
| 2014 | |
| 2015 | typ := val.node.typ |
| 2016 | if typ.node == nil && typ.cat == valueT { |
| 2017 | // It happens with a var of empty interface type, that has value of concrete type |
| 2018 | // from runtime, being asserted to "user-defined" interface. |
| 2019 | if _, ok := typ.rtype.MethodByName(name); !ok { |
| 2020 | panic(n.cfgErrorf("method not found: %s", name)) |
| 2021 | } |
| 2022 | return next |
| 2023 | } |
| 2024 | |
| 2025 | // Finally search method recursively in embedded valueInterfaces. |
| 2026 | r, m, li := lookupMethodValue(val, name) |
| 2027 | if r.IsValid() { |
| 2028 | getFrame(f, l).data[i] = r |
| 2029 | return next |
| 2030 | } |
| 2031 | if m == nil { |
| 2032 | panic(n.cfgErrorf("method not found: %s", name)) |
| 2033 | } |
| 2034 | |
| 2035 | nod := *m |
| 2036 | nod.val = &nod |
| 2037 | nod.recv = &receiver{nil, val.value, li} |
| 2038 | getFrame(f, l).data[i] = genFuncValue(&nod)(f) |
| 2039 | return next |
| 2040 | } |
| 2041 | } |
| 2042 | |
| 2043 | // lookupMethodValue recursively looks within val for the method with the given |
| 2044 | // name. If a runtime value is found, it is returned in r, otherwise it is returned |
| 2045 | // in m, with li as the list of recursive field indexes. |
| 2046 | func lookupMethodValue(val valueInterface, name string) (r reflect.Value, m *node, li []int) { |
| 2047 | if r = val.value.MethodByName(name); r.IsValid() { |
| 2048 | return |
| 2049 | } |
| 2050 | if m, li = val.node.typ.lookupMethod(name); m != nil { |
| 2051 | return |
| 2052 | } |
| 2053 | if !isStruct(val.node.typ) { |
| 2054 | return |
| 2055 | } |
| 2056 | v := val.value |
| 2057 | for v.Type().Kind() == reflect.Ptr { |
| 2058 | v = v.Elem() |
| 2059 | } |
| 2060 | nf := v.NumField() |
| 2061 | for i := 0; i < nf; i++ { |
| 2062 | vi, ok := v.Field(i).Interface().(valueInterface) |
| 2063 | if !ok { |
| 2064 | continue |
| 2065 | } |
| 2066 | if r, m, li = lookupMethodValue(vi, name); m != nil { |
| 2067 | li = append([]int{i}, li...) |
| 2068 | return |
| 2069 | } |
| 2070 | } |
| 2071 | return |
| 2072 | } |
| 2073 | |
| 2074 | func getIndexSeq(n *node) { |
| 2075 | value := genValue(n.child[0]) |
| 2076 | index := n.val.([]int) |
| 2077 | tnext := getExec(n.tnext) |
| 2078 | i := n.findex |
| 2079 | l := n.level |
| 2080 | |
| 2081 | // Note: |
| 2082 | // Here we have to store the result using |
| 2083 | // f.data[i] = value(...) |
| 2084 | // instead of normal |
| 2085 | // dest(f).Set(value(...) |
| 2086 | // because the value returned by FieldByIndex() must be preserved |
| 2087 | // for possible future Set operations on the struct field (avoid a |
| 2088 | // dereference from Set, resulting in setting a copy of the |
| 2089 | // original field). |
| 2090 | |
| 2091 | if n.fnext != nil { |
| 2092 | fnext := getExec(n.fnext) |
| 2093 | n.exec = func(f *frame) bltn { |
| 2094 | v := value(f) |
| 2095 | r := v.FieldByIndex(index) |
| 2096 | getFrame(f, l).data[i] = r |
| 2097 | if r.Bool() { |
| 2098 | return tnext |
| 2099 | } |
| 2100 | return fnext |
| 2101 | } |
| 2102 | } else { |
| 2103 | n.exec = func(f *frame) bltn { |
| 2104 | v := value(f) |
| 2105 | getFrame(f, l).data[i] = v.FieldByIndex(index) |
| 2106 | return tnext |
| 2107 | } |
| 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | func getPtrIndexSeq(n *node) { |
| 2112 | index := n.val.([]int) |
| 2113 | tnext := getExec(n.tnext) |
| 2114 | value := genValue(n.child[0]) |
| 2115 | i := n.findex |
| 2116 | l := n.level |
| 2117 | |
| 2118 | if n.fnext != nil { |
| 2119 | fnext := getExec(n.fnext) |
| 2120 | n.exec = func(f *frame) bltn { |
| 2121 | r := value(f).Elem().FieldByIndex(index) |
| 2122 | getFrame(f, l).data[i] = r |
| 2123 | if r.Bool() { |
| 2124 | return tnext |
| 2125 | } |
| 2126 | return fnext |
| 2127 | } |
| 2128 | } else { |
| 2129 | n.exec = func(f *frame) bltn { |
| 2130 | getFrame(f, l).data[i] = value(f).Elem().FieldByIndex(index) |
| 2131 | return tnext |
| 2132 | } |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | func getIndexSeqField(n *node) { |
| 2137 | value := genValue(n.child[0]) |
| 2138 | index := n.val.([]int) |
| 2139 | i := n.findex |
| 2140 | l := n.level |
| 2141 | tnext := getExec(n.tnext) |
| 2142 | |
| 2143 | if n.fnext != nil { |
| 2144 | fnext := getExec(n.fnext) |
| 2145 | if n.child[0].typ.TypeOf().Kind() == reflect.Ptr { |
| 2146 | n.exec = func(f *frame) bltn { |
| 2147 | r := value(f).Elem().FieldByIndex(index) |
| 2148 | getFrame(f, l).data[i] = r |
| 2149 | if r.Bool() { |
| 2150 | return tnext |
| 2151 | } |
| 2152 | return fnext |
| 2153 | } |
| 2154 | } else { |
| 2155 | n.exec = func(f *frame) bltn { |
| 2156 | r := value(f).FieldByIndex(index) |
| 2157 | getFrame(f, l).data[i] = r |
| 2158 | if r.Bool() { |
| 2159 | return tnext |
| 2160 | } |
| 2161 | return fnext |
| 2162 | } |
| 2163 | } |
| 2164 | } else { |
| 2165 | if n.child[0].typ.TypeOf().Kind() == reflect.Ptr { |
| 2166 | n.exec = func(f *frame) bltn { |
| 2167 | getFrame(f, l).data[i] = value(f).Elem().FieldByIndex(index) |
| 2168 | return tnext |
| 2169 | } |
| 2170 | } else { |
| 2171 | n.exec = func(f *frame) bltn { |
| 2172 | getFrame(f, l).data[i] = value(f).FieldByIndex(index) |
| 2173 | return tnext |
| 2174 | } |
| 2175 | } |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | func getIndexSeqPtrMethod(n *node) { |
| 2180 | value := genValue(n.child[0]) |
| 2181 | index := n.val.([]int) |
| 2182 | fi := index[1:] |
| 2183 | mi := index[0] |
| 2184 | i := n.findex |
| 2185 | l := n.level |
| 2186 | next := getExec(n.tnext) |
| 2187 | |
| 2188 | if n.child[0].typ.TypeOf().Kind() == reflect.Ptr { |
| 2189 | if len(fi) == 0 { |
| 2190 | n.exec = func(f *frame) bltn { |
| 2191 | getFrame(f, l).data[i] = value(f).Method(mi) |
| 2192 | return next |
| 2193 | } |
| 2194 | } else { |
| 2195 | n.exec = func(f *frame) bltn { |
| 2196 | getFrame(f, l).data[i] = value(f).Elem().FieldByIndex(fi).Addr().Method(mi) |
| 2197 | return next |
| 2198 | } |
| 2199 | } |
| 2200 | } else { |
| 2201 | if len(fi) == 0 { |
| 2202 | n.exec = func(f *frame) bltn { |
| 2203 | getFrame(f, l).data[i] = value(f).Addr().Method(mi) |
| 2204 | return next |
| 2205 | } |
| 2206 | } else { |
| 2207 | n.exec = func(f *frame) bltn { |
| 2208 | getFrame(f, l).data[i] = value(f).FieldByIndex(fi).Addr().Method(mi) |
| 2209 | return next |
| 2210 | } |
| 2211 | } |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | func getIndexSeqMethod(n *node) { |
| 2216 | value := genValue(n.child[0]) |
| 2217 | index := n.val.([]int) |
| 2218 | fi := index[1:] |
| 2219 | mi := index[0] |
| 2220 | i := n.findex |
| 2221 | l := n.level |
| 2222 | next := getExec(n.tnext) |
| 2223 | |
| 2224 | if n.child[0].typ.TypeOf().Kind() == reflect.Ptr { |
| 2225 | if len(fi) == 0 { |
| 2226 | n.exec = func(f *frame) bltn { |
| 2227 | getFrame(f, l).data[i] = value(f).Elem().Method(mi) |
| 2228 | return next |
| 2229 | } |
| 2230 | } else { |
| 2231 | n.exec = func(f *frame) bltn { |
| 2232 | getFrame(f, l).data[i] = value(f).Elem().FieldByIndex(fi).Method(mi) |
| 2233 | return next |
| 2234 | } |
| 2235 | } |
| 2236 | } else { |
| 2237 | if len(fi) == 0 { |
| 2238 | n.exec = func(f *frame) bltn { |
| 2239 | getFrame(f, l).data[i] = value(f).Method(mi) |
| 2240 | return next |
| 2241 | } |
| 2242 | } else { |
| 2243 | n.exec = func(f *frame) bltn { |
| 2244 | getFrame(f, l).data[i] = value(f).FieldByIndex(fi).Method(mi) |
| 2245 | return next |
| 2246 | } |
| 2247 | } |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | func neg(n *node) { |
| 2252 | dest := genValue(n) |
| 2253 | value := genValue(n.child[0]) |
| 2254 | next := getExec(n.tnext) |
| 2255 | typ := n.typ.concrete().TypeOf() |
| 2256 | isInterface := n.typ.TypeOf().Kind() == reflect.Interface |
| 2257 | |
| 2258 | switch n.typ.TypeOf().Kind() { |
| 2259 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 2260 | if isInterface { |
| 2261 | n.exec = func(f *frame) bltn { |
| 2262 | dest(f).Set(reflect.ValueOf(-value(f).Int()).Convert(typ)) |
| 2263 | return next |
| 2264 | } |
| 2265 | return |
| 2266 | } |
| 2267 | n.exec = func(f *frame) bltn { |
| 2268 | dest(f).SetInt(-value(f).Int()) |
| 2269 | return next |
| 2270 | } |
| 2271 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 2272 | if isInterface { |
| 2273 | n.exec = func(f *frame) bltn { |
| 2274 | dest(f).Set(reflect.ValueOf(-value(f).Uint()).Convert(typ)) |
| 2275 | return next |
| 2276 | } |
| 2277 | return |
| 2278 | } |
| 2279 | n.exec = func(f *frame) bltn { |
| 2280 | dest(f).SetUint(-value(f).Uint()) |
| 2281 | return next |
| 2282 | } |
| 2283 | case reflect.Float32, reflect.Float64: |
| 2284 | if isInterface { |
| 2285 | n.exec = func(f *frame) bltn { |
| 2286 | dest(f).Set(reflect.ValueOf(-value(f).Float()).Convert(typ)) |
| 2287 | return next |
| 2288 | } |
| 2289 | return |
| 2290 | } |
| 2291 | n.exec = func(f *frame) bltn { |
| 2292 | dest(f).SetFloat(-value(f).Float()) |
| 2293 | return next |
| 2294 | } |
| 2295 | case reflect.Complex64, reflect.Complex128: |
| 2296 | if isInterface { |
| 2297 | n.exec = func(f *frame) bltn { |
| 2298 | dest(f).Set(reflect.ValueOf(-value(f).Complex()).Convert(typ)) |
| 2299 | return next |
| 2300 | } |
| 2301 | return |
| 2302 | } |
| 2303 | n.exec = func(f *frame) bltn { |
| 2304 | dest(f).SetComplex(-value(f).Complex()) |
| 2305 | return next |
| 2306 | } |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | func pos(n *node) { |
| 2311 | dest := genValue(n) |
| 2312 | value := genValue(n.child[0]) |
| 2313 | next := getExec(n.tnext) |
| 2314 | |
| 2315 | n.exec = func(f *frame) bltn { |
| 2316 | dest(f).Set(value(f)) |
| 2317 | return next |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | func bitNot(n *node) { |
| 2322 | dest := genValue(n) |
| 2323 | value := genValue(n.child[0]) |
| 2324 | next := getExec(n.tnext) |
| 2325 | typ := n.typ.concrete().TypeOf() |
| 2326 | isInterface := n.typ.TypeOf().Kind() == reflect.Interface |
| 2327 | |
| 2328 | switch typ.Kind() { |
| 2329 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 2330 | if isInterface { |
| 2331 | n.exec = func(f *frame) bltn { |
| 2332 | dest(f).Set(reflect.ValueOf(^value(f).Int()).Convert(typ)) |
| 2333 | return next |
| 2334 | } |
| 2335 | return |
| 2336 | } |
| 2337 | n.exec = func(f *frame) bltn { |
| 2338 | dest(f).SetInt(^value(f).Int()) |
| 2339 | return next |
| 2340 | } |
| 2341 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 2342 | if isInterface { |
| 2343 | n.exec = func(f *frame) bltn { |
| 2344 | dest(f).Set(reflect.ValueOf(^value(f).Uint()).Convert(typ)) |
| 2345 | return next |
| 2346 | } |
| 2347 | return |
| 2348 | } |
| 2349 | n.exec = func(f *frame) bltn { |
| 2350 | dest(f).SetUint(^value(f).Uint()) |
| 2351 | return next |
| 2352 | } |
| 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | func land(n *node) { |
| 2357 | value0 := genValue(n.child[0]) |
| 2358 | value1 := genValue(n.child[1]) |
| 2359 | tnext := getExec(n.tnext) |
| 2360 | dest := genValue(n) |
| 2361 | typ := n.typ.concrete().TypeOf() |
| 2362 | isInterface := n.typ.TypeOf().Kind() == reflect.Interface |
| 2363 | |
| 2364 | if n.fnext != nil { |
| 2365 | fnext := getExec(n.fnext) |
| 2366 | n.exec = func(f *frame) bltn { |
| 2367 | if value0(f).Bool() && value1(f).Bool() { |
| 2368 | dest(f).SetBool(true) |
| 2369 | return tnext |
| 2370 | } |
| 2371 | dest(f).SetBool(false) |
| 2372 | return fnext |
| 2373 | } |
| 2374 | return |
| 2375 | } |
| 2376 | if isInterface { |
| 2377 | n.exec = func(f *frame) bltn { |
| 2378 | dest(f).Set(reflect.ValueOf(value0(f).Bool() && value1(f).Bool()).Convert(typ)) |
| 2379 | return tnext |
| 2380 | } |
| 2381 | return |
| 2382 | } |
| 2383 | n.exec = func(f *frame) bltn { |
| 2384 | dest(f).SetBool(value0(f).Bool() && value1(f).Bool()) |
| 2385 | return tnext |
| 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | func lor(n *node) { |
| 2390 | value0 := genValue(n.child[0]) |
| 2391 | value1 := genValue(n.child[1]) |
| 2392 | tnext := getExec(n.tnext) |
| 2393 | dest := genValue(n) |
| 2394 | typ := n.typ.concrete().TypeOf() |
| 2395 | isInterface := n.typ.TypeOf().Kind() == reflect.Interface |
| 2396 | |
| 2397 | if n.fnext != nil { |
| 2398 | fnext := getExec(n.fnext) |
| 2399 | n.exec = func(f *frame) bltn { |
| 2400 | if value0(f).Bool() || value1(f).Bool() { |
| 2401 | dest(f).SetBool(true) |
| 2402 | return tnext |
| 2403 | } |
| 2404 | dest(f).SetBool(false) |
| 2405 | return fnext |
| 2406 | } |
| 2407 | return |
| 2408 | } |
| 2409 | if isInterface { |
| 2410 | n.exec = func(f *frame) bltn { |
| 2411 | dest(f).Set(reflect.ValueOf(value0(f).Bool() || value1(f).Bool()).Convert(typ)) |
| 2412 | return tnext |
| 2413 | } |
| 2414 | return |
| 2415 | } |
| 2416 | n.exec = func(f *frame) bltn { |
| 2417 | dest(f).SetBool(value0(f).Bool() || value1(f).Bool()) |
| 2418 | return tnext |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | func nop(n *node) { |
| 2423 | next := getExec(n.tnext) |
| 2424 | |
| 2425 | n.exec = func(f *frame) bltn { |
| 2426 | return next |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | func branch(n *node) { |
| 2431 | tnext := getExec(n.tnext) |
| 2432 | fnext := getExec(n.fnext) |
| 2433 | value := genValue(n) |
| 2434 | |
| 2435 | n.exec = func(f *frame) bltn { |
| 2436 | if value(f).Bool() { |
| 2437 | return tnext |
| 2438 | } |
| 2439 | return fnext |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | func _return(n *node) { |
| 2444 | child := n.child |
| 2445 | def := n.val.(*node) |
| 2446 | values := make([]func(*frame) reflect.Value, len(child)) |
| 2447 | for i, c := range child { |
| 2448 | switch t := def.typ.ret[i]; t.cat { |
| 2449 | case errorT: |
| 2450 | values[i] = genInterfaceWrapper(c, t.TypeOf()) |
| 2451 | case funcT: |
| 2452 | values[i] = genValue(c) |
| 2453 | case valueT: |
| 2454 | switch t.rtype.Kind() { |
| 2455 | case reflect.Interface: |
| 2456 | values[i] = genInterfaceWrapper(c, t.TypeOf()) |
| 2457 | continue |
| 2458 | case reflect.Func: |
| 2459 | values[i] = genFunctionWrapper(c) |
| 2460 | continue |
| 2461 | } |
| 2462 | fallthrough |
| 2463 | default: |
| 2464 | switch { |
| 2465 | case isInterfaceSrc(t): |
| 2466 | if len(t.field) == 0 { |
| 2467 | // empty interface case. |
| 2468 | // we can't let genValueInterface deal with it, because we call on c, |
| 2469 | // not on n, which means that the interfaceT knowledge is lost. |
| 2470 | values[i] = genValue(c) |
| 2471 | break |
| 2472 | } |
| 2473 | values[i] = genValueInterface(c) |
| 2474 | case c.typ.untyped: |
| 2475 | values[i] = genValueAs(c, t.TypeOf()) |
| 2476 | default: |
| 2477 | values[i] = genValue(c) |
| 2478 | } |
| 2479 | } |
| 2480 | } |
| 2481 | |
| 2482 | switch len(child) { |
| 2483 | case 0: |
| 2484 | n.exec = nil |
| 2485 | case 1: |
| 2486 | switch { |
| 2487 | case !child[0].rval.IsValid() && child[0].kind == binaryExpr: |
| 2488 | // No additional runtime operation is necessary for constants (not in frame) or |
| 2489 | // binary expressions (stored directly at the right location in frame). |
| 2490 | n.exec = nil |
| 2491 | case isCall(child[0]) && n.child[0].typ.id() == def.typ.ret[0].id(): |
| 2492 | // Calls are optmized as long as no type conversion is involved. |
| 2493 | n.exec = nil |
| 2494 | default: |
| 2495 | // Regular return: store the value to return at to start of the frame. |
| 2496 | v := values[0] |
| 2497 | n.exec = func(f *frame) bltn { |
| 2498 | f.data[0].Set(v(f)) |
| 2499 | return nil |
| 2500 | } |
| 2501 | } |
| 2502 | case 2: |
| 2503 | v0, v1 := values[0], values[1] |
| 2504 | n.exec = func(f *frame) bltn { |
| 2505 | f.data[0].Set(v0(f)) |
| 2506 | f.data[1].Set(v1(f)) |
| 2507 | return nil |
| 2508 | } |
| 2509 | default: |
| 2510 | n.exec = func(f *frame) bltn { |
| 2511 | for i, value := range values { |
| 2512 | f.data[i].Set(value(f)) |
| 2513 | } |
| 2514 | return nil |
| 2515 | } |
| 2516 | } |
| 2517 | } |
| 2518 | |
| 2519 | func arrayLit(n *node) { |
| 2520 | value := valueGenerator(n, n.findex) |
| 2521 | next := getExec(n.tnext) |
| 2522 | child := n.child |
| 2523 | if n.nleft == 1 { |
| 2524 | child = n.child[1:] |
| 2525 | } |
| 2526 | |
| 2527 | values := make([]func(*frame) reflect.Value, len(child)) |
| 2528 | index := make([]int, len(child)) |
| 2529 | var max, prev int |
| 2530 | |
| 2531 | ntyp := n.typ.resolveAlias() |
| 2532 | for i, c := range child { |
| 2533 | if c.kind == keyValueExpr { |
| 2534 | values[i] = genDestValue(ntyp.val, c.child[1]) |
| 2535 | index[i] = int(vInt(c.child[0].rval)) |
| 2536 | } else { |
| 2537 | values[i] = genDestValue(ntyp.val, c) |
| 2538 | index[i] = prev |
| 2539 | } |
| 2540 | prev = index[i] + 1 |
| 2541 | if prev > max { |
| 2542 | max = prev |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | typ := n.typ.frameType() |
| 2547 | kind := typ.Kind() |
| 2548 | n.exec = func(f *frame) bltn { |
| 2549 | var a reflect.Value |
| 2550 | if kind == reflect.Slice { |
| 2551 | a = reflect.MakeSlice(typ, max, max) |
| 2552 | } else { |
| 2553 | a, _ = n.typ.zero() |
| 2554 | } |
| 2555 | for i, v := range values { |
| 2556 | a.Index(index[i]).Set(v(f)) |
| 2557 | } |
| 2558 | value(f).Set(a) |
| 2559 | return next |
| 2560 | } |
| 2561 | } |
| 2562 | |
| 2563 | func mapLit(n *node) { |
| 2564 | value := valueGenerator(n, n.findex) |
| 2565 | next := getExec(n.tnext) |
| 2566 | child := n.child |
| 2567 | if n.nleft == 1 { |
| 2568 | child = n.child[1:] |
| 2569 | } |
| 2570 | typ := n.typ.frameType() |
| 2571 | keys := make([]func(*frame) reflect.Value, len(child)) |
| 2572 | values := make([]func(*frame) reflect.Value, len(child)) |
| 2573 | for i, c := range child { |
| 2574 | keys[i] = genDestValue(n.typ.key, c.child[0]) |
| 2575 | values[i] = genDestValue(n.typ.val, c.child[1]) |
| 2576 | } |
| 2577 | |
| 2578 | n.exec = func(f *frame) bltn { |
| 2579 | m := reflect.MakeMap(typ) |
| 2580 | for i, k := range keys { |
| 2581 | m.SetMapIndex(k(f), values[i](f)) |
| 2582 | } |
| 2583 | value(f).Set(m) |
| 2584 | return next |
| 2585 | } |
| 2586 | } |
| 2587 | |
| 2588 | func compositeBinMap(n *node) { |
| 2589 | value := valueGenerator(n, n.findex) |
| 2590 | next := getExec(n.tnext) |
| 2591 | child := n.child |
| 2592 | if n.nleft == 1 { |
| 2593 | child = n.child[1:] |
| 2594 | } |
| 2595 | typ := n.typ.frameType() |
| 2596 | keys := make([]func(*frame) reflect.Value, len(child)) |
| 2597 | values := make([]func(*frame) reflect.Value, len(child)) |
| 2598 | for i, c := range child { |
| 2599 | convertLiteralValue(c.child[0], typ.Key()) |
| 2600 | convertLiteralValue(c.child[1], typ.Elem()) |
| 2601 | keys[i] = genValue(c.child[0]) |
| 2602 | |
| 2603 | if isFuncSrc(c.child[1].typ) { |
| 2604 | values[i] = genFunctionWrapper(c.child[1]) |
| 2605 | } else { |
| 2606 | values[i] = genValue(c.child[1]) |
| 2607 | } |
| 2608 | } |
| 2609 | |
| 2610 | n.exec = func(f *frame) bltn { |
| 2611 | m := reflect.MakeMap(typ) |
| 2612 | for i, k := range keys { |
| 2613 | m.SetMapIndex(k(f), values[i](f)) |
| 2614 | } |
| 2615 | value(f).Set(m) |
| 2616 | return next |
| 2617 | } |
| 2618 | } |
| 2619 | |
| 2620 | func compositeBinSlice(n *node) { |
| 2621 | value := valueGenerator(n, n.findex) |
| 2622 | next := getExec(n.tnext) |
| 2623 | child := n.child |
| 2624 | if n.nleft == 1 { |
| 2625 | child = n.child[1:] |
| 2626 | } |
| 2627 | |
| 2628 | values := make([]func(*frame) reflect.Value, len(child)) |
| 2629 | index := make([]int, len(child)) |
| 2630 | rtype := n.typ.rtype.Elem() |
| 2631 | var max, prev int |
| 2632 | |
| 2633 | for i, c := range child { |
| 2634 | if c.kind == keyValueExpr { |
| 2635 | convertLiteralValue(c.child[1], rtype) |
| 2636 | values[i] = genValue(c.child[1]) |
| 2637 | index[i] = int(vInt(c.child[0].rval)) |
| 2638 | } else { |
| 2639 | convertLiteralValue(c, rtype) |
| 2640 | values[i] = genValue(c) |
| 2641 | index[i] = prev |
| 2642 | } |
| 2643 | prev = index[i] + 1 |
| 2644 | if prev > max { |
| 2645 | max = prev |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | typ := n.typ.frameType() |
| 2650 | kind := typ.Kind() |
| 2651 | n.exec = func(f *frame) bltn { |
| 2652 | var a reflect.Value |
| 2653 | if kind == reflect.Slice { |
| 2654 | a = reflect.MakeSlice(typ, max, max) |
| 2655 | } else { |
| 2656 | a, _ = n.typ.zero() |
| 2657 | } |
| 2658 | for i, v := range values { |
| 2659 | a.Index(index[i]).Set(v(f)) |
| 2660 | } |
| 2661 | value(f).Set(a) |
| 2662 | return next |
| 2663 | } |
| 2664 | } |
| 2665 | |
| 2666 | // doCompositeBinStruct creates and populates a struct object from a binary type. |
| 2667 | func doCompositeBinStruct(n *node, hasType bool) { |
| 2668 | next := getExec(n.tnext) |
| 2669 | value := valueGenerator(n, n.findex) |
| 2670 | typ := baseType(n.typ).rtype |
| 2671 | child := n.child |
| 2672 | if hasType { |
| 2673 | child = n.child[1:] |
| 2674 | } |
| 2675 | values := make([]func(*frame) reflect.Value, len(child)) |
| 2676 | fieldIndex := make([][]int, len(child)) |
| 2677 | for i, c := range child { |
| 2678 | if c.kind == keyValueExpr { |
| 2679 | if sf, ok := typ.FieldByName(c.child[0].ident); ok { |
| 2680 | fieldIndex[i] = sf.Index |
| 2681 | convertLiteralValue(c.child[1], sf.Type) |
| 2682 | if isFuncSrc(c.child[1].typ) { |
| 2683 | values[i] = genFunctionWrapper(c.child[1]) |
| 2684 | } else { |
| 2685 | values[i] = genValue(c.child[1]) |
| 2686 | } |
| 2687 | } |
| 2688 | } else { |
| 2689 | fieldIndex[i] = []int{i} |
| 2690 | if isFuncSrc(c.typ) && len(c.child) > 1 { |
| 2691 | convertLiteralValue(c.child[1], typ.Field(i).Type) |
| 2692 | values[i] = genFunctionWrapper(c.child[1]) |
| 2693 | } else { |
| 2694 | convertLiteralValue(c, typ.Field(i).Type) |
| 2695 | values[i] = genValue(c) |
| 2696 | } |
| 2697 | } |
| 2698 | } |
| 2699 | |
| 2700 | frameIndex := n.findex |
| 2701 | l := n.level |
| 2702 | |
| 2703 | n.exec = func(f *frame) bltn { |
| 2704 | s := reflect.New(typ).Elem() |
| 2705 | for i, v := range values { |
| 2706 | s.FieldByIndex(fieldIndex[i]).Set(v(f)) |
| 2707 | } |
| 2708 | d := value(f) |
| 2709 | switch { |
| 2710 | case d.Kind() == reflect.Ptr: |
| 2711 | d.Set(s.Addr()) |
| 2712 | default: |
| 2713 | getFrame(f, l).data[frameIndex] = s |
| 2714 | } |
| 2715 | return next |
| 2716 | } |
| 2717 | } |
| 2718 | |
| 2719 | func compositeBinStruct(n *node) { doCompositeBinStruct(n, true) } |
| 2720 | func compositeBinStructNotype(n *node) { doCompositeBinStruct(n, false) } |
| 2721 | |
| 2722 | func destType(n *node) *itype { |
| 2723 | switch n.anc.kind { |
| 2724 | case assignStmt, defineStmt: |
| 2725 | return n.anc.child[0].typ |
| 2726 | default: |
| 2727 | return n.typ |
| 2728 | } |
| 2729 | } |
| 2730 | |
| 2731 | func doComposite(n *node, hasType bool, keyed bool) { |
| 2732 | value := valueGenerator(n, n.findex) |
| 2733 | next := getExec(n.tnext) |
| 2734 | typ := baseType(n.typ) |
| 2735 | child := n.child |
| 2736 | if hasType { |
| 2737 | child = n.child[1:] |
| 2738 | } |
| 2739 | destInterface := isInterfaceSrc(destType(n)) |
| 2740 | |
| 2741 | values := make(map[int]func(*frame) reflect.Value) |
| 2742 | for i, c := range child { |
| 2743 | var val *node |
| 2744 | var fieldIndex int |
| 2745 | if keyed { |
| 2746 | val = c.child[1] |
| 2747 | fieldIndex = typ.fieldIndex(c.child[0].ident) |
| 2748 | } else { |
| 2749 | val = c |
| 2750 | fieldIndex = i |
| 2751 | } |
| 2752 | ft := typ.field[fieldIndex].typ |
| 2753 | rft := ft.TypeOf() |
| 2754 | convertLiteralValue(val, rft) |
| 2755 | switch { |
| 2756 | case val.typ.cat == nilT: |
| 2757 | values[fieldIndex] = func(*frame) reflect.Value { return reflect.New(rft).Elem() } |
| 2758 | case isNamedFuncSrc(val.typ): |
| 2759 | values[fieldIndex] = genValueAsFunctionWrapper(val) |
| 2760 | case isInterfaceSrc(ft) && (!isEmptyInterface(ft) || len(val.typ.method) > 0): |
| 2761 | values[fieldIndex] = genValueInterface(val) |
| 2762 | case isInterface(ft): |
| 2763 | values[fieldIndex] = genInterfaceWrapper(val, rft) |
| 2764 | default: |
| 2765 | values[fieldIndex] = genValue(val) |
| 2766 | } |
| 2767 | } |
| 2768 | |
| 2769 | frameIndex := n.findex |
| 2770 | l := n.level |
| 2771 | rt := typ.TypeOf() |
| 2772 | |
| 2773 | n.exec = func(f *frame) bltn { |
| 2774 | a := reflect.New(rt).Elem() |
| 2775 | for i, v := range values { |
| 2776 | a.Field(i).Set(v(f)) |
| 2777 | } |
| 2778 | d := value(f) |
| 2779 | switch { |
| 2780 | case d.Kind() == reflect.Ptr: |
| 2781 | d.Set(a.Addr()) |
| 2782 | case destInterface: |
| 2783 | if len(destType(n).field) > 0 { |
| 2784 | d.Set(reflect.ValueOf(valueInterface{n, a})) |
| 2785 | break |
| 2786 | } |
| 2787 | d.Set(a) |
| 2788 | default: |
| 2789 | getFrame(f, l).data[frameIndex] = a |
| 2790 | } |
| 2791 | return next |
| 2792 | } |
| 2793 | } |
| 2794 | |
| 2795 | // doCompositeLit creates and populates a struct object. |
| 2796 | func doCompositeLit(n *node, hasType bool) { |
| 2797 | doComposite(n, hasType, false) |
| 2798 | } |
| 2799 | |
| 2800 | func compositeLit(n *node) { doCompositeLit(n, true) } |
| 2801 | func compositeLitNotype(n *node) { doCompositeLit(n, false) } |
| 2802 | |
| 2803 | // doCompositeLitKeyed creates a struct Object, filling fields from sparse key-values. |
| 2804 | func doCompositeLitKeyed(n *node, hasType bool) { |
| 2805 | doComposite(n, hasType, true) |
| 2806 | } |
| 2807 | |
| 2808 | func compositeLitKeyed(n *node) { doCompositeLitKeyed(n, true) } |
| 2809 | func compositeLitKeyedNotype(n *node) { doCompositeLitKeyed(n, false) } |
| 2810 | |
| 2811 | func empty(n *node) {} |
| 2812 | |
| 2813 | var rat = reflect.ValueOf((*[]rune)(nil)).Type().Elem() // runes array type |
| 2814 | |
| 2815 | func _range(n *node) { |
| 2816 | index0 := n.child[0].findex // array index location in frame |
| 2817 | index2 := index0 - 1 // shallow array for range, always just behind index0 |
| 2818 | index3 := index2 - 1 // additional location to store string char position |
| 2819 | fnext := getExec(n.fnext) |
| 2820 | tnext := getExec(n.tnext) |
| 2821 | |
| 2822 | var value func(*frame) reflect.Value |
| 2823 | var an *node |
| 2824 | if len(n.child) == 4 { |
| 2825 | an = n.child[2] |
| 2826 | index1 := n.child[1].findex // array value location in frame |
| 2827 | if isString(an.typ.TypeOf()) { |
| 2828 | // Special variant of "range" for string, where the index indicates the byte position |
| 2829 | // of the rune in the string, rather than the index of the rune in array. |
| 2830 | stringType := reflect.TypeOf("") |
| 2831 | value = genValueAs(an, rat) // range on string iterates over runes |
| 2832 | n.exec = func(f *frame) bltn { |
| 2833 | a := f.data[index2] |
| 2834 | v0 := f.data[index3] |
| 2835 | v0.SetInt(v0.Int() + 1) |
| 2836 | i := int(v0.Int()) |
| 2837 | if i >= a.Len() { |
| 2838 | return fnext |
| 2839 | } |
| 2840 | // Compute byte position of the rune in string |
| 2841 | pos := a.Slice(0, i).Convert(stringType).Len() |
| 2842 | f.data[index0].SetInt(int64(pos)) |
| 2843 | f.data[index1].Set(a.Index(i)) |
| 2844 | return tnext |
| 2845 | } |
| 2846 | } else { |
| 2847 | value = genValueRangeArray(an) |
| 2848 | n.exec = func(f *frame) bltn { |
| 2849 | a := f.data[index2] |
| 2850 | v0 := f.data[index0] |
| 2851 | v0.SetInt(v0.Int() + 1) |
| 2852 | i := int(v0.Int()) |
| 2853 | if i >= a.Len() { |
| 2854 | return fnext |
| 2855 | } |
| 2856 | f.data[index1].Set(a.Index(i)) |
| 2857 | return tnext |
| 2858 | } |
| 2859 | } |
| 2860 | } else { |
| 2861 | an = n.child[1] |
| 2862 | if isString(an.typ.TypeOf()) { |
| 2863 | value = genValueAs(an, rat) // range on string iterates over runes |
| 2864 | } else { |
| 2865 | value = genValueRangeArray(an) |
| 2866 | } |
| 2867 | n.exec = func(f *frame) bltn { |
| 2868 | v0 := f.data[index0] |
| 2869 | v0.SetInt(v0.Int() + 1) |
| 2870 | if int(v0.Int()) >= f.data[index2].Len() { |
| 2871 | return fnext |
| 2872 | } |
| 2873 | return tnext |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | // Init sequence |
| 2878 | next := n.exec |
| 2879 | index := index0 |
| 2880 | if isString(an.typ.TypeOf()) && len(n.child) == 4 { |
| 2881 | index = index3 |
| 2882 | } |
| 2883 | n.child[0].exec = func(f *frame) bltn { |
| 2884 | f.data[index2] = value(f) // set array shallow copy for range |
| 2885 | f.data[index].SetInt(-1) // assing index value |
| 2886 | return next |
| 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | func rangeInt(n *node) { |
| 2891 | ixn := n.child[0] |
| 2892 | index0 := ixn.findex // array index location in frame |
| 2893 | index2 := index0 - 1 // max |
| 2894 | fnext := getExec(n.fnext) |
| 2895 | tnext := getExec(n.tnext) |
| 2896 | |
| 2897 | var value func(*frame) reflect.Value |
| 2898 | mxn := n.child[1] |
| 2899 | value = genValue(mxn) |
| 2900 | n.exec = func(f *frame) bltn { |
| 2901 | rv := f.data[index0] |
| 2902 | rv.SetInt(rv.Int() + 1) |
| 2903 | if int(rv.Int()) >= int(f.data[index2].Int()) { |
| 2904 | return fnext |
| 2905 | } |
| 2906 | return tnext |
| 2907 | } |
| 2908 | |
| 2909 | // Init sequence |
| 2910 | next := n.exec |
| 2911 | index := index0 |
| 2912 | ixn.exec = func(f *frame) bltn { |
| 2913 | f.data[index2] = value(f) // set max |
| 2914 | f.data[index].SetInt(-1) // assing index value |
| 2915 | return next |
| 2916 | } |
| 2917 | } |
| 2918 | |
| 2919 | func loopVarKey(n *node) { |
| 2920 | ixn := n.anc.anc.child[0] |
| 2921 | next := getExec(n.tnext) |
| 2922 | n.exec = func(f *frame) bltn { |
| 2923 | rv := f.data[ixn.findex] |
| 2924 | nv := reflect.New(rv.Type()).Elem() |
| 2925 | nv.Set(rv) |
| 2926 | f.data[n.findex] = nv |
| 2927 | return next |
| 2928 | } |
| 2929 | } |
| 2930 | |
| 2931 | func loopVarVal(n *node) { |
| 2932 | vln := n.anc.anc.child[1] |
| 2933 | next := getExec(n.tnext) |
| 2934 | n.exec = func(f *frame) bltn { |
| 2935 | rv := f.data[vln.findex] |
| 2936 | nv := reflect.New(rv.Type()).Elem() |
| 2937 | nv.Set(rv) |
| 2938 | f.data[n.findex] = nv |
| 2939 | return next |
| 2940 | } |
| 2941 | } |
| 2942 | |
| 2943 | func loopVarFor(n *node) { |
| 2944 | ixn := n.anc.anc.child[0].child[0] |
| 2945 | next := getExec(n.tnext) |
| 2946 | n.exec = func(f *frame) bltn { |
| 2947 | fv := f.data[ixn.findex] |
| 2948 | nv := reflect.New(fv.Type()).Elem() |
| 2949 | nv.Set(fv) |
| 2950 | f.data[n.findex] = nv |
| 2951 | return next |
| 2952 | } |
| 2953 | } |
| 2954 | |
| 2955 | func rangeChan(n *node) { |
| 2956 | i := n.child[0].findex // element index location in frame |
| 2957 | value := genValue(n.child[1]) // chan |
| 2958 | fnext := getExec(n.fnext) |
| 2959 | tnext := getExec(n.tnext) |
| 2960 | |
| 2961 | n.exec = func(f *frame) bltn { |
| 2962 | f.mutex.RLock() |
| 2963 | done := f.done |
| 2964 | f.mutex.RUnlock() |
| 2965 | |
| 2966 | chosen, v, ok := reflect.Select([]reflect.SelectCase{done, {Dir: reflect.SelectRecv, Chan: value(f)}}) |
| 2967 | if chosen == 0 { |
| 2968 | return nil |
| 2969 | } |
| 2970 | if !ok { |
| 2971 | return fnext |
| 2972 | } |
| 2973 | f.data[i].Set(v) |
| 2974 | return tnext |
| 2975 | } |
| 2976 | } |
| 2977 | |
| 2978 | func rangeMap(n *node) { |
| 2979 | index0 := n.child[0].findex // map index location in frame |
| 2980 | index2 := index0 - 1 // iterator for range, always just behind index0 |
| 2981 | fnext := getExec(n.fnext) |
| 2982 | tnext := getExec(n.tnext) |
| 2983 | value := genValue(n.child[len(n.child)-2]) // map value |
| 2984 | |
| 2985 | if len(n.child) == 4 && n.child[1].ident != "_" { |
| 2986 | index1 := n.child[1].findex // map value location in frame |
| 2987 | n.exec = func(f *frame) bltn { |
| 2988 | iter := f.data[index2].Interface().(*reflect.MapIter) |
| 2989 | if !iter.Next() { |
| 2990 | return fnext |
| 2991 | } |
| 2992 | f.data[index0].Set(iter.Key()) |
| 2993 | f.data[index1].Set(iter.Value()) |
| 2994 | return tnext |
| 2995 | } |
| 2996 | } else { |
| 2997 | n.exec = func(f *frame) bltn { |
| 2998 | iter := f.data[index2].Interface().(*reflect.MapIter) |
| 2999 | if !iter.Next() { |
| 3000 | return fnext |
| 3001 | } |
| 3002 | f.data[index0].Set(iter.Key()) |
| 3003 | return tnext |
| 3004 | } |
| 3005 | } |
| 3006 | |
| 3007 | // Init sequence |
| 3008 | next := n.exec |
| 3009 | n.child[0].exec = func(f *frame) bltn { |
| 3010 | f.data[index2].Set(reflect.ValueOf(value(f).MapRange())) |
| 3011 | return next |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | func _case(n *node) { |
| 3016 | tnext := getExec(n.tnext) |
| 3017 | |
| 3018 | // TODO(mpl): a lot of what is done in typeAssert should probably be redone/reused here. |
| 3019 | switch { |
| 3020 | case n.anc.anc.kind == typeSwitch: |
| 3021 | fnext := getExec(n.fnext) |
| 3022 | sn := n.anc.anc // switch node |
| 3023 | types := make([]*itype, len(n.child)-1) |
| 3024 | for i := range types { |
| 3025 | types[i] = n.child[i].typ |
| 3026 | } |
| 3027 | srcValue := genValue(sn.child[1].lastChild().child[0]) |
| 3028 | |
| 3029 | if len(sn.child[1].child) != 2 { |
| 3030 | // no assign in switch guard |
| 3031 | if len(n.child) <= 1 { |
| 3032 | n.exec = func(f *frame) bltn { return tnext } |
| 3033 | } else { |
| 3034 | n.exec = func(f *frame) bltn { |
| 3035 | ival := srcValue(f).Interface() |
| 3036 | val, ok := ival.(valueInterface) |
| 3037 | // TODO(mpl): I'm assuming here that !ok means that we're dealing with the empty |
| 3038 | // interface case. But maybe we should make sure by checking the relevant cat |
| 3039 | // instead? later. Use t := v.Type(); t.Kind() == reflect.Interface , like above. |
| 3040 | if !ok { |
| 3041 | var stype string |
| 3042 | if ival != nil { |
| 3043 | stype = strings.ReplaceAll(reflect.TypeOf(ival).String(), " {}", "{}") |
| 3044 | } |
| 3045 | for _, typ := range types { |
| 3046 | // TODO(mpl): we should actually use canAssertTypes, but need to find a valid |
| 3047 | // rtype for typ. Plus we need to refactor with typeAssert(). |
| 3048 | // weak check instead for now. |
| 3049 | if ival == nil { |
| 3050 | if typ.cat == nilT { |
| 3051 | return tnext |
| 3052 | } |
| 3053 | continue |
| 3054 | } |
| 3055 | if stype == typ.id() { |
| 3056 | return tnext |
| 3057 | } |
| 3058 | } |
| 3059 | return fnext |
| 3060 | } |
| 3061 | if v := val.node; v != nil { |
| 3062 | for _, typ := range types { |
| 3063 | if v.typ.id() == typ.id() { |
| 3064 | return tnext |
| 3065 | } |
| 3066 | } |
| 3067 | } |
| 3068 | return fnext |
| 3069 | } |
| 3070 | } |
| 3071 | break |
| 3072 | } |
| 3073 | |
| 3074 | // assign in switch guard |
| 3075 | destValue := genValue(n.lastChild().child[0]) |
| 3076 | switch len(types) { |
| 3077 | case 0: |
| 3078 | // default clause: assign var to interface value |
| 3079 | n.exec = func(f *frame) bltn { |
| 3080 | destValue(f).Set(srcValue(f)) |
| 3081 | return tnext |
| 3082 | } |
| 3083 | case 1: |
| 3084 | // match against 1 type: assign var to concrete value |
| 3085 | typ := types[0] |
| 3086 | n.exec = func(f *frame) bltn { |
| 3087 | v := srcValue(f) |
| 3088 | if !v.IsValid() { |
| 3089 | // match zero value against nil |
| 3090 | if typ.cat == nilT { |
| 3091 | return tnext |
| 3092 | } |
| 3093 | return fnext |
| 3094 | } |
| 3095 | if t := v.Type(); t.Kind() == reflect.Interface { |
| 3096 | if typ.cat == nilT && v.IsNil() { |
| 3097 | return tnext |
| 3098 | } |
| 3099 | rtyp := typ.TypeOf() |
| 3100 | if rtyp == nil { |
| 3101 | return fnext |
| 3102 | } |
| 3103 | elem := v.Elem() |
| 3104 | if rtyp.String() == t.String() && implementsInterface(v, typ) { |
| 3105 | destValue(f).Set(elem) |
| 3106 | return tnext |
| 3107 | } |
| 3108 | ival := v.Interface() |
| 3109 | if ival != nil && rtyp.String() == reflect.TypeOf(ival).String() { |
| 3110 | destValue(f).Set(elem) |
| 3111 | return tnext |
| 3112 | } |
| 3113 | if typ.cat == valueT && rtyp.Kind() == reflect.Interface && elem.IsValid() && elem.Type().Implements(rtyp) { |
| 3114 | destValue(f).Set(elem) |
| 3115 | return tnext |
| 3116 | } |
| 3117 | return fnext |
| 3118 | } |
| 3119 | if vi, ok := v.Interface().(valueInterface); ok { |
| 3120 | if vi.node != nil { |
| 3121 | if vi.node.typ.id() == typ.id() { |
| 3122 | destValue(f).Set(vi.value) |
| 3123 | return tnext |
| 3124 | } |
| 3125 | } |
| 3126 | return fnext |
| 3127 | } |
| 3128 | if v.Type() == typ.TypeOf() { |
| 3129 | destValue(f).Set(v) |
| 3130 | return tnext |
| 3131 | } |
| 3132 | return fnext |
| 3133 | } |
| 3134 | |
| 3135 | default: |
| 3136 | n.exec = func(f *frame) bltn { |
| 3137 | val := srcValue(f) |
| 3138 | if t := val.Type(); t.Kind() == reflect.Interface { |
| 3139 | for _, typ := range types { |
| 3140 | if typ.cat == nilT && val.IsNil() { |
| 3141 | return tnext |
| 3142 | } |
| 3143 | rtyp := typ.TypeOf() |
| 3144 | if rtyp == nil { |
| 3145 | continue |
| 3146 | } |
| 3147 | elem := val.Elem() |
| 3148 | if rtyp.String() == t.String() && implementsInterface(val, typ) { |
| 3149 | destValue(f).Set(elem) |
| 3150 | return tnext |
| 3151 | } |
| 3152 | ival := val.Interface() |
| 3153 | if ival != nil && rtyp.String() == reflect.TypeOf(ival).String() { |
| 3154 | destValue(f).Set(elem) |
| 3155 | return tnext |
| 3156 | } |
| 3157 | if typ.cat == valueT && rtyp.Kind() == reflect.Interface && elem.IsValid() && elem.Type().Implements(rtyp) { |
| 3158 | destValue(f).Set(elem) |
| 3159 | return tnext |
| 3160 | } |
| 3161 | } |
| 3162 | return fnext |
| 3163 | } |
| 3164 | if vi, ok := val.Interface().(valueInterface); ok { |
| 3165 | if v := vi.node; v != nil { |
| 3166 | for _, typ := range types { |
| 3167 | if v.typ.id() == typ.id() { |
| 3168 | destValue(f).Set(val) |
| 3169 | return tnext |
| 3170 | } |
| 3171 | } |
| 3172 | } |
| 3173 | return fnext |
| 3174 | } |
| 3175 | vt := val.Type() |
| 3176 | for _, typ := range types { |
| 3177 | if vt == typ.TypeOf() { |
| 3178 | destValue(f).Set(val) |
| 3179 | return tnext |
| 3180 | } |
| 3181 | } |
| 3182 | return fnext |
| 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | case len(n.child) <= 1: // default clause |
| 3187 | n.exec = func(f *frame) bltn { return tnext } |
| 3188 | |
| 3189 | default: |
| 3190 | fnext := getExec(n.fnext) |
| 3191 | l := len(n.anc.anc.child) |
| 3192 | value := genValue(n.anc.anc.child[l-2]) |
| 3193 | values := make([]func(*frame) reflect.Value, len(n.child)-1) |
| 3194 | for i := range values { |
| 3195 | values[i] = genValue(n.child[i]) |
| 3196 | } |
| 3197 | n.exec = func(f *frame) bltn { |
| 3198 | v0 := value(f) |
| 3199 | for _, v := range values { |
| 3200 | v1 := v(f) |
| 3201 | if !v0.Type().AssignableTo(v1.Type()) { |
| 3202 | v0 = v0.Convert(v1.Type()) |
| 3203 | } |
| 3204 | if v0.Interface() == v1.Interface() { |
| 3205 | return tnext |
| 3206 | } |
| 3207 | } |
| 3208 | return fnext |
| 3209 | } |
| 3210 | } |
| 3211 | } |
| 3212 | |
| 3213 | func implementsInterface(v reflect.Value, t *itype) bool { |
| 3214 | rt := v.Type() |
| 3215 | if t.cat == valueT { |
| 3216 | return rt.Implements(t.rtype) |
| 3217 | } |
| 3218 | vt := &itype{cat: valueT, rtype: rt} |
| 3219 | if vt.methods().contains(t.methods()) { |
| 3220 | return true |
| 3221 | } |
| 3222 | vi, ok := v.Interface().(valueInterface) |
| 3223 | if !ok { |
| 3224 | return false |
| 3225 | } |
| 3226 | return vi.node != nil && vi.node.typ.methods().contains(t.methods()) |
| 3227 | } |
| 3228 | |
| 3229 | func appendSlice(n *node) { |
| 3230 | dest := genValueOutput(n, n.typ.rtype) |
| 3231 | next := getExec(n.tnext) |
| 3232 | value := genValue(n.child[1]) |
| 3233 | value0 := genValue(n.child[2]) |
| 3234 | |
| 3235 | if isString(n.child[2].typ.TypeOf()) { |
| 3236 | typ := reflect.TypeOf([]byte{}) |
| 3237 | n.exec = func(f *frame) bltn { |
| 3238 | dest(f).Set(reflect.AppendSlice(value(f), value0(f).Convert(typ))) |
| 3239 | return next |
| 3240 | } |
| 3241 | } else { |
| 3242 | n.exec = func(f *frame) bltn { |
| 3243 | dest(f).Set(reflect.AppendSlice(value(f), value0(f))) |
| 3244 | return next |
| 3245 | } |
| 3246 | } |
| 3247 | } |
| 3248 | |
| 3249 | func _append(n *node) { |
| 3250 | if len(n.child) == 3 { |
| 3251 | c1, c2 := n.child[1], n.child[2] |
| 3252 | if (c1.typ.cat == valueT || c2.typ.cat == valueT) && c1.typ.rtype == c2.typ.rtype || |
| 3253 | isArray(c2.typ) && c2.typ.elem().id() == n.typ.elem().id() || |
| 3254 | isByteArray(c1.typ.TypeOf()) && isString(c2.typ.TypeOf()) { |
| 3255 | appendSlice(n) |
| 3256 | return |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | dest := genValueOutput(n, n.typ.rtype) |
| 3261 | value := genValue(n.child[1]) |
| 3262 | next := getExec(n.tnext) |
| 3263 | |
| 3264 | switch l := len(n.child); { |
| 3265 | case l == 2: |
| 3266 | n.exec = func(f *frame) bltn { |
| 3267 | dest(f).Set(value(f)) |
| 3268 | return next |
| 3269 | } |
| 3270 | case l > 3: |
| 3271 | args := n.child[2:] |
| 3272 | l := len(args) |
| 3273 | values := make([]func(*frame) reflect.Value, l) |
| 3274 | for i, arg := range args { |
| 3275 | switch elem := n.typ.elem(); { |
| 3276 | case isInterfaceSrc(elem) && (!isEmptyInterface(elem) || len(arg.typ.method) > 0): |
| 3277 | values[i] = genValueInterface(arg) |
| 3278 | case isInterfaceBin(elem): |
| 3279 | values[i] = genInterfaceWrapper(arg, elem.rtype) |
| 3280 | case arg.typ.untyped: |
| 3281 | values[i] = genValueAs(arg, n.child[1].typ.TypeOf().Elem()) |
| 3282 | default: |
| 3283 | values[i] = genValue(arg) |
| 3284 | } |
| 3285 | } |
| 3286 | |
| 3287 | n.exec = func(f *frame) bltn { |
| 3288 | sl := make([]reflect.Value, l) |
| 3289 | for i, v := range values { |
| 3290 | sl[i] = v(f) |
| 3291 | } |
| 3292 | dest(f).Set(reflect.Append(value(f), sl...)) |
| 3293 | return next |
| 3294 | } |
| 3295 | default: |
| 3296 | var value0 func(*frame) reflect.Value |
| 3297 | switch elem := n.typ.elem(); { |
| 3298 | case isInterfaceSrc(elem) && (!isEmptyInterface(elem) || len(n.child[2].typ.method) > 0): |
| 3299 | value0 = genValueInterface(n.child[2]) |
| 3300 | case isInterfaceBin(elem): |
| 3301 | value0 = genInterfaceWrapper(n.child[2], elem.rtype) |
| 3302 | case n.child[2].typ.untyped: |
| 3303 | value0 = genValueAs(n.child[2], n.child[1].typ.TypeOf().Elem()) |
| 3304 | default: |
| 3305 | value0 = genValue(n.child[2]) |
| 3306 | } |
| 3307 | |
| 3308 | n.exec = func(f *frame) bltn { |
| 3309 | dest(f).Set(reflect.Append(value(f), value0(f))) |
| 3310 | return next |
| 3311 | } |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | func _cap(n *node) { |
| 3316 | dest := genValueOutput(n, reflect.TypeOf(int(0))) |
| 3317 | value := genValue(n.child[1]) |
| 3318 | next := getExec(n.tnext) |
| 3319 | |
| 3320 | if wantEmptyInterface(n) { |
| 3321 | n.exec = func(f *frame) bltn { |
| 3322 | dest(f).Set(reflect.ValueOf(value(f).Cap())) |
| 3323 | return next |
| 3324 | } |
| 3325 | return |
| 3326 | } |
| 3327 | n.exec = func(f *frame) bltn { |
| 3328 | dest(f).SetInt(int64(value(f).Cap())) |
| 3329 | return next |
| 3330 | } |
| 3331 | } |
| 3332 | |
| 3333 | func _copy(n *node) { |
| 3334 | in := []func(*frame) reflect.Value{genValueArray(n.child[1]), genValue(n.child[2])} |
| 3335 | out := []func(*frame) reflect.Value{genValueOutput(n, reflect.TypeOf(0))} |
| 3336 | |
| 3337 | genBuiltinDeferWrapper(n, in, out, func(args []reflect.Value) []reflect.Value { |
| 3338 | cnt := reflect.Copy(args[0], args[1]) |
| 3339 | return []reflect.Value{reflect.ValueOf(cnt)} |
| 3340 | }) |
| 3341 | } |
| 3342 | |
| 3343 | func _close(n *node) { |
| 3344 | in := []func(*frame) reflect.Value{genValue(n.child[1])} |
| 3345 | |
| 3346 | genBuiltinDeferWrapper(n, in, nil, func(args []reflect.Value) []reflect.Value { |
| 3347 | args[0].Close() |
| 3348 | return nil |
| 3349 | }) |
| 3350 | } |
| 3351 | |
| 3352 | func _complex(n *node) { |
| 3353 | dest := genValueOutput(n, reflect.TypeOf(complex(0, 0))) |
| 3354 | c1, c2 := n.child[1], n.child[2] |
| 3355 | convertLiteralValue(c1, floatType) |
| 3356 | convertLiteralValue(c2, floatType) |
| 3357 | value0 := genValue(c1) |
| 3358 | value1 := genValue(c2) |
| 3359 | next := getExec(n.tnext) |
| 3360 | |
| 3361 | typ := n.typ.TypeOf() |
| 3362 | if isComplex(typ) { |
| 3363 | if wantEmptyInterface(n) { |
| 3364 | n.exec = func(f *frame) bltn { |
| 3365 | dest(f).Set(reflect.ValueOf(complex(value0(f).Float(), value1(f).Float()))) |
| 3366 | return next |
| 3367 | } |
| 3368 | return |
| 3369 | } |
| 3370 | n.exec = func(f *frame) bltn { |
| 3371 | dest(f).SetComplex(complex(value0(f).Float(), value1(f).Float())) |
| 3372 | return next |
| 3373 | } |
| 3374 | return |
| 3375 | } |
| 3376 | // Not a complex type: ignore imaginary part |
| 3377 | n.exec = func(f *frame) bltn { |
| 3378 | dest(f).Set(value0(f).Convert(typ)) |
| 3379 | return next |
| 3380 | } |
| 3381 | } |
| 3382 | |
| 3383 | func _imag(n *node) { |
| 3384 | dest := genValueOutput(n, reflect.TypeOf(float64(0))) |
| 3385 | convertLiteralValue(n.child[1], complexType) |
| 3386 | value := genValue(n.child[1]) |
| 3387 | next := getExec(n.tnext) |
| 3388 | |
| 3389 | if wantEmptyInterface(n) { |
| 3390 | n.exec = func(f *frame) bltn { |
| 3391 | dest(f).Set(reflect.ValueOf(imag(value(f).Complex()))) |
| 3392 | return next |
| 3393 | } |
| 3394 | return |
| 3395 | } |
| 3396 | n.exec = func(f *frame) bltn { |
| 3397 | dest(f).SetFloat(imag(value(f).Complex())) |
| 3398 | return next |
| 3399 | } |
| 3400 | } |
| 3401 | |
| 3402 | func _real(n *node) { |
| 3403 | dest := genValueOutput(n, reflect.TypeOf(float64(0))) |
| 3404 | convertLiteralValue(n.child[1], complexType) |
| 3405 | value := genValue(n.child[1]) |
| 3406 | next := getExec(n.tnext) |
| 3407 | |
| 3408 | if wantEmptyInterface(n) { |
| 3409 | n.exec = func(f *frame) bltn { |
| 3410 | dest(f).Set(reflect.ValueOf(real(value(f).Complex()))) |
| 3411 | return next |
| 3412 | } |
| 3413 | return |
| 3414 | } |
| 3415 | n.exec = func(f *frame) bltn { |
| 3416 | dest(f).SetFloat(real(value(f).Complex())) |
| 3417 | return next |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | func _delete(n *node) { |
| 3422 | value0 := genValue(n.child[1]) // map |
| 3423 | value1 := genValue(n.child[2]) // key |
| 3424 | in := []func(*frame) reflect.Value{value0, value1} |
| 3425 | var z reflect.Value |
| 3426 | |
| 3427 | genBuiltinDeferWrapper(n, in, nil, func(args []reflect.Value) []reflect.Value { |
| 3428 | args[0].SetMapIndex(args[1], z) |
| 3429 | return nil |
| 3430 | }) |
| 3431 | } |
| 3432 | |
| 3433 | func capConst(n *node) { |
| 3434 | // There is no Cap() method for reflect.Type, just return Len() instead. |
| 3435 | lenConst(n) |
| 3436 | } |
| 3437 | |
| 3438 | func lenConst(n *node) { |
| 3439 | n.rval = reflect.New(reflect.TypeOf(int(0))).Elem() |
| 3440 | c1 := n.child[1] |
| 3441 | if c1.rval.IsValid() { |
| 3442 | n.rval.SetInt(int64(len(vString(c1.rval)))) |
| 3443 | return |
| 3444 | } |
| 3445 | t := c1.typ.TypeOf() |
| 3446 | for t.Kind() == reflect.Ptr { |
| 3447 | t = t.Elem() |
| 3448 | } |
| 3449 | n.rval.SetInt(int64(t.Len())) |
| 3450 | } |
| 3451 | |
| 3452 | func _len(n *node) { |
| 3453 | dest := genValueOutput(n, reflect.TypeOf(int(0))) |
| 3454 | value := genValue(n.child[1]) |
| 3455 | if isPtr(n.child[1].typ) { |
| 3456 | val := value |
| 3457 | value = func(f *frame) reflect.Value { |
| 3458 | v := val(f).Elem() |
| 3459 | for v.Kind() == reflect.Ptr { |
| 3460 | v = v.Elem() |
| 3461 | } |
| 3462 | return v |
| 3463 | } |
| 3464 | } |
| 3465 | next := getExec(n.tnext) |
| 3466 | |
| 3467 | if wantEmptyInterface(n) { |
| 3468 | n.exec = func(f *frame) bltn { |
| 3469 | dest(f).Set(reflect.ValueOf(value(f).Len())) |
| 3470 | return next |
| 3471 | } |
| 3472 | return |
| 3473 | } |
| 3474 | n.exec = func(f *frame) bltn { |
| 3475 | dest(f).SetInt(int64(value(f).Len())) |
| 3476 | return next |
| 3477 | } |
| 3478 | } |
| 3479 | |
| 3480 | func _new(n *node) { |
| 3481 | next := getExec(n.tnext) |
| 3482 | t1 := n.child[1].typ |
| 3483 | typ := t1.TypeOf() |
| 3484 | dest := genValueOutput(n, reflect.PtrTo(typ)) |
| 3485 | |
| 3486 | if isInterfaceSrc(t1) && (!isEmptyInterface(t1) || len(t1.method) > 0) { |
| 3487 | typ = zeroInterfaceValue().Type() |
| 3488 | } |
| 3489 | |
| 3490 | n.exec = func(f *frame) bltn { |
| 3491 | v := reflect.New(typ) |
| 3492 | if vi, ok := v.Interface().(*valueInterface); ok { |
| 3493 | vi.node = n |
| 3494 | } |
| 3495 | dest(f).Set(v) |
| 3496 | return next |
| 3497 | } |
| 3498 | } |
| 3499 | |
| 3500 | // _make allocates and initializes a slice, a map or a chan. |
| 3501 | func _make(n *node) { |
| 3502 | next := getExec(n.tnext) |
| 3503 | typ := n.child[1].typ.frameType() |
| 3504 | dest := genValueOutput(n, typ) |
| 3505 | |
| 3506 | switch typ.Kind() { |
| 3507 | case reflect.Array, reflect.Slice: |
| 3508 | value := genValue(n.child[2]) |
| 3509 | |
| 3510 | switch len(n.child) { |
| 3511 | case 3: |
| 3512 | n.exec = func(f *frame) bltn { |
| 3513 | length := int(vInt(value(f))) |
| 3514 | dest(f).Set(reflect.MakeSlice(typ, length, length)) |
| 3515 | return next |
| 3516 | } |
| 3517 | case 4: |
| 3518 | value1 := genValue(n.child[3]) |
| 3519 | n.exec = func(f *frame) bltn { |
| 3520 | dest(f).Set(reflect.MakeSlice(typ, int(vInt(value(f))), int(vInt(value1(f))))) |
| 3521 | return next |
| 3522 | } |
| 3523 | } |
| 3524 | |
| 3525 | case reflect.Chan: |
| 3526 | switch len(n.child) { |
| 3527 | case 2: |
| 3528 | n.exec = func(f *frame) bltn { |
| 3529 | dest(f).Set(reflect.MakeChan(typ, 0)) |
| 3530 | return next |
| 3531 | } |
| 3532 | case 3: |
| 3533 | value := genValue(n.child[2]) |
| 3534 | n.exec = func(f *frame) bltn { |
| 3535 | dest(f).Set(reflect.MakeChan(typ, int(vInt(value(f))))) |
| 3536 | return next |
| 3537 | } |
| 3538 | } |
| 3539 | |
| 3540 | case reflect.Map: |
| 3541 | switch len(n.child) { |
| 3542 | case 2: |
| 3543 | n.exec = func(f *frame) bltn { |
| 3544 | dest(f).Set(reflect.MakeMap(typ)) |
| 3545 | return next |
| 3546 | } |
| 3547 | case 3: |
| 3548 | value := genValue(n.child[2]) |
| 3549 | n.exec = func(f *frame) bltn { |
| 3550 | dest(f).Set(reflect.MakeMapWithSize(typ, int(vInt(value(f))))) |
| 3551 | return next |
| 3552 | } |
| 3553 | } |
| 3554 | } |
| 3555 | } |
| 3556 | |
| 3557 | func reset(n *node) { |
| 3558 | next := getExec(n.tnext) |
| 3559 | |
| 3560 | switch l := len(n.child) - 1; l { |
| 3561 | case 1: |
| 3562 | typ := n.child[0].typ.frameType() |
| 3563 | i := n.child[0].findex |
| 3564 | n.exec = func(f *frame) bltn { |
| 3565 | f.data[i] = reflect.New(typ).Elem() |
| 3566 | return next |
| 3567 | } |
| 3568 | case 2: |
| 3569 | c0, c1 := n.child[0], n.child[1] |
| 3570 | i0, i1 := c0.findex, c1.findex |
| 3571 | t0, t1 := c0.typ.frameType(), c1.typ.frameType() |
| 3572 | n.exec = func(f *frame) bltn { |
| 3573 | f.data[i0] = reflect.New(t0).Elem() |
| 3574 | f.data[i1] = reflect.New(t1).Elem() |
| 3575 | return next |
| 3576 | } |
| 3577 | default: |
| 3578 | types := make([]reflect.Type, l) |
| 3579 | index := make([]int, l) |
| 3580 | for i, c := range n.child[:l] { |
| 3581 | index[i] = c.findex |
| 3582 | types[i] = c.typ.frameType() |
| 3583 | } |
| 3584 | n.exec = func(f *frame) bltn { |
| 3585 | for i, ind := range index { |
| 3586 | f.data[ind] = reflect.New(types[i]).Elem() |
| 3587 | } |
| 3588 | return next |
| 3589 | } |
| 3590 | } |
| 3591 | } |
| 3592 | |
| 3593 | // recv reads from a channel. |
| 3594 | func recv(n *node) { |
| 3595 | value := genValue(n.child[0]) |
| 3596 | tnext := getExec(n.tnext) |
| 3597 | i := n.findex |
| 3598 | l := n.level |
| 3599 | |
| 3600 | if n.interp.cancelChan { |
| 3601 | // Cancellable channel read |
| 3602 | if n.fnext != nil { |
| 3603 | fnext := getExec(n.fnext) |
| 3604 | n.exec = func(f *frame) bltn { |
| 3605 | // Fast: channel read doesn't block |
| 3606 | ch := value(f) |
| 3607 | if r, ok := ch.TryRecv(); ok { |
| 3608 | getFrame(f, l).data[i] = r |
| 3609 | if r.Bool() { |
| 3610 | return tnext |
| 3611 | } |
| 3612 | return fnext |
| 3613 | } |
| 3614 | // Slow: channel read blocks, allow cancel |
| 3615 | f.mutex.RLock() |
| 3616 | done := f.done |
| 3617 | f.mutex.RUnlock() |
| 3618 | |
| 3619 | chosen, v, _ := reflect.Select([]reflect.SelectCase{done, {Dir: reflect.SelectRecv, Chan: ch}}) |
| 3620 | if chosen == 0 { |
| 3621 | return nil |
| 3622 | } |
| 3623 | if v.Bool() { |
| 3624 | return tnext |
| 3625 | } |
| 3626 | return fnext |
| 3627 | } |
| 3628 | } else { |
| 3629 | n.exec = func(f *frame) bltn { |
| 3630 | // Fast: channel read doesn't block |
| 3631 | ch := value(f) |
| 3632 | if r, ok := ch.TryRecv(); ok { |
| 3633 | getFrame(f, l).data[i] = r |
| 3634 | return tnext |
| 3635 | } |
| 3636 | // Slow: channel is blocked, allow cancel |
| 3637 | f.mutex.RLock() |
| 3638 | done := f.done |
| 3639 | f.mutex.RUnlock() |
| 3640 | |
| 3641 | var chosen int |
| 3642 | chosen, getFrame(f, l).data[i], _ = reflect.Select([]reflect.SelectCase{done, {Dir: reflect.SelectRecv, Chan: ch}}) |
| 3643 | if chosen == 0 { |
| 3644 | return nil |
| 3645 | } |
| 3646 | return tnext |
| 3647 | } |
| 3648 | } |
| 3649 | } else { |
| 3650 | // Blocking channel read (less overhead) |
| 3651 | if n.fnext != nil { |
| 3652 | fnext := getExec(n.fnext) |
| 3653 | n.exec = func(f *frame) bltn { |
| 3654 | if r, _ := value(f).Recv(); r.Bool() { |
| 3655 | getFrame(f, l).data[i] = r |
| 3656 | return tnext |
| 3657 | } |
| 3658 | return fnext |
| 3659 | } |
| 3660 | } else { |
| 3661 | i := n.findex |
| 3662 | n.exec = func(f *frame) bltn { |
| 3663 | getFrame(f, l).data[i], _ = value(f).Recv() |
| 3664 | return tnext |
| 3665 | } |
| 3666 | } |
| 3667 | } |
| 3668 | } |
| 3669 | |
| 3670 | func recv2(n *node) { |
| 3671 | vchan := genValue(n.child[0]) // chan |
| 3672 | vres := genValue(n.anc.child[0]) // result |
| 3673 | vok := genValue(n.anc.child[1]) // status |
| 3674 | tnext := getExec(n.tnext) |
| 3675 | |
| 3676 | if n.interp.cancelChan { |
| 3677 | // Cancellable channel read |
| 3678 | n.exec = func(f *frame) bltn { |
| 3679 | ch, result, status := vchan(f), vres(f), vok(f) |
| 3680 | // Fast: channel read doesn't block |
| 3681 | if v, ok := ch.TryRecv(); ok { |
| 3682 | result.Set(v) |
| 3683 | status.SetBool(true) |
| 3684 | return tnext |
| 3685 | } |
| 3686 | // Slow: channel is blocked, allow cancel |
| 3687 | f.mutex.RLock() |
| 3688 | done := f.done |
| 3689 | f.mutex.RUnlock() |
| 3690 | |
| 3691 | chosen, v, ok := reflect.Select([]reflect.SelectCase{done, {Dir: reflect.SelectRecv, Chan: ch}}) |
| 3692 | if chosen == 0 { |
| 3693 | return nil |
| 3694 | } |
| 3695 | result.Set(v) |
| 3696 | status.SetBool(ok) |
| 3697 | return tnext |
| 3698 | } |
| 3699 | } else { |
| 3700 | // Blocking channel read (less overhead) |
| 3701 | n.exec = func(f *frame) bltn { |
| 3702 | v, ok := vchan(f).Recv() |
| 3703 | vres(f).Set(v) |
| 3704 | vok(f).SetBool(ok) |
| 3705 | return tnext |
| 3706 | } |
| 3707 | } |
| 3708 | } |
| 3709 | |
| 3710 | func convertLiteralValue(n *node, t reflect.Type) { |
| 3711 | switch { |
| 3712 | case n.typ.cat == nilT: |
| 3713 | // Create a zero value of target type. |
| 3714 | n.rval = reflect.New(t).Elem() |
| 3715 | case !(n.kind == basicLit || n.rval.IsValid()) || t == nil || t.Kind() == reflect.Interface || t == valueInterfaceType || t.Kind() == reflect.Slice && t.Elem().Kind() == reflect.Interface: |
| 3716 | // Skip non-constant values, undefined target type or interface target type. |
| 3717 | case n.rval.IsValid(): |
| 3718 | // Convert constant value to target type. |
| 3719 | convertConstantValue(n) |
| 3720 | n.rval = n.rval.Convert(t) |
| 3721 | default: |
| 3722 | // Create a zero value of target type. |
| 3723 | n.rval = reflect.New(t).Elem() |
| 3724 | } |
| 3725 | } |
| 3726 | |
| 3727 | func convertConstantValue(n *node) { |
| 3728 | if !n.rval.IsValid() { |
| 3729 | return |
| 3730 | } |
| 3731 | c, ok := n.rval.Interface().(constant.Value) |
| 3732 | if !ok { |
| 3733 | return |
| 3734 | } |
| 3735 | |
| 3736 | var v reflect.Value |
| 3737 | |
| 3738 | switch c.Kind() { |
| 3739 | case constant.Bool: |
| 3740 | v = reflect.ValueOf(constant.BoolVal(c)) |
| 3741 | case constant.String: |
| 3742 | v = reflect.ValueOf(constant.StringVal(c)) |
| 3743 | case constant.Int: |
| 3744 | i, x := constant.Int64Val(c) |
| 3745 | if !x { |
| 3746 | panic(n.cfgErrorf("constant %s overflows int64", c.ExactString())) |
| 3747 | } |
| 3748 | v = reflect.ValueOf(int(i)) |
| 3749 | case constant.Float: |
| 3750 | f, _ := constant.Float64Val(c) |
| 3751 | v = reflect.ValueOf(f) |
| 3752 | case constant.Complex: |
| 3753 | r, _ := constant.Float64Val(constant.Real(c)) |
| 3754 | i, _ := constant.Float64Val(constant.Imag(c)) |
| 3755 | v = reflect.ValueOf(complex(r, i)) |
| 3756 | } |
| 3757 | |
| 3758 | n.rval = v.Convert(n.typ.TypeOf()) |
| 3759 | } |
| 3760 | |
| 3761 | // Write to a channel. |
| 3762 | func send(n *node) { |
| 3763 | next := getExec(n.tnext) |
| 3764 | c0, c1 := n.child[0], n.child[1] |
| 3765 | value0 := genValue(c0) // Send channel. |
| 3766 | value1 := genDestValue(c0.typ.elem(), c1) |
| 3767 | |
| 3768 | if !n.interp.cancelChan { |
| 3769 | // Send is non-cancellable, has the least overhead. |
| 3770 | n.exec = func(f *frame) bltn { |
| 3771 | value0(f).Send(value1(f)) |
| 3772 | return next |
| 3773 | } |
| 3774 | return |
| 3775 | } |
| 3776 | |
| 3777 | // Send is cancellable, may have some overhead. |
| 3778 | n.exec = func(f *frame) bltn { |
| 3779 | ch, data := value0(f), value1(f) |
| 3780 | // Fast: send on channel doesn't block. |
| 3781 | if ok := ch.TrySend(data); ok { |
| 3782 | return next |
| 3783 | } |
| 3784 | // Slow: send on channel blocks, allow cancel. |
| 3785 | f.mutex.RLock() |
| 3786 | done := f.done |
| 3787 | f.mutex.RUnlock() |
| 3788 | |
| 3789 | chosen, _, _ := reflect.Select([]reflect.SelectCase{done, {Dir: reflect.SelectSend, Chan: ch, Send: data}}) |
| 3790 | if chosen == 0 { |
| 3791 | return nil |
| 3792 | } |
| 3793 | return next |
| 3794 | } |
| 3795 | } |
| 3796 | |
| 3797 | func clauseChanDir(n *node) (*node, *node, *node, reflect.SelectDir) { |
| 3798 | dir := reflect.SelectDefault |
| 3799 | var nod, assigned, ok *node |
| 3800 | var stop bool |
| 3801 | |
| 3802 | n.Walk(func(m *node) bool { |
| 3803 | switch m.action { |
| 3804 | case aRecv: |
| 3805 | dir = reflect.SelectRecv |
| 3806 | nod = m.child[0] |
| 3807 | switch m.anc.action { |
| 3808 | case aAssign: |
| 3809 | assigned = m.anc.child[0] |
| 3810 | case aAssignX: |
| 3811 | assigned = m.anc.child[0] |
| 3812 | ok = m.anc.child[1] |
| 3813 | } |
| 3814 | stop = true |
| 3815 | case aSend: |
| 3816 | dir = reflect.SelectSend |
| 3817 | nod = m.child[0] |
| 3818 | assigned = m.child[1] |
| 3819 | stop = true |
| 3820 | } |
| 3821 | return !stop |
| 3822 | }, nil) |
| 3823 | return nod, assigned, ok, dir |
| 3824 | } |
| 3825 | |
| 3826 | func _select(n *node) { |
| 3827 | nbClause := len(n.child) |
| 3828 | chans := make([]*node, nbClause) |
| 3829 | assigned := make([]*node, nbClause) |
| 3830 | ok := make([]*node, nbClause) |
| 3831 | clause := make([]bltn, nbClause) |
| 3832 | chanValues := make([]func(*frame) reflect.Value, nbClause) |
| 3833 | assignedValues := make([]func(*frame) reflect.Value, nbClause) |
| 3834 | okValues := make([]func(*frame) reflect.Value, nbClause) |
| 3835 | cases := make([]reflect.SelectCase, nbClause+1) |
| 3836 | next := getExec(n.tnext) |
| 3837 | |
| 3838 | for i := 0; i < nbClause; i++ { |
| 3839 | cl := n.child[i] |
| 3840 | if cl.kind == commClauseDefault { |
| 3841 | cases[i].Dir = reflect.SelectDefault |
| 3842 | if len(cl.child) == 0 { |
| 3843 | clause[i] = func(*frame) bltn { return next } |
| 3844 | } else { |
| 3845 | clause[i] = getExec(cl.child[0].start) |
| 3846 | } |
| 3847 | continue |
| 3848 | } |
| 3849 | // The comm clause is in send or recv direction. |
| 3850 | switch c0 := cl.child[0]; { |
| 3851 | case len(cl.child) > 1: |
| 3852 | // The comm clause contains a channel operation and a clause body. |
| 3853 | clause[i] = getExec(cl.child[1].start) |
| 3854 | chans[i], assigned[i], ok[i], cases[i].Dir = clauseChanDir(c0) |
| 3855 | chanValues[i] = genValue(chans[i]) |
| 3856 | if assigned[i] != nil { |
| 3857 | assignedValues[i] = genValue(assigned[i]) |
| 3858 | } |
| 3859 | if ok[i] != nil { |
| 3860 | okValues[i] = genValue(ok[i]) |
| 3861 | } |
| 3862 | case c0.kind == exprStmt && len(c0.child) == 1 && c0.child[0].action == aRecv: |
| 3863 | // The comm clause has an empty body clause after channel receive. |
| 3864 | chanValues[i] = genValue(c0.child[0].child[0]) |
| 3865 | cases[i].Dir = reflect.SelectRecv |
| 3866 | clause[i] = func(*frame) bltn { return next } |
| 3867 | case c0.kind == sendStmt: |
| 3868 | // The comm clause as an empty body clause after channel send. |
| 3869 | chanValues[i] = genValue(c0.child[0]) |
| 3870 | cases[i].Dir = reflect.SelectSend |
| 3871 | assignedValues[i] = genValue(c0.child[1]) |
| 3872 | clause[i] = func(*frame) bltn { return next } |
| 3873 | } |
| 3874 | } |
| 3875 | |
| 3876 | n.exec = func(f *frame) bltn { |
| 3877 | f.mutex.RLock() |
| 3878 | cases[nbClause] = f.done |
| 3879 | f.mutex.RUnlock() |
| 3880 | |
| 3881 | for i := range cases[:nbClause] { |
| 3882 | switch cases[i].Dir { |
| 3883 | case reflect.SelectRecv: |
| 3884 | cases[i].Chan = chanValues[i](f) |
| 3885 | case reflect.SelectSend: |
| 3886 | cases[i].Chan = chanValues[i](f) |
| 3887 | cases[i].Send = assignedValues[i](f) |
| 3888 | case reflect.SelectDefault: |
| 3889 | // Keep zero values for comm clause |
| 3890 | } |
| 3891 | } |
| 3892 | j, v, s := reflect.Select(cases) |
| 3893 | if j == nbClause { |
| 3894 | return nil |
| 3895 | } |
| 3896 | if cases[j].Dir == reflect.SelectRecv && assignedValues[j] != nil { |
| 3897 | assignedValues[j](f).Set(v) |
| 3898 | if ok[j] != nil { |
| 3899 | okValues[j](f).SetBool(s) |
| 3900 | } |
| 3901 | } |
| 3902 | return clause[j] |
| 3903 | } |
| 3904 | } |
| 3905 | |
| 3906 | // slice expression: array[low:high:max]. |
| 3907 | func slice(n *node) { |
| 3908 | i := n.findex |
| 3909 | l := n.level |
| 3910 | next := getExec(n.tnext) |
| 3911 | value0 := genValueArray(n.child[0]) // array |
| 3912 | value1 := genValue(n.child[1]) // low (if 2 or 3 args) or high (if 1 arg) |
| 3913 | |
| 3914 | switch len(n.child) { |
| 3915 | case 2: |
| 3916 | n.exec = func(f *frame) bltn { |
| 3917 | a := value0(f) |
| 3918 | getFrame(f, l).data[i] = a.Slice(int(vInt(value1(f))), a.Len()) |
| 3919 | return next |
| 3920 | } |
| 3921 | case 3: |
| 3922 | value2 := genValue(n.child[2]) // max |
| 3923 | |
| 3924 | n.exec = func(f *frame) bltn { |
| 3925 | a := value0(f) |
| 3926 | getFrame(f, l).data[i] = a.Slice(int(vInt(value1(f))), int(vInt(value2(f)))) |
| 3927 | return next |
| 3928 | } |
| 3929 | case 4: |
| 3930 | value2 := genValue(n.child[2]) |
| 3931 | value3 := genValue(n.child[3]) |
| 3932 | |
| 3933 | n.exec = func(f *frame) bltn { |
| 3934 | a := value0(f) |
| 3935 | getFrame(f, l).data[i] = a.Slice3(int(vInt(value1(f))), int(vInt(value2(f))), int(vInt(value3(f)))) |
| 3936 | return next |
| 3937 | } |
| 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | // slice expression, no low value: array[:high:max]. |
| 3942 | func slice0(n *node) { |
| 3943 | i := n.findex |
| 3944 | l := n.level |
| 3945 | next := getExec(n.tnext) |
| 3946 | value0 := genValueArray(n.child[0]) |
| 3947 | |
| 3948 | switch len(n.child) { |
| 3949 | case 1: |
| 3950 | n.exec = func(f *frame) bltn { |
| 3951 | a := value0(f) |
| 3952 | getFrame(f, l).data[i] = a.Slice(0, a.Len()) |
| 3953 | return next |
| 3954 | } |
| 3955 | case 2: |
| 3956 | value1 := genValue(n.child[1]) |
| 3957 | n.exec = func(f *frame) bltn { |
| 3958 | a := value0(f) |
| 3959 | getFrame(f, l).data[i] = a.Slice(0, int(vInt(value1(f)))) |
| 3960 | return next |
| 3961 | } |
| 3962 | case 3: |
| 3963 | value1 := genValue(n.child[1]) |
| 3964 | value2 := genValue(n.child[2]) |
| 3965 | n.exec = func(f *frame) bltn { |
| 3966 | a := value0(f) |
| 3967 | getFrame(f, l).data[i] = a.Slice3(0, int(vInt(value1(f))), int(vInt(value2(f)))) |
| 3968 | return next |
| 3969 | } |
| 3970 | } |
| 3971 | } |
| 3972 | |
| 3973 | func isNilChild(child int) func(n *node) { |
| 3974 | return func(n *node) { |
| 3975 | var value func(*frame) reflect.Value |
| 3976 | child := n.child[child] |
| 3977 | value = genValue(child) |
| 3978 | typ := n.typ.concrete().TypeOf() |
| 3979 | isInterface := n.typ.TypeOf().Kind() == reflect.Interface |
| 3980 | tnext := getExec(n.tnext) |
| 3981 | dest := genValue(n) |
| 3982 | if n.fnext == nil { |
| 3983 | if !isInterfaceSrc(child.typ) { |
| 3984 | if isInterface { |
| 3985 | n.exec = func(f *frame) bltn { |
| 3986 | dest(f).Set(reflect.ValueOf(value(f).IsNil()).Convert(typ)) |
| 3987 | return tnext |
| 3988 | } |
| 3989 | return |
| 3990 | } |
| 3991 | n.exec = func(f *frame) bltn { |
| 3992 | dest(f).SetBool(value(f).IsNil()) |
| 3993 | return tnext |
| 3994 | } |
| 3995 | return |
| 3996 | } |
| 3997 | if isInterface { |
| 3998 | n.exec = func(f *frame) bltn { |
| 3999 | v := value(f) |
| 4000 | var r bool |
| 4001 | if vi, ok := v.Interface().(valueInterface); ok { |
| 4002 | r = (vi == valueInterface{} || vi.node.kind == basicLit && vi.node.typ.cat == nilT) |
| 4003 | } else { |
| 4004 | r = v.IsNil() |
| 4005 | } |
| 4006 | dest(f).Set(reflect.ValueOf(r).Convert(typ)) |
| 4007 | return tnext |
| 4008 | } |
| 4009 | return |
| 4010 | } |
| 4011 | n.exec = func(f *frame) bltn { |
| 4012 | v := value(f) |
| 4013 | var r bool |
| 4014 | if vi, ok := v.Interface().(valueInterface); ok { |
| 4015 | r = (vi == valueInterface{} || vi.node.kind == basicLit && vi.node.typ.cat == nilT) |
| 4016 | } else { |
| 4017 | r = v.IsNil() |
| 4018 | } |
| 4019 | dest(f).SetBool(r) |
| 4020 | return tnext |
| 4021 | } |
| 4022 | return |
| 4023 | } |
| 4024 | |
| 4025 | fnext := getExec(n.fnext) |
| 4026 | |
| 4027 | if !isInterfaceSrc(child.typ) { |
| 4028 | n.exec = func(f *frame) bltn { |
| 4029 | if value(f).IsNil() { |
| 4030 | dest(f).SetBool(true) |
| 4031 | return tnext |
| 4032 | } |
| 4033 | dest(f).SetBool(false) |
| 4034 | return fnext |
| 4035 | } |
| 4036 | return |
| 4037 | } |
| 4038 | |
| 4039 | n.exec = func(f *frame) bltn { |
| 4040 | v := value(f) |
| 4041 | if vi, ok := v.Interface().(valueInterface); ok { |
| 4042 | if (vi == valueInterface{} || vi.node.kind == basicLit && vi.node.typ.cat == nilT) { |
| 4043 | dest(f).SetBool(true) |
| 4044 | return tnext |
| 4045 | } |
| 4046 | dest(f).SetBool(false) |
| 4047 | return fnext |
| 4048 | } |
| 4049 | if v.IsNil() { |
| 4050 | dest(f).SetBool(true) |
| 4051 | return tnext |
| 4052 | } |
| 4053 | dest(f).SetBool(false) |
| 4054 | return fnext |
| 4055 | } |
| 4056 | } |
| 4057 | } |
| 4058 | |
| 4059 | func isNotNil(n *node) { |
| 4060 | var value func(*frame) reflect.Value |
| 4061 | c0 := n.child[0] |
| 4062 | value = genValue(c0) |
| 4063 | typ := n.typ.concrete().TypeOf() |
| 4064 | isInterface := n.typ.TypeOf().Kind() == reflect.Interface |
| 4065 | tnext := getExec(n.tnext) |
| 4066 | dest := genValue(n) |
| 4067 | |
| 4068 | if n.fnext == nil { |
| 4069 | if isInterfaceSrc(c0.typ) && c0.typ.TypeOf() != valueInterfaceType { |
| 4070 | if isInterface { |
| 4071 | n.exec = func(f *frame) bltn { |
| 4072 | dest(f).Set(reflect.ValueOf(!value(f).IsNil()).Convert(typ)) |
| 4073 | return tnext |
| 4074 | } |
| 4075 | return |
| 4076 | } |
| 4077 | n.exec = func(f *frame) bltn { |
| 4078 | dest(f).SetBool(!value(f).IsNil()) |
| 4079 | return tnext |
| 4080 | } |
| 4081 | return |
| 4082 | } |
| 4083 | |
| 4084 | if isInterface { |
| 4085 | n.exec = func(f *frame) bltn { |
| 4086 | v := value(f) |
| 4087 | var r bool |
| 4088 | if vi, ok := v.Interface().(valueInterface); ok { |
| 4089 | r = (vi == valueInterface{} || vi.node.kind == basicLit && vi.node.typ.cat == nilT) |
| 4090 | } else { |
| 4091 | r = v.IsNil() |
| 4092 | } |
| 4093 | dest(f).Set(reflect.ValueOf(!r).Convert(typ)) |
| 4094 | return tnext |
| 4095 | } |
| 4096 | return |
| 4097 | } |
| 4098 | n.exec = func(f *frame) bltn { |
| 4099 | v := value(f) |
| 4100 | var r bool |
| 4101 | if vi, ok := v.Interface().(valueInterface); ok { |
| 4102 | r = (vi == valueInterface{} || vi.node.kind == basicLit && vi.node.typ.cat == nilT) |
| 4103 | } else { |
| 4104 | r = v.IsNil() |
| 4105 | } |
| 4106 | dest(f).SetBool(!r) |
| 4107 | return tnext |
| 4108 | } |
| 4109 | return |
| 4110 | } |
| 4111 | |
| 4112 | fnext := getExec(n.fnext) |
| 4113 | |
| 4114 | if isInterfaceSrc(c0.typ) && c0.typ.TypeOf() != valueInterfaceType { |
| 4115 | n.exec = func(f *frame) bltn { |
| 4116 | if value(f).IsNil() { |
| 4117 | dest(f).SetBool(false) |
| 4118 | return fnext |
| 4119 | } |
| 4120 | dest(f).SetBool(true) |
| 4121 | return tnext |
| 4122 | } |
| 4123 | return |
| 4124 | } |
| 4125 | |
| 4126 | n.exec = func(f *frame) bltn { |
| 4127 | v := value(f) |
| 4128 | if vi, ok := v.Interface().(valueInterface); ok { |
| 4129 | if (vi == valueInterface{} || vi.node.kind == basicLit && vi.node.typ.cat == nilT) { |
| 4130 | dest(f).SetBool(false) |
| 4131 | return fnext |
| 4132 | } |
| 4133 | dest(f).SetBool(true) |
| 4134 | return tnext |
| 4135 | } |
| 4136 | if v.IsNil() { |
| 4137 | dest(f).SetBool(false) |
| 4138 | return fnext |
| 4139 | } |
| 4140 | dest(f).SetBool(true) |
| 4141 | return tnext |
| 4142 | } |
| 4143 | } |
| 4144 | |
| 4145 | func complexConst(n *node) { |
| 4146 | if v0, v1 := n.child[1].rval, n.child[2].rval; v0.IsValid() && v1.IsValid() { |
| 4147 | n.rval = reflect.ValueOf(complex(vFloat(v0), vFloat(v1))) |
| 4148 | n.gen = nop |
| 4149 | } |
| 4150 | } |
| 4151 | |
| 4152 | func imagConst(n *node) { |
| 4153 | if v := n.child[1].rval; v.IsValid() { |
| 4154 | n.rval = reflect.ValueOf(imag(v.Complex())) |
| 4155 | n.gen = nop |
| 4156 | } |
| 4157 | } |
| 4158 | |
| 4159 | func realConst(n *node) { |
| 4160 | if v := n.child[1].rval; v.IsValid() { |
| 4161 | n.rval = reflect.ValueOf(real(v.Complex())) |
| 4162 | n.gen = nop |
| 4163 | } |
| 4164 | } |