How can I read ‘native code’ JavaScript functions?
JavascriptJavascript Problem Overview
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?
Javascript Solutions
Solution 1 - Javascript
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.
Solution 2 - Javascript
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
Solution 3 - Javascript
Firefox now supports the inspection of "[native code]" objects via standard dev tools.
To see it in action:
- update Firefox (definitely works w/ v68+, may work with older versions as well)
- open dev tools
- go to the Console
- type the name of a native object, e.g., "Document"
- hit return
- go nuts on all those little dropdown arrows
FireFox - inspect "native code" - Document object
Chrome still returns ƒ Document() { [native code] }, unfortunately.
Solution 4 - Javascript
Chromium Code Search provides a good way to read javascript native code. This post addresses a litter bit about how to use it. And in this video, you can see an example.