Itcl:$ this变量的不一致性

时间:2017-05-03 19:18:33

标签: tcl itcl

在从Tcl 8.5.9 / Itcl 3.4到Tcl 8.6.6 / Itcl 4.0.5进行项目迁移时,我遇到了$this变量的不一致性,具体取决于它的访问方式。这是最小化的测试用例:

puts "Tcl  version : $tcl_patchLevel"
puts "Itcl version : [package require Itcl]"

itcl::class Base {
    public {
        method base_process {script} {
            uplevel $m_main main_process [list $script]
        }
        method set_main {main} {
          set m_main $main
        }
    }
    protected {
        variable m_main
    }
}

itcl::class Main {
  inherit Base
    public {
        method main_process {script} {
            uplevel $script
        }
    }
}

itcl::class Worker {
  inherit Base
    public {
        method worker_process_direct {} {
            puts "Direct query: this = $this"
        }
        method worker_process_inderect {} {
            base_process {puts "Indirect query: this = $this"}
        }
        method worker_process_both {} {
            puts "Direct query: this = $this"
            base_process {puts "Indirect query: this = $this"}
        }
    }
}

Main main

Worker worker
worker set_main main

puts "\n==== worker_process_direct   ===="
worker worker_process_direct

puts "\n==== worker_process_indirect ===="
worker worker_process_inderect

puts "\n==== worker_process_both     ===="
worker worker_process_both

worker_process_directworker_process_both函数始终提供正确的结果。但worker_process_inderect仅适用于旧版本的Tcl / Itcl。对于Tcl 8.6.6 / Itcl 4.0.5 $this变量奇怪地更改为Main类而不是Worker的实例。

以上是两个版本的Tcl / Itcl的上述脚本输出。

Tcl  version : 8.5.9
Itcl version : 3.4

==== worker_process_direct   ====
Direct query: this = ::worker

==== worker_process_indirect ====
Indirect query: this = ::worker          <<<<<<<<<<<< CORRECT

==== worker_process_both     ====
Direct query: this = ::worker
Indirect query: this = ::worker
Tcl  version : 8.6.6
Itcl version : 4.0.5

==== worker_process_direct   ====
Direct query: this = ::worker

==== worker_process_indirect ====
Indirect query: this = ::main            <<<<<<<<<< INCORRECT

==== worker_process_both     ====
Direct query: this = ::worker
Indirect query: this = ::worker

我是否遗漏了某些东西,而且我没有注意到Tcl / Itcl发生了重大变化?

1 个答案:

答案 0 :(得分:2)

现在非常好奇!我扩充了您的脚本以定义Main,如下所示:

itcl::class Main {
    inherit Base
    public {
        method main_process {script} {
            uplevel $script
            # Print what is actually going on!
            puts >>[tcl::unsupported::disassemble script $script]<<
        }
    }
}

使用8.5 / 3.4我得到了这个输出:

Tcl  version : 8.5.9
Itcl version : 3.4

==== worker_process_direct   ====
Direct query: this = ::worker

==== worker_process_indirect ====
Indirect query: this = ::worker
>>ByteCode 0x0x7fedea044c10, refCt 1, epoch 3, interp 0x0x7fedea033010 (epoch 3)
  Source "puts \"Indirect query: this = $this\""
  Cmds 1, src 35, inst 12, litObjs 3, aux 0, stkDepth 3, code/src 0.00
  Commands 1:
      1: pc 0-10, src 0-34
  Command 1: "puts \"Indirect query: this = $this\""
    (0) push1 0     # "puts"
    (2) push1 1     # "Indirect query: this = "
    (4) push1 2     # "this"
    (6) loadScalarStk 
    (7) concat1 2 
    (9) invokeStk1 2 
    (11) done 
<<

==== worker_process_both     ====
Direct query: this = ::worker
Indirect query: this = ::worker
>>ByteCode 0x0x7fedea044c10, refCt 1, epoch 3, interp 0x0x7fedea033010 (epoch 3)
  Source "puts \"Indirect query: this = $this\""
  Cmds 1, src 35, inst 12, litObjs 3, aux 0, stkDepth 3, code/src 0.00
  Commands 1:
      1: pc 0-10, src 0-34
  Command 1: "puts \"Indirect query: this = $this\""
    (0) push1 0     # "puts"
    (2) push1 1     # "Indirect query: this = "
    (4) push1 2     # "this"
    (6) loadScalarStk 
    (7) concat1 2 
    (9) invokeStk1 2 
    (11) done 
<<

使用8.6 / 4.0我得到了这个:

Tcl  version : 8.6.3
Itcl version : 4.0.2

==== worker_process_direct   ====
Direct query: this = ::worker

==== worker_process_indirect ====
Indirect query: this = ::main
>>ByteCode 0x0x1009af010, refCt 1, epoch 136, interp 0x0x100829a10 (epoch 136)
  Source "puts \"Indirect query: this = $this"...
  Cmds 1, src 35, inst 12, litObjs 3, aux 0, stkDepth 3, code/src 0.00
  Commands 1:
      1: pc 0-10, src 0-34
  Command 1: "puts \"Indirect query: this = $this"...
    (0) push1 0     # "puts"
    (2) push1 1     # "Indirect query: this = "
    (4) push1 2     # "this"
    (6) loadStk 
    (7) strcat 2 
    (9) invokeStk1 2 
    (11) done 
<<

==== worker_process_both     ====
Direct query: this = ::worker
Indirect query: this = ::worker
>>ByteCode 0x0x1009b0210, refCt 1, epoch 136, interp 0x0x100829a10 (epoch 136)
  Source "puts \"Indirect query: this = $this"...
  Cmds 1, src 35, inst 11, litObjs 2, aux 0, stkDepth 3, code/src 0.00
  Commands 1:
      1: pc 0-9, src 0-34
  Command 1: "puts \"Indirect query: this = $this"...
    (0) push1 0     # "puts"
    (2) push1 1     # "Indirect query: this = "
    (4) loadScalar1 %v0 
    (6) strcat 2 
    (8) invokeStk1 2 
    (10) done 
<<

因此,8.5使用loadScalarStk指令在两个(间接)情况下读取变量,而8.6使用loadStkloadScalar1在两种情况下加载变量。哪个是奇怪的;我不希望loadScalar1出现在脚本片段中(它需要一个局部变量表),但至少它正在提取预期值,而loadStk只是完全取错了值。我也尝试在两个地方使用完全相同的值 - 脚本保存在共享变量中 - 但是产生相同的输出;它看起来像是在一个地方进行评估但是拾取了错误的值(可能是一个可变的解析器问题?)而在另一个地方它正在拾取正确的值,但出于错误的原因(因为LVT不应该用于脚本片段;仅适用于完整的程序/方法)。无论哪种方式,这都是坏消息。

请在http://core.tcl-lang.org/tcl/tktnew提交错误报告,因为这会混淆几种错误的行为。