Posted to tcl by colin at Mon May 17 23:37:26 GMT 2010view raw
- proc ::Coroutine {name command args} {
- # determine the appropriate namespace for coro creation
- set ns [namespace qualifiers $name]
- if {![string match ::* $ns]} {
- set ns [uplevel 1 namespace current]::$ns
- }
- set name [namespace tail $name]
- # create a like-named coro
- set x [uplevel 1 [list ::coroutine ${ns}_$name $command {*}$args]]
- # wrap the coro in a shim
- proc ${ns}$name {args} [string map [list $x %N%] {
- tailcall %N% $args ;# wrap the args into a list for the old-style coro
- }]
- # the two commands need to be paired for destruction
- trace add command $x delete ::delshim ${ns}$name
- trace add command ${ns}$name ::delshim $x
- # tell it we created the one they requested
- return ${ns}$name
- }