Exemple #1
0
static void lluv_on_process_exit(uv_process_t* arg, int64_t exit_status, int term_signal){
  lluv_handle_t *handle = lluv_handle_byptr((uv_handle_t*)arg);
  lua_State *L = LLUV_HCALLBACK_L(handle);

  LLUV_CHECK_LOOP_CB_INVARIANT(L);

  if(!IS_(handle, OPEN)){
    lluv_handle_unlock(L, handle, LLUV_LOCK_EXIT);

    LLUV_CHECK_LOOP_CB_INVARIANT(L);
    return;
  }

  lua_rawgeti(L, LLUV_LUA_REGISTRY, LLUV_EXIT_CB(handle));
  lluv_handle_pushself(L, handle);
  lluv_handle_unlock(L, handle, LLUV_LOCK_EXIT);

  if(lua_isnil(L, -2)){
    lua_pop(L, 2);

    LLUV_CHECK_LOOP_CB_INVARIANT(L);
    return;
  }

  lua_pushnil(L);
  lutil_pushint64(L, exit_status);
  lutil_pushint64(L, term_signal);

  LLUV_HANDLE_CALL_CB(L, handle, 4);

  LLUV_CHECK_LOOP_CB_INVARIANT(L);
}
Exemple #2
0
void lcurl_push_os_socket(lua_State *L, curl_socket_t fd) {
#if !defined(_WIN32)
  lutil_pushint64(L, fd);
#else /*_WIN32*/
  /* Assumes that compiler can optimize constant conditions. MSVC do this. */

  /*On Lua 5.3 lua_Integer type can be represented exactly*/
#if LUA_VERSION_NUM >= 503
  if (sizeof(curl_socket_t) <= sizeof(lua_Integer)) {
    lua_pushinteger(L, (lua_Integer)fd);
    return;
  }
#endif

#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT)
  /*! @todo test DBL_MANT_DIG, FLT_MANT_DIG */

  if (sizeof(lua_Number) == 8) { /*we have 53 bits for integer*/
    if ((sizeof(curl_socket_t) <= 6)) {
      lua_pushnumber(L, (lua_Number)fd);
      return;
    }

    if(((UINT_PTR)fd & 0x1FFFFFFFFFFFFF) == (UINT_PTR)fd)
      lua_pushnumber(L, (lua_Number)fd);
    else
      lua_pushlightuserdata(L, (void*)fd);

    return;
  }

  if (sizeof(lua_Number) == 4) { /*we have 24 bits for integer*/
    if (((UINT_PTR)fd & 0xFFFFFF) == (UINT_PTR)fd)
      lua_pushnumber(L, (lua_Number)fd);
    else
      lua_pushlightuserdata(L, (void*)fd);
    return;
  }
#endif

  lutil_pushint64(L, fd);
  if (lcurl_opt_os_socket(L, -1, 0) != fd)
    lua_pushlightuserdata(L, (void*)fd);

#endif /*_WIN32*/
}
Exemple #3
0
static int lluv_file_pipe(lua_State *L){
  lluv_file_t *f = lluv_check_file(L, 1, LLUV_FLAG_OPEN);
  int ipc = lua_toboolean(L, 2);

  /*local ok, err = uv.pipe(loop, ipc)*/
  lua_pushvalue(L, LLUV_LUA_REGISTRY);
  lua_pushvalue(L, LLUV_LUA_HANDLES);
  lua_pushcclosure(L, IS_(f, RAISE_ERROR) ? lluv_pipe_create_unsafe : lluv_pipe_create_safe, 2);
  lluv_loop_pushself(L, f->loop);
  lua_pushboolean(L, ipc);
  lua_call(L, 2, 2);

  /*if not ok then return nil, err*/
  if(lua_isnil(L, -2)) return 2;
  lua_pop(L, 1);

  /*local ok, err = pipe:open(fd)*/
  lua_getfield(L, -1, "open");
  assert(lua_isfunction(L, -1));
  lua_pushvalue(L, -2);
  lutil_pushint64(L, f->handle);
  lua_call(L, 2, 2);

  /*if not ok then pipe:close() return nil, err*/
  if(lua_isnil(L, -2)){
    int top = lua_gettop(L);
    lua_getfield(L, -3, "close");
    assert(lua_isfunction(L, -1));
    lua_pushvalue(L, -4);
    lua_pcall(L, 0, 0, 0);
    lua_settop(L, top);
    return 2;
  }
  lua_pop(L, 2);

  UNSET_(f, OPEN);

  return 1;
}
Exemple #4
0
static int lluv_file_fileno(lua_State *L){
  lluv_file_t *f = lluv_check_file(L, 1, LLUV_FLAG_OPEN);
  lutil_pushint64(L, f->handle);
  return 1;
}
Exemple #5
0
static int lluv_push_fs_result(lua_State* L, lluv_fs_request_t* lreq) {
  uv_fs_t *req = &lreq->req;
  /*lluv_loop_t *loop = req->loop->data;*/

  switch (req->fs_type) {
    case UV_FS_RENAME:
    case UV_FS_UNLINK:
    case UV_FS_RMDIR:
    case UV_FS_MKDIR:
    case UV_FS_MKDTEMP:
    case UV_FS_UTIME:
    case UV_FS_CHMOD:
    case UV_FS_LINK:
    case UV_FS_SYMLINK:
    case UV_FS_CHOWN:
      lua_pushstring(L, req->path);
      return 1;

    case UV_FS_CLOSE:
    case UV_FS_FTRUNCATE:
    case UV_FS_FSYNC:
    case UV_FS_FDATASYNC:
    case UV_FS_FUTIME:
    case UV_FS_FCHMOD:
    case UV_FS_FCHOWN:
    case UV_FS_OPEN:
      if(req->path) lua_pushstring(L, req->path);
      else lua_pushboolean(L, 1);
      return 1;

    case UV_FS_SENDFILE:
      lutil_pushint64(L, req->result);
      return 1;

    case UV_FS_STAT:
    case UV_FS_LSTAT:
      lluv_push_stat(L, &req->statbuf);
      lua_pushstring(L, req->path);
      return 2;

    case UV_FS_FSTAT:
      lluv_push_stat(L, &req->statbuf);
      return 1;

    case UV_FS_READLINK:
#if LLUV_UV_VER_GE(1,8,0)
    case UV_FS_REALPATH:
#endif
      lua_pushstring(L, (char*)req->ptr);
      return 1;

    case UV_FS_WRITE:
    case UV_FS_READ:
      lua_rawgetp(L, LLUV_LUA_REGISTRY, req);
      lua_pushnil(L); lua_rawsetp(L, LLUV_LUA_REGISTRY, req);
      lutil_pushint64(L, req->result);
      return 2;

    case UV_FS_SCANDIR:{
      uv_dirent_t ent;
      int i = 0, err;
      lua_createtable(L, (int)req->result, 0);
      lua_createtable(L, (int)req->result, 0);
      while((err = uv_fs_scandir_next(req, &ent)) >= 0){
        lua_pushstring (L, ent.name); lua_rawseti(L, -3, ++i);
#define XX(C,S) case S: lua_pushliteral(L, C); lua_rawseti(L, -2, i); break;
          switch(ent.type){
            LLUV_DIRENT_MAP(XX)
            default: lua_pushstring(L, "unknown"); lua_rawseti(L, -2, i);
          }
#undef XX
      }
      lua_pushstring(L, req->path);
      lua_insert(L, -2);
      return 3;
    }

    case UV_FS_ACCESS:{
      lua_pushboolean(L, req->result == 0);
      lua_pushstring(L, req->path);
      return 2;
    }

#if LLUV_UV_VER_GE(1,14,0)
    case UV_FS_COPYFILE:
      lua_pushboolean(L, req->result == 0);
      return 1;
#endif

    default:
      fprintf(stderr, "UNKNOWN FS TYPE %d\n", req->fs_type);
      return 0;
  }
}
Exemple #6
0
static int lluv_process_pid(lua_State *L){
  lluv_handle_t *handle = lluv_check_process(L, 1, LLUV_FLAG_OPEN);
  lutil_pushint64(L, LLUV_H(handle, uv_process_t)->pid);
  return 1;
}