Skip to content

Commit

Permalink
refactor: place user facing types in the Extism.Sdk namespace (#29)
Browse files Browse the repository at this point in the history
- Only the types defined by the native runtime should be in the
Extism.Sdk.Native namespace
- Use file level namespace (this has caused a bit of a mess in the
GitHub diff viewer)
  • Loading branch information
mhmd-azeez authored Nov 16, 2023
1 parent 6ffb653 commit 5390eb0
Show file tree
Hide file tree
Showing 8 changed files with 478 additions and 486 deletions.
1 change: 0 additions & 1 deletion samples/Extism.Sdk.FSharpSample/Program.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
open Extism.Sdk
open Extism.Sdk.Native
open System
open System.Text
open System.Collections.Generic
Expand Down
260 changes: 129 additions & 131 deletions src/Extism.Sdk/CurrentPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,156 +1,154 @@
using Extism.Sdk.Native;
using System.Text;
using Extism.Sdk.Native;

using System.Text;
namespace Extism.Sdk;

namespace Extism.Sdk
/// <summary>
/// Represents the current plugin. Can only be used within <see cref="HostFunction"/>s.
/// </summary>
public class CurrentPlugin
{
internal CurrentPlugin(long nativeHandle, nint userData)
{
NativeHandle = nativeHandle;
UserData = userData;
}

internal long NativeHandle { get; }

/// <summary>
/// Represents the current plugin. Can only be used within <see cref="HostFunction"/>s.
/// An opaque pointer to an object from the host, passed in when a <see cref="HostFunction"/> is registered.
/// </summary>
public class CurrentPlugin
{
internal CurrentPlugin(long nativeHandle, nint userData)
{
NativeHandle = nativeHandle;
UserData = userData;
}
public nint UserData { get; set; }

internal long NativeHandle { get; }
/// <summary>
/// Returns a offset to the memory of the currently running plugin.
/// NOTE: this should only be called from host functions.
/// </summary>
/// <returns></returns>
public long GetMemory()
{
return LibExtism.extism_current_plugin_memory(NativeHandle);
}

/// <summary>
/// An opaque pointer to an object from the host, passed in when a <see cref="HostFunction"/> is registered.
/// </summary>
public nint UserData { get; set; }
/// <summary>
/// Reads a string from a memory block using UTF8.
/// </summary>
/// <param name="offset"></param>
/// <returns></returns>
public string ReadString(long offset)
{
return ReadString(offset, Encoding.UTF8);
}

/// <summary>
/// Returns a offset to the memory of the currently running plugin.
/// NOTE: this should only be called from host functions.
/// </summary>
/// <returns></returns>
public long GetMemory()
{
return LibExtism.extism_current_plugin_memory(NativeHandle);
}
/// <summary>
/// Reads a string form a memory block.
/// </summary>
/// <param name="offset"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public string ReadString(long offset, Encoding encoding)
{
var buffer = ReadBytes(offset);

/// <summary>
/// Reads a string from a memory block using UTF8.
/// </summary>
/// <param name="offset"></param>
/// <returns></returns>
public string ReadString(long offset)
{
return ReadString(offset, Encoding.UTF8);
}
return encoding.GetString(buffer);
}

/// <summary>
/// Reads a string form a memory block.
/// </summary>
/// <param name="offset"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public string ReadString(long offset, Encoding encoding)
{
var buffer = ReadBytes(offset);
/// <summary>
/// Returns a span of bytes for a given block.
/// </summary>
/// <param name="offset"></param>
/// <returns></returns>
public unsafe Span<byte> ReadBytes(long offset)
{
var mem = GetMemory();
var length = (int)BlockLength(offset);
var ptr = (byte*)mem + offset;

return encoding.GetString(buffer);
}
return new Span<byte>(ptr, length);
}

/// <summary>
/// Returns a span of bytes for a given block.
/// </summary>
/// <param name="offset"></param>
/// <returns></returns>
public unsafe Span<byte> ReadBytes(long offset)
{
var mem = GetMemory();
var length = (int)BlockLength(offset);
var ptr = (byte*)mem + offset;
/// <summary>
/// Writes a string into the current plugin memory using UTF-8 encoding and returns the offset of the block.
/// </summary>
/// <param name="value"></param>
public long WriteString(string value)
=> WriteString(value, Encoding.UTF8);

return new Span<byte>(ptr, length);
}
/// <summary>
/// Writes a string into the current plugin memory and returns the offset of the block.
/// </summary>
/// <param name="value"></param>
/// <param name="encoding"></param>
public long WriteString(string value, Encoding encoding)
{
var bytes = encoding.GetBytes(value);
var offset = AllocateBlock(bytes.Length);
WriteBytes(offset, bytes);

/// <summary>
/// Writes a string into the current plugin memory using UTF-8 encoding and returns the offset of the block.
/// </summary>
/// <param name="value"></param>
public long WriteString(string value)
=> WriteString(value, Encoding.UTF8);

/// <summary>
/// Writes a string into the current plugin memory and returns the offset of the block.
/// </summary>
/// <param name="value"></param>
/// <param name="encoding"></param>
public long WriteString(string value, Encoding encoding)
{
var bytes = encoding.GetBytes(value);
var offset = AllocateBlock(bytes.Length);
WriteBytes(offset, bytes);
return offset;
}

return offset;
}
/// <summary>
/// Writes a byte array into a newly allocated block of memory.
/// </summary>
/// <param name="bytes"></param>
/// <returns>Returns the offset of the allocated block</returns>
public long WriteBytes(Span<byte> bytes)
{
var offset = AllocateBlock(bytes.Length);
WriteBytes(offset, bytes);
return offset;
}

/// <summary>
/// Writes a byte array into a newly allocated block of memory.
/// </summary>
/// <param name="bytes"></param>
/// <returns>Returns the offset of the allocated block</returns>
public long WriteBytes(Span<byte> bytes)
/// <summary>
/// Writes a byte array into a block of memory.
/// </summary>
/// <param name="offset"></param>
/// <param name="bytes"></param>
public unsafe void WriteBytes(long offset, Span<byte> bytes)
{
var length = BlockLength(offset);
if (length < bytes.Length)
{
var offset = AllocateBlock(bytes.Length);
WriteBytes(offset, bytes);
return offset;
throw new InvalidOperationException("Destination block length is less than source block length.");
}

/// <summary>
/// Writes a byte array into a block of memory.
/// </summary>
/// <param name="offset"></param>
/// <param name="bytes"></param>
public unsafe void WriteBytes(long offset, Span<byte> bytes)
{
var length = BlockLength(offset);
if (length < bytes.Length)
{
throw new InvalidOperationException("Destination block length is less than source block length.");
}

var mem = GetMemory();
var ptr = (void*)(mem + offset);
var destination = new Span<byte>(ptr, bytes.Length);
var mem = GetMemory();
var ptr = (void*)(mem + offset);
var destination = new Span<byte>(ptr, bytes.Length);

bytes.CopyTo(destination);
}
bytes.CopyTo(destination);
}

/// <summary>
/// Frees a block of memory belonging to the current plugin.
/// </summary>
/// <param name="offset"></param>
public void FreeBlock(long offset)
{
LibExtism.extism_current_plugin_memory_free(NativeHandle, offset);
}
/// <summary>
/// Frees a block of memory belonging to the current plugin.
/// </summary>
/// <param name="offset"></param>
public void FreeBlock(long offset)
{
LibExtism.extism_current_plugin_memory_free(NativeHandle, offset);
}

/// <summary>
/// Allocate a memory block in the currently running plugin.
///
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public long AllocateBlock(long length)
{
return LibExtism.extism_current_plugin_memory_alloc(NativeHandle, length);
}
/// <summary>
/// Allocate a memory block in the currently running plugin.
///
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public long AllocateBlock(long length)
{
return LibExtism.extism_current_plugin_memory_alloc(NativeHandle, length);
}

/// <summary>
/// Get the length of an allocated block.
/// NOTE: this should only be called from host functions.
/// </summary>
/// <param name="offset"></param>
/// <returns></returns>
public long BlockLength(long offset)
{
return LibExtism.extism_current_plugin_memory_length(NativeHandle, offset);
}
/// <summary>
/// Get the length of an allocated block.
/// NOTE: this should only be called from host functions.
/// </summary>
/// <param name="offset"></param>
/// <returns></returns>
public long BlockLength(long offset)
{
return LibExtism.extism_current_plugin_memory_length(NativeHandle, offset);
}
}
2 changes: 1 addition & 1 deletion src/Extism.Sdk/ExtismException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Extism.Sdk.Native;
namespace Extism.Sdk;

using System;

Expand Down
Loading

0 comments on commit 5390eb0

Please sign in to comment.