-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
266 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Hosihikari.NativeInterop.Hook.ObjectOriented; | ||
using Hosihikari.VanillaScript.QuickJS.Types; | ||
|
||
namespace Hosihikari.VanillaScript.Hook.QuickJS; | ||
|
||
internal class FreeRuntime : HookBase<FreeRuntime.HookDelegate> | ||
{ | ||
internal unsafe delegate void HookDelegate(JsRuntime* rt); | ||
|
||
public FreeRuntime() | ||
: base("JS_FreeRuntime") { } | ||
|
||
public override unsafe HookDelegate HookedFunc => | ||
rt => | ||
{ | ||
Loader.Manager.FreeRuntime(rt); | ||
Original.Invoke(rt); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Hosihikari.NativeInterop.Hook.ObjectOriented; | ||
using Hosihikari.VanillaScript.QuickJS.Types; | ||
|
||
namespace Hosihikari.VanillaScript.Hook.QuickJS; | ||
|
||
internal class NewRuntime2 : HookBase<NewRuntime2.HookDelegate> | ||
{ | ||
internal unsafe delegate JsRuntime* HookDelegate(JsMallocFunctions* mf, void* opaque); | ||
|
||
public NewRuntime2() | ||
: base("JS_NewRuntime2") { } | ||
|
||
public override unsafe HookDelegate HookedFunc => | ||
(mf, opaque) => | ||
{ | ||
var runtime = Original(mf, opaque); | ||
Loader.Manager.AddRuntime(runtime); | ||
return runtime; | ||
}; | ||
} | ||
//JS_FreeRuntime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Hosihikari.VanillaScript.QuickJS.Wrapper; | ||
|
||
namespace Hosihikari.VanillaScript.Loader; | ||
|
||
public static partial class Manager | ||
{ | ||
public static event Action<JsRuntimeWrapper>? OnRuntimeCreated; | ||
|
||
internal static void SetupRuntime(JsRuntimeWrapper ctx) | ||
{ | ||
OnRuntimeCreated?.Invoke(ctx); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace Hosihikari.VanillaScript.QuickJS.Types; | ||
|
||
using size_t = UIntPtr; | ||
|
||
public ref struct JsMallocState | ||
{ | ||
public size_t MallocCount; | ||
public size_t MallocSize; | ||
public size_t MallocLimit; | ||
public unsafe void* Opaque; /* user opaque */ | ||
} | ||
|
||
public unsafe ref struct JsMallocFunctions | ||
{ | ||
//void *(*js_malloc)(JSMallocState *s, size_t size); | ||
public delegate* unmanaged<JsMallocState*, nuint, void*> JsMalloc; | ||
|
||
//void (*js_free)(JSMallocState *s, void *ptr); | ||
public delegate* unmanaged<JsMallocState*, void*, void> JsFree; | ||
|
||
//void *(*js_realloc)(JSMallocState *s, void *ptr, size_t size); | ||
public delegate* unmanaged<JsMallocState*, void*, nuint, void*> JsReAlloc; | ||
|
||
//size_t (*js_malloc_usable_size)(const void *ptr); | ||
public delegate* unmanaged<void*, size_t> JsMallocUsableSize; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Hosihikari.VanillaScript.QuickJS.Types; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.InteropServices; | ||
using Hosihikari.VanillaScript.Loader; | ||
|
||
namespace Hosihikari.VanillaScript.QuickJS.Wrapper; | ||
|
||
public class JsRuntimeWrapper | ||
{ | ||
public unsafe JsRuntime* Runtime { get; } | ||
private readonly List<GCHandle> _savedObject = new(); | ||
|
||
internal void Pin(object obj) | ||
{ | ||
_savedObject.Add(GCHandle.Alloc(obj)); | ||
} | ||
|
||
public static unsafe implicit operator JsRuntimeWrapper(JsRuntime* rt) | ||
{ | ||
return FetchOrCreate(rt); | ||
} | ||
|
||
private unsafe JsRuntimeWrapper(JsRuntime* rt) | ||
{ | ||
Runtime = rt; | ||
Manager.SetupRuntime(this); | ||
} | ||
|
||
public static bool TryGet(nint ctxPtr, [NotNullWhen(true)] out JsRuntimeWrapper? ctx) | ||
{ | ||
unsafe | ||
{ | ||
if ( | ||
Manager.LoadedScriptsRuntime.FirstOrDefault(x => x.Runtime == ctxPtr.ToPointer()) is | ||
{ } oldCtx | ||
) | ||
{ | ||
ctx = oldCtx; | ||
return true; | ||
} | ||
|
||
ctx = null; | ||
return false; | ||
} | ||
} | ||
|
||
public static unsafe JsRuntimeWrapper FetchOrCreate(JsRuntime* ctx) | ||
{ | ||
if (Manager.LoadedScriptsRuntime.FirstOrDefault(x => x.Runtime == ctx) is { } oldCtx) | ||
{ | ||
return oldCtx; | ||
} | ||
var newInstance = new JsRuntimeWrapper(ctx); | ||
Manager.LoadedScriptsRuntime.Add(newInstance); | ||
return newInstance; | ||
} | ||
|
||
internal void Free() | ||
{ | ||
foreach (var pinedItem in _savedObject) | ||
{ | ||
pinedItem.Free(); | ||
} | ||
} | ||
} |
Oops, something went wrong.