原生 Javascript 函数在 V8 中如何工作?

How do native Javascript functions work in V8?

我试图弄清楚一些 Javascript 原生函数和运算符(shorthand 函数)是如何在 Chrome 的 V8 中实现的。特别是,我试图了解一元 (-) 运算的工作原理。

我在 V8 here

中找到了 unary operators 的代码

谁能解释一下这里发生了什么:

Type* Typer::Visitor::JSTypeOfTyper(Type* type, Typer* t) {
  Factory* const f = t->isolate()->factory();
  if (type->Is(Type::Boolean())) {
    return Type::Constant(f->boolean_string(), t->zone());
  } else if (type->Is(Type::Number())) {
    return Type::Constant(f->number_string(), t->zone());
  } else if (type->Is(Type::String())) {
    return Type::Constant(f->string_string(), t->zone());
  } else if (type->Is(Type::Symbol())) {
    return Type::Constant(f->symbol_string(), t->zone());
  } else if (type->Is(Type::Union(Type::Undefined(), Type::OtherUndetectable(),
                                  t->zone()))) {
    return Type::Constant(f->undefined_string(), t->zone());
  } else if (type->Is(Type::Null())) {
    return Type::Constant(f->object_string(), t->zone());
  } else if (type->Is(Type::Function())) {
    return Type::Constant(f->function_string(), t->zone());
  } else if (type->IsConstant()) {
    return Type::Constant(
        Object::TypeOf(t->isolate(), type->AsConstant()->Value()), t->zone());
  }
  return Type::InternalizedString();
}


Type* Typer::Visitor::TypeJSTypeOf(Node* node) {
  return TypeUnaryOp(node, JSTypeOfTyper);
}

我完全没有 C++ 背景,因为我是一名网络开发人员,所以我似乎无法理解发生了什么。谢谢!

我认为--1变成了乘法。

src/parsing/parser.cc

  // The same idea for '-foo' => 'foo*(-1)'.
  if (op == Token::SUB) {
    return factory()->NewBinaryOperation(
         Token::MUL, expression, factory()->NewNumberLiteral(-1, pos), pos);
  }

关于-,您想更详细地谈谈您想了解的内容吗?