#pragma once

// Helper inline di marshalling, scope di chiamata e gestione errori condivisi
// tra function.cc, function_marshal.cc, function_fast.cc e function_async.cc.

#include "function.h"

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace ctypes {

// Chiamata a una funzione di una libreria: rifiutata dopo close(), e contata
// finché è in corso così che close() non scarichi il codice in esecuzione.
class LibraryCallScope {
 public:
  LibraryCallScope(Library* library, Napi::Env env) : library_(library) {
    if (library_ == nullptr) {
      return;
    }
    if (!library_->IsLoaded()) [[unlikely]] {
      throw Napi::Error::New(env, "Library is closed");
    }
    library_->BeginCall();
  }
  ~LibraryCallScope() {
    if (library_ != nullptr) {
      library_->EndCall();
    }
  }
  LibraryCallScope(const LibraryCallScope&) = delete;
  LibraryCallScope& operator=(const LibraryCallScope&) = delete;

 private:
  Library* library_;
};

// Come LibraryCallScope per ingressi noexcept che hanno già verificato
// library == nullptr || library->IsLoaded() prima di iniziare la chiamata.
class ActiveLibraryCallScope {
 public:
  explicit ActiveLibraryCallScope(Library* library) noexcept : library_(library) {
    if (library_ != nullptr) {
      library_->BeginCall();
    }
  }
  ~ActiveLibraryCallScope() noexcept {
    if (library_ != nullptr) {
      library_->EndCall();
    }
  }
  ActiveLibraryCallScope(const ActiveLibraryCallScope&) = delete;
  ActiveLibraryCallScope& operator=(const ActiveLibraryCallScope&) = delete;

 private:
  Library* library_;
};
// Incrementa un contatore (se non nullo) per la durata dello scope: profondità
// di rientranza, uso di uno slot della cache variadica.
class CountScope {
 public:
  explicit CountScope(uint32_t* counter) noexcept : counter_(counter) {
    if (counter_ != nullptr) {
      ++*counter_;
    }
  }
  ~CountScope() {
    if (counter_ != nullptr) {
      --*counter_;
    }
  }
  CountScope(const CountScope&) = delete;
  CountScope& operator=(const CountScope&) = delete;

 private:
  uint32_t* counter_;
};

// Nome Python del tipo argomento nei messaggi: "... cannot be interpreted as ctypes.c_void_p".
constexpr std::string_view PointerTypeName(CType type) {
  switch (type) {
    case CType::CTYPES_STRING:
      return "c_char_p";
    case CType::CTYPES_WSTRING:
      return "c_wchar_p";
    default:
      return "c_void_p";
  }
}

[[noreturn]] inline void ThrowWrongType(const Napi::Value& val, CType type) {
  throw Napi::TypeError::New(val.Env(), std::format("'{}' object cannot be interpreted as ctypes.{}",
                                                    PythonTypeName(val), PointerTypeName(type)));
}

// Memoria di un oggetto passato a un parametro puntatore: Buffer e ogni
// TypedArray/DataView/ArrayBuffer (in place); altri oggetti sono un TypeError.
inline void* ObjectData(const Napi::Value& val, CType type) {
  if (void* data = napi_fast::TryViewData(val)) [[likely]] {
    return data;
  }
  // DataView (rifiutato da napi_get_buffer_info), ArrayBuffer, viste vuote.
  if (const std::optional<std::span<uint8_t>> bytes = MemoryBytes(val)) {
    return bytes->data();
  }
  ThrowWrongType(val, type);
}

// Memoria di un argomento staccata (transfer(), transfer list) mentre il codice
// nativo poteva usarla.
[[nodiscard]] bool IsDetachedView(const Napi::Value& value);

// Struttura, unione o array per valore: i byte sono copiati prima della
// chiamata, quindi la memoria JS da cui vengono non è usata in place.
[[nodiscard]] constexpr bool IsByValueAggregate(CType type) noexcept {
  return type == CType::CTYPES_STRUCT || type == CType::CTYPES_UNION || type == CType::CTYPES_ARRAY;
}

inline constexpr const char* kDetachedArgumentMessage =
  "memory was detached (transfer() or a transfer list) or shrunk out of bounds while the native call could use it; "
  "native code may have accessed memory no longer owned by this buffer";

inline uintptr_t AddressFromBigInt(const Napi::Value& val) {
  bool lossless;
  return static_cast<uintptr_t>(val.As<Napi::BigInt>().Uint64Value(&lossless));
}

inline uintptr_t AddressFromNumber(const Napi::Value& val, double d, CType type) {
  if (int64_t address; TryExactInt(d, address)) [[likely]] {
    return static_cast<uintptr_t>(address);
  }
  if (!std::isfinite(d) || d != std::trunc(d)) [[unlikely]] {
    ThrowWrongType(val, type);
  }
  return static_cast<uintptr_t>(IntegerBitsFromDouble(val, d));
}

inline void MarshalOther(const Napi::Value& val, CType type, uint8_t* slot) {
  if (JSToC(val, type, std::span(slot, ARG_SLOT_SIZE)) < 0) {
    throw Napi::TypeError::New(val.Env(), "unsupported argument type");
  }
}

// Puntatore da un valore che non è una stringa: memoria di Buffer / TypedArray
// / DataView / ArrayBuffer, indirizzo BigInt o number, NULL per null e
// undefined; true e false solo per c_void_p (Python: un int).
inline void* PointerArg(const Napi::Value& val, napi_valuetype kind, CType type) {
  switch (kind) {
    case napi_object:
      return ObjectData(val, type);
    case napi_bigint:
      return reinterpret_cast<void*>(AddressFromBigInt(val));
    case napi_number:
      return reinterpret_cast<void*>(AddressFromNumber(val, val.As<Napi::Number>().DoubleValue(), type));
    case napi_boolean:
      if (type == CType::CTYPES_POINTER) {
        return reinterpret_cast<void*>(static_cast<uintptr_t>(val.As<Napi::Boolean>().Value()));
      }
      break;
    case napi_null:
    case napi_undefined:
      return nullptr;
    default:
      break;
  }
  ThrowWrongType(val, type);
}

// Un oggetto o una funzione: argomento che solo il JavaScript sa convertire
// (istanze CData, from_param, _as_parameter_, ...).
[[nodiscard]] inline bool IsObjectLike(const Napi::Value& val) {
  const napi_valuetype kind = val.Type();
  return kind == napi_object || kind == napi_function;
}

[[nodiscard]] inline bool HasObjectArgument(const Napi::CallbackInfo& info) {
  for (size_t i = 0; i < info.Length(); i++) {
    if (IsObjectLike(info[i])) {
      return true;
    }
  }
  return false;
}

enum class NumericKind { Integer, Real };

[[noreturn]] inline void ThrowNotNumeric(const Napi::Value& val, NumericKind kind) {
  throw Napi::TypeError::New(val.Env(),
                             kind == NumericKind::Integer
                               ? std::format("'{}' object cannot be interpreted as an integer", PythonTypeName(val))
                               : std::format("must be real number, not {}", PythonTypeName(val)));
}

// Valori non-number e costruzione ArgumentError: fuori dal percorso caldo (function_marshal.cc).
int64_t IntegerArgSlow(const Napi::Value& val);
double DoubleArgSlow(const Napi::Value& val);
Napi::Error ArgumentErrorFor(Napi::Env env, size_t index, const Napi::Error& cause);

// ConvParam di CPython: una str senza argtypes (wchar_t* terminato) con un
// carattere NUL è un ValueError, non una stringa troncata.
[[noreturn]] void ThrowEmbeddedNull(Napi::Env env);
CTYPES_ALWAYS_INLINE void ThrowIfEmbeddedNull(Napi::Env env, std::u16string_view str) {
  if (str.find(u'\0') != std::u16string_view::npos) [[unlikely]] {
    ThrowEmbeddedNull(env);
  }
}
CTYPES_ALWAYS_INLINE int32_t Int32FromNumber(const Napi::Value& val, double d) {
  if (int32_t v; TryExactInt(d, v)) [[likely]] {
    return v;
  }
  return static_cast<int32_t>(static_cast<uint32_t>(IntegerBitsFromDouble(val, d)));
}

CTYPES_ALWAYS_INLINE uint32_t Uint32FromNumber(const Napi::Value& val, double d) {
  if (uint32_t v; TryExactInt(d, v)) [[likely]] {
    return v;
  }
  return static_cast<uint32_t>(IntegerBitsFromDouble(val, d));
}

CTYPES_ALWAYS_INLINE int64_t Int64FromNumber(const Napi::Value& val, double d) {
  if (int64_t v; TryExactInt(d, v)) [[likely]] {
    return v;
  }
  return static_cast<int64_t>(IntegerBitsFromDouble(val, d));
}

CTYPES_ALWAYS_INLINE int32_t Int32Arg(const Napi::Value& val) {
  if (double d; napi_fast::TryDouble(val, d)) [[likely]] {
    return Int32FromNumber(val, d);
  }
  return static_cast<int32_t>(IntegerArgSlow(val));
}

CTYPES_ALWAYS_INLINE uint32_t Uint32Arg(const Napi::Value& val) {
  if (double d; napi_fast::TryDouble(val, d)) [[likely]] {
    return Uint32FromNumber(val, d);
  }
  return static_cast<uint32_t>(IntegerArgSlow(val));
}

CTYPES_ALWAYS_INLINE bool TryInt64Number(const Napi::Value& val, int64_t& out) {
  double d;
  if (!napi_fast::TryDouble(val, d)) {
    return false;
  }
  out = Int64FromNumber(val, d);
  return true;
}

CTYPES_ALWAYS_INLINE int64_t Int64Arg(const Napi::Value& val) {
  if (int64_t v; TryInt64Number(val, v)) [[likely]] {
    return v;
  }
  return IntegerArgSlow(val);
}

CTYPES_ALWAYS_INLINE double DoubleArg(const Napi::Value& val) {
  if (double v; napi_fast::TryDouble(val, v)) [[likely]] {
    return v;
  }
  return DoubleArgSlow(val);
}

// Confine Node-API condiviso dagli ingressi nativi grezzi: converte ogni
// eccezione C++ in eccezione JS, come details::WrapCallback di node-addon-api.
template <typename Fn>
CTYPES_ALWAYS_INLINE napi_value GuardNapiCall(napi_env env, Fn&& fn) noexcept {
  try {
    return fn();
  } catch (const Napi::Error& e) {
    e.ThrowAsJavaScriptException();
  } catch (const std::exception& e) {
    Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
  } catch (...) {
    Napi::Error::New(env, "A native exception was thrown").ThrowAsJavaScriptException();
  }
  return nullptr;
}

[[nodiscard]] CTYPES_NOINLINE inline napi_value RaiseForeignFault(napi_env env, const ForeignFault& fault) noexcept {
  return GuardNapiCall(env, [&]() -> napi_value { ThrowForeignFault(Napi::Env(env), fault); });
}

// Valore di un argomento nel suo slot (almeno 8 byte), sempre come parola
// intera: gli interi stretti estesi, un float nei 32 bit bassi con zero sopra.
template <typename T>
CTYPES_ALWAYS_INLINE void StoreArgument(uint8_t* slot, T value) noexcept {
  if constexpr (std::is_integral_v<T> && sizeof(T) < sizeof(uint64_t) && std::endian::native == std::endian::little) {
    const uint64_t word =
      std::is_signed_v<T> ? static_cast<uint64_t>(static_cast<int64_t>(value)) : static_cast<uint64_t>(value);
    memcpy(slot, &word, sizeof(word));
  } else if constexpr (std::same_as<T, float> && std::endian::native == std::endian::little) {
    const uint64_t word = std::bit_cast<uint32_t>(value);
    memcpy(slot, &word, sizeof(word));
  } else {
    memcpy(slot, &value, sizeof(value));
  }
}

// Number senza argtypes già accettato da VariadicNumberType (CTYPES_INT32):
// un intero entro long / unsigned long, passato come C int (i 32 bit bassi,
// come ConvParam di CPython).
CTYPES_ALWAYS_INLINE void StoreVariadicNumber(double number, uint8_t* slot) {
  if (int32_t value; TryExactInt(number, value)) [[likely]] {
    StoreArgument(slot, value);
    return;
  }
  const uint64_t bits =
    number < 0 ? static_cast<uint64_t>(static_cast<int64_t>(number)) : static_cast<uint64_t>(number);
  StoreArgument(slot, static_cast<int32_t>(static_cast<uint32_t>(bits)));
}

// Verità di un number come in Python: NaN è vero (bool(float("nan"))).
[[nodiscard]] CTYPES_ALWAYS_INLINE bool NumberTruth(double number) {
  return number != 0 || std::isnan(number);
}

// Argomento extra con il tipo dichiarato dal wrapper JS (TypedExtras): il
// valore C già convertito, come primitivo JS. false se il valore non ha la
// forma attesa: la chiamata passa allora al percorso generale, che lo converte
// (MarshalPrimitive) o lo rifiuta.
[[nodiscard]] CTYPES_ALWAYS_INLINE bool StoreTypedExtra(const Napi::Value& val, CType type, uint8_t* slot) {
  switch (type) {
    case CType::CTYPES_FLOAT:
      if (double number; napi_fast::TryDouble(val, number)) {
        StoreArgument(slot, static_cast<float>(number));
        return true;
      }
      return false;
    case CType::CTYPES_DOUBLE:
      if (double number; napi_fast::TryDouble(val, number)) [[likely]] {
        StoreArgument(slot, number);
        return true;
      }
      return false;
    case CType::CTYPES_INT32:
      if (double number; napi_fast::TryDouble(val, number)) [[likely]] {
        if (int32_t value; TryExactInt(number, value)) [[likely]] {
          StoreArgument(slot, value);
          return true;
        }
      }
      return false;
    case CType::CTYPES_UINT32:
      if (double number; napi_fast::TryDouble(val, number)) [[likely]] {
        if (uint32_t value; TryExactInt(number, value)) [[likely]] {
          StoreArgument(slot, value);
          return true;
        }
      }
      return false;
    case CType::CTYPES_INT64:
    case CType::CTYPES_UINT64:
    case CType::CTYPES_SIZE_T:
    case CType::CTYPES_SSIZE_T:
      if (uint64_t bits; napi_fast::TryBigIntWord(val, bits)) [[likely]] {
        if (type == CType::CTYPES_INT64) {
          StoreArgument(slot, static_cast<int64_t>(bits));
        } else if (type == CType::CTYPES_UINT64) {
          StoreArgument(slot, bits);
        } else if (type == CType::CTYPES_SIZE_T) {
          StoreArgument(slot, static_cast<size_t>(bits));
        } else {
          StoreArgument(slot, static_cast<std::ptrdiff_t>(bits));
        }
        return true;
      }
      return false;
    default:
      return false;
  }
}

// Argomento extra dalla memoria condivisa del convertitore JS (TypedExtras::bits):
// 8 byte per posizione: un double per i tipi floating point, i bit per gli interi.
CTYPES_ALWAYS_INLINE void StoreTypedExtraBits(const std::byte* bits, CType type, uint8_t* slot) {
  switch (type) {
    case CType::CTYPES_INT8:
      StoreArgument(slot, static_cast<int8_t>(LoadAs<uint32_t>(bits)));
      break;
    case CType::CTYPES_UINT8:
      StoreArgument(slot, static_cast<uint8_t>(LoadAs<uint32_t>(bits)));
      break;
    case CType::CTYPES_CHAR:
      StoreArgument(slot, static_cast<char>(LoadAs<uint32_t>(bits)));
      break;
    case CType::CTYPES_BOOL:
      StoreArgument(slot, static_cast<uint8_t>(LoadAs<uint32_t>(bits) != 0));
      break;
    case CType::CTYPES_INT16:
      StoreArgument(slot, static_cast<int16_t>(LoadAs<uint32_t>(bits)));
      break;
    case CType::CTYPES_UINT16:
      StoreArgument(slot, static_cast<uint16_t>(LoadAs<uint32_t>(bits)));
      break;
    case CType::CTYPES_WCHAR:
      StoreArgument(slot, static_cast<wchar_t>(LoadAs<uint32_t>(bits)));
      break;
    case CType::CTYPES_FLOAT:
      StoreArgument(slot, static_cast<float>(LoadAs<double>(bits)));
      break;
    case CType::CTYPES_DOUBLE:
      StoreArgument(slot, LoadAs<double>(bits));
      break;
    case CType::CTYPES_INT32:
      StoreArgument(slot, LoadAs<int32_t>(bits));
      break;
    case CType::CTYPES_UINT32:
      StoreArgument(slot, LoadAs<uint32_t>(bits));
      break;
    case CType::CTYPES_INT64:
      StoreArgument(slot, LoadAs<int64_t>(bits));
      break;
    case CType::CTYPES_UINT64:
      StoreArgument(slot, LoadAs<uint64_t>(bits));
      break;
    case CType::CTYPES_SIZE_T:
      StoreArgument(slot, static_cast<size_t>(LoadAs<uint64_t>(bits)));
      break;
    case CType::CTYPES_SSIZE_T:
      StoreArgument(slot, static_cast<std::ptrdiff_t>(LoadAs<int64_t>(bits)));
      break;
    default:
      break;
  }
}

// Argomento intero dai bit di un BigInt (troncati al tipo).
CTYPES_ALWAYS_INLINE void MarshalIntegerBits(uint64_t bits, CType type, uint8_t* slot) {
  switch (type) {
    case CType::CTYPES_INT8:
      StoreArgument(slot, static_cast<int8_t>(bits));
      break;
    case CType::CTYPES_UINT8:
      StoreArgument(slot, static_cast<uint8_t>(bits));
      break;
    case CType::CTYPES_INT16:
      StoreArgument(slot, static_cast<int16_t>(bits));
      break;
    case CType::CTYPES_UINT16:
      StoreArgument(slot, static_cast<uint16_t>(bits));
      break;
    case CType::CTYPES_INT32:
      StoreArgument(slot, static_cast<int32_t>(static_cast<uint32_t>(bits)));
      break;
    case CType::CTYPES_UINT32:
      StoreArgument(slot, static_cast<uint32_t>(bits));
      break;
    case CType::CTYPES_LONG:
      StoreArgument(slot, static_cast<long>(bits));
      break;
    case CType::CTYPES_ULONG:
      StoreArgument(slot, static_cast<unsigned long>(bits));
      break;
    default:
      memcpy(slot, &bits, sizeof(bits));
      break;
  }
}

template <typename T>
  requires(std::same_as<T, int8_t> || std::same_as<T, uint8_t> || std::same_as<T, int16_t> || std::same_as<T, uint16_t>)
[[nodiscard]] CTYPES_ALWAYS_INLINE T NarrowFromNumber(const Napi::Value& value, double number) {
  // Lo stesso limite per segno e senza segno: il cast a int64 è sempre definito, quello a T conserva i bit bassi.
  if (std::abs(number) < 0x1p63) [[likely]] {
    const auto whole = static_cast<int64_t>(number);
    if (static_cast<double>(whole) == number) [[likely]] {
      return static_cast<T>(whole);
    }
  }
  return static_cast<T>(IntegerBitsFromDouble(value, number));
}

CTYPES_ALWAYS_INLINE void MarshalNumber(const Napi::Value& val, double d, CType type, uint8_t* slot) {
  switch (type) {
    case CType::CTYPES_INT8:
      StoreArgument(slot, NarrowFromNumber<int8_t>(val, d));
      break;
    case CType::CTYPES_UINT8:
      StoreArgument(slot, NarrowFromNumber<uint8_t>(val, d));
      break;
    case CType::CTYPES_INT16:
      StoreArgument(slot, NarrowFromNumber<int16_t>(val, d));
      break;
    case CType::CTYPES_UINT16:
      StoreArgument(slot, NarrowFromNumber<uint16_t>(val, d));
      break;
    case CType::CTYPES_INT32:
      StoreArgument(slot, Int32FromNumber(val, d));
      break;
    case CType::CTYPES_UINT32:
      StoreArgument(slot, Uint32FromNumber(val, d));
      break;
    case CType::CTYPES_LONG:
      StoreArgument(slot, static_cast<long>(Int64FromNumber(val, d)));
      break;
    case CType::CTYPES_ULONG:
      StoreArgument(slot, static_cast<unsigned long>(Int64FromNumber(val, d)));
      break;
    case CType::CTYPES_FLOAT:
      StoreArgument(slot, static_cast<float>(d));
      break;
    case CType::CTYPES_DOUBLE:
      StoreArgument(slot, d);
      break;
    default: {
      const int64_t v = Int64FromNumber(val, d);
      memcpy(slot, &v, sizeof(v));
      break;
    }
  }
}

// Conversione diretta dei ritorni scalari/puntatore (stesse regole di CToJS);
// i valori si leggono dallo slot con LoadReturnValue.
[[nodiscard]] CTYPES_ALWAYS_INLINE Napi::Value ReturnScalar(Napi::Env env, CType type, const void* ret) {
  switch (type) {
    case CType::CTYPES_VOID:
      return env.Undefined();
    case CType::CTYPES_BOOL:
      return Napi::Boolean::New(env, LoadReturnValue<uint8_t>(ret) != 0);
    case CType::CTYPES_INT8:
      return napi_fast::Int32(env, LoadReturnValue<int8_t>(ret));
    case CType::CTYPES_UINT8:
      return napi_fast::Uint32(env, LoadReturnValue<uint8_t>(ret));
    case CType::CTYPES_INT16:
      return napi_fast::Int32(env, LoadReturnValue<int16_t>(ret));
    case CType::CTYPES_UINT16:
      return napi_fast::Uint32(env, LoadReturnValue<uint16_t>(ret));
    case CType::CTYPES_INT32:
      return napi_fast::Int32(env, LoadReturnValue<int32_t>(ret));
    case CType::CTYPES_UINT32:
      return napi_fast::Uint32(env, LoadReturnValue<uint32_t>(ret));
    case CType::CTYPES_INT64:
      return Napi::BigInt::New(env, LoadReturnValue<int64_t>(ret));
    case CType::CTYPES_UINT64:
      return Napi::BigInt::New(env, LoadReturnValue<uint64_t>(ret));
    case CType::CTYPES_SIZE_T:
      return Napi::BigInt::New(env, static_cast<uint64_t>(LoadReturnValue<size_t>(ret)));
    case CType::CTYPES_SSIZE_T:
      return Napi::BigInt::New(env, static_cast<int64_t>(LoadReturnValue<ssize_t>(ret)));
    case CType::CTYPES_LONG:
      return Napi::BigInt::New(env, static_cast<int64_t>(LoadReturnValue<long>(ret)));
    case CType::CTYPES_ULONG:
      return Napi::BigInt::New(env, static_cast<uint64_t>(LoadReturnValue<unsigned long>(ret)));
    case CType::CTYPES_FLOAT:
      return Napi::Number::New(env, LoadReturnValue<float>(ret));
    case CType::CTYPES_DOUBLE:
      return Napi::Number::New(env, LoadReturnValue<double>(ret));
    case CType::CTYPES_POINTER: {
      void* const ptr = LoadReturnValue<void*>(ret);
      return ptr == nullptr ? env.Null()
                            : Napi::BigInt::New(env, static_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr)));
    }
    case CType::CTYPES_STRING: {
      const char* const str = LoadReturnValue<const char*>(ret);
      return str == nullptr ? env.Null() : Napi::String::New(env, str);
    }
    // CToJS legge i primi byte: il carattere passa da una copia ristretta.
    case CType::CTYPES_CHAR: {
      const char value = LoadReturnValue<char>(ret);
      return CToJS(env, &value, type);
    }
    case CType::CTYPES_WCHAR: {
      const wchar_t value = LoadReturnValue<wchar_t>(ret);
      return CToJS(env, &value, type);
    }
    default:
      return CToJS(env, ret, type);
  }
}

