MoonSharp

repository·master·Indexed 23 days ago

https://github.com/moonsharp-devs/moonsharp

A complete Lua 5.2 compatible interpreter written entirely in C# for .NET, Mono, Xamarin, and Unity3D. It features high-performance interop with CLR objects, AOT compatibility for iOS and Android, and support for coroutines and async methods. The library includes a VS Code debugger (moonsharp-debug), a REPL/CLI environment with a command system for script execution and type registration, and tools for bytecode compilation and hardwiring Lua dump tables into C# or VB code.

Tokens
3.1K
Snippets
10
Records
25
Agent score
82%

What's inside MoonSharp

  1. Overview of MoonSharp features

    master

    MoonSharp is a complete Lua solution written in C# for .NET, Mono, Xamarin, and Unity3D. It is approximately 99% compatible with Lua 5.2 (excluding weak tables).

    Key capabilities include:

    • Platform Support: Runs on .NET 4.5, .NET (Core), Mono, Xamarin, Unity, iOS (AOT), and IL2CPP.
    • Interop: High-performance interop with CLR objects, supporting methods, extension methods, overloads, fields, properties, and indexers.
    • Lua Features: Supports metalua-style anonymous functions, the complete Lua standard library (with minor exceptions in the debug and string modules), coroutines (invokable as C# iterators), and async method support.
    • Tooling: Debugger support via Debug Adapter Protocol (e.g., VSCode), an embedded JSON parser for Lua table conversion, and a REPL interpreter.
    • Security: Easy sandboxing by opting out of specific Lua standard library modules.
    • Performance: Supports bytecode dumping/loading for obfuscation and faster runtime parsing.
  2. Use the MoonSharp VSCode Debugger

    master

    The MoonSharp VSCode Debugger extension allows you to debug MoonSharp scripts running inside other applications.

    Requirements:

    • The host application must be embedding MoonSharp version 1.8.0.0 or later.
    • The host application must expose the VSCode debugger extensions.

    Supported Features:

    • Connecting to a single script object embedded in an application.
    • Breakpoints, watches, and local variable inspection.
    • self inspection.
    • Call stack visualization, including the current coroutine.
    • Watches supporting free-formed, side-effect-free expressions.
    • Inspection of values, including internal IDs and table contents.

    Limitations:

    • You cannot debug multiple script objects from the same VSCode instance.
    • You cannot edit values during a debug session.
    • There are no checks for file content changes.
    • Token-based breakpoints are not supported due to VSCode architecture.
  3. Configure launch.json for MoonSharp Attach

    master

    To use the debugger, you must create a launch.json file in your VSCode project. You must specify the debugServer port immediately after the version field. This port must match the port the host application is using to expose the debugger.

    {
        "version": "0.2.0",
        "debugServer" : 41912,
        "configurations": [
            {
                "name": "MoonSharp Attach",
                "type": "moonsharp-debug",
                "request": "attach",
                "HELP": "Please set 'debugServer':41912 (or whatever port you ar connecting to) right after the 'version' field in this json."
            }
        ]
    }
  4. Install MoonSharp in Unity via UPM

    master

    You can install MoonSharp directly into your Unity project by adding it to your Packages/manifest.json file.

    Install specific version

    Add the following entry to your dependencies object: "org.moonsharp.moonsharp": "https://github.com/moonsharp-devs/moonsharp.git?path=/interpreter#upm/v3.0"

    Pin to a major version

    To pin to a major version (e.g., version 3) instead of a specific minor version, use the branch upm/v3.

    Install the VSCode Debugger

    The VSCode debugger is a separate package. Add it to manifest.json using: "org.moonsharp.debugger.vscode": "https://github.com/moonsharp-devs/moonsharp.git?path=/debugger/vscode#upm/v3.0"

    Beta versions

    Beta branches follow the naming convention upm/beta/v3.0.

  5. Build the MoonSharp Unity Package (UPM) locally

    master

    To create a local tarball of the MoonSharp Unity Package, run the following commands from the repository root:

    1. Stage the local package using the provided script.
    2. Navigate to the staged directory.
    3. Use npm pack to generate the .tgz file.

    Note: Replace 3.0.0-local with your desired version string.

    tools/upm/stage-local-package.sh 3.0.0-local
    cd .upm-staging/org.moonsharp.moonsharp
    npm pack
  6. Run Lua scripts using Script.RunString

    master

    To execute Lua code within a C# application, use the Script.RunString method. The result is returned as a DynValue, which can then be converted to native C# types (such as double, int, or string).

    double MoonSharpFactorial()
    {
    	string script = @"    
    		-- defines a factorial function
    		function fact (n)
    			if (n == 0) then
    				return 1
    			else
    				return n*fact(n - 1)
    			end
    		end
    
    	return fact(5)";
    
    	DynValue res = Script.RunString(script);
    	return res.Number;
    }
  7. Manage REPL/CLI commands with CommandManager

    master

    The CommandManager class is responsible for managing the lifecycle and execution of commands within the MoonSharp REPL or CLI environment. It uses an internal registry of objects implementing the ICommand interface.

    To use the command system, you must first call Initialize() to scan the executing assembly and register all available non-abstract classes that implement ICommand. Once initialized, you can execute command strings via Execute or retrieve available commands using GetCommands or Find.