2

I was working on node and want to see the compiled bytecode . Is there a way we can compile a js file and save the bytecode in a seperate file using node ??

10
  • 1
    javascript isn't compiled (in the traditional sense) - it's an interpreted language Commented Apr 18, 2017 at 1:11
  • Yeah, I mean interpreted/compiled into bytecode by node. Node do compile JS into bytecode. I did not get your comment. Commented Apr 18, 2017 at 1:12
  • 2
    The basic answer is "no". Commented Apr 18, 2017 at 1:14
  • 1
    Node (or rather V8 JS engine that Node runs on) does not compile into bytecode, it is compiled on-the-fly into native code. If you wish to see the generated native code, this answer might be of use. Commented Apr 18, 2017 at 1:25
  • 1
    Note that other JS engines do have bytecode; e.g. SpiderMonkey, used by Mozilla, does have bytecode; and Rhino compiles JavaScript to JVM. None of those are used by Node, though. Commented Apr 18, 2017 at 1:57

2 Answers 2

1

You can use --trace_ignition to get the intermediary byte code:

#node --trace_ignition a.js
 -> 0x3aa1c02bf66e @    0 : 80                StackCheck 
 -> 0x3aa1c02bf66f @    1 : 13 13             LdaImmutableCurrentContextSlot [19]
      [ accumulator <- 0xd88d0f93ce9 <JS Function NativeModule (SharedFunctionInfo 0x3aa1c02b86a1)> ]
 -> 0x3aa1c02bf671 @    3 : 1f f7             Star r3
      [ accumulator -> 0xd88d0f93ce9 <JS Function NativeModule (SharedFunctionInfo 0x3aa1c02b86a1)> ]
      [          r3 <- 0xd88d0f93ce9 <JS Function NativeModule (SharedFunctionInfo 0x3aa1c02b86a1)> ]
 -> 0x3aa1c02bf673 @    5 : 21 f7 00 04       LdaNamedProperty r3, [0], [4]
      [          r3 -> 0xd88d0f93ce9 <JS Function NativeModule (SharedFunctionInfo 0x3aa1c02b86a1)> ]
      [ accumulator <- 0x3aa1c02bcb49 <JS Function NativeModule.getSource (SharedFunctionInfo 0x3aa1c02b8c51)> ]
 -> 0x3aa1c02bf677 @    9 : 1f f8             Star r2
      [ accumulator -> 0x3aa1c02bcb49 <JS Function NativeModule.getSource (SharedFunctionInfo 0x3aa1c02b8c51)> ]
      [          r2 <- 0x3aa1c02bcb49 <JS Function NativeModule.getSource (SharedFunctionInfo 0x3aa1c02b8c51)> ]
 -> 0x3aa1c02bf679 @   11 : 21 02 01 06       LdaNamedProperty <this>, [1], [6]
      [      <this> -> 0xd88d0f9a329 <a NativeModule with map 0x11929f412849> ]
      [ accumulator <- 0x3aa1c02bb969 <String[6]: events> ]
 -> 0x3aa1c02bf67d @   15 : 1f f6             Star r4
      [ accumulator -> 0x3aa1c02bb969 <String[6]: events> ]
      [          r4 <- 0x3aa1c02bb969 <String[6]: events> ]
 -> 0x3aa1c02bf67f @   17 : 45 f8 f7 02 02    CallProperty r2, r3-r4, [2]
      [          r2 -> 0x3aa1c02bcb49 <JS Function NativeModule.getSource (SharedFunctionInfo 0x3aa1c02b8c51)> ]
      [          r3 -> 0xd88d0f93ce9 <JS Function NativeModule (SharedFunctionInfo 0x3aa1c02b86a1)> ]
      [          r4 -> 0x3aa1c02bb969 <String[6]: events> ]
      [ accumulator <- 0xd88d0f956f1 <Very long string[15201]> ]
 -> 0x3aa1c02bf684 @   22 : 1f f9             Star r1
      [ accumulator -> 0xd88d0f956f1 <Very long string[15201]> ]
      [          r1 <- 0xd88d0f956f1 <Very long string[15201]> ]

You can use --print_code to get the natively generated code:

#node --print_code -e 'console.log("hello")' > code.txt
#head -100 code.txt
    --- Raw source ---
    (){
      var originAssert = this.assert;
      originAssert.apply = Function.prototype.apply;
      this.assert = assertWrapper;
      assertWrapper.toString = () => originAssert.toString();
      function assertWrapper(){
        if (!!arguments[0]) return;
        originAssert.apply(null, arguments);
      }
    })

    --- Code ---
    source_position = 9
    kind = FUNCTION
    compiler = full-codegen
    Instructions (size = 500)
    0x29b8195043c0     0  55             push rbp
    0x29b8195043c1     1  4889e5         REX.W movq rbp,rsp
    0x29b8195043c4     4  56             push rsi
    0x29b8195043c5     5  57             push rdi
    0x29b8195043c6     6  488b4f2f       REX.W movq rcx,[rdi+0x2f]
    0x29b8195043ca    10  488b4907       REX.W movq rcx,[rcx+0x7]
    0x29b8195043ce    14  83411b01       addl [rcx+0x1b],0x1
    0x29b8195043d2    18  41ff75a0       push [r13-0x60]
    0x29b8195043d6    22  b801000000     movl rax,0x1
    0x29b8195043db    27  e80038f3ff     call FastNewFunctionContextFunction  (0x29b819437be0)    ;; code: BUILTIN
    0x29b8195043e0    32  488bf0         REX.W movq rsi,rax
    0x29b8195043e3    35  488945f8       REX.W movq [rbp-0x8],rax

Hope this helps.

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

Comments

0

You can see the byte code

If you want to see V8's bytecode of JavaScript code, you can print it by calling D8 or Node.js (8.3 or higher) with the flag --print-bytecode. For Chrome, start Chrome from the command line with --js-flags="--print-bytecode", see Run Chromium with flags.

Read full article here Understanding V8 Byte code

Not sure how to print into a file.

1 Comment

You can redirect the output to a file using output redirection via >

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.