1

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!

2
  • I guess what without goog knowledge of v8 architecture no one could explain what's happening here. Why exactly do you want to dig so deep? Commented Oct 4, 2016 at 10:27
  • The problem here that this question cannot be answered without deep knowledge of the v8 implementation. It is clear that the code identifies the type of type and takes a corresponding representation of it. For more explanation maybe contact a developer, or study the documentation v8docs.nodesource.com Commented Oct 4, 2016 at 11:42

1 Answer 1

2

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 -?

Sign up to request clarification or add additional context in comments.

2 Comments

That looks like what I was looking for. Could you link me to the LOC?
It's in src/parsing/parser.cc line 466.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.