-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCompilationResult.cs
More file actions
93 lines (79 loc) · 2.43 KB
/
Copy pathCompilationResult.cs
File metadata and controls
93 lines (79 loc) · 2.43 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
namespace ChibiRuby.Compiler
{
public class CompilationResult : IDisposable
{
public IReadOnlyList<DiagnosticsDescriptor> Diagnostics { get; }
public bool HasError => contextHandle.HasError;
readonly MRubyState mrb;
readonly MrbStateHandle stateHandle;
readonly MrcCContextHandle contextHandle;
readonly IntPtr bytecodeDataPtr;
readonly int bytecodeLength;
bool disposed;
internal CompilationResult(
MRubyState mrb,
MrbStateHandle stateHandle,
MrcCContextHandle contextHandle,
IntPtr bytecodeDataPtr,
int bytecodeLength)
{
this.mrb = mrb;
this.stateHandle = stateHandle;
this.contextHandle = contextHandle;
this.bytecodeDataPtr = bytecodeDataPtr;
this.bytecodeLength = bytecodeLength;
Diagnostics = contextHandle.GetDiagnostics();
}
internal CompilationResult(
MRubyState mrb,
MrbStateHandle stateHandle,
MrcCContextHandle contextHandle)
{
this.mrb = mrb;
this.stateHandle = stateHandle;
this.contextHandle = contextHandle;
Diagnostics = contextHandle.GetDiagnostics();
}
~CompilationResult()
{
Dispose(disposing: false);
}
public unsafe ReadOnlySpan<byte> AsBytecode()
{
if (HasError || bytecodeDataPtr == IntPtr.Zero || bytecodeLength <= 0)
{
throw new MRubyCompileException(FormatDiagnostics());
}
return new ReadOnlySpan<byte>((byte*)bytecodeDataPtr, bytecodeLength);
}
string FormatDiagnostics()
{
return Diagnostics.Count > 0
? string.Join(Environment.NewLine, Diagnostics)
: "Ruby compilation failed (no diagnostics available).";
}
public ReadOnlySpan<byte> AsSpan() => AsBytecode();
public Irep ToIrep()
{
return mrb.RiteParser.Parse(AsBytecode());
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this); // ファイナライザの呼び出しを抑制
}
unsafe void Dispose(bool disposing)
{
if (disposed) return;
if (bytecodeDataPtr != IntPtr.Zero)
{
// NativeMethods.MrcFree(stateHandle.DangerousGetPtr(), bytecodeDataPtr.ToPointer());
NativeMethods.MrcFree(bytecodeDataPtr.ToPointer());
}
contextHandle.Dispose();
disposed = true;
}
}
}