Repository: neovim/neovim
File: src/nvim/lua/executor.c (C)

Code before:
  }
  lua_remove(lstate, top + 1);
  (*nresults)--;
  return true;  // break if true
}

/// "vim.wait(timeout, condition[, interval])" function
static int nlua_wait(lua_State *lstate)
  FUNC_ATTR_NONNULL_ALL
{
  if (in_fast_callback) {
    return luaL_error(lstate, e_fast_api_disabled, "vim.wait");
  }

  double timeout_number = luaL_checknumber(lstate, 1);
  if (timeout_number < 0) {
    return luaL_error(lstate, "timeout must be >= 0");
  }
  int timeout;
  if (isinf(timeout_number)) {
    timeout = INT_MAX;
  } else {
    if (isnan(timeout_number) || timeout_number != trunc(timeout_number)) {
      return luaL_error(lstate, "timeout has no integer representation");
    }
    timeout = (int)timeout_number;
  }

  int lua_top = lua_gettop(lstate);

  // Check if condition can be called.
  bool is_function = false;
  if (lua_top >= 2 && !lua_isnil(lstate, 2)) {
    is_function = (lua_type(lstate, 2) == LUA_TFUNCTION);

    // Check if condition is callable table
    if (!is_function && luaL_getmetafield(lstate, 2, "__call") != 0) {
      is_function = (lua_type(lstate, -1) == LUA_TFUNCTION);
      lua_pop(lstate, 1);
    }

    if (!is_function) {
      lua_pushliteral(lstate, "vim.wait: callback must be callable");
      return lua_error(lstate);
    }
  }

  intptr_t interval = 200;
  if (lua_top >= 3 && !lua_isnil(lstate, 3)) {
    interval = luaL_checkinteger(lstate, 3);
    if (interval < 0) {

Review comment (bug):
oldtest failure. also not sure that casting a double to an int is always valid?

```
[OLDTEST] Running test_perl
/home/runner/work/neovim/neovim/src/nvim/lua/executor.c:466:15: runtime error: 4.29497e+09 is outside the range of representable values of type 'int'
```

Code after:
  }
  lua_remove(lstate, top + 1);
  (*nresults)--;
  return true;  // break if true
}

/// "vim.wait(timeout, condition[, interval])" function
static int nlua_wait(lua_State *lstate)
  FUNC_ATTR_NONNULL_ALL
{
  if (in_fast_callback) {
    return luaL_error(lstate, e_fast_api_disabled, "vim.wait");
  }

  double timeout_number = luaL_checknumber(lstate, 1);
  if (timeout_number < 0) {
    return luaL_error(lstate, "timeout must be >= 0");
  }
  int64_t timeout;
  if (isinf(timeout_number) || timeout_number > (double)INT64_MAX) {
    timeout = INT64_MAX;
  } else {
    if (isnan(timeout_number) || timeout_number != trunc(timeout_number)) {
      return luaL_error(lstate, "timeout has no integer representation");
    }
    timeout = (int64_t)timeout_number;
  }

  int lua_top = lua_gettop(lstate);

  // Check if condition can be called.
  bool is_function = false;
  if (lua_top >= 2 && !lua_isnil(lstate, 2)) {
    is_function = (lua_type(lstate, 2) == LUA_TFUNCTION);

    // Check if condition is callable table
    if (!is_function && luaL_getmetafield(lstate, 2, "__call") != 0) {
      is_function = (lua_type(lstate, -1) == LUA_TFUNCTION);
      lua_pop(lstate, 1);
    }

    if (!is_function) {
      lua_pushliteral(lstate, "vim.wait: callback must be callable");
      return lua_error(lstate);
    }
  }

  intptr_t interval = 200;
  if (lua_top >= 3 && !lua_isnil(lstate, 3)) {
    interval = luaL_checkinteger(lstate, 3);
    if (interval < 0) {