Browse Source

Pull request #9912: KERNEL-12469 js引擎j2v8剥离成独立插件

Merge in DESIGN/design from ~PENGDA/design:release/11.0 to release/11.0

* commit 'afee1887d4a3fa79c195b257733d23ca49f1705a':
  KERNEL-12469 js引擎j2v8剥离成独立插件
  KERNEL-12469 js引擎j2v8剥离成独立插件
release/11.0
pengda 2 years ago
parent
commit
eeba80dfe9
  1. 31
      designer-base/src/main/java/com/fr/design/javascript/beautify/JavaScriptFormatHelper.java

31
designer-base/src/main/java/com/fr/design/javascript/beautify/JavaScriptFormatHelper.java

@ -1,6 +1,13 @@
package com.fr.design.javascript.beautify;
import com.fr.general.IOUtils;
import com.fr.log.FineLoggerFactory;
import com.fr.stable.EncodeConstants;
import java.io.InputStream;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class JavaScriptFormatHelper {
@ -23,24 +30,16 @@ public class JavaScriptFormatHelper {
* @see <a href="https://github.com/beautify-web/js-beautify">JSBeautify<a/>
*/
public static String beautify(String jsCode, BeautifyOption option) {
/* InputStream resourceAsStream = IOUtils.readResource("com/fr/design/javascript/beautify/beautify.js");
String result = jsCode;
V8 v8 = V8.createV8Runtime();
try {
v8.executeVoidScript(IOUtils.inputStream2String(resourceAsStream, EncodeConstants.ENCODING_UTF_8));
V8Array parameters = new V8Array(v8);
parameters.push(jsCode);
V8Object arg = V8ObjectUtils.toV8Object(v8, option.toFormatArgument());
parameters.push(arg);
result = v8.executeStringFunction("js_beautify_global", parameters);
parameters.release();
arg.release();
} catch (UnsupportedEncodingException e) {
try (InputStream resourceAsStream = IOUtils.readResource("com/fr/design/javascript/beautify/beautify.js")) {
ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("JavaScript");
nashorn.eval(IOUtils.inputStream2String(resourceAsStream, EncodeConstants.ENCODING_UTF_8));
Invocable invocable = (Invocable) nashorn;
result = (String) invocable.invokeFunction("js_beautify_global", new Object[]{jsCode, option.toFormatArgument()});
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
} finally {
v8.release(true);
}*/
return jsCode;
}
return result;
}
public static void main(String[] args) {

Loading…
Cancel
Save