[[nodiscard]] CTYPES_NOINLINE inline napi_value RawReturnScalarSlow(napi_env env,
                                                                    CType type,
                                                                    const void* ret) noexcept {
  return GuardNapiCall(env, [&]() -> napi_value { return ReturnScalar(Napi::Env(env), type, ret); });
}

[[nodiscard]] CTYPES_ALWAYS_INLINE napi_value RawReturnScalar(napi_env env, CType type, const void* ret) noexcept {
  switch (type) {
    case CType::CTYPES_VOID:
      return napi_fast::RawUndefined(env);
    case CType::CTYPES_BOOL:
      return napi_fast::RawBool(env, LoadReturnValue<uint8_t>(ret) != 0);
    case CType::CTYPES_INT8:
      return napi_fast::RawInt32(env, LoadReturnValue<int8_t>(ret));
    case CType::CTYPES_UINT8:
      return napi_fast::RawUint32(env, LoadReturnValue<uint8_t>(ret));
    case CType::CTYPES_INT16:
      return napi_fast::RawInt32(env, LoadReturnValue<int16_t>(ret));
    case CType::CTYPES_UINT16:
      return napi_fast::RawUint32(env, LoadReturnValue<uint16_t>(ret));
    case CType::CTYPES_INT32:
      return napi_fast::RawInt32(env, LoadReturnValue<int32_t>(ret));
    case CType::CTYPES_UINT32:
      return napi_fast::RawUint32(env, LoadReturnValue<uint32_t>(ret));
    case CType::CTYPES_INT64:
      return napi_fast::RawInt64BigInt(env, LoadReturnValue<int64_t>(ret));
    case CType::CTYPES_UINT64:
      return napi_fast::RawUint64BigInt(env, LoadReturnValue<uint64_t>(ret));
    case CType::CTYPES_SIZE_T:
      return napi_fast::RawUint64BigInt(env, static_cast<uint64_t>(LoadReturnValue<size_t>(ret)));
    case CType::CTYPES_SSIZE_T:
      return napi_fast::RawInt64BigInt(env, static_cast<int64_t>(LoadReturnValue<ssize_t>(ret)));
    case CType::CTYPES_LONG:
      return napi_fast::RawInt64BigInt(env, static_cast<int64_t>(LoadReturnValue<long>(ret)));
    case CType::CTYPES_ULONG:
      return napi_fast::RawUint64BigInt(env, static_cast<uint64_t>(LoadReturnValue<unsigned long>(ret)));
    case CType::CTYPES_FLOAT:
      return napi_fast::RawDouble(env, static_cast<double>(LoadReturnValue<float>(ret)));
    case CType::CTYPES_DOUBLE:
      return napi_fast::RawDouble(env, LoadReturnValue<double>(ret));
    case CType::CTYPES_POINTER: {
      void* const ptr = LoadReturnValue<void*>(ret);
      return ptr == nullptr ? napi_fast::RawNull(env)
                            : napi_fast::RawUint64BigInt(env, static_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr)));
    }
    case CType::CTYPES_STRING:
      return napi_fast::RawUtf8String(env, LoadReturnValue<const char*>(ret));
    default:
      return RawReturnScalarSlow(env, type, ret);
  }
}

