Calling function.toString() returns `[native code]`
See original GitHub issueI just tried pkg
ing 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:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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, thentoString
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@stephenmathieson is there a workaround for this?
Just did a quick test:
test.js file:
Return with node test.js:
After running pkg:
pkg --targets node12-macos-x64 test.js
./test
As you can see with pkg
Function.prototype.toString()
returnsfunction aFunction() { [native code] }
instead of the actual function.JavaScript Reference
CC: @igorklopov