Ejemplo n.º 1
0
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 */
  double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
  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_check_int(L, 1);
      luaL_arg_check(L, 1<=u, 1, "interval is empty");
      lua_pushnumber(L, (int)(r*u)+1);  /* integer between 1 and `u' */
      break;
    }
    case 2: {  /* lower and upper limits */
      int l = luaL_check_int(L, 1);
      int u = luaL_check_int(L, 2);
      luaL_arg_check(L, l<=u, 2, "interval is empty");
      lua_pushnumber(L, (int)(r*(u-l+1))+l);  /* integer between `l' and `u' */
      break;
    }
    default: lua_error(L, "wrong number of arguments");
  }
  return 1;
}
Ejemplo n.º 2
0
static void CreateImageRGBA(void)
{
  int i, count, width, height;
  unsigned char *pixels;
  lua_Object n, obj = luaL_tablearg(3);
  width = luaL_check_int(1);
  height = luaL_check_int(2);
  count = width * height * 4;
  pixels = (unsigned char *) malloc(count);

  for (i = 0; i < count; i++)
  {
    lua_beginblock();
    lua_pushobject(obj);
    lua_pushnumber(i+1);

    n = lua_gettable();
    if (!lua_isnumber(n)) 
    {
      lua_endblock();
      lua_error("iupCreateImage: incorrect value in argument");
    }
    pixels[i] = (unsigned char)lua_getnumber(n);
    lua_endblock();
  }

  lua_pushusertag(IupImageRGBA(width, height, pixels), iuplua_tag);
  free(pixels);
}
Ejemplo n.º 3
0
Archivo: ldblib.c Proyecto: zig/dcplaya
static int setlocal (lua_State *L) {
  lua_Debug ar;
  if (!lua_getstack(L, luaL_check_int(L, 1), &ar))  /* level out of range? */
    luaL_argerror(L, 1, "level out of range");
  luaL_checkany(L, 3);
  lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
  return 1;
}
Ejemplo n.º 4
0
static void CreateImage(void)
{
  int i, j;
  lua_Object linha;
  int width, height;
  unsigned char *pixels;
  lua_Object obj = luaL_tablearg(3);
  width = luaL_check_int(1);
  height = luaL_check_int(2);
  pixels = (unsigned char *) malloc(width*height);

  for (i = 1; i <= height; i++) 
  {
    lua_beginblock();
    lua_pushobject(obj);
    lua_pushnumber(i);
    linha = lua_gettable();
    if (!lua_istable(linha)) 
    {
      lua_endblock();
      lua_error("iupCreateImage: incorrect value in argument");
    }

    for (j = 1; j <= width; j++) 
    {
      lua_Object n;
      lua_beginblock();
      lua_pushobject(linha);
      lua_pushnumber(j);
      n = lua_gettable();
      if (!lua_isnumber(n)) 
      {
        lua_endblock();
        lua_error("iupCreateImage: incorrect value in argument");
      }
      pixels[(i - 1) * width + (j - 1)] = (unsigned char) lua_getnumber(n);
      lua_endblock();
    }

    lua_pushobject(obj);
    lua_pushnumber(i);
    lua_pushnil();
    lua_settable();
    lua_endblock();
  }

  lua_pushusertag(IupImage(width, height, pixels), iuplua_tag);
  free(pixels);
}
Ejemplo n.º 5
0
Archivo: ldblib.c Proyecto: zig/dcplaya
static int getlocal (lua_State *L) {
  lua_Debug ar;
  const char *name;
  if (!lua_getstack(L, luaL_check_int(L, 1), &ar))  /* level out of range? */
    luaL_argerror(L, 1, "level out of range");
  name = lua_getlocal(L, &ar, luaL_check_int(L, 2));
  if (name) {
    lua_pushstring(L, name);
    lua_pushvalue(L, -2);
    return 2;
  }
  else {
    lua_pushnil(L);
    return 1;
  }
}
Ejemplo n.º 6
0
static void getlocal (void) {
  lua_Object func = lua_stackedfunction(luaL_check_int(1));
  lua_Object val;
  char *name;
  if (func == LUA_NOOBJECT)  /* level out of range? */
    return;  /* return nil */
  else if (lua_getparam(2) != LUA_NOOBJECT) {  /* 2nd argument? */
    if ((val = lua_getlocal(func, findlocal(func, 2), &name)) != LUA_NOOBJECT) {
      lua_pushobject(val);
      lua_pushstring(name);
    }
    /* else return nil */
  }
  else {  /* collect all locals in a table */
    lua_Object result = lua_createtable();
    int i;
    for (i=1; ;i++) {
      if ((val = lua_getlocal(func, i, &name)) == LUA_NOOBJECT)
        break;
      lua_pushobject(result);
      lua_pushstring(name);
      lua_pushobject(val);
      lua_settable();  /* result[name] = value */
    }
    lua_pushobject(result);
  }
}
Ejemplo n.º 7
0
static void cfTreeSetTableId(void)
{
  Ihandle* ih = iuplua_checkihandle(1);
  int id = luaL_check_int(2);

  lua_Object o3 = lua_getparam(3);
  if(lua_isnil(o3))
  {
		int ref;
		/* Setting in the id table the reference as nil */
		o3 = tree_gettable(ih, id);
		if(o3 != LUA_NOOBJECT)
		  tree_setnumberintable(o3, 0);

    ref = (int)IupTreeGetUserId(ih, id);
    lua_unref(ref);
    lua_pushnumber(IupTreeSetUserId(ih, id, NULL));
  }
  else
  {
    int ref;
    lua_pushobject(o3);
    ref = lua_ref(1);
    lua_pushnumber(IupTreeSetUserId(ih, id, (void*)(ref+1)));
		tree_setnumberintable(o3, ref+1);
  }
} 
Ejemplo n.º 8
0
static int luaB_settag (lua_State *L)
{
    luaL_checktype(L, 1, LUA_TTABLE);
    lua_pushvalue(L, 1);  /* push table */
    lua_settag(L, luaL_check_int(L, 2));
    return 1;  /* return table */
}
Ejemplo n.º 9
0
static void GetClassAttributes(void)
{
  int max_n = luaL_check_int(2);
  char **names = (char **) malloc(max_n * sizeof(char *));
  int n = IupGetClassAttributes(luaL_check_string(1), names, max_n);
  lua_Object tb;
  int i;

  if (n == -1)
  {
    lua_pushnil();
    return;
  }

  tb = lua_createtable();
  for (i = 0; i < n; i++) 
  {
    lua_beginblock();
    lua_pushobject(tb);
    lua_pushnumber(i);
    lua_pushstring(names[i]);
    lua_settable();
    lua_endblock();                /* end a section and starts another */
  }

  lua_pushobject(tb);
  lua_pushnumber(n);
  free(names);
}
Ejemplo n.º 10
0
static void TextConvertPosToLinCol(void)
{
  int lin, col;
  IupTextConvertPosToLinCol(iuplua_checkihandle(1), luaL_check_int(2), &lin, &col);
  lua_pushnumber(lin);
  lua_pushnumber(col);
}
Ejemplo n.º 11
0
static int luaB_gettagmethod (lua_State *L) {
  int tag = luaL_check_int(L, 1);
  const char *event = luaL_check_string(L, 2);
  if (strcmp(event, "gc") == 0)
    lua_error(L, "deprecated use: cannot get the `gc' tag method from Lua");
  lua_gettagmethod(L, tag, event);
  return 1;
}
Ejemplo n.º 12
0
static void setlocal (void) {
  lua_Object func = lua_stackedfunction(luaL_check_int(1));
  int numvar;
  luaL_arg_check(func != LUA_NOOBJECT, 1, "level out of range");
  numvar = findlocal(func, 2);
  lua_pushobject(luaL_nonnullarg(3));
  if (!lua_setlocal(func, numvar))
    lua_error("no such local variable");
}
Ejemplo n.º 13
0
static void GetParamParam(void)
{
  Ihandle* param;
  Ihandle *dialog = iuplua_checkihandle(1);
  int param_index = luaL_check_int(2);
  char param_str[50];
  sprintf(param_str, "PARAM%d", param_index);
  param = (Ihandle*)IupGetAttribute(dialog, param_str);
  iuplua_pushihandle(param);
}
Ejemplo n.º 14
0
/* Split lists and tuples slices o[start:end] */
static void pyobject_slice(lua_State *L) {
    lua_Object lobj = lua_getparam(L, 1);
    if (is_object_container(L, lobj)) {
        int start = luaL_check_int(L, 2);
        int end = luaL_check_int(L, 3);
        PyObject *object = get_pobject(L, lobj);
        PyObject *obj = PySequence_GetSlice(object, start, end);
        if (!obj) {
            const char *name = object->ob_type->tp_name;
            char *format = "object \"%s\" does not support slices";
            const char *str = name ? name : "?";
            char buff[buffsize_calc(2, format, str)];
            sprintf(buff, format, str);
            lua_new_error(L, &buff[0]);
        }
        push_pyobject_container(L, obj, 1);
    } else {
        lua_error(L, "#1 is not a container for python object!");
    }
}
Ejemplo n.º 15
0
static void cfTreeGetTable(void)
{
  Ihandle* ih = iuplua_checkihandle(1);
  int id = luaL_check_int(2);

	lua_Object o3 = tree_gettable(ih, id);
  if(o3 == LUA_NOOBJECT)
    lua_pushnil();
  else
    lua_pushobject(o3);
}
Ejemplo n.º 16
0
static int str_rep (lua_State *L) {
    size_t l;
    luaL_Buffer b;
    const char *s = luaL_check_lstr(L, 1, &l);
    int n = luaL_check_int(L, 2);
    luaL_buffinit(L, &b);
    while (n-- > 0)
        luaL_addlstring(&b, s, l);
    luaL_pushresult(&b);
    return 1;
}
Ejemplo n.º 17
0
static int luaB_settagmethod (lua_State *L) {
  int tag = luaL_check_int(L, 1);
  const char *event = luaL_check_string(L, 2);
  luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3,
                 "function or nil expected");
  if (strcmp(event, "gc") == 0)
    lua_error(L, "deprecated use: cannot set the `gc' tag method from Lua");
  lua_gettagmethod(L, tag, event);
  lua_pushvalue(L, 3);
  lua_settagmethod(L, tag, event);
  return 1;
}
Ejemplo n.º 18
0
static int str_char (lua_State *L) {
    int n = lua_gettop(L);  /* number of arguments */
    int i;
    luaL_Buffer b;
    luaL_buffinit(L, &b);
    for (i=1; i<=n; i++) {
        int c = luaL_check_int(L, i);
        luaL_arg_check(L, (unsigned char)c == c, i, "invalid value");
        luaL_putchar(&b, (unsigned char)c);
    }
    luaL_pushresult(&b);
    return 1;
}
Ejemplo n.º 19
0
static void getstack (void) {
  lua_Object func = lua_stackedfunction(luaL_check_int(1));
  if (func == LUA_NOOBJECT)  /* level out of range? */
    return;
  else {
    lua_Object result = getfuncinfo(func);
    int currline = lua_currentline(func);
    if (currline > 0)
      settabsi(result, "current", currline);
    lua_pushobject(result);
    lua_pushstring("func");
    lua_pushobject(func);
    lua_settable();  /* result.func = func */
    lua_pushobject(result);
  }
}
Ejemplo n.º 20
0
static void luaB_tinsert (void) {
  Hash *a = gethash(1);
  lua_Object v = lua_getparam(3);
  int n = (int)getnarg(a);
  int pos;
  if (v != LUA_NOOBJECT)
    pos = luaL_check_int(2);
  else {  /* called with only 2 arguments */
    v = luaL_nonnullarg(2);
    pos = n+1;
  }
  luaV_setn(a, n+1);  /* a.n = n+1 */
  for ( ;n>=pos; n--)
    luaH_move(a, n, n+1);  /* a[n+1] = a[n] */
  luaH_setint(a, pos, luaA_Address(v));  /* a[pos] = v */
}
Ejemplo n.º 21
0
static int luaB_tinsert (lua_State *L) {
  int v = lua_gettop(L);  /* last argument: to be inserted */
  int n, pos;
  luaL_checktype(L, 1, LUA_TTABLE);
  n = lua_getn(L, 1);
  if (v == 2)  /* called with only 2 arguments */
    pos = n+1;
  else
    pos = luaL_check_int(L, 2);  /* 2nd argument is the position */
  lua_pushstring(L, "n");
  lua_pushnumber(L, n+1);
  lua_rawset(L, 1);  /* t.n = n+1 */
  for (; n>=pos; n--) {
    lua_rawgeti(L, 1, n);
    lua_rawseti(L, 1, n+1);  /* t[n+1] = t[n] */
  }
  lua_pushvalue(L, v);
  lua_rawseti(L, 1, pos);  /* t[pos] = v */
  return 0;
}
Ejemplo n.º 22
0
/*
 * HMAC-SHA256
 * params: password (str), salt (str), iterations (int)
 */
