Index: code/branches/pch/src/tolua/lua/package.lua
===================================================================
--- code/branches/pch/src/tolua/lua/package.lua	(revision 3114)
+++ code/branches/pch/src/tolua/lua/package.lua	(revision 3127)
@@ -42,6 +42,5 @@
                                                return "\n#["..getn(L).."]#"
                                            end
-    )
-    -- avoid preprocessing embedded C code
+    )    -- avoid preprocessing embedded C code
     local C = {}
     self.code = gsub(self.code,"\n%s*%$%<","\3") -- deal with embedded C code
@@ -51,6 +50,5 @@
                                                return "\n#<"..getn(C)..">#"
                                            end
-    )
-    -- avoid preprocessing embedded C code
+    )    -- avoid preprocessing embedded C code
     self.code = gsub(self.code,"\n%s*%$%{","\5") -- deal with embedded C code
     self.code = gsub(self.code,"\n%s*%$%}","\6")
@@ -60,5 +58,4 @@
                                            end
     )
-
     --self.code = gsub(self.code,"\n%s*#[^d][^\n]*\n", "\n\n") -- eliminate preprocessor directives that don't start with 'd'
     self.code = gsub(self.code,"\n[ \t]*#[ \t]*[^d%<%[]", "\n//") -- eliminate preprocessor directives that don't start with 'd'
@@ -71,5 +68,4 @@
                                                          end
     )
-
     -- perform global substitution
 
@@ -94,23 +90,18 @@
                                                     return L[tonumber(n)]
                                                 end
-    )
-    -- restore embedded C code
+    )    -- restore embedded C code
     self.code = gsub(self.code,"%#%<(%d+)%>%#", function (n)
                                                     return C[tonumber(n)]
                                                 end
-    )
-    -- restore verbatim lines
+    )    -- restore verbatim lines
     self.code = gsub(self.code,"%#(%d+)%#", function (n)
                                                 return V[tonumber(n)]
                                             end
     )
-
     self.code = string.gsub(self.code, "\n%s*%$([^\n]+)", function (l)
                                                               Verbatim(l.."\n")
                                                               return "\n"
                                                           end
-    )
-end
-
+    )end
 -- translate verbatim
 function classPackage:preamble ()
@@ -146,5 +137,5 @@
             output('{')
             output(' '..i..'* self = ('..i..'*) tolua_tousertype(tolua_S,1,0);')
-            output('    delete self;')
+            output('    Mtolua_delete(self);')
             output('    return 0;')
             output('}')
@@ -157,9 +148,16 @@
     output('static void tolua_reg_types (lua_State* tolua_S)')
     output('{')
-    foreach(_usertype,function(n,v) output(' tolua_usertype(tolua_S,"',v,'");') end)
+
     if flags.t then
         output("#ifndef Mtolua_typeid\n#define Mtolua_typeid(L,TI,T)\n#endif\n")
-        foreach(_usertype,function(n,v) output(' Mtolua_typeid(tolua_S,typeid(',v,'), "',v,'");') end)
-    end
+    end
+    foreach(_usertype,function(n,v)
+        if (not _global_classes[v]) or _global_classes[v]:check_public_access() then
+            output(' tolua_usertype(tolua_S,"',v,'");')
+            if flags.t then
+                output(' Mtolua_typeid(tolua_S,typeid(',v,'), "',v,'");')
+            end
+        end
+    end)
     output('}')
     output('\n')
@@ -191,5 +189,5 @@
     output(pre.."int luaopen_"..self.name.." (lua_State* tolua_S) {")
     output(pre.." return tolua_"..self.name.."_open(tolua_S);")
-    output(pre.."};")
+    output(pre.."}")
     output("#endif\n\n")
 
@@ -204,10 +202,7 @@
 
     if flags.H then
-        local package_lower = string.lower(self.name)
-        output('#include "'..package_lower..'/'..self.name..'Prereqs.h"\n')
-        output('/* Exported function */')
+        local package_lower = string.lower(self.name)        output('#include "'..package_lower..'/'..self.name..'Prereqs.h"\n')        output('/* Exported function */')
         output('_'..self.name..'Export')
-        output('int  tolua_'..self.name..'_open (lua_State* tolua_S);')
-        output('\n')
+        output('int  tolua_'..self.name..'_open (lua_State* tolua_S);')        output('\n')
     end
 end
@@ -224,9 +219,5 @@
     local code = '\n$#include "'..string.lower(flags.n)..'/'..fn..'"\n'
     s= "\n" .. s .. "\n" -- add blank lines as sentinels
