static int db_sethook (lua_State *L)
{
    int arg, mask, count;
    lua_Hook func;
    lua_State *L1 = getthread(L, &arg);
    if (lua_isnoneornil(L, arg+1)) {
        lua_settop(L, arg+1);
        func = NULL;
        mask = 0;
        count = 0;  /* turn off hooks */
    } else {
        const char *smask = luaL_checkstring(L, arg+2);
        luaL_checkanyfunction(L, arg+1);
        count = luaL_optint(L, arg+3, 0);
        func = hookf;
        mask = makemask(smask, count);
    }
    gethooktable(L);
    lua_pushlightuserdata(L, L1);
    lua_pushvalue(L, arg+1);
    lua_rawset(L, -3);  /* set new hook */
    lua_pop(L, 1);  /* remove hook table */
    lua_sethook(L1, func, mask, count);  /* set hooks */
    return 0;
}
Esempio n. 2
0
// Lua: net.dns.resolve( domain, function(sk, ip) )
static int net_dns_static( lua_State* L ) {
  size_t dl;
  const char* domain = luaL_checklstring(L, 1, &dl);
  if (!domain && dl > 128) {
    return luaL_error(L, "wrong domain");
  }

  luaL_checkanyfunction(L, 2);
  lua_pushvalue(L, 2);  // copy argument (func) to the top of stack
  int cbref = luaL_ref(L, LUA_REGISTRYINDEX);
  if (cbref == LUA_NOREF) {
    return luaL_error(L, "wrong callback");
  }
  int *cbref_ptr = c_zalloc(sizeof(int));
  cbref_ptr[0] = cbref;
  ip_addr_t addr;
  err_t err = dns_gethostbyname(domain, &addr, net_dns_static_cb, cbref_ptr);
  if (err == ERR_OK) {
    net_dns_static_cb(domain, &addr, cbref_ptr);
    return 0;
  } else if (err == ERR_INPROGRESS) {
    return 0;
  } else {
    int e = lwip_lua_checkerr(L, err);
    c_free(cbref_ptr);
    return e;
  }
  return 0;
}
Esempio n. 3
0
static int foreach (lua_State *L) {
  luaL_checktype(L, 1, LUA_TTABLE);
  luaL_checkanyfunction(L, 2);
  lua_pushnil(L);  /* first key */
  while (lua_next(L, 1)) {
    lua_pushvalue(L, 2);  /* function */
    lua_pushvalue(L, -3);  /* key */
    lua_pushvalue(L, -3);  /* value */
    lua_call(L, 2, 1);
    if (!lua_isnil(L, -1))
      return 1;
    lua_pop(L, 2);  /* remove value and result */
  }
  return 0;
}
Esempio n. 4
0
static int foreachi (lua_State *L) {
  int i;
  int n = aux_getn(L, 1);
  luaL_checkanyfunction(L, 2);
  for (i=1; i <= n; i++) {
    lua_pushvalue(L, 2);  /* function */
    lua_pushinteger(L, i);  /* 1st argument */
    lua_rawgeti(L, 1, i);  /* 2nd argument */
    lua_call(L, 2, 1);
    if (!lua_isnil(L, -1))
      return 1;
    lua_pop(L, 1);  /* remove nil result */
  }
  return 0;
}
Esempio n. 5
0
static int tls_socket_on( lua_State *L ) {
  tls_socket_ud *ud = (tls_socket_ud *)luaL_checkudata(L, 1, "tls.socket");
  luaL_argcheck(L, ud, 1, "TLS socket expected");
  if(ud==NULL){
  	NODE_DBG("userdata is nil.\n");
  	return 0;
  }

  size_t sl;
  const char *method = luaL_checklstring( L, 2, &sl );
  if (method == NULL)
    return luaL_error( L, "wrong arg type" );

  luaL_checkanyfunction(L, 3);
  lua_pushvalue(L, 3);  // copy argument (func) to the top of stack

  if (strcmp(method, "connection") == 0) {
    luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_connect_ref);
    ud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  } else if (strcmp(method, "disconnection") == 0) {
    luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_disconnect_ref);
    ud->cb_disconnect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  } else if (strcmp(method, "reconnection") == 0) {
    luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_reconnect_ref);
    ud->cb_reconnect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  } else if (strcmp(method, "receive") == 0) {
    luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_receive_ref);
    ud->cb_receive_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  } else if (strcmp(method, "sent") == 0) {
    luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_sent_ref);
    ud->cb_sent_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  } else if (strcmp(method, "dns") == 0) {
    luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_dns_ref);
    ud->cb_dns_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  } else {
    return luaL_error(L, "invalid method");
  }
  return 0;
}
Esempio n. 6
0
File: mqtt.c Progetto: pvvx/EspLua
// Lua: mqtt:on( "method", function() )
static int mqtt_socket_on( lua_State* L )
{
  NODE_DBG("enter mqtt_socket_on.\n");
  lmqtt_userdata *mud;
  size_t sl;

  mud = (lmqtt_userdata *)luaL_checkudata(L, 1, "mqtt.socket");
  luaL_argcheck(L, mud, 1, "mqtt.socket expected");
  if(mud==NULL){
    NODE_DBG("userdata is nil.\n");
    return 0;
  }

  const char *method = luaL_checklstring( L, 2, &sl );
  if (method == NULL)
    return luaL_error( L, "wrong arg type" );

  luaL_checkanyfunction(L, 3);
  lua_pushvalue(L, 3);  // copy argument (func) to the top of stack

  if( sl == 7 && c_strcmp(method, "connect") == 0){
    if(mud->cb_connect_ref != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_connect_ref);
    mud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  }else if( sl == 7 && c_strcmp(method, "offline") == 0){
    if(mud->cb_disconnect_ref != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_disconnect_ref);
    mud->cb_disconnect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  }else if( sl == 7 && c_strcmp(method, "message") == 0){
    if(mud->cb_message_ref != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_message_ref);
    mud->cb_message_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  }else{
    lua_pop(L, 1);
    return luaL_error( L, "method not supported" );
  }
  NODE_DBG("leave mqtt_socket_on.\n");
  return 0;
}
Esempio n. 7
0
// Lua: socket/udpserver:on( "method", function(s) )
static int net_on( lua_State* L, const char* mt )
{
  NODE_DBG("net_on is called.\n");
  bool isserver = false;
  lnet_userdata *nud;
  size_t sl;
  
  nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  luaL_argcheck(L, nud, 1, "Server/Socket expected");
  if(nud==NULL){
  	NODE_DBG("userdata is nil.\n");
  	return 0;
  }

  if (mt!=NULL && c_strcmp(mt, "net.server")==0)
    isserver = true;
  else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
    isserver = false;
  else
  {
    NODE_DBG("wrong metatable for net_on.\n");
    return 0;
  }

  const char *method = luaL_checklstring( L, 2, &sl );
  if (method == NULL)
    return luaL_error( L, "wrong arg type" );

  luaL_checkanyfunction(L, 3);
  lua_pushvalue(L, 3);  // copy argument (func) to the top of stack

  if(!isserver && nud->pesp_conn->type == ESPCONN_TCP && sl == 10 && c_strcmp(method, "connection") == 0){
    if(nud->cb_connect_ref != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_connect_ref);
    nud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  }else if(!isserver && nud->pesp_conn->type == ESPCONN_TCP && sl == 12 && c_strcmp(method, "reconnection") == 0){
    if(nud->cb_reconnect_ref != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_reconnect_ref);
    nud->cb_reconnect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  }else if(!isserver && nud->pesp_conn->type == ESPCONN_TCP && sl == 13 && c_strcmp(method, "disconnection") == 0){
    if(nud->cb_disconnect_ref != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
    nud->cb_disconnect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  }else if((!isserver || nud->pesp_conn->type == ESPCONN_UDP) && sl == 7 && c_strcmp(method, "receive") == 0){
    if(nud->cb_receive_ref != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_receive_ref);
    nud->cb_receive_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  }else if((!isserver || nud->pesp_conn->type == ESPCONN_UDP) && sl == 4 && c_strcmp(method, "sent") == 0){
    if(nud->cb_send_ref != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_send_ref);
    nud->cb_send_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  }else if(!isserver && nud->pesp_conn->type == ESPCONN_TCP && sl == 3 && c_strcmp(method, "dns") == 0){
    if(nud->cb_dns_found_ref != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref);
    nud->cb_dns_found_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  }else{
  	lua_pop(L, 1);
    return luaL_error( L, "method not supported" );
  }

  return 0;  
}