void pbkdf2_hmac_sha256(lua_State *L) {
    char *pwd = luaL_check_string(L, 1);
    char *salt = luaL_check_string(L, 2);

    uint32_t iterations = (unsigned) luaL_check_int(L, 3);

    size_t byteSize = 32; // 256-bit (32-byte)
    char encoded[byteSize];

    fastpbkdf2_hmac_sha256(
            (unsigned char *) pwd, strlen(pwd),
            (unsigned char *) salt, strlen(salt),
            iterations,
            (unsigned char *) encoded, byteSize);

    char out[BUFF_SIZE];
    int length = base64_encode((unsigned char *) encoded, byteSize, out);

    lua_pushlstring(L, &out[0], length);
}
Ejemplo n.º 23
0
static void GetAllDialogs(void)
{
  int max_n = luaL_check_int(1);
  char **names = (char **) malloc(max_n * sizeof(char *));
  int i;
  int n = IupGetAllDialogs(names, 100);

  lua_Object tb = lua_createtable();
  for (i = 0; i < n; i++) 
  {
    lua_beginblock();
    lua_pushobject(tb);
    lua_pushnumber(i);
    lua_pushstring(names[i]);
    lua_settable();
    lua_endblock();                /* end a section and starts another */
  }

  lua_pushobject(tb);
  lua_pushnumber(n);
  free(names);
}
Ejemplo n.º 24
0
static void PlotInsert(void)
{
  Ihandle *ih = iuplua_checkihandle(1);
  IupPlotInsert(ih, luaL_check_int(2), luaL_check_int(3), (float)luaL_check_number(4), (float)luaL_check_number(5));
}
Ejemplo n.º 25
0
static void cfTreeGetUserId(void)
{
  lua_pushusertag(IupTreeGetUserId(iuplua_checkihandle(1), luaL_check_int(2)), LUA_ANYTAG);
}
Ejemplo n.º 26
0
static void PlotBegin(void)
{
  Ihandle *ih = iuplua_checkihandle(1);
  IupPlotBegin(ih, luaL_check_int(2));
}
Ejemplo n.º 27
0
static int math_randomseed (lua_State *L) {
  srand(luaL_check_int(L, 1));
  return 0;
}
Ejemplo n.º 28
0
static int luaB_copytagmethods (lua_State *L) {
  lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1),
                                          luaL_check_int(L, 2)));
  return 1;
}
Ejemplo n.º 29
0
static int math_ldexp (lua_State *L) {
  lua_pushnumber(L, ldexp(Re(luaL_check_number(L, 1)), luaL_check_int(L, 2)));
  return 1;
}
Ejemplo n.º 30
0
static int str_format (lua_State *L) {
    int arg = 1;
    const char *strfrmt = luaL_check_string(L, arg);
    luaL_Buffer b;
    luaL_buffinit(L, &b);
    while (*strfrmt) {
        if (*strfrmt != '%')
            luaL_putchar(&b, *strfrmt++);
        else if (*++strfrmt == '%')
            luaL_putchar(&b, *strfrmt++);  /* %% */
        else { /* format item */
            struct Capture cap;
            char form[MAX_FORMAT];  /* to store the format ('%...') */
            char buff[MAX_ITEM];  /* to store the formatted item */
            const char *initf = strfrmt;
            form[0] = '%';
            if (isdigit((unsigned char)*initf) && *(initf+1) == '$') {
                arg = *initf - '0';
                initf += 2;  /* skip the 'n$' */
            }
            arg++;
            cap.src_end = strfrmt+strlen(strfrmt)+1;
            cap.level = 0;
            strfrmt = match(L, initf, "[-+ #0]*(%d*)%.?(%d*)", &cap);
            if (cap.capture[0].len > 2 || cap.capture[1].len > 2 ||  /* < 100? */
                    strfrmt-initf > MAX_FORMAT-2)
                lua_error(L, "invalid format (width or precision too long)");
            strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include conversion */
            form[strfrmt-initf+2] = 0;
            switch (*strfrmt++) {
            case 'c':
            case 'd':
            case 'i':
                sprintf(buff, form, luaL_check_int(L, arg));
                break;
            case 'o':
            case 'u':
            case 'x':
            case 'X':
                sprintf(buff, form, (unsigned int)luaL_check_number(L, arg));
                break;
            case 'e':
            case 'E':
            case 'f':
            case 'g':
            case 'G':
                sprintf(buff, form, luaL_check_number(L, arg));
                break;
            case 'q':
                luaI_addquoted(L, &b, arg);
                continue;  /* skip the "addsize" at the end */
            case 's': {
                size_t l;
                const char *s = luaL_check_lstr(L, arg, &l);
                if (cap.capture[1].len == 0 && l >= 100) {
                    /* no precision and string is too long to be formatted;
                       keep original string */
                    lua_pushvalue(L, arg);
                    luaL_addvalue(&b);
                    continue;  /* skip the "addsize" at the end */
                }
                else {
                    sprintf(buff, form, s);
                    break;
                }
            }
            default:  /* also treat cases 'pnLlh' */
                lua_error(L, "invalid option in `format'");
            }
            luaL_addlstring(&b, buff, strlen(buff));
        }
    }
    luaL_pushresult(&b);
    return 1;
}