Procházet zdrojové kódy

Switch World, PhysicsWorld, SoundWorld, ResourcePackage to fulluserdata. Also provide a macro-based mechanism to add type checking to lightuserdata objects. Also simplify LuaSystem and do not export lua binding functions.

Daniele Bartolini před 12 roky
rodič
revize
cb6170edc8

+ 1 - 1
engine/lua/LuaAccelerometer.cpp

@@ -33,7 +33,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int accelerometer_orientation(lua_State* L)
+static int accelerometer_orientation(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 24 - 24
engine/lua/LuaActor.cpp

@@ -32,7 +32,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_enable_gravity(lua_State* L)
+static int actor_enable_gravity(lua_State* L)
 {
 	LuaStack stack(L);
 	Actor* actor = stack.get_actor(1);
@@ -43,7 +43,7 @@ CE_EXPORT int actor_enable_gravity(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_disable_gravity(lua_State* L)
+static int actor_disable_gravity(lua_State* L)
 {
 	LuaStack stack(L);
 	Actor* actor = stack.get_actor(1);
@@ -53,7 +53,7 @@ CE_EXPORT int actor_disable_gravity(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_enable_collision(lua_State* L)
+static int actor_enable_collision(lua_State* L)
 {
 	LuaStack stack(L);
 	Actor* actor = stack.get_actor(1);
@@ -64,7 +64,7 @@ CE_EXPORT int actor_enable_collision(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_disable_collision(lua_State* L)
+static int actor_disable_collision(lua_State* L)
 {
 	LuaStack stack(L);
 	Actor* actor = stack.get_actor(1);
@@ -75,7 +75,7 @@ CE_EXPORT int actor_disable_collision(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_set_kinematic(lua_State* L)
+static int actor_set_kinematic(lua_State* L)
 {
 	LuaStack stack(L);
 	Actor* actor = stack.get_actor(1);
@@ -86,7 +86,7 @@ CE_EXPORT int actor_set_kinematic(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_clear_kinematic(lua_State* L)
+static int actor_clear_kinematic(lua_State* L)
 {
 	LuaStack stack(L);
 	Actor* actor = stack.get_actor(1);
@@ -97,7 +97,7 @@ CE_EXPORT int actor_clear_kinematic(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_move(lua_State* L)
+static int actor_move(lua_State* L)
 {
 	LuaStack stack(L);
 	Actor* actor = stack.get_actor(1);
@@ -109,7 +109,7 @@ CE_EXPORT int actor_move(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_is_static(lua_State* L)
+static int actor_is_static(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -120,7 +120,7 @@ CE_EXPORT int actor_is_static(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_is_dynamic(lua_State* L)
+static int actor_is_dynamic(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -131,7 +131,7 @@ CE_EXPORT int actor_is_dynamic(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_is_kinematic(lua_State* L)
+static int actor_is_kinematic(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -142,7 +142,7 @@ CE_EXPORT int actor_is_kinematic(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_linear_damping(lua_State* L)
+static int actor_linear_damping(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -153,7 +153,7 @@ CE_EXPORT int actor_linear_damping(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_set_linear_damping(lua_State* L)
+static int actor_set_linear_damping(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -165,7 +165,7 @@ CE_EXPORT int actor_set_linear_damping(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_angular_damping(lua_State* L)
+static int actor_angular_damping(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -176,7 +176,7 @@ CE_EXPORT int actor_angular_damping(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_set_angular_damping(lua_State* L)
+static int actor_set_angular_damping(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -188,7 +188,7 @@ CE_EXPORT int actor_set_angular_damping(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_linear_velocity(lua_State* L)
+static int actor_linear_velocity(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -199,7 +199,7 @@ CE_EXPORT int actor_linear_velocity(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_set_linear_velocity(lua_State* L)
+static int actor_set_linear_velocity(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -211,7 +211,7 @@ CE_EXPORT int actor_set_linear_velocity(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_angular_velocity(lua_State* L)
+static int actor_angular_velocity(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -222,7 +222,7 @@ CE_EXPORT int actor_angular_velocity(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_set_angular_velocity(lua_State* L)
+static int actor_set_angular_velocity(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -234,7 +234,7 @@ CE_EXPORT int actor_set_angular_velocity(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_add_impulse(lua_State* L)
+static int actor_add_impulse(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -247,7 +247,7 @@ CE_EXPORT int actor_add_impulse(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_add_impulse_at(lua_State* L)
+static int actor_add_impulse_at(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -261,7 +261,7 @@ CE_EXPORT int actor_add_impulse_at(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_push(lua_State* L)
+static int actor_push(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -275,7 +275,7 @@ CE_EXPORT int actor_push(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_is_sleeping(lua_State* L)
+static int actor_is_sleeping(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -286,7 +286,7 @@ CE_EXPORT int actor_is_sleeping(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_wake_up(lua_State* L)
+static int actor_wake_up(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -297,7 +297,7 @@ CE_EXPORT int actor_wake_up(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int actor_unit(lua_State* L)
+static int actor_unit(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 23 - 23
engine/lua/LuaCamera.cpp

@@ -35,7 +35,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_local_position(lua_State* L)
+static int camera_local_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -46,7 +46,7 @@ CE_EXPORT int camera_local_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_local_rotation(lua_State* L)
+static int camera_local_rotation(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -57,7 +57,7 @@ CE_EXPORT int camera_local_rotation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_local_pose(lua_State* L)
+static int camera_local_pose(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -68,7 +68,7 @@ CE_EXPORT int camera_local_pose(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_world_position(lua_State* L)
+static int camera_world_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -79,7 +79,7 @@ CE_EXPORT int camera_world_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_world_rotation(lua_State* L)
+static int camera_world_rotation(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -90,7 +90,7 @@ CE_EXPORT int camera_world_rotation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_world_pose(lua_State* L)
+static int camera_world_pose(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -101,7 +101,7 @@ CE_EXPORT int camera_world_pose(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_set_local_position(lua_State* L)
+static int camera_set_local_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -114,7 +114,7 @@ CE_EXPORT int camera_set_local_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_set_local_rotation(lua_State* L)
+static int camera_set_local_rotation(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -127,7 +127,7 @@ CE_EXPORT int camera_set_local_rotation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_set_local_pose(lua_State* L)
+static int camera_set_local_pose(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -140,7 +140,7 @@ CE_EXPORT int camera_set_local_pose(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_set_projection_type(lua_State* L)
+static int camera_set_projection_type(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -152,7 +152,7 @@ CE_EXPORT int camera_set_projection_type(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_projection_type(lua_State* L)
+static int camera_projection_type(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -163,7 +163,7 @@ CE_EXPORT int camera_projection_type(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_fov(lua_State* L)
+static int camera_fov(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -174,7 +174,7 @@ CE_EXPORT int camera_fov(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_set_fov(lua_State* L)
+static int camera_set_fov(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -186,7 +186,7 @@ CE_EXPORT int camera_set_fov(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_aspect(lua_State* L)
+static int camera_aspect(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -197,7 +197,7 @@ CE_EXPORT int camera_aspect(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_set_aspect(lua_State* L)
+static int camera_set_aspect(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -209,7 +209,7 @@ CE_EXPORT int camera_set_aspect(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_near_clip_distance(lua_State* L)
+static int camera_near_clip_distance(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -220,7 +220,7 @@ CE_EXPORT int camera_near_clip_distance(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_set_near_clip_distance(lua_State* L)
+static int camera_set_near_clip_distance(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -232,7 +232,7 @@ CE_EXPORT int camera_set_near_clip_distance(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_far_clip_distance(lua_State* L)
+static int camera_far_clip_distance(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -243,7 +243,7 @@ CE_EXPORT int camera_far_clip_distance(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_set_far_clip_distance(lua_State* L)
+static int camera_set_far_clip_distance(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -255,7 +255,7 @@ CE_EXPORT int camera_set_far_clip_distance(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_set_orthographic_metrics(lua_State* L)
+static int camera_set_orthographic_metrics(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -270,7 +270,7 @@ CE_EXPORT int camera_set_orthographic_metrics(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_set_viewport_metrics(lua_State* L)
+static int camera_set_viewport_metrics(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -285,7 +285,7 @@ CE_EXPORT int camera_set_viewport_metrics(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_screen_to_world(lua_State* L)
+static int camera_screen_to_world(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -297,7 +297,7 @@ CE_EXPORT int camera_screen_to_world(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int camera_world_to_screen(lua_State* L)
+static int camera_world_to_screen(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 5 - 5
engine/lua/LuaController.cpp

@@ -33,7 +33,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int controller_move(lua_State* L)
+static int controller_move(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -45,7 +45,7 @@ CE_EXPORT int controller_move(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int controller_position(lua_State* L)
+static int controller_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -56,7 +56,7 @@ CE_EXPORT int controller_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int controller_collides_up(lua_State* L)
+static int controller_collides_up(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -67,7 +67,7 @@ CE_EXPORT int controller_collides_up(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int controller_collides_down(lua_State* L)
+static int controller_collides_down(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -78,7 +78,7 @@ CE_EXPORT int controller_collides_down(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int controller_collides_sides(lua_State* L)
+static int controller_collides_sides(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 4 - 4
engine/lua/LuaDebugLine.cpp

@@ -34,7 +34,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int debug_line_add_line(lua_State* L)
+static int debug_line_add_line(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -47,7 +47,7 @@ CE_EXPORT int debug_line_add_line(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int debug_line_add_sphere(lua_State* L)
+static int debug_line_add_sphere(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -60,7 +60,7 @@ CE_EXPORT int debug_line_add_sphere(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int debug_line_clear(lua_State* L)
+static int debug_line_clear(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -70,7 +70,7 @@ CE_EXPORT int debug_line_clear(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int debug_line_commit(lua_State* L)
+static int debug_line_commit(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 11 - 11
engine/lua/LuaDevice.cpp

@@ -35,7 +35,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int device_argv(lua_State* L)
+static int device_argv(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -55,7 +55,7 @@ CE_EXPORT int device_argv(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int device_frame_count(lua_State* L)
+static int device_frame_count(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -67,7 +67,7 @@ CE_EXPORT int device_frame_count(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int device_last_delta_time(lua_State* L)
+static int device_last_delta_time(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -79,7 +79,7 @@ CE_EXPORT int device_last_delta_time(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int device_start(lua_State* /*L*/)
+static int device_start(lua_State* /*L*/)
 {
 	device()->start();
 
@@ -87,7 +87,7 @@ CE_EXPORT int device_start(lua_State* /*L*/)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int device_stop(lua_State* /*L*/)
+static int device_stop(lua_State* /*L*/)
 {
 	device()->stop();
 
@@ -95,7 +95,7 @@ CE_EXPORT int device_stop(lua_State* /*L*/)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int device_create_world(lua_State* L)
+static int device_create_world(lua_State* L)
 {
 	LuaStack stack(L);
 	const WorldId world_id = device()->create_world();
@@ -104,7 +104,7 @@ CE_EXPORT int device_create_world(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int device_destroy_world(lua_State* L)
+static int device_destroy_world(lua_State* L)
 {
 	LuaStack stack(L);
 	device()->destroy_world(stack.get_world(1)->id());
@@ -112,7 +112,7 @@ CE_EXPORT int device_destroy_world(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int device_update_world(lua_State* L)
+static int device_update_world(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -124,7 +124,7 @@ CE_EXPORT int device_update_world(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int device_render_world(lua_State* L)
+static int device_render_world(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -136,7 +136,7 @@ CE_EXPORT int device_render_world(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int device_create_resource_package(lua_State* L)
+static int device_create_resource_package(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -147,7 +147,7 @@ CE_EXPORT int device_create_resource_package(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int device_destroy_resource_package(lua_State* L)
+static int device_destroy_resource_package(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 53 - 70
engine/lua/LuaEnvironment.cpp

@@ -38,17 +38,17 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
+namespace lua_system { extern int error_handler(lua_State*); }
+
 //-----------------------------------------------------------------------------
 LuaEnvironment::LuaEnvironment(lua_State* L)
-	: m_state(L)
+	: m_L(L)
 {
 }
 
 //-----------------------------------------------------------------------------
-bool LuaEnvironment::load_and_execute(const char* res_name)
+void LuaEnvironment::load_and_execute(const char* res_name)
 {
-	CE_ASSERT_NOT_NULL(res_name);
-
 	ResourceManager* resman = device()->resource_manager();
 
 	// Load the resource
@@ -56,49 +56,26 @@ bool LuaEnvironment::load_and_execute(const char* res_name)
 	resman->flush();
 	LuaResource* lr = (LuaResource*) resman->data(res_id);
 	
-	lua_getglobal(m_state, "debug");
-	lua_getfield(m_state, -1, "traceback");
-	if (luaL_loadbuffer(m_state, (const char*) lr->program(), lr->size(), res_name) == 0)
-	{
-		if (lua_pcall(m_state, 0, 0, -2) == 0)
-		{
-			// Unloading is OK since the script data has been copied to Lua
-			resman->unload(res_id);
-			lua_pop(m_state, 2); // Pop debug.traceback
-			return true;
-		}
-	}
+	lua_pushcfunction(m_L, lua_system::error_handler);
+	luaL_loadbuffer(m_L, (const char*) lr->program(), lr->size(), res_name);
+	lua_pcall(m_L, 0, 0, -2);
 
-	error();
-	lua_pop(m_state, 2); // Pop debug.traceback
-	return false;
+	// Unloading is OK since the script data has been copied to Lua
+	resman->unload(res_id);
 }
 
 //-----------------------------------------------------------------------------
-bool LuaEnvironment::execute_string(const char* s)
+void LuaEnvironment::execute_string(const char* s)
 {
-	CE_ASSERT_NOT_NULL(s);
-
-	lua_getglobal(m_state, "debug");
-	lua_getfield(m_state, -1, "traceback");
-	if (luaL_loadstring(m_state, s) == 0)
-	{
-		if (lua_pcall(m_state, 0, 0, -2) == 0)
-		{
-			// Unloading is OK since the script data has been copied to Lua
-			lua_pop(m_state, 2); // Pop debug.traceback
-			return true;
-		}
-	}
-
-	error();
-	lua_pop(m_state, 2); // Pop debug.traceback
-	return false;
+	lua_pushcfunction(m_L, lua_system::error_handler);
+	luaL_loadstring(m_L, s);
+	lua_pcall(m_L, 0, 0, -2);
 }
 
 //-----------------------------------------------------------------------------
 void LuaEnvironment::load_module_function(const char* module, const char* name, const lua_CFunction func)
 {
+	luaL_newmetatable(m_L, module);
 	luaL_Reg entry[2];
 
 	entry[0].name = name;
@@ -106,41 +83,65 @@ void LuaEnvironment::load_module_function(const char* module, const char* name,
 	entry[1].name = NULL;
 	entry[1].func = NULL;
 
-	luaL_register(m_state, module, entry);
+	luaL_register(m_L, NULL, entry);
+	lua_setglobal(m_L, module);
+	lua_pop(m_L, -1);
+}
+
+//-----------------------------------------------------------------------------
+void LuaEnvironment::load_module_function(const char* module, const char* name, const char* value)
+{
+	luaL_newmetatable(m_L, module);
+	lua_getglobal(m_L, value);
+	lua_setfield(m_L, -2, name);
+	lua_setglobal(m_L, module);
+}
+
+//-----------------------------------------------------------------------------
+void LuaEnvironment::load_module_constructor(const char* module, const lua_CFunction func)
+{
+	// Create dummy tables to be used as module's metatable
+	lua_createtable(m_L, 0, 1);
+	lua_pushstring(m_L, "__call");
+	lua_pushcfunction(m_L, func);
+	lua_settable(m_L, 1); // dummy.__call = func
+	lua_getglobal(m_L, module);
+	lua_pushvalue(m_L, -2); // Duplicate dummy metatable
+	lua_setmetatable(m_L, -2); // setmetatable(module, dummy)
+	lua_pop(m_L, -1);
 }
 
 //-----------------------------------------------------------------------------
 void LuaEnvironment::load_module_enum(const char* module, const char* name, uint32_t value)
 {
 	// Checks table existance
-	lua_pushstring(m_state, module);
-	lua_rawget(m_state, LUA_GLOBALSINDEX);
-	if (!lua_istable(m_state, -1)) // If not exixts
+	lua_pushstring(m_L, module);
+	lua_rawget(m_L, LUA_GLOBALSINDEX);
+	if (!lua_istable(m_L, -1)) // If not exixts
 	{
 		// Creates table
-		lua_newtable(m_state);
-		lua_setglobal(m_state, module);
+		lua_newtable(m_L);
+		lua_setglobal(m_L, module);
 	}
 
 	// Adds field to table
-	lua_getglobal(m_state, module);
-	lua_pushinteger(m_state, value);
-	lua_setfield(m_state, -2, name);
+	lua_getglobal(m_L, module);
+	lua_pushinteger(m_L, value);
+	lua_setfield(m_L, -2, name);
 }
 
 //-----------------------------------------------------------------------------
-bool LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
+void LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
 {
 	CE_ASSERT_NOT_NULL(func);
 
-	LuaStack stack(m_state);
+	LuaStack stack(m_L);
 
 	va_list vl;
 	va_start(vl, argc);
 
-	lua_getglobal(m_state, "debug");
-	lua_getfield(m_state, -1, "traceback");
-	lua_getglobal(m_state, func);
+	lua_pushcfunction(m_L, lua_system::error_handler);
+	lua_getglobal(m_L, func);
 
 	for (uint8_t i = 0; i < argc; i++)
 	{
@@ -161,25 +162,7 @@ bool LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
 	}
 
 	va_end(vl);
-
-	if (lua_pcall(m_state, argc, 0, -argc - 2) != 0)
-	{
-		error();
-		lua_pop(m_state, 2); // Pop debug.traceback
-		return false;
-	}
-
-	lua_pop(m_state, 2); // Pop debug.traceback
-
-	return true;
-}
-
-//-----------------------------------------------------------------------------
-void LuaEnvironment::error()
-{
-	const char* msg = lua_tostring(m_state, -1);
-	Log::e(msg);
-	lua_pop(m_state, 1);
+	lua_pcall(m_L, argc, 0, -argc - 2);
 }
 
 } // namespace crown

+ 10 - 47
engine/lua/LuaEnvironment.h

@@ -49,20 +49,18 @@ public:
 	
 	LuaEnvironment(lua_State* L);
 	
-	/// Loads and execute the given @a res_name Lua resource, returns
-	/// true if success, false otherwise.
-	bool load_and_execute(const char* res_name);
+	/// Loads and execute the given @a res_name Lua resource.
+	void load_and_execute(const char* res_name);
 
-	bool execute_string(const char* s);
+	/// Loads and executes the given @a s lua string.
+	void execute_string(const char* s);
 
-	/// Load a function which will be used in Lua. @a module is the name of table contenitor,
-	/// @a name is the name of function in module and @func is the pointer to the function.
-	/// _func_ must be a C/lua function (__int32_t function_name(lua_State* L)__)
+	/// Loads the function with the given @a name and @a func into the table @a module.
 	void load_module_function(const char* module, const char* name, const lua_CFunction func);
+	void load_module_function(const char* module, const char* name, const char* value);
+	void load_module_constructor(const char* module, const lua_CFunction func);
 
-	/// Load a enum's value which will be used in Lua. 
-	/// @a module is the name of table contenitor, generally take  enum's name
-	/// @a name is module's name that refears _value_ and @value is an unsigned integer
+	/// Loads the enum with the given @a name and @a value into the table @a module.
 	void load_module_enum(const char* module, const char* name, uint32_t value);
 
 	/// Calls the global function @a func with @a argc argument number.
@@ -70,9 +68,7 @@ public:
 	/// Example call:
 	/// call_global("myfunc", 1, ARGUMENT_FLOAT, 3.14f)
 	/// Returns true if success, false otherwise
-	bool call_global(const char* func, uint8_t argc, ...);
-
-	void error();
+	void call_global(const char* func, uint8_t argc, ...);
 
 private:
 
@@ -82,40 +78,7 @@ private:
 
 private:
 
-	lua_State* m_state;
+	lua_State* m_L;
 };
 
-void load_accelerometer(LuaEnvironment& env);
-void load_actor(LuaEnvironment& env);
-void load_camera(LuaEnvironment& env);
-void load_controller(LuaEnvironment& env);
-void load_debug_line(LuaEnvironment& env);
-void load_device(LuaEnvironment& env);
-void load_float_setting(LuaEnvironment& env);
-void load_gui(LuaEnvironment& env);
-void load_int_setting(LuaEnvironment& env);
-void load_keyboard(LuaEnvironment& env);
-void load_math(LuaEnvironment& env);
-void load_matrix4x4(LuaEnvironment& env);
-void load_matrix4x4box(LuaEnvironment& env);
-void load_mesh(LuaEnvironment& env);
-void load_mouse(LuaEnvironment& env);
-void load_physics_world(LuaEnvironment& env);
-void load_quaternion(LuaEnvironment& env);
-void load_quaternionbox(LuaEnvironment& env);
-void load_raycast(LuaEnvironment& env);
-void load_resource_package(LuaEnvironment& env);
-void load_sound_world(LuaEnvironment& env);
-void load_sprite(LuaEnvironment& env);
-void load_string_setting(LuaEnvironment& env);
-void load_touch(LuaEnvironment& env);
-void load_unit(LuaEnvironment& env);
-void load_vector2(LuaEnvironment& env);
-void load_vector3(LuaEnvironment& env);
-void load_vector3box(LuaEnvironment& env);
-void load_window(LuaEnvironment& env);
-void load_world(LuaEnvironment& env);
-
-CE_EXPORT int32_t luaopen_libcrown(lua_State* L);
-
 } // namespace crown

+ 5 - 5
engine/lua/LuaFloatSetting.cpp

@@ -32,7 +32,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int float_setting_value(lua_State* L)
+static int float_setting_value(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -53,7 +53,7 @@ CE_EXPORT int float_setting_value(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int float_setting_synopsis(lua_State* L)
+static int float_setting_synopsis(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -74,7 +74,7 @@ CE_EXPORT int float_setting_synopsis(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int float_setting_min(lua_State* L)
+static int float_setting_min(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -95,7 +95,7 @@ CE_EXPORT int float_setting_min(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int float_setting_max(lua_State* L)
+static int float_setting_max(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -116,7 +116,7 @@ CE_EXPORT int float_setting_max(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int float_setting_update(lua_State* L)
+static int float_setting_update(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 16 - 16
engine/lua/LuaGui.cpp

@@ -38,7 +38,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_resolution(lua_State* L)
+static int gui_resolution(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -52,7 +52,7 @@ CE_EXPORT int gui_resolution(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_move(lua_State* L)
+static int gui_move(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -66,7 +66,7 @@ CE_EXPORT int gui_move(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_show(lua_State* L)
+static int gui_show(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -78,7 +78,7 @@ CE_EXPORT int gui_show(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_hide(lua_State* L)
+static int gui_hide(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -90,7 +90,7 @@ CE_EXPORT int gui_hide(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_create_rect(lua_State* L)
+static int gui_create_rect(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -107,7 +107,7 @@ CE_EXPORT int gui_create_rect(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_update_rect(lua_State* L)
+static int gui_update_rect(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -124,7 +124,7 @@ CE_EXPORT int gui_update_rect(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_destroy_rect(lua_State* L)
+static int gui_destroy_rect(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -137,7 +137,7 @@ CE_EXPORT int gui_destroy_rect(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_create_triangle(lua_State* L)
+static int gui_create_triangle(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -156,7 +156,7 @@ CE_EXPORT int gui_create_triangle(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_update_triangle(lua_State* L)
+static int gui_update_triangle(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -174,7 +174,7 @@ CE_EXPORT int gui_update_triangle(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_destroy_triangle(lua_State* L)
+static int gui_destroy_triangle(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -187,7 +187,7 @@ CE_EXPORT int gui_destroy_triangle(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_create_image(lua_State* L)
+static int gui_create_image(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -207,7 +207,7 @@ CE_EXPORT int gui_create_image(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_update_image(lua_State* L)
+static int gui_update_image(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -222,7 +222,7 @@ CE_EXPORT int gui_update_image(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_destroy_image(lua_State* L)
+static int gui_destroy_image(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -235,7 +235,7 @@ CE_EXPORT int gui_destroy_image(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_create_text(lua_State* L)
+static int gui_create_text(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -255,7 +255,7 @@ CE_EXPORT int gui_create_text(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_update_text(lua_State* L)
+static int gui_update_text(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -271,7 +271,7 @@ CE_EXPORT int gui_update_text(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int gui_destroy_text(lua_State* L)
+static int gui_destroy_text(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 5 - 5
engine/lua/LuaIntSetting.cpp

@@ -32,7 +32,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int int_setting_query_value(lua_State* L)
+static int int_setting_query_value(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -53,7 +53,7 @@ CE_EXPORT int int_setting_query_value(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int int_setting_query_synopsis(lua_State* L)
+static int int_setting_query_synopsis(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -74,7 +74,7 @@ CE_EXPORT int int_setting_query_synopsis(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int int_setting_query_min(lua_State* L)
+static int int_setting_query_min(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -95,7 +95,7 @@ CE_EXPORT int int_setting_query_min(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int int_setting_query_max(lua_State* L)
+static int int_setting_query_max(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -116,7 +116,7 @@ CE_EXPORT int int_setting_query_max(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int int_setting_update(lua_State* L)
+static int int_setting_update(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 5 - 5
engine/lua/LuaKeyboard.cpp

@@ -33,7 +33,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int keyboard_modifier_pressed(lua_State* L)
+static int keyboard_modifier_pressed(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -45,7 +45,7 @@ CE_EXPORT int keyboard_modifier_pressed(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int keyboard_button_pressed(lua_State* L)
+static int keyboard_button_pressed(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -57,7 +57,7 @@ CE_EXPORT int keyboard_button_pressed(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int keyboard_button_released(lua_State* L)
+static int keyboard_button_released(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -69,7 +69,7 @@ CE_EXPORT int keyboard_button_released(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int keyboard_any_pressed(lua_State* L)
+static int keyboard_any_pressed(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -79,7 +79,7 @@ CE_EXPORT int keyboard_any_pressed(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int keyboard_any_released(lua_State* L)
+static int keyboard_any_released(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 16 - 16
engine/lua/LuaMath.cpp

@@ -32,7 +32,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_deg_to_rad(lua_State* L)
+static int math_deg_to_rad(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -44,7 +44,7 @@ CE_EXPORT int math_deg_to_rad(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_rad_to_deg(lua_State* L)
+static int math_rad_to_deg(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -56,7 +56,7 @@ CE_EXPORT int math_rad_to_deg(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_next_pow_2(lua_State* L)
+static int math_next_pow_2(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -68,7 +68,7 @@ CE_EXPORT int math_next_pow_2(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_is_pow_2(lua_State* L)
+static int math_is_pow_2(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -80,7 +80,7 @@ CE_EXPORT int math_is_pow_2(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_ceil(lua_State* L)
+static int math_ceil(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -92,7 +92,7 @@ CE_EXPORT int math_ceil(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_floor(lua_State* L)
+static int math_floor(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -104,7 +104,7 @@ CE_EXPORT int math_floor(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_sqrt(lua_State* L)
+static int math_sqrt(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -116,7 +116,7 @@ CE_EXPORT int math_sqrt(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_inv_sqrt(lua_State* L)
+static int math_inv_sqrt(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -128,7 +128,7 @@ CE_EXPORT int math_inv_sqrt(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_sin(lua_State* L)
+static int math_sin(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -140,7 +140,7 @@ CE_EXPORT int math_sin(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_cos(lua_State* L)
+static int math_cos(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -152,7 +152,7 @@ CE_EXPORT int math_cos(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_asin(lua_State* L)
+static int math_asin(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -164,7 +164,7 @@ CE_EXPORT int math_asin(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_acos(lua_State* L)
+static int math_acos(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -176,7 +176,7 @@ CE_EXPORT int math_acos(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_tan(lua_State* L)
+static int math_tan(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -188,7 +188,7 @@ CE_EXPORT int math_tan(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_atan2(lua_State* L)
+static int math_atan2(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -201,7 +201,7 @@ CE_EXPORT int math_atan2(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_abs(lua_State* L)
+static int math_abs(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -213,7 +213,7 @@ CE_EXPORT int math_abs(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int math_fmod(lua_State* L)
+static int math_fmod(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 38 - 28
engine/lua/LuaMatrix4x4.cpp

@@ -34,7 +34,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4(lua_State* L)
+static int matrix4x4_new(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -60,8 +60,16 @@ CE_EXPORT int matrix4x4(lua_State* L)
 	return 1;
 }
 
+//-----------------------------------------------------------------------------
+static int matrix4x4_ctor(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.remove(1); // Remove table
+	return matrix4x4_new(L);
+}
+
 //-----------------------------------------------------------------------------					
-CE_EXPORT int matrix4x4_add(lua_State* L)
+static int matrix4x4_add(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -74,7 +82,7 @@ CE_EXPORT int matrix4x4_add(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_subtract(lua_State* L)
+static int matrix4x4_subtract(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -87,7 +95,7 @@ CE_EXPORT int matrix4x4_subtract(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_multiply(lua_State* L)
+static int matrix4x4_multiply(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -100,7 +108,7 @@ CE_EXPORT int matrix4x4_multiply(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_build_rotation_x(lua_State* L)
+static int matrix4x4_build_rotation_x(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -113,7 +121,7 @@ CE_EXPORT int matrix4x4_build_rotation_x(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_build_rotation_y(lua_State* L)
+static int matrix4x4_build_rotation_y(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -126,7 +134,7 @@ CE_EXPORT int matrix4x4_build_rotation_y(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_build_rotation_z(lua_State* L)
+static int matrix4x4_build_rotation_z(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -139,7 +147,7 @@ CE_EXPORT int matrix4x4_build_rotation_z(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_build_rotation(lua_State* L)
+static int matrix4x4_build_rotation(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -153,7 +161,7 @@ CE_EXPORT int matrix4x4_build_rotation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_build_look_at_rh(lua_State* L)
+static int matrix4x4_build_look_at_rh(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -168,7 +176,7 @@ CE_EXPORT int matrix4x4_build_look_at_rh(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_build_viewpoint_billboard(lua_State* L)
+static int matrix4x4_build_viewpoint_billboard(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -183,7 +191,7 @@ CE_EXPORT int matrix4x4_build_viewpoint_billboard(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_build_axis_billboard(lua_State* L)
+static int matrix4x4_build_axis_billboard(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -198,7 +206,7 @@ CE_EXPORT int matrix4x4_build_axis_billboard(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_transpose(lua_State* L)
+static int matrix4x4_transpose(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -210,7 +218,7 @@ CE_EXPORT int matrix4x4_transpose(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_determinant(lua_State* L)
+static int matrix4x4_determinant(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -222,7 +230,7 @@ CE_EXPORT int matrix4x4_determinant(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_invert(lua_State* L)
+static int matrix4x4_invert(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -234,7 +242,7 @@ CE_EXPORT int matrix4x4_invert(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_load_identity(lua_State* L)
+static int matrix4x4_load_identity(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -246,7 +254,7 @@ CE_EXPORT int matrix4x4_load_identity(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_x(lua_State* L)
+static int matrix4x4_x(lua_State* L)
 {	
 	LuaStack stack(L);
 
@@ -258,7 +266,7 @@ CE_EXPORT int matrix4x4_x(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_y(lua_State* L)
+static int matrix4x4_y(lua_State* L)
 {	
 	LuaStack stack(L);
 
@@ -270,7 +278,7 @@ CE_EXPORT int matrix4x4_y(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_z(lua_State* L)
+static int matrix4x4_z(lua_State* L)
 {	
 	LuaStack stack(L);
 
@@ -282,7 +290,7 @@ CE_EXPORT int matrix4x4_z(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_set_x(lua_State* L)
+static int matrix4x4_set_x(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -295,7 +303,7 @@ CE_EXPORT int matrix4x4_set_x(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_set_y(lua_State* L)
+static int matrix4x4_set_y(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -308,7 +316,7 @@ CE_EXPORT int matrix4x4_set_y(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_set_z(lua_State* L)
+static int matrix4x4_set_z(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -321,7 +329,7 @@ CE_EXPORT int matrix4x4_set_z(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_translation(lua_State* L)
+static int matrix4x4_translation(lua_State* L)
 {	
 	LuaStack stack(L);
 
@@ -333,7 +341,7 @@ CE_EXPORT int matrix4x4_translation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_set_translation(lua_State* L)
+static int matrix4x4_set_translation(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -346,7 +354,7 @@ CE_EXPORT int matrix4x4_set_translation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_get_scale(lua_State* L)
+static int matrix4x4_get_scale(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -358,7 +366,7 @@ CE_EXPORT int matrix4x4_get_scale(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_set_scale(lua_State* L)
+static int matrix4x4_set_scale(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -371,7 +379,7 @@ CE_EXPORT int matrix4x4_set_scale(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_identity(lua_State* L)
+static int matrix4x4_identity(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -381,7 +389,7 @@ CE_EXPORT int matrix4x4_identity(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4_print(lua_State* L)
+static int matrix4x4_print(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -398,7 +406,7 @@ CE_EXPORT int matrix4x4_print(lua_State* L)
 //-----------------------------------------------------------------------------
 void load_matrix4x4(LuaEnvironment& env)
 {
-	env.load_module_function("Matrix4x4", "new", 							matrix4x4);
+	env.load_module_function("Matrix4x4", "new", 							matrix4x4_new);
 	env.load_module_function("Matrix4x4", "add", 							matrix4x4_add);
 	env.load_module_function("Matrix4x4", "subtract", 						matrix4x4_subtract);
 	env.load_module_function("Matrix4x4", "multiply", 						matrix4x4_multiply);
@@ -425,6 +433,8 @@ void load_matrix4x4(LuaEnvironment& env)
 	env.load_module_function("Matrix4x4", "set_scale", 						matrix4x4_set_scale);
 	env.load_module_function("Matrix4x4", "identity", 						matrix4x4_identity);	
 	env.load_module_function("Matrix4x4", "print", 							matrix4x4_print);
+
+	env.load_module_constructor("Matrix4x4",								matrix4x4_ctor);
 }
 
 } //namespace crown

+ 30 - 15
engine/lua/LuaMatrix4x4Box.cpp

@@ -34,35 +34,38 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4box(lua_State* L)
+static int matrix4x4box_new(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Matrix4x4& m = stack.push_matrix4x4box();
+	const Matrix4x4& tm = stack.get_matrix4x4(1);
 
-	stack.get_global_metatable("Matrix4x4_i_mt");
-	stack.set_metatable();
-
-	Matrix4x4 tm = stack.get_matrix4x4(1);
-
-	m = tm;
+	stack.push_matrix4x4box(tm);
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4box_store(lua_State* L)
+static int matrix4x4box_ctor(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.remove(1); // Remove table
+	return matrix4x4box_new(L);
+}
+
+//-----------------------------------------------------------------------------
+static int matrix4x4box_store(lua_State* L)
 {
 	LuaStack stack(L);
 
 	Matrix4x4& m = stack.get_matrix4x4box(1);
-	Matrix4x4 tm = stack.get_matrix4x4(2);
+	const Matrix4x4 tm = stack.get_matrix4x4(2);
 
 	m = tm;
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int matrix4x4box_unbox(lua_State* L)
+static int matrix4x4box_unbox(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -72,12 +75,24 @@ CE_EXPORT int matrix4x4box_unbox(lua_State* L)
 	return 1;
 }
 
+//-----------------------------------------------------------------------------
+static int matrix4x4box_tostring(lua_State* L)
+{
+	LuaStack stack(L);
+	Matrix4x4& m = stack.get_matrix4x4box(1);
+	stack.push_fstring("Matrix4x4Box (%p)", &m);
+	return 1;
+}
+
 //-----------------------------------------------------------------------------
 void load_matrix4x4box(LuaEnvironment& env)
 {
-	env.load_module_function("Matrix4x4Box", "new", matrix4x4box);
-	env.load_module_function("Matrix4x4Box", "store", matrix4x4box_store);
-	env.load_module_function("Matrix4x4Box", "unbox", matrix4x4box_unbox);
+	env.load_module_function("Matrix4x4Box", "new",			matrix4x4box_new);
+	env.load_module_function("Matrix4x4Box", "store",		matrix4x4box_store);
+	env.load_module_function("Matrix4x4Box", "unbox",		matrix4x4box_unbox);
+	env.load_module_function("Matrix4x4Box", "__tostring",	matrix4x4box_tostring);
+
+	env.load_module_constructor("Matrix4x4Box",				matrix4x4box_ctor);
 }
 
-} // namespace crown
+} // namespace crown

+ 9 - 9
engine/lua/LuaMesh.cpp

@@ -33,7 +33,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mesh_local_position(lua_State* L)
+static int mesh_local_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -44,7 +44,7 @@ CE_EXPORT int mesh_local_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mesh_local_rotation(lua_State* L)
+static int mesh_local_rotation(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -55,7 +55,7 @@ CE_EXPORT int mesh_local_rotation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mesh_local_pose(lua_State* L)
+static int mesh_local_pose(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -66,7 +66,7 @@ CE_EXPORT int mesh_local_pose(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mesh_world_position(lua_State* L)
+static int mesh_world_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -77,7 +77,7 @@ CE_EXPORT int mesh_world_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mesh_world_rotation(lua_State* L)
+static int mesh_world_rotation(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -88,7 +88,7 @@ CE_EXPORT int mesh_world_rotation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mesh_world_pose(lua_State* L)
+static int mesh_world_pose(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -99,7 +99,7 @@ CE_EXPORT int mesh_world_pose(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mesh_set_local_position(lua_State* L)
+static int mesh_set_local_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -112,7 +112,7 @@ CE_EXPORT int mesh_set_local_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mesh_set_local_rotation(lua_State* L)
+static int mesh_set_local_rotation(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -125,7 +125,7 @@ CE_EXPORT int mesh_set_local_rotation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mesh_set_local_pose(lua_State* L)
+static int mesh_set_local_pose(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 8 - 8
engine/lua/LuaMouse.cpp

@@ -34,7 +34,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mouse_button_pressed(lua_State* L)
+static int mouse_button_pressed(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -46,7 +46,7 @@ CE_EXPORT int mouse_button_pressed(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mouse_button_released(lua_State* L)
+static int mouse_button_released(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -58,7 +58,7 @@ CE_EXPORT int mouse_button_released(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mouse_any_pressed(lua_State* L)
+static int mouse_any_pressed(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -68,7 +68,7 @@ CE_EXPORT int mouse_any_pressed(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mouse_any_released(lua_State* L)
+static int mouse_any_released(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -78,7 +78,7 @@ CE_EXPORT int mouse_any_released(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mouse_cursor_xy(lua_State* L)
+static int mouse_cursor_xy(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -88,7 +88,7 @@ CE_EXPORT int mouse_cursor_xy(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mouse_set_cursor_xy(lua_State* L)
+static int mouse_set_cursor_xy(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -100,7 +100,7 @@ CE_EXPORT int mouse_set_cursor_xy(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mouse_cursor_relative_xy(lua_State* L)
+static int mouse_cursor_relative_xy(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -110,7 +110,7 @@ CE_EXPORT int mouse_cursor_relative_xy(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int mouse_set_cursor_relative_xy(lua_State* L)
+static int mouse_set_cursor_relative_xy(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 15 - 4
engine/lua/LuaPhysicsWorld.cpp

@@ -33,7 +33,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int physics_world_gravity(lua_State* L)
+static int physics_world_gravity(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -44,7 +44,7 @@ CE_EXPORT int physics_world_gravity(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int physics_world_set_gravity(lua_State* L)
+static int physics_world_set_gravity(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -56,7 +56,7 @@ CE_EXPORT int physics_world_set_gravity(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int physics_world_make_raycast(lua_State* L)
+static int physics_world_make_raycast(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -72,7 +72,7 @@ CE_EXPORT int physics_world_make_raycast(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int physics_world_overlap_test(lua_State* L)
+static int physics_world_overlap_test(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -99,6 +99,15 @@ CE_EXPORT int physics_world_overlap_test(lua_State* L)
 	return 1;
 }
 
+//-----------------------------------------------------------------------------
+static int physics_world_tostring(lua_State* L)
+{
+	LuaStack stack(L);
+	PhysicsWorld* pw = stack.get_physics_world(1);
+	stack.push_fstring("PhysicsWorld (%p)", pw);
+	return 1;
+}
+
 //-----------------------------------------------------------------------------
 void load_physics_world(LuaEnvironment& env)
 {
@@ -106,6 +115,8 @@ void load_physics_world(LuaEnvironment& env)
 	env.load_module_function("PhysicsWorld", "set_gravity",			physics_world_set_gravity);
 	env.load_module_function("PhysicsWorld", "make_raycast",		physics_world_make_raycast);
 	env.load_module_function("PhysicsWorld", "overlap_test",		physics_world_overlap_test);
+	env.load_module_function("PhysicsWorld", "__index",				"PhysicsWorld");
+	env.load_module_function("PhysicsWorld", "__tostring",			physics_world_tostring);
 
 	// Actor types
 	env.load_module_enum("ActorType", "STATIC",						ActorType::STATIC);

+ 19 - 9
engine/lua/LuaQuaternion.cpp

@@ -34,7 +34,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int quaternion_new(lua_State* L)
+static int quaternion_new(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -47,7 +47,15 @@ CE_EXPORT int quaternion_new(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int quaternion_negate(lua_State* L)
+static int quaternion_ctor(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.remove(1); // Remove table
+	return quaternion_new(L);
+}
+
+//-----------------------------------------------------------------------------
+static int quaternion_negate(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -59,7 +67,7 @@ CE_EXPORT int quaternion_negate(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int quaternion_identity(lua_State* L)
+static int quaternion_identity(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -68,7 +76,7 @@ CE_EXPORT int quaternion_identity(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int quaternion_length(lua_State* L)
+static int quaternion_length(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -80,7 +88,7 @@ CE_EXPORT int quaternion_length(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int quaternion_conjugate(lua_State* L)
+static int quaternion_conjugate(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -92,7 +100,7 @@ CE_EXPORT int quaternion_conjugate(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int quaternion_inverse(lua_State* L)
+static int quaternion_inverse(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -104,7 +112,7 @@ CE_EXPORT int quaternion_inverse(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int quaternion_multiply(lua_State* L)
+static int quaternion_multiply(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -117,7 +125,7 @@ CE_EXPORT int quaternion_multiply(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int quaternion_multiply_by_scalar(lua_State* L)
+static int quaternion_multiply_by_scalar(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -130,7 +138,7 @@ CE_EXPORT int quaternion_multiply_by_scalar(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int quaternion_power(lua_State* L)
+static int quaternion_power(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -154,6 +162,8 @@ void load_quaternion(LuaEnvironment& env)
 	env.load_module_function("Quaternion", "conjugate",				quaternion_conjugate);
 	env.load_module_function("Quaternion", "inverse",				quaternion_inverse);
 	env.load_module_function("Quaternion", "power",					quaternion_power);
+
+	env.load_module_constructor("Quaternion",						quaternion_ctor);
 }
 
 } //namespace crown

+ 32 - 14
engine/lua/LuaQuaternionBox.cpp

@@ -33,16 +33,12 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int quaternionbox(lua_State* L)
+static int quaternionbox_new(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Quaternion& q = stack.push_quaternionbox();
-
-	stack.get_global_metatable("Quaternion_i_mt");
-	stack.set_metatable();
-
-	if (stack.num_args() == 3)
+	Quaternion q;
+	if (stack.num_args() == 2)
 	{
 		const Vector3& v = stack.get_vector3(1);
 		q.x = v.x;
@@ -50,18 +46,28 @@ CE_EXPORT int quaternionbox(lua_State* L)
 		q.z = v.z;
 		q.w = stack.get_float(2);
 	}
-	else if (stack.num_args() == 5)
+	else if (stack.num_args() == 4)
 	{
 		q.x = stack.get_float(1);
 		q.y = stack.get_float(2);
 		q.z = stack.get_float(3);
 		q.w = stack.get_float(4);
 	}
+
+	stack.push_quaternionbox(q);
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int quaternionbox_store(lua_State* L)
+static int quaternionbox_ctor(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.remove(1); // Remove table
+	return quaternionbox_new(L);
+}
+
+//-----------------------------------------------------------------------------
+static int quaternionbox_store(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -86,7 +92,7 @@ CE_EXPORT int quaternionbox_store(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int quaternionbox_unbox(lua_State* L)
+static int quaternionbox_unbox(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -96,12 +102,24 @@ CE_EXPORT int quaternionbox_unbox(lua_State* L)
 	return 1;
 }
 
+//-----------------------------------------------------------------------------
+static int quaternionbox_tostring(lua_State* L)
+{
+	LuaStack stack(L);
+	Quaternion& q = stack.get_quaternionbox(1);
+	stack.push_fstring("QuaternionBox (%p)", &q);
+	return 1;
+}
+
 //-----------------------------------------------------------------------------
 void load_quaternionbox(LuaEnvironment& env)
 {
-	env.load_module_function("QuaternionBox", "new", quaternionbox);
-	env.load_module_function("QuaternionBox", "store", quaternionbox_store);
-	env.load_module_function("QuaternionBox", "unbox", quaternionbox_unbox);
+	env.load_module_function("QuaternionBox", "new",        quaternionbox_new);
+	env.load_module_function("QuaternionBox", "store",      quaternionbox_store);
+	env.load_module_function("QuaternionBox", "unbox",      quaternionbox_unbox);
+	env.load_module_function("QuaternionBox", "__tostring", quaternionbox_tostring);
+
+	env.load_module_constructor("QuaternionBox",			quaternionbox_ctor);
 }
 
-} // namespace crown
+} // namespace crown

+ 1 - 1
engine/lua/LuaRaycast.cpp

@@ -33,7 +33,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int raycast_cast(lua_State* L)
+static int raycast_cast(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 15 - 4
engine/lua/LuaResourcePackage.cpp

@@ -32,7 +32,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int resource_package_load(lua_State* L)
+static int resource_package_load(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -43,7 +43,7 @@ CE_EXPORT int resource_package_load(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int resource_package_unload(lua_State* L)
+static int resource_package_unload(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -54,7 +54,7 @@ CE_EXPORT int resource_package_unload(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int resource_package_flush(lua_State* L)
+static int resource_package_flush(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -65,7 +65,7 @@ CE_EXPORT int resource_package_flush(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int resource_package_has_loaded(lua_State* L)
+static int resource_package_has_loaded(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -75,6 +75,15 @@ CE_EXPORT int resource_package_has_loaded(lua_State* L)
 	return 1;
 }
 
+//-----------------------------------------------------------------------------
+static int resource_package_tostring(lua_State* L)
+{
+	LuaStack stack(L);
+	ResourcePackage* package = stack.get_resource_package(1);
+	stack.push_fstring("ResourcePackage (%p)", package);
+	return 1;
+}
+
 //-----------------------------------------------------------------------------
 void load_resource_package(LuaEnvironment& env)
 {
@@ -82,6 +91,8 @@ void load_resource_package(LuaEnvironment& env)
 	env.load_module_function("ResourcePackage", "unload",     resource_package_unload);
 	env.load_module_function("ResourcePackage", "flush",      resource_package_flush);
 	env.load_module_function("ResourcePackage", "has_loaded", resource_package_has_loaded);
+	env.load_module_function("ResourcePackage", "__index",    "ResourcePackage");
+	env.load_module_function("ResourcePackage", "__tostring", resource_package_tostring);
 }
 
 } // namespace crown

+ 15 - 4
engine/lua/LuaSoundWorld.cpp

@@ -32,7 +32,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int sound_world_stop_all(lua_State* L)
+static int sound_world_stop_all(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -42,7 +42,7 @@ CE_EXPORT int sound_world_stop_all(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int sound_world_pause_all(lua_State* L)
+static int sound_world_pause_all(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -52,7 +52,7 @@ CE_EXPORT int sound_world_pause_all(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int sound_world_resume_all(lua_State* L)
+static int sound_world_resume_all(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -62,7 +62,7 @@ CE_EXPORT int sound_world_resume_all(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int sound_world_is_playing(lua_State* L)
+static int sound_world_is_playing(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -72,6 +72,15 @@ CE_EXPORT int sound_world_is_playing(lua_State* L)
 	return 1;
 }
 
+//-----------------------------------------------------------------------------
+static int sound_world_tostring(lua_State* L)
+{
+	LuaStack stack(L);
+	SoundWorld* sw = stack.get_sound_world(1);
+	stack.push_fstring("SoundWorld (%p)", sw);
+	return 1;
+}
+
 //-----------------------------------------------------------------------------
 void load_sound_world(LuaEnvironment& env)
 {
@@ -79,6 +88,8 @@ void load_sound_world(LuaEnvironment& env)
 	env.load_module_function("SoundWorld", "pause_all",   sound_world_pause_all);
 	env.load_module_function("SoundWorld", "resume_all",  sound_world_resume_all);
 	env.load_module_function("SoundWorld", "is_playing",  sound_world_is_playing);
+	env.load_module_function("SoundWorld", "__index",	  "SoundWorld");
+	env.load_module_function("SoundWorld", "__tostring",  sound_world_tostring);
 }
 
 } // namespace crown

+ 8 - 8
engine/lua/LuaSprite.cpp

@@ -33,7 +33,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int sprite_local_position(lua_State* L)
+static int sprite_local_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -45,7 +45,7 @@ CE_EXPORT int sprite_local_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int sprite_local_rotation(lua_State* L)
+static int sprite_local_rotation(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -57,7 +57,7 @@ CE_EXPORT int sprite_local_rotation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int sprite_local_pose(lua_State* L)
+static int sprite_local_pose(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -69,7 +69,7 @@ CE_EXPORT int sprite_local_pose(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int sprite_set_local_position(lua_State* L)
+static int sprite_set_local_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -83,7 +83,7 @@ CE_EXPORT int sprite_set_local_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int sprite_set_local_rotation(lua_State* L)
+static int sprite_set_local_rotation(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -97,7 +97,7 @@ CE_EXPORT int sprite_set_local_rotation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int sprite_set_local_pose(lua_State* L)
+static int sprite_set_local_pose(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -111,7 +111,7 @@ CE_EXPORT int sprite_set_local_pose(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int sprite_play_animation(lua_State* L)
+static int sprite_play_animation(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -126,7 +126,7 @@ CE_EXPORT int sprite_play_animation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int sprite_stop_animation(lua_State* L)
+static int sprite_stop_animation(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 174 - 172
engine/lua/LuaStack.h

@@ -35,6 +35,42 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Matrix4x4.h"
 #include "StringUtils.h"
 
+//-----------------------------------------------------------------------------
+#if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
+	static void* checkudata(lua_State* L, int index, const char* expected)
+	{
+		luaL_checktype(L, index, LUA_TUSERDATA);
+		return luaL_checkudata(L, index, expected);
+	}
+
+	typedef bool (*checkfn)(int);
+	static void* checklightdata(lua_State* L, int index, checkfn cf, const char* expected)
+	{
+		luaL_checktype(L, index, LUA_TLIGHTUSERDATA);
+		if (!cf(index)) luaL_typerror(L, index, expected);
+		return lua_touserdata(L, index);
+	}
+
+	static bool always_true(int index)
+	{
+		return index == index;
+	}
+
+	#define CHECKUDATA(stack, index, expected) checkudata(stack, index, expected)
+	#define CHECKLIGHTDATA(stack, index, cf, expected) checklightdata(stack, index, cf, expected)
+	#define CHECKBOOLEAN(stack, index) lua_toboolean(stack, index)
+	#define CHECKINTEGER(stack, index) luaL_checkinteger(stack, index)
+	#define CHECKNUMBER(stack, index) luaL_checknumber(stack, index)
+	#define CHECKSTRING(stack, index) luaL_checkstring(stack, index)
+#else
+	#define CHECKUDATA(stack, index, expected) lua_touserdata(stack, index)
+	#define CHECKLIGHTDATA(stack, index, cf, expected) lua_touserdata(stack, index)
+	#define CHECKBOOLEAN(stack, index) lua_toboolean(stack, index)
+	#define CHECKINTEGER(stack, index) lua_tointeger(stack, index)
+	#define CHECKNUMBER(stack, index) lua_tonumber(stack, index)
+	#define CHECKSTRING(stack, index) lua_tostring(stack, index)
+#endif
+
 namespace crown
 {
 
@@ -58,22 +94,20 @@ typedef Id GuiComponentId;
 
 typedef int (*MetamethodFunction)(lua_State*);
 
-void clear_lua_temporaries();
-
 class LuaStack
 {
 public:
 
 	//-----------------------------------------------------------------------------	
 	LuaStack(lua_State* L)
-		: m_state(L)
+		: m_L(L)
 	{
 	}
 
 	//-----------------------------------------------------------------------------
 	lua_State* state()
 	{
-		return m_state;
+		return m_L;
 	}
 
 	/// Returns the number of elements in the stack.
@@ -81,85 +115,102 @@ public:
 	/// the number of arguments passed to the function itself.
 	int32_t num_args()
 	{
-		return lua_gettop(m_state);
+		return lua_gettop(m_L);
+	}
+
+	/// Removes the element at the given valid index, shifting down the elements
+	/// above this index to fill the gap. Cannot be called with a pseudo-index,
+	/// because a pseudo-index is not an actual stack position.
+	void remove(int32_t index)
+	{
+		lua_remove(m_L, index);
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_nil()
 	{
-		lua_pushnil(m_state);
+		lua_pushnil(m_L);
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_bool(bool value)
 	{
-		lua_pushboolean(m_state, value);
+		lua_pushboolean(m_L, value);
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_int32(int32_t value)
 	{
-		lua_pushinteger(m_state, value);
+		lua_pushinteger(m_L, value);
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_uint32(uint32_t value)
 	{
-		lua_pushinteger(m_state, value);
+		lua_pushinteger(m_L, value);
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_int64(int64_t value)
 	{
-		lua_pushinteger(m_state, value);
+		lua_pushinteger(m_L, value);
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_uint64(uint64_t value)
 	{
-		lua_pushinteger(m_state, value);
+		lua_pushinteger(m_L, value);
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_float(float value)
 	{
-		lua_pushnumber(m_state, value);
+		lua_pushnumber(m_L, value);
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_string(const char* s)
 	{
-		lua_pushstring(m_state, s);
+		lua_pushstring(m_L, s);
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_fstring(const char* fmt, ...)
+	{
+		va_list vl;
+		va_start(vl, fmt);
+		lua_pushvfstring(m_L, fmt, vl);
+		va_end(vl);
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_literal(const char* s, size_t len)
 	{
-		lua_pushlstring(m_state, s, len);
+		lua_pushlstring(m_L, s, len);
 	}
 
 	//-----------------------------------------------------------------------------
 	bool get_bool(int32_t index)
 	{
-		return (bool) lua_toboolean(m_state, index);
+		return (bool) CHECKBOOLEAN(m_L, index);
 	}
 
 	//-----------------------------------------------------------------------------
 	int32_t get_int(int32_t index)
 	{
-		return luaL_checkinteger(m_state, index);
+		return CHECKINTEGER(m_L, index);
 	}
 
 	//-----------------------------------------------------------------------------
 	float get_float(int32_t index)
 	{
-		return luaL_checknumber(m_state, index);
+		return CHECKNUMBER(m_L, index);
 	}
 
 	//-----------------------------------------------------------------------------
 	const char* get_string(int32_t index)
 	{
-		return luaL_checkstring(m_state, index);
+		return CHECKSTRING(m_L, index);
 	}
 
 	/// Pushes an empty table onto the stack.
@@ -173,225 +224,186 @@ public:
 	/// return 1;
 	void push_table()
 	{
-		lua_newtable(m_state);
+		lua_newtable(m_L);
 	}
 
 	/// See Stack::push_table()
 	void push_key_begin(const char* key)
 	{
-		lua_pushstring(m_state, key);
+		lua_pushstring(m_L, key);
 	}
 
 	/// See Stack::push_table()
 	void push_key_begin(int32_t i)
 	{
-		lua_pushnumber(m_state, i);
+		lua_pushnumber(m_L, i);
 	}
 
 	/// See Stack::push_table()
 	void push_key_end()
 	{
-		lua_settable(m_state, -3);
+		lua_settable(m_L, -3);
 	}
 
-	/// Pushes an empty metatable onto the stack.
-	/// When you want to associate a metamethod to a function, you have to use 
-	/// LuaStack::set_metamethod_function(const char* key, MetamethodFunction func).
-	/// Call LuaStack::end_metatable() to associate this metatable to userdata of any kind.
-	///
-	/// stack.push_userdata(); // Could be Vector3Box or a table
-	/// stack.begin_metatable("Foo_mt");
-	/// stack.set_metamethod_function("__index", function_ptr);
-	/// stack.set_metamethod_function("__newindex", another_function_ptr);
-	/// stack.end_metatable();
-	void create_metatable(const char* name)
-	{
-		luaL_newmetatable(m_state, name);
-	}
-
-	/// See LuaStack::begin_metatable()
-	void set_metatable_function(const char* key, MetamethodFunction func)
-	{
-		lua_pushcfunction(m_state, func);
-		lua_setfield(m_state, -2, key);
-	}
-
-	///	Sets table on top of stack to metafield @a key of another table.
-	/// Only '__index' and '__newindex' metamethods are available.
-	void set_metatable_table(const char* key)
-	{
-		CE_ASSERT(string::strcmp(key, "__index") == 0 || string::strcmp(key, "__newindex") == 0, "Illegal metamethod");
-		lua_setfield(m_state, -2, key);		
-	}
-
-	/// Sets table's __index metamethod to itself
-	void set_self_index()
-	{
-		lua_pushvalue(m_state, -1);
-		lua_setfield(m_state, -2, "__index");
-	}
-
-	/// See LuaStack::begin_metatable()
-	void set_metatable()
-	{
-		lua_setmetatable(m_state, -2);
-	}
-
-	///
-	void get_global(const char* name)
+	//-----------------------------------------------------------------------------
+	void push_resource_package(ResourcePackage* package)
 	{
-		lua_getglobal(m_state, name);
+		ResourcePackage** p = (ResourcePackage**) lua_newuserdata(m_L, sizeof(ResourcePackage*));
+		*p = package;
+		luaL_getmetatable(m_L, "ResourcePackage");
+		lua_setmetatable(m_L, -2);
 	}
 
-	///
-	void get_global_metatable(const char* name)
+	//-----------------------------------------------------------------------------
+	ResourcePackage* get_resource_package(int32_t index)
 	{
-		luaL_getmetatable(m_state, name);
+		ResourcePackage* pkg = *(ResourcePackage**) CHECKUDATA(m_L, index, "ResourcePackage");
+		return pkg;
 	}
 
 	//-----------------------------------------------------------------------------
-	void push_resource_package(ResourcePackage* package)
+	void push_world(World* world)
 	{
-		lua_pushlightuserdata(m_state, package);
-	}
+		World** w = (World**) lua_newuserdata(m_L, sizeof(World*));
+		*w = world;
+		luaL_getmetatable(m_L, "World");
+		lua_setmetatable(m_L, -2);
+	};
 
 	//-----------------------------------------------------------------------------
-	ResourcePackage* get_resource_package(int32_t index)
+	World* get_world(int32_t index)
 	{
-		return (ResourcePackage*) lua_touserdata(m_state, index);
-	}
+		World* w = *(World**) CHECKUDATA(m_L, index, "World");
+		return w;
+	};
 
 	//-----------------------------------------------------------------------------
-	void push_world(World* world)
+	void push_physics_world(PhysicsWorld* world)
 	{
-		lua_pushlightuserdata(m_state, world);
+		PhysicsWorld** w = (PhysicsWorld**) lua_newuserdata(m_L, sizeof(PhysicsWorld*));
+		luaL_getmetatable(m_L, "PhysicsWorld");
+		lua_setmetatable(m_L, -2);
+		*w = world;
 	}
 
 	//-----------------------------------------------------------------------------
-	World* get_world(int32_t index)
+	PhysicsWorld* get_physics_world(int32_t index)
 	{
-		return (World*) lua_touserdata(m_state, index);
+		PhysicsWorld* w = *(PhysicsWorld**) CHECKUDATA(m_L, index, "PhysicsWorld");
+		return w;
 	}
 
 	//-----------------------------------------------------------------------------
-	void push_sound_world(SoundWorld* sw)
+	void push_sound_world(SoundWorld* world)
 	{
-		lua_pushlightuserdata(m_state, sw);
+		SoundWorld** w = (SoundWorld**) lua_newuserdata(m_L, sizeof(SoundWorld*));
+		*w = world;
+		luaL_getmetatable(m_L, "SoundWorld");
+		lua_setmetatable(m_L, -2);
 	}
 
 	//-----------------------------------------------------------------------------
 	SoundWorld* get_sound_world(int32_t index)
 	{
-		return (SoundWorld*) lua_touserdata(m_state, index);
+		SoundWorld* w = *(SoundWorld**) CHECKUDATA(m_L, index, "SoundWorld");
+		return w;
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_unit(Unit* unit)
 	{
-		lua_pushlightuserdata(m_state, unit);
+		lua_pushlightuserdata(m_L, unit);
 	}
 
 	//-----------------------------------------------------------------------------
 	Unit* get_unit(int32_t index)
 	{
-		return (Unit*) lua_touserdata(m_state, index);
+		return (Unit*) CHECKLIGHTDATA(m_L, index, always_true, "Unit");
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_camera(Camera* camera)
 	{
-		lua_pushlightuserdata(m_state, camera);
+		lua_pushlightuserdata(m_L, camera);
 	}
 
 	//-----------------------------------------------------------------------------
 	Camera* get_camera(int32_t index)
 	{
-		return (Camera*) lua_touserdata(m_state, index);
+		return (Camera*) CHECKLIGHTDATA(m_L, index, always_true, "Camera");
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_mesh(Mesh* mesh)
 	{
-		lua_pushlightuserdata(m_state, mesh);
+		lua_pushlightuserdata(m_L, mesh);
 	}
 
 	//-----------------------------------------------------------------------------
 	Mesh* get_mesh(int32_t index)
 	{
-		return (Mesh*) lua_touserdata(m_state, index);
+		return (Mesh*) CHECKLIGHTDATA(m_L, index, always_true, "Mesh");
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_sprite(Sprite* sprite)
 	{
-		lua_pushlightuserdata(m_state, sprite);
+		lua_pushlightuserdata(m_L, sprite);
 	}
 
 	//-----------------------------------------------------------------------------
 	Sprite* get_sprite(int32_t index)
 	{
-		return (Sprite*) lua_touserdata(m_state, index);
-	}
-
-	//-----------------------------------------------------------------------------
-	PhysicsWorld* get_physics_world(int32_t index)
-	{
-		return (PhysicsWorld*) lua_touserdata(m_state, index);
-	}
-
-	//-----------------------------------------------------------------------------
-	void push_physics_world(PhysicsWorld* world)
-	{
-		lua_pushlightuserdata(m_state, world);
+		return (Sprite*) CHECKLIGHTDATA(m_L, index, always_true, "Sprite");
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_actor(Actor* actor)
 	{
-		lua_pushlightuserdata(m_state, actor);
+		lua_pushlightuserdata(m_L, actor);
 	}
 
 	//-----------------------------------------------------------------------------
 	Actor* get_actor(int32_t index)
 	{
-		return (Actor*) lua_touserdata(m_state, index);
+		return (Actor*) CHECKLIGHTDATA(m_L, index, always_true, "Actor");
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_controller(Controller* controller)
 	{
-		lua_pushlightuserdata(m_state, controller);
+		lua_pushlightuserdata(m_L, controller);
 	}
 
 	//-----------------------------------------------------------------------------
 	Controller* get_controller(int32_t index)
 	{
-		return (Controller*) lua_touserdata(m_state, index);
+		return (Controller*) CHECKLIGHTDATA(m_L, index, always_true, "Controller");
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_raycast(Raycast* raycast)
 	{
-		lua_pushlightuserdata(m_state, raycast);
+		lua_pushlightuserdata(m_L, raycast);
 	}
 
 	//-----------------------------------------------------------------------------
 	Raycast* get_raycast(int32_t index)
 	{
-		return (Raycast*) lua_touserdata(m_state, index);
+		return (Raycast*) CHECKLIGHTDATA(m_L, index, always_true, "Raycast");
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_sound_instance_id(const SoundInstanceId id)
 	{
 		uintptr_t enc = id.encode();
-		lua_pushlightuserdata(m_state, (void*)enc);
+		lua_pushlightuserdata(m_L, (void*)enc);
 	}
 
 	//-----------------------------------------------------------------------------
 	SoundInstanceId get_sound_instance_id(int32_t index)
 	{
-		uint32_t enc = (uintptr_t) lua_touserdata(m_state, index);
+		uint32_t enc = (uintptr_t) CHECKLIGHTDATA(m_L, index, always_true, "SoundInstanceId");
 		SoundInstanceId id;
 		id.decode(enc);
 		return id;
@@ -400,26 +412,26 @@ public:
 	//-----------------------------------------------------------------------------
 	void push_gui(Gui* gui)
 	{
-		lua_pushlightuserdata(m_state, gui);
+		lua_pushlightuserdata(m_L, gui);
 	}
 
 	//-----------------------------------------------------------------------------
 	Gui* get_gui(int32_t index)
 	{
-		return (Gui*) lua_touserdata(m_state, index);
+		return (Gui*) CHECKLIGHTDATA(m_L, index, always_true, "Gui");
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_gui_component_id(GuiComponentId id)
 	{
 		uintptr_t enc = id.encode();
-		lua_pushlightuserdata(m_state, (void*)enc);		
+		lua_pushlightuserdata(m_L, (void*)enc);		
 	}
 
 	//-----------------------------------------------------------------------------
 	GuiComponentId get_gui_component_id(int32_t index)
 	{
-		uint32_t enc = (uintptr_t) lua_touserdata(m_state, index);
+		uint32_t enc = (uintptr_t) CHECKLIGHTDATA(m_L, index, always_true, "GuiComponentId");
 		GuiComponentId id;
 		id.decode(enc);
 		return id;	
@@ -428,136 +440,126 @@ public:
 	//-----------------------------------------------------------------------------
 	void push_debug_line(DebugLine* line)
 	{
-		lua_pushlightuserdata(m_state, line);
+		lua_pushlightuserdata(m_L, line);
 	}
 
 	//-----------------------------------------------------------------------------
 	DebugLine* get_debug_line(int32_t index)
 	{
-		return (DebugLine*) lua_touserdata(m_state, index);
+		return (DebugLine*) CHECKLIGHTDATA(m_L, index, always_true, "DebugLine");
 	}
 
 	//-----------------------------------------------------------------------------
 	Vector2& get_vector2(int32_t index)
 	{
-		void* v = lua_touserdata(m_state, index);
-
-		if (!lua_system::is_vector2(index))
-		{
-			luaL_typerror(m_state, index, "Vector2");
-		}
-
+		void* v = CHECKLIGHTDATA(m_L, index, lua_system::is_vector2, "Vector2");
 		return *(Vector2*)v;
 	}
 
 	//-----------------------------------------------------------------------------
 	Vector3& get_vector3(int32_t index)
 	{
-		void* v = lua_touserdata(m_state, index);
-
-		if (!lua_system::is_vector3(index))
-		{
-			luaL_typerror(m_state, index, "Vector3");
-		}
-
+		void* v = CHECKLIGHTDATA(m_L, index, lua_system::is_vector3, "Vector3");
 		return *(Vector3*)v;
 	}
 
 	//-----------------------------------------------------------------------------
 	Matrix4x4& get_matrix4x4(int32_t index)
 	{
-		void* m = lua_touserdata(m_state, index);
-
-		if (!lua_system::is_matrix4x4(index))
-		{
-			luaL_typerror(m_state, index, "Matrix4x4");
-		}
-
+		void* m = CHECKLIGHTDATA(m_L, index, lua_system::is_matrix4x4, "Matrix4x4");
 		return *(Matrix4x4*)m;
 	}
 
 	//-----------------------------------------------------------------------------
 	Quaternion& get_quaternion(int32_t index)
 	{
-		void* q = lua_touserdata(m_state, index);
-
-		if (!lua_system::is_quaternion(index))
-		{
-			luaL_typerror(m_state, index, "Quaternion");
-		}
-
+		void* q = CHECKLIGHTDATA(m_L, index, lua_system::is_quaternion, "Quaternion");
 		return *(Quaternion*)q;
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_vector2(const Vector2& v)
 	{
-		lua_pushlightuserdata(m_state, lua_system::next_vector2(v));
+		lua_pushlightuserdata(m_L, lua_system::next_vector2(v));
+		luaL_getmetatable(m_L, "Lightuserdata_mt");
+		lua_setmetatable(m_L, -2);
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_vector3(const Vector3& v)
 	{
-		lua_pushlightuserdata(m_state, lua_system::next_vector3(v));
+		lua_pushlightuserdata(m_L, lua_system::next_vector3(v));
+		luaL_getmetatable(m_L, "Lightuserdata_mt");
+		lua_setmetatable(m_L, -2);
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_matrix4x4(const Matrix4x4& m)
 	{
-		lua_pushlightuserdata(m_state, lua_system::next_matrix4x4(m));
+		lua_pushlightuserdata(m_L, lua_system::next_matrix4x4(m));
+		luaL_getmetatable(m_L, "Lightuserdata_mt");
+		lua_setmetatable(m_L, -2);
 	}
 
 	//-----------------------------------------------------------------------------
 	void push_quaternion(const Quaternion& q)
 	{
-		lua_pushlightuserdata(m_state, lua_system::next_quaternion(q));
+		lua_pushlightuserdata(m_L, lua_system::next_quaternion(q));
+		luaL_getmetatable(m_L, "Lightuserdata_mt");
+		lua_setmetatable(m_L, -2);
 	}
 
-	/// Allocates a new Vector3Box (full userdata)
-	Vector3& push_vector3box()
+	//-----------------------------------------------------------------------------
+	void push_vector3box(const Vector3& v)
 	{
-		return *(Vector3*) lua_newuserdata(m_state, sizeof(Vector3));
+		Vector3* vec = (Vector3*) lua_newuserdata(m_L, sizeof(Vector3));
+		luaL_getmetatable(m_L, "Vector3Box");
+		lua_setmetatable(m_L, -2);
+		*vec = v;
 	}
 
-	/// Retrieves Vector3Box (full userdata)
+	//-----------------------------------------------------------------------------
 	Vector3& get_vector3box(uint32_t index)
 	{
-		CE_ASSERT(lua_isuserdata(m_state, index), "Unable to retrieve 'Vector3Box' (wrong index)");
-		Vector3* ud = (Vector3*) luaL_checkudata(m_state, index, "Vector3Box_i_mt");
-		return *ud;
+		Vector3* v = (Vector3*) CHECKUDATA(m_L, index, "Vector3Box");
+		return *v;
 	}
 
-	/// Allocates a new QuaternionBox (full userdata)
-	Quaternion& push_quaternionbox()
+	//-----------------------------------------------------------------------------
+	void push_quaternionbox(const Quaternion& q)
 	{
-		return *(Quaternion*) lua_newuserdata(m_state, sizeof(Quaternion));
+		Quaternion* quat = (Quaternion*) lua_newuserdata(m_L, sizeof(Quaternion));
+		luaL_getmetatable(m_L, "QuaternionBox");
+		lua_setmetatable(m_L, -2);
+		*quat = q;
 	}
 
-	/// Retrieves QuaternionBox (full userdata)
+	//-----------------------------------------------------------------------------
 	Quaternion& get_quaternionbox(uint32_t index)
 	{
-		CE_ASSERT(lua_isuserdata(m_state, index), "Unable to retrieve QuaternionBox (wrong index)");
-		Quaternion* ud = (Quaternion*) luaL_checkudata(m_state, index, "QuaternionBox_i_mt");
-		return *ud;
+		Quaternion* q = (Quaternion*) CHECKUDATA(m_L, index, "QuaternionBox");
+		return *q;
 	}
 
-	/// Allocates a new Matrix4x4Box (full userdata)
-	Matrix4x4& push_matrix4x4box()
+	//-----------------------------------------------------------------------------
+	void push_matrix4x4box(const Matrix4x4& m)
 	{
-		return *(Matrix4x4*) lua_newuserdata(m_state, sizeof(Matrix4x4));
+		Matrix4x4* mat = (Matrix4x4*) lua_newuserdata(m_L, sizeof(Matrix4x4));
+		luaL_getmetatable(m_L, "Matrix4x4Box");
+		lua_setmetatable(m_L, -2);
+		*mat = m;
 	}
 
-	/// Retrieves QuaternionBox (full userdata)
+	//-----------------------------------------------------------------------------
 	Matrix4x4& get_matrix4x4box(uint32_t index)
 	{
-		CE_ASSERT(lua_isuserdata(m_state, index), "Unable to retrieve Matrix4x4Box (wrong index)");
-		Matrix4x4* ud = (Matrix4x4*) luaL_checkudata(m_state, index, "Matrix4x4Box_i_mt");
-		return *ud;
+		Matrix4x4* m = (Matrix4x4*) CHECKUDATA(m_L, index, "Matrix4x4Box");
+		return *m;
 	}
 
 private:
 
-	lua_State* m_state;
+	lua_State* m_L;
 };
 
 } // namespace crown

+ 3 - 3
engine/lua/LuaStringSetting.cpp

@@ -32,7 +32,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int string_setting_value(lua_State* L)
+static int string_setting_value(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -53,7 +53,7 @@ CE_EXPORT int string_setting_value(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int string_setting_synopsis(lua_State* L)
+static int string_setting_synopsis(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -74,7 +74,7 @@ CE_EXPORT int string_setting_synopsis(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int string_setting_update(lua_State* L)
+static int string_setting_update(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 179 - 217
engine/lua/LuaSystem.cpp

@@ -42,168 +42,189 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
-extern int vector2_add(lua_State* L);
-extern int vector2_subtract(lua_State* L);
-extern int vector2_multiply(lua_State* L);
-extern int vector2_divide(lua_State* L);
-extern int vector2_negate(lua_State* L);
-extern int vector3_add(lua_State* L);
-extern int vector3_subtract(lua_State* L);
-extern int vector3_multiply(lua_State* L);
-extern int vector3_divide(lua_State* L);
-extern int vector3_negate(lua_State* L);
-extern int vector2_new(lua_State* L);
-extern int vector3_new(lua_State* L);
-extern int matrix4x4(lua_State* L);
-extern int quaternion_new(lua_State* L);
-
-extern int matrix4x4box(lua_State* L);
-extern int quaternionbox(lua_State* L);
-extern int vector3box(lua_State* L);
-extern int vector3box_get_value(lua_State* L);
-extern int vector3box_set_value(lua_State* L);
-
-//-----------------------------------------------------------------------------
-static int crown_lua_vector2_call(lua_State* L)
-{
-	lua_remove(L, 1);
-	return vector2_new(L);
-}
+// Lua modules
+extern void load_accelerometer(LuaEnvironment& env);
+extern void load_actor(LuaEnvironment& env);
+extern void load_camera(LuaEnvironment& env);
+extern void load_controller(LuaEnvironment& env);
+extern void load_debug_line(LuaEnvironment& env);
+extern void load_device(LuaEnvironment& env);
+extern void load_float_setting(LuaEnvironment& env);
+extern void load_gui(LuaEnvironment& env);
+extern void load_int_setting(LuaEnvironment& env);
+extern void load_keyboard(LuaEnvironment& env);
+extern void load_math(LuaEnvironment& env);
+extern void load_matrix4x4(LuaEnvironment& env);
+extern void load_matrix4x4box(LuaEnvironment& env);
+extern void load_mesh(LuaEnvironment& env);
+extern void load_mouse(LuaEnvironment& env);
+extern void load_physics_world(LuaEnvironment& env);
+extern void load_quaternion(LuaEnvironment& env);
+extern void load_quaternionbox(LuaEnvironment& env);
+extern void load_raycast(LuaEnvironment& env);
+extern void load_resource_package(LuaEnvironment& env);
+extern void load_sound_world(LuaEnvironment& env);
+extern void load_sprite(LuaEnvironment& env);
+extern void load_string_setting(LuaEnvironment& env);
+extern void load_touch(LuaEnvironment& env);
+extern void load_unit(LuaEnvironment& env);
+extern void load_vector2(LuaEnvironment& env);
+extern void load_vector3(LuaEnvironment& env);
+extern void load_vector3box(LuaEnvironment& env);
+extern void load_window(LuaEnvironment& env);
+extern void load_world(LuaEnvironment& env);
 
-//-----------------------------------------------------------------------------
-static int crown_lua_vector3_call(lua_State* L)
+namespace lua_system
 {
-	lua_remove(L, 1);
-	return vector3_new(L);
-}
+	static lua_State* s_L;
 
-//-----------------------------------------------------------------------------
-static int crown_lua_matrix4x4_call(lua_State* L)
-{
-	lua_remove(L, 1);
-	return matrix4x4(L);
-}
+	static uint32_t 		s_vec2_used = 0;
+	static Vector2 			s_vec2_buffer[CE_MAX_LUA_VECTOR2];
+	static uint32_t 		s_vec3_used = 0;
+	static Vector3 			s_vec3_buffer[CE_MAX_LUA_VECTOR3];
+	static uint32_t 		s_mat4_used = 0;
+	static Matrix4x4 		s_mat4_buffer[CE_MAX_LUA_MATRIX4X4];
+	static uint32_t 		s_quat_used = 0;
+	static Quaternion 		s_quat_buffer[CE_MAX_LUA_QUATERNION];
 
-//-----------------------------------------------------------------------------
-static int crown_lua_quaternion_call(lua_State* L)
-{
-	lua_remove(L, 1);
-	return quaternion_new(L);
-}
+	// When an error occurs, logs the error message and pauses the engine.
+	int error_handler(lua_State* L)
+	{
+		lua_getfield(L, LUA_GLOBALSINDEX, "debug");
+		if (!lua_istable(L, -1))
+		{
+			lua_pop(L, 1);
+			return 0;
+		}
 
-//-----------------------------------------------------------------------------
-static int crown_lua_lightuserdata_add(lua_State* L)
-{
-	LuaStack stack(L);
+		lua_getfield(L, -1, "traceback");
+		if (!lua_isfunction(L, -1))
+		{
+			lua_pop(L, 2);
+			return 0;
+		}
 
-	if (lua_system::is_vector3(1))
-	{
-		return vector3_add(L);
-	}
-	return vector2_add(L);
-}
+		lua_pushvalue(L, 1); // Pass error message
+		lua_pushinteger(L, 2);
+		lua_call(L, 2, 1); // Call debug.traceback
 
-//-----------------------------------------------------------------------------
-static int crown_lua_lightuserdata_sub(lua_State* L)
-{
-	LuaStack stack(L);
+		Log::e(lua_tostring(L, -1)); // Print error message
+		lua_pop(L, 1); // Remove error message from stack
+		lua_pop(L, 1); // Remove debug.traceback from stack
 
-	if (lua_system::is_vector3(1))
-	{
-		return vector3_subtract(L);
+		device()->pause();
+		return 0;
 	}
-	return vector2_subtract(L);
-}
 
-//-----------------------------------------------------------------------------
-static int crown_lua_lightuserdata_mul(lua_State* L)
-{
-	LuaStack stack(L);
-
-	if (lua_system::is_vector3(1))
+	// Redirects require to the resource manager.
+	static int require(lua_State* L)
 	{
-		return vector3_multiply(L);
-	}
-	return vector2_multiply(L);
-}
+		LuaStack stack(L);
 
-//-----------------------------------------------------------------------------
-static int crown_lua_lightuserdata_div(lua_State* L)
-{
-	LuaStack stack(L);
+		const char* filename = stack.get_string(1);
 
-	if (lua_system::is_vector3(1))
-	{
-		return vector3_divide(L);
-	}
-	return vector2_divide(L);
-}
+		const ResourceId lua_res = device()->resource_manager()->load("lua", filename);
+		device()->resource_manager()->flush();
 
-//-----------------------------------------------------------------------------
-static int crown_lua_lightuserdata_unm(lua_State* L)
-{
-	LuaStack stack(L);
+		const LuaResource* lr = (LuaResource*) device()->resource_manager()->data(lua_res);
+		luaL_loadbuffer(L, (const char*) lr->program(), lr->size(), "");
 
-	if (lua_system::is_vector3(1))
-	{
-		return vector3_negate(L);
+		device()->resource_manager()->unload(lua_res);
+
+		return 1;
 	}
-	return vector2_negate(L);
-}
 
-//-----------------------------------------------------------------------------
-static int crown_lua_vector3box_call(lua_State* L)
-{
-	lua_remove(L, 1);
-	return vector3box(L);
-}
+	//-----------------------------------------------------------------------------
+	static int lightuserdata_add(lua_State* L)
+	{
+		LuaStack stack(L);
 
-//-----------------------------------------------------------------------------
-static int crown_lua_quaternionbox_call(lua_State* L)
-{
-	lua_remove(L, 1);
-	return quaternionbox(L);
-}
+		if (lua_system::is_vector3(1))
+		{
+			const Vector3& a = stack.get_vector3(1);
+			const Vector3& b = stack.get_vector3(2);
+			stack.push_vector3(a + b);
+			return 1;
+		}
 
-//-----------------------------------------------------------------------------
-static int crown_lua_matrix4x4box_call(lua_State* L)
-{
-	lua_remove(L, 1);
-	return matrix4x4box(L);	
-}
+		const Vector2& a = stack.get_vector2(1);
+		const Vector2& b = stack.get_vector2(2);
+		stack.push_vector2(a + b);
+		return 1;
+	}
 
-//-----------------------------------------------------------------------------
-static int crown_lua_require(lua_State* L)
-{
-	LuaStack stack(L);
+	//-----------------------------------------------------------------------------
+	static int lightuserdata_sub(lua_State* L)
+	{
+		LuaStack stack(L);
 
-	const char* filename = stack.get_string(1);
+		if (lua_system::is_vector3(1))
+		{
+			const Vector3& a = stack.get_vector3(1);
+			const Vector3& b = stack.get_vector3(2);
+			stack.push_vector3(a - b);
+			return 1;
+		}
 
-	const ResourceId lua_res = device()->resource_manager()->load("lua", filename);
-	device()->resource_manager()->flush();
+		const Vector2& a = stack.get_vector2(1);
+		const Vector2& b = stack.get_vector2(2);
+		stack.push_vector2(a - b);
+		return 1;
+	}
 
-	const LuaResource* lr = (LuaResource*) device()->resource_manager()->data(lua_res);
-	luaL_loadbuffer(L, (const char*) lr->program(), lr->size(), "");
+	//-----------------------------------------------------------------------------
+	static int lightuserdata_mul(lua_State* L)
+	{
+		LuaStack stack(L);
 
-	device()->resource_manager()->unload(lua_res);
+		if (lua_system::is_vector3(1))
+		{
+			const Vector3& a = stack.get_vector3(1);
+			const float b = stack.get_float(2);
+			stack.push_vector3(a * b);
+			return 1;
+		}
 
-	return 1;
-}
+		const Vector2& a = stack.get_vector2(1);
+		const float b = stack.get_float(2);
+		stack.push_vector2(a * b);
+		return 1;
+	}
 
-namespace lua_system
-{
-	static lua_State* s_L;
+	//-----------------------------------------------------------------------------
+	static int lightuserdata_div(lua_State* L)
+	{
+		LuaStack stack(L);
 
-	static uint32_t 		s_vec2_used = 0;
-	static Vector2 			s_vec2_buffer[CE_MAX_LUA_VECTOR2];
-	static uint32_t 		s_vec3_used = 0;
-	static Vector3 			s_vec3_buffer[CE_MAX_LUA_VECTOR3];
-	static uint32_t 		s_mat4_used = 0;
-	static Matrix4x4 		s_mat4_buffer[CE_MAX_LUA_MATRIX4X4];
-	static uint32_t 		s_quat_used = 0;
-	static Quaternion 		s_quat_buffer[CE_MAX_LUA_QUATERNION];
+		if (lua_system::is_vector3(1))
+		{
+			const Vector3& a = stack.get_vector3(1);
+			const float b = stack.get_float(2);
+			stack.push_vector3(a / b);
+			return 1;
+		}
+
+		const Vector2& a = stack.get_vector2(1);
+		const float b = stack.get_float(2);
+		stack.push_vector2(a / b);
+		return 1;
+	}
 
 	//-----------------------------------------------------------------------------
+	static int lightuserdata_unm(lua_State* L)
+	{
+		LuaStack stack(L);
+
+		if (lua_system::is_vector3(1))
+		{
+			stack.push_vector3(-stack.get_vector3(1));
+			return 1;
+		}
+		stack.push_vector2(-stack.get_vector2(1));
+		return 1;
+	}
+
+	// Initializes lua subsystem
 	void init()
 	{
 		s_L = luaL_newstate();
@@ -214,7 +235,6 @@ namespace lua_system
 
 		// Register crown libraries
 		LuaEnvironment env(s_L);
-
 		load_accelerometer(env);
 		load_actor(env);
 		load_camera(env);
@@ -256,95 +276,37 @@ namespace lua_system
 			lua_pop(s_L, 1);
 			num_loaders++;
 		}
-
 		lua_pushinteger(s_L, num_loaders + 1);
-		lua_pushcfunction(s_L, crown_lua_require);
+		lua_pushcfunction(s_L, require);
 		lua_rawset(s_L, -3);
 		lua_pop(s_L, 1);
 
+		// Create metatable for lightuserdata
+		luaL_newmetatable(s_L, "Lightuserdata_mt");
+		lua_pushstring(s_L, "__add");
+		lua_pushcfunction(s_L, lightuserdata_add);
+		lua_settable(s_L, 1);
+
+		lua_pushstring(s_L, "__sub");
+		lua_pushcfunction(s_L, lightuserdata_sub);
+		lua_settable(s_L, 1);
 
-		LuaStack stack(s_L);
+		lua_pushstring(s_L, "__mul");
+		lua_pushcfunction(s_L, lightuserdata_mul);
+		lua_settable(s_L, 1);
 
-		// Create metatable for lightuserdata
-		lua_pushlightuserdata(s_L, (void*)0x0); // Just dummy userdata
-		stack.create_metatable("Lightuserdata_mt");
-		stack.set_metatable();
-		lua_pop(s_L, -1); // Pop dummy userdata
-
-		stack.get_global_metatable("Lightuserdata_mt");
-		stack.set_metatable_function("__add", crown_lua_lightuserdata_add);
-		stack.set_metatable_function("__sub", crown_lua_lightuserdata_sub);
-		stack.set_metatable_function("__mul", crown_lua_lightuserdata_mul);
-		stack.set_metatable_function("__div", crown_lua_lightuserdata_div);
-		stack.set_metatable_function("__unm", crown_lua_lightuserdata_unm);
-
-		// Vector2 metatable
-		stack.get_global("Vector2");
-		stack.create_metatable("Vector2_mt");
-		stack.set_self_index();
-		stack.set_metatable_function("__call", crown_lua_vector2_call);
-		stack.set_metatable();
-
-		// Vector3 metatable
-		stack.get_global("Vector3");
-		stack.create_metatable("Vector3_mt");
-		stack.set_self_index();
-		stack.set_metatable_function("__call", crown_lua_vector3_call);
-		stack.set_metatable();
-
-		// Matrix4x4 metatable
-		stack.get_global("Matrix4x4");
-		stack.create_metatable("Matrix4x4_mt");
-		stack.set_self_index();
-		stack.set_metatable_function("__call", crown_lua_matrix4x4_call);
-		stack.set_metatable();
-
-		// Quaternion metatable
-		stack.get_global("Quaternion");
-		stack.create_metatable("Quaternion_mt");
-		stack.set_self_index();
-		stack.set_metatable_function("__call", crown_lua_quaternion_call);
-		stack.set_metatable();
-
-		// Create and attach metatable to global table Vector3Box
-		stack.get_global("Vector3Box");
-		stack.create_metatable("Vector3Box_g_mt");
-		stack.set_self_index();
-		stack.set_metatable_function("__call", crown_lua_vector3box_call);
-		stack.set_metatable();
-
-		// Create metatable for userdata Vector3Box (instance)
-		// Used for type checking and also provide access facilities to data
-		// e.g: 
-		// inst = Vector3Box(0, 0, 0)
-		// print (inst.x)	<= cannot do it without the following code
-		stack.create_metatable("Vector3Box_i_mt");
-		stack.set_metatable_function("__index", vector3box_get_value);
-		stack.set_metatable_function("__newindex", vector3box_set_value);
-
-		// Create and attach metatable to global table Quaternion
-		stack.get_global("QuaternionBox");
-		stack.create_metatable("QuaternionBox_g_mt");
-		stack.set_self_index();
-		stack.set_metatable_function("__call", crown_lua_quaternionbox_call);
-		stack.set_metatable();
-
-		// Create metatable for userdata QuaternionBox (instance)
-		// Used for type checking
-		stack.create_metatable("QuaternionBox_i_mt");
-
-		// Create and attach metatable to global table Matrix4x4
-		stack.get_global("Matrix4x4");
-		stack.create_metatable("Matrix4x4_g_mt");
-		stack.set_self_index();
-		stack.set_metatable_function("__call", crown_lua_matrix4x4box_call);
-		stack.set_metatable();
-
-		// Create metatable for userdata QuaternionBox (instance)
-		// Used for type checking
-		stack.create_metatable("Matrix4x4_i_mt");
-
-		Log::d("Lua stack size = %d", lua_gettop(s_L));
+		lua_pushstring(s_L, "__div");
+		lua_pushcfunction(s_L, lightuserdata_div);
+		lua_settable(s_L, 1);
+
+		lua_pushstring(s_L, "__unm");
+		lua_pushcfunction(s_L, lightuserdata_unm);
+		lua_settable(s_L, 1);
+
+		lua_pop(s_L, 1); // Pop Lightuserdata_mt
+
+		// Ensure stack is clean
+		CE_ASSERT(lua_gettop(s_L) == 0, "Stack not clean");
 	}
 
 	//-----------------------------------------------------------------------------

+ 5 - 5
engine/lua/LuaTouch.cpp

@@ -33,7 +33,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int touch_pointer_down(lua_State* L)
+static int touch_pointer_down(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -45,7 +45,7 @@ CE_EXPORT int touch_pointer_down(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int touch_pointer_up(lua_State* L)
+static int touch_pointer_up(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -57,7 +57,7 @@ CE_EXPORT int touch_pointer_up(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int touch_any_down(lua_State* L)
+static int touch_any_down(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -67,7 +67,7 @@ CE_EXPORT int touch_any_down(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int touch_any_up(lua_State* L)
+static int touch_any_up(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -77,7 +77,7 @@ CE_EXPORT int touch_any_up(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int touch_pointer_xy(lua_State* L)
+static int touch_pointer_xy(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 20 - 20
engine/lua/LuaUnit.cpp

@@ -32,7 +32,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_node(lua_State* L)
+static int unit_node(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -44,7 +44,7 @@ CE_EXPORT int unit_node(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_has_node(lua_State* L)
+static int unit_has_node(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -56,7 +56,7 @@ CE_EXPORT int unit_has_node(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_num_nodes(lua_State* L)
+static int unit_num_nodes(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -67,7 +67,7 @@ CE_EXPORT int unit_num_nodes(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_local_position(lua_State* L)
+static int unit_local_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -79,7 +79,7 @@ CE_EXPORT int unit_local_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_local_rotation(lua_State* L)
+static int unit_local_rotation(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -91,7 +91,7 @@ CE_EXPORT int unit_local_rotation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_local_pose(lua_State* L)
+static int unit_local_pose(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -103,7 +103,7 @@ CE_EXPORT int unit_local_pose(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_world_position(lua_State* L)
+static int unit_world_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -115,7 +115,7 @@ CE_EXPORT int unit_world_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_world_rotation(lua_State* L)
+static int unit_world_rotation(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -127,7 +127,7 @@ CE_EXPORT int unit_world_rotation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_world_pose(lua_State* L)
+static int unit_world_pose(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -139,7 +139,7 @@ CE_EXPORT int unit_world_pose(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_set_local_position(lua_State* L)
+static int unit_set_local_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -152,7 +152,7 @@ CE_EXPORT int unit_set_local_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_set_local_rotation(lua_State* L)
+static int unit_set_local_rotation(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -165,7 +165,7 @@ CE_EXPORT int unit_set_local_rotation(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_set_local_pose(lua_State* L)
+static int unit_set_local_pose(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -178,7 +178,7 @@ CE_EXPORT int unit_set_local_pose(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_link_node(lua_State* L)
+static int unit_link_node(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -191,7 +191,7 @@ CE_EXPORT int unit_link_node(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_unlink_node(lua_State* L)
+static int unit_unlink_node(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -203,7 +203,7 @@ CE_EXPORT int unit_unlink_node(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_camera(lua_State* L)
+static int unit_camera(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -215,7 +215,7 @@ CE_EXPORT int unit_camera(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_mesh(lua_State* L)
+static int unit_mesh(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -227,7 +227,7 @@ CE_EXPORT int unit_mesh(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_sprite(lua_State* L)
+static int unit_sprite(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -239,7 +239,7 @@ CE_EXPORT int unit_sprite(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_actor(lua_State* L)
+static int unit_actor(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -251,7 +251,7 @@ CE_EXPORT int unit_actor(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_controller(lua_State* L)
+static int unit_controller(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -263,7 +263,7 @@ CE_EXPORT int unit_controller(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int unit_is_a(lua_State* L)
+static int unit_is_a(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 29 - 19
engine/lua/LuaVector2.cpp

@@ -32,7 +32,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_new(lua_State* L)
+static int vector2_new(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -45,7 +45,15 @@ CE_EXPORT int vector2_new(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_x(lua_State* L)
+static int vector2_ctor(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.remove(1); // Remove table
+	return vector2_new(L);
+}
+
+//-----------------------------------------------------------------------------
+static int vector2_x(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -56,7 +64,7 @@ CE_EXPORT int vector2_x(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_y(lua_State* L)
+static int vector2_y(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -67,7 +75,7 @@ CE_EXPORT int vector2_y(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_set_x(lua_State* L)
+static int vector2_set_x(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -79,7 +87,7 @@ CE_EXPORT int vector2_set_x(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_set_y(lua_State* L)
+static int vector2_set_y(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -91,7 +99,7 @@ CE_EXPORT int vector2_set_y(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_values(lua_State* L)
+static int vector2_values(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -104,7 +112,7 @@ CE_EXPORT int vector2_values(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_add(lua_State* L)
+static int vector2_add(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -117,7 +125,7 @@ CE_EXPORT int vector2_add(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_subtract(lua_State* L)
+static int vector2_subtract(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -130,7 +138,7 @@ CE_EXPORT int vector2_subtract(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_multiply(lua_State* L)
+static int vector2_multiply(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -143,7 +151,7 @@ CE_EXPORT int vector2_multiply(lua_State* L)
 }			
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_divide(lua_State* L)
+static int vector2_divide(lua_State* L)
 {
 	LuaStack stack(L);
 	
@@ -156,7 +164,7 @@ CE_EXPORT int vector2_divide(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_dot(lua_State* L)
+static int vector2_dot(lua_State* L)
 {
 	LuaStack stack(L);
 	
@@ -169,7 +177,7 @@ CE_EXPORT int vector2_dot(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_equal(lua_State* L)
+static int vector2_equal(lua_State* L)
 {
 	LuaStack stack(L);
 	
@@ -182,7 +190,7 @@ CE_EXPORT int vector2_equal(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_length(lua_State* L)
+static int vector2_length(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -194,7 +202,7 @@ CE_EXPORT int vector2_length(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_squared_length(lua_State* L)
+static int vector2_squared_length(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -206,7 +214,7 @@ CE_EXPORT int vector2_squared_length(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_set_length(lua_State* L)
+static int vector2_set_length(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -219,7 +227,7 @@ CE_EXPORT int vector2_set_length(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_normalize(lua_State* L)
+static int vector2_normalize(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -231,7 +239,7 @@ CE_EXPORT int vector2_normalize(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_negate(lua_State* L)
+static int vector2_negate(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -243,7 +251,7 @@ CE_EXPORT int vector2_negate(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_distance(lua_State* L)
+static int vector2_distance(lua_State* L)
 {
 	LuaStack stack(L);
 	
@@ -256,7 +264,7 @@ CE_EXPORT int vector2_distance(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector2_angle(lua_State* L)
+static int vector2_angle(lua_State* L)
 {
 	LuaStack stack(L);
 	
@@ -289,6 +297,8 @@ void load_vector2(LuaEnvironment& env)
 	env.load_module_function("Vector2", "normalize",			vector2_normalize);
 	env.load_module_function("Vector2", "distance",				vector2_distance);
 	env.load_module_function("Vector2", "angle",				vector2_angle);
+
+	env.load_module_constructor("Vector2",						vector2_ctor);
 }
 
 } // namespace crown

+ 32 - 22
engine/lua/LuaVector3.cpp

@@ -32,7 +32,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_new(lua_State* L)
+static int vector3_new(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -46,7 +46,15 @@ CE_EXPORT int vector3_new(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_x(lua_State* L)
+static int vector3_ctor(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.remove(1); // Remove table
+	return vector3_new(L);
+}
+
+//-----------------------------------------------------------------------------
+static int vector3_x(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -57,7 +65,7 @@ CE_EXPORT int vector3_x(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_y(lua_State* L)
+static int vector3_y(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -68,7 +76,7 @@ CE_EXPORT int vector3_y(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_z(lua_State* L)
+static int vector3_z(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -79,7 +87,7 @@ CE_EXPORT int vector3_z(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_set_x(lua_State* L)
+static int vector3_set_x(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -91,7 +99,7 @@ CE_EXPORT int vector3_set_x(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_set_y(lua_State* L)
+static int vector3_set_y(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -103,7 +111,7 @@ CE_EXPORT int vector3_set_y(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_set_z(lua_State* L)
+static int vector3_set_z(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -115,7 +123,7 @@ CE_EXPORT int vector3_set_z(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_values(lua_State* L)
+static int vector3_values(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -129,7 +137,7 @@ CE_EXPORT int vector3_values(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_add(lua_State* L)
+static int vector3_add(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -142,7 +150,7 @@ CE_EXPORT int vector3_add(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_subtract(lua_State* L)
+static int vector3_subtract(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -155,7 +163,7 @@ CE_EXPORT int vector3_subtract(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_multiply(lua_State* L)
+static int vector3_multiply(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -168,7 +176,7 @@ CE_EXPORT int vector3_multiply(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_divide(lua_State* L)
+static int vector3_divide(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -181,7 +189,7 @@ CE_EXPORT int vector3_divide(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_dot(lua_State* L)
+static int vector3_dot(lua_State* L)
 {
 	LuaStack stack(L);
 	
@@ -194,7 +202,7 @@ CE_EXPORT int vector3_dot(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_cross(lua_State* L)
+static int vector3_cross(lua_State* L)
 {
 	LuaStack stack(L);
 	
@@ -207,7 +215,7 @@ CE_EXPORT int vector3_cross(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_equal(lua_State* L)
+static int vector3_equal(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -220,7 +228,7 @@ CE_EXPORT int vector3_equal(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_length(lua_State* L)
+static int vector3_length(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -232,7 +240,7 @@ CE_EXPORT int vector3_length(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_squared_length(lua_State* L)
+static int vector3_squared_length(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -244,7 +252,7 @@ CE_EXPORT int vector3_squared_length(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_set_length(lua_State* L)
+static int vector3_set_length(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -257,7 +265,7 @@ CE_EXPORT int vector3_set_length(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_normalize(lua_State* L)
+static int vector3_normalize(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -269,7 +277,7 @@ CE_EXPORT int vector3_normalize(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_negate(lua_State* L)
+static int vector3_negate(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -281,7 +289,7 @@ CE_EXPORT int vector3_negate(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_distance(lua_State* L)
+static int vector3_distance(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -294,7 +302,7 @@ CE_EXPORT int vector3_distance(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3_angle(lua_State* L)
+static int vector3_angle(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -330,6 +338,8 @@ void load_vector3(LuaEnvironment& env)
 	env.load_module_function("Vector3", "normalize", 		vector3_normalize);
 	env.load_module_function("Vector3", "distance", 		vector3_distance);
 	env.load_module_function("Vector3", "angle",			vector3_angle);
+
+	env.load_module_constructor("Vector3",					vector3_ctor);
 }
 
 } // namespace crown

+ 50 - 32
engine/lua/LuaVector3Box.cpp

@@ -33,7 +33,37 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3box_get_value(lua_State* L)
+static int vector3box_new(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Vector3 v;
+	if (stack.num_args() == 2)
+	{
+		const Vector3 tv = stack.get_vector3(1);
+		v = tv;
+	}
+	else if (stack.num_args() == 4)
+	{
+		v.x = stack.get_float(1);
+		v.y = stack.get_float(2);
+		v.z = stack.get_float(3);
+	}
+
+	stack.push_vector3box(v);
+	return 1;
+}
+
+//-----------------------------------------------------------------------------
+static int vector3box_ctor(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.remove(1); // Remove table
+	return vector3box_new(L);
+}
+
+//-----------------------------------------------------------------------------
+static int vector3box_get_value(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -61,7 +91,7 @@ CE_EXPORT int vector3box_get_value(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3box_set_value(lua_State* L)
+static int vector3box_set_value(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -86,16 +116,12 @@ CE_EXPORT int vector3box_set_value(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3box(lua_State* L)
+static int vector3box_store(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vector3& v = stack.push_vector3box();
-
-	// Associates a metatable to userdata
-	stack.get_global_metatable("Vector3Box_i_mt");
-	stack.set_metatable();
-
+	Vector3& v = stack.get_vector3box(1);
+	
 	if (stack.num_args() == 2)
 	{
 		Vector3 tv = stack.get_vector3(1);
@@ -108,48 +134,40 @@ CE_EXPORT int vector3box(lua_State* L)
 		v.y = stack.get_float(2);
 		v.z = stack.get_float(3);
 	}
-	return 1;
+	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3box_store(lua_State* L)
+static int vector3box_unbox(lua_State* L)
 {
 	LuaStack stack(L);
 
 	Vector3& v = stack.get_vector3box(1);
-	
-	if (stack.num_args() == 2)
-	{
-		Vector3 tv = stack.get_vector3(1);
 
-		v = tv;
-	}
-	else if (stack.num_args() == 4)
-	{
-		v.x = stack.get_float(1);
-		v.y = stack.get_float(2);
-		v.z = stack.get_float(3);
-	}
-	return 0;
+	stack.push_vector3(v);
+	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int vector3box_unbox(lua_State* L)
+static int vector3box_tostring(lua_State* L)
 {
 	LuaStack stack(L);
-
 	Vector3& v = stack.get_vector3box(1);
-
-	stack.push_vector3(v);
+	stack.push_fstring("Vector3Box (%p)", &v);
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
 void load_vector3box(LuaEnvironment& env)
 {
-	env.load_module_function("Vector3Box", "new", vector3box);
-	env.load_module_function("Vector3Box", "store", vector3box_store);
-	env.load_module_function("Vector3Box", "unbox", vector3box_unbox);
+	env.load_module_function("Vector3Box", "new",			vector3box_new);
+	env.load_module_function("Vector3Box", "store",			vector3box_store);
+	env.load_module_function("Vector3Box", "unbox",			vector3box_unbox);
+	env.load_module_function("Vector3Box", "__index",		vector3box_get_value);
+	env.load_module_function("Vector3Box", "__newindex",	vector3box_set_value);
+	env.load_module_function("Vector3Box", "__tostring",	vector3box_tostring);
+
+	env.load_module_constructor("Vector3Box",				vector3box_ctor);
 }
 
-} // namespace crown
+} // namespace crown

+ 12 - 12
engine/lua/LuaWindow.cpp

@@ -33,7 +33,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int window_show(lua_State* L)
+static int window_show(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -43,7 +43,7 @@ CE_EXPORT int window_show(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int window_hide(lua_State* L)
+static int window_hide(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -53,7 +53,7 @@ CE_EXPORT int window_hide(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int window_get_size(lua_State* L)
+static int window_get_size(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -68,7 +68,7 @@ CE_EXPORT int window_get_size(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int window_get_position(lua_State* L)
+static int window_get_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -83,7 +83,7 @@ CE_EXPORT int window_get_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int window_resize(lua_State* L)
+static int window_resize(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -96,7 +96,7 @@ CE_EXPORT int window_resize(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int window_move(lua_State* L)
+static int window_move(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -109,21 +109,21 @@ CE_EXPORT int window_move(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int window_minimize(lua_State* /*L*/)
+static int window_minimize(lua_State* /*L*/)
 {
 	device()->window()->minimize();
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int window_restore(lua_State* /*L*/)
+static int window_restore(lua_State* /*L*/)
 {
 	device()->window()->restore();
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int window_is_resizable(lua_State* L)
+static int window_is_resizable(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -133,7 +133,7 @@ CE_EXPORT int window_is_resizable(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int window_set_resizable(lua_State* L)
+static int window_set_resizable(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -143,7 +143,7 @@ CE_EXPORT int window_set_resizable(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int window_title(lua_State* L)
+static int window_title(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -155,7 +155,7 @@ CE_EXPORT int window_title(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int window_set_title(lua_State* L)
+static int window_set_title(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 30 - 17
engine/lua/LuaWorld.cpp

@@ -29,12 +29,13 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "World.h"
 #include "Gui.h"
 #include "Device.h"
+#include "LuaSystem.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_spawn_unit(lua_State* L)
+static int world_spawn_unit(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -51,7 +52,7 @@ CE_EXPORT int world_spawn_unit(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_destroy_unit(lua_State* L)
+static int world_destroy_unit(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -63,7 +64,7 @@ CE_EXPORT int world_destroy_unit(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_num_units(lua_State* L)
+static int world_num_units(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -74,7 +75,7 @@ CE_EXPORT int world_num_units(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_play_sound(lua_State* L)
+static int world_play_sound(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -93,7 +94,7 @@ CE_EXPORT int world_play_sound(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_stop_sound(lua_State* L)
+static int world_stop_sound(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -105,7 +106,7 @@ CE_EXPORT int world_stop_sound(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_link_sound(lua_State* L)
+static int world_link_sound(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -119,7 +120,7 @@ CE_EXPORT int world_link_sound(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_set_listener_pose(lua_State* L)
+static int world_set_listener_pose(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -130,7 +131,7 @@ CE_EXPORT int world_set_listener_pose(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_set_sound_position(lua_State* L)
+static int world_set_sound_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -143,7 +144,7 @@ CE_EXPORT int world_set_sound_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_set_sound_range(lua_State* L)
+static int world_set_sound_range(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -156,7 +157,7 @@ CE_EXPORT int world_set_sound_range(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_set_sound_volume(lua_State* L)
+static int world_set_sound_volume(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -169,7 +170,7 @@ CE_EXPORT int world_set_sound_volume(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_create_window_gui(lua_State* L)
+static int world_create_window_gui(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -181,7 +182,7 @@ CE_EXPORT int world_create_window_gui(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_destroy_gui(lua_State* L)
+static int world_destroy_gui(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -194,7 +195,7 @@ CE_EXPORT int world_destroy_gui(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_physics_world(lua_State* L)
+static int world_physics_world(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -205,7 +206,7 @@ CE_EXPORT int world_physics_world(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_sound_world(lua_State* L)
+static int world_sound_world(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -216,7 +217,7 @@ CE_EXPORT int world_sound_world(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_create_debug_line(lua_State* L)
+static int world_create_debug_line(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -228,7 +229,7 @@ CE_EXPORT int world_create_debug_line(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int world_destroy_debug_line(lua_State* L)
+static int world_destroy_debug_line(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -239,6 +240,15 @@ CE_EXPORT int world_destroy_debug_line(lua_State* L)
 	return 0;
 }
 
+//-----------------------------------------------------------------------------
+static int world_tostring(lua_State* L)
+{
+	LuaStack stack(L);
+	World* w = stack.get_world(1);
+	stack.push_fstring("World (%p)", w);
+	return 1;
+}
+
 //-----------------------------------------------------------------------------
 void load_world(LuaEnvironment& env)
 {
@@ -262,6 +272,9 @@ void load_world(LuaEnvironment& env)
 
 	env.load_module_function("World", "create_debug_line",  world_create_debug_line);
 	env.load_module_function("World", "destroy_debug_line", world_destroy_debug_line);
+
+	env.load_module_function("World", "__index",			"World");
+	env.load_module_function("World", "__tostring",			world_tostring);
 }
 
-} // namespace crown
+} // namespace crown