KScript 文档
ZH

调试

断点

断点会在其所在行运行之前暂停脚本。点击行号旁的边距即可添加断点,再次点击红点即可移除。F9可对光标所在行执行相同操作。

断点若设在没有语句的行上,例如空行、注释或闭合的},则会移动到下一个包含语句的行。脚本永远不会在你选择的那一行停下,因此VS Code会将圆点显示在它实际会停止的位置。

当一条语句与其主体位于同一行时,该行只算一个停止点。此处调试器会在第2行暂停一次,而不会再为其中的echo再次暂停:

<let name="ready" value="true" />
<if $ready>
  <echo>ready to go</echo>
</if>
<echo>done</echo>
$ready = true
if $ready { echo 'ready to go' }
echo 'done'

条件断点

循环内的断点会在每次经过时都暂停。若只想在你关心的那一次暂停,请右键点击红点,选择编辑断点,并为其设置条件。条件可以是任何KScript表达式,每次到达该行时,会结合该行可见的变量进行计算:

<!-- orders.kscript -->
<let name="orders">[
    { id: 101, total: 25 },
    { id: 102, total: 0 },
    { id: 103, total: 40 },
  ]</let>
<for each="order" in="$orders">
  <!-- A breakpoint here with the condition $order.total == 0 pauses on order 102 only. -->
  <echo>Order {$order.id}: {$order.total}</echo>
</for>
// orders.kscript
$orders = [
  { id: 101, total: 25 },
  { id: 102, total: 0 },
  { id: 103, total: 40 },
]
for order in $orders {
  // A breakpoint here with the condition $order.total == 0 pauses on order 102 only.
  echo "Order {$order.id}: {$order.total}"
}

若条件中存在错误,例如该行不可见的变量,脚本会暂停,以便你看清出错之处,而不会错过这次停止。

命中次数

命中次数断点会在该行被执行到指定次数时暂停脚本。在同一对话框中选择命中次数。3(或>= 3)表示在第三次及之后的每次都暂停,== 3仅在第三次暂停,> 3表示从第四次起暂停,% 3表示每第三次暂停。同时带有条件和命中次数的断点只统计其条件成立时的次数。

日志点

日志点会向调试控制台写入一条消息,而不是暂停。它是一种echo,无需添加到脚本中,事后也无需手动删除。右键点击边距,选择添加日志点,然后输入消息。其中每个{expression}都会被填充,与双引号字符串中的做法相同:

<!-- stock.kscript -->
<let name="stock" value="{ apples: 12, pears: 0, plums: 5 }" />
<for each="item, count" in="$stock">
  <!--
    A log point here with the message "checking {$item}: {$count} left" writes
    checking apples: 12 left, checking pears: 0 left and checking plums: 5 left.
  -->
  <if $count == 0>
    <echo>{$item} sold out</echo>
  </if>
</for>
// stock.kscript
$stock = { apples: 12, pears: 0, plums: 5 }
for item, count in $stock {
  // A log point here with the message "checking {$item}: {$count} left" writes
  // checking apples: 12 left, checking pears: 0 left and checking plums: 5 left.
  if $count == 0 => echo "{$item} sold out"
}

Edit this page on GitHub