Search code examples
javajavascriptrhino

using Rhino, I've got a "ReferenceError" exception


I've got an error using Rhino(17R4).

My Goal is indent a javascript source code using jsbeautifier.js.

here's my 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 i = 0;
        while ((i = stream.read(readBytes)) > 0)
            contents.append(new String(readBytes, 0, i));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return contents.toString();
}
}

when I execute this code, it occurred below exception.

org.mozilla.javascript.EcmaError: ReferenceError: "js_beautify" is not defined. (beautify#1)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)
at org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3750)
at org.mozilla.javascript.ScriptRuntime.getNameFunctionAndThis(ScriptRuntime.java:2176)
at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:61)
at org.mozilla.javascript.gen.beautify_2._c_script_0(beautify:1)
at org.mozilla.javascript.gen.beautify_2.call(beautify)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
at org.mozilla.javascript.gen.beautify_2.call(beautify)
at org.mozilla.javascript.gen.beautify_2.exec(beautify)
at org.mozilla.javascript.Context.evaluateString(Context.java:1079)
at test.util.jsconverter.JSBeautifier.beautify(JSBeautifier.java:20)

How can I fix this problem?


Solution

  • The problem is that beautify.js is assigning its function to a variable in the global scope

    if (typeof define === "function") {
         // Add support for require.js
        define(function(require, exports, module) {
            exports.js_beautify = js_beautify;
        });
    } else if (typeof exports !== "undefined") {
        // Add support for CommonJS. Just put this file somewhere on your require.paths
        // and you will be able to `var js_beautify = require("beautify").js_beautify`.
        exports.js_beautify = js_beautify;
    } else if (typeof window !== "undefined") {
        // If we're running a web page and don't have either of the above, add our one global
        window.js_beautify = js_beautify;
    } else if (typeof global !== "undefined") {
        // If we don't even have window, try global.
        global.js_beautify = js_beautify;
    }
    

    So, you have to create a a global variable that it can assign its function to

    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);