Skip to content

Commit

Permalink
feat: add DaedalusInstance UserData
Browse files Browse the repository at this point in the history
  • Loading branch information
lmichaelis committed Oct 6, 2024
1 parent d26fd0c commit ec06506
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ZenKit/DaedalusInstance.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.InteropServices;
using ZenKit.Daedalus;

namespace ZenKit
Expand Down Expand Up @@ -42,6 +43,21 @@ protected DaedalusInstance(UIntPtr handle)
public DaedalusInstanceType Type => Native.ZkDaedalusInstance_getType(Handle);
public int Index => (int)Native.ZkDaedalusInstance_getIndex(Handle);

public object? UserData
{
get
{
var userPtr = Native.ZkDaedalusInstance_getUserPointer(Handle);
return userPtr == IntPtr.Zero ? null : GCHandle.FromIntPtr(userPtr).Target;
}
set
{
// TODO: This leaks a handle.
var handle = GCHandle.Alloc(value, GCHandleType.Weak);
Native.ZkDaedalusInstance_setUserPointer(Handle, GCHandle.ToIntPtr(handle));
}
}

public static DaedalusInstance? FromNative(UIntPtr handle)
{
if (handle == UIntPtr.Zero) return null;
Expand Down
6 changes: 6 additions & 0 deletions ZenKit/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4027,6 +4027,12 @@ public static extern void ZkDaedalusScript_enumerateInstanceSymbols(UIntPtr slf,

[DllImport(DllName)]
public static extern uint ZkDaedalusInstance_getIndex(UIntPtr ptr);

[DllImport(DllName)]
public static extern IntPtr ZkDaedalusInstance_getUserPointer(UIntPtr slf);

[DllImport(DllName)]
public static extern void ZkDaedalusInstance_setUserPointer(UIntPtr slf, IntPtr ptr);

[DllImport(DllName)]
public static extern UIntPtr ZkDaedalusVm_load(UIntPtr buf);
Expand Down
Binary file modified ZenKit/runtimes/android-arm64/native/libzenkitcapi.so
Binary file not shown.
Binary file modified ZenKit/runtimes/linux-x64/native/libzenkitcapi.so
Binary file not shown.
Binary file modified ZenKit/runtimes/osx-x64/native/libzenkitcapi.dylib
Binary file not shown.
Binary file modified ZenKit/runtimes/win-x64/native/zenkitcapi.dll
Binary file not shown.

0 comments on commit ec06506

Please sign in to comment.