Is there a way to view native Javascript function code?

Asked

Viewed 379 times

1

I recently 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 can tell by giving console.log function without rewriting it appears: function confirm() { [native code] }

Is there any way to find out which is the native code of Javascript functions?

2 answers

3


In pure Javascript probably not, this is because the native functions usually may not be written in Javascript completely, usually they are "interfaces", that are used to access an internal browser functionality or provide functionality for another feature on a level above.

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

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();

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

That is, there is no way to view the native code, because it is probably just an interface (in most cases/browsers), of course there may be some exception, but it will be one or the other.

So there really isn’t 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 functionality.


Now if you just want to find out, independent of Javascript, you will have to read the source code of the engine used by the browser (Webkit, Gecko, presto, Blink, Trident, etc) and the browsers also (Chromium, firefox, msedge) in its "versions" not compiled, 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..

That extend from this (line 33):

Gecko

Gecko is the "engine" used by Firefox or other Mozilla products, such as Thunderbird, i am not sure this is the appropriate repository, but it seems that the code you are looking for is this:

2

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

If it is the V8 that is used by Chrome and a lot of product, among them Node.js, you can check on Github from the V8. Just don’t ask me to find it for you :) Maybe someone who familiarity with this code base can tell you where it is. In a quick search I couldn’t find.

  • I found this repository: https://chromium.googlesource.com/v8/v8/+/6a7ec6a3bf779cdd41c66a768fd7a37195ed7b7f/src/js/ But it still seems to me to find a needle in the haystack

Browser other questions tagged

You are not signed in. Login or sign up in order to post.