/*
 * This file is part of the Fun programming language.
 * https://fun-lang.xyz/
 *
 * Copyright 2025 Johannes Findeisen <you@hanez.org>
 * Licensed under the terms of the Apache-2.0 license.
 * https://opensource.org/license/apache-2-0
 */

/**
 * @file socket_close.c
 * @brief Implements OP_SOCK_CLOSE to close a socket file descriptor.
 *
 * Behavior:
 * - Pops fd (int) and closes it; pushes 1 on success, 0 on failure.
 * - On non-UNIX platforms, always pushes 0 (unsupported).
 *
 * Errors:
 * - If argument type is wrong, prints an error and pushes 0.
 */

case OP_SOCK_CLOSE: {
  /* Pops fd; returns 1/0 */
  Value fdv = pop_value(vm);
  int ok = 0;
#ifdef __unix__
  if (fdv.type != VAL_INT) {
    fprintf(stderr, "Runtime type error: sock_close expects (int fd)\n");
    free_value(fdv);
    push_value(vm, make_int(0));
    break;
  }
  int fd = (int)fdv.i;
  ok = (close(fd) == 0) ? 1 : 0;
#endif
  free_value(fdv);
  push_value(vm, make_int(ok));
  break;
}
