On Jun 9, 12:03 pm, Mike Tardif <[EMAIL PROTECTED]> wrote:
> Given a piece of JS script, how do I enumerate the list of functions
> and global variables associated with that particular script.

Attila kindly replied
> cast the Script object into a DebuggableScript (you might need to use a 
> Context with optimization level set
> to -1;  I'm not sure about that).
> DebuggableScript has all the methods you'll need.
>
> Attila.

Had some difficulty interpreting what cast really meant, but
eventually figured it out.  For future references then, here is a
solution:
    Context cx = Context.enter();
    Scriptable scope = cx.initStandardObjects();
    String source =
        "function foo(a,b,c) {" +
        "    var str = \"sum:\";" +
        "    var sum = a + b + c;" +
        "    return (str + sum);" +
        "}" +
        "var var1 = \"one\";" +
        "var var2 = 2;" +
        "var var3;" +
        "var3 = \"three\";";
    cx.setOptimizationLevel(-1);
    Script script = cx.compileString(source, null, 0, null);
    DebuggableScript view = cx.getDebuggableView(script);
    int funcs = view.getFunctionCount();
    for (int i = 0; i < funcs; i++) {
        DebuggableScript func = view.getFunction(i);
        String name = func.getFunctionName();
        System.out.println(name);
    }
    int vars = view.getParamAndVarCount();
    for (int i = 0; i < vars; i++) {
        String name = view.getParamOrVarName(i);
        System.out.println(name);
    }
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to