-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathProgram.cs
More file actions
46 lines (41 loc) · 1.87 KB
/
Program.cs
File metadata and controls
46 lines (41 loc) · 1.87 KB
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
// Minimal embedded host that demonstrates how to wire up ChibiRuby.Debugger.Dap from a
// regular C# application. Run with `dotnet run --project sandbox/SampleDebuggerEmbedded`
// and the program will block at the first binding.irb call until a DAP client (e.g.
// VSCode with the chibiruby-debugger extension) attaches to 127.0.0.1:4711.
using System.Net;
using ChibiRuby;
using ChibiRuby.Compiler;
using ChibiRuby.Debugger.Dap;
var mrb = MRubyState.Create();
var compiler = MRubyCompiler.Create(mrb);
// One DapServer per port — owns the listener and serves clients sequentially (one at a
// time, with reconnect). In a real game / app guard this with `#if DEBUG` or a config
// flag so production builds don't hold a TCP port.
//
// `bindAddress: IPAddress.Any` exposes the debugger to the LAN so an attached editor
// on another machine (iPhone on the same Wi-Fi, etc.) can attach to <host-LAN-IP>:4711.
// Drop the parameter (or pass IPAddress.Loopback) to restrict to 127.0.0.1 only.
using var dap = new MRubyDapServer(mrb, compiler, port: 4711, bindAddress: IPAddress.Any);
_ = dap.StartAsync();
var scriptPath = args.Length > 0
? args[0]
: Path.Combine(AppContext.BaseDirectory, "scenarios", "quest.rb");
Console.Error.WriteLine($"running script: {scriptPath}");
var source = File.ReadAllBytes(scriptPath);
try
{
// Compile with the file path so the bytecode's DBG section records the right
// filename; the debugger uses that to surface file:line in the editor.
using var compilation = compiler.Compile(source, filename: scriptPath);
mrb.LoadBytecode(compilation.AsBytecode());
Console.Error.WriteLine("script completed");
}
catch (MRubyRaiseException ex)
{
Console.Error.WriteLine($"unhandled mruby exception: {ex.Message}");
var bt = ex.ExceptionObject.Backtrace;
if (bt is not null)
{
Console.Error.WriteLine(bt.ToString(mrb).TrimEnd());
}
}