Beispiel #1
0
static int
ts_lua_get_shared_dict(lua_State *L)
{
    char                    c;
    int                     n, i, option;
    int64_t                 quota;
    const char              *name, *opt;
    size_t                  name_len, opt_len;
    ts_lua_shared_dict      *dct;
    ts_lua_instance_conf    *conf;

    n = lua_gettop(L);
    conf = ts_lua_get_instance_conf(L);

    if (n < 1) {
        return luaL_error(L, "ts.shared.DICT expecting name argument.");
    }

    name = lua_tolstring(L, 1, &name_len);

    if (name == NULL || name_len == 0) {
        return luaL_error(L, "The 1st param of ts.shared.DICT should be string");

    } else if (name_len >= TS_LUA_MAX_SHARED_DICT_NAME_LENGTH - 4) {
        return luaL_error(L, "length of name for ts.shared.DICT is more than %d",
                          TS_LUA_MAX_SHARED_DICT_NAME_LENGTH);
    }

    for (i = 0; i < conf->shdict_n; i++) {

        dct = &conf->shdict[i];

        if (strlen(dct->name) == name_len &&
                memcmp(dct->name, name, name_len) == 0) {

            lua_pushlightuserdata(L, dct);
            ts_lua_shared_dict_set_metatable(L);
            return 1;
        }
    }

    if (conf->shdict_n >= TS_LUA_MAX_SHARED_DICT_COUNT)
        return luaL_error(L, "There are too many shared dicts.");

    option = quota = 0;

    if (n == 2) {
        if (!lua_istable(L, 2)) {
            return luaL_error(L, "The 2nd param of ts.shared.DICT should be a table.");
        }
        
        /* quota */
        lua_pushlstring(L, "quota", sizeof("quota") - 1);
        lua_gettable(L, 2);

        if (lua_isnil(L, -1)) {
            quota = 0;

        } else if (lua_isnumber(L, -1)) {
            quota = lua_tonumber(L, -1);

        } else {
            return luaL_error(L, "ts.shared.DICT: 'quota' is invalid");
        }

        lua_pop(L, 1);

        /* option */
        lua_pushlstring(L, "options", sizeof("options") - 1);
        lua_gettable(L, 2);
        
        if (lua_isstring(L, -1)) {
            opt = luaL_checklstring(L, -1, &opt_len);
            i = 0;

            while (i < opt_len) {
                c = *(opt + i);

                switch (c) {

                    case 'i':
                        option |= TS_LUA_SHDICT_FLAG_INTKEY;
                        break;

                    case 's':
                        option |= TS_LUA_SHDICT_FLAG_STATIC;
                        break;

                    default:
                        break;
                }

                i++;
            }
        }
    }

    dct = &(conf->shdict[conf->shdict_n++]);

    /* basic set */
    snprintf(dct->name, TS_LUA_MAX_SHARED_DICT_NAME_LENGTH - 4, "%.*s", (int)name_len, name);
    dct->quota = quota;
    dct->flags = option;

    /* init hashtable */
    if (option & TS_LUA_SHDICT_FLAG_INTKEY) {
        Tcl_InitHashTable(&(dct->map.t), TCL_ONE_WORD_KEYS);

    } else {
        Tcl_InitHashTable(&(dct->map.t), TCL_STRING_KEYS);
    }

    dct->map.mutexp = TSMutexCreate();

    /* return to lua */
    lua_pushlightuserdata(L, dct);
    ts_lua_shared_dict_set_metatable(L);

    return 1;
}
static int
ts_lua_add_package_path(lua_State *L)
{
  ts_lua_instance_conf *conf;
  const char *data;
  const char *ptr, *end, *hit;
  size_t dlen;
  int i, n;
  size_t item_len;
  ts_lua_package_path pp[TS_LUA_MAX_PACKAGE_NUM];
  ts_lua_package_path *elt;

  conf = ts_lua_get_instance_conf(L);
  if (conf == NULL) {
    return luaL_error(L, "cann't get the instance conf.");
  }

  data = luaL_checklstring(L, 1, &dlen);
  end = data + dlen;

  ptr = data;
  n = 0;

  while (ptr < end) {
    hit = memchr(ptr, ';', end - ptr);

    if (hit) {
      item_len = hit - ptr;

    } else {
      item_len = end - ptr;
    }

    if (item_len > 0) {
      for (i = 0; i < g_path_cnt; i++) {
        if (g_path[i].len == item_len && memcmp(g_path[i].name, ptr, item_len) == 0) // exist
        {
          break;
        }
      }

      if (i >= g_path_cnt) {
        if (n + i >= TS_LUA_MAX_PACKAGE_NUM)
          return luaL_error(L, "extended package path number exceeds %d.", TS_LUA_MAX_PACKAGE_NUM);

        pp[n].name = (char *)ptr;
        pp[n].len = item_len;
        n++;
      }
    }

    ptr += item_len + 1; // ??
  }

  if (n > 0) {
    ts_lua_add_package_path_items(L, pp, n);

    if (conf->_last) {
      elt = &g_path[g_path_cnt];

      for (i = 0; i < n; i++) {
        elt->len = pp[i].len;
        elt->name = (char *)TSmalloc(pp[i].len);
        memcpy(elt->name, pp[i].name, pp[i].len);
        elt++;
      }

      g_path_cnt += n;
    }
  }

  return 0;
}