Skip to content

Commit

Permalink
Add binary imports
Browse files Browse the repository at this point in the history
  • Loading branch information
nloum committed Nov 12, 2024
1 parent ad69b1b commit 5bb60f3
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions CodegenBot/Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ public interface ICodegenBotImports
/// </summary>
public class CodegenBotImports : ICodegenBotImports
{
[DllImport("extism", EntryPoint = "cgb_append_to_binary_file")]
public static extern ulong ExternRead(ulong pathOffset, ulong bytesOffset);

public byte[] AppendToBinaryFile(string filePath, byte[] binary)
{
using var block = Pdk.Allocate(filePath);
using var binaryBlock = Pdk.Allocate(binary);
var ptr = ExternRead(block.Offset, binaryBlock.Offset);
var bytes = MemoryBlock.Find(ptr).ReadBytes();
return bytes;
}

[DllImport("extism", EntryPoint = "cgb_read_binary_file")]
public static extern ulong ExternRead(ulong jsonOffset);

public byte[] ReadBinaryFile(ReadBinaryFile binaryFile)
{
var json = JsonSerializer.Serialize(binaryFile, ReadBinaryFileJsonSerializerContext.Default.ReadBinaryFile);
using var block = Pdk.Allocate(json);
var ptr = ExternRead(block.Offset);
var bytes = MemoryBlock.Find(ptr).ReadBytes();
return bytes;
}

[DllImport("extism", EntryPoint = "cgb_graphql")]
public static extern ulong ExternGraphQL(ulong offset);

Expand Down

0 comments on commit 5bb60f3

Please sign in to comment.