pjc0247 / SlowSharp

C# interpreter written in C#

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SlowSharp Build Status

Slow version of C# interpreter because modern JITs are too fast.
Web Playground

Pros

  • More Compatability: Brings C# scripting into WSA, iOS and WebAssembly.
  • And nothing

Zero-binding

  • No manual assembly references
  • No type or method binding

Even this can be possible:

CScript.Run(@"
   CScript.Run("" Console.WriteLine(""hello from inception""); "");
");

Hot Reloading

Can replace methods after parsing. This also affects already instantiated objects.

var r = CScript.CreateRunner(@"
class Foo { public int GiveMeNumber() => 10; }
");

var foo = r.Instantiate("Foo");
// should be 10
foo.Invoke("GiveMeNumber");
ss.UpdateMethodsOnly(@"
class Foo { public int GiveMeNumber() => 20; }
");
// should be 20
foo.Invoke("GiveMeNumber");

Overriding (Virtual inheritance)

Supports virtual inheritance, this is useful with Unity.

class MyBehaviour : MonoBehaviour { 
   public void SayHello() { /* ... */ }
}
// Error, MonoBehaviour cannot be instantiated by Unity's law
ss.Instantiate("MyBehaviour");

// Use this when you're not able to create an object yourself.
ss.Override("MyBehaviour", gameObject);

Finally, there will be two instances, but act as one derivered object.

// You can invoke methods from `MyBehaviour` which are defined in script-side.
ss.Invoke("SayHello");

// You can also invoke methods from `MonoBehaviour`
ss.Invoke("StopAllCoroutines");

Sandboxing

Access control

Prevents malicious function call such as Threading and File.

var ac = new AccessControl();
ac.AddBlockedType("System.Threading.Thread");
ac.AddBlockedNamespace("System.IO");

new RunConfig() {
  accessControl = ac
};

Timeout

Prevents long-running tasks with a timeout option.

new RunConfig() {
  timeout = 1000 /* ms */
};

Limitations

Since SlowSharp is an interpreter, there're some differences and limitations.

Lazy semantic validation
SlowSharp does not validate whole syntaxs during the parse time. It only checks the overall form is valid or not.
As a result, some mistakes won't be detected until it's fully reached and executed.

Console.WriteLine("Hello");

// There's not method `WriteLn` in `Conosle` class.
// However, this will not throw an exception util SlowSharp actually 
// evaluates the below line.
Console.WriteLn("World");
var a = 1234;
Console.WriteLine(a);
var a = 4556; // redefination

About

C# interpreter written in C#

License:MIT License


Languages

Language:C# 100.0%