Skip to content

Manual Binding

Rohan Singh edited this page Feb 17, 2024 · 1 revision

If you can't or don't want to use the bindings automatically generated by Mond.SourceGenerator then you can manually bind anything you'd like to be used by Mond.

You will need to define a class which implements the IMondLibrary interface. This should output whatever bindings are needed for a MondState and provide which names to expose the built-ins as.

public class MyAwesomeLibrary : IMondLibrary
{
    public IEnumerable<KeyValuePair<string, MondValue>> GetDefinitions(MondState state)
    {
        var hello = MondValue.Function((_, args) =>
        {
            Console.WriteLine("hello world!");
            return MondValue.Undefined;
        });

        yield return new KeyValuePair<string, MondValue>("hello", hello);
    }
}

The above class binds a function and instructs Mond to make it accessible using the hello built-in.

The MyAwesomeLibrary library must be added to a MondState in order for it to be used.

var state = new MondState
{
    Libraries =
    {
        new MyAwesomeLibrary(),
    }
};

This newly constructed MondState instance will have the hello built-in defined so we can write scripts like this:

hello();

Mond libraries can also support configurations to support different scenarios if needed. Some of the standard Mond libraries support a few configurations, such as where printLn should print, how to load modules with require, etc.

state.Libraries.Configure(libraries =>
{
    var myLibrary = libraries.Get<MyAwesomeLibrary>();
    // todo: set properties on it
});