| 1 | package YATT::Class::Tcl; | ||
| 2 | use strict; | ||
| 3 | use warnings qw(FATAL all NONFATAL misc); | ||
| 4 | use Tcl; | ||
| 5 | |||
| 6 | use base qw(YATT::Class::Configurable); | ||
| 7 | use YATT::Fields (qw(tcl) | ||
| 8 | , ['^cf_myname' => '::yatt::perl'] | ||
| 9 | , ['cf_tclinit' => 1]); | ||
| 10 | |||
| 11 | use YATT::Util::Symbol; | ||
| 12 | |||
| 13 | foreach my $meth (qw(Eval EvalFile Init call icall invoke result | ||
| 14 | CreateCommand DeleteCommand | ||
| 15 | SetResult AppendResult AppendElement ResetResult | ||
| 16 | SplitList | ||
| 17 | SetVar SetVar2 GetVar GetVar2 | ||
| 18 | UnsetVar UnsetVar2 | ||
| 19 | return_ref delete_ref )) { | ||
| 20 | *{globref(MY, $meth)} = sub { | ||
| 21 | my MY $self = shift; | ||
| 22 | $self->{tcl}->$meth(@_); | ||
| 23 | }; | ||
| 24 | } | ||
| 25 | |||
| 26 | # | ||
| 27 | # Expose tcl commands (in ::*) as methods. | ||
| 28 | # | ||
| 29 | sub AUTOLOAD { | ||
| 30 | my $method = our $AUTOLOAD; | ||
| 31 | $method =~ s/.*:://; | ||
| 32 | (my MY $self) = @_; | ||
| 33 | our ($sub, %NEGATIVE_CACHE); | ||
| 34 | if (exists $NEGATIVE_CACHE{$method} or not do { | ||
| 35 | if (my @found = $self->{tcl}->invoke(info => commands => $method)) { | ||
| 36 | $sub = sub { | ||
| 37 | my MY $self = shift; | ||
| 38 | $self->{tcl}->invoke($method, @_); | ||
| 39 | }; | ||
| 40 | } else { | ||
| 41 | undef $NEGATIVE_CACHE{$method} | ||
| 42 | } | ||
| 43 | }) { | ||
| 44 | die "No such method: $method"; | ||
| 45 | } | ||
| 46 | *{globref(class($self), $method)} = $sub; | ||
| 47 | goto &$sub; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub new { | ||
| 51 | my MY $self = shift->SUPER::new(@_); | ||
| 52 | $self->{tcl}->Init if $self->{cf_tclinit}; | ||
| 53 | if (my $myname = $self->myname) { | ||
| 54 | # ·è¤áÂǤÁ¤Ï¤É¤¦¤«¤È¤â»×¤¦¤¬¡¢¤½¤â¤½¤â¤³¤Î tcl interp ¤Ï | ||
| 55 | # ¤³¤Î YATT::Class::Tcl ¥¤¥ó¥¹¥¿¥ó¥¹¤Ë¸ÇͤÀ¤«¤é¡¢¾×ÆÍ¤Î¤·¤è¤¦¤¬¤Ê¤¤¡£ | ||
| 56 | $self->{tcl}->Eval(<<'END'); | ||
| 57 | namespace eval ::yatt { | ||
| 58 | proc nslist nsname { | ||
| 59 | set q [namespace qualifier $nsname] | ||
| 60 | if {$q eq ""} { | ||
| 61 | return [list $nsname] | ||
| 62 | } else { | ||
| 63 | linsert [nslist $q] end [namespace tail $nsname] | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | proc ensure-nslist nslist { | ||
| 68 | if {![llength $nslist]} { | ||
| 69 | list | ||
| 70 | } else { | ||
| 71 | list namespace eval [lindex $nslist 0] \ | ||
| 72 | [ensure-nslist [lrange $nslist 1 end]] | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | proc ensure-ns nsname { | ||
| 77 | uplevel #0 [ensure-nslist [nslist [namespace qualifier $nsname]]] | ||
| 78 | } | ||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | END | ||
| 83 | |||
| 84 | $self->{tcl}->invoke('yatt::ensure-ns', $myname); | ||
| 85 | $self->{tcl}->CreateCommand($myname, \&perl_dispatch, $self); | ||
| 86 | } | ||
| 87 | $self | ||
| 88 | } | ||
| 89 | |||
| 90 | sub before_configure { | ||
| 91 | my MY $self = shift; | ||
| 92 | $self->{tcl} = new Tcl; | ||
| 93 | $self | ||
| 94 | } | ||
| 95 | |||
| 96 | sub perl_dispatch { | ||
| 97 | (my MY $self, my ($tcl, undef, $method)) = splice @_, 0, 4; | ||
| 98 | $tcl->ResetResult; | ||
| 99 | my @result = $self->$method(@_); # To make debugger happy. | ||
| 100 | $tcl->AppendResult(map {defined $_ ? $_ : ""} @result); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub MainLoop { | ||
| 104 | (my MY $self, my $varname) = @_; | ||
| 105 | $self->{tcl}->invoke(vwait => $varname || 'forever'); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub lexpand { | ||
| 109 | (my MY $tcl, my ($list, $n)) = @_; | ||
| 110 | $tcl->{tcl}->invoke(lrange => $list, defined $n ? $n : 0, 'end'); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub lexpand_if { | ||
| 114 | (my MY $tcl, my ($typename, $list)) = @_; | ||
| 115 | return if $tcl->{tcl}->lindex($list, 0) ne $typename; | ||
| 116 | # ÀèÆ¬¤¬°ìÃפ¹¤ë¤Ê¤é¡¢»Ä¤ê¤òÊÖ¤¹¡£ | ||
| 117 | $tcl->lexpand($list, 1); | ||
| 118 | } | ||
| 119 | |||
| 120 | 1; |