Example #1
0
COMPAT53_API void luaL_requiref (lua_State *L, const char *modname,
                                 lua_CFunction openf, int glb) {
  luaL_checkstack(L, 3, "not enough stack slots available");
  luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
  if (lua_getfield(L, -1, modname) == LUA_TNIL) {
    lua_pop(L, 1);
    lua_pushcfunction(L, openf);
    lua_pushstring(L, modname);
    lua_call(L, 1, 1);
    lua_pushvalue(L, -1);
    lua_setfield(L, -3, modname);
  }
  if (glb) {
    lua_pushvalue(L, -1);
    lua_setglobal(L, modname);
  }
  lua_replace(L, -2);
}
Example #2
0
static int str_byte (lua_State *L)
{
	size_t l;
	const char *s = luaL_checklstring(L, 1, &l);
	ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
	ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
	int n, i;
	if (posi <= 0) posi = 1;
	if ((size_t)pose > l) pose = l;
	if (posi > pose) return 0;  /* empty interval; return no values */
	n = (int)(pose -  posi + 1);
	if (posi + n <= pose)  /* overflow? */
		luaL_error(L, "string slice too long");
	luaL_checkstack(L, n, "string slice too long");
	for (i=0; i<n; i++)
		lua_pushinteger(L, uchar(s[posi+i-1]));
	return n;
}
Example #3
0
static int
elua_getargs(lua_State *L, int argc, char **argv, int n)
{
   int i;
   int narg = argc - (n + 1);
   luaL_checkstack(L, narg + 3, "too many arguments to script");
   for (i = n + 1; i < argc; ++i)
     {
        lua_pushstring(L, argv[i]);
     }
   lua_createtable(L, narg, n + 1);
   for (i = 0; i < argc; ++i)
     {
        lua_pushstring(L, argv[i]);
        lua_rawseti(L, -2, i - n);
     }
   return narg;
}
Example #4
0
/*
** Reader for generic `load' function: `lua_load' uses the
** stack for internal stuff, so the reader cannot change the
** stack top. Instead, it keeps its resulting string in a
** reserved slot inside the stack.
*/
static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
  (void)ud;  /* to avoid warnings */
  if (L == NULL && size == NULL) // direct mode check, doesn't happen
    return NULL;
  luaL_checkstack(L, 2, "too many nested functions");
  lua_pushvalue(L, 1);  /* get function */
  lua_call(L, 0, 1);  /* call it */
  if (lua_isnil(L, -1)) {
    *size = 0;
    return NULL;
  }
  else if (lua_isstring(L, -1)) {
    lua_replace(L, 3);  /* save string in a reserved stack slot */
    return lua_tolstring(L, 3, size);
  }
  else luaL_error(L, "reader function must return a string");
  return NULL;  /* to avoid warnings */
}
Example #5
0
/*
 * test if the giving lua table is a dense array
 * return array length if true else -1
 */
