| 1 | ## oils_failures_allowed: 0
|
| 2 |
|
| 3 | #### Open proc (any number of args)
|
| 4 | shopt --set parse_proc
|
| 5 |
|
| 6 | proc f {
|
| 7 | var x = 42
|
| 8 | return $x
|
| 9 | }
|
| 10 | # this gets called with 3 args then?
|
| 11 | f a b c
|
| 12 | echo status=$?
|
| 13 | ## STDOUT:
|
| 14 | status=42
|
| 15 | ## END
|
| 16 |
|
| 17 | #### Closed proc with no args, passed too many
|
| 18 | shopt --set parse_proc
|
| 19 |
|
| 20 | proc f() {
|
| 21 | return 42
|
| 22 | }
|
| 23 | f
|
| 24 | echo status=$?
|
| 25 |
|
| 26 | f a b # status 2
|
| 27 |
|
| 28 | ## status: 3
|
| 29 | ## STDOUT:
|
| 30 | status=42
|
| 31 | ## END
|
| 32 |
|
| 33 | #### Open proc has ARGV
|
| 34 | shopt -s ysh:all
|
| 35 | proc foo {
|
| 36 | echo ARGV @ARGV
|
| 37 | # do we care about this? I think we want to syntactically remove it from YSH
|
| 38 | # but it can still be used for legacy
|
| 39 | echo dollar-at "$@"
|
| 40 | }
|
| 41 | builtin set -- a b c
|
| 42 | foo x y z
|
| 43 | ## STDOUT:
|
| 44 | ARGV x y z
|
| 45 | dollar-at a b c
|
| 46 | ## END
|
| 47 |
|
| 48 | #### Closed proc has empty "$@" or ARGV
|
| 49 | shopt -s ysh:all
|
| 50 |
|
| 51 | proc foo(d, e, f) {
|
| 52 | write params $d $e $f
|
| 53 | argv.py dollar-at "$@"
|
| 54 | argv.py ARGV @ARGV
|
| 55 | }
|
| 56 | builtin set -- a b c
|
| 57 | foo x y z
|
| 58 | ## STDOUT:
|
| 59 | params
|
| 60 | x
|
| 61 | y
|
| 62 | z
|
| 63 | ['dollar-at', 'a', 'b', 'c']
|
| 64 | ['ARGV']
|
| 65 | ## END
|
| 66 |
|
| 67 | #### Proc with default args
|
| 68 | shopt --set parse_proc
|
| 69 |
|
| 70 | proc f(x='foo') {
|
| 71 | echo x=$x
|
| 72 | }
|
| 73 | f
|
| 74 | ## STDOUT:
|
| 75 | x=foo
|
| 76 | ## END
|
| 77 |
|
| 78 | #### Proc with word params
|
| 79 | shopt --set parse_proc
|
| 80 |
|
| 81 | # doesn't require ysh:all
|
| 82 | proc f(x, y, z) {
|
| 83 | echo $x $y $z
|
| 84 | var ret = 42
|
| 85 | return $ret
|
| 86 | }
|
| 87 | # this gets called with 3 args then?
|
| 88 | f a b c
|
| 89 | echo status=$?
|
| 90 | ## STDOUT:
|
| 91 | a b c
|
| 92 | status=42
|
| 93 | ## END
|
| 94 |
|
| 95 | #### Proc with ... "rest" word params
|
| 96 |
|
| 97 | # TODO: opts goes with this
|
| 98 | # var opt = grep_opts.parse(ARGV)
|
| 99 | #
|
| 100 | # func(**opt) # Assumes keyword args match?
|
| 101 | # parse :grep_opts :opt @ARGV
|
| 102 |
|
| 103 | shopt -s ysh:all
|
| 104 |
|
| 105 | proc f(...names) {
|
| 106 | write names: @names
|
| 107 | }
|
| 108 | # this gets called with 3 args then?
|
| 109 | f a b c
|
| 110 | echo status=$?
|
| 111 | ## STDOUT:
|
| 112 | names:
|
| 113 | a
|
| 114 | b
|
| 115 | c
|
| 116 | status=0
|
| 117 | ## END
|
| 118 |
|
| 119 | #### word rest params 2
|
| 120 | shopt --set ysh:all
|
| 121 |
|
| 122 | proc f(first, ...rest) { # @ means "the rest of the arguments"
|
| 123 | write --sep ' ' -- $first
|
| 124 | write --sep ' ' -- @rest # @ means "splice this array"
|
| 125 | }
|
| 126 | f a b c
|
| 127 | ## STDOUT:
|
| 128 | a
|
| 129 | b c
|
| 130 | ## END
|
| 131 |
|
| 132 | #### proc with typed args
|
| 133 | shopt --set ysh:upgrade
|
| 134 |
|
| 135 | # TODO: duplicate param names aren't allowed
|
| 136 | proc p (a; mylist, mydict; opt Int = 42) {
|
| 137 | pp test_ (a)
|
| 138 | pp test_ (mylist)
|
| 139 | pp test_ (mydict)
|
| 140 | #pp test_ (opt)
|
| 141 | }
|
| 142 |
|
| 143 | p WORD ([1,2,3], {name: 'bob'})
|
| 144 |
|
| 145 | echo ---
|
| 146 |
|
| 147 | p x (:| a b |, {bob: 42}, a = 5)
|
| 148 |
|
| 149 | ## STDOUT:
|
| 150 | (Str) "WORD"
|
| 151 | (List) [1,2,3]
|
| 152 | (Dict) {"name":"bob"}
|
| 153 | ---
|
| 154 | (Str) "x"
|
| 155 | (List) ["a","b"]
|
| 156 | (Dict) {"bob":42}
|
| 157 | ## END
|
| 158 |
|
| 159 | #### Proc name-with-hyphen
|
| 160 | shopt --set parse_proc parse_at
|
| 161 |
|
| 162 | proc name-with-hyphen {
|
| 163 | echo @ARGV
|
| 164 | }
|
| 165 | name-with-hyphen x y z
|
| 166 | ## STDOUT:
|
| 167 | x y z
|
| 168 | ## END
|
| 169 |
|
| 170 | #### Proc with block arg
|
| 171 | shopt --set ysh:upgrade
|
| 172 |
|
| 173 | # TODO: Test more of this
|
| 174 | proc f(x, y ; ; ; block) {
|
| 175 | echo f word $x $y
|
| 176 |
|
| 177 | if (block) {
|
| 178 | call io->eval(block)
|
| 179 | }
|
| 180 | }
|
| 181 | f a b { echo FFF }
|
| 182 |
|
| 183 | # With varargs and block
|
| 184 | shopt --set parse_proc
|
| 185 |
|
| 186 | proc g(x, y, ...rest ; ; ; block) {
|
| 187 | echo g word $x $y
|
| 188 | echo g rest @rest
|
| 189 |
|
| 190 | if (block) {
|
| 191 | call io->eval(block)
|
| 192 | }
|
| 193 | }
|
| 194 | g a b c d {
|
| 195 | echo GGG
|
| 196 | }
|
| 197 |
|
| 198 | ## STDOUT:
|
| 199 | f word a b
|
| 200 | FFF
|
| 201 | g word a b
|
| 202 | g rest c d
|
| 203 | GGG
|
| 204 | ## END
|
| 205 |
|
| 206 | #### proc returning wrong type
|
| 207 | shopt --set parse_proc
|
| 208 |
|
| 209 | # this should print an error message
|
| 210 | proc f {
|
| 211 | var a = %(one two)
|
| 212 | return $a
|
| 213 | }
|
| 214 | f
|
| 215 | ## status: 3
|
| 216 | ## STDOUT:
|
| 217 | ## END
|
| 218 |
|
| 219 | #### proc returning invalid string
|
| 220 | shopt --set parse_proc
|
| 221 |
|
| 222 | # this should print an error message
|
| 223 | proc f {
|
| 224 | var s = 'not an integer status'
|
| 225 | return $s
|
| 226 | }
|
| 227 | f
|
| 228 | ## status: 1
|
| 229 | ## STDOUT:
|
| 230 | ## END
|
| 231 |
|
| 232 | #### 'return' doesn't accept expressions
|
| 233 | proc p {
|
| 234 | return 1 + 2
|
| 235 | }
|
| 236 | p
|
| 237 | ## status: 2
|
| 238 | ## STDOUT:
|
| 239 | ## END
|
| 240 |
|
| 241 | #### procs are in same namespace as variables
|
| 242 | shopt --set parse_proc
|
| 243 |
|
| 244 | proc myproc {
|
| 245 | echo hi
|
| 246 | }
|
| 247 |
|
| 248 | echo "myproc is a $[type(myproc)]"
|
| 249 |
|
| 250 | ## STDOUT:
|
| 251 | myproc is a Proc
|
| 252 | ## END
|
| 253 |
|
| 254 | #### Nested proc is allowed
|
| 255 | shopt --set parse_proc
|
| 256 |
|
| 257 | proc f {
|
| 258 | proc g {
|
| 259 | echo 'G'
|
| 260 | }
|
| 261 | g
|
| 262 | }
|
| 263 | f
|
| 264 | g # g is defined in the local scope of f
|
| 265 | ## status: 127
|
| 266 | ## STDOUT:
|
| 267 | G
|
| 268 | ## END
|
| 269 |
|
| 270 | #### Procs defined inside compound statements
|
| 271 |
|
| 272 | shopt --set ysh:upgrade
|
| 273 |
|
| 274 | var p = null
|
| 275 | for x in 1 2 {
|
| 276 | proc inner {
|
| 277 | echo 'loop'
|
| 278 | }
|
| 279 | setvar p = inner
|
| 280 | }
|
| 281 | p
|
| 282 |
|
| 283 | {
|
| 284 | proc p {
|
| 285 | echo 'brace'
|
| 286 | }
|
| 287 | }
|
| 288 | p
|
| 289 |
|
| 290 | ## STDOUT:
|
| 291 | loop
|
| 292 | brace
|
| 293 | ## END
|
| 294 |
|
| 295 | #### Block can be passed literally, or as expression in third arg group
|
| 296 | shopt --set ysh:upgrade
|
| 297 |
|
| 298 | proc p ( ; ; ; block) {
|
| 299 | call io->eval(block)
|
| 300 | }
|
| 301 |
|
| 302 | p { echo literal }
|
| 303 |
|
| 304 | var block = ^(echo expression)
|
| 305 | p (; ; block)
|
| 306 |
|
| 307 | ## STDOUT:
|
| 308 | literal
|
| 309 | expression
|
| 310 | ## END
|
| 311 |
|
| 312 | #### Pass through all 4 kinds of args
|
| 313 |
|
| 314 | shopt --set ysh:upgrade
|
| 315 |
|
| 316 | proc p2 (...words; ...typed; ...named; block) {
|
| 317 | pp test_ (words)
|
| 318 | pp test_ (typed)
|
| 319 | pp test_ (named)
|
| 320 | #pp test_ (block)
|
| 321 | # To avoid <Block 0x??> - could change pp test_
|
| 322 | echo $[type(block)]
|
| 323 | }
|
| 324 |
|
| 325 | proc p1 (...words; ...typed; ...named; block) {
|
| 326 | p2 @words (...typed; ...named; block)
|
| 327 | }
|
| 328 |
|
| 329 | p2 a b ('c', 'd', n=99) {
|
| 330 | echo literal
|
| 331 | }
|
| 332 | echo
|
| 333 |
|
| 334 | # Same thing
|
| 335 | var block = ^(echo expression)
|
| 336 |
|
| 337 | # Note: you need the second explicit ;
|
| 338 |
|
| 339 | p2 a b ('c', 'd'; n=99; block)
|
| 340 | echo
|
| 341 |
|
| 342 | # what happens when you do this?
|
| 343 | p2 a b ('c', 'd'; n=99; block) {
|
| 344 | echo duplicate
|
| 345 | }
|
| 346 |
|
| 347 | ## status: 1
|
| 348 | ## STDOUT:
|
| 349 | (List) ["a","b"]
|
| 350 | (List) ["c","d"]
|
| 351 | (Dict) {"n":99}
|
| 352 | Command
|
| 353 |
|
| 354 | (List) ["a","b"]
|
| 355 | (List) ["c","d"]
|
| 356 | (Dict) {"n":99}
|
| 357 | Command
|
| 358 |
|
| 359 | ## END
|
| 360 |
|
| 361 | #### Global and local ARGV, like "$@"
|
| 362 |
|
| 363 | $SH -o ysh:upgrade -c '
|
| 364 | argv.py "$@"
|
| 365 | argv.py @ARGV
|
| 366 | #argv.py "${ARGV[@]}" # not useful, but it works!
|
| 367 |
|
| 368 | set -- "a b" c
|
| 369 | argv.py "$@"
|
| 370 | argv.py @ARGV # separate from the argv stack
|
| 371 |
|
| 372 | f() {
|
| 373 | argv.py "$@"
|
| 374 | argv.py @ARGV # separate from the argv stack
|
| 375 | }
|
| 376 | f 1 "2 3"
|
| 377 | '
|
| 378 |
|
| 379 | ## STDOUT:
|
| 380 | []
|
| 381 | []
|
| 382 | ['a b', 'c']
|
| 383 | []
|
| 384 | ['1', '2 3']
|
| 385 | []
|
| 386 | ## END
|
| 387 |
|
| 388 |
|
| 389 | #### Mutating global ARGV
|
| 390 |
|
| 391 | $SH -o ysh:upgrade -c '
|
| 392 | argv.py global @ARGV
|
| 393 |
|
| 394 | # should not be ignored
|
| 395 | call ARGV->append("GG")
|
| 396 |
|
| 397 | argv.py global @ARGV
|
| 398 | '
|
| 399 | ## STDOUT:
|
| 400 | ['global']
|
| 401 | ['global', 'GG']
|
| 402 | ## END
|
| 403 |
|
| 404 | #### Mutating local ARGV
|
| 405 |
|
| 406 | $SH -o ysh:upgrade -c '
|
| 407 | argv.py global @ARGV
|
| 408 |
|
| 409 | proc p {
|
| 410 | argv.py @ARGV
|
| 411 | call ARGV->append("LL")
|
| 412 | argv.py @ARGV
|
| 413 | }
|
| 414 |
|
| 415 | p local @ARGV
|
| 416 |
|
| 417 | argv.py global @ARGV
|
| 418 |
|
| 419 | ' dummy0 'a b' c
|
| 420 |
|
| 421 | ## STDOUT:
|
| 422 | ['global', 'a b', 'c']
|
| 423 | ['local', 'a b', 'c']
|
| 424 | ['local', 'a b', 'c', 'LL']
|
| 425 | ['global', 'a b', 'c']
|
| 426 | ## END
|
| 427 |
|
| 428 |
|
| 429 | #### typed proc allows all kinds of args
|
| 430 | shopt -s ysh:upgrade
|
| 431 |
|
| 432 | typed proc p (w; t; n; block) {
|
| 433 | pp test_ (w)
|
| 434 | pp test_ (t)
|
| 435 | pp test_ (n)
|
| 436 | echo $[type(block)]
|
| 437 | }
|
| 438 |
|
| 439 | p word (42, n=99) {
|
| 440 | echo block
|
| 441 | }
|
| 442 |
|
| 443 |
|
| 444 | ## STDOUT:
|
| 445 | (Str) "word"
|
| 446 | (Int) 42
|
| 447 | (Int) 99
|
| 448 | Command
|
| 449 | ## END
|
| 450 |
|
| 451 | #### can unset procs without -f
|
| 452 | shopt -s ysh:upgrade
|
| 453 |
|
| 454 | proc foo() {
|
| 455 | echo bar
|
| 456 | }
|
| 457 |
|
| 458 | try { foo }
|
| 459 | echo status=$[_error.code]
|
| 460 |
|
| 461 | pp test_ (foo)
|
| 462 | unset foo
|
| 463 | #pp test_ (foo)
|
| 464 |
|
| 465 | try { foo }
|
| 466 | echo status=$[_error.code]
|
| 467 |
|
| 468 | ## STDOUT:
|
| 469 | bar
|
| 470 | status=0
|
| 471 | <Proc>
|
| 472 | status=127
|
| 473 | ## END
|
| 474 |
|
| 475 | #### procs shadow sh-funcs
|
| 476 | shopt -s ysh:upgrade
|
| 477 |
|
| 478 | f() {
|
| 479 | echo sh-func
|
| 480 | }
|
| 481 |
|
| 482 | proc f {
|
| 483 | echo proc
|
| 484 | }
|
| 485 |
|
| 486 | f
|
| 487 | ## STDOUT:
|
| 488 | proc
|
| 489 | ## END
|
| 490 |
|
| 491 | #### first word skips non-proc variables
|
| 492 | shopt -s ysh:upgrade
|
| 493 |
|
| 494 | grep() {
|
| 495 | echo 'sh-func grep'
|
| 496 | }
|
| 497 |
|
| 498 | var grep = 'variable grep'
|
| 499 |
|
| 500 | grep
|
| 501 |
|
| 502 | # We first find `var grep`, but it's a Str not a Proc, so we skip it and then
|
| 503 | # find `function grep`.
|
| 504 |
|
| 505 | ## STDOUT:
|
| 506 | sh-func grep
|
| 507 | ## END
|
| 508 |
|
| 509 | #### proc resolution changes with the local scope
|
| 510 | shopt -s ysh:upgrade
|
| 511 |
|
| 512 | proc foo {
|
| 513 | echo foo
|
| 514 | }
|
| 515 |
|
| 516 | proc bar {
|
| 517 | echo bar
|
| 518 | }
|
| 519 |
|
| 520 | proc inner {
|
| 521 | var foo = bar
|
| 522 | foo # Will now reference `proc bar`
|
| 523 | }
|
| 524 |
|
| 525 | foo
|
| 526 | inner
|
| 527 | foo # Back to the global scope, foo still references `proc foo`
|
| 528 |
|
| 529 | # Without this behavior, features like `eval(b, vars={ flag: __flag })`, needed
|
| 530 | # by parseArgs, will not work. `eval` with `vars` adds a new frame to the end of
|
| 531 | # `mem.var_stack` with a local `flag` set to `proc __flag`. However, then we
|
| 532 | # cannot resolve `flag` by only checking `mem.var_stack[0]` like we could with
|
| 533 | # a proc declared normally, so we must search `mem.var_stack` from last to first.
|
| 534 |
|
| 535 | ## STDOUT:
|
| 536 | foo
|
| 537 | bar
|
| 538 | foo
|
| 539 | ## END
|
| 540 |
|
| 541 |
|
| 542 | #### procs are defined in local scope
|
| 543 | shopt -s ysh:upgrade
|
| 544 |
|
| 545 | proc gen-proc {
|
| 546 | eval 'proc localproc { echo hi }'
|
| 547 | pp frame_vars_
|
| 548 |
|
| 549 | }
|
| 550 |
|
| 551 | gen-proc
|
| 552 |
|
| 553 | # can't suppress 'grep' failure
|
| 554 | if false {
|
| 555 | try {
|
| 556 | pp frame_vars_ | grep localproc
|
| 557 | }
|
| 558 | pp test_ (_pipeline_status)
|
| 559 | #pp test_ (PIPESTATUS)
|
| 560 | }
|
| 561 |
|
| 562 | ## STDOUT:
|
| 563 | [frame_vars_] __E__ ARGV localproc
|
| 564 | ## END
|
| 565 |
|
| 566 |
|
| 567 | #### declare -f -F only prints shell functions
|
| 568 | shopt --set parse_proc
|
| 569 |
|
| 570 | myfunc() {
|
| 571 | echo hi
|
| 572 | }
|
| 573 |
|
| 574 | proc myproc {
|
| 575 | echo hi
|
| 576 | }
|
| 577 |
|
| 578 | declare -F
|
| 579 | echo ---
|
| 580 |
|
| 581 | declare -F myproc
|
| 582 | echo status=$?
|
| 583 |
|
| 584 | declare -f myproc
|
| 585 | echo status=$?
|
| 586 |
|
| 587 | ## status: 0
|
| 588 | ## STDOUT:
|
| 589 | declare -f myfunc
|
| 590 | ---
|
| 591 | status=1
|
| 592 | status=1
|
| 593 | ## END
|
| 594 |
|
| 595 | #### compgen -A function shows user-defined invokables - shell funcs, Proc, Obj
|
| 596 | shopt --set ysh:upgrade
|
| 597 |
|
| 598 | my-shell-func() {
|
| 599 | echo hi
|
| 600 | }
|
| 601 |
|
| 602 | proc myproc {
|
| 603 | echo hi
|
| 604 | }
|
| 605 |
|
| 606 | compgen -A function
|
| 607 |
|
| 608 | echo ---
|
| 609 |
|
| 610 | proc define-inner {
|
| 611 | eval 'proc inner { echo inner }'
|
| 612 | #eval 'proc myproc { echo inner }' # shadowed name
|
| 613 | compgen -A function
|
| 614 | }
|
| 615 | define-inner
|
| 616 |
|
| 617 | echo ---
|
| 618 |
|
| 619 | proc myinvoke (w; self) {
|
| 620 | pp test_ ([w, self])
|
| 621 | }
|
| 622 |
|
| 623 | var methods = Object(null, {__invoke__: myinvoke})
|
| 624 | var myobj = Object(methods, {})
|
| 625 |
|
| 626 | compgen -A function
|
| 627 |
|
| 628 | ## STDOUT:
|
| 629 | my-shell-func
|
| 630 | myproc
|
| 631 | ---
|
| 632 | define-inner
|
| 633 | inner
|
| 634 | my-shell-func
|
| 635 | myproc
|
| 636 | ---
|
| 637 | define-inner
|
| 638 | my-shell-func
|
| 639 | myinvoke
|
| 640 | myobj
|
| 641 | myproc
|
| 642 | ## END
|
| 643 |
|
| 644 | #### type / type -a builtin on invokables - shell func, proc, invokable
|
| 645 | shopt --set ysh:upgrade
|
| 646 |
|
| 647 | my-shell-func() {
|
| 648 | echo hi
|
| 649 | }
|
| 650 |
|
| 651 | proc myproc {
|
| 652 | echo hi
|
| 653 | }
|
| 654 |
|
| 655 | proc boundProc(; self) {
|
| 656 | echo hi
|
| 657 | }
|
| 658 |
|
| 659 | var methods = Object(null, {__invoke__: boundProc})
|
| 660 | var invokable = Object(methods, {})
|
| 661 |
|
| 662 | type -t my-shell-func
|
| 663 | type -t myproc
|
| 664 | type -t invokable
|
| 665 | try {
|
| 666 | type -t methods # not invokable!
|
| 667 | }
|
| 668 | echo $[_error.code]
|
| 669 |
|
| 670 | echo ---
|
| 671 |
|
| 672 | type my-shell-func
|
| 673 | type myproc
|
| 674 | type invokable
|
| 675 | try {
|
| 676 | type methods # not invokable!
|
| 677 | }
|
| 678 | echo $[_error.code]
|
| 679 |
|
| 680 | echo ---
|
| 681 |
|
| 682 | type -a my-shell-func
|
| 683 | type -a myproc
|
| 684 | type -a invokable
|
| 685 |
|
| 686 | echo ---
|
| 687 |
|
| 688 | if false { # can't redefine right now
|
| 689 | invokable() {
|
| 690 | echo sh-func
|
| 691 | }
|
| 692 | type -a invokable
|
| 693 | }
|
| 694 |
|
| 695 | ## STDOUT:
|
| 696 | function
|
| 697 | proc
|
| 698 | proc
|
| 699 | 1
|
| 700 | ---
|
| 701 | my-shell-func is a shell function
|
| 702 | my-shell-func() {
|
| 703 | echo hi
|
| 704 | }
|
| 705 | myproc is a YSH proc
|
| 706 | invokable is a YSH invokable
|
| 707 | 1
|
| 708 | ---
|
| 709 | my-shell-func is a shell function
|
| 710 | my-shell-func() {
|
| 711 | echo hi
|
| 712 | }
|
| 713 | myproc is a YSH proc
|
| 714 | invokable is a YSH invokable
|
| 715 | ---
|
| 716 | ## END
|
| 717 |
|
| 718 | #### type / type -a agrees on ordering of shell functions and procs
|
| 719 | shopt --set ysh:upgrade
|
| 720 |
|
| 721 | #type -t p
|
| 722 |
|
| 723 | p() {
|
| 724 | echo 'sh-func'
|
| 725 | }
|
| 726 | p
|
| 727 | type -t p
|
| 728 | echo
|
| 729 |
|
| 730 | proc p {
|
| 731 | echo 'YSH proc'
|
| 732 | }
|
| 733 | p
|
| 734 | type -t p
|
| 735 | ## STDOUT:
|
| 736 | sh-func
|
| 737 | function
|
| 738 |
|
| 739 | YSH proc
|
| 740 | proc
|
| 741 | ## END
|
| 742 |
|
| 743 | #### invokable Obj that doesn't declare self
|
| 744 | shopt --set ysh:upgrade
|
| 745 |
|
| 746 | proc boundProc(no_self; ) {
|
| 747 | echo 'bad'
|
| 748 | }
|
| 749 |
|
| 750 | var methods = Object(null, {__invoke__: boundProc})
|
| 751 | var invokable = Object(methods, {x: 3, y: 5})
|
| 752 |
|
| 753 | invokable no_self
|
| 754 |
|
| 755 | ## status: 3
|
| 756 | ## STDOUT:
|
| 757 | ## END
|
| 758 |
|
| 759 | #### invokable Obj is called with self
|
| 760 | shopt --set ysh:upgrade
|
| 761 |
|
| 762 | proc boundProc(; self) {
|
| 763 | echo "sum = $[self.x + self.y]"
|
| 764 | }
|
| 765 |
|
| 766 | var methods = Object(null, {__invoke__: boundProc})
|
| 767 | var invokable = Object(methods, {x: 3, y: 5})
|
| 768 |
|
| 769 | invokable
|
| 770 |
|
| 771 | ## STDOUT:
|
| 772 | sum = 8
|
| 773 | ## END
|
| 774 |
|
| 775 |
|
| 776 | #### invokable Obj with more typed args
|
| 777 | shopt --set ysh:upgrade
|
| 778 |
|
| 779 | proc myInvoke (word1, word2; self, int1, int2) {
|
| 780 | echo "sum = $[self.x + self.y]"
|
| 781 | pp test_ (self)
|
| 782 | pp test_ ([word1, word2, int1, int2])
|
| 783 | }
|
| 784 |
|
| 785 | # call it directly with 'self'
|
| 786 | myInvoke a b ({x: 0, y: 1}, 42, 43)
|
| 787 | echo
|
| 788 |
|
| 789 | var methods = Object(null, {__invoke__: myInvoke})
|
| 790 |
|
| 791 | var callable = Object(methods, {x: 2, y: 3})
|
| 792 |
|
| 793 | # call it through the obj
|
| 794 | callable a b (44, 45)
|
| 795 |
|
| 796 | ## STDOUT:
|
| 797 | sum = 1
|
| 798 | (Dict) {"x":0,"y":1}
|
| 799 | (List) ["a","b",42,43]
|
| 800 |
|
| 801 | sum = 5
|
| 802 | (Obj) ("x":2,"y":3) --> ("__invoke__":<Proc>)
|
| 803 | (List) ["a","b",44,45]
|
| 804 | ## END
|
| 805 |
|
| 806 | #### two different objects can share the same __invoke__
|
| 807 | shopt --set ysh:upgrade
|
| 808 |
|
| 809 | proc boundProc(; self, more) {
|
| 810 | echo "sum = $[self.x + self.y + more]"
|
| 811 | }
|
| 812 |
|
| 813 | var methods = Object(null, {__invoke__: boundProc})
|
| 814 |
|
| 815 | var i1 = Object(methods, {x: 3, y: 5})
|
| 816 | var i2 = Object(methods, {x: 10, y: 42})
|
| 817 |
|
| 818 | i1 (1)
|
| 819 | i2 (1)
|
| 820 |
|
| 821 | ## STDOUT:
|
| 822 | sum = 9
|
| 823 | sum = 53
|
| 824 | ## END
|
| 825 |
|
| 826 |
|
| 827 | #### Stateful proc with counter
|
| 828 | shopt --set ysh:upgrade
|
| 829 |
|
| 830 | proc invokeCounter(; self, inc) {
|
| 831 | setvar self.i += inc
|
| 832 | echo "counter = $[self.i]"
|
| 833 | }
|
| 834 |
|
| 835 | var methods = Object(null, {__invoke__: invokeCounter})
|
| 836 | var counter = Object(methods, {i: 0})
|
| 837 |
|
| 838 | counter (1)
|
| 839 | counter (2)
|
| 840 | counter (3)
|
| 841 |
|
| 842 | ## STDOUT:
|
| 843 | counter = 1
|
| 844 | counter = 3
|
| 845 | counter = 6
|
| 846 | ## END
|
| 847 |
|
| 848 | #### Procs are closures (capture their environment)
|
| 849 | shopt --set ysh:upgrade
|
| 850 |
|
| 851 | func makeProc() {
|
| 852 | var x = 42
|
| 853 | proc inner {
|
| 854 | echo "x = $x"
|
| 855 | }
|
| 856 | setvar x += 1
|
| 857 | return (inner)
|
| 858 | }
|
| 859 |
|
| 860 | const p = makeProc()
|
| 861 | p
|
| 862 |
|
| 863 | ## STDOUT:
|
| 864 | x = 43
|
| 865 | ## END
|