EScript - writing your own module

So you want to write your own scripting module? This is far easier than you might imagine, lets go!

Prerequisites:

Download the EScript sources from github. Best clone the repository, it will come with a working script module example project.

If you need to use https for fetching you might get an error when creating the repository location. I had to disable SSL verification in eclipse to successfully connect to github. In case you experience the same problems go to Preferences / Team / Git / Configuration and add a new entry: http.sslVerify = false

Creating the module:

Create a new Plug-in project. Navigate to the Extensions tab of your plugin.xml and add an extension com.codeandme.scripting.language. This will also add a plugin dependency to com.codeandme.scripting.

Right click on the new extension and add a New -> module. Provide a nice, unique name (which will be the name the module is addressed later on) and an implementing class. Let this class derive from com.codeandme.scripting.modules.AbstractScriptModule (if you won't, you will have to create all the nice wrapper functions on your own!)

Your plugin.xml should contain following section:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="com.codeandme.scripting.language">
      <module
            class="com.codeandme.modules.math.MathModule"
            documentation="true"
            name="Math"
            visible="true">
      </module>
   </extension>
</plugin>

Now start implementing your java methods. To make a method available in the script interpreter make sure it is public and uses the @WrapToScript annotation.

An example implementation of com.codeandme.modules.math.MathModule:
package com.codeandme.modules.math;

import com.codeandme.scripting.modules.AbstractScriptModule;
import com.codeandme.scripting.modules.IScriptModule;
import com.codeandme.scripting.modules.WrapToScript;

public class MathModule extends AbstractScriptModule implements IScriptModule {

    public MathModule() {
    }

    // visible in script interpreter (wrapper generated)
    @WrapToScript
    public int sum(final int a, final int b) {
        return a + b;
    }

    // invisible in script interpreter (no wrapper generated, still usable)
    public int diff(final int a, final int b) {
        return a - b;
    }
}

Using the module:

To use your module you have to load it in your interpreter first, afterwards all wrapped methods are directly usable:
loadModule("Math");

sum(12, 3);
// will return 15

diff(5, 1)
// will throw an exception as diff is not exported

2 comments:

  1. "http.sslVerify false" might be correct, IIRC.

    ReplyDelete
    Replies
    1. You are right, of course. Thanks for pointing this out!

      Delete