-
-    -- eliminate export macro problems in class declarations
-    s = gsub(s, ' _%w*Export ', ' ')
-
-    local _,e,c,t = strfind(s, "\n([^\n]-)[Tt][Oo][Ll][Uu][Aa]_([^%s]*)[^\n]*\n")
+    -- eliminate export macro problems in class declarations    s = gsub(s, ' _%w*Export ', ' ')    local _,e,c,t = strfind(s, "\n([^\n]-)[Tt][Oo][Ll][Uu][Aa]_([^%s]*)[^\n]*\n")
     while e do
         t = strlower(t)
@@ -249,24 +240,20 @@
 
     -- open input file, if any
+    local st,msg
     if fn then
         local file
-        if flags.f then
-            if string.sub(flags.f, 1, 1) == '/' or string.sub(flags.f, 1, 1) == '\\' or (string.len(flags.f) > 1 and string.sub(flags.f, 2, 2) == ':') then
-                file = flags.f
-            else
-                file = flags.w..'/'..flags.f
-            end
-        else
-            file = flags.f
-        end
-        local st, msg = readfrom(file)
-        if not st then
+        if flags.f then            if string.sub(flags.f, 1, 1) == '/' or string.sub(flags.f, 1, 1) == '\\' or (string.len(flags.f) > 1 and string.sub(flags.f, 2, 2) == ':') then                file = flags.f            else                file = flags.w..'/'..flags.f            end        else            file = flags.f        end        st, msg = readfrom(flags.f)        if not st then
             error('#'..msg..' path: '..flags.f)
         end
         local _; _, _, ext = strfind(fn,".*%.(.*)$")
     end
-    local code = "\n" .. read('*a')
-    if ext == 'h' or ext == 'hpp' then
-        code = extract_code(fn,code)
+    local code
+    if ext == 'pkg' then
+        code = prep(st)
+    else
+        code = "\n" .. read('*a')
+        if ext == 'h' or ext == 'hpp' then
+            code = extract_code(fn,code)
+        end
     end
 
@@ -276,19 +263,5 @@
     end
 
-    -- prepare working directory
-    local current_path
-    if not flags.w and flags.f then
-        current_path = gsub(flags.f, '(/)[^/]*%.?[^/]*$', '%1')
-    elseif flags.w then
-        if not (string.sub(flags.w, string.len(flags.w)) == '/') then
-            current_path = flags.w..'/'
-        else
-            current_path = flags.w
-        end
-    else
-        current_path = ''
-    end
-
-    -- deal with include directive
+    -- prepare working directory    local current_path    if not flags.w and flags.f then        current_path = gsub(flags.f, '(/)[^/]*%.?[^/]*$', '%1')    elseif flags.w then        if not (string.sub(flags.w, string.len(flags.w)) == '/') then            current_path = flags.w..'/'        else            current_path = flags.w        end    else        current_path = ''    end    -- deal with include directive
     local nsubst
     repeat
@@ -300,10 +273,13 @@
                     error('#'..msg..': '..fn)
                 end
+                if kind == 'p' then
+                    local s = prep(fp)
+                    closefile(fp)
+                    return s
+                end
                 local s = read(fp,'*a')
                 closefile(fp)
                 if kind == 'c' or kind == 'h' then
                     return extract_code(fn,s)
-                elseif kind == 'p' then
-                    return "\n\n" .. s
                 elseif kind == 'l' then
                     return "\n$[--##"..fn.."\n" .. s .. "\n$]\n"
@@ -318,6 +294,5 @@
                 end
             end
-        )
-    until nsubst==0
+        )    until nsubst==0
 
     -- deal with renaming directive
@@ -337,2 +312,31 @@
 
 
+setmetatable(_extra_parameters, { __index = _G })
+
+function prep(file)
+
+    local chunk = {'local __ret = {"\\n"}\n'}
+    for line in file:lines() do
+       if string.find(line, "^##") then
+           table.insert(chunk, string.sub(line, 3) .. "\n")
+       else
+           local last = 1
+           for text, expr, index in string.gfind(line, "(.-)$(%b())()") do 
+               last = index
+               if text ~= "" then
+                   table.insert(chunk, string.format('table.insert(__ret, %q )', text))
+               end
+               table.insert(chunk, string.format('table.insert(__ret, %s )', expr))
+           end
+           table.insert(chunk, string.format('table.insert(__ret, %q)\n',
+                               string.sub(line, last).."\n"))
+       end
+    end
+    table.insert(chunk, '\nreturn table.concat(__ret)\n')
+    local f,e = loadstring(table.concat(chunk))
+    if e then
+        error("#"..e)
+    end
+    setfenv(f, _extra_parameters)
+    return f()
+end
