Beispiel #1
0
static int hookInterface(lua_State* L, infHook ih) {
	lua_rawgetp(L, lua_upvalueindex(1), &FFI_NEW);
	if (lua_type(L, -1) != LUA_TFUNCTION) {
		return luaL_error(L, "No initialization.");
	}

	lua_pushvalue(L, -1);
	lua_pushvalue(L, -4);
	lua_pushnil(L);
	lua_call(L, 2, 1);

	lua_insert(L, -4);
	lua_insert(L, -3);
	lua_call(L, 2, 1);

	hook_t* h = (hook_t*)lua_newuserdata(L, sizeof hook_t);
	if (!h) {
		return 0;
	}
	new (h) hook_t;
	luaL_setmetatable(L, "hook");
	lua_newtable(L);
	lua_pushvalue(L, -4);
	lua_seti(L, -2, 1);
	lua_pushvalue(L, -3);
	lua_seti(L, -2, 2);
	lua_setuservalue(L, -2);

	h->fake = *(uintptr_t*)((uintptr_t)lua_touserdata(L, -2) + 16);
	ih(L, h);
	uintptr_t cd = (uintptr_t)lua_touserdata(L, -3);
	*(uintptr_t*)(cd + 16) = h->real;
	return 1;
}
Beispiel #2
0
static int tinsert (lua_State *L) {
  lua_Integer e = aux_getn(L, 1, TAB_RW) + 1;  /* first empty element */
  lua_Integer pos;  /* where to insert new element */
  switch (lua_gettop(L)) {
    case 2: {  /* called with only 2 arguments */
      pos = e;  /* insert new element at the end */
      break;
    }
    case 3: {
      lua_Integer i;
      pos = luaL_checkinteger(L, 2);  /* 2nd argument is the position */
      luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
      for (i = e; i > pos; i--) {  /* move up elements */
        lua_geti(L, 1, i - 1);
        lua_seti(L, 1, i);  /* t[i] = t[i - 1] */
      }
      break;
    }
    default: {
      return luaL_error(L, "wrong number of arguments to 'insert'");
    }
  }
  lua_seti(L, 1, pos);  /* t[pos] = v */
  return 0;
}
Beispiel #3
0
/*
** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever
** possible, copy in increasing order, which is better for rehashing.
** "possible" means destination after original range, or smaller
** than origin, or copying to another table.
*/
static int tmove (lua_State *L) {
  lua_Integer f = luaL_checkinteger(L, 2);
  lua_Integer e = luaL_checkinteger(L, 3);
  lua_Integer t = luaL_checkinteger(L, 4);
  int tt = !lua_isnoneornil(L, 5) ? 5 : 1;  /* destination table */
  checktab(L, 1, TAB_R);
  checktab(L, tt, TAB_W);
  if (e >= f) {  /* otherwise, nothing to move */
    lua_Integer n, i;
    luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3,
                  "too many elements to move");
    n = e - f + 1;  /* number of elements to move */
    luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4,
                  "destination wrap around");
    if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) {
      for (i = 0; i < n; i++) {
        lua_geti(L, 1, f + i);
        lua_seti(L, tt, t + i);
      }
    }
    else {
      for (i = n - 1; i >= 0; i--) {
        lua_geti(L, 1, f + i);
        lua_seti(L, tt, t + i);
      }
    }
  }
  lua_pushvalue(L, tt);  /* return destination table */
  return 1;
}
Beispiel #4
0
static void reverse (lua_State *L, lua_Integer a, lua_Integer b) {
  for (; a < b; ++a, --b) {
    lua_geti(L, -1, a);
    lua_geti(L, -2, b);
    lua_seti(L, -3, a);
    lua_seti(L, -2, b);
  }
}
Beispiel #5
0
int pushvector2(lua_State *L, vector2_t *vec, int astable)
    {
    if(astable)
        {
        lua_newtable(L);
        lua_pushnumber(L, vec->x); lua_seti(L, -2, 1);
        lua_pushnumber(L, vec->y); lua_seti(L, -2, 2);
        return 1;
        }
    lua_pushnumber(L, vec->x);  
    lua_pushnumber(L, vec->y);  
    return 2;
    }
