Skip to content

Examples

Manuel Zarat edited this page Dec 11, 2025 · 2 revisions

Writing a host application requires to implement the Host interface and the method Invoke with this signature.

public object Invoke(string functionName, List<object> parameters)

Here is a full example.

using ScriptStack;
using ScriptStack.Compiler;
using ScriptStack.Runtime;

class ScriptStack : Host
{
 
    private Manager manager;
    private Script script;
    private Interpreter interpreter;
 
    public ScriptStack(String[] args)
    {
    
        manager = new Manager();

        manager.LoadComponents(".\Plugins");
 
        manager.Register(new Routine((Type)null, "print", (Type)null));
 
        script = new Script(manager, args[0]);
 
        interpreter = new Interpreter(script);
                      
        interpreter.Handler = this;
 
        while (!interpreter.Finished)
            interpreter.Interpret(1);
 
    }
 
    public object Invoke(string functionName, List<object> parameters)
    {
 
        if (functionName == "print")
        {
            // do some stuff..
        }
 
        return null;
 
    }
 
}

Writing a model (plugin) requires to implement the Model interface and the method Invoke with this signature.

public object Invoke(string functionName, List<object> parameters)

In addition it must expose a List<Routine> of all the (script) functions it implements.

public ReadOnlyCollection<Routine> Routines

Here is a full example.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
 
using ScriptStack;
using ScriptStack.Compiler;
using ScriptStack.Runtime;
 
namespace DemoModel
{
 
    public class DemoModel : Model
    {
 
        private static ReadOnlyCollection<Routine> prototypes;
 
        public ReadOnlyCollection<Routine> Routines
        {
            get { return prototypes; }
        }
 
        public DemoModel()
        {
 
            if (prototypes != null)
                return;
 
            List<Routine> routines = new List<Routine>();
 
            routines.Add(new Routine((Type)null, "toUpper", (Type)null, "Make everything upper case"));
            routines.Add(new Routine((Type)null, "toLower", (Type)null, "Make everything lower case"));
 
            prototypes = routines.AsReadOnly();
 
        }
 
        public object Invoke(String functionName, List<object> parameters)
        {
 
            if (functionName == "toUpper")
            {
 
                return parameters[0].ToString().ToUpper();
 
            }
 
            if (functionName == "toLower")
            {
 
                return parameters[0].ToString().ToLower();
 
            }
 
            return null;
 
        }
 
    }
 
}

Clone this wiki locally