4

This question is in reference to this old question Where-can-i-find-javascript-native-functions-source-code

The answer on that page says, that the source code is in c or c++ but I am curious as to why the source (definition) is in those languages? I mean they are JS functions definitions for e.g toString() method. It's a JavaScript function so its definition must be written using Javascript syntax.

toString; in chrome console outputs function toString() { [native code] }.

If it's a user-defined function then you can see the definition but not toString() or for that matter other in-built functions after all they are just function/methods that must be defined in JavaScript syntax for the engine to interpret them correctly.

I hope you can understand what point I am trying to make.

Community
  • 1
  • 1
b Tech
  • 412
  • 4
  • 14
  • `but I am curious as to why the source (definition) is in those languages?` because JS does not natively run anywhere but the browser. It's an interpreted language. Like pretty much all of them, it eventually calls something underlying in a different language in order to manipulate memory and C/C++ are the usual choice, since they can do that. – VLAZ Oct 09 '16 at 15:24
  • 3
    *It's a Javascript function so it's definition must be written using Javascript syntax.* Why would you say that? Many languages implement libraries in a foreign language, for various reasons, performance primary among them. –  Oct 09 '16 at 16:36
  • 2
    "*must be defined in JavaScript syntax for the engine to interpret them correctly*" - no. The whole point of the builtin functions is that they are built into the engine which *can* call them. – Bergi Oct 09 '16 at 16:41
  • Actually **some** builtin methods [are defined as JS functions](https://github.com/v8/v8/tree/master/src/js), but those accessing engine internals are not (cannot be). – Bergi Oct 09 '16 at 16:53
  • @torazaburo, yeah, that's a possible explanation I can live with, however that also means, if the engine authors wanted they could have very well defined methods in JS syntax? or is it that they must be defined in `c/c++` ? – b Tech Oct 09 '16 at 16:55
  • @bTech there's no *theoretical* limit to the implementation language, but generally speaking they are implemented in the same language as the host environment (see my answer). – Jared Smith Oct 09 '16 at 16:58
  • @Bergi, care to explain your last point taking `toString` (or any other inbuilt) method as an example, I feel this will certainly help me knowing JS inside-out better. – b Tech Oct 09 '16 at 16:59
  • @bTech for example anything touching the event loop (like `setTimeout`) would be unwise to expose. As would the browser's ability to touch the filesystem. – Jared Smith Oct 09 '16 at 17:10
  • @bTech Just look into any of the linked files. They define some methods that are then installed on the realm global objects. – Bergi Oct 09 '16 at 17:44
  • Your assumption that builtin functions "must be defined in JavaScript" does not hold. Also consider the bootstrap problem. If every function is implemented with other javascript functions, in the end you would be limited to functionality that can be provided with Javascript primitives. Some things cannot, for example `setTimeout`. – Knut Forkalsrud Oct 11 '16 at 21:30
  • V8 is implemented in C++, other javascript environments are implemented differently, for example Nashorn which is implemented in Java http://openjdk.java.net/projects/nashorn/ – Knut Forkalsrud Oct 11 '16 at 21:34

2 Answers2

8

As pointed out in the comments, you have a fundamental misunderstanding of how JavaScript works.

JavaScript is a scripting language, in the purest sense of that term, i.e. it is meant to script a host environment. It is meant to be embedded in a larger system (in this case, a web browser written in C/C++) to manipulate that system in a limited way.

Some other examples would be bash as the scripting language of unix, python as the scripting language of the sublime text editor, elisp as the scripting language of emacs, lua as the scripting language for World of Warcraft, etc.

When we say a function is 'built-in', we mean it is actually a function of the hosting environment (e.g. web-browser), not a function of the scripting language (JavaScript).

Although the JavaScript standard mandates certain built in functions, all that means is that a conforming host environment needs to expose that functionality, regardless of what language the underlying implementation is in.

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
  • That's a gr8 explanation, However, It would be more helpful if you can point out some functions which are actually functions for host env. e.g's are `Function()` or `Object()` functions for host env that must be defined in `c\c++`? – b Tech Oct 09 '16 at 17:10
  • @bTech sorry added that very thing to the comments on your question right about the time you added this. – Jared Smith Oct 09 '16 at 17:23
  • 1
    @bTech the standard only specifies behaviors, not implementations. Anything can be implemented however the implementors desire as long as the behavior is conformant. – xaxxon Oct 10 '16 at 17:55
0

This Questions opens up a lot of doors for beginners to understand more how Javascript works and helped me. The answers/comments given whilst helpful stop short at the bigger picture which I think could be helpful. I have written an answer in a way which assumes limited knowledge to the person that wants an answer. In 2016 that would have been the op.

Javascript was built to run in another environment rather than on a desktop or a server or any other programmable feature. In this case here, the Browser. It was built to interact with Browser API's such as The DOM and other useful Browser API's that have post dated it. Javascript itself does not come with built in modules e.g. to access the users host system/ create UI’s. It didn’t need this to implement what it was created for. Its nothing like Python or Java which come with built in modules which can access file systems along with various other capabilities through its built in Library/modules. Yes like any language we could install a library to access a file system however this will be blocked by the security of the Browsers Sandbox.

Javascript

Yes Javascript is a Programming Language but it is often referred to as a "scripting Language" although I'm yet to see thats an official term other than something that describes it well.

ECMAScript is a standard that all JS Engines within browsers adhere to. When we write our javascript code, we expect a result based on the ECMAScript specification. If using our own Functions e.g.

Const sayName(name) = ()=> {
  return name
} 

sayName('Kevin')

The JS engine (another program in itself written in another Language , more commonly C++) interprets our code within the execution. The JS engine has various functions that can be invoked and during the currently running execution. Firstly tho, the code is parsed and the parser recognises the keyword "Const" then expects the name of the Constant after and then followed by an = sign. The Parser does the same with the return keyword and also with the () and {}.

If any syntax is wrong anywhere within the program, the parser will fail and our execution will not go to the next phase of the engine which is the Abstract Syntax Tree ( Lets not worry about that today as we are in danger of going off topic.)

Once the Abstract Syntax Tree has been created I think this is where the Op ( and I ) used to get confused which is the phase where we reach the interpreter. Now the interpreter can clearly understand our simple function written above. However how about these "built in" things that Javascript comes with. E.g. Array String Math methods etc . Well as a developer of javascript we call these methods and in effect I like to see this as "our Job is done." We have called this Method. Now, whether it be a Static Method or an instance Method of one of our created objects e.g. an Array, I expect the exact result when my code is run that was promised to me by ECMAScript.

It’s important to note that some of these Functions/Methods may be implemented in Javascript, but most will be implemented in Lower Level code such as C/C++. This is similar to how Python works where it's built in modules are written in C and executed via the Python Interpretation. Back to Javascript - How these functions are implemented is not important to neither myself or ECMAScript. Its the choice of the Browser how it goes about doing this and some very talented lower level programmers. ( Those C programmers and those employed by the Browser to implement the functionality of the Engine to comply with ECMAScripts expected result). Now we have all the functionality we need, the interpreter gets to work and after other processes ( depending on what engine is used ) turns this into Machine Code that CPU's can understand ( again explaining this will be off topic). So If we are logging to the console a definition of a function/Method which is within the JS Engine that we didn't write ourself thats where we get the Native code. E.g.

console.log(Math.random)

Thats when we get

function random() {
[native code]
}

Above this is described by a good and experienced developer as "Native code will appear if its a function of the hosting environment" which is the Browser and gives SetTimeout as an example. Yes this will also appear as Native Code which is code we didn't write ourselves, but this is a Function of the Browser, not part of the Javascript Language itself. All functions/Methods not written by us e.g. within the browser or at a lower level will appear as “Native Code”.

Nodejs

Now however we have another runtime environment that the JS engine can fit into. A technology everyone knows as Nodejs. Our Browser are replaced with other Api’s more useful on a server. We can now access a filesystem within the Node runtime environment. There is nothing wrong with accessing a filesystem on our own server. These API’s are suited to servers, however we are still in a runtime environment, we still have a javascript interpreter, our code is still interpreted, we still call lower level functions during the interpretation, it just means we are inside the Nodejs runtime environment rather than our “favourite” Browser.