【问题标题】:How do native Javascript functions work in V8?原生 Javascript 函数如何在 V8 中工作?
【发布时间】:2017-02-12 08:23:15
【问题描述】:

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

我在V8中找到unary operators的代码here

有人能解释一下这里发生了什么吗:

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++ 的背景,因为我是一名 Web 开发人员,所以我似乎无法理解发生了什么。谢谢!

【问题讨论】:

  • 我猜如果没有 v8 架构的谷歌知识,没有人可以解释这里发生了什么。你到底为什么要挖这么深?
  • 这里的问题是,如果不深入了解 v8 实现,就无法回答这个问题。很明显,该代码标识了type 的类型并对其进行了相应的表示。如需更多解释,请联系开发人员,或研究文档v8docs.nodesource.com

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


【解决方案1】:

我认为- 变成了乘以-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);
  }

您想更详细地说一下您想了解的关于- 的内容吗?

【讨论】:

  • 这看起来像我要找的东西。你能把我链接到 LOC 吗?
  • 它在 src/parsing/parser.cc 第 466 行。
猜你喜欢
  • 2017-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多