4

I am looking to see how some Javascript functions work under the hood. For e.g. I want to learn how Chrome's V8 Engine implements the Unary (-) operation or the String.prototype.toString() method.

How can I see the native C/C++ implementation? I have seen many answers here linking to the Chromium repository and the V8 repository, but these are giant and not very beginner friendly, and there aren't really any guides anywhere as far as I could find.

I'm looking for something like this:

// Pseudo code
function -(arg) {
  return arg * -1
}

Obviously, I understand that I wouldn't find this written in Javascript. I'm just looking for a similar level of detail.

I'm yet to find an answer that concisely shows how to find the native implementation of Javascript functions anywhere. Could someone point me in the right direction?

The ECMA specs here give the following specs for the Unary - operation:

Unary - Operator

The unary - operator converts its operand to Number type and then negates it. Note that negating +0 produces −0, and negating −0 produces +0.

The production UnaryExpression : - UnaryExpression is evaluated as follows:

Let expr be the result of evaluating UnaryExpression. Let oldValue be ToNumber(GetValue(expr)). If oldValue is NaN, return NaN. Return the result of negating oldValue; that is, compute a Number with the same magnitude but opposite sign.

This is quite useful, but what I'm trying to understand is, how

compute a Number with the same magnitude but opposite sign

Is calculated. Is it the number * -1 or something else? Or is it multiple ways?

nikjohn
  • 20,026
  • 14
  • 50
  • 86
  • i do not think, that's a multiplication is involved, because sign change is cheaper than multiplication. so `function -(x) { return -x; }`. – Nina Scholz Oct 04 '16 at 07:00
  • 1
    @NinaScholz It might not be multiplication, that's just my guess. I am trying to find what it _actually_ is, to understand the native implementation – nikjohn Oct 04 '16 at 07:02
  • You wont find anything better than looking in the V8 source code, or SpiderMonkey (Mozilla) or Chakra (Edge) - or maybe read the ecmascript ecma-262 language specification to get an idea of how the functions should work - because no doubt V8, spidermonkey and chakra will do many things differently to each other – Jaromanda X Oct 04 '16 at 07:54
  • @JaromandaX I'm completely for that approach. But the trouble is that I'm not a C or C++ programmer, and I don't know where to start. The code bases are pretty large and my search hasn't proved useful so far. – nikjohn Oct 04 '16 at 07:59
  • 1
    That's the difficulty - you aren't a C or C++ programmer, yet you want to know how C/C++ code works ... two things come to mind ... how are you going to understand what you see, and why do you have this need to see how things work? – Jaromanda X Oct 04 '16 at 08:03
  • I can understand enough C and C++ to see how things are implemented, but not enough to figure out more framework level things like how files and directories are structured and so on. So if I can see where in the V8 code the `Unary -` is implemented, I can figure out how it is done. Would you be able to help out with that? – nikjohn Oct 04 '16 at 08:05
  • "Could someone explain what's happening here" — What about it don't you understand? What problem are you trying to solve? – Quentin Oct 04 '16 at 08:43
  • @Quentin I'm trying to understand how the `-1` is returned when I pass in `1` to the unary operator function. My C++ is limited to just basic functions, types etc. – nikjohn Oct 04 '16 at 08:45
  • For e.g. I don't understand the syntax in this: `Type* Typer::Visitor::JSTypeOfTyper(Type* type, Typer* t)` – nikjohn Oct 04 '16 at 08:46
  • @nikjohn — Why are you trying to understand that? – Quentin Oct 04 '16 at 08:48
  • @Quentin I've updated the question to exactly what I'm trying to know. I don't really have an answer for `Why`. I just am interested in knowing, that's all :) – nikjohn Oct 04 '16 at 08:56
  • possible duplicate of https://stackoverflow.com/questions/9103336/how-can-i-read-native-code-javascript-functions – Bergi Aug 22 '21 at 17:32

1 Answers1

2

There is no piece of code or single implementation of individual operators in V8. V8 is a just-in-time compiler that executes all JavaScript by compiling it to native code on the fly. V8 supports about 10 different CPU architectures and for each has 4 tiers of compilers. That already makes 40 different implementations of every operator. In many of those, compilation goes through a long pipeline of stages that transform the input to the actual machine code. And in each case the exact transformation depends on the type information that is available at compile time (collected on previous runs).

To understand what's going on you would need to understand a significant part of V8's complicated architecture, so it is pretty much impossible to answer your question in an SO reply. If you are just interested in the semantics, I rather suggest looking at the EcmaScript language definition.

(The snippet you cite is just a helper function for the converting compiler-internal type information for unary operators in one of the many stages.)

Edit: the excerpt from the EcmaScript definition you cite in the updated question is the right place to look. Keep in mind that all JavaScript numbers are IEEE floating point numbers. The sentence is basically saying that - just inverts the sign bit of such a number. You'd have to refer to the IEEE 754 standard for more details. Multiplication with -1.0 is a much more complicated operation, but will have the same result in most cases (probably with the exception of NaN operands).

Andreas Rossberg
  • 34,518
  • 3
  • 61
  • 72
  • Thanks for the answer. That clears up some of the confusion I had. I've updated the question with the ECMA specs. Could you look at it? – nikjohn Oct 04 '16 at 08:55