Beispiel #6
0
static int tremove (lua_State *L) {
  lua_Integer size = aux_getn(L, 1, TAB_RW);
  lua_Integer pos = luaL_optinteger(L, 2, size);
  if (pos != size)  /* validate 'pos' if given */
    luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");
  lua_geti(L, 1, pos);  /* result = t[pos] */
  for ( ; pos < size; pos++) {
    lua_geti(L, 1, pos + 1);
    lua_seti(L, 1, pos);  /* t[pos] = t[pos + 1] */
  }
  lua_pushnil(L);
  lua_seti(L, 1, pos);  /* remove entry t[pos] */
  return 1;
}
Beispiel #7
0
int pushcolor3(lua_State *L, color3_t *col, int astable)
    {
    if(astable)
        {
        lua_newtable(L);
        lua_pushnumber(L, col->r); lua_seti(L, -2, 1);
        lua_pushnumber(L, col->g); lua_seti(L, -2, 2);
        lua_pushnumber(L, col->b); lua_seti(L, -2, 3);
        return 1;
        }
    lua_pushnumber(L, col->r);  
    lua_pushnumber(L, col->g);  
    lua_pushnumber(L, col->b);  
    return 3;
    }
Beispiel #8
0
static int tshuffle (lua_State *L) {
  lua_Integer begin, end;
  checktab(L, 1, TAB_RW);
  begin = luaL_optinteger(L, 2, 1);
  end = luaL_opt(L, luaL_checkinteger, 3, check_n(L, 1));
  while (end >= begin) {
    double f = l_rand() * (1.0/(L_RANDMAX+1.0));
    lua_Integer j = begin + (lua_Integer)(f * (end-begin+1));
    lua_geti(L, 1, end);
    lua_geti(L, 1, j);
    lua_seti(L, 1, end);
    lua_seti(L, 1, j);
    --end;
  }
  return 0;
}
void luaInterpreterWrapper::vectorSpaceArrayToStack(const Field<T> &f)
{
    lua_newtable(luaState_);
    forAll(f,i) {
        vectorSpaceToStack(f[i]);
        lua_seti(luaState_,-2,i+1);
    }
Beispiel #10
0
static int Solver_query(struct lua_State *state) {
  
  LGL* lgl = get_lgl(state);

  if (lua_gettop(state) != 2) {
    lua_pushliteral(state, "Query must be called with arguments self and a table");
    lua_error(state);
  }

  int type = lua_type(state, 2);

  if (type != LUA_TTABLE) {
    lua_pushliteral(state, "Query must be called with a table argument");
    lua_error(state);
  }

  lua_newtable (state); // Stack pos 3

  lua_len(state, 2);
  lua_Integer len = lua_tointeger(state, -1);
  lua_pop(state, 1);
  for (int j = 1; j <= len; j++) {
    lua_geti(state, 2, j);
    lua_Integer c = lua_tointeger (state, -1);
    if (!c) {
      lua_pushliteral(state, "Array may contain only non-zero integers");
      lua_error(state);    
    }
    lua_pop(state,1);
    lua_pushinteger(state, lglderef(lgl, c));
    lua_seti(state, 3, j);
  }     

  return 1;
}
Beispiel #11
0
int Script::OnDestroy(GameObject * GameObj) {
    // this will completelly remove the reference to gameobject in scripting space
    // so scripts can no longer use this gameobjects
    // gameobject maynot be removed immediatly after this call
    // because of how lua's garbage collection works

    if (ObjectId == -1) {
        return 1;
    }
    // get gameobject table
    lua_getglobal(vm, "entities");
    lua_geti(vm, -1, ObjectId);
    // todo:: set obj's metatable to a default special table to disable operations on this object
    // can't do this now because it will ruin the __gc meta method
    //lua_getglobal(vm, "destroyed_mt");
    //lua_setmetatable(vm, -2);
    lua_pop(vm, 1);
    // set entitis[id] to nil
    lua_pushnil(vm);
    lua_seti(vm, -2, ObjectId);
    // balance the stack
    lua_pop(vm, 1);
    // reset objectid
    ObjectId = -1;
    Component::OnDestroy(GameObj);

    return 0;
}
Beispiel #12
0
int pushtexelf(lua_State *L, texel_t *vec, int astable)
    {
    if(astable)
        {
        lua_newtable(L);
        lua_pushnumber(L, vec->r/255.f); lua_seti(L, -2, 1);
        lua_pushnumber(L, vec->g/255.f); lua_seti(L, -2, 2);
        lua_pushnumber(L, vec->b/255.f); lua_seti(L, -2, 3);
        lua_pushnumber(L, vec->a/255.f); lua_seti(L, -2, 4);
        return 1;
        }
    lua_pushnumber(L, vec->r/255.f);    
    lua_pushnumber(L, vec->g/255.f);    
    lua_pushnumber(L, vec->b/255.f);    
    lua_pushnumber(L, vec->a/255.f);    
    return 4;
    }
Beispiel #13
0
int pushquaternion(lua_State *L, quaternion_t *quat, int astable)
    {
    if(astable)
        {
        lua_newtable(L);
        lua_pushnumber(L, quat->w); lua_seti(L, -2, 1);
        lua_pushnumber(L, quat->x); lua_seti(L, -2, 2);
        lua_pushnumber(L, quat->y); lua_seti(L, -2, 3);
        lua_pushnumber(L, quat->z); lua_seti(L, -2, 4);
        return 1;
        }
    lua_pushnumber(L, quat->w); 
    lua_pushnumber(L, quat->x); 
    lua_pushnumber(L, quat->y); 
    lua_pushnumber(L, quat->z); 
    return 4;
    }
Beispiel #14
0
int pushtexel(lua_State *L, texel_t *vec, int astable)
    {
    if(astable)
        {
        lua_newtable(L);
        lua_pushinteger(L, vec->r); lua_seti(L, -2, 1);
        lua_pushinteger(L, vec->g); lua_seti(L, -2, 2);
        lua_pushinteger(L, vec->b); lua_seti(L, -2, 3);
        lua_pushinteger(L, vec->a); lua_seti(L, -2, 4);
        return 1;
        }
    lua_pushinteger(L, vec->r); 
    lua_pushinteger(L, vec->g); 
    lua_pushinteger(L, vec->b); 
    lua_pushinteger(L, vec->a); 
    return 4;
    }
Beispiel #15
0
static int tpack (lua_State *L) {
  int i;
  int n = lua_gettop(L);  /* number of elements to pack */
  lua_createtable(L, n, 1);  /* create result table */
  lua_insert(L, 1);  /* put it at index 1 */
  for (i = n; i >= 1; i--)  /* assign elements */
    lua_seti(L, 1, i);
  lua_pushinteger(L, n);
  lua_setfield(L, 1, "n");  /* t.n = number of elements */
  return 1;  /* return table */
}
Beispiel #16
0
int pushquat(lua_State *L, quat_t q)
    {
    size_t i;
    lua_newtable(L);
    setmetatable(L, QUAT_MT);
    for(i=0; i<4; i++)
        {
        lua_pushnumber(L, q[i]);
        lua_seti(L, -2, i+1);
        }
    return 1;
    }
Beispiel #17
0
static int Init(lua_State *L)
    {
    int table_index;
    int argc, i;
    char **argv;
    const char *arg;

    if(lua_isnoneornil(L, 1))
        argc = 1;
    else
        {
        if(!lua_istable(L,1))
            return luaL_argerror(L, 1, "table expected");
        table_index = 1;
        argc = luaL_len(L, table_index) + 1;
        }

    argv = (char**)malloc(sizeof(char*) * argc);
    if(argv == NULL)
        return luaL_error(L, "cannot allocate memory"); 
    //argv[0] = "moonglut";
    argv[0] = strdup("moonglut");
    if(argc > 1)
        {
        for(i = 1; i < argc; i++)
            {
            lua_geti(L, table_index, i);
            arg = luaL_checkstring(L, -1);
            argv[i] = strdup(arg);
            if(argv[i] == NULL)
                return luaL_error(L, "cannot allocate memory");
            lua_pop(L, 1);
            }
        }
    glutInit(&argc, argv); 

    /* return the modified arg table */
    lua_newtable(L);
    table_index = lua_gettop(L);
    for(i = 1; i < argc; i++)
        {
        lua_pushstring(L, argv[i]);
        lua_seti(L, table_index, i);
        }
    return 1;
    }
Beispiel #18
0
int Script::Subscribe(int Event, String& Callback) {
    if (ObjectId == -1) {
        return 1;
    }
    Component::Subscribe(Event, Callback);
    // set event to function mapping in gameobject's event_table
    // get gameobhect
    lua_getglobal(vm, "objects");
    lua_geti(vm, -1, ObjectId);
    lua_getfield(vm, -1, "event");
    // get callback functions
    lua_getfield(vm, -2, Callback.ToStr());
    lua_seti(vm, -2, Event);
    // done. clear the stack
    lua_pop(vm, 3);
    return 0;
}
Beispiel #19
0
int luajack_xmove(lua_State *T, lua_State *L, int chunck_index, int last_index)
/* Checks and copies arguments between unrelated states L and T (L to T)
 * (lua_xmove() cannot be used here, because the states are not related).
 *
 * L[chunck_index] -----> arg[0] script name, or chunk
 * L[chunck_index +1] --> arg[1]
 * L[chunck_index +2] --> arg[2]
 * ...
 * L[last_index]      --> arg[N]
 * Since arguments are to be passed between unrelated states, the only admitted
 * types are: nil, boolean, number and string.
 */ 
	{
	int arg_index, argn, n, typ, nargs;
	nargs = last_index - chunck_index; /* no. of optional arguments */
	luaL_checkstack(T, nargs, "cannot grow Lua stack for thread");

	lua_newtable(T); /* "arg" table */
	arg_index = lua_gettop(T);

	lua_pushstring(T, lua_tostring(L, chunck_index)); /* arg[0] = full scriptname */
	lua_seti(T, arg_index, 0);

	argn = 1;
	for(n = chunck_index+1; n <= last_index; n++)
		{
		typ = lua_type(L, n);
		switch(typ)
			{
#define SetArg() do { lua_pushvalue(T, -1); lua_seti(T, arg_index, argn++); } while(0)
			case LUA_TNIL: lua_pushnil(T); SetArg(); break;
			case LUA_TBOOLEAN: lua_pushboolean(T, lua_toboolean(L, n)); SetArg(); break;
			case LUA_TNUMBER: lua_pushnumber(T, lua_tonumber(L, n)); SetArg(); break;
			case LUA_TSTRING: lua_pushstring(T, lua_tostring(L, n)); SetArg(); break;
			default:
				return luaL_error(L, "invalid type '%s' of argument #%d ", 
									lua_typename(L, typ), n - chunck_index);
#undef SetArg
			}
		}
	lua_pushvalue(T, arg_index);
	lua_setglobal(T, "arg");
	lua_remove(T, arg_index);
	return 0;
	}
Beispiel #20
0
static int luaC_array__preserve(lua_State *L)
{
  Array *A = lunum_checkarray1(L, 1);

  lua_getglobal(L, "lunum");
  lua_getfield(L, -1, "array");

  lunum_astable(L, 1);

  lua_pushinteger(L, A->dtype);

  lua_createtable(L, A->ndims, 0);
  for (int d = 0; d < A->ndims; d++) {
    lua_pushinteger(L, A->shape[d]);
    lua_seti(L, -2, d+1);
  }

  return 4;
}
Beispiel #21
0
void initialize_arguments (const struct vox_engine *engine,
                           int nargs, char * const arguments[])
{
    lua_State *L = engine->L;
    int i;

    if (nargs != 0) {
        lua_getglobal (L, "voxvision");
        lua_newtable (L);
        for (i=0; i<nargs; i++) {
            lua_pushstring (L, arguments[i]);
            lua_seti (L, -2, i+1);
        }

        lua_setfield (L, -2, "arg");
        // Pop voxvision table from the stack
        lua_pop (L, 1);
    }

    assert (lua_gettop (L) == 0);
}
Beispiel #22
0
int pushmatrix4(lua_State *L, matrix4_t *mat, int how)
    {
    if(how==0)
        {
        luaL_checkstack(L, 16, NULL);
        lua_pushnumber(L, mat->a1); 
        lua_pushnumber(L, mat->a2); 
        lua_pushnumber(L, mat->a3); 
        lua_pushnumber(L, mat->a4); 
        lua_pushnumber(L, mat->b1); 
        lua_pushnumber(L, mat->b2); 
        lua_pushnumber(L, mat->b3); 
        lua_pushnumber(L, mat->b4); 
        lua_pushnumber(L, mat->c1); 
        lua_pushnumber(L, mat->c2); 
        lua_pushnumber(L, mat->c3); 
        lua_pushnumber(L, mat->c4); 
        lua_pushnumber(L, mat->d1); 
        lua_pushnumber(L, mat->d2); 
        lua_pushnumber(L, mat->d3); 
        lua_pushnumber(L, mat->d4); 
        return 16;
        }
    if(how==2) lua_newtable(L);

    lua_newtable(L);
    lua_pushnumber(L, mat->a1); lua_seti(L, -2, 1);
    lua_pushnumber(L, mat->a2); lua_seti(L, -2, 2);
    lua_pushnumber(L, mat->a3); lua_seti(L, -2, 3);
    lua_pushnumber(L, mat->a4); lua_seti(L, -2, 4);
    if(how==2) lua_seti(L, -2, 1);
    lua_newtable(L);
    lua_pushnumber(L, mat->b1); lua_seti(L, -2, 1);
    lua_pushnumber(L, mat->b2); lua_seti(L, -2, 2);
    lua_pushnumber(L, mat->b3); lua_seti(L, -2, 3);
    lua_pushnumber(L, mat->b4); lua_seti(L, -2, 4);
    if(how==2) lua_seti(L, -2, 2);
    lua_newtable(L);
    lua_pushnumber(L, mat->c1); lua_seti(L, -2, 1);
    lua_pushnumber(L, mat->c2); lua_seti(L, -2, 2);
    lua_pushnumber(L, mat->c3); lua_seti(L, -2, 3);
    lua_pushnumber(L, mat->c4); lua_seti(L, -2, 4);
    if(how==2) lua_seti(L, -2, 3);
    lua_newtable(L);
    lua_pushnumber(L, mat->d1); lua_seti(L, -2, 1);
    lua_pushnumber(L, mat->d2); lua_seti(L, -2, 2);
    lua_pushnumber(L, mat->d3); lua_seti(L, -2, 3);
    lua_pushnumber(L, mat->d4); lua_seti(L, -2, 4);
    if(how==2) lua_seti(L, -2, 4);

    return (how==2) ? 1 : 4;
    }
Beispiel #23
0
int main(int argc, char *argv[]) {
	FILE *fp;
	unsigned char header[44] = {};
	unsigned int channel_num;
	unsigned int sample_rate;
	unsigned int bit_per_sample;
	unsigned int block_size;
	unsigned int data_size;
	unsigned int sample_num;
	unsigned int sample;
	unsigned int channel;
	int i;
	int retnum;
	lua_State *s;
	if (argc < 4) {
		fprintf(stderr, "Usage: %s <input file> <output file> <script file> [parameters...]\n", argc > 0 ? argv[0] : "wav_lua");
		return 1;
	}
	if (argv[1][0] != '-' || argv[1][1] != '\0') {
		unsigned int header_size;
		fp = fopen(argv[1], "rb");
		if (fp == NULL) {
			fputs("input file open error\n", stderr);
			return 1;
		}
		if (fread(header, 1, 44, fp) != 44) {
			fputs("input file read error\n", stderr);
			fclose(fp);
			return 1;
		}
		channel_num = getData(&header[22], 2);
		sample_rate = getData(&header[24], 4);
		bit_per_sample = getData(&header[34], 2);
		block_size = getData(&header[32], 2);

		header_size = getData(&header[16], 4);
		if (header_size > 16) {
			if (fseek(fp, 20 + header_size, SEEK_SET)) {
				fputs("input file seek error\n", stderr);
				fclose(fp);
				return 1;
			}
			header[16] = 0x10; header[17] = 0; header[18] = 0; header[19] = 0;
			for (;;) {
				if (fread(&header[36], 1, 8, fp) != 8) {
					fputs("input file read error\n", stderr);
					fclose(fp);
					return 1;
				}
				if (isMatch(&header[36], "data", 4)) {
					break;
				} else {
					unsigned int chunk_size = getData(&header[40], 4);
					if (fseek(fp, chunk_size, SEEK_CUR)) {
						fputs("input file seek error\n", stderr);
						fclose(fp);
						return 1;
					}
				}
			}
		}

		data_size = getData(&header[40], 4);
		if (!isMatch(&header[0], "RIFF", 4) || !isMatch(&header[8], "WAVEfmt ", 8) || !isMatch(&header[36], "data", 4) ||
		getData(&header[20], 2) != 1 || getData(&header[28], 4) != channel_num * bit_per_sample / 8 * sample_rate ||
		block_size != channel_num * bit_per_sample / 8 || data_size % block_size != 0 ||
		(bit_per_sample != 8 && bit_per_sample != 16)) {
			fputs("unsupported input format\n", stderr);
			return 1;
		}
		sample_num = data_size / block_size;
		s = luaL_newstate();
		if (s == NULL) {
			fputs("failed to create Lua state\n", stderr);
			fclose(fp);
			return 1;
		}
		luaL_openlibs(s);
		if (luaL_dofile(s, argv[3])) {
			const char *str = luaL_checkstring(s, -1);
			fputs("failed to load Lua script\n", stderr);
			if (str != NULL) fputs(str, stderr);
			fclose(fp);
			lua_close(s);
			return 1;
		}

		if (lua_getglobal(s, "process") != LUA_TFUNCTION) {
			fputs("process is not a function\n", stderr);
			fclose(fp);
			lua_close(s);
			return 1;
		}

		lua_createtable(s, channel_num, 0);
		for (channel = 0; channel < channel_num; channel++) {
			lua_createtable(s, sample_num, 0);
		}
		for (sample = 1; sample <= sample_num; sample++) {
			for (channel = 0; channel < channel_num; channel++) {
				unsigned char data[2];
				double value = 0;
				if (fread(data, 1, bit_per_sample / 8, fp) != bit_per_sample / 8) {
					fputs("input read error\n", stderr);
					fclose(fp);
					lua_close(s);
					return 1;
				}
				if (bit_per_sample == 8) {
					value = (double)(data[0] - 128) / 128.0;
				} else if (bit_per_sample == 16) {
					unsigned int v = getData(data, 2);
					if (v & 0x8000) {
						value = (double)(-((int)(~v+1) & 0xffff)) / 32768.0;
					} else {
						value = (double)v / 32768.0;
					}
				}
				lua_pushnumber(s, value);
				lua_seti(s, -2 - channel, sample);
			}
		}
		fclose(fp);
		for (channel = 1; channel <= channel_num; channel++) {
			lua_seti(s, 2, channel);
		}
	} else {
		header[0] = 'R'; header[1] = 'I'; header[2] = 'F'; header[3] = 'F';
		header[8] = 'W'; header[9] = 'A'; header[10] = 'V'; header[11] = 'E';
		header[12] = 'f'; header[13] = 'm'; header[14] = 't'; header[15] = ' ';
		header[16] = 0x10; header[17] = 0; header[18] = 0; header[19] = 0;
		header[20] = 1; header[21] = 0;
		header[36]= 'd'; header[37] = 'a'; header[38] = 't'; header[39] = 'a';

		channel_num = 0;
		sample_rate = 44100;
		bit_per_sample = 16;
		block_size = 0;
		data_size = 0;

		s = luaL_newstate();
		if (s == NULL) {
			fputs("failed to create Lua state\n", stderr);
			return 1;
		}
		luaL_openlibs(s);
		if (luaL_dofile(s, argv[3])) {
			const char *str = luaL_checkstring(s, -1);
			fputs("failed to load Lua script\n", stderr);
			if (str != NULL) fputs(str, stderr);
			lua_close(s);
			return 1;
		}

		if (lua_getglobal(s, "process") != LUA_TFUNCTION) {
			fputs("process is not a function\n", stderr);
			lua_close(s);
			return 1;
		}

		lua_createtable(s, channel_num, 0);
	}

	lua_pushinteger(s, sample_rate);

	lua_createtable(s, argc - 4, 0);
	for (i = 0; i < argc - 4; i++) {
		lua_pushstring(s, argv[i + 4]);
		lua_seti(s, -2, i + 1);
	}

	if (lua_pcall(s, 3, LUA_MULTRET, 0) != LUA_OK) {
		const char *str = luaL_checkstring(s, -1);
		fputs("error in executing Lua script\n", stderr);
		if (str != NULL) fputs(str, stderr);
		lua_close(s);
		return 1;
	}

	if (argv[2][0] != '-' || argv[2][1] != '\0') {
		retnum = lua_gettop(s);
		if (retnum > 3) {
			fputs("too many return data from Lua script\n", stderr);
			lua_close(s);
			return 1;
		} else if (retnum > 0 && !lua_isnil(s, 1)) {
			if (!lua_istable(s, 1)) {
				fputs("the first return data is neither a table nor nil\n", stderr);
				lua_close(s);
				return 1;
			}
			if (retnum >= 2) {
				if (lua_isinteger(s, 2)) {
					sample_rate = luaL_checkinteger(s, 2);
				} else {
					fputs("the second return data is not an integer\n", stderr);
					lua_close(s);
					return 1;
				}
			}
			if (retnum >= 3) {
				if (lua_isinteger(s, 3)) {
					bit_per_sample = luaL_checkinteger(s, 3);
				} else {
					fputs("the third return data is not an integer\n", stderr);
					lua_close(s);
					return 1;
				}
			}
		}
		if (retnum > 1) lua_pop(s, retnum - 1);
		channel_num = luaL_len(s, 1);
		if (channel_num > 0) {
			if (bit_per_sample != 8 && bit_per_sample != 16) {
				fputs("unsupported output bit per sample\n", stderr);
				lua_close(s);
				return 1;
			}
			for (channel = 1; channel <= channel_num; channel++) {
				lua_geti(s, 1, channel);
				if (channel == 1) {
					sample_num = luaL_len(s, -1);
				} else if (sample_num != luaL_len(s, -1)) {
					fputs("the number of samples mismatch\n", stderr);
					lua_close(s);
					return 1;
				}
			}
			setData(&header[4], channel_num * bit_per_sample / 8 * sample_num + 36, 4);
			setData(&header[22], channel_num, 2);
			setData(&header[24], sample_rate, 4);
			setData(&header[28], channel_num * bit_per_sample / 8 * sample_rate, 4);
			setData(&header[32], channel_num * bit_per_sample / 8, 2);
			setData(&header[34], bit_per_sample, 2);
			setData(&header[40], channel_num * bit_per_sample / 8 * sample_num, 4);
			for (sample = 1; sample <= sample_num; sample++) {
				for (channel = 1; channel <= channel_num; channel++) {
					lua_geti(s, channel + 1, sample);
					if (!lua_isnumber(s, -1)) {
						fputs("some of the data is invalid\n", stderr);
						lua_close(s);
						return 1;
					}
					lua_pop(s, 1);
				}
			}
			fp = fopen(argv[2], "wb");
			if (fp == NULL) {
				fputs("output file open error\n", stderr);
				lua_close(s);
				return 1;
			}
			if (fwrite(header, 1, 44, fp) != 44) {
				fputs("output file write error\n", stderr);
				fclose(fp);
				lua_close(s);
				return 1;
			}
			for (sample = 1; sample <= sample_num; sample++) {
				for (channel = 1; channel <= channel_num; channel++) {
					double value;
					unsigned char data[2];
					int v;
					lua_geti(s, channel + 1, sample);
					value = luaL_checknumber(s, -1);
					if (value < -1.0) value = -1.0;
					if (value > 1.0) value = 1.0;
					lua_pop(s, 1);
					if (bit_per_sample == 8) {
						v = (int)(value * 128 + 128);
						if (v < 0) v = 0;
						if (v > 255) v = 255;
						data[0] = (unsigned char)v;
						if (fwrite(data, 1, 1, fp) != 1) {
							fputs("output file write error\n", stderr);
							fclose(fp);
							lua_close(s);
							return 1;
						}
					} else if (bit_per_sample == 16) {
						v = (int)(value * 32768);
						if (v < -32768) v = -32768;
						if (v > 32767) v = 32767;
						setData(data, v, 2);
						if (fwrite(data, 1, 2, fp) != 2) {
							fputs("output file write error\n", stderr);
							fclose(fp);
							lua_close(s);
							return 1;
						}
					}
				}
			}
			fclose(fp);
		}

		lua_pop(s, channel_num + 1);
	} else {
		lua_pop(s, lua_gettop(s));
	}
	lua_close(s);
	return 0;
}
Beispiel #24
0
static void set2 (lua_State *L, IdxT i, IdxT j) {
  lua_seti(L, 1, i);
  lua_seti(L, 1, j);
}
Beispiel #25
0
int pushmatrix3(lua_State *L, matrix3_t *mat, int how)
    {
    if(how==0)
        {
        luaL_checkstack(L, 9, NULL);
        lua_pushnumber(L, mat->a1); 
        lua_pushnumber(L, mat->a2); 
        lua_pushnumber(L, mat->a3); 
        lua_pushnumber(L, mat->b1); 
        lua_pushnumber(L, mat->b2); 
        lua_pushnumber(L, mat->b3); 
        lua_pushnumber(L, mat->c1); 
        lua_pushnumber(L, mat->c2); 
        lua_pushnumber(L, mat->c3); 
        return 9;
        }
    if(how==2) lua_newtable(L);

    lua_newtable(L);
    lua_pushnumber(L, mat->a1); lua_seti(L, -2, 1);
    lua_pushnumber(L, mat->a2); lua_seti(L, -2, 2);
    lua_pushnumber(L, mat->a3); lua_seti(L, -2, 3);
    if(how==2) lua_seti(L, -2, 1);
    lua_newtable(L);
    lua_pushnumber(L, mat->b1); lua_seti(L, -2, 1);
    lua_pushnumber(L, mat->b2); lua_seti(L, -2, 2);
    lua_pushnumber(L, mat->b3); lua_seti(L, -2, 3);
    if(how==2) lua_seti(L, -2, 2);
    lua_newtable(L);
    lua_pushnumber(L, mat->c1); lua_seti(L, -2, 1);
    lua_pushnumber(L, mat->c2); lua_seti(L, -2, 2);
    lua_pushnumber(L, mat->c3); lua_seti(L, -2, 3);
    if(how==2) lua_seti(L, -2, 3);

    return (how==2) ? 1 : 3;
    }
Beispiel #26
0
static void set2 (lua_State *L, unsigned int i, unsigned int j) {
  lua_seti(L, 1, i);
  lua_seti(L, 1, j);
}
Beispiel #27
0
static int treplace (lua_State *L) {
  lua_Integer len, tpos, start, end, start2, end2, i;
  len = aux_getn(L, 1, TAB_RW);
  if (lua_type(L, 2) == LUA_TNUMBER) {
    start = luaL_checkinteger(L, 2);
    luaL_argcheck(L, start >= 1 && start <= len+1, 2,
                  "index out of bounds");
    if (lua_type(L, 3) == LUA_TNUMBER) {
      end = luaL_checkinteger(L, 3);
      luaL_argcheck(L, end >= start-1 && end <= len, 3,
                    "invalid end index");
      tpos = 4;
    } else {
      end = start;
      if (end > len)
        end = len;
      tpos = 3;
    }
  } else {
    start = len+1;
    end = len;
    tpos = 2;
  }
  checktab(L, tpos, TAB_R);
  start2 = luaL_optinteger(L, tpos+1, 1);
  end2 = luaL_opt(L, luaL_checkinteger, tpos+2, check_n(L, tpos));
  luaL_argcheck(L, end2 >= start2-1, tpos+2, "invalid end index");
  if (end2-start2 > end-start) { /* array needs to grow */
    lua_pushinteger(L, len+end2-start2-end+start);
    lua_setfield(L, 1, "n");  /* t.n = number of elements */
  }
  if (start <= len) { /* replace values */
    lua_Integer shift = end2-start2-end+start;
    if (shift < 0) { /* shift to left */
      for (i = end+1; i <= len; ++i) {
        lua_geti(L, 1, i);
        lua_seti(L, 1, i+shift);
      }
      for (i = len; i > len+shift; --i) {
        lua_pushnil(L);
        lua_seti(L, 1, i);
      }
    } else if (shift != 0) { /* shift to right */
      for (i = len-shift+1; i <= len; ++i) {
        lua_geti(L, 1, i);
        lua_seti(L, 1, i+shift);
      }
      for (i = len-shift; i > end; --i) {
        lua_geti(L, 1, i);
        lua_seti(L, 1, i+shift);
      }
    }
  }
  /* copy from list2 to list1 */
  for (i = start2; i <= end2; ++i) {
    lua_geti(L, tpos, i);
    lua_seti(L, 1, start+i-start2);
  }
  /* array must shrink */
  if (end2-start2 < end-start) {
    lua_pushinteger(L, len+end2-start2-end+start);
    lua_setfield(L, 1, "n");  /* t.n = number of elements */
  }
  return 0;
}
Beispiel #28
0
	void Set(int key, T value) {
		PushToLua<T>(m_luaState, value);
		lua_seti(m_luaState, m_index, key);
	}