-
Notifications
You must be signed in to change notification settings - Fork 47
/
Tracing script execution.cs
60 lines (50 loc) · 1.54 KB
/
Tracing script execution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExamplesFramework;
using NiL.JS;
using NiL.JS.Core;
namespace Examples._8.Debugger_callback
{
[Level(8)]
class Tracing_script_execution : Example
{
private readonly string _code = @"
var a = 1;
var b = 2;
var c = a + b;
console.log(c);
";
public override void Run()
{
var module = new Module(_code);
module.Context.DebuggerCallback += Context_DebuggerCallback;
module.Context.Debugging = true;
module.Run();
}
private void Context_DebuggerCallback(Context sender, DebuggerCallbackEventArgs e)
{
Console.Clear();
for (var i = 0; i < _code.Length; i++)
{
if (i >= e.Statement.Position && i <= e.Statement.EndPosition)
{
Console.BackgroundColor = ConsoleColor.Yellow;
}
else
{
Console.BackgroundColor = ConsoleColor.Black;
}
Console.Write(_code[i]);
}
Console.WriteLine();
Console.WriteLine("Variables:");
Console.WriteLine(string.Join(Environment.NewLine, new ContextDebuggerProxy(sender).Variables.Select(x => x.Key + ": " + x.Value)));
Console.WriteLine();
Console.WriteLine("Output:");
while (Console.ReadKey().Key != ConsoleKey.Spacebar) ;
}
}
}