java - using Rhino, I've got a "ReferenceError" exception -
i've got error using rhino(17r4).
my goal indent javascript source code using jsbeautifier.js.
here's code:
import java.io.inputstream; import org.mozilla.javascript.context; import org.mozilla.javascript.scriptable; public class jsbeautifier { public string beautify(string jssource) { context context = context.enter(); scriptable globalscope = context.initstandardobjects(); string beautify = getfilecontents(jsbeautifier.class.getresourceasstream("beautify.js")); context.evaluatestring(globalscope, beautify, "beautify", 1, null); globalscope.put("source", globalscope, jssource); context.evaluatestring(globalscope, "result = js_beautify(source);", "beautify", 1, null); object result = globalscope.get("result", globalscope); return (string)result; } private string getfilecontents(inputstream stream) { stringbuffer contents = new stringbuffer(""); try { byte[] readbytes = new byte[1024]; int = 0; while ((i = stream.read(readbytes)) > 0) contents.append(new string(readbytes, 0, i)); } catch (exception e) { e.printstacktrace(); } return contents.tostring(); } }
when execute code, occurred below exception.
org.mozilla.javascript.ecmaerror: referenceerror: "js_beautify" not defined. (beautify#1) @ org.mozilla.javascript.scriptruntime.constructerror(scriptruntime.java:3687) @ org.mozilla.javascript.scriptruntime.constructerror(scriptruntime.java:3665) @ org.mozilla.javascript.scriptruntime.notfounderror(scriptruntime.java:3750) @ org.mozilla.javascript.scriptruntime.getnamefunctionandthis(scriptruntime.java:2176) @ org.mozilla.javascript.optimizer.optruntime.callname(optruntime.java:61) @ org.mozilla.javascript.gen.beautify_2._c_script_0(beautify:1) @ org.mozilla.javascript.gen.beautify_2.call(beautify) @ org.mozilla.javascript.contextfactory.dotopcall(contextfactory.java:394) @ org.mozilla.javascript.scriptruntime.dotopcall(scriptruntime.java:3091) @ org.mozilla.javascript.gen.beautify_2.call(beautify) @ org.mozilla.javascript.gen.beautify_2.exec(beautify) @ org.mozilla.javascript.context.evaluatestring(context.java:1079) @ test.util.jsconverter.jsbeautifier.beautify(jsbeautifier.java:20)
how can fix problem?
the problem beautify.js assigning function variable in global scope
if (typeof define === "function") { // add support require.js define(function(require, exports, module) { exports.js_beautify = js_beautify; }); } else if (typeof exports !== "undefined") { // add support commonjs. put file somewhere on require.paths // , able `var js_beautify = require("beautify").js_beautify`. exports.js_beautify = js_beautify; } else if (typeof window !== "undefined") { // if we're running web page , don't have either of above, add our 1 global window.js_beautify = js_beautify; } else if (typeof global !== "undefined") { // if don't have window, try global. global.js_beautify = js_beautify; }
so, have create a global variable can assign function
context.evaluatestring(globalscope, "var global = {};", "global", 1, null); context.evaluatestring(globalscope, beautify, "beautify", 1, null); globalscope.put("source", globalscope, jssource); context.evaluatestring(globalscope, "result = global.js_beautify(source);", "beautify", 1, null);
Comments
Post a Comment