Extending PlatformScript

PlatformScript is an extensible scripting language. You can build additional PlatformScript functions with C# that can then be called from an executing script with 0 or more arguments.

This is a quick demonstration on creating a new function.

Creating a class

Each function will need to be within a class that extends CustomFeature. This gives you access to the Variable Evaluate(List<Variable> args = null) method.

class MyNewFunction : CustomFeature
{
    public override Variable Evaluate(List<Variable> args = null)
    {
        // Handle the function execution here
        return new Variable();
    }
}

PlatformScript does not check how many arguments are required or what types they should be. You should handle this validation here and throw an exception or return a Variable here.

The Variable can be created with it's constructor or manually:

Variable varString = new Variable("MyString");
Variable varNumber = new Variable(100.0d);

// Or
Variable varString = new Variable();
varString.Type = VarType.String;
varString.String = "MyString";

Variable varNumber = new Variable(100.d);
varString.Type = VarType.Number;
varString.Value = 100.0d;

The return value should not be NULL.

Register your function

You need to tell the PlatformScript interpreter about your new function before you execute your script.

SandboxScript sandbox = new Sandbox();
sandbox.Interpreter.Functions.Add("myFunction", new MyFunctionClass());
sandbox.ScriptFromFile(scriptFileLocation);
sandbox.Prepare();
Variable result = sandbox.Process();

When you call myFunction from within PlatformScript, the interpreter creates a new instance of MyFunctionClass using the NewInstance() method buried in the extended inheritances.

PlatformScript: Take a look

Head over to our Github Repo to browse the code and grab a copy.

Written by Kieran on 2019-11-29
This website uses Cookies to enhance your experience. Close Manage