Is there a way to see the native JavaScript function code?

1

Recently I realized that there is a way to rewrite the native code of certain functions in JavaScript. For example:

console.log(confirm);

function confirm() {
  var a = "oi";
  return a;
}

var a = confirm();
console.log(a);

console.log(confirm);
/*
function confirm() {
  var a = "oi";
  return a;
} 
*/
var a = confirm();
console.log(a);

If you notice when giving a console.log in the function without rewriting it it appears: function confirm() { [native code] }

Is there any way to find out what the native JavaScript function code is?

    
asked by anonymous 22.01.2017 / 18:37

2 answers

3

In pure JavaScript probably not , this is because native functions may not be written in JavaScript completely, usually they are "interfaces" , which are used to access a functionality of the browser or provide functionality for another feature at a level above.

This How to customize "Notification Web API" in Qt? is an example where I used QtWebkit (an API that uses the Webkit engine) and had to rewrite it via C ++ (actually Qt API) to be able to customize Desktop notifications. p>

An example, still speaking of Qt, webkit is possible to write its own functions, with QWebFrame::addToJavaScriptWindowObject :

void Exemplo::setWebView( QWebView *view )
{
    QWebPage *page = view->page();
    frame = page->mainFrame();

    define();
    QObject::connect( frame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(define()) );
}

void Exemplo::define()
{
    frame->addToJavaScriptWindowObject( QString("Exemplo"), this );
}

Of course the question has nothing to do with Qt , but what I want to explain is that in this way above I wrote an object like this:

window.Exemplo.foo();

This will allow me to access functions of my C ++ implementation.

In other words, you do not have to see the native code, since it is probably just an interface (in most cases / browsers), of course there may be some exceptions, but it will be one or the other. So there's really not much reason to read the source code via JavaScript, because there is "no JavaScript code" there, these native functions probably only communicate with the browser / engine API and this triggers an existing browser.

Now if you just want to find out, regardless of JavaScript, you will have to read the source code of the browser (webkit, Gecko, presto, blink, trident, etc.) and browsers too (chromium, firefox, msedge) in their unpublished "versions", which are probably written in C or C++

Blink

Blink is the fork of Webkit, used by Chrome and is also used in other browsers, such as Opera , Vivaldi , etc.

Extending this ( line 33 ):

Gecko

Gecko is the "engine" used by Firefox or other Mozilla products, such as , I'm not sure this is the appropriate repository, but it looks like the code you're looking for is this:

>
23.01.2017 / 14:27
2

Depends on implementation. If it has the source code available you can find it with some effort.

If it's V8 that is used by Chrome and a lot of product, including Node.js, check out GitHub from V8 . Just do not ask me to find it for you :) Maybe someone who is familiar with this codebase can tell you where it is. In a quick search I did not find it.

    
22.01.2017 / 18:54