#[cfg(not(feature="run-service"))]
fn no_op(_lua:&mlua::Lua,_time:Option<super::number::Number>)->mlua::Result<f64>{
	Ok(0.0)
}
fn tick(_lua:&mlua::Lua,_:())->mlua::Result<f64>{
	Ok(0.0)
}
// This is used to avoid calling coroutine.yield from the rust side.
const LUA_WAIT:&str=
"local coroutine_yield=coroutine.yield
local schedule_thread=schedule_thread
return function(dt)
	schedule_thread(dt)
	return coroutine_yield()
end";

pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
	let coroutine_table=globals.get::<mlua::Table>("coroutine")?;
	#[cfg(feature="run-service")]
	let schedule_thread=lua.create_function(crate::scheduler::schedule_thread)?;
	#[cfg(not(feature="run-service"))]
	let schedule_thread=lua.create_function(no_op)?;

	//create wait function environment
	let wait_env=lua.create_table()?;
	wait_env.raw_set("coroutine",coroutine_table)?;
	wait_env.raw_set("schedule_thread",schedule_thread)?;

	//construct wait function from Lua code
	let wait=lua.load(LUA_WAIT)
	.set_name("wait")
	.set_environment(wait_env)
	.call::<mlua::Function>(())?;

	globals.raw_set("wait",wait)?;

	// TODO: move this somewhere it belongs
	let tick=lua.create_function(tick)?;
	globals.raw_set("tick",tick)?;

	Ok(())
}
