Exemplo n.º 1
0
LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
                                       const char *fname, int szhint) {
  const char *e;
  lua_pushvalue(L, idx);
  do {
    e = c_strchr(fname, '.');
    if (e == NULL) e = fname + c_strlen(fname);
    lua_pushlstring(L, fname, e - fname);
    lua_rawget(L, -2);
    if (lua_isnil(L, -1)) {
      /* If looking for a global variable, check the rotables too */
      void *ptable = luaR_findglobal(fname, e - fname);
      if (ptable) {
        lua_pop(L, 1);
        lua_pushrotable(L, ptable);
      }
    }
    if (lua_isnil(L, -1)) {  /* no such field? */
      lua_pop(L, 1);  /* remove this nil */
      lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
      lua_pushlstring(L, fname, e - fname);
      lua_pushvalue(L, -2);
      lua_settable(L, -4);  /* set new table into field */
    }
    else if (!lua_istable(L, -1) && !lua_isrotable(L, -1)) {  /* field has a non-table value? */
      lua_pop(L, 2);  /* remove table and value */
      return fname;  /* return problematic part of the name */
    }
    lua_remove(L, -2);  /* remove previous table */
    fname = e + 1;
  } while (*e == '.');
  return NULL;
}
Exemplo n.º 2
0
LUALIB_API int luaL_rometatable (lua_State *L, const char* tname, void *p) {
  lua_getfield(L, LUA_REGISTRYINDEX, tname);  /* get registry.name */
  if (!lua_isnil(L, -1))  /* name already in use? */
    return 0;  /* leave previous value on top, but return 0 */
  lua_pop(L, 1);
  lua_pushrotable(L, p);
  lua_pushvalue(L, -1);
  lua_setfield(L, LUA_REGISTRYINDEX, tname);  /* registry.name = metatable */
  return 1;
}
Exemplo n.º 3
0
static int ll_require (lua_State *L) {
    const char *name = luaL_checkstring(L, 1);
    int i;
    lua_settop(L, 1);  /* _LOADED table will be at index 2 */
    lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
    lua_getfield(L, 2, name);
    if (lua_toboolean(L, -1)) {  /* is it there? */
        if (lua_touserdata(L, -1) == sentinel)  /* check loops */
            luaL_error(L, "loop or previous error loading module " LUA_QS, name);
        return 1;  /* package is already loaded */
    }
    /* Is this a readonly table? */
    {
        void *res = luaR_findglobal(name, strlen(name));
        if (res) {
            lua_pushrotable(L, res);
            return 1;
        }
    }
    /* else must load it; iterate over available loaders */
    lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
    if (!lua_istable(L, -1))
        luaL_error(L, LUA_QL("package.loaders") " must be a table");
    lua_pushliteral(L, "");  /* error message accumulator */
    for (i = 1; ; i++) {
        lua_rawgeti(L, -2, i);  /* get a loader */
        if (lua_isnil(L, -1))
            luaL_error(L, "module " LUA_QS " not found:%s",
                       name, lua_tostring(L, -2));
        lua_pushstring(L, name);
        lua_call(L, 1, 1);  /* call it */
        if (lua_isfunction(L, -1))  /* did it find module? */
            break;  /* module loaded successfully */
        else if (lua_isstring(L, -1))  /* loader returned error message? */
            lua_concat(L, 2);  /* accumulate it */
        else
            lua_pop(L, 1);
    }
    lua_pushlightuserdata(L, sentinel);
    lua_setfield(L, 2, name);  /* _LOADED[name] = sentinel */
    lua_pushstring(L, name);  /* pass name as argument to module */
    lua_call(L, 1, 1);  /* run loaded module */
    if (!lua_isnil(L, -1))  /* non-nil return? */
        lua_setfield(L, 2, name);  /* _LOADED[name] = returned value */
    lua_getfield(L, 2, name);
    if (lua_touserdata(L, -1) == sentinel) {   /* module did not set a value? */
        lua_pushboolean(L, 1);  /* use true as result */
        lua_pushvalue(L, -1);  /* extra copy to be returned */
        lua_setfield(L, 2, name);  /* _LOADED[name] = true */
    }
    return 1;
}
Exemplo n.º 4
0
/*
** Open string library
*/
LUALIB_API int luaopen_string (lua_State *L) {
#if LUA_OPTIMIZE_MEMORY == 0
  luaL_register(L, LUA_STRLIBNAME, strlib);
#if defined(LUA_COMPAT_GFIND)
  lua_getfield(L, -1, "gmatch");
  lua_setfield(L, -2, "gfind");
#endif
  createmetatable(L);
  return 1;
#else
  lua_pushliteral(L,"");
  lua_pushrotable(L, (void*)strlib);
  lua_setmetatable(L, -2);
  lua_pop(L,1);
  return 0;  
#endif
}
Exemplo n.º 5
0
static int luaB_index(lua_State *L) {
#if LUA_OPTIMIZE_MEMORY == 2
  int fres;
  if ((fres = luaR_findfunction(L, base_funcs_list)) != 0)
    return fres;
#endif  
  const char *keyname = luaL_checkstring(L, 2);
  if (!c_strcmp(keyname, "_VERSION")) {
    lua_pushliteral(L, LUA_VERSION);
    return 1;
  }
  void *res = luaR_findglobal(keyname, c_strlen(keyname));
  if (!res)
    return 0;
  else {
    lua_pushrotable(L, res);
    return 1;
  }
}