Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Using children sessions



It is possible to start another debug session in the context of one parent session. This can be useful for example if you want to start another ScriptWrapper in the Script helper.

Usage

The only thing you have to do to use another debug session in the context of a parent session is that rather from doing:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };
      Script Script = wrapper.getScript();
      wrapper.installScript(file);
      // do something with the script
You should do:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>(parentWrapper) {
      };
      Script Script = wrapper.getScript();
      wrapper.installScript(file);
      // do something with the script
In that case if the parent script wrapper is in a debug session, this script wrapper will be in a child session and will work correctly in the context of the debugger.

Example

Create the first script wrapper

Suppose for example that you have the following script interface:
      public interface Script {
        public int execute(int value) {
        }
      }
You can for example declare a Groovy script wrapper for this script by:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };
      Script Script = wrapper.getScript();
      File file = new File(<our script file>);
      wrapper.installScript(file);

      int value = script.execute(10);
Now you want to allow access to a scripted functions which correspond to another interface:
      public interface Script2 {
        public int compute(int value) {
        }
      }
The Groovy script wrapper for this script would be:
      ScriptWrapper<Script2> wrapper = new GroovyScriptWrapper<Script2>() {
      };

Create the second script wrapper

Suppose that you want to execute a script corresponding to this interface in the Script helper of the first wrapper. We would have in the first wrapper script helper:
      public class CustomHelper implements ScriptHelper {
        private final ScriptContext context;

        public CustomHelper(ScriptContext context) {
          this.context = context;
        }

        public int compute(File scriptFile, int value) {
          ScriptWrapper<Script2> wrapper = new GroovyScriptWrapper<Script2>(context.getScriptWrapper()) {
          };
          Script Script = wrapper.getScript();
          wrapper.installScript(file);
          int value = script.compute(value);
          return value;
        }
This allow to use any Script2 script to perform the computation. For example, we could have the following Script:
      public int execute(int value) {
        File scriptFile = context.getPath(<a Script2 script file>);
        int result = helper.compute(scriptFile, value);
        return result;
      }

Create a session

To debug the scripts, you just have to code:
      DebuggingWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };
      Script Script = wrapper.getScript();
      File file = new File(<our script file>);
      DebugSession<Script> session = wrapper.createDebugSession();
      SessionHook<Script> hook = new SessionHook<> {
        public Object start(Script script) {
          return script.execute();
        }
      }
      session.setSessionHook(hook);

      session.startSession();
      session.startScript(); // here the script.execute() method is effectively executed

See also


Categories: api | debugging

Copyright 2019-2020 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence