Created
January 7, 2018 16:05
-
-
Save jochemstoel/99784b307d275be41316092b55bf112b to your computer and use it in GitHub Desktop.
REPL snippet
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 System; | |
using Microsoft.ClearScript.V8; | |
namespace v8repl | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
/* create instance of V8 */ | |
V8ScriptEngine v8 = new V8ScriptEngine(); | |
/* assign System.Console to Javascript variable myConsole */ | |
v8.AddHostType("myConsole", typeof(Console)); | |
/* */ | |
bool kill = false; | |
/* keep doing the following while kill = false */ | |
while(!kill) | |
{ | |
/* get input string from process stdin */ | |
string input = Console.ReadLine(); | |
/* using a string literal for simplicity sake */ | |
if(input == "exit") | |
{ | |
Environment.Exit(0); /* exit code 0 means no error */ | |
} | |
/* safely evaluate input in a try/catch block */ | |
try | |
{ | |
v8.Evaluate(input); /* run the code */ | |
} catch (Exception e) | |
{ | |
/* something went wrong, show us the exception */ | |
Console.WriteLine(e.Message); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment