93

Is there any way to see the declaration of JavaScript native code? Whenever I try to look at a native function in Chrome or Firefox, it says "native code":

> String.fromCharCode
function fromCharCode() { [native code] }

What does this mean, and is there any tool which can help me to read actual code?

user3840170
  • 26,597
  • 4
  • 30
  • 62
emphaticsunshine
  • 3,725
  • 5
  • 32
  • 42
  • 4
    possible duplicate of [inspect native code](http://stackoverflow.com/questions/2872861/inspect-native-code) – Matt Ball Apr 03 '12 at 21:33
  • 1
    See also [How to get native javascript functions source code?](https://stackoverflow.com/q/22371251/1048572) – Bergi Jul 26 '20 at 20:49

3 Answers3

105

The reason Chrome or Firefox says that the code is native is that it really is native - the WebKit and Firefox developers have coded up that functionality in C or C++, not JavaScript. However, if you want to see the actual code, you can look at the source repositories for Chromium and Firefox.

Adam Mihalcin
  • 14,242
  • 4
  • 36
  • 52
  • 18
    I agree but I thought maybe some extension is there which does that for me. I can look into documentation but I was looking for a shortcut. – emphaticsunshine Feb 01 '12 at 21:12
  • Is there a way to look into native webkit code for Safari? I'm debugging an issue with webkitAudioContext (HTML5 WebAudio API). Dealing with ScriptProcessorNode, works fine in Chrome & Firefox and behaves weirdly in Safari. Since Safari code isn't open, is there a way to debug using something like `ptrace`? – singleton Oct 25 '20 at 10:38
34

Not within the JavaScript environment, but you can view the source for the open-source implementations.

Google V8: http://code.google.com/p/v8/source/browse

Mozilla SpiderMonkey: https://developer.mozilla.org/en/SpiderMonkey

  • 9
    That's very useful but I was looking within a console or something. A shortcut. Going to documentation and search for it was annoying. Anyways thank you for replying. – emphaticsunshine Feb 01 '12 at 21:14
-1

Firefox now supports the inspection of "[native code]" objects via standard dev tools.

To see it in action:

  1. update Firefox (definitely works w/ v68+, may work with older versions as well)
  2. open dev tools
  3. go to the Console
  4. type the name of a native object, e.g., "Document"
  5. hit return
  6. go nuts on all those little dropdown arrows

FireFox - inspect "native code" - Document object

Chrome still returns ƒ Document() { [native code] }, unfortunately.

Chrome - DON'T inspect "native code" - disappointment

gitbfd
  • 7
  • 1
  • 5
    This doesn't show the source code of the function, it just shows the object properties of the function object like `console.dir(Document)` does. – Bergi Jul 26 '20 at 20:52
  • 1
    I agree with Bergi. I used FF 80, ran `Number.prototype.constructor.toString()` and got `"function Number() { [native code] }"` as output. – Mr. Polywhirl Sep 17 '20 at 13:47