Created
January 29, 2017 01:21
-
-
Save anonymous/9db13de8346116e63329713645a04612 to your computer and use it in GitHub Desktop.
Example of C# host application for running Intuicio 4 sandboxed scripts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using I4.NET; | |
using System; | |
using System.Collections.Generic; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
using TestHost.Properties; | |
namespace TestHost | |
{ | |
class Program | |
{ | |
public class MemoryScriptLoader : Sandbox.IScriptLoader | |
{ | |
public class Scripts : Dictionary<string, string> { } | |
public Scripts ScriptsCollection { get { return m_scripts; } } | |
private Scripts m_scripts = new Scripts(); | |
public string OnLoadScript(string path) | |
{ | |
return m_scripts.ContainsKey(path) ? m_scripts[path] : ""; | |
} | |
} | |
public class TestSignalHandler : Sandbox.ISignalHandler | |
{ | |
public bool OnSignal(IntPtr runner, IntPtr context, uint pointerSize) | |
{ | |
var mode = Sandbox.PopInt(context); | |
if (mode < 3) | |
return Sandbox.DefaultSignalHandler.ProcessSignal(mode, runner, context, pointerSize); | |
else if (mode == 3) { | |
var pointer = Sandbox.PopPointer(context, runner, pointerSize); | |
var size = Sandbox.PopInt(context); | |
if (pointer != IntPtr.Zero && size > 0) { | |
Console.Write(Marshal.PtrToStringAnsi(pointer, size)); | |
} | |
return false; | |
} else if (mode == 4) { | |
var value = Sandbox.PopInt(context); | |
Console.Write(value); | |
return false; | |
} else { | |
Console.Out.WriteLine("Unknown signal occured: " + mode); | |
return false; | |
} | |
} | |
} | |
static void Main(string[] args) | |
{ | |
var memoryScriptLoader = new MemoryScriptLoader(); | |
memoryScriptLoader.ScriptsCollection.Add( | |
"main.i4s", | |
Encoding.UTF8.GetString(Resources.main) | |
); | |
var testSignalHandler = new TestSignalHandler(); | |
var sandbox = new Sandbox( | |
1 << 20, // available memory size for program (1mb) | |
8, // memory chunk size (in bytes). | |
memoryScriptLoader, // sandboxed script loading handler. | |
testSignalHandler // sandboxed signal handler. | |
); | |
sandbox.RunSync("main.i4s"); | |
sandbox.Dispose(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#intuicio 4.0; | |
#stack 8k; | |
#pointersize 32; | |
#entry @Main; | |
routine Main():i32 | |
<result:i32> | |
{ | |
add void 40:i32 2:i32 => $result; | |
push void 9:i32; // string length. | |
push void "40 + 2 = ":*i8; | |
push void 3:i32; // print string. | |
sig void; | |
push void $result; // value. | |
push void 4:i32; // print i32. | |
sig void; | |
ret $result; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./TestHost.exe | |
40 + 2 = 42 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment