KScript 文档
ZH

调试

逐步执行脚本

脚本暂停时,编辑器顶部的按钮(及其快捷键)用于让脚本继续执行:

按钮 快捷键 作用
继续 F5 运行到下一个断点或程序结束。
单步跳过 F10 运行当前行并在下一行暂停。该行调用的函数会整体运行。
单步进入 F11 暂停在该行所调用函数的第一行内。若该行没有函数调用,则与单步跳过相同。
单步跳出 Shift+F11 运行函数的剩余部分,并在调用后的下一行暂停。
暂停 F6 在运行中的脚本当前位置暂停,例如停在一个永不结束的循环中。

以下脚本在第 6 行暂停:

<fn name="greeting" params="name: string">
  <let name="message" value="&quot;Hello, {$name}!&quot;" />
  <return value="$message" />
</fn>

<let name="text" value="@greeting('Ada')" />
<echo value="$text" />
fn greeting(name: string) {
  $message = "Hello, {$name}!"
  return $message
}

$text = @greeting('Ada')
echo $text

单步进入会在 greeting() 内的第 2 行暂停,此时局部变量会显示 $name = 'Ada'。单步跳过运行第 2 行并在第 3 行暂停,此时 $message = 'Hello, Ada!' 已经出现。单步跳出运行 return 并在第 7 行暂停,回到顶层,此时全局变量会显示 $text = 'Hello, Ada!'。

进入 ->map() 调用的块函数时,操作方式相同。它会在调用堆栈中占有自己的位置,名称为 fn():

<let name="prices" value="[3, 8]" />
<let name="with_tax">$prices->map(fn(price) {
    // Paused here, Locals shows $price = 3 the first time and $price = 8 the second.
    $taxed = $price * 1.2
    return $taxed
  })</let>
<echo value="$with_tax" />
$prices = [3, 8]
$with_tax = $prices->map(fn(price) {
  // Paused here, Locals shows $price = 3 the first time and $price = 8 the second.
  $taxed = $price * 1.2
  return $taxed
})
echo $with_tax

用 => 编写的单行函数(例如 fn(price) => $price * 1.2)没有可以暂停的独立行,因此逐步执行时会整体运行。