Index: code/branches/pch/src/tolua/lua/function.lua
===================================================================
--- code/branches/pch/src/tolua/lua/function.lua	(revision 3114)
+++ code/branches/pch/src/tolua/lua/function.lua	(revision 3127)
@@ -209,5 +209,5 @@
     if class then narg=2 else narg=1 end
     if class then
-        local func = 'tolua_isusertype'
+        local func = get_is_function(self.parent.type)
         local type = self.parent.type
         if self.name=='new' or static~=nil then
@@ -226,5 +226,5 @@
             local btype = isbasic(self.args[i].type)
             if btype ~= 'value' and btype ~= 'state' then
-                output('     !'..self.args[i]:outchecktype(narg)..' ||\n')
+                output('     '..self.args[i]:outchecktype(narg)..' ||\n')
             end
             if btype ~= 'state' then
@@ -250,5 +250,6 @@
         output(' ',self.const,self.parent.type,'*','self = ')
         output('(',self.const,self.parent.type,'*) ')
-        output('tolua_tousertype(tolua_S,1,0);')
+        local to_func = get_to_function(self.parent.type)
+        output(to_func,'(tolua_S,1,0);')
     elseif static then
         _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)')
@@ -269,5 +270,5 @@
     if class and self.name~='new' and static==nil then
         output('#ifndef TOLUA_RELEASE\n')
-        output('  if (!self) tolua_error(tolua_S,"invalid \'self\' in function \''..self.name..'\'",NULL);');
+        output('  if (!self) tolua_error(tolua_S,"'..output_error_hook("invalid \'self\' in function \'%s\'", self.name)..'", NULL);');
         output('#endif\n')
     end
@@ -312,4 +313,6 @@
     --------------------------------------------------
 
+    pre_call_hook(self)
+
     local out = string.find(self.mod, "tolua_outside")
 
@@ -323,5 +326,5 @@
     -- call function
     if class and self.name=='delete' then
-        output('  delete self;')
+        output('  Mtolua_delete(self);')
     elseif class and self.name == 'operator&[]' then
         if flags['1'] then -- for compatibility with tolua5 ?
@@ -352,5 +355,5 @@
     end
     if class and self.name=='new' then
-        output('new',self.type,'(')
+        output('Mtolua_new((',self.type,')(')
     elseif class and static then
         if out then
@@ -364,5 +367,6 @@
         else
             if self.cast_operator then
-                output('static_cast<',self.mod,self.type,self.ptr,'>(*self')
+                --output('static_cast<',self.mod,self.type,self.ptr,' >(*self')
+                output('self->operator ',self.mod,self.type,'(')
             else
                 output('self->'..self.name,'(')
@@ -392,5 +396,9 @@
         output('-1);')
     else
-        output(');')
+        if class and self.name=='new' then
+            output('));') -- close Mtolua_new(
+        else
+            output(');')
+        end
     end
 
@@ -399,5 +407,5 @@
         nret = nret + 1
         local t,ct = isbasic(self.type)
-        if t then
+        if t and self.name ~= "new" then
             if self.cast_operator and _basic_raw_push[t] then
                 output('   ',_basic_raw_push[t],'(tolua_S,(',ct,')tolua_ret);')
@@ -408,21 +416,27 @@
                 t = self.type
                 new_t = string.gsub(t, "const%s+", "")
+                local owned = false
+                if string.find(self.mod, "tolua_owned") then
+                    owned = true
+                end
+                local push_func = get_push_function(t)
                 if self.ptr == '' then
                     output('   {')
                     output('#ifdef __cplusplus\n')
-                    output('    void* tolua_obj = new',new_t,'(tolua_ret);')
-                    output('    tolua_pushusertype_and_takeownership(tolua_S,tolua_obj,"',t,'");')
+                    output('    void* tolua_obj = Mtolua_new((',new_t,')(tolua_ret));')
+                    output('    ',push_func,'(tolua_S,tolua_obj,"',t,'");')
+                    output('    tolua_register_gc(tolua_S,lua_gettop(tolua_S));')
                     output('#else\n')
                     output('    void* tolua_obj = tolua_copy(tolua_S,(void*)&tolua_ret,sizeof(',t,'));')
-                    output('    tolua_pushusertype_and_takeownership(tolua_S,tolua_obj,"',t,'");')
+                    output('    ',push_func,'(tolua_S,tolua_obj,"',t,'");')
+                    output('    tolua_register_gc(tolua_S,lua_gettop(tolua_S));')
                     output('#endif\n')
                     output('   }')
                 elseif self.ptr == '&' then
-                    output('   tolua_pushusertype(tolua_S,(void*)&tolua_ret,"',t,'");')
+                    output('   ',push_func,'(tolua_S,(void*)&tolua_ret,"',t,'");')
                 else
-                    if local_constructor then
-                        output('   tolua_pushusertype_and_takeownership(tolua_S,(void *)tolua_ret,"',t,'");')
-                    else
-                        output('   tolua_pushusertype(tolua_S,(void*)tolua_ret,"',t,'");')
+                    output('   ',push_func,'(tolua_S,(void*)tolua_ret,"',t,'");')
+                    if owned or local_constructor then
+                        output('    tolua_register_gc(tolua_S,lua_gettop(tolua_S));')
                     end
                 end
@@ -466,4 +480,6 @@
     end
 
+    post_call_hook(self)
+
     output(' }')
     output(' return '..nret..';')
@@ -474,5 +490,5 @@
         output('#ifndef TOLUA_RELEASE\n')
         output('tolua_lerror:\n')
-        output(' tolua_error(tolua_S,"#ferror in function \''..self.lname..'\'.",&tolua_err);')
+        output(' tolua_error(tolua_S,"'..output_error_hook("#ferror in function \'%s\'.", self.lname)..'",&tolua_err);')
         output(' return 0;')
         output('#endif\n')
@@ -574,5 +590,5 @@
     if string.find(par, "%*") then -- it's a pointer with a default value
 
-        if string.find(par, '=%s*new') then -- it's a pointer with an instance as default parameter.. is that valid?
+        if string.find(par, '=%s*new') or string.find(par, "%(") then -- it's a pointer with an instance as default parameter.. is that valid?
             return true
         end
@@ -661,5 +677,5 @@
         --ns = strip_defaults(ns)
 
-        Function(d, ns, c)
+        local f = Function(d, ns, c)
         for i=1,last do
             t[i] = string.gsub(t[i], "=.*$", "")
