Skip to content

Commit abb6fcc

Browse files
committed
c abi stuff
1 parent f51285c commit abb6fcc

3 files changed

Lines changed: 67 additions & 27 deletions

File tree

RSML.Native/MetadataExports.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,12 @@ namespace OceanApocalypseStudios.RSML.Native
1111
public static unsafe class MetadataExports
1212
{
1313

14+
private const string ApiVersion = "2.0.0";
1415
private const byte ApiAuthorNameLen = 16;
1516

16-
private static readonly byte[] authorName =
17-
[
18-
0x4F, 0x63, 0x65, 0x61, 0x6E, 0x41,
19-
0x70, 0x6F, 0x63, 0x61, 0x6C, 0x79,
20-
0x70, 0x73, 0x65, 0x53, 0x74, 0x75,
21-
0x64, 0x69, 0x6F, 0x73
22-
]; // OceanApocalypseStudios
23-
24-
private static readonly byte[] docsLink = Encoding.UTF8.GetBytes($"https://oceanapocalypsestudios.org/rsml-docs/{"change me"}/"); // todo
25-
26-
private static readonly byte[] utf8ApiVersion = Encoding.UTF8.GetBytes("change me"); // todo
17+
private static readonly byte[] authorName = "OceanApocalypseStudios"u8.ToArray();
18+
private static readonly byte[] docsLink = Encoding.UTF8.GetBytes($"https://oceanapocalypsestudios.org/rsml-docs/{ApiVersion}/");
19+
private static readonly byte[] utf8ApiVersion = Encoding.UTF8.GetBytes(ApiVersion);
2720

2821
/// <summary>
2922
/// Writes the name of the creator (and lead maintainer) of RSML's API to a supplied buffer.

RSML.Native/ParserExports.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

RSML.Native/ToolchainExports.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using System.Text;
4+
5+
using OceanApocalypseStudios.RSML.Evaluation;
6+
7+
8+
namespace OceanApocalypseStudios.RSML.Native
9+
{
10+
11+
/// <summary>
12+
/// C ABI exports for RSML toolchain components.
13+
/// </summary>
14+
public static unsafe class ToolchainExports
15+
{
16+
17+
/// <summary>
18+
/// Evaluates a RSML document from the local machine.
19+
/// </summary>
20+
/// <param name="document">The document to evaluate</param>
21+
/// <param name="documentLength">The length of the document</param>
22+
/// <param name="resultBuffer">The buffer in which to store the evaluation result</param>
23+
/// <param name="resultBufferLength">The length of the result buffer</param>
24+
/// <returns>
25+
/// <c>-1:</c> An error occured while evaluating the document OR the result buffer is too small to hold the result<br />
26+
/// <c>0:</c> No matches were found (result buffer unassigned)<br />
27+
/// <c>1:</c> A match was found (result buffer assigned)<br />
28+
/// </returns>
29+
[UnmanagedCallersOnly(EntryPoint = "rsml_evaluate_document")]
30+
public static int EvaluateRsmlDocument(byte* document, int documentLength, byte* resultBuffer, int resultBufferLength)
31+
{
32+
33+
ReadOnlySpan<byte> span = new(document, documentLength);
34+
Evaluator evaluator = new(span);
35+
EvaluationResult result;
36+
37+
try
38+
{
39+
result = evaluator.Evaluate();
40+
}
41+
catch (Exception)
42+
{
43+
return -1;
44+
}
45+
46+
if (!result.WasMatchFound)
47+
return 0;
48+
49+
if (resultBufferLength > result.MatchValue!.Length)
50+
return -1;
51+
52+
var asBytes = Encoding.UTF8.GetBytes(result.MatchValue!);
53+
54+
for (int i = 0; i < asBytes.Length; i++)
55+
resultBuffer[i] = asBytes[i];
56+
57+
return 1;
58+
59+
}
60+
61+
}
62+
63+
}

0 commit comments

Comments
 (0)