本机Javascript函数如何在V8中运行?

时间:2016-10-04 08:37:00

标签: javascript c++ google-chrome v8 chromium

我试图找出一些Javascript原生函数和运算符(简写函数)是如何在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 个答案:

答案 0 :(得分:2)

我认为-会变成乘以-1

的src /解析/ parser.cc

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

您想更详细地说明您对-的理解吗?