// Chiamata diretta R(const void*) per funzioni a 1 argomento puntatore/stringa
// su ABI 64-bit predefinita (primo registro intero dell'ABI, senza frame).
CTYPES_ALWAYS_INLINE void InvokeUnaryPointer(void* fn, CType return_type, const void* ptr, void* ret) {
  switch (return_type) {
    case CType::CTYPES_VOID:
      reinterpret_cast<void (*)(const void*)>(fn)(ptr);
      break;
    case CType::CTYPES_BOOL:
      // Come uint8_t: un byte diverso da 0/1 non è un bool C++ valido.
      StoreReturnValue(ret, reinterpret_cast<uint8_t (*)(const void*)>(fn)(ptr));
      break;
    case CType::CTYPES_INT8:
      StoreReturnValue(ret, reinterpret_cast<int8_t (*)(const void*)>(fn)(ptr));
      break;
    case CType::CTYPES_UINT8:
      StoreReturnValue(ret, reinterpret_cast<uint8_t (*)(const void*)>(fn)(ptr));
      break;
    case CType::CTYPES_INT16:
      StoreReturnValue(ret, reinterpret_cast<int16_t (*)(const void*)>(fn)(ptr));
      break;
    case CType::CTYPES_UINT16:
      StoreReturnValue(ret, reinterpret_cast<uint16_t (*)(const void*)>(fn)(ptr));
      break;
    case CType::CTYPES_INT32:
      StoreReturnValue(ret, reinterpret_cast<int32_t (*)(const void*)>(fn)(ptr));
      break;
    case CType::CTYPES_UINT32:
      StoreReturnValue(ret, reinterpret_cast<uint32_t (*)(const void*)>(fn)(ptr));
      break;
    case CType::CTYPES_INT64:
    case CType::CTYPES_SSIZE_T:
      StoreReturnValue(ret, reinterpret_cast<int64_t (*)(const void*)>(fn)(ptr));
      break;
    case CType::CTYPES_UINT64:
    case CType::CTYPES_SIZE_T:
    case CType::CTYPES_POINTER:
    case CType::CTYPES_STRING:
      StoreReturnValue(ret, reinterpret_cast<uint64_t (*)(const void*)>(fn)(ptr));
      break;
    case CType::CTYPES_LONG:
      StoreReturnValue(ret, reinterpret_cast<long (*)(const void*)>(fn)(ptr));
      break;
    case CType::CTYPES_ULONG:
      StoreReturnValue(ret, reinterpret_cast<unsigned long (*)(const void*)>(fn)(ptr));
      break;
    case CType::CTYPES_FLOAT:
      StoreReturnValue(ret, reinterpret_cast<float (*)(const void*)>(fn)(ptr));
      break;
    case CType::CTYPES_DOUBLE:
      StoreReturnValue(ret, reinterpret_cast<double (*)(const void*)>(fn)(ptr));
      break;
    default:
      break;
  }
}

