Index: library/serialize/serializer.tcl =================================================================== diff -u -r46688d146087a76aa06b15391708736fa68fc05a -r6e4c08c5cf598e08cbc29516f84b09e5983e347c --- library/serialize/serializer.tcl (.../serializer.tcl) (revision 46688d146087a76aa06b15391708736fa68fc05a) +++ library/serialize/serializer.tcl (.../serializer.tcl) (revision 6e4c08c5cf598e08cbc29516f84b09e5983e347c) @@ -1,4 +1,7 @@ package require nx +# TODO: should go away +#package require nx::plain-object-method + package require XOTcl 2.0 package provide nx::serializer 1.0 @@ -142,7 +145,7 @@ ########################################################################### Class create Serializer { - :property ignoreVarsRE + :property -accessor public ignoreVarsRE :public method ignore args { # Ignore the objects passed via args. @@ -153,6 +156,9 @@ } } } + :public method objmap {map} { + array set :objmap $map + } :method init {} { # Never serialize the (volatile) serializer object @@ -191,6 +197,17 @@ set o [::nsf::dispatch $o ::nsf::methods::object::info::parent] } } + + :public method getTargetName {sourceName} { + # TODO: make more efficent; + set targetName $sourceName + if {[array exists :objmap]} { + foreach {source target} [array get :objmap] { + regsub ^$source $targetName $target targetName + } + } + return $targetName + } :method topoSort {set all} { if {[array exists :s]} {array unset :s} @@ -242,7 +259,7 @@ set result "" foreach l [lsort -integer [array names :level]] { foreach i [set :level($l)] { - #.warn "serialize $i" + #:warn "serialize $i" #append result "# Stratum $l\n" set oss [set :serializer($i)] append result [$oss serialize $i [::nsf::current object]] \n @@ -295,7 +312,7 @@ # class object specfic methods ############################### - :public class method allChildren o { + :public object method allChildren o { # return o and all its children fully qualified set set [::nsf::directdispatch $o -frame method ::nsf::current] foreach c [$o info children] { @@ -304,23 +321,23 @@ return $set } - :public class method exportMethods list { + :public object method exportMethods list { foreach {o p m} $list {set :exportMethods([list $o $p $m]) 1} } - :public class method exportObjects list { + :public object method exportObjects list { foreach o $list {set :exportObjects($o) 1} } - :public class method exportedMethods {} {array names :exportMethods} - :public class method exportedObjects {} {array names :exportObjects} + :public object method exportedMethods {} {array names :exportMethods} + :public object method exportedObjects {} {array names :exportObjects} - :public class method resetPattern {} {array unset :ignorePattern} - :public class method addPattern {p} {set :ignorePattern($p) 1} + :public object method resetPattern {} {array unset :ignorePattern} + :public object method addPattern {p} {set :ignorePattern($p) 1} - :class method checkExportedMethods {} { + :object method checkExportedMethods {} { foreach k [array names :exportMethods] { - foreach {o p m} $k break + lassign $k o p m set ok 0 foreach p [array names :ignorePattern] { if {[string match $p $o]} { @@ -334,7 +351,7 @@ } } - :class method checkExportedObject {} { + :object method checkExportedObject {} { foreach o [array names :exportObjects] { if {![::nsf::object::exists $o]} { :warn "Serializer exportObject: ignore non-existing object $o" @@ -348,7 +365,7 @@ } } - :public class method all {-ignoreVarsRE -ignore} { + :public object method all {-ignoreVarsRE -ignore} { # # Remove objects which should not be included in the # blueprint. TODO: this is not the best place to do this, since @@ -367,7 +384,7 @@ #::nx::Slot mixin add ::nx::Slot::Nocheck ::nsf::exithandler set [list [::nsf::exithandler get]] }] - foreach option {debug softrecreate keepinitcmd checkresults checkarguments} { + foreach option {debug softrecreate keepcmds checkresults checkarguments} { append r \t [list ::nsf::configure $option [::nsf::configure $option]] \n } :resetPattern @@ -409,29 +426,29 @@ return $r } - :class method add_child_namespaces {ns} { + :object method add_child_namespaces {ns} { if {$ns eq "::nsf"} return lappend :namespaces $ns foreach n [namespace children $ns] { :add_child_namespaces $n } } - :public class method application_namespaces {ns} { + :public object method application_namespaces {ns} { set :namespaces "" :add_child_namespaces $ns return ${:namespaces} } - :public class method export_nsfprocs {ns} { + :public object method export_nsfprocs {ns} { set result "" foreach n [:application_namespaces $ns] { - foreach p [:info methods -methodtype nsfproc ${n}::*] { + foreach p [:info methods -type nsfproc ${n}::*] { append result [:info method definition $p] \n } } return $result } - :public class method methodSerialize {object method prefix} { + :public object method methodSerialize {object method prefix} { set s [:new -childof [::nsf::current object] -volatile] foreach oss [ObjectSystemSerializer info instances] { if {[$oss responsibleSerializer $object]} { @@ -443,15 +460,15 @@ return $result } - :public class method deepSerialize {-ignoreVarsRE -ignore -map args} { + :public object method deepSerialize {-ignoreVarsRE -ignore -map -objmap args} { :resetPattern set s [:new -childof [::nsf::current object] -volatile] #$s volatile if {[info exists ignoreVarsRE]} {$s ignoreVarsRE $ignoreVarsRE} if {[info exists ignore]} {$s ignore $ignore} - + if {[info exists objmap]} {$s objmap $objmap} foreach o $args { - append r [$s deepSerialize [$o]] + append r [$s deepSerialize [::nsf::directdispatch $o -frame method ::nsf::current]] } if {[info exists map]} {return [string map $map $r]} return $r @@ -536,7 +553,7 @@ # which this object specific serializer is responsible # foreach k [Serializer exportedMethods] { - foreach {o p m} $k break + lassign $k o p m if {![::nsf::object::exists $o]} { :warn "$o is not an object" } elseif {[::nsf::dispatch $o ::nsf::methods::object::info::hastype ${:rootClass}]} { @@ -587,17 +604,18 @@ set v [$cmd $o $relation] if {$v eq ""} {return ""} if {[info exists unless] && $v eq $unless} {return ""} - return [list $cmd $o $relation $v]\n + return [list $cmd ${:targetName} $relation $v]\n } :method serializeExportedMethods {s} { set r "" foreach k [array names :exportMethods] { - foreach {o p m} $k break + lassign $f o p m if {![:methodExists $o $p $m]} { :warn "Method does not exist: $o $p $m" continue } + set :targetName [$s getTargetName $o] append methods($o) [:serializeExportedMethod $o $p $m]\n } foreach o [array names methods] {set ($o) 1} @@ -617,6 +635,7 @@ ############################### :public method serialize {objectOrClass s} { + set :targetName [$s getTargetName $objectOrClass] :[:classify $objectOrClass]-serialize $objectOrClass $s } @@ -635,7 +654,7 @@ if {$t ne ""} { foreach ops $t { - foreach {op cmd} $ops break + lassign $ops op cmd # save traces in post_cmds $s addPostCmd [list $o trace add variable $v $op $cmd] @@ -660,7 +679,7 @@ return [set $handle] } set needed [list] - foreach alias [$x ::nsf::methods::${where}::info::methods -methodtype alias -callprotection all -path] { + foreach alias [$x ::nsf::methods::${where}::info::methods -type alias -callprotection all -path] { set definition [$x ::nsf::methods::${where}::info::method definition $alias] set aliasedCmd [lindex $definition end] # @@ -718,7 +737,7 @@ set :rootMetaClass ::nx::Class array set :ignorePattern [list "::nsf::*" 1 "::nx::*" 1 "::xotcl::*" 1] - :public method serialize-all-start {s} { + :public object method serialize-all-start {s} { set intro [subst { package require nx ::nx::configure defaultMethodCallProtection [::nx::configure defaultMethodCallProtection] @@ -734,26 +753,28 @@ # nx method serialization ############################### - :method methodExists {object kind name} { + :object method methodExists {object kind name} { expr {[$object info method type $name] ne ""} } - :public method serializeExportedMethod {object kind name} { + :public object method serializeExportedMethod {object kind name} { # todo: object modifier is missing return [:method-serialize $object $name ""] } - :method method-serialize {o m modifier} { - if {![::nsf::is class $o]} {set modifier ""} - if {[$o {*}$modifier info method type $m] eq "object"} { + :object method method-serialize {o m modifier} { + if {![::nsf::is class $o]} {set modifier "object"} + if {[$o info {*}$modifier method type $m] eq "object"} { # object serialization is fully handled by the serializer - return "# [$o {*}$modifier info method definition $m]" + return "# [$o info {*}$modifier method definition $m]" } - if {[$o {*}$modifier info method type $m] eq "setter"} { + if {[$o info {*}$modifier method type $m] eq "setter"} { set def "" } else { - set def [$o {*}$modifier info method definition $m] - set handle [$o {*}$modifier info method registrationhandle $m] + set def [$o info {*}$modifier method definition $m] + if {${:targetName} ne $o} { + set def [lreplace $def 0 0 ${:targetName}] + } } return $def } @@ -762,7 +783,7 @@ # nx object serialization ############################### - :method Object-serialize {o s} { + :object method Object-serialize {o s} { if {[$o ::nsf::methods::object::info::hastype ::nx::EnsembleObject]} { return "" } @@ -772,16 +793,17 @@ set isSlotContainer [::nx::isSlotContainer $objectName] if {$isSlotContainer} { append cmd [list ::nx::slotObj -container [namespace tail $objectName] \ - [$o ::nsf::methods::object::info::parent]]\n + [$s getTargetName [$objectName ::nsf::methods::object::info::parent]]]\n } else { - append cmd [list [$o info class] create $objectName -noinit]\n + #puts stderr "CREATE targetName '${:targetName}'" + append cmd [list [$o info class] create ${:targetName} -noinit]\n foreach i [lsort [$o ::nsf::methods::object::info::methods -callprotection all -path]] { - append cmd [:method-serialize $o $i "class"] "\n" + append cmd [:method-serialize $o $i "object"] "\n" } } set vars [:collectVars $o $s] - if {[llength $vars]>0} {append cmd [list $o eval [join $vars "\n "]]\n} + if {[llength $vars]>0} {append cmd [list ${:targetName} eval [join $vars "\n "]]\n} append cmd \ [:frameWorkCmd ::nsf::relation $o object-mixin] \ @@ -792,7 +814,7 @@ if {[$o info has type ::nx::Slot]} { # Slots needs to be initialized to ensure # __invalidateobjectparameter to be called - append cmd [list $o eval :init] \n + append cmd [list ${:targetName} eval :init] \n } $s addPostCmd [:frameWorkCmd ::nsf::relation $o object-filter] @@ -803,9 +825,10 @@ # nx class serialization ############################### - :method Class-serialize {o s} { + :object method Class-serialize {o s} { set cmd [:Object-serialize $o $s] + foreach i [lsort [$o ::nsf::methods::class::info::methods -callprotection all -path]] { append cmd [:method-serialize $o $i ""] "\n" } @@ -838,15 +861,15 @@ #array set :ignorePattern [list "::xotcl::*" 1] array set :ignorePattern [list "::nsf::*" 1 "::nx::*" 1 "::xotcl::*" 1] - :public method serialize-all-start {s} { + :public object method serialize-all-start {s} { set intro "package require XOTcl 2.0" if {[info command ::Object] ne "" && [namespace origin ::Object] eq "::xotcl::Object"} { append intro "\nnamespace import -force ::xotcl::*" } return "$intro\n::xotcl::Object instproc trace args {}\n[next]" } - :public method serialize-all-end {s} { + :public object method serialize-all-end {s} { return "[next]\n::nsf::method::alias ::xotcl::Object trace -frame object ::trace\n" } @@ -855,7 +878,7 @@ # XOTcl method serialization ############################### - :method methodExists {object kind name} { + :object method methodExists {object kind name} { switch $kind { proc - instproc { return [expr {[$object info ${kind}s $name] ne ""}] @@ -866,7 +889,7 @@ } } - :public method serializeExportedMethod {object kind name} { + :public object method serializeExportedMethod {object kind name} { set code "" switch $kind { "" - inst { @@ -888,14 +911,14 @@ return $code } - :method method-serialize {o m prefix} { + :object method method-serialize {o m prefix} { set arglist [list] foreach v [$o info ${prefix}args $m] { if {[$o info ${prefix}default $m $v x]} { #puts "... [list $o info ${prefix}default $m $v x] returned 1, x?[info exists x] level=[info level]" lappend arglist [list $v $x] } {lappend arglist $v} } - lappend r $o ${prefix}proc $m \ + lappend r ${:targetName} ${prefix}proc $m \ [concat [$o info ${prefix}nonposargs $m] $arglist] \ [$o info ${prefix}body $m] foreach p {pre post} { @@ -908,21 +931,21 @@ # XOTcl object serialization ############################### - :method Object-serialize {o s} { + :object method Object-serialize {o s} { :collect-var-traces $o $s - append cmd [list [$o info class] create [::nsf::directdispatch $o -frame method ::nsf::current object]] + append cmd [list [$o info class] create ${:targetName}] append cmd " -noinit\n" - foreach i [$o ::nsf::methods::object::info::methods -methodtype scripted -callprotection all] { + foreach i [$o ::nsf::methods::object::info::methods -type scripted -callprotection all] { append cmd [:method-serialize $o $i ""] "\n" } - foreach i [$o ::nsf::methods::object::info::methods -methodtype forward -callprotection all] { - append cmd [concat [list $o] forward $i [$o info forward -definition $i]] "\n" + foreach i [$o ::nsf::methods::object::info::methods -type forward -callprotection all] { + append cmd [concat [list ${:targetName}] forward $i [$o info forward -definition $i]] "\n" } - foreach i [$o ::nsf::methods::object::info::methods -methodtype setter -callprotection all] { - append cmd [list $o parametercmd $i] "\n" + foreach i [$o ::nsf::methods::object::info::methods -type setter -callprotection all] { + append cmd [list ${:targetName} parametercmd $i] "\n" } append cmd \ - [list $o eval [join [:collectVars $o $s] "\n "]] \n \ + [list ${:targetName} eval [join [:collectVars $o $s] "\n "]] \n \ [:frameWorkCmd ::nsf::relation $o object-mixin] \ [:frameWorkCmd ::nsf::method::assertion $o object-invar] @@ -934,21 +957,21 @@ # XOTcl class serialization ############################### - :method Class-serialize {o s} { + :object method Class-serialize {o s} { set cmd [:Object-serialize $o $s] foreach i [$o info instprocs] { append cmd [:method-serialize $o $i inst] "\n" } foreach i [$o info instforward] { - append cmd [concat [list $o] instforward $i [$o info instforward -definition $i]] "\n" + append cmd [concat [list ${:targetName}] instforward $i [$o info instforward -definition $i]] "\n" } foreach i [$o info instparametercmd] { - append cmd [list $o instparametercmd $i] "\n" + append cmd [list ${:targetName} instparametercmd $i] "\n" } # provide limited support for exporting aliases for XOTcl objects - foreach i [$o ::nsf::methods::class::info::methods -methodtype alias -callprotection all] { + foreach i [$o ::nsf::methods::class::info::methods -type alias -callprotection all] { set nxDef [$o ::nsf::methods::class::info::method definition $i] - append cmd [list ::nsf::method::alias $o {*}[lrange $nxDef 3 end]]\n + append cmd [list ::nsf::method::alias ${:targetName} {*}[lrange $nxDef 3 end]]\n } append cmd \ [:frameWorkCmd ::nsf::relation $o superclass -unless ${:rootClass}] \