Example #1
0
		generic_callback(lua_State *_L)
		{
			L = getGlobalState(_L);
			ref = lua_ref(L, true);
		}
Example #2
0
		generic_callback(const generic_callback& other)
		{
			L = other.L;
			lua_getref(L, other.ref);
			ref = lua_ref(L, true);
		}
Example #3
0
static void testC() {
#define getnum(s)	((*s++) - '0')
#define getname(s)	(nome[0] = *s++, nome)

	static int32 locks[10];
	lua_Object reg[10];
	char nome[2];
	char *s = luaL_check_string(1);
	nome[1] = 0;
	while (1) {
		switch (*s++) {
			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9':
				lua_pushnumber(*(s - 1) - '0');
				break;
			case 'c':
				reg[getnum(s)] = lua_createtable();
				break;
			case 'C':
				{
					lua_CFunction f = lua_getcfunction(lua_getglobal(getname(s)));
					lua_pushCclosure(f, getnum(s));
					break;
				}
			case 'P':
				reg[getnum(s)] = lua_pop();
				break;
			case 'g':
				{
					int32 n=getnum(s);
					reg[n] = lua_getglobal(getname(s));
					break;
				}
			case 'G':
				{
					int32 n = getnum(s);
					reg[n] = lua_rawgetglobal(getname(s));
					break;
				}
			case 'l':
				locks[getnum(s)] = lua_ref(1);
				break;
			case 'L':
				locks[getnum(s)] = lua_ref(0);
				break;
			case 'r':
				{
					int32 n = getnum(s);
					reg[n] = lua_getref(locks[getnum(s)]);
					break;
				}
			case 'u':
				lua_unref(locks[getnum(s)]);
				break;
			case 'p':
				{
					int32 n = getnum(s);
					reg[n] = lua_getparam(getnum(s));
					break;
				}
			case '=':
				lua_setglobal(getname(s));
				break;
			case 's':
				lua_pushstring(getname(s));
				break;
			case 'o':
				lua_pushobject(reg[getnum(s)]);
				break;
			case 'f':
				lua_call(getname(s));
				break;
			case 'i':
				reg[getnum(s)] = lua_gettable();
				break;
			case 'I':
				reg[getnum(s)] = lua_rawgettable();
				break;
			case 't':
				lua_settable();
				break;
			case 'T':
				lua_rawsettable();
				break;
			default:
				luaL_verror("unknown command in `testC': %c", *(s - 1));
		}
		if (*s == 0)
			return;
		if (*s++ != ' ')
			lua_error("missing ` ' between commands in `testC'");
	}
}
Example #4
0
	 LuaRef LuaRef::fromTop( lua_State* L )
	 {
		 xassert(L != NULL);
		 int handle = lua_ref(L, true);
		 return LuaRef(L, handle);
	 }
