javadelight / delight-nashorn-sandbox

A sandbox for executing JavaScript with Nashorn in Java.
Other
267 stars 81 forks source link

the beautifyjs function make script invoke timeout #120

Open hlzhang123 opened 2 years ago

hlzhang123 commented 2 years ago

we find when the script contains multiline comments were invoke by scriptEngine, before invoked,the beautify funtion decrease the tps

mxro commented 2 years ago

Thank you for raising this issue!

Is the problem you encounter that when a script contains multiline comments that the processing time for the script increases? Could you provide an example for a script?

Would it be possible to remove comments from the script before running it?

hlzhang123 commented 2 years ago

Thank you for raising this issue!

Is the problem you encounter that when a script contains multiline comments that the processing time for the script increases? Could you provide an example for a script?

Would it be possible to remove comments from the script before running it? thanks for your response ~~ String js= "\n" + // "// ;\n" + // "// ;\n" + // "// ;\n" + // "// ;\n" + "\n" + "var timestamp = new Date().getTime() + '';\n" + "$log.info(\"timestamp{}\", timestamp)\n" + "\n" + "var ArrayType = Java.type(\"java.lang.Object[]\");\n" + "\n" + "var arr = new ArrayType(3);\n" + "var testList = [{\n" + " name: \"123\"\n" + "}];\n" ;

the multiline //:\n comment canceled will be increase the script invoked tps; the script above can be tested by JMC flight record .

"Would it be possible to remove comments from the script before running it?"——sometimes addtional funtions may be commented for use in future !

mxro commented 2 years ago

@hlzhang123 Thank you for providing the sample code. I have created a new branch and put some of the code into there:

https://github.com/javadelight/delight-nashorn-sandbox/compare/issue-120?expand=1

Could you expand on the unit test I put there to show the problem?

Btw, the sandbox generates the following code from the one provided:

0    [main] DEBUG delight.nashornsandbox.NashornSandbox  - --- Running JS ---
0    [main] DEBUG delight.nashornsandbox.NashornSandbox  - Object.defineProperty(this, 'engine', {});Object.defineProperty(this, 'context', {});delete this.__noSuchProperty__;// ;
// ;
// ;
// ;
var timestamp = new Date().getTime() + '';
print("timestamp{}", timestamp)
var ArrayType = Java.type("java.lang.Object[]");
var arr = new ArrayType(3);
var testList = [{
 name: "123"
}];
1    [main] DEBUG delight.nashornsandbox.NashornSandbox  - --- JS END ---
timestamp{} 1635109014850

So it looks like the comments are not removed as they should according to this class: https://github.com/javadelight/delight-nashorn-sandbox/blob/master/src/main/java/delight/nashornsandbox/internal/RemoveComments.java ?