Exemplo n.º 1
0
				~proxy_member_caller()
				{
					if (m_called) return;

					m_called = true;

					// don't count the function and self-reference
					// since those will be popped by pcall
					int top = lua_gettop(L) - 2;

					// pcall will pop the function and self reference
					// and all the parameters

					push_args_from_tuple<1>::apply(L, m_args);
# ifdef LUABIND_CPP0x
                    if (pcall(L, std::tuple_size<Tuple>::value + 1, 0))
# else
					if (pcall(L, boost::tuples::length<Tuple>::value + 1, 0))
# endif
					{
						assert(lua_gettop(L) == top + 1);
#ifndef LUABIND_NO_EXCEPTIONS
						throw luabind::error(L);
#else
						error_callback_fun e = get_error_callback();
						if (e) e(L);

						assert(0 && "the lua function threw an error and exceptions are disabled."
								"If you want to handle this error use luabind::set_error_callback()");
						std::terminate();
#endif
					}
					// pops the return values from the function
					stack_pop pop(L, lua_gettop(L) - top);
				}
Exemplo n.º 2
0
				Ret operator[](const Policies& p)
				{
					typedef typename find_conversion_policy<0, Policies>::type converter_policy;
					typename mpl::apply_wrap2<converter_policy,Ret,lua_to_cpp>::type converter;

					m_called = true;

					// don't count the function and self-reference
					// since those will be popped by pcall
					int top = lua_gettop(L) - 2;

					// pcall will pop the function and self reference
					// and all the parameters

					detail::push_args_from_tuple<1>::apply(L, m_args, p);
# ifdef LUABIND_CPP0x
                    if (pcall(L, std::tuple_size<Tuple>::value + 1, 1))
# else
					if (pcall(L, boost::tuples::length<Tuple>::value + 1, 1))
# endif
					{
						assert(lua_gettop(L) == top + 1);
#ifndef LUABIND_NO_EXCEPTIONS
						throw error(L);
#else
						error_callback_fun e = get_error_callback();
						if (e) e(L);

						assert(0 && "the lua function threw an error and exceptions are disabled."
							"If you want to handle this error use luabind::set_error_callback()");
						std::terminate();
#endif
					}

					// pops the return values from the function
					stack_pop pop(L, lua_gettop(L) - top);

					if (converter.match(L, LUABIND_DECORATE_TYPE(Ret), -1) < 0)
					{
						assert(lua_gettop(L) == top + 1);
#ifndef LUABIND_NO_EXCEPTIONS
						throw cast_failed(L, typeid(Ret));
#else
						cast_failed_callback_fun e = get_cast_failed_callback();
						if (e) e(L, typeid(Ret));

						assert(0 && "the lua function's return value could not be converted."
							"If you want to handle this error use luabind::set_error_callback()");
						std::terminate();
#endif
					}

					return converter.apply(L, LUABIND_DECORATE_TYPE(Ret), -1);
				}
Exemplo n.º 3
0
				void operator[](const Policies& p)
				{
					m_called = true;

					// don't count the function and self-reference
					// since those will be popped by pcall
					int top = lua_gettop(L) - 2;

					// pcall will pop the function and self reference
					// and all the parameters

					detail::push_args_from_tuple<1>::apply(L, m_args, p);
					if (pcall(L, boost::tuples::length<Tuple>::value + 1, 0))
					{
						assert(lua_gettop(L) == top + 1);
#ifndef LUABIND_NO_EXCEPTIONS
						throw error(L);
#else
						error_callback_fun e = get_error_callback();
						if (e) e(L);
	
						assert(0 && "the lua function threw an error and exceptions are disabled."
							"If you want to handle this error use luabind::set_error_callback()");
						std::terminate();
#endif
					}
					// pops the return values from the function
					stack_pop pop(L, lua_gettop(L) - top);
				}
