0

When I run this java code, i am able to get the values of variables define outside the function, but i am unable to get the values of variable define inside the function. how to access these variable values?

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;


public class JSFunctionTest {

public static void main(String args[]) {

    String code = "var name='nc',global_a = 'jill'; " + "\n"+
            "function myfunc(b) { " + "\n"+
            "var local_a = 1;" + "\n"+
            "global_a = 'jack';" + "\n"+
            " return b;" + "\n"+
            "}"; 

    Context context = Context.enter();
    context.setGeneratingDebug(true);
    context.setOptimizationLevel(-1);

    Scriptable scope = context.initStandardObjects(); 

    context.evaluateString(scope, code, "code", 1, null);
    //getting values of varables
    System.out.println("var name:"+scope.get("name", scope));
    System.out.println("var global_a:"+scope.get("global_a", scope));
    System.out.println("var local_a:"+scope.get("local_a", scope));//not found becase function wasnt run

    //calling the function.
    Object fObj = scope.get("myfunc", scope);
    if (!(fObj instanceof Function)) {
        System.out.println("myfunc is undefined or not a function.");
    } else {
        Object functionArgs[] = { "nc" };
        Function f = (Function)fObj;
        Object r = f.call(context, scope, scope, functionArgs);
        String report = "myfunc('nc') = " + Context.toString(r);

        //trying to access global and local a after calling function
        System.out.println("var global_a:"+scope.get("global_a", scope));//values is changed, because this is defined out side the function.
        System.out.println("var local_a:"+scope.get("local_a", scope));// still not found,after running the function.

        System.out.println(report);
    }

}

}

2 Answers 2

1

I was able to resolve this, by implementing a debugger using the debug API in Rhino.

  1. Implement your own debugger and debugframe classes.
  2. Hook them to your context using setDebugger() method
  3. in the DebugFrame class, I have implemented. cache the scope object in the onEnter method.
  4. in the onLineChange method, you can get the local variables using ScriptableObject.getProperty() passing the cache scope object and giving the names
Sign up to request clarification or add additional context in comments.

Comments

0

In ECMAScript, functions create their own scope. Outside the global scope, it's the only way to create new ones. See Rhino Scopes and Contexts for an example similar to yours.

The thing is that ECMAScript is a dynamic language (especially when optimization level is set to interpreted mode). This means the interpreter does not know in advance what it will encounter. The function's scope is only created/evaluated when the code is actually executed. So you can't evaluate a piece of code and query for variables inside a scope that isn't executed.

The question is why would you want to do that in practice? For debugging, you can step into the code and inspect the scopes, you should be able to see it.

2 Comments

Yes i want to do debugging.but i need to do it programmatic. my requirement is this,users of my system write there own javascript functions and send it to the system. system runs this function and users must be able to see how their function ran later time. its like annotated out put of the function with data values and how it changed in the function.
I see. It's sounds like an interesting project. I'm glad you found how to resolve your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.