How can I read ‘native code’ JavaScript functions?
Asked Answered
M

3

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?

Maury answered 1/2, 2012 at 21:4 Comment(2)
possible duplicate of inspect native codeDap
See also How to get native javascript functions source code?Decussate
T
106

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.

Tailback answered 1/2, 2012 at 21:7 Comment(3)
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.Maury
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?Neptune
how can search what i want in this doc?!Horsley
A
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

Aerate answered 1/2, 2012 at 21:4 Comment(1)
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.Maury
D
-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

Drab answered 23/9, 2019 at 10:33 Comment(2)
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.Decussate
I agree with Bergi. I used FF 80, ran Number.prototype.constructor.toString() and got "function Number() { [native code] }" as output.Forage

© 2022 - 2024 — McMap. All rights reserved.