Exemplo n.º 4
0
		R call_member_impl(lua_State* L, std::false_type /*void*/, meta::index_list<Indices...>, Args&&... args)
		{
			// don't count the function and self-reference
			// since those will be popped by pcall
			int top = lua_gettop(L) - 2;

			// pcall will pop the function and self reference
			// and all the parameters

			meta::expand_calls_hack((
				specialized_converter_policy_n<Indices, PolicyList, typename unwrapped<Args>::type, cpp_to_lua>().to_lua(L, unwrapped<Args>::get(std::forward<Args>(args))), 0)...
				);

			if (pcall(L, sizeof...(Args) +1, 1))
			{
				assert(lua_gettop(L) == top + 1);
				call_error(L);
			}
			// pops the return values from the function
			stack_pop pop(L, lua_gettop(L) - top);

			specialized_converter_policy_n<0, PolicyList, R, lua_to_cpp> converter;
			if (converter.match(L, decorated_type<R>(), -1) < 0) {
				cast_error<R>(L);
			}

			return converter.to_cpp(L, decorated_type<R>(), -1);
		}
Exemplo n.º 5
0
int LuaInterface::safeCall(int numArgs, int numRets)
{
    assert(hasIndex(-numArgs-1));

    // saves the current stack size for calculating the number of results later
    int previousStackSize = stackSize();

    // pushes error function
    int errorFuncIndex = previousStackSize - numArgs;
    pushCFunction(&LuaInterface::luaErrorHandler);
    insert(errorFuncIndex);

    // calls the function in protected mode (means errors will be caught)
    int ret = pcall(numArgs, LUA_MULTRET, errorFuncIndex);

    remove(errorFuncIndex); // remove error func

     // if there was an error throw an exception
    if(ret != 0)
        throw LuaException(popString());

    int rets = (stackSize() + numArgs + 1) - previousStackSize;
    while(numRets != -1 && rets != numRets) {
        if(rets < numRets) {
            pushNil();
            rets++;
        } else {
            pop();
            rets--;
        }
    }

    // returns the number of results
    return rets;
}
Exemplo n.º 6
0
				~proxy_functor_caller()
				{
					if (m_called) return;

					m_called = true;
					lua_State* L = m_func->lua_state();

					// get the function
					m_func->pushvalue();

					push_args_from_tuple<1>::apply(L, m_args);
					if (pcall(L, boost::tuples::length<Tuple>::value, 0))
					{ 
#ifndef LUABIND_NO_EXCEPTIONS
						throw luabind::error(L);
#else
						error_callback_fun e = get_error_callback();
						if (e) e(L);
	
						/*assert(0 && "the lua function threw an error and exceptions are disabled."
							"if you want to handle this error use luabind::set_error_callback()");
						std::terminate();*/
#endif
					}
				}
Exemplo n.º 7
0
bool LuaState::callFunction(std::string fname, LuaValue& value) {
	
	//place the function on the stack
	getGlobal(fname);
	
	//push the argument
	value.push(L);
	
	//do it
	return pcall(1, 0);
}
Exemplo n.º 8
0
int lqtL_pcall_debug (lua_State *L, int narg, int nres, int err) {
    int status = 0;
    lua_getfield(L, LUA_REGISTRYINDEX, LQT_PCALL);
    lqt_PCallPtr pcall = (lqt_PCallPtr)lua_touserdata(L, -1);
    lua_pop(L, 1);
    if (pcall) {
        status = pcall(L, narg, nres, err);
    } else {
        status = lqtL_pcall_debug_default(L, narg, nres, err);
    }
    return status;
}
Exemplo n.º 9
0
static int for_each(lua_State * L) {
    token_table const & t = to_token_table(L, 1);
    luaL_checktype(L, 2, LUA_TFUNCTION); // user-fun
    for_each(t, [&](char const * k, token_info const & info) {
            lua_pushvalue(L, 2);
            lua_pushstring(L, k);
            lua_pushboolean(L, info.is_command());
            push_name(L, info.value());
            lua_pushinteger(L, info.expr_precedence());
            pcall(L, 4, 0, 0);
        });
    return 0;
}
Exemplo n.º 10
0
static int lua_rb_map_for_each(lua_State * L) {
    // Remark: we take a copy of the map to make sure
    // for_each will not crash if the map is updated while being
    // traversed.
    // The copy operation is very cheap O(1).
    lua_rb_map m(to_lua_rb_map(L, 1)); // map
    luaL_checktype(L, 2, LUA_TFUNCTION); // user-fun
    m.for_each([&](luaref const & k, luaref const & v) {
        lua_pushvalue(L, 2); // push user-fun
        k.push();
        v.push();
        pcall(L, 2, 0, 0);
        });
    return 0;
}
Exemplo n.º 11
0
				void operator[](const Policies& p)
				{
					m_called = true;
					lua_State* L = m_func->lua_state();
#ifndef LUABIND_NO_ERROR_CHECKING
					if (L == 0)
					{
	#ifndef LUABIND_NO_EXCEPTIONS
						throw error(L); 
	#else
						error_callback_fun e = get_error_callback();
						if (e) e(L);
	
						/*assert(0 && "tried to call uninitialized functor object."
							"if you want to handle this error use luabind::set_error_callback()");
						std::terminate();*/
	#endif
					}
#endif


					// get the function
					m_func->pushvalue();

					detail::push_args_from_tuple<1>::apply(L, m_args, p);
					if (pcall(L, boost::tuples::length<Tuple>::value, 0))
					{ 
#ifndef LUABIND_NO_EXCEPTIONS
						throw error(L); 
#else
						error_callback_fun e = get_error_callback();
						if (e) e(L);
	
						assert(0 && "the lua function threw an error and exceptions are disabled."
							"if you want to handle this error use luabind::set_error_callback()");
						std::terminate();
#endif
					}
				}
