Where in the V8 source does the automatic cast for BinaryOperation occour?












0















I stumbled again in the good old '12' + 2 = '122'



I wanted to deeply understand what happens here, so my first thesis was that




Maybe Javascript casts the right operand to the type of the first one and
then operates, like so: '12' + String(2) = '122' all good...




But no, because 12 + '2' = '122' too; So the engine's magic is clearly favoring to concat over casting to number.



My second thesis was then




Maybe the engine enumerates all operands and looks for an "operator override", similar to C#? And then favor executing that over doing the self-magic thing?




My confusion got even weirder when I realized that also '5' * '8' = 40, it casts both operands to Number and does the operation.
The only way I could possibly really understand that was to read the V8 code directly from GitHub



The farther I could track down was at v8/src/parsing/parser-base.h line 2865



// We have a "normal" binary operation.
x = factory()->NewBinaryOperation(op, x, y, pos);
if (op == Token::OR || op == Token::AND) {
impl()->RecordBinaryOperationSourceRange(x, right_range);
}


From here I got lost, because I couldn't find where this factory() is coming from.



Long story short, where does the JavaScript "type Magic" come from in the V8 Engine Source code?










share|improve this question



























    0















    I stumbled again in the good old '12' + 2 = '122'



    I wanted to deeply understand what happens here, so my first thesis was that




    Maybe Javascript casts the right operand to the type of the first one and
    then operates, like so: '12' + String(2) = '122' all good...




    But no, because 12 + '2' = '122' too; So the engine's magic is clearly favoring to concat over casting to number.



    My second thesis was then




    Maybe the engine enumerates all operands and looks for an "operator override", similar to C#? And then favor executing that over doing the self-magic thing?




    My confusion got even weirder when I realized that also '5' * '8' = 40, it casts both operands to Number and does the operation.
    The only way I could possibly really understand that was to read the V8 code directly from GitHub



    The farther I could track down was at v8/src/parsing/parser-base.h line 2865



    // We have a "normal" binary operation.
    x = factory()->NewBinaryOperation(op, x, y, pos);
    if (op == Token::OR || op == Token::AND) {
    impl()->RecordBinaryOperationSourceRange(x, right_range);
    }


    From here I got lost, because I couldn't find where this factory() is coming from.



    Long story short, where does the JavaScript "type Magic" come from in the V8 Engine Source code?










    share|improve this question

























      0












      0








      0








      I stumbled again in the good old '12' + 2 = '122'



      I wanted to deeply understand what happens here, so my first thesis was that




      Maybe Javascript casts the right operand to the type of the first one and
      then operates, like so: '12' + String(2) = '122' all good...




      But no, because 12 + '2' = '122' too; So the engine's magic is clearly favoring to concat over casting to number.



      My second thesis was then




      Maybe the engine enumerates all operands and looks for an "operator override", similar to C#? And then favor executing that over doing the self-magic thing?




      My confusion got even weirder when I realized that also '5' * '8' = 40, it casts both operands to Number and does the operation.
      The only way I could possibly really understand that was to read the V8 code directly from GitHub



      The farther I could track down was at v8/src/parsing/parser-base.h line 2865



      // We have a "normal" binary operation.
      x = factory()->NewBinaryOperation(op, x, y, pos);
      if (op == Token::OR || op == Token::AND) {
      impl()->RecordBinaryOperationSourceRange(x, right_range);
      }


      From here I got lost, because I couldn't find where this factory() is coming from.



      Long story short, where does the JavaScript "type Magic" come from in the V8 Engine Source code?










      share|improve this question














      I stumbled again in the good old '12' + 2 = '122'



      I wanted to deeply understand what happens here, so my first thesis was that




      Maybe Javascript casts the right operand to the type of the first one and
      then operates, like so: '12' + String(2) = '122' all good...




      But no, because 12 + '2' = '122' too; So the engine's magic is clearly favoring to concat over casting to number.



      My second thesis was then




      Maybe the engine enumerates all operands and looks for an "operator override", similar to C#? And then favor executing that over doing the self-magic thing?




      My confusion got even weirder when I realized that also '5' * '8' = 40, it casts both operands to Number and does the operation.
      The only way I could possibly really understand that was to read the V8 code directly from GitHub



      The farther I could track down was at v8/src/parsing/parser-base.h line 2865



      // We have a "normal" binary operation.
      x = factory()->NewBinaryOperation(op, x, y, pos);
      if (op == Token::OR || op == Token::AND) {
      impl()->RecordBinaryOperationSourceRange(x, right_range);
      }


      From here I got lost, because I couldn't find where this factory() is coming from.



      Long story short, where does the JavaScript "type Magic" come from in the V8 Engine Source code?







      v8






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 1:35









      FelypeFelype

      1,77811327




      1,77811327
























          1 Answer
          1






          active

          oldest

          votes


















          2














          V8 developer here.



          There are several fast paths for various cases of addition and other operations in V8. If you want to study a canonical (slow, but complete) version, you can look for Object::Add in src/objects.cc.



          That said, the source of truth here is not any given engine's implementation, but the JavaScript specification. What the + operator is supposed to do is defined here: https://tc39.github.io/ecma262/#sec-addition-operator-plus.
          Any engine's implementation either does precisely that, or something that from the outside is indistinguishable from that -- otherwise it's a bug. It's not a coincidence that the implementation of Object::Add reads almost exactly like the spec ;-)






          share|improve this answer
























          • Awesome, this is precisely what I was looking for, tyvm.

            – Felype
            Nov 24 '18 at 3:21











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454449%2fwhere-in-the-v8-source-does-the-automatic-cast-for-binaryoperation-occour%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          V8 developer here.



          There are several fast paths for various cases of addition and other operations in V8. If you want to study a canonical (slow, but complete) version, you can look for Object::Add in src/objects.cc.



          That said, the source of truth here is not any given engine's implementation, but the JavaScript specification. What the + operator is supposed to do is defined here: https://tc39.github.io/ecma262/#sec-addition-operator-plus.
          Any engine's implementation either does precisely that, or something that from the outside is indistinguishable from that -- otherwise it's a bug. It's not a coincidence that the implementation of Object::Add reads almost exactly like the spec ;-)






          share|improve this answer
























          • Awesome, this is precisely what I was looking for, tyvm.

            – Felype
            Nov 24 '18 at 3:21
















          2














          V8 developer here.



          There are several fast paths for various cases of addition and other operations in V8. If you want to study a canonical (slow, but complete) version, you can look for Object::Add in src/objects.cc.



          That said, the source of truth here is not any given engine's implementation, but the JavaScript specification. What the + operator is supposed to do is defined here: https://tc39.github.io/ecma262/#sec-addition-operator-plus.
          Any engine's implementation either does precisely that, or something that from the outside is indistinguishable from that -- otherwise it's a bug. It's not a coincidence that the implementation of Object::Add reads almost exactly like the spec ;-)






          share|improve this answer
























          • Awesome, this is precisely what I was looking for, tyvm.

            – Felype
            Nov 24 '18 at 3:21














          2












          2








          2







          V8 developer here.



          There are several fast paths for various cases of addition and other operations in V8. If you want to study a canonical (slow, but complete) version, you can look for Object::Add in src/objects.cc.



          That said, the source of truth here is not any given engine's implementation, but the JavaScript specification. What the + operator is supposed to do is defined here: https://tc39.github.io/ecma262/#sec-addition-operator-plus.
          Any engine's implementation either does precisely that, or something that from the outside is indistinguishable from that -- otherwise it's a bug. It's not a coincidence that the implementation of Object::Add reads almost exactly like the spec ;-)






          share|improve this answer













          V8 developer here.



          There are several fast paths for various cases of addition and other operations in V8. If you want to study a canonical (slow, but complete) version, you can look for Object::Add in src/objects.cc.



          That said, the source of truth here is not any given engine's implementation, but the JavaScript specification. What the + operator is supposed to do is defined here: https://tc39.github.io/ecma262/#sec-addition-operator-plus.
          Any engine's implementation either does precisely that, or something that from the outside is indistinguishable from that -- otherwise it's a bug. It's not a coincidence that the implementation of Object::Add reads almost exactly like the spec ;-)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 2:33









          jmrkjmrk

          6,203828




          6,203828













          • Awesome, this is precisely what I was looking for, tyvm.

            – Felype
            Nov 24 '18 at 3:21



















          • Awesome, this is precisely what I was looking for, tyvm.

            – Felype
            Nov 24 '18 at 3:21

















          Awesome, this is precisely what I was looking for, tyvm.

          – Felype
          Nov 24 '18 at 3:21





          Awesome, this is precisely what I was looking for, tyvm.

          – Felype
          Nov 24 '18 at 3:21




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454449%2fwhere-in-the-v8-source-does-the-automatic-cast-for-binaryoperation-occour%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin