Interface InterpreterHook
- All Known Implementing Classes:
NullHook,RunLimitHook,SimpleHook
To implement a hook the class should implement this interface. It is
recommended to extend the class SimpleHook
instead of implementing a fresh class just implementing this interface. There
are a lot of methods in this interface and usually a hook class need
functionality only for a subset. The class SimpleHook gives default implementation for the
methods and eases hook development providing extended methods.
To make a hook class used by the interpreter the class should be on the
classpath during runtime and the configuration key hook.N
should contains the FQN of the implementing class. The configuration
parameter N should run from 0 ... and the hooks will be registered in
the order they are numbered and executed in backward order. To use the hook
delivered with jScriptBasic you should configure:
hook.0=com.scriptbasic.hooks.RunLimitHook
to have this hook called last (registered first). There are no hooks registered automatically.
The hooks are in a chain and every hook method should call the same hook method on the next hook object on the chain. The hooks are chained backward. The last hook will be called directly by the interpreter and that hook will call the previous and so on. Thus the first hook registered is called last and the last registered will be called first.
A hook method can decide for some good reason to break the chain not invoking
the next element in the chain. When implementing a hook class extending the class
SimpleHook all methods defined in this interface
are implemented. They simply disable hook handling, call the method of the same
name with the extra 'Ex' at the end, enable hook handling and then call the next hook.
For example the method SimpleHook.beforePop() calls SimpleHook.beforePopEx()
and then SimpleHook.beforePop() on the next hook object. During the execution
of SimpleHook.beforePopEx() the hook handling is disabled. The methods defined
only in SimpleHook are empty and do nothing.
More information about why disabling the hook handling is needed see
setReturnValue(RightValue) and Interpreter.disableHook()
- Author:
- Peter Verhas date Aug 3, 2012
-
Method Summary
Modifier and TypeMethodDescriptiondefault ObjectafterCallJavaFunction(Method method, Object result) This method is called when the interpreter was calling a Java static method.default voidThis method is called after the execution of the whole program has finished.default voidafterExecute(Command command) This method is called after the interpreter executed a command.default voiddefault voiddefault voidbeforeCallJavaFunction(Method method) default voidThis method is called before the execution of the program starts.default voidbeforeExecute(Command command) This method is called before the interpreter executes a command.default voiddefault voidbeforePush(Command command) default voidbeforeRegisteringJavaMethod(String alias, Class<?> klass, String methodName, Class<?>[] argumentTypes) This method is called before registering a java method into the interpreter.default voidbeforeSubroutineCall(String subroutineName, LeftValueList arguments, RightValue[] argumentValues) This method is called before the interpreter invokes a subroutine.default voidinit()This method is called at the end of the hook registering.voidsetInterpreter(Interpreter interpreter) During registration the interpreter calls this method to make the interpreter accessible for the hook objects.voidsetNext(InterpreterHook next) When a hook is registered the registering process calls this method and passes the next element in the hook chain.default voidsetReturnValue(RightValue returnValue) This method is called after a subroutine has set its return value.default RightValuevariableRead(String variableName, RightValue value) This hook is called when the interpreter accesses a variable.
-
Method Details
-
init
default void init()This method is called at the end of the hook registering. When this method is called the hook does have interpreter and the field 'next' already set. The method should do its initialization and then call the same method of the next hook in the chain. -
setNext
When a hook is registered the registering process calls this method and passes the next element in the hook chain. The hook object should remember this object and call the appropriate methods when that is called not to break the chain.- Parameters:
next- the next element in the chain.
-
setInterpreter
During registration the interpreter calls this method to make the interpreter accessible for the hook objects.- Parameters:
interpreter- parameter
-
beforeExecute
This method is called before the interpreter executes a command.- Parameters:
command- the command object to be executed
-
beforeExecute
default void beforeExecute()This method is called before the execution of the program starts. -
afterExecute
This method is called after the interpreter executed a command.- Parameters:
command- the command just executed.
-
afterExecute
default void afterExecute()This method is called after the execution of the whole program has finished. -
beforeRegisteringJavaMethod
default void beforeRegisteringJavaMethod(String alias, Class<?> klass, String methodName, Class<?>[] argumentTypes) This method is called before registering a java method into the interpreter.- Parameters:
alias- the name of the function as it will be known to the BASIC program.klass- the Java class where the static method is.methodName- the Java name of the methodargumentTypes- the argument types of the methods. This, together with the name of the method and the class identifies the actual method that will be available to the BASIC programs to be called through the namealias.
-
beforePush
- Parameters:
command- parameter
-
afterPush
- Parameters:
command- parameter
-
beforePop
default void beforePop() -
afterPop
- Parameters:
command- parameter
-
setReturnValue
This method is called after a subroutine has set its return value. It is possible to modify the return value calling back to the interpreter but it has to be only invoked together withInterpreter.disableHook()andInterpreter.enableHook().- Parameters:
returnValue- parameter- See Also:
-
beforeSubroutineCall
default void beforeSubroutineCall(String subroutineName, LeftValueList arguments, RightValue[] argumentValues) This method is called before the interpreter invokes a subroutine. At this point the local variables are those of the subroutine to be called.- Parameters:
subroutineName- the symbolic name of the subroutinearguments- the argument left valuesargumentValues- the argument evaluated values that were assigned to the local variable table to the arguments
-
beforeCallJavaFunction
- Parameters:
method- parameter
-
afterCallJavaFunction
This method is called when the interpreter was calling a Java static method. The- Parameters:
method- the method that was calledresult- the result that the static method returned- Returns:
- the modified result or just the same object if the hook does not want to modify the result
-
variableRead
This hook is called when the interpreter accesses a variable.- Parameters:
variableName- the name of the variablevalue- the value of the variable when accessed- Returns:
- the value that will be used. The implementation may decide to alter the value used. Returning a modified value will not, however alterthe value of the variable itself.
-