Exemplo n.º 12
0
void main_loop(void *data) {
  MainLoopData* loopData = (MainLoopData*)data;
  // TODO use registry to get love.update and love.draw?

  chdir("love");

  timer_step();
  graphics_clear();
  matrixstack_origin();
  lua_rawgeti(loopData->luaState, LUA_REGISTRYINDEX, loopData->errhand);
  lua_getglobal(loopData->luaState, "love");
  lua_pushstring(loopData->luaState, "update");

  lua_rawget(loopData->luaState, -2);
  lua_pushnumber(loopData->luaState, timer_getDelta());
  pcall(loopData->luaState, 1);

  lua_pushstring(loopData->luaState, "draw");
  lua_rawget(loopData->luaState, -2);

  pcall(loopData->luaState, 0);

  graphics_DisplayState curState = *graphics_getState();
  matrixstack_push();

  mouse_cursor_draw();

  matrixstack_pop();
  graphics_setState(&curState);

  graphics_swap();

  lua_pop(loopData->luaState, 1);

  SDL_Event event;
  while(SDL_PollEvent(&event)) {
    switch(event.type) {
    case SDL_KEYDOWN:
      keyboard_keypressed(event.key.keysym.sym);
      break;
    case SDL_KEYUP:
      keyboard_keyreleased(event.key.keysym.sym);
      break;
    case SDL_TEXTINPUT:
      keyboard_textInput(event.text.text);
      break;
    case SDL_MOUSEMOTION:
      mouse_mousemoved(event.motion.x, event.motion.y);
      break;
    case SDL_MOUSEBUTTONDOWN:
      mouse_mousepressed(event.button.x, event.button.y, event.button.button);
      break;
    case SDL_MOUSEBUTTONUP:
      mouse_mousereleased(event.button.x, event.button.y, event.button.button);
      break;
    case SDL_JOYDEVICEADDED:
      joystick_deviceAdded(event.jdevice.which);
      break;
    case SDL_JOYDEVICEREMOVED:
      joystick_deviceRemoved(event.jdevice.which);
      break;
    case SDL_JOYBUTTONUP:
      joystick_buttonReleased(event.jbutton.which, event.jbutton.button);
      break;
    case SDL_JOYBUTTONDOWN:
      joystick_buttonPressed(event.jbutton.which, event.jbutton.button);
      break;
    case SDL_JOYAXISMOTION:
      joystick_axisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
      break;
    case SDL_CONTROLLERBUTTONUP:
      joystick_controllerButtonReleased(event.jbutton.which, event.jbutton.button);
      break;
    case SDL_CONTROLLERBUTTONDOWN:
      joystick_controllerButtonPressed(event.jbutton.which, event.jbutton.button);
      break;
    case SDL_CONTROLLERAXISMOTION:
      joystick_controllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
      break;


#ifndef EMSCRIPTEN
    case SDL_QUIT:
      exit(0);
#endif
    }
  }

  audio_updateStreams();
  //lua_gc(loopData->luaState, LUA_GCCOLLECT, 0);
}
Exemplo n.º 13
0
int main() {
  lua_State *lua = luaL_newstate();
  luaL_openlibs(lua);


  motor_Config config;
  l_motor_register(lua);
  l_audio_register(lua);
  l_graphics_register(lua);
  l_image_register(lua);
  l_keyboard_register(lua);
  l_mouse_register(lua);
  l_filesystem_register(lua);
  l_timer_register(lua);
  l_math_register(lua);
  l_event_register(lua);
  l_joystick_register(lua);

  chdir("/love");
  l_boot(lua, &config);

  if(config.identity) {
    filesystem_setIdentity(config.identity, false);
  }

  image_init();
  joystick_init();
  keyboard_init();
  graphics_init(config.window.width, config.window.height);
  audio_init();
  math_init();


  if(luaL_dofile(lua, "main.lua")) {
    printf("Error: %s\n", lua_tostring(lua, -1));
  }

  lua_pushcfunction(lua, errorhandler);
  lua_getglobal(lua, "love");
  lua_pushstring(lua, "load");
  lua_rawget(lua, -2);
  /*if(lua_pcall(lua, 0, 0, 1)) {
    printf("Error in love.load: %s\n", lua_tostring(lua, -1));
  }
  */
  pcall(lua, 0);
  lua_pop(lua, 1);

  lua_pushcfunction(lua, errorhandler);

  mainLoopData.luaState = lua;
  mainLoopData.errhand = luaL_ref(lua, LUA_REGISTRYINDEX);

  timer_init();
#ifdef EMSCRIPTEN
  emscripten_set_main_loop_arg(main_loop, &mainLoopData, 0, 1);
#else
  for(;;) {
    main_loop(&mainLoopData);
  }
#endif
}
Exemplo n.º 14
0
 void State::pcall(int nargs, int nresults) {
     pcall(nargs, nresults, 0);
 }
