nikjohn
nikjohn

Reputation: 21910

How do native Javascript functions work in V8?

I am trying to figure out how some Javascript native functions and operators (shorthand functions) are implemented under the hood in Chrome's V8. In particular, I'm trying to understand how the Unary (-) operation works.

I have found the code for unary operators in V8 here

Could someone explain what's happening 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);
}

I have absolutely no background on C++, as I'm a web developer, so I can't seem to understand what's happening. Thanks!

Upvotes: 1

Views: 422

Answers (1)

user835611
user835611

Reputation: 2376

I think - is turned into multiplication by -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);
  }

Do you want to say in more detail what you want to understand about -?

Upvotes: 2

Related Questions