Where can I find javascript native functions source code? [duplicate]
Asked Answered
T

3

22

where can I find the source code of native js functions, for example I want to look at eval() function

Transpicuous answered 23/4, 2012 at 22:26 Comment(2)
Depending on which one too...Sufi
Just want to point out: eval in particular is the whole engine (or at least, it's an entry point to the whole engine). There are lots of Javascript engines out there to look at.Electrotype
A
2

In the JavaScript engine's source code.

Adherent answered 23/4, 2012 at 22:27 Comment(11)
can I online look at the code?Transpicuous
@ASD, if it's open source, then probably yes. What engine are you looking for?Adherent
And keep in mind that the source isn't in JavaScript -- it's usually either C or C++. (Otherwise it wouldn't be native!)Introit
if yes,can share link to the eval() functionTranspicuous
@Transpicuous - V8 (the engine used in Google Chrome) is here: code.google.com/p/v8/source/browse.Komsomol
Google it...I mean... literally.Sufi
@Radu - This seems like a flip and unhelpful answer. Of course, anyone who can look at the source code for a particular browsers engine can look at native functions. Tell us all something we don't already know.Listel
@jfriend00, it may be obvious to you, but it's certainly not obvious to the poster, otherwise he wouldn't have asked the question. If it was obvious, then the question would have been closer to "where in the source code of engine X is the eval function defined?". And "tell us something we don't already know"? This is not an answer for you or the people who already know, it's for the ones who don't.Adherent
@Radu - look at how they have edited their question. They want more specific info than you have offered.Listel
@Adherent Is my understanding deeply flawed when I roughly equate a JS engine to a JVM or CLR? While the JVM may not be written in Java, the source for the actual Java classes I use can be found online, whereas your answer makes me think everything I use that's built into JS is in the engine.Karankaras
@Abdul, the JS engine is indeed like the JVM or CLR. In the case of JS, the standard does not explicitly specify which language the VM and libraries must be written in, but both are usually written as native code. Try for example something like console.log(Date) and you'll most likely see function Date() { [native code] }.Adherent
L
24

Both Chrome and Firefox are open source and you can look at the implementation of any part of the javascript engine in the source code for those products. Other browsers have their own implementation (like IE) that is not available to the public to look at.

The Chrome v8 javascript engine code is here: https://github.com/v8/v8

The Firefox SpiderMonkey engine code is here: https://searchfox.org/mozilla-central/source/js/src

Warning, if you aren't already familiar with those products and their tools, it may take while to get familiar enough to find what you're looking for.

These links may change over time, but can easily be found with Google searches if they move.

Listel answered 23/4, 2012 at 22:35 Comment(3)
i found v8 but not found the code that contains the functions of the window objectMcdonald
@Igor - The window object is not part of Javascript per se. It's a host object so you would find code for it in the actual implementation of Chrome or Firefox, not in the JS engine.Listel
both links are outdated :(Saffian
D
4

Javascript is a script language that is implemented within a Browser's code-base. This means that there may be different implementations of the script language, with different levels of quality, and possibly different intepretations of what is required. Hence the head-against-wall frustrations of many a web-developer when dealing with different web-browsers.

It is possible for you to examine a browser's implementation of Javascript if the browser is an Open Source version, eg: Chrome, Firefox, as given in other answers listed.

Dahlberg answered 23/4, 2012 at 22:39 Comment(0)
A
2

In the JavaScript engine's source code.

Adherent answered 23/4, 2012 at 22:27 Comment(11)
can I online look at the code?Transpicuous
@ASD, if it's open source, then probably yes. What engine are you looking for?Adherent
And keep in mind that the source isn't in JavaScript -- it's usually either C or C++. (Otherwise it wouldn't be native!)Introit
if yes,can share link to the eval() functionTranspicuous
@Transpicuous - V8 (the engine used in Google Chrome) is here: code.google.com/p/v8/source/browse.Komsomol
Google it...I mean... literally.Sufi
@Radu - This seems like a flip and unhelpful answer. Of course, anyone who can look at the source code for a particular browsers engine can look at native functions. Tell us all something we don't already know.Listel
@jfriend00, it may be obvious to you, but it's certainly not obvious to the poster, otherwise he wouldn't have asked the question. If it was obvious, then the question would have been closer to "where in the source code of engine X is the eval function defined?". And "tell us something we don't already know"? This is not an answer for you or the people who already know, it's for the ones who don't.Adherent
@Radu - look at how they have edited their question. They want more specific info than you have offered.Listel
@Adherent Is my understanding deeply flawed when I roughly equate a JS engine to a JVM or CLR? While the JVM may not be written in Java, the source for the actual Java classes I use can be found online, whereas your answer makes me think everything I use that's built into JS is in the engine.Karankaras
@Abdul, the JS engine is indeed like the JVM or CLR. In the case of JS, the standard does not explicitly specify which language the VM and libraries must be written in, but both are usually written as native code. Try for example something like console.log(Date) and you'll most likely see function Date() { [native code] }.Adherent

© 2022 - 2024 — McMap. All rights reserved.