Changeset 3127 for code/branches/pch/src/tolua/lua
- Timestamp:
- Jun 9, 2009, 4:29:42 PM (16 years ago)
- 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 102 102 -- return value 103 103 local t,ct = isbasic(self.type) 104 local push_func = get_push_function(t) 104 105 if t then 105 106 output(' tolua_push'..t..'(tolua_S,(',ct,')'..self:getvalue(class,static)..');') … … 107 108 t = self.type 108 109 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,'");') 112 113 end 113 114 end … … 193 194 output('tolua_to'..t,'(tolua_S,3,',def,'));') 194 195 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,'));') 196 198 end 197 199 output(' return 0;') … … 203 205 204 206 function classArray:register (pre) 207 if not self:check_public_access() then 208 return 209 end 210 205 211 pre = pre or '' 206 212 if self.csetname then -
code/branches/pch/src/tolua/lua/basic.lua
r2710 r3127 112 112 113 113 function warning (msg) 114 if flags.q then 115 return 116 end 114 117 local out = _OUTPUT 115 118 _OUTPUT = _STDERR … … 333 336 end 334 337 338 -- called before starting output 339 function pre_output_hook(package) 340 341 end 335 342 336 343 -- called after writing all the output. … … 354 361 end 355 362 356 363 -- called from classFunction:supcode, before the call to the function is output 364 function pre_call_hook(f) 365 366 end 367 368 -- called from classFunction:supcode, after the call to the function is output 369 function post_call_hook(f) 370 371 end 372 373 -- called before the register code is output 374 function pre_register_hook(package) 375 376 end 377 378 -- called to output an error message 379 function output_error_hook(...) 380 return string.format(...) 381 end 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 393 local 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 403 end 404 405 function get_push_function(t) 406 return _push_functions[t] or search_base(t, _base_push_functions) or "tolua_pushusertype" 407 end 408 409 function get_to_function(t) 410 return _to_functions[t] or search_base(t, _base_to_functions) or "tolua_tousertype" 411 end 412 413 function get_is_function(t) 414 return _is_functions[t] or search_base(t, _base_is_functions) or "tolua_isusertype" 415 end 416 -
code/branches/pch/src/tolua/lua/class.lua
r2710 r3127 64 64 -- return collection requirement 65 65 function classClass:requirecollection (t) 66 if self.flags.protected_destructor then66 if self.flags.protected_destructor or (not self:check_public_access()) then 67 67 return false 68 68 end … … 148 148 -- check for template 149 149 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*%)+') 151 151 if t then 152 152 … … 159 159 local bs = string.gsub(b, "^{%s*TOLUA_TEMPLATE_BIND[^\n]*\n", "{\n") 160 160 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 188 168 return 189 169 end -
code/branches/pch/src/tolua/lua/code.lua
r2710 r3127 47 47 end 48 48 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 49 57 -- convert to C 50 58 output('\n'..pre..'{ /* begin embedded lua code */\n') 51 59 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 ') 53 61 local t={n=0} 54 62 local b = gsub(s, '(.)', -
code/branches/pch/src/tolua/lua/compat-5.1.lua
r2710 r3127 26 26 27 27 local f = load(getfile, path) 28 if not f then 29 error("error loading file "..path) 30 end 28 31 return f() 29 32 end -
code/branches/pch/src/tolua/lua/container.lua
r2710 r3127 615 615 b,e,decl,arg,const = strfind(s,"^%s*([_%w])%s*(%b())%s*(c?o?n?s?t?)%s*;%s*") 616 616 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 617 624 if b then 618 625 if virt and string.find(virt, "[=0]") then … … 647 654 base = '' body = '' 648 655 b,e,name = strfind(s,"^%s*class%s*([_%w][_%w@]*)%s*;") -- dummy class 656 local dummy = false 649 657 if not b then 650 658 b,e,name = strfind(s,"^%s*struct%s*([_%w][_%w@]*)%s*;") -- dummy struct 651 659 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*") 653 661 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*") 655 663 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*") 657 665 if not b then 658 666 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*;") 660 668 end 661 669 end 662 670 end 663 e nd664 e nd671 else dummy = 1 end 672 else dummy = 1 end 665 673 if b then 666 674 if base ~= '' then … … 675 683 _curr_code = strsub(s,b,e) 676 684 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 677 692 return strsub(s,e+1) 678 693 end … … 723 738 -- try array 724 739 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*") 726 741 if b then 727 742 _curr_code = strsub(s,b,e) … … 743 758 function classContainer:parse (s) 744 759 745 self.curr_member_access = nil760 --self.curr_member_access = nil 746 761 747 762 while s ~= '' do -
code/branches/pch/src/tolua/lua/declaration.lua
r2710 r3127 140 140 for i=1, table.getn(m) do 141 141 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 144 147 end 145 148 146 149 local b,i 147 150 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, ",")..">" 149 153 type = rebuild_template(type, b, template_part) 150 154 type = string.gsub(type, ">>", "> >") … … 187 191 -- check if array of values are returned to Lua 188 192 function classDeclaration:requirecollection (t) 189 if self.mod ~= 'const' and193 if self.mod ~= 'const' and 190 194 self.dim and self.dim ~= '' and 191 195 not isbasic(self.type) and 192 self.ptr == '' then196 self.ptr == '' and self:check_public_access() then 193 197 local type = gsub(self.type,"%s*const%s+","") 194 198 t[type] = "tolua_collect_" .. clean_template(type) … … 222 226 -- return 'tolua_isstringarray(tolua_S,'..narg..','..def..',&tolua_err)' 223 227 --else 224 return ' tolua_istable(tolua_S,'..narg..',0,&tolua_err)'228 return '!tolua_istable(tolua_S,'..narg..',0,&tolua_err)' 225 229 --end 226 230 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 231 240 end 232 241 … … 237 246 local mod 238 247 local type = self.type 248 local nctype = gsub(self.type,'const%s+','') 239 249 if self.dim ~= '' then 240 250 type = gsub(self.type,'const%s+','') -- eliminates const modifier for arrays … … 251 261 else 252 262 if cplusplus then 253 line = concatparam(line,' = new',type,ptr,'['..self.dim..'];')263 line = concatparam(line,' = Mtolua_new_dim(',type,ptr,', '..self.dim..');') 254 264 else 255 265 line = concatparam(line,' = (',type,ptr,'*)', … … 273 283 end 274 284 line = concatparam(line,') ') 275 if isenum( type) then285 if isenum(nctype) then 276 286 line = concatparam(line,'(int) ') 277 287 end … … 286 296 line = concatparam(line,'tolua_to'..t,'(tolua_S,',narg,',',def,'));') 287 297 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,'));') 289 300 end 290 301 end … … 361 372 output(' {') 362 373 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]));') 364 375 output(' tolua_pushfieldusertype_and_takeownership(tolua_S,',narg,',i+1,tolua_obj,"',type,'");') 365 376 output('#else\n') … … 380 391 if self.dim ~= '' and tonumber(self.dim)==nil then 381 392 output('#ifdef __cplusplus\n') 382 output(' delete []',self.name,';')393 output(' Mtolua_delete_dim(',self.name,');') 383 394 output('#else\n') 384 395 output(' free(',self.name,');') … … 405 416 output(' tolua_push'..t..'(tolua_S,(',ct,')'..self.name..');') 406 417 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,'");') 408 420 end 409 421 return 1 -
code/branches/pch/src/tolua/lua/doit.lua
r2710 r3127 16 16 for k,v in ipairs(_extra_parameters or {}) do 17 17 18 local b,e,name,value = string.find(v, "^([^=] )=(.*)$")18 local b,e,name,value = string.find(v, "^([^=]*)=(.*)$") 19 19 if b then 20 20 _extra_parameters[name] = value … … 94 94 p:print() 95 95 else 96 push(p) 97 pre_output_hook(p) 98 pop() 96 99 p:preamble() 97 100 p:supcode() 101 push(p) 102 pre_register_hook(p) 103 pop() 98 104 p:register() 99 105 push(p) -
code/branches/pch/src/tolua/lua/enumerate.lua
r2710 r3127 22 22 -- register enumeration 23 23 function classEnumerate:register (pre) 24 if not self:check_public_access() then 25 return 26 end 24 27 pre = pre or '' 25 28 local nspace = getnamespace(classContainer.curr) 26 29 local i=1 27 30 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 29 34 i = i+1 30 35 end … … 56 61 Variable("tolua_readonly int "..varname) 57 62 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() 58 68 end 59 69 return t -
code/branches/pch/src/tolua/lua/feature.lua
r2710 r3127 48 48 local n = split(self.name,'@') 49 49 self.name = n[1] 50 self.name = string.gsub(self.name, ":%d*$", "") 50 51 if not n[2] then 51 52 n[2] = applyrenaming(n[1]) 52 53 end 53 54 self.lname = n[2] or gsub(n[1],"%[.-%]","") 55 self.lname = string.gsub(self.lname, ":%d*$", "") 54 56 self.original_name = self.name 55 57 self.lname = clean_template(self.lname) … … 62 64 if parent then 63 65 self.access = parent.curr_member_access 66 self.global_access = self:check_public_access() 64 67 else 65 68 end … … 67 70 68 71 function classFeature:check_public_access() 72 73 if type(self.global_access) == "boolean" then 74 return self.global_access 75 end 69 76 70 77 if self.access and self.access ~= 0 then … … 122 129 end 123 130 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), "[<>:, \.%*&]", "_") 125 136 126 137 return n -
code/branches/pch/src/tolua/lua/function.lua
r2710 r3127 209 209 if class then narg=2 else narg=1 end 210 210 if class then 211 local func = 'tolua_isusertype'211 local func = get_is_function(self.parent.type) 212 212 local type = self.parent.type 213 213 if self.name=='new' or static~=nil then … … 226 226 local btype = isbasic(self.args[i].type) 227 227 if btype ~= 'value' and btype ~= 'state' then 228 output(' !'..self.args[i]:outchecktype(narg)..' ||\n')228 output(' '..self.args[i]:outchecktype(narg)..' ||\n') 229 229 end 230 230 if btype ~= 'state' then … … 250 250 output(' ',self.const,self.parent.type,'*','self = ') 251 251 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);') 253 254 elseif static then 254 255 _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)') … … 269 270 if class and self.name~='new' and static==nil then 270 271 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);'); 272 273 output('#endif\n') 273 274 end … … 312 313 -------------------------------------------------- 313 314 315 pre_call_hook(self) 316 314 317 local out = string.find(self.mod, "tolua_outside") 315 318 … … 323 326 -- call function 324 327 if class and self.name=='delete' then 325 output(' delete self;')328 output(' Mtolua_delete(self);') 326 329 elseif class and self.name == 'operator&[]' then 327 330 if flags['1'] then -- for compatibility with tolua5 ? … … 352 355 end 353 356 if class and self.name=='new' then 354 output(' new',self.type,'(')357 output('Mtolua_new((',self.type,')(') 355 358 elseif class and static then 356 359 if out then … … 364 367 else 365 368 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,'(') 367 371 else 368 372 output('self->'..self.name,'(') … … 392 396 output('-1);') 393 397 else 394 output(');') 398 if class and self.name=='new' then 399 output('));') -- close Mtolua_new( 400 else 401 output(');') 402 end 395 403 end 396 404 … … 399 407 nret = nret + 1 400 408 local t,ct = isbasic(self.type) 401 if t then409 if t and self.name ~= "new" then 402 410 if self.cast_operator and _basic_raw_push[t] then 403 411 output(' ',_basic_raw_push[t],'(tolua_S,(',ct,')tolua_ret);') … … 408 416 t = self.type 409 417 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) 410 423 if self.ptr == '' then 411 424 output(' {') 412 425 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));') 415 429 output('#else\n') 416 430 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));') 418 433 output('#endif\n') 419 434 output(' }') 420 435 elseif self.ptr == '&' then 421 output(' tolua_pushusertype(tolua_S,(void*)&tolua_ret,"',t,'");')436 output(' ',push_func,'(tolua_S,(void*)&tolua_ret,"',t,'");') 422 437 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));') 427 441 end 428 442 end … … 466 480 end 467 481 482 post_call_hook(self) 483 468 484 output(' }') 469 485 output(' return '..nret..';') … … 474 490 output('#ifndef TOLUA_RELEASE\n') 475 491 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);') 477 493 output(' return 0;') 478 494 output('#endif\n') … … 574 590 if string.find(par, "%*") then -- it's a pointer with a default value 575 591 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? 577 593 return true 578 594 end … … 661 677 --ns = strip_defaults(ns) 662 678 663 Function(d, ns, c)679 local f = Function(d, ns, c) 664 680 for i=1,last do 665 681 t[i] = string.gsub(t[i], "=.*$", "") -
code/branches/pch/src/tolua/lua/operator.lua
r2710 r3127 75 75 output(' if (\n') 76 76 -- 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') 78 79 output(' !tolua_isnoobj(tolua_S,2,&tolua_err)\n )') 79 80 output(' goto tolua_lerror;') … … 86 87 output(' ',self.const,self.parent.type,'*','self = ') 87 88 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);') 89 91 90 92 -- check self 91 93 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);'); 93 95 output('#endif\n') 94 96 … … 103 105 else 104 106 t = self.type 107 local push_func = get_push_function(t) 105 108 new_t = string.gsub(t, "const%s+", "") 106 109 if self.ptr == '' then 107 110 output(' {') 108 111 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));') 111 115 output('#else\n') 112 116 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));') 114 119 output('#endif\n') 115 120 output(' }') 116 121 elseif self.ptr == '&' then 117 output(' tolua_pushusertype(tolua_S,(void*)&tolua_ret,"',t,'");')122 output(' ',push_func,'(tolua_S,(void*)&tolua_ret,"',t,'");') 118 123 else 119 124 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));') 121 127 else 122 output(' tolua_pushusertype(tolua_S,(void*)tolua_ret,"',t,'");')128 output(' ',push_func,'(tolua_S,(void*)tolua_ret,"',t,'");') 123 129 end 124 130 end … … 130 136 output('#ifndef TOLUA_RELEASE\n') 131 137 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);') 133 139 output(' return 0;') 134 140 output('#endif\n') -
code/branches/pch/src/tolua/lua/package.lua
r2710 r3127 42 42 return "\n#["..getn(L).."]#" 43 43 end 44 ) 45 -- avoid preprocessing embedded C code 44 ) -- avoid preprocessing embedded C code 46 45 local C = {} 47 46 self.code = gsub(self.code,"\n%s*%$%<","\3") -- deal with embedded C code … … 51 50 return "\n#<"..getn(C)..">#" 52 51 end 53 ) 54 -- avoid preprocessing embedded C code 52 ) -- avoid preprocessing embedded C code 55 53 self.code = gsub(self.code,"\n%s*%$%{","\5") -- deal with embedded C code 56 54 self.code = gsub(self.code,"\n%s*%$%}","\6") … … 60 58 end 61 59 ) 62 63 60 --self.code = gsub(self.code,"\n%s*#[^d][^\n]*\n", "\n\n") -- eliminate preprocessor directives that don't start with 'd' 64 61 self.code = gsub(self.code,"\n[ \t]*#[ \t]*[^d%<%[]", "\n//") -- eliminate preprocessor directives that don't start with 'd' … … 71 68 end 72 69 ) 73 74 70 -- perform global substitution 75 71 … … 94 90 return L[tonumber(n)] 95 91 end 96 ) 97 -- restore embedded C code 92 ) -- restore embedded C code 98 93 self.code = gsub(self.code,"%#%<(%d+)%>%#", function (n) 99 94 return C[tonumber(n)] 100 95 end 101 ) 102 -- restore verbatim lines 96 ) -- restore verbatim lines 103 97 self.code = gsub(self.code,"%#(%d+)%#", function (n) 104 98 return V[tonumber(n)] 105 99 end 106 100 ) 107 108 101 self.code = string.gsub(self.code, "\n%s*%$([^\n]+)", function (l) 109 102 Verbatim(l.."\n") 110 103 return "\n" 111 104 end 112 ) 113 end 114 105 )end 115 106 -- translate verbatim 116 107 function classPackage:preamble () … … 146 137 output('{') 147 138 output(' '..i..'* self = ('..i..'*) tolua_tousertype(tolua_S,1,0);') 148 output(' delete self;')139 output(' Mtolua_delete(self);') 149 140 output(' return 0;') 150 141 output('}') … … 157 148 output('static void tolua_reg_types (lua_State* tolua_S)') 158 149 output('{') 159 foreach(_usertype,function(n,v) output(' tolua_usertype(tolua_S,"',v,'");') end) 150 160 151 if flags.t then 161 152 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) 164 162 output('}') 165 163 output('\n') … … 191 189 output(pre.."int luaopen_"..self.name.." (lua_State* tolua_S) {") 192 190 output(pre.." return tolua_"..self.name.."_open(tolua_S);") 193 output(pre.."} ;")191 output(pre.."}") 194 192 output("#endif\n\n") 195 193 … … 204 202 205 203 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 */') 209 205 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') 212 207 end 213 208 end … … 224 219 local code = '\n$#include "'..string.lower(flags.n)..'/'..fn..'"\n' 225 220 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") 231 222 while e do 232 223 t = strlower(t) … … 249 240 250 241 -- open input file, if any 242 local st,msg 251 243 if fn then 252 244 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 264 246 error('#'..msg..' path: '..flags.f) 265 247 end 266 248 local _; _, _, ext = strfind(fn,".*%.(.*)$") 267 249 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 271 258 end 272 259 … … 276 263 end 277 264 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 293 266 local nsubst 294 267 repeat … … 300 273 error('#'..msg..': '..fn) 301 274 end 275 if kind == 'p' then 276 local s = prep(fp) 277 closefile(fp) 278 return s 279 end 302 280 local s = read(fp,'*a') 303 281 closefile(fp) 304 282 if kind == 'c' or kind == 'h' then 305 283 return extract_code(fn,s) 306 elseif kind == 'p' then307 return "\n\n" .. s308 284 elseif kind == 'l' then 309 285 return "\n$[--##"..fn.."\n" .. s .. "\n$]\n" … … 318 294 end 319 295 end 320 ) 321 until nsubst==0 296 ) until nsubst==0 322 297 323 298 -- deal with renaming directive … … 337 312 338 313 314 setmetatable(_extra_parameters, { __index = _G }) 315 316 function 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() 342 end -
code/branches/pch/src/tolua/lua/variable.lua
r2710 r3127 109 109 if string.find(self.mod, 'tolua_property') then 110 110 111 _,_,type = string.find(self.mod, "tolua_property__([^%s]*)")111 local _,_,type = string.find(self.mod, "tolua_property__([^%s]*)") 112 112 type = type or "default" 113 113 prop_get,prop_set = get_property_methods(type, self.name) … … 131 131 output(' ',self.parent.type,'*','self = ') 132 132 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);') 134 135 elseif static then 135 136 _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)') … … 140 141 if class and static==nil then 141 142 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);'); 143 144 output('#endif\n') 144 145 end … … 146 147 -- return value 147 148 if string.find(self.mod, 'tolua_inherits') then 149 local push_func = get_push_function(self.type) 148 150 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,'");') 150 152 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,'");') 152 154 output('#endif\n') 153 155 else … … 156 158 output(' tolua_push'..t..'(tolua_S,(',ct,')'..self:getvalue(class,static,prop_get)..');') 157 159 else 160 local push_func = get_push_function(self.type) 158 161 t = self.type 159 162 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,'");') 163 166 end 164 167 end … … 185 188 output(' ',self.parent.type,'*','self = ') 186 189 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);') 188 192 -- check self value 189 193 end … … 192 196 output(' tolua_Error tolua_err;') 193 197 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);'); 195 199 elseif static then 196 200 _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)') … … 198 202 199 203 -- check variable type 200 output(' if ( !'..self:outchecktype(2)..')')204 output(' if ('..self:outchecktype(2)..')') 201 205 output(' tolua_error(tolua_S,"#vinvalid type in variable assignment.",&tolua_err);') 202 206 output('#endif\n') … … 206 210 if self.def ~= '' then def = self.def end 207 211 if self.type == 'char*' and self.dim ~= '' then -- is string 208 output(' strncpy( ')212 output(' strncpy((char*)') 209 213 if class and static then 210 214 output(self.parent.type..'::'..self.name) … … 214 218 output(self.name) 215 219 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);') 217 221 else 218 222 local ptr = '' … … 245 249 output('tolua_to'..t,'(tolua_S,2,',def,'))') 246 250 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,'))') 248 253 end 249 254 if prop_set then
Note: See TracChangeset
for help on using the changeset viewer.