question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling function.toString() returns `[native code]`

See original GitHub issue

I just tried pkging an application which injects JavaScript code into a Selenium-driven browser. The JS code/injection looks something like:

var browserCode = (something) => {
  // Do various things... but for the sake of this contrived example, we'll change the <body>
  document.body.innerHTML = something
}

var error = await webdriverInstance.executeAsyncScript(`
  var __selenium_callback__ = arguments[arguments.length - 1]

  try {
    (
      ${browserCode.toString()}
    )(
      "${someValue}"
    )

    __selenium_callback__(null)
  } catch (error) {
    __selenium_callback__(error)
  }
`);

if (error) {
  throw new Error(error.message);
}

This yields a SyntaxError, as the injected code resembles something like:

var __selenium_callback__ = arguments[arguments.length - 1]

try {
  (
    [native code]
  )(
    "hello world"
  )

  __selenium_callback__(null)
} catch (error) {
  __selenium_callback__(error)
}

This seems very related to https://github.com/zeit/pkg/issues/62, but it’s not exclusively caused by calling Class#toString().

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
igorklopovcommented, Jun 21, 2020

If source code is stripped while packaging, then toString has nothing to show, that’s why [native code]. But if you force pkg to leave source code for that js file, then toString will likely work as in node.js environment. For example --public option leaves source code of most js files intact. Also you can use any kind of config (package.json or something.json with -c something.json) and specify something like

{
  "pkg": {
    "assets": [
      "file-to-leave-sources-in-executable.js"
    ]
  }
}
1reaction
t16ncommented, Apr 29, 2020

@stephenmathieson is there a workaround for this?

Just did a quick test:

test.js file:

const aFunction = () => 'Hello World';

console.log(typeof aFunction);
console.log(aFunction);
console.log(aFunction());
console.log(aFunction.toString());

console.log('end');

Return with node test.js:

function
[Function: aFunction]
Hello World
() => 'Hello World'
end

After running pkg: pkg --targets node12-macos-x64 test.js ./test

function
[Function: aFunction]
Hello World
function aFunction() { [native code] }
end

As you can see with pkg Function.prototype.toString() returns function aFunction() { [native code] } instead of the actual function.

JavaScript Reference

CC: @igorklopov

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does this JavaScript function return: "0:0function toString ...
Thus the parser has no problems with your code. What goes wrong is when you implicitly convert those references to strings. If you...
Read more >
Calling function.toString() returns [native code] #676 - GitHub
I just tried pkging an application which injects JavaScript code into a Selenium-driven browser. The JS code/injection looks something like: ...
Read more >
Function.prototype.toString() - JavaScript - MDN Web Docs
JavaScript calls the toString method automatically when a Function is to be represented as a text value, e.g. when a function is concatenated ......
Read more >
What is the code behind the JavaScript function toString?
prototype.toString has been overridden), you can see the source by calling, foo.toString.toString() (calling toString on a function returns the source code ...
Read more >
Simple WebService call results in 'function toString ... - MSDN
Insteading of getting HTML, I get: function toString() { [native code] }. Anyone know what's happening here, because I'm stumped.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found