Esempio n. 1
0
static int ll_module (lua_State *L) {
  const char *modname = luaL_checkstring(L, 1);
  int loaded;
  if (luaR_findglobal(modname, strlen(modname)))
    return 0;
   loaded = lua_gettop(L) + 1;  /* index of _LOADED table */
  lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  lua_getfield(L, loaded, modname);  /* get _LOADED[modname] */
  if (!lua_istable(L, -1)) {  /* not found? */
    lua_pop(L, 1);  /* remove previous result */
    /* try global variable (and create one if it does not exist) */
    if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL)
      return luaL_error(L, "name conflict for module " LUA_QS, modname);
    lua_pushvalue(L, -1);
    lua_setfield(L, loaded, modname);  /* _LOADED[modname] = new table */
  }
  /* check whether table already has a _NAME field */
  lua_getfield(L, -1, "_NAME");
  if (!lua_isnil(L, -1))  /* is table an initialized module? */
    lua_pop(L, 1);
  else {  /* no; initialize it */
    lua_pop(L, 1);
    modinit(L, modname);
  }
  lua_pushvalue(L, -1);
  setfenv(L);
  dooptions(L, loaded - 1);
  return 0;
}
Esempio n. 2
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;
}
Esempio 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;
}
Esempio n. 4
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;
  }
}