[英]Why does this && not short-circuit?
This code is from the node module templatizer .此代码来自节点模块templatizer 。
if (i === 3 && node.type === "ExpressionStatement" && node.expression.callee.object.name === "buf" &&
node.expression.arguments.length === 1 && node.expression.arguments[0].type === "Literal") {
// save the simple string
simpleString = node.expression.arguments[0].value;
cnt++;
}
The value of node.type
is 'VariableDeclaration' so the logical expression is false and so node.expression
shouldn't even be evaluated but it appears to be so... node.type
的值是 'VariableDeclaration' 所以逻辑表达式是假的,所以node.expression
甚至不应该被评估,但它似乎是这样......
TypeError: Cannot read property 'object' of undefined at /node_modules/templatizer/lib/simplifyTemplate.js:34:89 at Array.forEach (native)
类型错误:无法在 Array.forEach(本机)处读取 /node_modules/templatizer/lib/simplifyTemplate.js:34:89 处未定义的属性“对象”
It means the 'object' in 'callee'.它的意思是“被调用者”中的“对象”。
expression
and callee
are both undefined. expression
和callee
都是未定义的。 As soon as I step over the conditional node crashes.一旦我跨过条件节点就会崩溃。
EDIT编辑
I think Javascript works fine and perhaps some async code is resulting in strange results form the debugger.我认为 Javascript 工作正常,也许一些异步代码会导致调试器产生奇怪的结果。 If I put a console.log at the top of the loop it sometimes gives output that makes sense...
如果我将 console.log 放在循环的顶部,它有时会提供有意义的输出......
/usr/local/bin/node bin/www
count: 1
i:0
node.type: VariableDeclaration
expression: undefined
node.expression.callee: undefined
count: 2
i:1
node.type: VariableDeclaration
expression: undefined
node.expression.callee: undefined
count: 3
i:2
node.type: VariableDeclaration
expression: undefined
node.expression.callee: undefined
count: 4
i:3
node.type: ExpressionStatement
expression: [object Object]
node.expression.callee: [object Object]
count: 5
i:4
node.type: ReturnStatement
expression: undefined
node.expression.callee: undefined
count: 6
i:0
node.type: VariableDeclaration
expression: undefined
node.expression.callee: undefined
count: 7
i:1
node.type: VariableDeclaration
expression: undefined
node.expression.callee: undefined
count: 8
i:2
node.type: VariableDeclaration
expression: undefined
node.expression.callee: undefined
count: 9
i:3
node.type: ExpressionStatement
expression: [object Object]
node.expression.callee: undefined
/Users/ME/WebstormProjects/MySite/node_modules/templatizer/lib/simplifyTemplate.js:41
= 3 && node.type === "ExpressionStatement" && node.expression.callee.object.na
^
TypeError: Cannot read property 'object' of undefined
at /Users/ME/WebstormProjects/MySite/node_modules/templatizer/lib/simplifyTemplate.js:41:89
at Array.forEach (native)
at module.exports (/Users/ME/WebstormProjects/MySite/node_modules/templatizer/lib/simplifyTemplate.js:15:18)
at /Users/ME/WebstormProjects/MySite/node_modules/templatizer/templatizer.js:111:20
at Array.forEach (native)
at module.exports (/Users/ME/WebstormProjects/MySite/node_modules/templatizer/templatizer.js:95:15)
at Object.<anonymous> (/Users/ME/WebstormProjects/MySite/app.js:23:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
Process finished with exit code 8
So looks like a bug in the module.所以看起来像是模块中的一个错误。
Seems that the debugger was showing values from a different part of the execution than the one that caused the crash.似乎调试器显示的值来自执行的不同部分,而不是导致崩溃的部分。 Surely it must be running things asynchronously somewhere.
当然,它必须在某处异步运行。 So a bug in the module.
所以是模块中的一个错误。 Temporary fix was to add an extra check to the conditional.
临时修复是为条件添加额外的检查。
if (i === 3 && node.type === "ExpressionStatement" && node.expression.callee && node.expression.callee.object.name === "buf" &&
node.expression.arguments.length === 1 && node.expression.arguments[0].type === "Literal")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.