KScript 文档
ZH

调试

变量视图

脚本暂停时,变量视图会显示此刻每个变量的值。变量按作用域分组:

  • 局部变量:脚本所暂停的函数中的变量,包括其参数及其赋值的变量。在循环中,循环自身的变量(例如 $price 和 $index)也会显示在这里,即使是在脚本顶层。不在任何函数或循环中时,则没有局部变量。
  • 全局变量:脚本在顶层赋值的变量。函数和类型不会列出,因为它们不会改变。
  • 内置:脚本的包为每个脚本提供的内容,例如 $args。若其中没有内容,则不显示。

文本以引号显示,因此 '7' 是文本,7 是数字。将鼠标悬停在某个值上时会显示其类型。列表、映射或记录旁边有箭头,点击即可查看其内部内容。记录会显示其类型名称:

<type name="Point">
  <field name="x" type="int" />
  <field name="y" type="int" />
</type>

<let name="corner" value="Point(x: 3, y: 4)" />
<let name="tags" value="['red', 'blue']" />
<let name="sizes" value="{ small: 1, large: 9 }" />
<!--
  Paused here, Globals shows
    $corner = Point { x: 3, y: 4 }
    $tags = ['red', 'blue']
    $sizes = { small: 1, large: 9 }
  and opening $tags shows [0] = 'red' and [1] = 'blue'.
-->
<echo>done</echo>
type Point {
  x: int
  y: int
}

$corner = Point(x: 3, y: 4)
$tags = ['red', 'blue']
$sizes = { small: 1, large: 9 }
// Paused here, Globals shows
//   $corner = Point { x: 3, y: 4 }
//   $tags = ['red', 'blue']
//   $sizes = { small: 1, large: 9 }
// and opening $tags shows [0] = 'red' and [1] = 'blue'.
echo 'done'

在调用堆栈中点击其他行,即可查看上层函数的变量:变量视图、调试控制台和悬停提示都会跟随你所选的行。

悬停提示、监视和调试控制台

脚本暂停时,将鼠标悬停在编辑器中的变量上即可查看其值。监视视图会在你逐步执行时持续显示某个表达式的值,例如 $basket_total 或 $prices->count()。调试控制台会使用当前暂停行的变量计算你输入的任何表达式:

文本
> $prices->sum()
7.75
> $quantities[2] * 2
8

表达式可以调用函数和方法,因此调试控制台可以在你将修复写入脚本之前先进行试验。对于会改变内容的函数(例如写入文件)要小心:它们会真正运行。