Binary Operation 的自动转换发生在 V8 源代码的哪个位置?
Where in the V8 source does the automatic cast for BinaryOperation occour?
我又在美好的旧时光里跌跌撞撞'12' + 2 = '122'
我想深入了解这里发生了什么,所以我的第一篇 论文 是
Maybe Javascript casts the right operand to the type of the first one and
then operates, like so: '12' + String(2) = '122'
all good...
但是没有,因为 12 + '2' = '122'
也是;因此,引擎的魔力显然更倾向于连接而不是强制转换为数字。
我的第二个论文当时是
Maybe the engine enumerates all operands and looks for an "operator override", similar to C#? And then favor executing that over doing the self-magic thing?
当我意识到 '5' * '8' = 40
时,我的困惑变得更加奇怪,它将两个操作数都转换为 Number 并执行操作。
我可能真正理解的唯一方法是直接从 GitHub
读取 V8 代码
我能找到的最远的地方是 v8/src/parsing/parser-base.h
第 2865 行
// We have a "normal" binary operation.
x = factory()->NewBinaryOperation(op, x, y, pos);
if (op == Token::OR || op == Token::AND) {
impl()->RecordBinaryOperationSourceRange(x, right_range);
}
从这里我迷路了,因为我找不到这个 factory()
来自哪里。
长话短说,V8 引擎源代码中的 JavaScript "type Magic" 来自哪里?
这里是 V8 开发人员。
V8 中有多种加法和其他操作的快速路径。如果你想研究一个规范的(缓慢但完整的)版本,你可以寻找 Object::Add in src/objects.cc.
也就是说,这里的真实来源不是任何给定引擎的实现,而是 JavaScript 规范。 +
运算符应该做什么在这里定义:https://tc39.github.io/ecma262/#sec-addition-operator-plus。
任何引擎的实现要么准确地做到这一点,要么从外部看与它无法区分——否则就是一个错误。 Object::Add
的实现读起来几乎与规范完全一样,这并非巧合 ;-)
我又在美好的旧时光里跌跌撞撞'12' + 2 = '122'
我想深入了解这里发生了什么,所以我的第一篇 论文 是
Maybe Javascript casts the right operand to the type of the first one and then operates, like so:
'12' + String(2) = '122'
all good...
但是没有,因为 12 + '2' = '122'
也是;因此,引擎的魔力显然更倾向于连接而不是强制转换为数字。
我的第二个论文当时是
Maybe the engine enumerates all operands and looks for an "operator override", similar to C#? And then favor executing that over doing the self-magic thing?
当我意识到 '5' * '8' = 40
时,我的困惑变得更加奇怪,它将两个操作数都转换为 Number 并执行操作。
我可能真正理解的唯一方法是直接从 GitHub
我能找到的最远的地方是 v8/src/parsing/parser-base.h
第 2865 行
// We have a "normal" binary operation.
x = factory()->NewBinaryOperation(op, x, y, pos);
if (op == Token::OR || op == Token::AND) {
impl()->RecordBinaryOperationSourceRange(x, right_range);
}
从这里我迷路了,因为我找不到这个 factory()
来自哪里。
长话短说,V8 引擎源代码中的 JavaScript "type Magic" 来自哪里?
这里是 V8 开发人员。
V8 中有多种加法和其他操作的快速路径。如果你想研究一个规范的(缓慢但完整的)版本,你可以寻找 Object::Add in src/objects.cc.
也就是说,这里的真实来源不是任何给定引擎的实现,而是 JavaScript 规范。 +
运算符应该做什么在这里定义:https://tc39.github.io/ecma262/#sec-addition-operator-plus。
任何引擎的实现要么准确地做到这一点,要么从外部看与它无法区分——否则就是一个错误。 Object::Add
的实现读起来几乎与规范完全一样,这并非巧合 ;-)