Esempio n. 1
0
static int lcurl_url_set(lua_State *L, CURLUPart what){
  lcurl_url_t *p = lcurl_geturl(L);
  CURLUcode code;
  const char *part;
  unsigned int flags = 0;
  
  luaL_argcheck(L, lua_type(L, 2) == LUA_TSTRING || lutil_is_null(L, 2), 2, "string expected");

  part = lua_tostring(L, 2);
  flags = (unsigned int)lutil_optint64(L, 3, 0);

  code = curl_url_set(p->url, what, part, flags);
  if (code != CURLUE_OK) {
    return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_URL, code);
  }

  lua_settop(L, 1);
  return 1;
}
Esempio n. 2
0
int lcurl_set_callback(lua_State *L, lcurl_callback_t *c, int i, const char *method){
  int top = lua_gettop(L);
  i = lua_absindex(L, i);

  luaL_argcheck(L, !lua_isnoneornil(L, i), i, "no function present");
  luaL_argcheck(L, (top < (i + 2)), i + 2, "no arguments expected");

  assert((top == i)||(top == (i + 1)));

  if(c->ud_ref != LUA_NOREF){
    luaL_unref(L, LCURL_LUA_REGISTRY, c->ud_ref);
    c->ud_ref = LUA_NOREF;
  }

  if(c->cb_ref != LUA_NOREF){
    luaL_unref(L, LCURL_LUA_REGISTRY, c->cb_ref);
    c->cb_ref = LUA_NOREF;
  }

  if(lutil_is_null(L, i)){
    if(top == (i + 1)){
      // Do we can just ignore this?
      luaL_argcheck(L, 
        lua_isnoneornil(L, i + 1) || lutil_is_null(L, i + 1)
        ,i + 1, "no context allowed when set callback to null"
      );
    }
    lua_pop(L, top - i + 1);

    return 1;
  }

  if(lua_gettop(L) == (i + 1)){// function + context
    c->ud_ref = luaL_ref(L, LCURL_LUA_REGISTRY);
    c->cb_ref = luaL_ref(L, LCURL_LUA_REGISTRY);

    assert(top == (2 + lua_gettop(L)));
    return 1;
  }

  assert(top == i);

  if(lua_isfunction(L, i)){ // function
    c->cb_ref = luaL_ref(L, LCURL_LUA_REGISTRY);

    assert(top == (1 + lua_gettop(L)));
    return 1;
  }

  if(lua_isuserdata(L, i) || lua_istable(L, i)){ // object
    lua_getfield(L, i, method);

    luaL_argcheck(L, lua_isfunction(L, -1), 2, "method not found in object");

    c->cb_ref = luaL_ref(L, LCURL_LUA_REGISTRY);
    c->ud_ref = luaL_ref(L, LCURL_LUA_REGISTRY);

    assert(top == (1 + lua_gettop(L)));
    return 1;
  }

  lua_pushliteral(L, "invalid object type");
  return lua_error(L);
}