RingDotNetfor Ring

How it works

The Ring process hosts the .NET runtime and talks to it through a small managed bridge. This page explains the pieces, so the rest of the documentation makes sense.

The layers#

Your Ring program oSB = new StringBuilder("Hi") · oForm.Text = "Hello" · oBtn.setClickEvent("...") dotnetlib (Ring library) generated wrapper classes · using · dnCallback · gui.ring · wpf.ring · web.ring · data.ring ring_dotnet.dll (C extension) starts the runtime through hostfxr · sends requests · runs Ring callbacks RingDotNet.Bridge.dll (C#): reflection, handle table, type conversion, delegates .NET class library WinForms · WPF · ASP.NET · EF Your DLLs and NuGet packages

The extension and the bridge#

ring_dotnet.dll is a normal Ring extension written in C. When the first .NET call is made it:

  1. finds the installed .NET runtime and starts it inside the Ring process with hostfxr (no separate process, no IPC),
  2. loads RingDotNet.Bridge.dll, a small C# assembly,
  3. keeps one entry point for requests: dotnet_invoke(request). (The extension also has the functions for callbacks and for releasing objects, below.)

The bridge uses reflection. A Ring call such as oSB.Append(", ") becomes a request call [handle, "Append", ", "]; the bridge finds the right overload from the argument types, invokes it, and returns the result.

Handles#

.NET objects never leave .NET. Ring holds a handle (a number) inside a DotNetObject, and the bridge keeps the real object in a table. The generated wrapper classes are DotNetObjects with typed methods, so oSB.Append(...) and oSB.callMethod("Append",[...]) do the same thing.

Releasing objects#

A handle keeps its object alive, so handles must be released. Each wrapper object holds a managed pointer (a Ring pointer with a function that runs when Ring drops the value). When the wrapper is dropped, the pointer queues the handle, and the queued handles are released in groups of 32, so a program that makes thousands of objects does not grow without end. release() releases one handle at once, and dnFlush() releases the queued ones now (the tutorial Memory and the garbage collector).

The wire format#

Values travel as a compact tagged text: s5:Hello (string), n2:42 (number), l... (list), o3:17 (object handle), c... (callback), e... (error). Numbers that are not whole travel as the 8 bytes of the double, so there is no loss of precision; the number becomes the type of the parameter that receives it, and for a parameter of type object a whole number is an Int32 (Core concepts). Binary data (byte[]) travels as a Ring string: a Ring text can hold any byte. A ByteBuffer object (dnBuffer) is the one case where Ring holds a live array, for the methods that fill an array that you pass in.

Generated wrapper classes#

tools/dotnettools/gendotnet.ring reads the manifest (tools/dotnettools/dotnetmanifest.ring) and uses reflection to write one Ring class per .NET class into libraries/dotnetlib/generated/ (two files per namespace: a loader and the classes). These files are plain Ring source with real inheritance, properties (as attributes with getX/setX), events and enums. using { "System.IO" } loads one namespace on demand. A class index (a list of every class and its namespace, loaded the first time it is needed) lets the library import the right namespace automatically when a returned object has a type that is not loaded yet.

Results are wrapped by their real type#

When a method returns an object, the bridge sends the list of its type, its base types and its interfaces (the interface named after the class first, then the ones that inherit the most interfaces: a class that is not public is wrapped as its interface). dnWrapObject looks for the most specific generated class (importing its namespace if needed) and creates it. If no class exists the result is a plain DotNetObject — use callMethod and getProp on it. (A delegate type is a callback and has a class only when extension methods extend it.)

A class that is not public is all of its interfaces at once (ServiceProviderEngineScope is IServiceScope and IServiceProvider: ServiceProvider is a property of the first, GetService a method of the second), and sometimes no interface is named after it (the elements of a fluent API are IElement and IContainer: Text() is a method of the second). The bridge sends its interfaces that are not generic (up to six) as one group, and Ring wraps the object with a class made for that combination (its name starts with DnMix_). It derives from the first class and has the methods of the others merged in, so the object has the members of all of its interfaces. The class is made the first time it is needed and reused.

Callbacks: Ring code called from .NET#

dnCallback("...") passes Ring code where .NET expects a delegate. The bridge builds a delegate of the exact type, bound to a method of the bridge (a compiled expression for a delegate that a method cannot be bound to: ref and out parameters, more than 8 parameters); when .NET invokes it, the C extension runs the Ring function on the Ring thread and returns its result. Callbacks run synchronously during a call, which is why events from timers on other threads are ignored (see Rules and limits).

Threads#

ThreadWhat runs there
The Ring threadAll Ring code, all calls into .NET and all callbacks. With dnUseDesktop() it is an STA thread and runs the message loop in Application.Run().
.NET threadsKestrel connections, tasks, timers. They never call Ring directly. The web server queues requests and Ring handles them in serveOne().

Runtime modes#

The runtime is configured with one of four runtimeconfig.json files: base (RingDotNet.Bridge), desktop (RingDotNet.Desktop), web (RingDotNet.Web) and all (RingDotNet.All). dnUseDesktop(), dnUseWeb() and dnUseAll() select the file before the runtime starts. dnUseData() only loads the Entity Framework assemblies; an AssemblyResolve handler in the bridge finds them next to it.

How a call is made#

A call from Ring to .NET is a request that Ring packs, the extension sends to the bridge, and the bridge runs by reflection.

Packages#

FileRole
ring_dotnet.dllC extension
RingDotNet.Bridge.dllreflection bridge
RingDotNet.Web.dllrequest queue and Kestrel host
RingDotNet.Data.dllRingDb and RingTypedDb (Entity Framework)
Microsoft.EntityFrameworkCore*.dll, e_sqlite3.dll ...Entity Framework Core and SQLite

Where to go next#

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.