Example #5
0
static void GetParam(void)
{
  getparam_data gp;
  lua_Object func;
  const char* title = luaL_check_string(1);
  void* user_data = (void*)&gp;
  const char* format = luaL_check_string(3);
  int param_count, param_extra, i, size, ret,
      line_size = 0, lua_param_start = 4;
  const char* f = format;
  const char* s;
  void* param_data[50];
  char param_type[50];

  gp.has_func = 0;
  gp.func_ref = 0;

  memset(param_data, 0, sizeof(void*)*50);
  memset(param_type, 0, sizeof(char)*50);

  param_count = iupGetParamCount(format, &param_extra);

  for (i = 0; i < param_count; i++)
  {
    char t = iupGetParamType(f, &line_size);

    if (t == 't') /* if separator */
    {
      f += line_size;
      i--; /* compensate next increment */
      continue;
    }

    switch(t)
    {
    case 'b':
    case 'i':
    case 'l':
      param_data[i] = malloc(sizeof(int));
      *(int*)(param_data[i]) = (int)luaL_check_number(lua_param_start); lua_param_start++;
      break;
    case 'a':
    case 'r':
      param_data[i] = malloc(sizeof(float));
      *(float*)(param_data[i]) = (float)luaL_check_number(lua_param_start); lua_param_start++;
      break;
    case 's':
    case 'm':
      s = luaL_check_string(lua_param_start); lua_param_start++;
      size = strlen(s);
      if (size < 512)
        param_data[i] = malloc(512);
      else
        param_data[i] = malloc(2*size);
      memcpy(param_data[i], s, size+1);
      break;
    }

    param_type[i] = t;
    f += line_size;
  }

  func = lua_getparam(2);
  if (lua_isfunction(func))
  {
    lua_pushobject(func);
    gp.func_ref = lua_ref(1);
    gp.has_func = 1;
  }

  ret = IupGetParamv(title, param_action, user_data, format, param_count, param_extra, param_data);

  lua_pushnumber(ret);

  if (ret)
  {
    for (i = 0; i < param_count; i++)
    {
      switch(param_type[i])
      {
      case 'b':
      case 'i':
      case 'l':
        lua_pushnumber(*(int*)(param_data[i]));
        break;
      case 'a':
      case 'r':
        lua_pushnumber(*(float*)(param_data[i]));
        break;
      case 's':
      case 'm':
        lua_pushstring((char*)(param_data[i]));
        break;
      }
    }
  }

  for (i = 0; i < param_count; i++)
  {
    free(param_data[i]);
  }

  if (gp.has_func)
    lua_unref(gp.func_ref);
}
Example #6
0
static int GetParam(lua_State *L)
{
  getparam_data gp;
  const char* title = luaL_checkstring(L, 1);
  void* user_data = (void*)&gp;
  const char* format = luaL_checkstring(L, 3);
  int param_count, param_extra, i, size, ret,
      line_size = 0, lua_param_start = 4;
  const char* f = format;
  const char* s;
  void* param_data[50];
  char param_type[50];

  gp.L = L;
  gp.has_func = 0;
  gp.func_ref = 0;

  memset(param_data, 0, sizeof(void*)*50);
  memset(param_type, 0, sizeof(char)*50);

  param_count = iupGetParamCount(format, &param_extra);

  for (i = 0; i < param_count; i++)
  {
    char t = iupGetParamType(f, &line_size);

    if (t == 't') /* if separator */
    {
      f += line_size;
      i--; /* compensate next increment */
      continue;
    }

    switch(t)
    {
    case 'b':
/*  TO DO: add this code some day:
      if (lua_isboolean(L, lua_param_start))
      {
        param_data[i] = malloc(sizeof(int));
        *(int*)(param_data[i]) = lua_toboolean(L, lua_param_start); lua_param_start++;
        break;
      }  */
      /* else continuous and get an integer */
    case 'i':
    case 'l':
      param_data[i] = malloc(sizeof(int));
      *(int*)(param_data[i]) = luaL_checkinteger(L, lua_param_start); lua_param_start++;
      break;
    case 'a':
    case 'r':
      param_data[i] = malloc(sizeof(float));
      *(float*)(param_data[i]) = (float)luaL_checknumber(L, lua_param_start); lua_param_start++;
      break;
    case 'f':
    case 'c':
    case 's':
    case 'm':
      s = luaL_checkstring(L, lua_param_start); lua_param_start++;
      size = strlen(s);
      if (size < 512)
        param_data[i] = malloc(512);
      else
        param_data[i] = malloc(2*size);
      memcpy(param_data[i], s, size+1);
      break;
    }

    param_type[i] = t;
    f += line_size;
  }

  if (lua_isfunction(L, 2))
  {
    lua_pushvalue(L, 2);
    gp.func_ref = lua_ref(L, 1);
    gp.has_func = 1;
  }

  ret = IupGetParamv(title, param_action, user_data, format, param_count, param_extra, param_data);

  lua_pushboolean(L, ret);

  if (ret)
  {
    for (i = 0; i < param_count; i++)
    {
      switch(param_type[i])
      {
      case 'b':
      case 'i':
      case 'l':
        lua_pushinteger(L, *(int*)(param_data[i]));
        break;
      case 'a':
      case 'r':
        lua_pushnumber(L, *(float*)(param_data[i]));
        break;
      case 'f':
      case 'c':
      case 's':
      case 'm':
        lua_pushstring(L, (char*)(param_data[i]));
        break;
      }
    }
  }

  for (i = 0; i < param_count; i++)
  {
    free(param_data[i]);
  }

  if (gp.has_func)
    lua_unref(L, gp.func_ref);

  if (ret)
    return param_count+1;
  else
    return 1;
}
Example #7
0
void LuaBase::registerLua() {
	// Register system table
	lua_Object system_table = lua_createtable();
	lua_pushobject(system_table);
	lua_setglobal("system");

	lua_pushobject(system_table);
	refSystemTable = lua_ref(1);

	for (unsigned i = 0; i < ARRAYSIZE(system_defaults); i++) {
		lua_pushobject(lua_getref(refSystemTable));
		lua_pushstring(system_defaults[i].name);
		lua_pushnumber(system_defaults[i].key);
		lua_settable();
	}

	// Create and populate system.controls table
	lua_Object controls_table = lua_createtable();
	lua_pushobject(lua_getref(refSystemTable));
	lua_pushstring("controls");
	lua_pushobject(controls_table);
	lua_settable();

	for (int i = 0; controls[i].name; i++) {
		lua_pushobject(controls_table);
		lua_pushstring(controls[i].name);
		lua_pushnumber(controls[i].key);
		lua_settable();
	}

	lua_pushobject(lua_getref(refSystemTable));
	lua_pushstring("camChangeHandler");
	lua_pushcfunction(LUA_OPCODE(LuaBase, dummyHandler));
	lua_settable();

	lua_pushobject(lua_getref(refSystemTable));
	lua_pushstring("axisHandler");
	lua_pushcfunction(LUA_OPCODE(LuaBase, dummyHandler));
	lua_settable();

	lua_pushobject(lua_getref(refSystemTable));
	lua_pushstring("buttonHandler");
	lua_pushcfunction(LUA_OPCODE(LuaBase, dummyHandler));
	lua_settable();

	lua_pushobject(lua_getglobal("type"));
	refTypeOverride = lua_ref(true);
	lua_pushCclosure(LUA_OPCODE(LuaBase, typeOverride), 0);
	lua_setglobal("type");

	// Register constants for box types
	lua_pushnumber(Sector::NoneType);
	lua_setglobal("NONE");
	lua_pushnumber(Sector::WalkType);
	lua_setglobal("WALK");
	lua_pushnumber(Sector::CameraType);
	lua_setglobal("CAMERA");
	lua_pushnumber(Sector::SpecialType);
	lua_setglobal("SPECIAL");
	lua_pushnumber(Sector::HotType);
	lua_setglobal("HOT");

	lua_pushobject(lua_setfallback("concat", LUA_OPCODE(LuaBase, concatFallback)));
	refOldConcatFallback = lua_ref(1);

	// initialize Text globals
	lua_pushstring("x");
	refTextObjectX = lua_ref(true);
	lua_pushstring("y");
	refTextObjectY = lua_ref(true);
	lua_pushstring("font");
	refTextObjectFont = lua_ref(true);
	lua_pushstring("width");
	refTextObjectWidth = lua_ref(true);
	lua_pushstring("height");
	refTextObjectHeight = lua_ref(true);
	lua_pushstring("fgcolor");
	refTextObjectFGColor = lua_ref(true);
	lua_pushstring("bgcolor");
	refTextObjectBGColor = lua_ref(true);
	lua_pushstring("fxcolor");
	refTextObjectFXColor = lua_ref(true);
	lua_pushstring("hicolor");
	refTextObjectHIColor = lua_ref(true);
	lua_pushstring("duration");
	refTextObjectDuration = lua_ref(true);
	lua_pushstring("center");
	refTextObjectCenter = lua_ref(true);
	lua_pushstring("ljustify");
	refTextObjectLJustify = lua_ref(true);
	lua_pushstring("rjustify");
	refTextObjectRJustify = lua_ref(true);
	lua_pushstring("volume");
	refTextObjectVolume = lua_ref(true);
	lua_pushstring("pan");
	refTextObjectPan = lua_ref(true);
	lua_pushstring("background");
	refTextObjectBackground = lua_ref(true);
}
Example #8
0
static int 
e_thread_new(lua_State *L)
{
    lua_State        *L1;
    lua_ecos_state_t *state1;
    cyg_addrword_t   *lst;
    int ref, prio;

    // thread priority 
    prio = luaL_checkint(L, 1);

    // check arguments 
    luaL_checktype(L, 2, LUA_TFUNCTION);
    luaL_checktype(L, 3, LUA_TTABLE);
 
    // create new lua thread 
    L1 = lua_newthread(L);
    if (L1 == NULL)
    {
        luaL_error(L, "cannot create new stack");
        return 0; 
    }
    
    // free any terminated threads
    lua_ecosfreeterminatedthreads();
 
    // allocate new lua-ecos thread struct 
    state1 = (lua_ecos_state_t *) lua_getuserspace(L1);
    state1->thread = (lua_ecos_thread_t*) malloc(sizeof(lua_ecos_thread_t));
    if (!state1->thread)
    {
        luaL_error(L, "out of memory");
        return 0;
    }
    
    // reference the lua thread, so it won't get garbage collected 
    state1->thread->ref = lua_ref(L, 4);
    
    // adjust stacksize 
    lua_settop(L, 3);

    // move table and function from parent to child
    ref = lua_ref(L, 2);
    lua_getref(L1, ref);
    lua_unref(L, ref);  
    ref = lua_ref(L, 2);
    lua_getref(L1, ref);
    lua_unref(L, ref);  
    
    // create new ecos thread 
    state1->thread->stack = malloc(CYGNUM_LUA_THREAD_STACK_SIZE);
    if (!state1->thread->stack) 
    {
        lua_unref(L, state1->thread->ref);
        free(state1->thread);
        luaL_error(L, "out of memory");
        return 0;
    }
    
    state1->thread->next = NULL;

    cyg_thread_create(prio, thread_prog, (cyg_addrword_t) L1, "Lua thread",
                      state1->thread->stack,
                      CYGNUM_LUA_THREAD_STACK_SIZE,
                      &state1->thread->handle,
                      &state1->thread->thread_s);
    
    // create a pointer to new lua state wich will be passed back 
    lst = (cyg_addrword_t *) lua_newuserdata(L, sizeof(cyg_addrword_t));
    *lst = (cyg_addrword_t) L1;
    set_user_data_type(L, E_THREAD);

    return 1;
}
Example #9
0
/* CALL FUNCTION HOOK */
static void callhook(lua_State *L, lua_Function func, char *file, int line) {
    check_start(L);
    Meta **array = get_metadata_array(L);
    if (!array) return; // check if exists (call profile_stop ?)

    if (STACK_INDEX > MEM_BLOCKSIZE - 1) {
        // Reached memory limit, relocated to double.
        int blocksize = MEM_BLOCKSIZE * 2;

        array = realloc(array, blocksize * sizeof(Meta **));

        if (array) {
            lua_unref(L, META_REF); // Remove the old reference (new block of memory).
            lua_pushuserdata(L, array); // Saves the new reference.
            META_REF = lua_ref(L, 1);
            MEM_BLOCKSIZE = blocksize; // Updates the size of the memory block.
        } else {
            lua_error(L, "profiler: out of memory!");
            return; // suppress inspect
        }
    }

    char *func_name;
    char *func_scope;

    if (lua_isfunction(L, func)) {
        func_scope = lua_getobjname(L, func, &func_name);
        Meta *meta = (Meta *) malloc(sizeof(Meta));

        meta->fun_name = func_name ? func_name : "unnamed";
        if (func_scope && strlen(func_scope) > 0) {
            meta->fun_scope = func_scope;
        } else {
            meta->fun_scope = "unknown";
        }
        meta->func_file = file ? file : "unnamed";
        meta->stack_level = STACK_SIZE;
        meta->line = line;

        Children *children = (Children *) malloc(sizeof(Children));
        meta->children = children;
        children->index = 0;
        children->list = NULL;
        children->size = 20;

        Measure *measure = (Measure *) malloc(sizeof(Measure));
        measure->begin = clock();
        meta->measure = measure;

        stack_record.meta = meta;
        push(&stack, stack_record);

        if (STACK_SIZE == 0) {
            array[STACK_INDEX] = meta;
            STACK_INDEX++;
        }
        STACK_SIZE++;

    } else if (STACK_SIZE > 0) {
        STACK_RECORD top_record = pop(&stack);
        STACK_RECORD *new_record = next(&stack);

        Meta *meta = top_record.meta;
        meta->measure->end = clock();
        meta->measure->time_spent = calc_time_spent(meta->measure);

        if (new_record != NULL && meta->measure->time_spent >= PROFILE_RECORD_TIME) {
            Meta *_meta = new_record->meta;
            if (!_meta->children->list) { // already allocated ?
                _meta->children->list = (Meta **) malloc(_meta->children->size * sizeof(Meta **));
                if (!_meta->children->list) lua_error(L, "out of memory");
            }
            if (_meta->children->index > _meta->children->size - 1) {
                _meta->children->size *= 2; // more
                _meta->children->list = (Meta **) realloc(_meta->children->list,
                                                          _meta->children->size * sizeof(Meta **));
                if (!_meta->children->list) lua_error(L, "out of memory");
            }
            _meta->children->list[_meta->children->index] = meta;
            _meta->children->index++;
        }
        STACK_SIZE--;
    }
}