Example #1
0
    static int Get(lua_State *L) {
        luacl_buffer_object *buffer = traits::CheckObject(L, 1);
        lua_Integer index = luaL_checkinteger(L, 2);

        luaL_argcheck(L, index >= 0, 2, "Invalid index");
        luaL_argcheck(L, (index + 1) * sizeof(T) <= buffer->size, 2, "Index out of bound");
        
        T * data = reinterpret_cast<T *>(&buffer->data);
        lua_pushnumber(L, lua_Number(data[index]));
        return 1;
    }
static int math_random (lua_State *L) {
  /* the `%' avoids the (rare) case of r==1, and is needed also because on
     some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */

  // SPRING
  // respect the original lua code that uses rand,
  // and rand is auto-seeded always with the same value
#ifdef STREFLOP_H // unitsync is compiled without streflop
  if (lua_streflop_random_seed == 0) {
    lua_streflop_random_seed = 1;
    streflop::RandomInit(1);
  }

  lua_Number r =
    streflop::Random<true, false, lua_Number>(lua_Number(0.0), lua_Number(1.0));
#else
  lua_Number r = 0.0f;
#endif

  switch (lua_gettop(L)) {  /* check number of arguments */
    case 0: {  /* no arguments */
      lua_pushnumber(L, r);  /* Number between 0 and 1 */
      break;
    }
    case 1: {  /* only upper limit */
      int u = luaL_checkint(L, 1);
      luaL_argcheck(L, 1<=u, 1, "interval is empty");
      lua_pushnumber(L, floor(r*u)+1);  /* int between 1 and `u' */
      break;
    }
    case 2: {  /* lower and upper limits */
      int l = luaL_checkint(L, 1);
      int u = luaL_checkint(L, 2);
      luaL_argcheck(L, l<=u, 2, "interval is empty");
      lua_pushnumber(L, floor(r*(u-l+1))+l);  /* int between `l' and `u' */
      break;
    }
    default: return luaL_error(L, "wrong number of arguments");
  }
  return 1;
}
Example #3
0
 static int GetSize(lua_State *L) {
     lua_pushnumber(L, lua_Number(sizeof(T)));
     return 1;
 }
Example #4
0
static int l_fixed_tostring(lua_State *L)
{
	const fixed *v = LuaFixed::CheckFromLua(L, 1);
	lua_pushfstring(L, "fixed(%f)", lua_Number(v->ToDouble()));
	return 1;
}