Exemplo n.º 15
0
static void exec(lua_State * L) {
    pcall(L, 0, LUA_MULTRET, 0);
}
Exemplo n.º 16
0
				Ret operator[](const Policies& p)
				{
					typedef typename detail::find_conversion_policy<0, Policies>::type converter_policy;
					typename converter_policy::template generate_converter<Ret, lua_to_cpp>::type converter;

					m_called = true;
					lua_State* L = m_func->lua_state();
#ifndef LUABIND_NO_ERROR_CHECKING
					if (L == 0)
					{
	#ifndef LUABIND_NO_EXCEPTIONS
						throw error(L); 
	#else
						error_callback_fun e = get_error_callback();
						if (e) e(L);
	
						/*assert(0 && "tried to call uninitialized functor object."
							"if you want to handle this error use luabind::set_error_callback()");
						std::terminate();*/
	#endif
					}
#endif

					detail::stack_pop popper(L, 1); // pop the return value

					// get the function
					m_func->pushvalue();

					detail::push_args_from_tuple<1>::apply(L, m_args, p);
					if (pcall(L, boost::tuples::length<Tuple>::value, 1))
					{ 
#ifndef LUABIND_NO_EXCEPTIONS
						throw error(L);
#else
						error_callback_fun e = get_error_callback();
						if (e) e(L);
	
						/*assert(0 && "the lua function threw an error and exceptions are disabled."
							"if you want to handle this error use luabind::set_error_callback()");
						std::terminate();*/
#endif
					}

#ifndef LUABIND_NO_ERROR_CHECKING

					if (converter.match(L, LUABIND_DECORATE_TYPE(Ret), -1) < 0)
					{
#ifndef LUABIND_NO_EXCEPTIONS
						throw cast_failed(L, LUABIND_TYPEID(Ret));
#else
						cast_failed_callback_fun e = get_cast_failed_callback();
						if (e) e(L, LUABIND_TYPEID(Ret));

						/*assert(0 && "the lua function's return value could not be converted."
							"if you want to handle this error use luabind::set_error_callback()");
						std::terminate();*/
#endif
					}
#endif
					return converter.apply(L, LUABIND_DECORATE_TYPE(Ret), -1);
				}
Exemplo n.º 17
0
bool LuaState::callFunction(std::string fname) {
	getGlobal(fname);
	return pcall(0, 0);
}