static int
strict_array_length(lua_State *L, int idx)
{
    luaL_checkstack(L, 2, "No more extra lua stack");
    abs_idx(L, idx);
    int len = 0;

    for (lua_pushnil(L); lua_next(L, idx); lua_pop(L, 1)) {
        len++;

        if (lua_type(L, -2) != LUA_TNUMBER || lua_tointeger(L, -2) != len) {
            lua_pop(L, 2);
            return -1;
        }
    }

    return len;
}
Example #6
0
/* Note: upvalues are assumed to be after first two slots. */
static void push_LCL(lua_State *L, Proto *pt, Table *env)
{
  Closure *cl;
  int i, nup = pt->nups;
  /* Adjust the number of stack slots to the number of upvalues. */
  luaL_checkstack(L, nup, "too many upvalues");
  lua_settop(L, 2+nup);
  /* Create a closure from the subroutine prototype. */
  cl = luaF_newLclosure(L, nup, env);
  cl->l.p = pt;
  /* Allocate new upvalues and close them. */
  for (i = 0; i < nup; i++)
    cl->l.upvals[i] = luaF_findupval(L, L->base + (2+i));
  luaF_close(L, L->base + 2);
  lua_settop(L, 2);  /* Remove upvalues. */
  setclvalue(L, L->top++, cl);  /* Return closure on top of stack. */
  luaC_checkGC(L);
}
Example #7
0
static void push_nif_ref(lua_State* lua, ERL_NIF_TERM message, ErlNifEnv* env)
{
	(void)env; // unused
	const int top = lua_gettop(lua);

	luaL_checkstack(lua, 2, ERROR_STACK_MESSAGE);

	erlref_ptr erlref = (erlref_ptr) lua_newuserdata( lua, sizeof(struct erlref) );
	memset(erlref, 0, sizeof(struct erlref));

	erlref->env = enif_alloc_env();
	erlref->reference = enif_make_copy( erlref->env, message);

	luaL_getmetatable(lua, TYPE_ERL_REF);
	lua_setmetatable(lua, -2);

	assert(lua_gettop(lua) == top+1);
}
Example #8
0
void CUnsyncedLuaHandle::RecvFromSynced(lua_State* srcState, int args)
{
	if (!IsValid())
		return;


	LUA_CALL_IN_CHECK(L);
	luaL_checkstack(L, 2 + args, __FUNCTION__);

	static const LuaHashString cmdStr("RecvFromSynced");
	if (!cmdStr.GetGlobalFunc(L))
		return; // the call is not defined

	LuaUtils::CopyData(L, srcState, args);

	// call the routine
	RunCallIn(L, cmdStr, args, 0);
}
Example #9
0
static int getargs(lua_State *L, char **argv, int n)
{
    int narg;
    int i;
    int argc = 0;
    while(argv[argc])
        argc++;            /* count total number of arguments */
    narg = argc - (n + 1); /* number of arguments to the script */
    luaL_checkstack(L, narg + 3, "too many arguments to script");
    for(i = n + 1; i < argc; i++)
        lua_pushstring(L, argv[i]);
    lua_createtable(L, narg, n + 1);
    for(i = 0; i < argc; i++) {
        lua_pushstring(L, argv[i]);
        lua_rawseti(L, -2, i - n);
    }
    return narg;
}
Example #10
0
File: lbind.c Project: ifzz/nui
LB_API int lbind_newmetatable(lua_State *L, luaL_Reg *libs, const lbind_Type *t) {
  if (type_exists(L, t)) return 0;

  lua_createtable(L, 0, 8);
  if (libs != NULL)
    luaL_setfuncs(L, libs, 0);

  /* init type metatable */
  lua_pushlightuserdata(L, (void*)t);
  lua_setfield(L, -2, "__type");

  if (!lbind_hasfield(L, -1, "__gc")) {
    lua_pushcfunction(L, Lgc);
    lua_setfield(L, -2, "__gc");
  }

  if (!lbind_hasfield(L, -1, "__tostring")) {
    lua_pushcfunction(L, Ltostring);
    lua_setfield(L, -2, "__tostring");
  }

  if (t->bases != NULL && t->bases[0] != NULL) {
    int nups = 0;
    int freeslots = 0;
    lbind_Type **bases = t->bases;
    for (; *bases != NULL; ++nups, ++bases) {
      if (nups > freeslots) {
        luaL_checkstack(L, 10, "no space for base types");
        freeslots += 10;
      }
      if (!lbind_getmetatable(L, *bases))
        lua_pushlightuserdata(L, *bases);
    }
    lbind_setindexf(L, nups);
  }

  else if (!lbind_hasfield(L, -1, "__index")) {
    lua_pushvalue(L, -1);
    lua_setfield(L, -2, "__index");
  }

  register_type(L, t->name, (const void*)t);
  return 1;
}
static int g_read (lua_State *L, FILE *f, int first) {
  int nargs = lua_gettop(L) - 1;
  int success;
  int n;
  if (nargs == 0) {  /* no arguments? */
    success = read_line(L, f);
    n = first+1;  /* to return 1 result */
  }
  else {  /* ensure stack space for all results and for auxlib's buffer */
    luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
    success = 1;
    for (n = first; nargs-- && success; n++) {
      if (lua_type(L, n) == LUA_TNUMBER) {
        size_t l = (size_t)lua_tonumber(L, n);
        success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
      }
      else {
        const char *p = lua_tostring(L, n);
        luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
        switch (p[1]) {
          case 'n':  /* number */
            success = read_number(L, f);
            break;
          case 'l':  /* line */
            success = read_line(L, f);
            break;
          case 'a':  /* file */
            read_chars(L, f, ~((size_t)0));  /* read MAX_SIZE_T chars */
            success = 1; /* always success */
            break;
          case 'w':  /* word */
            return luaL_error(L, "obsolete option `*w' to `read'");
          default:
            return luaL_argerror(L, n, "invalid format");
        }
      }
    }
  }
  if (!success) {
    lua_pop(L, 1);  /* remove last result */
    lua_pushnil(L);  /* push nil instead */
  }
  return n - first;
}
Example #12
0
void CLuaIntro::DrawLoadScreen()
{
	LUA_CALL_IN_CHECK(L);
	luaL_checkstack(L, 2, __FUNCTION__);
	static const LuaHashString cmdStr("DrawLoadScreen");
	if (!cmdStr.GetGlobalFunc(L)) {
		//LuaOpenGL::DisableCommon(LuaOpenGL::DRAW_SCREEN);
		return; // the call is not defined
	}

	LuaOpenGL::SetDrawingEnabled(L, true);
	LuaOpenGL::EnableCommon(LuaOpenGL::DRAW_SCREEN);

	// call the routine
	RunCallIn(L, cmdStr, 0, 0);

	LuaOpenGL::DisableCommon(LuaOpenGL::DRAW_SCREEN);
	LuaOpenGL::SetDrawingEnabled(L, false);
}
Example #13
0
static inline int push_nif_number(lua_State* lua, ERL_NIF_TERM message, ErlNifEnv* env)
{
	const int top = lua_gettop(lua);

	int result = 0;

    int intval;
    unsigned int uintval;
    long longval;
    unsigned long ulongval;
    double doubleval;

	luaL_checkstack(lua, 1, ERROR_STACK_MESSAGE);
    if(enif_get_int(env, message, &intval))
    {
    	lua_pushnumber(lua, (double) intval);
        result = 1;
    }
    else if(enif_get_uint(env, message, &uintval))
    {
    	lua_pushnumber(lua, (double) uintval);
        result = 1;
    }
    else if(enif_get_long(env, message, &longval))
    {
    	lua_pushnumber(lua, (double) longval);
        result = 1;
    }
    else if(enif_get_ulong(env, message, &ulongval))
    {
    	lua_pushnumber(lua, (double) ulongval);
        result = 1;
    }
    else if(enif_get_double(env, message, &doubleval))
    {
    	lua_pushnumber(lua, (double) doubleval);
        result = 1;
    }

	assert(lua_gettop(lua) == top+result);

	return result;
}
Example #14
0
static int icu_ufile_read(lua_State *L) {
	int nargs = lua_gettop(L) - 1;
	int success;
	int n;
	UFILE* ufile = icu4lua_checkufile(L,1,UFILE_UV_META);
	if (nargs == 0) { // no arguments?
		success = read_line(L, ufile);
		n = 3; // to return 1 result
	}
	else {
		luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
		success = 1;
		for (n = 2; nargs-- && success; n++) {
			if (lua_type(L,n) == LUA_TNUMBER) {
				int32_t l = (int32_t)lua_tointeger(L, n);
				success = (l == 0) ? test_eof(L, ufile) : read_chars(L, ufile, l);
			}
			else {
				const char* p = lua_tostring(L, n);
				luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
				switch(p[1]) {
					case 'n': // number
						success = read_number(L, ufile);
						break;
					case 'l': // line
						success = read_line(L, ufile);
						break;
					case 'a': // file
						read_chars(L, ufile, INT32_MAX);
						success = 1;
						break;
					default:
						return luaL_argerror(L, n, "invalid format");
				}
			}
		}
	}
	if (!success) {
		lua_pop(L,1); // remove last result
		lua_pushnil(L); // push nil instead
	}
	return n - 2;
}
Example #15
0
bool CLuaRules::AllowWeaponTarget(
	unsigned int attackerID,
	unsigned int targetID,
	unsigned int attackerWeaponNum,
	unsigned int attackerWeaponDefID,
	float* targetPriority)
{
	assert(targetPriority != NULL);

	bool ret = true;

	if (!haveAllowWeaponTarget)
		return ret;
	if (!watchWeaponDefs[attackerWeaponDefID])
		return ret;

	LUA_CALL_IN_CHECK(L, true);
	luaL_checkstack(L, 2 + 5 + 2, __FUNCTION__);

	static const LuaHashString cmdStr(__FUNCTION__);
	const LuaUtils::ScopedDebugTraceBack traceBack(L);

	if (cmdStr.GetGlobalFunc(L)) {
		lua_pushnumber(L, attackerID);
		lua_pushnumber(L, targetID);
		lua_pushnumber(L, attackerWeaponNum);
		lua_pushnumber(L, attackerWeaponDefID);
		lua_pushnumber(L, *targetPriority);

		if (!RunCallInTraceback(cmdStr, 5, 2, traceBack.GetErrFuncIdx(), false))
			return ret;

		ret = (lua_isboolean(L, -2) && lua_toboolean(L, -2));

		if (lua_isnumber(L, -1)) {
			*targetPriority = lua_tonumber(L, -1);
		}

		lua_pop(L, 2);
	}

	return ret;
}
Example #16
0
static int lstatus_index(lua_State * L)
{
  size_t key_len = 0;
  const char * key = NULL;
  luaL_checktype(L, 1, LUA_TTABLE);
  key = luaL_checklstring(L, 2, &key_len);

  push_new_const(
      L, key, key_len, REDIS_REPLY_STATUS /* status */
    );
  lua_rawset(L, 1); /* t[key] = status */

  luaL_checkstack(L, 1, "not enough stack");

  lua_pushlstring(L, key, key_len); /* Push the key again */
  lua_gettable(L, 1); /* return t[key] */

  return 1;
}
Example #17
0
static void
mark_table(lua_State *L, lua_State *dL, const void * parent, const char * desc) {
	const void * t = readobject(L, dL, parent, desc);
	if (t == NULL)
		return;

	bool weakk = false;
	bool weakv = false;
	if (lua_getmetatable(L, -1)) {
		lua_pushliteral(L, "__mode");
		lua_rawget(L, -2);
		if (lua_isstring(L,-1)) {
			const char *mode = lua_tostring(L, -1);
			if (strchr(mode, 'k')) {
				weakk = true;
			}
			if (strchr(mode, 'v')) {
				weakv = true;
			}
		}
		lua_pop(L,1);

        luaL_checkstack(L, LUA_MINSTACK, NULL);
		mark_table(L, dL, t, "[metatable]");
	}

	lua_pushnil(L);
	while (lua_next(L, -2) != 0) {
		if (weakv) {
			lua_pop(L,1);
		} else {
			char temp[32];
			const char * desc = keystring(L, -2, temp);
			mark_object(L, dL, t , desc);
		}
		if (!weakk) {
			lua_pushvalue(L,-1);
			mark_object(L, dL, t , "[key]");
		}
	}

	lua_pop(L,1);
}
Example #18
0
bool CSyncedLuaHandle::CommandFallback(const CUnit* unit, const Command& cmd)
{
	LUA_CALL_IN_CHECK(L, true);
	luaL_checkstack(L, 9, __FUNCTION__);

	static const LuaHashString cmdStr(__FUNCTION__);
	if (!cmdStr.GetGlobalFunc(L))
		return true; // the call is not defined

	PushUnitAndCommand(L, unit, cmd);

	// call the function
	if (!RunCallIn(L, cmdStr, 7, 1))
		return true;

	const bool retval = luaL_optboolean(L, -1, true);
	lua_pop(L, 1);
	return retval; // return 'true' to remove the command
}
Example #19
0
static int compress_cb(lua_State* L)
{
	size_t srcsize;
	const char* srcbuffer = luaL_checklstring(L, 1, &srcsize);

	int outputchunks = 0;
	uint8_t outputbuffer[64*1024];

	z_stream zs = {0};
	int i = deflateInit(&zs, 1);
	if (i != Z_OK)
		return 0;

	zs.avail_in = srcsize;
	zs.next_in = (uint8_t*) srcbuffer;

	luaL_checkstack(L, STACKSIZE, "out of memory");
	do
	{
		zs.avail_out = sizeof(outputbuffer);
		zs.next_out = outputbuffer;

		i = deflate(&zs, Z_FINISH);

		int have = sizeof(outputbuffer) - zs.avail_out;
		lua_pushlstring(L, (char*) outputbuffer, have);
		outputchunks++;

		if (outputchunks == (STACKSIZE-1))
		{
			/* Stack full! Concatenate what we've got, to empty the stack, and
			 * keep going. This will only happen on very large input files. */
			lua_concat(L, outputchunks);
			outputchunks = 1;
		}
	}
	while (i != Z_STREAM_END);

	(void)deflateEnd(&zs);

	lua_concat(L, outputchunks);
	return 1;
}
Example #20
0
static int lzstream_decompress(lua_State *L) {
    lz_stream *s = lzstream_check(L, 1, LZ_INFLATE);
    int nargs = lua_gettop(L) - 1;
    int success;
    int n;
    if (nargs == 0) {  /* no arguments? */
        success = lz_read_line(L, s);
        n = 3;  /* to return 1 result */
    }
    else {  /* ensure stack space for all results and for auxlib's buffer */
        luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
        success = 1;
        for (n = 2; nargs-- && success; n++) {
            if (lua_type(L, n) == LUA_TNUMBER) {
                size_t l = (size_t)lua_tointeger(L, n);
                success = (l == 0) ? lz_test_eof(L, s) : lz_read_chars(L, s, l);
            }
            else {
                const char *p = lua_tostring(L, n);
                luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
                switch (p[1]) {
                    case 'l':  /* line */
                        success = lz_read_line(L, s);
                        break;
                    case 'a':  /* file */
                        lz_read_chars(L, s, ~((size_t)0));  /* read MAX_SIZE_T chars */
                        success = 1; /* always success */
                        break;
                    default:
                        return luaL_argerror(L, n, "invalid format");
                }
            }
        }
    }
    if (s->error != Z_OK) {
        return lz_pushresult(L, s);
    }
    if (!success) {
        lua_pop(L, 1);  /* remove last result */
        lua_pushnil(L);  /* push nil instead */
    }
    return n - 2;
}
Example #21
0
static void push_nif_list(lua_State* lua, ERL_NIF_TERM list, ErlNifEnv* env, ErlNifResourceType* resource_type)
{
	const int top = lua_gettop(lua);

	int map_count = 0;
	int array_count = 0;
	if(enif_is_list(env, list))
	{
		ERL_NIF_TERM head;
		ERL_NIF_TERM tail = list;
		while(enif_get_list_cell(env, tail, &head, &tail))
		{
			if(push_table_iskvp(env, head, NULL))
			{
				++map_count;
			}
			else
			{
				++array_count;
			}
		}

		luaL_checkstack(lua, 1, ERROR_STACK_MESSAGE);
		lua_createtable(lua, array_count, map_count);

		ERL_NIF_TERM array_items[array_count + map_count];

		tail = list;
		int index = 0;
		while(enif_get_list_cell(env, tail, &head, &tail))
		{
			if(!push_table_set(lua, env, head, resource_type))
			{
				array_items[index++] = head;
			}
		}

		push_table_append(lua, env, resource_type, array_items, index);
	}

	assert(lua_gettop(lua) == top+1);
}
Example #22
0
static void buffer_pop_table(lua_State *L, struct buffer *b) {
	lua_Integer *v = (lua_Integer *)buffer_pop(b, sizeof(lua_Integer));
	int array_size = (int)*v;
	int i;
	luaL_checkstack(L, LUA_MINSTACK, 0);
	lua_createtable(L, array_size, 0);
	for (i=1; i<=array_size; i++) {
		lunpack_one(L, b);
		lua_rawseti(L, -2, i);
	}
	for (;;) {
		lunpack_one(L, b);
		if (lua_isnil(L, -1)) {
			lua_pop(L, 1);
			return;
		}
		lunpack_one(L, b);
		lua_rawset(L, -3);
	}
}
Example #23
0
// table must be on stack to which you want to set this function
void luastate_SetGCCallback(void* luastate, int tablestackindex,
int (*callback)(void*)) {
    lua_State* l = luastate;
    luaL_checkstack(l, 5,
    "insufficient stack to set gc callback on metatable");
    if (!lua_getmetatable(l, tablestackindex)) {
        lua_newtable(l);
    } else {
        if (lua_type(l, -1) != LUA_TTABLE) {
            lua_pop(l, 1);
            lua_newtable(l);
        }
    }
    lua_pushstring(l, "__gc");
    lua_pushcfunction(l, (lua_CFunction)callback);
    lua_rawset(l, -3);
    if (tablestackindex < 0) {tablestackindex--;} // metatable is now at the top
    lua_setmetatable(l, tablestackindex);
    if (tablestackindex < 0) {tablestackindex++;}
}
Example #24
0
static void download_callback(struct wait_id id __attribute__((unused)), void *data, int status, size_t out_size, const char *out) {
	struct lua_download_data *d = data;
	struct lua_State *L = d->L;
	ASSERT(L);
	// This may be called from C code with a dirty stack
	luaL_checkstack(L, 4, "Not enough stack space to call download callback");
	int handler = push_err_handler(L);
	// Get the lua function.
	ASSERT(d->callback);
	extract_registry_value(L, d->callback);
	/*
	 * We terminated the command, we won't need it any more.
	 * Make sure we don't leak even if the lua throws or whatever.
	 */
	free(d);
	lua_pushinteger(L, status);
	lua_pushlstring(L, out, out_size);
	int result = lua_pcall(L, 2, 0, handler);
	ASSERT_MSG(!result, "%s", interpreter_error_result(L));
}
Example #25
0
void lua_getuservalue (lua_State *L, int i) {
  luaL_checktype(L, i, LUA_TUSERDATA);
  luaL_checkstack(L, 2, "not enough stack slots");
  lua_getfenv(L, i);
  lua_pushvalue(L, LUA_GLOBALSINDEX);
  if (lua_rawequal(L, -1, -2)) {
    lua_pop(L, 1);
    lua_pushnil(L);
    lua_replace(L, -2);
  } else {
    lua_pop(L, 1);
    push_package_table(L);
    if (lua_rawequal(L, -1, -2)) {
      lua_pop(L, 1);
      lua_pushnil(L);
      lua_replace(L, -2);
    } else
      lua_pop(L, 1);
  }
}
Example #26
0
static void command_terminated(struct wait_id id __attribute__((unused)), void *data, int status, enum command_kill_status killed, size_t out_size, const char *out, size_t err_size, const char *err) {
	struct lua_command_data *lcd = data;
	struct lua_State *L = lcd->L;
	ASSERT(L);
	// This may be called from C code with a dirty stack
	luaL_checkstack(L, 6, "Not enough stack space to call command callback");
	int handler = push_err_handler(L);
	if (lcd->postfork_callback) {
		/*
		 * This already happened in the child. But we need to free
		 * resources ‒ remove it from the registry table.
		 */
		extract_registry_value(L, lcd->postfork_callback);
		lua_pop(L, 1);
	}
	// Get the lua function.
	ASSERT(lcd->terminated_callback);
	extract_registry_value(L, lcd->terminated_callback);
	/*
	 * We terminated the command, we won't need it any more.
	 * Make sure we don't leak even if the lua throws or whatever.
	 */
	free(lcd);
	// Push the rest of parameters here
	lua_pushinteger(L, WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status));
	const char *ks = NULL;
	switch (killed) {
#define KS(NAME) case CK_##NAME: ks = #NAME; break
		KS(TERMINATED);
		KS(TERMED);
		KS(KILLED);
		KS(SIGNAL_OTHER);
#undef KS
	}
	ASSERT(ks);
	lua_pushstring(L, ks);
	lua_pushlstring(L, out, out_size);
	lua_pushlstring(L, err, err_size);
	int result = lua_pcall(L, 4, 0, handler);
	ASSERT_MSG(!result, "%s", interpreter_error_result(L));
}
Example #27
0
static void dotty (lua_State *L) {
  int status;
  const char *oldprogname = progname;
  progname = NULL;
  while ((status = loadline(L)) != -1) {
    if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET);
    report(L, status);
    if (status == LUA_OK && lua_gettop(L) > 0) {  /* any result to print? */
      luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
      lua_getglobal(L, "print");
      lua_insert(L, 1);
      if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK)
        l_message(progname, lua_pushfstring(L,
                               "error calling " LUA_QL("print") " (%s)",
                               lua_tostring(L, -1)));
    }
  }
  lua_settop(L, 0);  /* clear stack */
  luai_writeline();
  progname = oldprogname;
}
Example #28
0
static int luaB_call (lua_State *L)
{
    int oldtop;
    const char *options = luaL_opt_string(L, 3, "");
    int err = 0;  /* index of old error method */
    int i, status;
    int n;
    luaL_checktype(L, 2, LUA_TTABLE);
    n = lua_getn(L, 2);
    if (!lua_isnull(L, 4))    /* set new error method */
    {
        lua_getglobal(L, LUA_ERRORMESSAGE);
        err = lua_gettop(L);  /* get index */
        lua_pushvalue(L, 4);
        lua_setglobal(L, LUA_ERRORMESSAGE);
    }
    oldtop = lua_gettop(L);  /* top before function-call preparation */
    /* push function */
    lua_pushvalue(L, 1);
    luaL_checkstack(L, n, "too many arguments");
    for (i=0; i<n; i++)  /* push arg[1...n] */
        lua_rawgeti(L, 2, i+1);
    status = lua_call(L, n, LUA_MULTRET);
    if (err != 0)    /* restore old error method */
    {
        lua_pushvalue(L, err);
        lua_setglobal(L, LUA_ERRORMESSAGE);
    }
    if (status != 0)    /* error in call? */
    {
        if (strchr(options, 'x'))
            lua_pushnil(L);  /* return nil to signal the error */
        else
            lua_error(L, NULL);  /* propagate error without additional messages */
        return 1;
    }
    if (strchr(options, 'p'))  /* pack results? */
        lua_error(L, "deprecated option `p' in `call'");
    return lua_gettop(L) - oldtop;  /* results are already on the stack */
}
Example #29
0
static void 
mark_object(lua_State *L, lua_State *dL, const void * parent, const char *desc) {
    luaL_checkstack(L, LUA_MINSTACK, NULL);
	int t = lua_type(L, -1);
	switch (t) {
	case LUA_TTABLE:
		mark_table(L, dL, parent, desc);
		break;
	case LUA_TUSERDATA:
		mark_userdata(L, dL, parent, desc);
		break;
	case LUA_TFUNCTION:
		mark_function(L, dL, parent, desc);
		break;
	case LUA_TTHREAD:
		mark_thread(L, dL, parent, desc);
		break;
	default:
		lua_pop(L,1);
		break;
	}
}
Example #30
0
static int lunpack(lua_State *L) {
	if (lua_isnoneornil(L, 1)) {
		return 0;
	} else {
		void *data;
		int size;
		int i;
		struct buffer b;
		if (lua_type(L, 1) == LUA_TSTRING) {
			size_t _size;
			data = (void *)lua_tolstring(L, 1, &_size);
			size = (int)_size;
		} else {
			data = lua_touserdata(L, 1);
			size = (int)luaL_checkinteger(L, 2);
		}
		if (size == 0 || data == 0) {
			return 0;
		}
		b.offset = 0;
		b.data = (char *)data;
		b.size = size;
		lua_settop(L, 0);
		for (i=0; ;i++) {
			uint8_t type;
			uint8_t *t;
			if (i%8==7) {
				luaL_checkstack(L, LUA_MINSTACK, 0);
			}
			t = (uint8_t *)buffer_pop(&b, 1);
			if (!t)	{
				break;
			}
			type = *t;
			lunpack_value(L, &b, type);
		}
		return lua_gettop(L);
	}
}