7.2 Watch、对象检查与即时求值
Watch 表达式是一串属性路径:a.b[0].c。求值器把它拆成记号逐个下钻,中途遇到空值就返回 undefined。对象检查器把任意值展开成有限深度的树,深度到顶就不再展开——调试器不能被一个巨大对象卡死。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 反射 | 《架构内核》 | 反射提供类型信息;本节按路径读取运行时值 |
| 运行态检查 | 《视图与子编辑器》 第 7 章 | 卷三做运行态对象面板;本节做 Watch 求值与深度检查 |
| 全卷脉络 | 本卷目录 | 本节承接 7.1 的暂停状态 |
从零实现:Watch、对象检查与即时求值
路径求值先把 [n] 统一改写为 .n,再按点拆分并过滤空段,因此 a[0].b 与 a.0.b 等价。下钻前检查当前值为空,遇到 undefined 或 null 立即返回 undefined。对象检查按值类型标注,只有对象与数组才生成子节点,且深度减一。
对标调试设施
Unreal 的 Blueprint Watch 可显示调试期间记录的引脚值。helio 示例按点分路径和数字索引读属性,并限制对象展开深度。应将输入限定为调试快照。直接读取活对象可能执行属性访问器。 Unreal Blueprint Debugger
可运行产物:6 条用例
下面是 src/watch-eval.ts,层级 L1,完整可编译。运行命令 node --test test/07-2-watch-object-inspect-eval.test.ts:
// file: src/watch-eval.ts
// tier: L1
// verify: node --test test/07-2-watch-object-inspect-eval.test.ts
//
// 卷四(第七章):Watch、对象检查与即时求值。
// Watch 表达式是一串属性路径:a.b[0].c。求值器把它拆成记号逐个下钻,
// 中途遇到空值就返回 undefined。对象检查器把任意值展开成有限深度的树,
// 深度到顶就不再展开——调试器不能被一个巨大对象卡死。
export function evalPath(root: unknown, path: string): unknown {
const tokens = path
.replace(/\[(\d+)\]/g, '.$1')
.split('.')
.filter((t) => t.length > 0);
let current: unknown = root;
for (const token of tokens) {
if (current === null || current === undefined) return undefined;
current = (current as Record<string, unknown>)[token];
}
return current;
}
export interface InspectNode {
key: string;
type: string;
value?: string;
children?: InspectNode[];
}
function typeOf(value: unknown): string {
if (value === null) return 'null';
if (Array.isArray(value)) return 'array';
return typeof value;
}
export function inspect(value: unknown, depth = 1, key = '$'): InspectNode {
const type = typeOf(value);
if (type !== 'object' && type !== 'array') {
return { key, type, value: String(value) };
}
const node: InspectNode = { key, type };
if (depth <= 0) return node;
const entries = Object.entries(value as Record<string, unknown>);
node.children = entries.map(([childKey, childValue]) =>
inspect(childValue, depth - 1, childKey),
);
return node;
}
export function evaluateWatches(root: unknown, paths: string[]): unknown[] {
return paths.map((path) => evalPath(root, path));
}
深度限制是安全阀:运行时对象常有循环引用,无限制展开会直接卡死界面。inspect 返回纯数据树而不是原对象,界面可以安全地异步渲染,也不会意外触发 getter。
下面是 test/07-2-watch-object-inspect-eval.test.ts,层级 L1:
// file: test/07-2-watch-object-inspect-eval.test.ts
// tier: L1
// verify: node --test test/07-2-watch-object-inspect-eval.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { evalPath, inspect, evaluateWatches } from '../src/watch-eval.ts';
const state = { player: { hp: 80, bag: [{ id: 'potion' }] } };
test('路径求值支持嵌套属性', () => {
assert.equal(evalPath(state, 'player.hp'), 80);
});
test('路径求值支持数组下标', () => {
assert.equal(evalPath(state, 'player.bag[0].id'), 'potion');
});
test('缺失路径返回 undefined', () => {
assert.equal(evalPath(state, 'player.bag[9].id'), undefined);
assert.equal(evalPath(state, 'ghost.deep.path'), undefined);
});
test('对象检查标注类型', () => {
assert.equal(inspect(42).type, 'number');
assert.equal(inspect('boat').type, 'string');
assert.equal(inspect(null).type, 'null');
assert.equal(inspect([1]).type, 'array');
assert.equal(inspect({ a: 1 }).type, 'object');
});
test('检查深度到顶不再展开', () => {
const shallow = inspect(state, 1);
const player = shallow.children?.find((c) => c.key === 'player');
assert.ok(player);
assert.equal(player.children, undefined);
const deep = inspect(state, 2);
const bag = deep.children
?.find((c) => c.key === 'player')
?.children?.find((c) => c.key === 'bag');
assert.equal(bag?.type, 'array');
assert.equal(bag?.children, undefined);
});
test('Watch 列表批量求值', () => {
assert.deepEqual(evaluateWatches(state, ['player.hp', 'player.bag[0].id', 'none']), [
80,
'potion',
undefined,
]);
});
本机实测(Node v26.5.0,node --test test/07-2-watch-object-inspect-eval.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉嵌套与数组下标;第三条钉缺失路径;第四条钉类型标注;第五条钉深度限制;第六条钉批量 Watch。
小结
- Watch 表达式是属性路径,数组下标可统一成点语法。
- 下钻遇到空值立即返回 undefined。
- 对象检查必须限制深度,循环引用是常态。
- 检查结果返回纯数据树,界面可安全渲染。
阅读导航:上一节:7.1 断点、步进与调用栈 · 下一节:7.3 运行时数据结构可视化 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。