#ifdef _WIN32
// GetLastError() / SetLastError() senza chiamate, per lo stato di sistema
// salvato a ogni chiamata (vedi CTypesAddon::system_last_error): il campo
// LastErrorValue del TEB che le due funzioni leggono e scrivono (offset 0x68
// nel TEB a 64 bit, x64 e ARM64). Altrove le funzioni stesse.
CTYPES_ALWAYS_INLINE uint32_t LoadSystemLastError() noexcept {
#if defined(_MSC_VER) && defined(_M_X64) && !defined(_M_ARM64EC)
  return __readgsdword(0x68);
#elif defined(_MSC_VER) && defined(_M_ARM64)
  return __readx18dword(0x68);
#else
  return static_cast<uint32_t>(::GetLastError());
#endif
}

CTYPES_ALWAYS_INLINE void StoreSystemLastError(uint32_t value) noexcept {
#if defined(_MSC_VER) && defined(_M_X64) && !defined(_M_ARM64EC)
  __writegsdword(0x68, value);
#elif defined(_MSC_VER) && defined(_M_ARM64)
  __writex18dword(0x68, value);
#else
  ::SetLastError(static_cast<DWORD>(value));
#endif
}
#endif

// Scambio errno / GetLastError() condiviso tra percorso sincrono e PreparedCall::Run().
CTYPES_ALWAYS_INLINE SavedErrorState SwapErrorStateIn([[maybe_unused]] bool use_last_error,
                                                      bool use_errno,
                                                      [[maybe_unused]] uint32_t last_error,
                                                      int errno_value) noexcept {
  SavedErrorState saved;
#ifdef _WIN32
  if (use_last_error) {
    saved.last_error = LoadSystemLastError();
    StoreSystemLastError(last_error);
  }
#endif
  if (use_errno) {
    saved.errno_value = errno;
    errno = errno_value;
  }
  return saved;
}

