Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 9, 2009, 4:29:42 PM (15 years ago)
Author:
rgrieder
Message:

Update to tolua 1.0.93

Location:
code/branches/pch/src/tolua/lua
Files:
1 added
14 edited

Legend:

Unmodified
Added
Removed
  • code/branches/pch/src/tolua/lua/array.lua

    r2710 r3127  
    102102    -- return value
    103103    local t,ct = isbasic(self.type)
     104    local push_func = get_push_function(t)
    104105    if t then
    105106        output(' tolua_push'..t..'(tolua_S,(',ct,')'..self:getvalue(class,static)..');')
     
    107108        t = self.type
    108109        if self.ptr == '&' or self.ptr == '' then
    109             output(' tolua_pushusertype(tolua_S,(void*)&'..self:getvalue(class,static)..',"',t,'");')
    110         else
    111             output(' tolua_pushusertype(tolua_S,(void*)'..self:getvalue(class,static)..',"',t,'");')
     110            output(' ',push_func,'(tolua_S,(void*)&'..self:getvalue(class,static)..',"',t,'");')
     111        else
     112            output(' ',push_func,'(tolua_S,(void*)'..self:getvalue(class,static)..',"',t,'");')
    112113        end
    113114    end
     
    193194            output('tolua_to'..t,'(tolua_S,3,',def,'));')
    194195        else
    195             output('tolua_tousertype(tolua_S,3,',def,'));')
     196            local to_func = get_to_function(self.type)
     197            output(to_func,'(tolua_S,3,',def,'));')
    196198        end
    197199        output(' return 0;')
     
    203205
    204206function classArray:register (pre)
     207    if not self:check_public_access() then
     208        return
     209    end
     210
    205211    pre = pre or ''
    206212    if self.csetname then
  • code/branches/pch/src/tolua/lua/basic.lua

    r2710 r3127  
    112112
    113113function warning (msg)
     114    if flags.q then
     115        return
     116    end
    114117    local out = _OUTPUT
    115118    _OUTPUT = _STDERR
     
    333336end
    334337
     338-- called before starting output
     339function pre_output_hook(package)
     340
     341end
    335342
    336343-- called after writing all the output.
     
    354361end
    355362
    356 
     363-- called from classFunction:supcode, before the call to the function is output
     364function pre_call_hook(f)
     365
     366end
     367
     368-- called from classFunction:supcode, after the call to the function is output
     369function post_call_hook(f)
     370
     371end
     372
     373-- called before the register code is output
     374function pre_register_hook(package)
     375
     376end
     377
     378-- called to output an error message
     379function output_error_hook(...)
     380    return string.format(...)
     381end
     382
     383-- custom pushers
     384
     385_push_functions = {}
     386_is_functions = {}
     387_to_functions = {}
     388
     389_base_push_functions = {}
     390_base_is_functions = {}
     391_base_to_functions = {}
     392
     393local function search_base(t, funcs)
     394    local class = _global_classes[t]
     395
     396    while class do
     397        if funcs[class.type] then
     398            return funcs[class.type]
     399        end
     400        class = _global_classes[class.btype]
     401    end
     402    return nil
     403end
     404
     405function get_push_function(t)
     406    return _push_functions[t] or search_base(t, _base_push_functions) or "tolua_pushusertype"
     407end
     408
     409function get_to_function(t)
     410    return _to_functions[t] or search_base(t, _base_to_functions) or "tolua_tousertype"
     411end
     412 
     413function get_is_function(t)
     414    return _is_functions[t] or search_base(t, _base_is_functions) or "tolua_isusertype"
     415end
     416
  • code/branches/pch/src/tolua/lua/class.lua

    r2710 r3127  
    6464-- return collection requirement
    6565function classClass:requirecollection (t)
    66     if self.flags.protected_destructor then
     66    if self.flags.protected_destructor or (not self:check_public_access()) then
    6767        return false
    6868    end
     
    148148    -- check for template
    149149    b = string.gsub(b, "^{%s*TEMPLATE_BIND", "{\nTOLUA_TEMPLATE_BIND")
    150     local t,_,T,I = string.find(b, "^{%s*TOLUA_TEMPLATE_BIND%s*%(+%s*\"?([^\",]*)\"?%s*,%s*([^%)]*)%s*%)+")
     150    local t,_,T,I = string.find(b, '^{%s*TOLUA_TEMPLATE_BIND%s*%(+%s*\"?([^\",]*)\"?%s*,%s*([^%)]*)%s*%)+')
    151151    if t then
    152152
     
    159159        local bs = string.gsub(b, "^{%s*TOLUA_TEMPLATE_BIND[^\n]*\n", "{\n")
    160160
    161         -- replace
    162         for i =1 , types.n do
    163 
    164             local Tl = split(T, " ")
    165             local Il = split_c_tokens(types[i], " ")
    166             local bI = bs
    167             local pI = {}
    168             for j = 1,Tl.n do
    169                 Tl[j] = findtype(Tl[j]) or Tl[j]
    170                 bI = string.gsub(bI, "([^_%w])"..Tl[j].."([^_%w])", "%1"..Il[j].."%2")
    171                 if p then
    172                     for i=1,table.getn(p) do
    173                         pI[i] = string.gsub(p[i], "([^_%w]?)"..Tl[j].."([^_%w]?)", "%1"..Il[j].."%2")
    174                     end
    175                 end
    176             end
    177             --local append = "<"..string.gsub(types[i], "%s+", ",")..">"
    178             local append = "<"..concat(Il, 1, table.getn(Il), ",")..">"
    179             append = string.gsub(append, "%s*,%s*", ",")
    180             append = string.gsub(append, ">>", "> >")
    181             for i=1,table.getn(pI) do
    182                 --pI[i] = string.gsub(pI[i], ">>", "> >")
    183                 pI[i] = resolve_template_types(pI[i])
    184             end
    185             bI = string.gsub(bI, ">>", "> >")
    186             Class(n..append, pI, bI)
    187         end
     161        local Tl = split(T, " ")
     162        local tc = TemplateClass(n, p, bs, Tl)
     163
     164        tc:throw(types, true)
     165        --for i=1,types.n do
     166        --    tc:throw(split_c_tokens(types[i], " "), true)
     167        --end
    188168        return
    189169    end
  • code/branches/pch/src/tolua/lua/code.lua

    r2710 r3127  
    4747    end
    4848
     49    -- pad to 16 bytes
     50    local npad = 16 - (#s % 16)
     51    local spad = ""
     52    for i=1,npad do
     53        spad = spad .. "-"
     54    end
     55    s = s..spad
     56
    4957    -- convert to C
    5058    output('\n'..pre..'{ /* begin embedded lua code */\n')
    5159    output(pre..' int top = lua_gettop(tolua_S);')
    52     output(pre..' static unsigned char B[] = {\n   ')
     60    output(pre..' static const unsigned char B[] = {\n   ')
    5361    local t={n=0}
    5462    local b = gsub(s, '(.)',
  • code/branches/pch/src/tolua/lua/compat-5.1.lua

    r2710 r3127  
    2626
    2727    local f = load(getfile, path)
     28    if not f then
     29        error("error loading file "..path)
     30    end
    2831    return f()
    2932end
  • code/branches/pch/src/tolua/lua/container.lua

    r2710 r3127  
    615615            b,e,decl,arg,const = strfind(s,"^%s*([_%w])%s*(%b())%s*(c?o?n?s?t?)%s*;%s*")
    616616        end
     617        if not b then
     618            -- try function pointer
     619            b,e,decl,arg,const = strfind(s,"^%s*([^%(;\n]+%b())%s*(%b())%s*;%s*")
     620            if b then
     621                decl = string.gsub(decl, "%(%s*%*([^%)]*)%s*%)", " %1 ")
     622            end
     623        end
    617624        if b then
    618625            if virt and string.find(virt, "[=0]") then
     
    647654        base = '' body = ''
    648655        b,e,name = strfind(s,"^%s*class%s*([_%w][_%w@]*)%s*;")  -- dummy class
     656        local dummy = false
    649657        if not b then
    650658            b,e,name = strfind(s,"^%s*struct%s*([_%w][_%w@]*)%s*;")    -- dummy struct
    651659            if not b then
    652                 b,e,name,base,body = strfind(s,"^%s*class%s*([_%w][_%w@]*)%s*(.-)%s*(%b{})%s*;%s*")
     660                b,e,name,base,body = strfind(s,"^%s*class%s*([_%w][_%w@]*)%s*([^{]-)%s*(%b{})%s*")
    653661                if not b then
    654                     b,e,name,base,body = strfind(s,"^%s*struct%s*([_%w][_%w@]*)%s*(.-)%s*(%b{})%s*;%s*")
     662                    b,e,name,base,body = strfind(s,"^%s*struct%s+([_%w][_%w@]*)%s*([^{]-)%s*(%b{})%s*")
    655663                    if not b then
    656                         b,e,name,base,body = strfind(s,"^%s*union%s*([_%w][_%w@]*)%s*(.-)%s*(%b{})%s*;%s*")
     664                        b,e,name,base,body = strfind(s,"^%s*union%s*([_%w][_%w@]*)%s*([^{]-)%s*(%b{})%s*")
    657665                        if not b then
    658666                            base = ''
    659                             b,e,body,name = strfind(s,"^%s*typedef%s%s*struct%s%s*[_%w]*%s*(%b{})%s*([_%w][_%w@]*)%s*;%s*")
     667                            b,e,body,name = strfind(s,"^%s*typedef%s%s*struct%s%s*[_%w]*%s*(%b{})%s*([_%w][_%w@]*)%s*;")
    660668                        end
    661669                    end
    662670                end
    663             end
    664         end
     671            else dummy = 1 end
     672        else dummy = 1 end
    665673        if b then
    666674            if base ~= '' then
     
    675683            _curr_code = strsub(s,b,e)
    676684            Class(name,base,body)
     685            if not dummy then
     686                varb,vare,varname = string.find(s, "^%s*([_%w]+)%s*;", e+1)
     687                if varb then
     688                    Variable(name.." "..varname)
     689                    e = vare
     690                end
     691            end
    677692            return strsub(s,e+1)
    678693        end
     
    723738    -- try array
    724739    do
    725         local b,e,decl = strfind(s,"^%s*([_%w][][_@%s%w%d%*&:]*[]_%w%d])%s*;%s*")
     740    local b,e,decl = strfind(s,"^%s*([_%w][][_@%s%w%d%*&:<>]*[]_%w%d])%s*;%s*")
    726741        if b then
    727742            _curr_code = strsub(s,b,e)
     
    743758function classContainer:parse (s)
    744759
    745     self.curr_member_access = nil
     760    --self.curr_member_access = nil
    746761
    747762    while s ~= '' do
  • code/branches/pch/src/tolua/lua/declaration.lua

    r2710 r3127  
    140140        for i=1, table.getn(m) do
    141141            m[i] = string.gsub(m[i],"%s*([%*&])", "%1")
    142             m[i] = findtype(m[i]) or m[i]
    143             m[i] = resolve_template_types(m[i])
     142            if not isbasic(m[i]) then
     143                if not isenum(m[i]) then _, m[i] = applytypedef("", m[i]) end
     144                m[i] = findtype(m[i]) or m[i]
     145                m[i] = resolve_template_types(m[i])
     146            end
    144147        end
    145148
    146149        local b,i
    147150        type,b,i = break_template(type)
    148         local template_part = "<"..string.gsub(concat(m, 1, m.n), " ", ",")..">"
     151        --print("concat is ",concat(m, 1, m.n))
     152        local template_part = "<"..concat(m, 1, m.n, ",")..">"
    149153        type = rebuild_template(type, b, template_part)
    150154        type = string.gsub(type, ">>", "> >")
     
    187191-- check if array of values are returned to Lua
    188192function classDeclaration:requirecollection (t)
    189  if self.mod ~= 'const' and
     193    if self.mod ~= 'const' and
    190194        self.dim and self.dim ~= '' and
    191195                 not isbasic(self.type) and
    192                  self.ptr == '' then
     196                 self.ptr == '' and self:check_public_access() then
    193197        local type = gsub(self.type,"%s*const%s+","")
    194198        t[type] = "tolua_collect_" .. clean_template(type)
     
    222226        --    return 'tolua_isstringarray(tolua_S,'..narg..','..def..',&tolua_err)'
    223227        --else
    224         return 'tolua_istable(tolua_S,'..narg..',0,&tolua_err)'
     228        return '!tolua_istable(tolua_S,'..narg..',0,&tolua_err)'
    225229        --end
    226230    elseif t then
    227         return 'tolua_is'..t..'(tolua_S,'..narg..','..def..',&tolua_err)'
    228     else
    229         return 'tolua_isusertype(tolua_S,'..narg..',"'..self.type..'",'..def..',&tolua_err)'
    230     end
     231        return '!tolua_is'..t..'(tolua_S,'..narg..','..def..',&tolua_err)'
     232    else
     233        local is_func = get_is_function(self.type)
     234        if self.ptr == '&' or self.ptr == '' then
     235            return '(tolua_isvaluenil(tolua_S,'..narg..',&tolua_err) || !'..is_func..'(tolua_S,'..narg..',"'..self.type..'",'..def..',&tolua_err))'
     236        else
     237            return '!'..is_func..'(tolua_S,'..narg..',"'..self.type..'",'..def..',&tolua_err)'
     238        end
     239   end
    231240end
    232241
     
    237246    local mod
    238247    local type = self.type
     248    local nctype = gsub(self.type,'const%s+','')
    239249    if self.dim ~= '' then
    240250        type = gsub(self.type,'const%s+','')  -- eliminates const modifier for arrays
     
    251261        else
    252262            if cplusplus then
    253                 line = concatparam(line,' = new',type,ptr,'['..self.dim..'];')
     263                line = concatparam(line,' = Mtolua_new_dim(',type,ptr,', '..self.dim..');')
    254264            else
    255265                line = concatparam(line,' = (',type,ptr,'*)',
     
    273283            end
    274284            line = concatparam(line,') ')
    275             if isenum(type) then
     285            if isenum(nctype) then
    276286                line = concatparam(line,'(int) ')
    277287            end
     
    286296                line = concatparam(line,'tolua_to'..t,'(tolua_S,',narg,',',def,'));')
    287297            else
    288                 line = concatparam(line,'tolua_tousertype(tolua_S,',narg,',',def,'));')
     298                local to_func = get_to_function(type)
     299                line = concatparam(line,to_func..'(tolua_S,',narg,',',def,'));')
    289300            end
    290301        end
     
    361372                output('   {')
    362373                output('#ifdef __cplusplus\n')
    363                 output('    void* tolua_obj = new',type,'(',self.name,'[i]);')
     374                output('    void* tolua_obj = Mtolua_new((',type,')(',self.name,'[i]));')
    364375                output('    tolua_pushfieldusertype_and_takeownership(tolua_S,',narg,',i+1,tolua_obj,"',type,'");')
    365376                output('#else\n')
     
    380391    if self.dim ~= '' and tonumber(self.dim)==nil then
    381392        output('#ifdef __cplusplus\n')
    382         output('  delete []',self.name,';')
     393        output('  Mtolua_delete_dim(',self.name,');')
    383394        output('#else\n')
    384395        output('  free(',self.name,');')
     
    405416            output('   tolua_push'..t..'(tolua_S,(',ct,')'..self.name..');')
    406417        else
    407             output('   tolua_pushusertype(tolua_S,(void*)'..self.name..',"',self.type,'");')
     418            local push_func = get_push_function(self.type)
     419            output('   ',push_func,'(tolua_S,(void*)'..self.name..',"',self.type,'");')
    408420        end
    409421        return 1
  • code/branches/pch/src/tolua/lua/doit.lua

    r2710 r3127  
    1616    for k,v in ipairs(_extra_parameters or {}) do
    1717       
    18         local b,e,name,value = string.find(v, "^([^=])=(.*)$")
     18        local b,e,name,value = string.find(v, "^([^=]*)=(.*)$")
    1919        if b then
    2020            _extra_parameters[name] = value
     
    9494        p:print()
    9595    else
     96        push(p)
     97        pre_output_hook(p)
     98        pop()
    9699        p:preamble()
    97100        p:supcode()
     101        push(p)
     102        pre_register_hook(p)
     103        pop()
    98104        p:register()
    99105        push(p)
  • code/branches/pch/src/tolua/lua/enumerate.lua

    r2710 r3127  
    2222-- register enumeration
    2323function classEnumerate:register (pre)
     24    if not self:check_public_access() then
     25        return
     26    end
    2427    pre = pre or ''
    2528    local nspace = getnamespace(classContainer.curr)
    2629    local i=1
    2730    while self[i] do
    28         output(pre..'tolua_constant(tolua_S,"'..self.lnames[i]..'",'..nspace..self[i]..');')
     31        if self.lnames[i] and self.lnames[i] ~= "" then
     32            output(pre..'tolua_constant(tolua_S,"'..self.lnames[i]..'",'..nspace..self[i]..');')
     33        end
    2934        i = i+1
    3035    end
     
    5661            Variable("tolua_readonly int "..varname)
    5762        end
     63    end
     64    local parent = classContainer.curr
     65    if parent then
     66        t.access = parent.curr_member_access
     67        t.global_access = t:check_public_access()
    5868    end
    5969    return t
  • code/branches/pch/src/tolua/lua/feature.lua

    r2710 r3127  
    4848        local n = split(self.name,'@')
    4949        self.name = n[1]
     50        self.name = string.gsub(self.name, ":%d*$", "")
    5051        if not n[2] then
    5152            n[2] = applyrenaming(n[1])
    5253        end
    5354        self.lname = n[2] or gsub(n[1],"%[.-%]","")
     55        self.lname = string.gsub(self.lname, ":%d*$", "")
    5456        self.original_name = self.name
    5557        self.lname = clean_template(self.lname)
     
    6264    if parent then
    6365        self.access = parent.curr_member_access
     66        self.global_access = self:check_public_access()
    6467    else
    6568    end
     
    6770
    6871function classFeature:check_public_access()
     72
     73    if type(self.global_access) == "boolean" then
     74        return self.global_access
     75    end
    6976
    7077    if self.access and self.access ~= 0 then
     
    122129    end
    123130
    124     n = string.gsub(n..'_'.. (self.lname or self.name), "[<>:, \.%*&]", "_")
     131    local fname = self.lname
     132    if not fname or fname == '' then
     133        fname = self.name
     134    end
     135    n = string.gsub(n..'_'.. (fname), "[<>:, \.%*&]", "_")
    125136
    126137    return n
  • code/branches/pch/src/tolua/lua/function.lua

    r2710 r3127  
    209209    if class then narg=2 else narg=1 end
    210210    if class then
    211         local func = 'tolua_isusertype'
     211        local func = get_is_function(self.parent.type)
    212212        local type = self.parent.type
    213213        if self.name=='new' or static~=nil then
     
    226226            local btype = isbasic(self.args[i].type)
    227227            if btype ~= 'value' and btype ~= 'state' then
    228                 output('     !'..self.args[i]:outchecktype(narg)..' ||\n')
     228                output('     '..self.args[i]:outchecktype(narg)..' ||\n')
    229229            end
    230230            if btype ~= 'state' then
     
    250250        output(' ',self.const,self.parent.type,'*','self = ')
    251251        output('(',self.const,self.parent.type,'*) ')
    252         output('tolua_tousertype(tolua_S,1,0);')
     252        local to_func = get_to_function(self.parent.type)
     253        output(to_func,'(tolua_S,1,0);')
    253254    elseif static then
    254255        _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)')
     
    269270    if class and self.name~='new' and static==nil then
    270271        output('#ifndef TOLUA_RELEASE\n')
    271         output('  if (!self) tolua_error(tolua_S,"invalid \'self\' in function \''..self.name..'\'",NULL);');
     272        output('  if (!self) tolua_error(tolua_S,"'..output_error_hook("invalid \'self\' in function \'%s\'", self.name)..'", NULL);');
    272273        output('#endif\n')
    273274    end
     
    312313    --------------------------------------------------
    313314
     315    pre_call_hook(self)
     316
    314317    local out = string.find(self.mod, "tolua_outside")
    315318
     
    323326    -- call function
    324327    if class and self.name=='delete' then
    325         output('  delete self;')
     328        output('  Mtolua_delete(self);')
    326329    elseif class and self.name == 'operator&[]' then
    327330        if flags['1'] then -- for compatibility with tolua5 ?
     
    352355    end
    353356    if class and self.name=='new' then
    354         output('new',self.type,'(')
     357        output('Mtolua_new((',self.type,')(')
    355358    elseif class and static then
    356359        if out then
     
    364367        else
    365368            if self.cast_operator then
    366                 output('static_cast<',self.mod,self.type,self.ptr,'>(*self')
     369                --output('static_cast<',self.mod,self.type,self.ptr,' >(*self')
     370                output('self->operator ',self.mod,self.type,'(')
    367371            else
    368372                output('self->'..self.name,'(')
     
    392396        output('-1);')
    393397    else
    394         output(');')
     398        if class and self.name=='new' then
     399            output('));') -- close Mtolua_new(
     400        else
     401            output(');')
     402        end
    395403    end
    396404
     
    399407        nret = nret + 1
    400408        local t,ct = isbasic(self.type)
    401         if t then
     409        if t and self.name ~= "new" then
    402410            if self.cast_operator and _basic_raw_push[t] then
    403411                output('   ',_basic_raw_push[t],'(tolua_S,(',ct,')tolua_ret);')
     
    408416                t = self.type
    409417                new_t = string.gsub(t, "const%s+", "")
     418                local owned = false
     419                if string.find(self.mod, "tolua_owned") then
     420                    owned = true
     421                end
     422                local push_func = get_push_function(t)
    410423                if self.ptr == '' then
    411424                    output('   {')
    412425                    output('#ifdef __cplusplus\n')
    413                     output('    void* tolua_obj = new',new_t,'(tolua_ret);')
    414                     output('    tolua_pushusertype_and_takeownership(tolua_S,tolua_obj,"',t,'");')
     426                    output('    void* tolua_obj = Mtolua_new((',new_t,')(tolua_ret));')
     427                    output('    ',push_func,'(tolua_S,tolua_obj,"',t,'");')
     428                    output('    tolua_register_gc(tolua_S,lua_gettop(tolua_S));')
    415429                    output('#else\n')
    416430                    output('    void* tolua_obj = tolua_copy(tolua_S,(void*)&tolua_ret,sizeof(',t,'));')
    417                     output('    tolua_pushusertype_and_takeownership(tolua_S,tolua_obj,"',t,'");')
     431                    output('    ',push_func,'(tolua_S,tolua_obj,"',t,'");')
     432                    output('    tolua_register_gc(tolua_S,lua_gettop(tolua_S));')
    418433                    output('#endif\n')
    419434                    output('   }')
    420435                elseif self.ptr == '&' then
    421                     output('   tolua_pushusertype(tolua_S,(void*)&tolua_ret,"',t,'");')
     436                    output('   ',push_func,'(tolua_S,(void*)&tolua_ret,"',t,'");')
    422437                else
    423                     if local_constructor then
    424                         output('   tolua_pushusertype_and_takeownership(tolua_S,(void *)tolua_ret,"',t,'");')
    425                     else
    426                         output('   tolua_pushusertype(tolua_S,(void*)tolua_ret,"',t,'");')
     438                    output('   ',push_func,'(tolua_S,(void*)tolua_ret,"',t,'");')
     439                    if owned or local_constructor then
     440                        output('    tolua_register_gc(tolua_S,lua_gettop(tolua_S));')
    427441                    end
    428442                end
     
    466480    end
    467481
     482    post_call_hook(self)
     483
    468484    output(' }')
    469485    output(' return '..nret..';')
     
    474490        output('#ifndef TOLUA_RELEASE\n')
    475491        output('tolua_lerror:\n')
    476         output(' tolua_error(tolua_S,"#ferror in function \''..self.lname..'\'.",&tolua_err);')
     492        output(' tolua_error(tolua_S,"'..output_error_hook("#ferror in function \'%s\'.", self.lname)..'",&tolua_err);')
    477493        output(' return 0;')
    478494        output('#endif\n')
     
    574590    if string.find(par, "%*") then -- it's a pointer with a default value
    575591
    576         if string.find(par, '=%s*new') then -- it's a pointer with an instance as default parameter.. is that valid?
     592        if string.find(par, '=%s*new') or string.find(par, "%(") then -- it's a pointer with an instance as default parameter.. is that valid?
    577593            return true
    578594        end
     
    661677        --ns = strip_defaults(ns)
    662678
    663         Function(d, ns, c)
     679        local f = Function(d, ns, c)
    664680        for i=1,last do
    665681            t[i] = string.gsub(t[i], "=.*$", "")
  • code/branches/pch/src/tolua/lua/operator.lua

    r2710 r3127  
    7575    output(' if (\n')
    7676    -- check self
    77     output('     !'..'tolua_isusertype(tolua_S,1,"'..self.parent.type..'",0,&tolua_err) ||\n')
     77    local is_func = get_is_function(self.parent.type)
     78    output('     !'..is_func..'(tolua_S,1,"'..self.parent.type..'",0,&tolua_err) ||\n')
    7879    output('     !tolua_isnoobj(tolua_S,2,&tolua_err)\n )')
    7980    output('  goto tolua_lerror;')
     
    8687    output(' ',self.const,self.parent.type,'*','self = ')
    8788    output('(',self.const,self.parent.type,'*) ')
    88     output('tolua_tousertype(tolua_S,1,0);')
     89    local to_func = get_to_func(self.parent.type)
     90    output(to_func,'(tolua_S,1,0);')
    8991
    9092    -- check self
    9193    output('#ifndef TOLUA_RELEASE\n')
    92     output('  if (!self) tolua_error(tolua_S,"invalid \'self\' in function \''..self.name..'\'",NULL);');
     94    output('  if (!self) tolua_error(tolua_S,"'..output_error_hook("invalid \'self\' in function \'%s\'", self.name)..'",NULL);');
    9395    output('#endif\n')
    9496
     
    103105    else
    104106        t = self.type
     107        local push_func = get_push_function(t)
    105108        new_t = string.gsub(t, "const%s+", "")
    106109        if self.ptr == '' then
    107110            output('   {')
    108111            output('#ifdef __cplusplus\n')
    109             output('    void* tolua_obj = new',new_t,'(tolua_ret);')
    110             output('    tolua_pushusertype_and_takeownership(tolua_S,tolua_obj,"',t,'");')
     112            output('    void* tolua_obj = Mtolua_new((',new_t,')(tolua_ret));')
     113            output('    ',push_func,'(tolua_S,tolua_obj,"',t,'");')
     114            output('    tolua_register_gc(tolua_S,lua_gettop(tolua_S));')
    111115            output('#else\n')
    112116            output('    void* tolua_obj = tolua_copy(tolua_S,(void*)&tolua_ret,sizeof(',t,'));')
    113             output('    tolua_pushusertype_and_takeownership(tolua_S,tolua_obj,"',t,'");')
     117            output('    ',push_func,'(tolua_S,tolua_obj,"',t,'");')
     118            output('    tolua_register_gc(tolua_S,lua_gettop(tolua_S));')
    114119            output('#endif\n')
    115120            output('   }')
    116121        elseif self.ptr == '&' then
    117             output('   tolua_pushusertype(tolua_S,(void*)&tolua_ret,"',t,'");')
     122            output('   ',push_func,'(tolua_S,(void*)&tolua_ret,"',t,'");')
    118123        else
    119124            if local_constructor then
    120                 output('   tolua_pushusertype_and_takeownership(tolua_S,(void *)tolua_ret,"',t,'");')
     125                output('   ',push_func,'(tolua_S,(void *)tolua_ret,"',t,'");')
     126                output('    tolua_register_gc(tolua_S,lua_gettop(tolua_S));')
    121127            else
    122                 output('   tolua_pushusertype(tolua_S,(void*)tolua_ret,"',t,'");')
     128                output('   ',push_func,'(tolua_S,(void*)tolua_ret,"',t,'");')
    123129            end
    124130        end
     
    130136    output('#ifndef TOLUA_RELEASE\n')
    131137    output('tolua_lerror:\n')
    132     output(' tolua_error(tolua_S,"#ferror in function \''..self.lname..'\'.",&tolua_err);')
     138    output(' tolua_error(tolua_S,"'..output_error_hook("#ferror in function \'%s\'.", self.lname)..'",&tolua_err);')
    133139    output(' return 0;')
    134140    output('#endif\n')
  • code/branches/pch/src/tolua/lua/package.lua

    r2710 r3127  
    4242                                               return "\n#["..getn(L).."]#"
    4343                                           end
    44     )
    45     -- avoid preprocessing embedded C code
     44    )    -- avoid preprocessing embedded C code
    4645    local C = {}
    4746    self.code = gsub(self.code,"\n%s*%$%<","\3") -- deal with embedded C code
     
    5150                                               return "\n#<"..getn(C)..">#"
    5251                                           end
    53     )
    54     -- avoid preprocessing embedded C code
     52    )    -- avoid preprocessing embedded C code
    5553    self.code = gsub(self.code,"\n%s*%$%{","\5") -- deal with embedded C code
    5654    self.code = gsub(self.code,"\n%s*%$%}","\6")
     
    6058                                           end
    6159    )
    62 
    6360    --self.code = gsub(self.code,"\n%s*#[^d][^\n]*\n", "\n\n") -- eliminate preprocessor directives that don't start with 'd'
    6461    self.code = gsub(self.code,"\n[ \t]*#[ \t]*[^d%<%[]", "\n//") -- eliminate preprocessor directives that don't start with 'd'
     
    7168                                                         end
    7269    )
    73 
    7470    -- perform global substitution
    7571
     
    9490                                                    return L[tonumber(n)]
    9591                                                end
    96     )
    97     -- restore embedded C code
     92    )    -- restore embedded C code
    9893    self.code = gsub(self.code,"%#%<(%d+)%>%#", function (n)
    9994                                                    return C[tonumber(n)]
    10095                                                end
    101     )
    102     -- restore verbatim lines
     96    )    -- restore verbatim lines
    10397    self.code = gsub(self.code,"%#(%d+)%#", function (n)
    10498                                                return V[tonumber(n)]
    10599                                            end
    106100    )
    107 
    108101    self.code = string.gsub(self.code, "\n%s*%$([^\n]+)", function (l)
    109102                                                              Verbatim(l.."\n")
    110103                                                              return "\n"
    111104                                                          end
    112     )
    113 end
    114 
     105    )end
    115106-- translate verbatim
    116107function classPackage:preamble ()
     
    146137            output('{')
    147138            output(' '..i..'* self = ('..i..'*) tolua_tousertype(tolua_S,1,0);')
    148             output('    delete self;')
     139            output('    Mtolua_delete(self);')
    149140            output('    return 0;')
    150141            output('}')
     
    157148    output('static void tolua_reg_types (lua_State* tolua_S)')
    158149    output('{')
    159     foreach(_usertype,function(n,v) output(' tolua_usertype(tolua_S,"',v,'");') end)
     150
    160151    if flags.t then
    161152        output("#ifndef Mtolua_typeid\n#define Mtolua_typeid(L,TI,T)\n#endif\n")
    162         foreach(_usertype,function(n,v) output(' Mtolua_typeid(tolua_S,typeid(',v,'), "',v,'");') end)
    163     end
     153    end
     154    foreach(_usertype,function(n,v)
     155        if (not _global_classes[v]) or _global_classes[v]:check_public_access() then
     156            output(' tolua_usertype(tolua_S,"',v,'");')
     157            if flags.t then
     158                output(' Mtolua_typeid(tolua_S,typeid(',v,'), "',v,'");')
     159            end
     160        end
     161    end)
    164162    output('}')
    165163    output('\n')
     
    191189    output(pre.."int luaopen_"..self.name.." (lua_State* tolua_S) {")
    192190    output(pre.." return tolua_"..self.name.."_open(tolua_S);")
    193     output(pre.."};")
     191    output(pre.."}")
    194192    output("#endif\n\n")
    195193
     
    204202
    205203    if flags.H then
    206         local package_lower = string.lower(self.name)
    207         output('#include "'..package_lower..'/'..self.name..'Prereqs.h"\n')
    208         output('/* Exported function */')
     204        local package_lower = string.lower(self.name)        output('#include "'..package_lower..'/'..self.name..'Prereqs.h"\n')        output('/* Exported function */')
    209205        output('_'..self.name..'Export')
    210         output('int  tolua_'..self.name..'_open (lua_State* tolua_S);')
    211         output('\n')
     206        output('int  tolua_'..self.name..'_open (lua_State* tolua_S);')        output('\n')
    212207    end
    213208end
     
    224219    local code = '\n$#include "'..string.lower(flags.n)..'/'..fn..'"\n'
    225220    s= "\n" .. s .. "\n" -- add blank lines as sentinels
    226 
    227     -- eliminate export macro problems in class declarations
    228     s = gsub(s, ' _%w*Export ', ' ')
    229 
    230     local _,e,c,t = strfind(s, "\n([^\n]-)[Tt][Oo][Ll][Uu][Aa]_([^%s]*)[^\n]*\n")
     221    -- 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")
    231222    while e do
    232223        t = strlower(t)
     
    249240
    250241    -- open input file, if any
     242    local st,msg
    251243    if fn then
    252244        local file
    253         if flags.f then
    254             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
    255                 file = flags.f
    256             else
    257                 file = flags.w..'/'..flags.f
    258             end
    259         else
    260             file = flags.f
    261         end
    262         local st, msg = readfrom(file)
    263         if not st then
     245        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
    264246            error('#'..msg..' path: '..flags.f)
    265247        end
    266248        local _; _, _, ext = strfind(fn,".*%.(.*)$")
    267249    end
    268     local code = "\n" .. read('*a')
    269     if ext == 'h' or ext == 'hpp' then
    270         code = extract_code(fn,code)
     250    local code
     251    if ext == 'pkg' then
     252        code = prep(st)
     253    else
     254        code = "\n" .. read('*a')
     255        if ext == 'h' or ext == 'hpp' then
     256            code = extract_code(fn,code)
     257        end
    271258    end
    272259
     
    276263    end
    277264
    278     -- prepare working directory
    279     local current_path
    280     if not flags.w and flags.f then
    281         current_path = gsub(flags.f, '(/)[^/]*%.?[^/]*$', '%1')
    282     elseif flags.w then
    283         if not (string.sub(flags.w, string.len(flags.w)) == '/') then
    284             current_path = flags.w..'/'
    285         else
    286             current_path = flags.w
    287         end
    288     else
    289         current_path = ''
    290     end
    291 
    292     -- deal with include directive
     265    -- 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
    293266    local nsubst
    294267    repeat
     
    300273                    error('#'..msg..': '..fn)
    301274                end
     275                if kind == 'p' then
     276                    local s = prep(fp)
     277                    closefile(fp)
     278                    return s
     279                end
    302280                local s = read(fp,'*a')
    303281                closefile(fp)
    304282                if kind == 'c' or kind == 'h' then
    305283                    return extract_code(fn,s)
    306                 elseif kind == 'p' then
    307                     return "\n\n" .. s
    308284                elseif kind == 'l' then
    309285                    return "\n$[--##"..fn.."\n" .. s .. "\n$]\n"
     
    318294                end
    319295            end
    320         )
    321     until nsubst==0
     296        )    until nsubst==0
    322297
    323298    -- deal with renaming directive
     
    337312
    338313
     314setmetatable(_extra_parameters, { __index = _G })
     315
     316function prep(file)
     317
     318    local chunk = {'local __ret = {"\\n"}\n'}
     319    for line in file:lines() do
     320       if string.find(line, "^##") then
     321           table.insert(chunk, string.sub(line, 3) .. "\n")
     322       else
     323           local last = 1
     324           for text, expr, index in string.gfind(line, "(.-)$(%b())()") do
     325               last = index
     326               if text ~= "" then
     327                   table.insert(chunk, string.format('table.insert(__ret, %q )', text))
     328               end
     329               table.insert(chunk, string.format('table.insert(__ret, %s )', expr))
     330           end
     331           table.insert(chunk, string.format('table.insert(__ret, %q)\n',
     332                               string.sub(line, last).."\n"))
     333       end
     334    end
     335    table.insert(chunk, '\nreturn table.concat(__ret)\n')
     336    local f,e = loadstring(table.concat(chunk))
     337    if e then
     338        error("#"..e)
     339    end
     340    setfenv(f, _extra_parameters)
     341    return f()
     342end
  • code/branches/pch/src/tolua/lua/variable.lua

    r2710 r3127  
    109109    if string.find(self.mod, 'tolua_property') then
    110110
    111         _,_,type = string.find(self.mod, "tolua_property__([^%s]*)")
     111        local _,_,type = string.find(self.mod, "tolua_property__([^%s]*)")
    112112        type = type or "default"
    113113        prop_get,prop_set = get_property_methods(type, self.name)
     
    131131        output(' ',self.parent.type,'*','self = ')
    132132        output('(',self.parent.type,'*) ')
    133         output('tolua_tousertype(tolua_S,1,0);')
     133        local to_func = get_to_function(self.parent.type)
     134        output(to_func,'(tolua_S,1,0);')
    134135    elseif static then
    135136        _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)')
     
    140141    if class and static==nil then
    141142        output('#ifndef TOLUA_RELEASE\n')
    142         output('  if (!self) tolua_error(tolua_S,"invalid \'self\' in accessing variable \''..self.name..'\'",NULL);');
     143        output('  if (!self) tolua_error(tolua_S,"'..output_error_hook("invalid \'self\' in accessing variable \'%s\'", self.name)..'",NULL);');
    143144        output('#endif\n')
    144145    end
     
    146147    -- return value
    147148    if string.find(self.mod, 'tolua_inherits') then
     149    local push_func = get_push_function(self.type)
    148150        output('#ifdef __cplusplus\n')
    149         output('  tolua_pushusertype(tolua_S,(void*)static_cast<'..self.type..'*>(self), "',self.type,'");')
     151        output('  ',push_func,'(tolua_S,(void*)static_cast<'..self.type..'*>(self), "',self.type,'");')
    150152        output('#else\n')
    151         output('  tolua_pushusertype(tolua_S,(void*)(('..self.type..'*)self), "',self.type,'");')
     153        output('  ',push_func,'(tolua_S,(void*)(('..self.type..'*)self), "',self.type,'");')
    152154        output('#endif\n')
    153155    else
     
    156158            output('  tolua_push'..t..'(tolua_S,(',ct,')'..self:getvalue(class,static,prop_get)..');')
    157159        else
     160            local push_func = get_push_function(self.type)
    158161            t = self.type
    159162            if self.ptr == '&' or self.ptr == '' then
    160                 output('  tolua_pushusertype(tolua_S,(void*)&'..self:getvalue(class,static,prop_get)..',"',t,'");')
    161             else
    162                 output('  tolua_pushusertype(tolua_S,(void*)'..self:getvalue(class,static,prop_get)..',"',t,'");')
     163                output('  ',push_func,'(tolua_S,(void*)&'..self:getvalue(class,static,prop_get)..',"',t,'");')
     164            else
     165                output('  ',push_func,'(tolua_S,(void*)'..self:getvalue(class,static,prop_get)..',"',t,'");')
    163166            end
    164167        end
     
    185188        output(' ',self.parent.type,'*','self = ')
    186189        output('(',self.parent.type,'*) ')
    187         output('tolua_tousertype(tolua_S,1,0);')
     190        local to_func = get_to_function(self.parent.type)
     191        output(to_func,'(tolua_S,1,0);')
    188192        -- check self value
    189193    end
     
    192196    output('  tolua_Error tolua_err;')
    193197    if class and static==nil then
    194         output('  if (!self) tolua_error(tolua_S,"invalid \'self\' in accessing variable \''..self.name..'\'",NULL);');
     198        output('  if (!self) tolua_error(tolua_S,"'..output_error_hook("invalid \'self\' in accessing variable \'%s\'", self.name)..'",NULL);');
    195199    elseif static then
    196200        _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)')
     
    198202
    199203    -- check variable type
    200     output('  if (!'..self:outchecktype(2)..')')
     204    output('  if ('..self:outchecktype(2)..')')
    201205    output('   tolua_error(tolua_S,"#vinvalid type in variable assignment.",&tolua_err);')
    202206    output('#endif\n')
     
    206210    if self.def ~= '' then def = self.def end
    207211    if self.type == 'char*' and self.dim ~= '' then -- is string
    208         output(' strncpy(')
     212        output(' strncpy((char*)')
    209213        if class and static then
    210214            output(self.parent.type..'::'..self.name)
     
    214218                output(self.name)
    215219            end
    216             output(',tolua_tostring(tolua_S,2,',def,'),',self.dim,'-1);')
     220            output(',(const char*)tolua_tostring(tolua_S,2,',def,'),',self.dim,'-1);')
    217221        else
    218222            local ptr = ''
     
    245249                output('tolua_to'..t,'(tolua_S,2,',def,'))')
    246250            else
    247                 output('tolua_tousertype(tolua_S,2,',def,'))')
     251                local to_func = get_to_function(self.type)
     252                output(to_func,'(tolua_S,2,',def,'))')
    248253            end
    249254            if prop_set then
Note: See TracChangeset for help on using the changeset viewer.