Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Javascript scripts



Javascript scripts are a type of scripts which can be used in the library. Javascript scripts use the Rhino library. Note that the JSScriptWrapper have more capabilities than other language wrappers.

Script syntax creation

By default you must implement all the methods of the script interface.

  • You don't need to implement any method to get the Script context. The framework will take care of it under the hood

Functions declarations

Functions and methods declared in your Javascript scripts will be seen as methods at the Java level. Which means that the following declarations are equivalent and are both handled by the framework:
      compute: function(value) {
        return value * 2;
      }
or:
      function compute(value) {
        return value * 2;
      }

Adding properties and built-in functions


In addition to the abilities that Javascript scripts share with other scripting languages, it is also possible to add built-in fonctions, such as for example:
      helloWorld: function() {
      alert("Im am here"!);
        return 10;
      }
and also properties, such as for example:
      getLabel: function(identifier) {
        return document.getElementById(identifier).label;
      }

Managing the Javascript context

You must be aware that Rhino manage a different Javascript context per thread, which can be a problem if you execute scripts in a Swing or JavaFX context[1]
Swing execute all GUI actions in the Awt event queue, and JavaFX execute all GUI actions on the JavaFX Application Thread
[2] .

In that case, you can specify in your JSScriptWrapper that the script must be executed in the Swing or JavaFX Thread by using the JSScriptWrapper.executeInGraphicThread(short) method.

Examples

Basic example

Suppose the following Script interface:
      public interface Script {
        public int execute();
      }
To create the wrapper, you must use a JSScriptWrapper:
      ScriptWrapper<Script> wrapper = new JSScriptWrapper<Script>(){
      };
      Script script = wrapper.getScript();

      File file = new File(<our script file>);
      wrapper.installScript(file);

      int value = script.execute();
You can use for example the following script:
      execute: function() {
        return 10;
      }
Or for example this one using an internal function:
      execute: function() {
        return this.getValue();
      }

      getValue: function() {
        return 10;
      }

Compiler strict behavior

By default Rhino will create a default method with the correct signature if one method of the script interface is not implemented, thus not throwing any exception. For example, for the following interface:
      public interface Script {
        public int execute();
      }
Installing the following script will not raise any exception:
      function toto() {
        return 1;
      }
However, the JSScriptWrapper.setCompilerIsStrict(boolean) method allows the compiler to be stricter about the methods encountered in the script:
  • If this option is set to true, it will raise an exception if the name of a method specified in the interface is not found in the script (if this method does not have a default implementation of course). It will also raise and exception if the number of arguments is different

Optimization level


The Rhino scripting language implementation optimizes by default to the 0 value (which means no optimization).
  • The "default" and "minimum" values will set the optimization level to 0
  • The "maximum" values will set the optimization level to 9
  • All other values will set the the optimization level to their associated number

Notes

  1. ^ Swing execute all GUI actions in the Awt event queue, and JavaFX execute all GUI actions on the JavaFX Application Thread
  2. ^ See Rhino Scopes_and_Contexts

See also


Categories: impls

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