CTYPES_ALWAYS_INLINE void SwapErrorStateOut(bool use_last_error,
                                            bool use_errno,
                                            const SavedErrorState& saved,
                                            uint32_t& last_error,
                                            int& errno_value) noexcept {
  if (use_last_error) {
#ifdef _WIN32
    last_error = LoadSystemLastError();
    StoreSystemLastError(saved.last_error);
#else
    // Fuori da Windows get_last_error() è l'errno della chiamata.
    last_error = static_cast<uint32_t>(errno);
#endif
  }
  if (use_errno) {
    errno_value = errno;
    errno = saved.errno_value;
  }
}

CTYPES_ALWAYS_INLINE SavedErrorState FFIFunction::SwapInErrorState() noexcept {
#ifdef _WIN32
  // Il GetLastError() di sistema della chiamata precedente (vedi
  // CTypesAddon::system_last_error).
  if (addon_ != nullptr) [[likely]] {
    StoreSystemLastError(addon_->system_last_error);
  }
#endif
  if (!(capture_last_error_ || capture_errno_)) [[likely]] {
    return {};
  }
  return SwapErrorStateIn(capture_last_error_, capture_errno_, addon_ ? addon_->captured_last_error : 0,
                          addon_ ? addon_->captured_errno : 0);
}

CTYPES_ALWAYS_INLINE void FFIFunction::CaptureErrorState(const SavedErrorState& saved) noexcept {
#ifdef _WIN32
  // Con use_last_error il valore di sistema resta quello di prima.
  if (!capture_last_error_ && addon_ != nullptr) [[likely]] {
    addon_->system_last_error = LoadSystemLastError();
  }
#endif
  if (capture_last_error_ || capture_errno_) [[unlikely]] {
    SwapErrorStateOut(capture_last_error_, capture_errno_, saved, last_error_, last_errno_);
    if (addon_) {
      if (capture_last_error_) {
        addon_->captured_last_error = last_error_;
      }
      if (capture_errno_) {
        addon_->captured_errno = last_errno_;
      }
    }
  }
}

