LiteScript.Net 0.1.0

dotnet add package LiteScript.Net --version 0.1.0
                    
NuGet\Install-Package LiteScript.Net -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LiteScript.Net" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LiteScript.Net" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="LiteScript.Net" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LiteScript.Net --version 0.1.0
                    
#r "nuget: LiteScript.Net, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LiteScript.Net@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LiteScript.Net&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=LiteScript.Net&version=0.1.0
                    
Install as a Cake Tool

LiteScript.Net

.NET wrapper of LiteScript, a little script engine to embed in applications: an object-oriented language similar to Javascript, an assembly language, a standard library, and states that can be saved and loaded in the middle of an execution.

The package contains the native engine for Windows (x64, arm64), Linux (x64, arm64) and macOS (x64, arm64), and targets .NET Standard 2.0 and .NET 8.

dotnet add package LiteScript.Net

Usage

using LiteScript;

using var memory = new Memory();
using var state = new State(memory);

// A function of the application
state.DeclareVariable("print", memory.RegisterFunction("print", (s, args) =>
{
    Console.WriteLine(string.Join(", ", args.Select(Operations.ToText)));
    return null;    // undefined
}));
Standard.Install(state);

Script.Result result = Script.Execute(state, @"
    function fibo(x) { return x < 2 ? 1 : fibo(x - 1) + fibo(x - 2); }
    print(fibo(10));
    return Math.sqrt(16);
");
if (!result.Success)
    Console.WriteLine($"{result.Line + 1}:{result.Column + 1} {result.GetMessage()}");
Console.WriteLine(result.Value);     // 4

// Calling a function of the script
Variable fibo = state.GetVariable("fibo")!;
Variable value = state.Call(fibo, memory.CreateNumber(5));
long n = value.AsNumber().ToInteger();     // 8

The API follows the C++ API, the names are the same except for a few adaptations:

C++ .NET
Memory, State, Script, Assembly, Standard Same names
Variable (empty variable) Variable (null)
Type ScriptType
v.Is<Number>(), v.As<Number>() v.Is(ScriptType.Number), v.AsNumber() (also AsBoolean, AsString, AsArray, AsClass, AsClassObject, AsNamespace, AsCallback, AsCharacter)
Array::indexed, Array::named ArrayValue.Indexed (IList<Variable?>), ArrayValue.Named (MemberList)
Copy, Clone, Operate, GetMember, IsTruthy... Operations.Copy, Operations.Clone...
ScriptError ScriptException
SerializationError LiteScript.SerializationException
SaveInstructions, LoadInstructions Serialization.SaveInstructions, Serialization.LoadInstructions
Object::ReleaseCachedBlocks Memory.ReleaseCachedBlocks
state.Save(stream), state.Load(stream) state.Save() / state.Save(stream), state.Load(bytes) / state.Load(stream)

Application functions

Variable Sum(State state, IReadOnlyList<Variable?> args)
{
    Number total = 0;
    foreach (Variable? v in args)
    {
        if (v != null && v.Is(ScriptType.Number))
            total += v.AsNumber();
    }
    return state.Memory.CreateNumber(total);
}

state.DeclareVariable("sum", memory.RegisterFunction("sum", Sum));
  • The arguments are only valid during the call: use Duplicate() to keep one.
  • An exception thrown by the function becomes a script error that the script can catch with try ... catch. Throw a ScriptException to raise an error with a value: throw new ScriptException(state.Memory.CreateString("invalid")).
  • RegisterFunction gives a name to the function, which makes it possible to save the states that use it. CreateCallback creates an unnamed function.

Errors

An error that the script doesn't catch is thrown as a ScriptException by Execute, ExecuteSingle, Call, Script.Execute...:

try
{
    state.Execute();
}
catch (ScriptException error)
{
    Console.WriteLine($"Error at line {error.Position.Line + 1}: {error.Message}");
    Variable? thrown = error.Value;
}

The exceptions thrown by the debug and error hooks (SetDebugHook, SetErrorHook) reach the caller unchanged.

Lifetime of the objects

  • Memory, State and Variable are IDisposable. Disposing a Variable releases its reference to the object immediately; a variable that isn't disposed is released after the .NET garbage collection (by the next operation of its memory).
  • Disposing a Memory disposes its states. The variables that still exist remain valid but must not be used with a state.
  • The functions of the application are kept alive by their memory: a function that references its memory (or a state) keeps it alive until it's disposed.
  • A memory and its states must be used from one thread at a time.

Building from the sources

The sources are in the dotnet folder of the repository: the C interface of the engine (native), the .NET library (src) and its tests (tests).

# Build the native library for the current platform (Meson, Ninja and a C++20 compiler)
dotnet/build-native.ps1       # Windows
dotnet/build-native.sh        # Linux and macOS

# Build and test the .NET library (.NET SDK 9.0.200 or newer)
dotnet test dotnet/LiteScript.Net.slnx

# Create the package (with the native libraries found in dotnet/artifacts/runtimes)
dotnet pack dotnet/src/LiteScript.Net -c Release -o dotnet/artifacts/packages

The native library is the shared library litescript_net, built by Meson with the option -Ddotnet=true. The scripts copy it in dotnet/artifacts/runtimes/<rid>/native.

The workflow .github/workflows/dotnet.yml builds and tests the native library on every platform, then creates the package. It publishes the package on NuGet for the tags v* (the version is the one of the tag, v2.1.0 gives 2.1.0) with the secret NUGET_API_KEY, or when it's started manually with the option "publish".

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 132 10/9/2026