Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: place user facing types in the Extism.Sdk namespace #29

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Expand Up @@ -32,8 +31,8 @@
)
|]

use plugin =

Check warning on line 34 in samples/Extism.Sdk.FSharpSample/Program.fs

View workflow job for this annotation

GitHub Actions / Test .NET SDK (ubuntu-latest)

'use' bindings are not permitted in modules and are treated as 'let' bindings

Check warning on line 34 in samples/Extism.Sdk.FSharpSample/Program.fs

View workflow job for this annotation

GitHub Actions / Test .NET SDK (ubuntu-latest)

'use' bindings are not permitted in modules and are treated as 'let' bindings

Check warning on line 34 in samples/Extism.Sdk.FSharpSample/Program.fs

View workflow job for this annotation

GitHub Actions / Test .NET SDK (macos-latest)

'use' bindings are not permitted in modules and are treated as 'let' bindings

Check warning on line 34 in samples/Extism.Sdk.FSharpSample/Program.fs

View workflow job for this annotation

GitHub Actions / Test .NET SDK (windows-latest)

'use' bindings are not permitted in modules and are treated as 'let' bindings
Plugin(manifest, functions, withWasi = true)

Check warning on line 35 in samples/Extism.Sdk.FSharpSample/Program.fs

View workflow job for this annotation

GitHub Actions / Test .NET SDK (ubuntu-latest)

It is recommended that objects supporting the IDisposable interface are created using the syntax 'new Type(args)', rather than 'Type(args)' or 'Type' as a function value representing the constructor, to indicate that resources may be owned by the generated value

Check warning on line 35 in samples/Extism.Sdk.FSharpSample/Program.fs

View workflow job for this annotation

GitHub Actions / Test .NET SDK (ubuntu-latest)

It is recommended that objects supporting the IDisposable interface are created using the syntax 'new Type(args)', rather than 'Type(args)' or 'Type' as a function value representing the constructor, to indicate that resources may be owned by the generated value

Check warning on line 35 in samples/Extism.Sdk.FSharpSample/Program.fs

View workflow job for this annotation

GitHub Actions / Test .NET SDK (macos-latest)

It is recommended that objects supporting the IDisposable interface are created using the syntax 'new Type(args)', rather than 'Type(args)' or 'Type' as a function value representing the constructor, to indicate that resources may be owned by the generated value

Check warning on line 35 in samples/Extism.Sdk.FSharpSample/Program.fs

View workflow job for this annotation

GitHub Actions / Test .NET SDK (windows-latest)

It is recommended that objects supporting the IDisposable interface are created using the syntax 'new Type(args)', rather than 'Type(args)' or 'Type' as a function value representing the constructor, to indicate that resources may be owned by the generated value

printfn "plugin created"

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
Loading