template <ArgumentList Args>
inline void FFIFunction::ThrowIfArgumentDetached(const Args& info, uint32_t memory) {
  for (size_t i = 0; memory != 0; i++, memory >>= 1) {
    if ((memory & 1) != 0 && IsDetachedView(info[i])) {
      throw Napi::Error::New(info.Env(), std::format("argument {}: {}", i + 1, kDetachedArgumentMessage));
    }
  }
}

template <ArgumentList Args, std::predicate<size_t> ByValue>
inline void FFIFunction::ThrowIfArgumentDetached(const Args& info, ByValue by_value) {
  for (size_t i = 0; i < info.Length(); i++) {
    if (!by_value(i) && IsDetachedView(info[i])) {
      throw Napi::Error::New(info.Env(), std::format("argument {}: {}", i + 1, kDetachedArgumentMessage));
    }
  }
}

// MarshalPrimitive condiviso da Call(), SimpleCall() e PrepareAsyncCall().
CTYPES_ALWAYS_INLINE bool FFIFunction::MarshalPrimitive(const Napi::Value& val,
                                                        CType type,
                                                        uint8_t* slot,
                                                        bool* memory) {
  switch (type) {
    case CType::CTYPES_INT32: {
      StoreArgument(slot, Int32Arg(val));
      return true;
    }
    case CType::CTYPES_UINT32: {
      StoreArgument(slot, Uint32Arg(val));
      return true;
    }
    case CType::CTYPES_INT64:
    case CType::CTYPES_UINT64:
    case CType::CTYPES_SIZE_T:
    case CType::CTYPES_SSIZE_T: {
      // number con una sola chiamata; poi una sola Value::Type() per gli altri.
      int64_t v = 0;
      if (TryInt64Number(val, v)) [[likely]] {
        memcpy(slot, &v, sizeof(v));
        return true;
      }
      switch (val.Type()) {
        case napi_bigint: {
          bool lossless;
          v = val.As<Napi::BigInt>().Int64Value(&lossless);
          break;
        }
        case napi_boolean:
          v = val.As<Napi::Boolean>().Value() ? 1 : 0;
          break;
        case napi_object:
          // Estensione node-ctypes: la memoria JS (viste, ArrayBuffer) passa il suo indirizzo.
          if (const std::optional<std::span<uint8_t>> bytes = MemoryBytes(val)) {
            v = static_cast<int64_t>(reinterpret_cast<intptr_t>(bytes->data()));
            if (memory != nullptr) {
              *memory = true;
            }
            break;
          }
          ThrowNotNumeric(val, NumericKind::Integer);
        default:
          ThrowNotNumeric(val, NumericKind::Integer);
      }
      memcpy(slot, &v, sizeof(v));
      return true;
    }
    case CType::CTYPES_DOUBLE: {
      double v = DoubleArg(val);
      memcpy(slot, &v, sizeof(v));
      return true;
    }
    case CType::CTYPES_FLOAT: {
      StoreArgument(slot, static_cast<float>(DoubleArg(val)));
      return true;
    }
    // Slot di ARG_SLOT_SIZE byte: mai una parola del frame (IsSimpleArgumentType è falso).
    case CType::CTYPES_LONGDOUBLE: {
      const long double v = DoubleArg(val);
      memcpy(slot, &v, sizeof(v));
      return true;
    }
    case CType::CTYPES_BOOL: {
      bool b;
      if (!napi_fast::TryBool(val, b)) {
        double number;
        b = napi_fast::TryDouble(val, number) ? NumberTruth(number) : val.ToBoolean().Value();
      }
      StoreArgument(slot, static_cast<uint8_t>(b ? 1 : 0));
      return true;
    }
    case CType::CTYPES_CHAR: {
      StoreArgument(slot, static_cast<char>(CharFromJS(val)));
      return true;
    }
    case CType::CTYPES_WCHAR: {
      StoreArgument(slot, WcharFromJS(val));
      return true;
    }
    case CType::CTYPES_INT8: {
      StoreArgument(slot, static_cast<int8_t>(Int32Arg(val)));
      return true;
    }
    case CType::CTYPES_UINT8: {
      StoreArgument(slot, static_cast<uint8_t>(Uint32Arg(val)));
      return true;
    }
    case CType::CTYPES_INT16: {
      StoreArgument(slot, static_cast<int16_t>(Int32Arg(val)));
      return true;
    }
    case CType::CTYPES_UINT16: {
      StoreArgument(slot, static_cast<uint16_t>(Uint32Arg(val)));
      return true;
    }
    case CType::CTYPES_LONG: {
      StoreArgument(slot, static_cast<long>(Int64Arg(val)));
      return true;
    }
    case CType::CTYPES_ULONG: {
      StoreArgument(slot, static_cast<unsigned long>(Int64Arg(val)));
      return true;
    }
    default:
      return false;
  }
}

}  // namespace ctypes
