welsonjs/WelsonJS.Toolkit/WelsonJS.Launcher/JsNative.cs
Namhyeon, Go 87020d35ac Refactor logger interface and add JsNative interop layer
Updated ICompatibleLogger to accept params object[] for flexible logging. Refactored TraceLogger to support the new interface and improved formatting. Added JsNative.cs to encapsulate ChakraCore P/Invoke interop, and updated JsCore to use JsNative for all native calls. Modified all resource tools to accept and use ICompatibleLogger for consistent logging. Updated project file to include new and updated sources.
2025-09-28 00:07:41 +09:00

94 lines
3.5 KiB
C#

using System;
using System.Runtime.InteropServices;
namespace WelsonJS.Launcher
{
public static class JsNative
{
// === Enums / handles ===
[Flags]
public enum JsRuntimeAttributes : uint
{
None = 0x00000000,
DisableBackgroundWork = 0x00000001,
AllowScriptInterrupt = 0x00000002,
EnableIdleProcessing = 0x00000004,
DisableNativeCodeGeneration = 0x00000008,
EnableExperimentalFeatures = 0x00000010,
}
// ChakraCore typedefs are opaque pointers; represent as IntPtr
public struct JsRuntime { public IntPtr Handle; }
public struct JsContext { public IntPtr Handle; }
public struct JsValue { public IntPtr Handle; }
// JsErrorCode (essential subset; expand as needed)
public enum JsErrorCode
{
JsNoError = 0,
// Usage
JsErrorInvalidArgument = 0x10001,
JsErrorNullArgument = 0x10002,
JsErrorNoCurrentContext = 0x10003,
JsErrorInExceptionState = 0x10004,
JsErrorNotImplemented = 0x10005,
JsErrorWrongThread = 0x10006,
JsErrorRuntimeInUse = 0x10007,
// Script
JsErrorScriptException = 0x30001,
JsErrorScriptCompile = 0x30002,
JsErrorScriptTerminated = 0x30003,
// Engine
JsErrorOutOfMemory = 0x20001,
// Fatal
JsErrorFatal = 0x40001,
}
// Thread service callback: __stdcall
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate JsErrorCode JsThreadServiceCallback(IntPtr callback, IntPtr callbackState);
// ======= FIXED SIGNATURES (StdCall + Unicode) =======
[DllImport("ChakraCore.dll", CallingConvention = CallingConvention.StdCall)]
public static extern JsErrorCode JsCreateRuntime(
JsRuntimeAttributes attributes,
JsThreadServiceCallback threadService, // pass null if unused
out JsRuntime runtime);
[DllImport("ChakraCore.dll", CallingConvention = CallingConvention.StdCall)]
public static extern JsErrorCode JsCreateContext(
JsRuntime runtime,
out JsContext newContext);
[DllImport("ChakraCore.dll", CallingConvention = CallingConvention.StdCall)]
public static extern JsErrorCode JsSetCurrentContext(JsContext context);
// JsSourceContext is size_t → UIntPtr; strings are wide-char
[DllImport("ChakraCore.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
public static extern JsErrorCode JsRunScript(
string script,
UIntPtr sourceContext,
string sourceUrl,
out JsValue result);
[DllImport("ChakraCore.dll", CallingConvention = CallingConvention.StdCall)]
public static extern JsErrorCode JsConvertValueToString(JsValue value, out JsValue stringValue);
// Returns pointer to UTF-16 buffer + length (size_t) for a JsString value
[DllImport("ChakraCore.dll", CallingConvention = CallingConvention.StdCall)]
public static extern JsErrorCode JsStringToPointer(
JsValue value,
out IntPtr buffer,
out UIntPtr length);
// Unset by passing "invalid" context (JS_INVALID_REFERENCE is typically null)
[DllImport("ChakraCore.dll", CallingConvention = CallingConvention.StdCall)]
public static extern JsErrorCode JsDisposeRuntime(JsRuntime runtime);
}
}