| 1 |
-- tolua: function class |
| 2 |
-- Written by Waldemar Celes |
| 3 |
-- TeCGraf/PUC-Rio |
| 4 |
-- Jul 1998 |
| 5 |
-- $Id: $ |
| 6 |
|
| 7 |
-- This code is free software; you can redistribute it and/or modify it. |
| 8 |
-- The software provided hereunder is on an "as is" basis, and |
| 9 |
-- the author has no obligation to provide maintenance, support, updates, |
| 10 |
-- enhancements, or modifications. |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
-- Function class |
| 15 |
-- Represents a function or a class method. |
| 16 |
-- The following fields are stored: |
| 17 |
-- mod = type modifiers |
| 18 |
-- type = type |
| 19 |
-- ptr = "*" or "&", if representing a pointer or a reference |
| 20 |
-- name = name |
| 21 |
-- lname = lua name |
| 22 |
-- args = list of argument declarations |
| 23 |
-- const = if it is a method receiving a const "this". |
| 24 |
classFunction = { |
| 25 |
mod = '', |
| 26 |
type = '', |
| 27 |
ptr = '', |
| 28 |
name = '', |
| 29 |
args = {n=0}, |
| 30 |
const = '', |
| 31 |
} |
| 32 |
classFunction.__index = classFunction |
| 33 |
setmetatable(classFunction,classFeature) |
| 34 |
|
| 35 |
-- declare tags |
| 36 |
function classFunction:decltype () |
| 37 |
self.type = typevar(self.type) |
| 38 |
if strfind(self.mod,'const') then |
| 39 |
self.type = 'const '..self.type |
| 40 |
self.mod = gsub(self.mod,'const','') |
| 41 |
end |
| 42 |
local i=1 |
| 43 |
while self.args[i] do |
| 44 |
self.args[i]:decltype() |
| 45 |
i = i+1 |
| 46 |
end |
| 47 |
end |
| 48 |
|
| 49 |
|
| 50 |
-- Write binding function |
| 51 |
-- Outputs C/C++ binding function. |
| 52 |
function classFunction:supcode (local_constructor) |
| 53 |
|
| 54 |
local overload = strsub(self.cname,-2,-1) - 1 -- indicate overloaded func |
| 55 |
local nret = 0 -- number of returned values |
| 56 |
local class = self:inclass() |
| 57 |
local _,_,static = strfind(self.mod,'^%s*(static)') |
| 58 |
if class then |
| 59 |
|
| 60 |
if self.name == 'new' and self.parent.flags.pure_virtual then |
| 61 |
-- no constructor for classes with pure virtual methods |
| 62 |
return |
| 63 |
end |
| 64 |
|
| 65 |
if local_constructor then |
| 66 |
output("/* method: new_local of class ",class," */") |
| 67 |
else |
| 68 |
output("/* method:",self.name," of class ",class," */") |
| 69 |
end |
| 70 |
else |
| 71 |
output("/* function:",self.name," */") |
| 72 |
end |
| 73 |
|
| 74 |
if local_constructor then |
| 75 |
output("#ifndef TOLUA_DISABLE_"..self.cname.."_local") |
| 76 |
output("\nstatic int",self.cname.."_local","(lua_State* tolua_S)") |
| 77 |
else |
| 78 |
output("#ifndef TOLUA_DISABLE_"..self.cname) |
| 79 |
output("\nstatic int",self.cname,"(lua_State* tolua_S)") |
| 80 |
end |
| 81 |
output("{") |
| 82 |
|
| 83 |
-- check types |
| 84 |
if overload < 0 then |
| 85 |
output('#ifndef TOLUA_RELEASE\n') |
| 86 |
end |
| 87 |
output(' tolua_Error tolua_err;') |
| 88 |
output(' if (\n') |
| 89 |
-- check self |
| 90 |
local narg |
| 91 |
if class then narg=2 else narg=1 end |
| 92 |
if class then |
| 93 |
local func = get_is_function(self.parent.type) |
| 94 |
local type = self.parent.type |
| 95 |
if self.name=='new' or static~=nil then |
| 96 |
func = 'tolua_isusertable' |
| 97 |
type = self.parent.type |
| 98 |
end |
| 99 |
if self.const ~= '' then |
| 100 |
type = "const "..type |
| 101 |
end |
| 102 |
output(' !'..func..'(tolua_S,1,"'..type..'",0,&tolua_err) ||\n') |
| 103 |
end |
| 104 |
-- check args |
| 105 |
if self.args[1].type ~= 'void' then |
| 106 |
local i=1 |
| 107 |
while self.args[i] do |
| 108 |
local btype = isbasic(self.args[i].type) |
| 109 |
if btype ~= 'value' and btype ~= 'state' then |
| 110 |
output(' '..self.args[i]:outchecktype(narg)..' ||\n') |
| 111 |
end |
| 112 |
if btype ~= 'state' then |
| 113 |
narg = narg+1 |
| 114 |
end |
| 115 |
i = i+1 |
| 116 |
end |
| 117 |
end |
| 118 |
-- check end of list |
| 119 |
output(' !tolua_isnoobj(tolua_S,'..narg..',&tolua_err)\n )') |
| 120 |
output(' goto tolua_lerror;') |
| 121 |
|
| 122 |
output(' else\n') |
| 123 |
if overload < 0 then |
| 124 |
output('#endif\n') |
| 125 |
end |
| 126 |
output(' {') |
| 127 |
|
| 128 |
-- declare self, if the case |
| 129 |
local narg |
| 130 |
if class then narg=2 else narg=1 end |
| 131 |
if class and self.name~='new' and static==nil then |
| 132 |
output(' ',self.const,self.parent.type,'*','self = ') |
| 133 |
output('(',self.const,self.parent.type,'*) ') |
| 134 |
local to_func = get_to_function(self.parent.type) |
| 135 |
output(to_func,'(tolua_S,1,0);') |
| 136 |
elseif static then |
| 137 |
_,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)') |
| 138 |
end |
| 139 |
-- declare parameters |
| 140 |
if self.args[1].type ~= 'void' then |
| 141 |
local i=1 |
| 142 |
while self.args[i] do |
| 143 |
self.args[i]:declare(narg) |
| 144 |
if isbasic(self.args[i].type) ~= "state" then |
| 145 |
narg = narg+1 |
| 146 |
end |
| 147 |
i = i+1 |
| 148 |
end |
| 149 |
end |
| 150 |
|
| 151 |
-- check self |
| 152 |
if class and self.name~='new' and static==nil then |
| 153 |
output('#ifndef TOLUA_RELEASE\n') |
| 154 |
output(' if (!self) tolua_error(tolua_S,"'..output_error_hook("invalid \'self\' in function \'%s\'", self.name)..'", NULL);'); |
| 155 |
output('#endif\n') |
| 156 |
end |
| 157 |
|
| 158 |
-- get array element values |
| 159 |
if class then narg=2 else narg=1 end |
| 160 |
if self.args[1].type ~= 'void' then |
| 161 |
local i=1 |
| 162 |
while self.args[i] do |
| 163 |
self.args[i]:getarray(narg) |
| 164 |
narg = narg+1 |
| 165 |
i = i+1 |
| 166 |
end |
| 167 |
end |
| 168 |
|
| 169 |
pre_call_hook(self) |
| 170 |
|
| 171 |
local out = string.find(self.mod, "tolua_outside") |
| 172 |
-- call function |
| 173 |
if class and self.name=='delete' then |
| 174 |
output(' Mtolua_delete(self);') |
| 175 |
elseif class and self.name == 'operator&[]' then |
| 176 |
if flags['1'] then -- for compatibility with tolua5 ? |
| 177 |
output(' self->operator[](',self.args[1].name,'-1) = ',self.args[2].name,';') |
| 178 |
else |
| 179 |
output(' self->operator[](',self.args[1].name,') = ',self.args[2].name,';') |
| 180 |
end |
| 181 |
else |
| 182 |
output(' {') |
| 183 |
if self.type ~= '' and self.type ~= 'void' then |
| 184 |
output(' ',self.mod,self.type,self.ptr,'tolua_ret = ') |
| 185 |
output('(',self.mod,self.type,self.ptr,') ') |
| 186 |
else |
| 187 |
output(' ') |
| 188 |
end |
| 189 |
if class and self.name=='new' then |
| 190 |
output('Mtolua_new((',self.type,')(') |
| 191 |
elseif class and static then |
| 192 |
if out then |
| 193 |
output(self.name,'(') |
| 194 |
else |
| 195 |
output(class..'::'..self.name,'(') |
| 196 |
end |
| 197 |
elseif class then |
| 198 |
if out then |
| 199 |
output(self.name,'(') |
| 200 |
else |
| 201 |
if self.cast_operator then |
| 202 |
--output('static_cast<',self.mod,self.type,self.ptr,' >(*self') |
| 203 |
output('self->operator ',self.mod,self.type,'(') |
| 204 |
else |
| 205 |
output('self->'..self.name,'(') |
| 206 |
end |
| 207 |
end |
| 208 |
else |
| 209 |
output(self.name,'(') |
| 210 |
end |
| 211 |
|
| 212 |
if out and not static then |
| 213 |
output('self') |
| 214 |
if self.args[1] and self.args[1].name ~= '' then |
| 215 |
output(',') |
| 216 |
end |
| 217 |
end |
| 218 |
-- write parameters |
| 219 |
local i=1 |
| 220 |
while self.args[i] do |
| 221 |
self.args[i]:passpar() |
| 222 |
i = i+1 |
| 223 |
if self.args[i] then |
| 224 |
output(',') |
| 225 |
end |
| 226 |
end |
| 227 |
|
| 228 |
if class and self.name == 'operator[]' and flags['1'] then |
| 229 |
output('-1);') |
| 230 |
else |
| 231 |
if class and self.name=='new' then |
| 232 |
output('));') -- close Mtolua_new( |
| 233 |
else |
| 234 |
output(');') |
| 235 |
end |
| 236 |
end |
| 237 |
|
| 238 |
-- return values |
| 239 |
if self.type ~= '' and self.type ~= 'void' then |
| 240 |
nret = nret + 1 |
| 241 |
local t,ct = isbasic(self.type) |
| 242 |
if t and self.name ~= "new" then |
| 243 |
if self.cast_operator and _basic_raw_push[t] then |
| 244 |
output(' ',_basic_raw_push[t],'(tolua_S,(',ct,')tolua_ret);') |
| 245 |
else |
| 246 |
output(' tolua_push'..t..'(tolua_S,(',ct,')tolua_ret);') |
| 247 |
end |
| 248 |
else |
| 249 |
t = self.type |
| 250 |
new_t = string.gsub(t, "const%s+", "") |
| 251 |
local owned = false |
| 252 |
if string.find(self.mod, "tolua_owned") then |
| 253 |
owned = true |
| 254 |
end |
| 255 |
local push_func = get_push_function(t) |
| 256 |
if self.ptr == '' then |
| 257 |
output(' {') |
| 258 |
output('#ifdef __cplusplus\n') |
| 259 |
output(' void* tolua_obj = Mtolua_new((',new_t,')(tolua_ret));') |
| 260 |
output(' ',push_func,'(tolua_S,tolua_obj,"',t,'");') |
| 261 |
output(' tolua_register_gc(tolua_S,lua_gettop(tolua_S));') |
| 262 |
output('#else\n') |
| 263 |
output(' void* tolua_obj = tolua_copy(tolua_S,(void*)&tolua_ret,sizeof(',t,'));') |
| 264 |
output(' ',push_func,'(tolua_S,tolua_obj,"',t,'");') |
| 265 |
output(' tolua_register_gc(tolua_S,lua_gettop(tolua_S));') |
| 266 |
output('#endif\n') |
| 267 |
output(' }') |
| 268 |
elseif self.ptr == '&' then |
| 269 |
output(' ',push_func,'(tolua_S,(void*)&tolua_ret,"',t,'");') |
| 270 |
else |
| 271 |
output(' ',push_func,'(tolua_S,(void*)tolua_ret,"',t,'");') |
| 272 |
if owned or local_constructor then |
| 273 |
output(' tolua_register_gc(tolua_S,lua_gettop(tolua_S));') |
| 274 |
end |
| 275 |
end |
| 276 |
end |
| 277 |
end |
| 278 |
local i=1 |
| 279 |
while self.args[i] do |
| 280 |
nret = nret + self.args[i]:retvalue() |
| 281 |
i = i+1 |
| 282 |
end |
| 283 |
output(' }') |
| 284 |
|
| 285 |
-- set array element values |
| 286 |
if class then narg=2 else narg=1 end |
| 287 |
if self.args[1].type ~= 'void' then |
| 288 |
local i=1 |
| 289 |
while self.args[i] do |
| 290 |
self.args[i]:setarray(narg) |
| 291 |
narg = narg+1 |
| 292 |
i = i+1 |
| 293 |
end |
| 294 |
end |
| 295 |
|
| 296 |
-- free dynamically allocated array |
| 297 |
if self.args[1].type ~= 'void' then |
| 298 |
local i=1 |
| 299 |
while self.args[i] do |
| 300 |
self.args[i]:freearray() |
| 301 |
i = i+1 |
| 302 |
end |
| 303 |
end |
| 304 |
end |
| 305 |
|
| 306 |
post_call_hook(self) |
| 307 |
|
| 308 |
output(' }') |
| 309 |
output(' return '..nret..';') |
| 310 |
|
| 311 |
-- call overloaded function or generate error |
| 312 |
if overload < 0 then |
| 313 |
|
| 314 |
output('#ifndef TOLUA_RELEASE\n') |
| 315 |
output('tolua_lerror:\n') |
| 316 |
output(' tolua_error(tolua_S,"'..output_error_hook("#ferror in function \'%s\'.", self.lname)..'",&tolua_err);') |
| 317 |
output(' return 0;') |
| 318 |
output('#endif\n') |
| 319 |
else |
| 320 |
local _local = "" |
| 321 |
if local_constructor then |
| 322 |
_local = "_local" |
| 323 |
end |
| 324 |
output('tolua_lerror:\n') |
| 325 |
output(' return '..strsub(self.cname,1,-3)..format("%02d",overload).._local..'(tolua_S);') |
| 326 |
end |
| 327 |
output('}') |
| 328 |
output('#endif //#ifndef TOLUA_DISABLE\n') |
| 329 |
output('\n') |
| 330 |
|
| 331 |
-- recursive call to write local constructor |
| 332 |
if class and self.name=='new' and not local_constructor then |
| 333 |
|
| 334 |
self:supcode(1) |
| 335 |
end |
| 336 |
|
| 337 |
end |
| 338 |
|
| 339 |
|
| 340 |
-- register function |
| 341 |
function classFunction:register (pre) |
| 342 |
|
| 343 |
if not self:check_public_access() then |
| 344 |
return |
| 345 |
end |
| 346 |
|
| 347 |
if self.name == 'new' and self.parent.flags.pure_virtual then |
| 348 |
-- no constructor for classes with pure virtual methods |
| 349 |
return |
| 350 |
end |
| 351 |
|
| 352 |
output(pre..'tolua_function(tolua_S,"'..self.lname..'",'..self.cname..');') |
| 353 |
if self.name == 'new' then |
| 354 |
output(pre..'tolua_function(tolua_S,"new_local",'..self.cname..'_local);') |
| 355 |
output(pre..'tolua_function(tolua_S,".call",'..self.cname..'_local);') |
| 356 |
--output(' tolua_set_call_event(tolua_S,'..self.cname..'_local, "'..self.parent.type..'");') |
| 357 |
end |
| 358 |
end |
| 359 |
|
| 360 |
-- Print method |
| 361 |
function classFunction:print (ident,close) |
| 362 |
print(ident.."Function{") |
| 363 |
print(ident.." mod = '"..self.mod.."',") |
| 364 |
print(ident.." type = '"..self.type.."',") |
| 365 |
print(ident.." ptr = '"..self.ptr.."',") |
| 366 |
print(ident.." name = '"..self.name.."',") |
| 367 |
print(ident.." lname = '"..self.lname.."',") |
| 368 |
print(ident.." const = '"..self.const.."',") |
| 369 |
print(ident.." cname = '"..self.cname.."',") |
| 370 |
print(ident.." lname = '"..self.lname.."',") |
| 371 |
print(ident.." args = {") |
| 372 |
local i=1 |
| 373 |
while self.args[i] do |
| 374 |
self.args[i]:print(ident.." ",",") |
| 375 |
i = i+1 |
| 376 |
end |
| 377 |
print(ident.." }") |
| 378 |
print(ident.."}"..close) |
| 379 |
end |
| 380 |
|
| 381 |
-- check if it returns an object by value |
| 382 |
function classFunction:requirecollection (t) |
| 383 |
local r = false |
| 384 |
if self.type ~= '' and not isbasic(self.type) and self.ptr=='' then |
| 385 |
local type = gsub(self.type,"%s*const%s+","") |
| 386 |
t[type] = "tolua_collect_" .. clean_template(type) |
| 387 |
r = true |
| 388 |
end |
| 389 |
local i=1 |
| 390 |
while self.args[i] do |
| 391 |
r = self.args[i]:requirecollection(t) or r |
| 392 |
i = i+1 |
| 393 |
end |
| 394 |
return r |
| 395 |
end |
| 396 |
|
| 397 |
-- determine lua function name overload |
| 398 |
function classFunction:overload () |
| 399 |
return self.parent:overload(self.lname) |
| 400 |
end |
| 401 |
|
| 402 |
|
| 403 |
function param_object(par) -- returns true if the parameter has an object as its default value |
| 404 |
|
| 405 |
if not string.find(par, '=') then return false end -- it has no default value |
| 406 |
|
| 407 |
local _,_,def = string.find(par, "=(.*)$") |
| 408 |
|
| 409 |
if string.find(par, "|") then -- a list of flags |
| 410 |
|
| 411 |
return true |
| 412 |
end |
| 413 |
|
| 414 |
if string.find(par, "%*") then -- it's a pointer with a default value |
| 415 |
|
| 416 |
if string.find(par, '=%s*new') or string.find(par, "%(") then -- it's a pointer with an instance as default parameter.. is that valid? |
| 417 |
return true |
| 418 |
end |
| 419 |
return false -- default value is 'NULL' or something |
| 420 |
end |
| 421 |
|
| 422 |
|
| 423 |
if string.find(par, "[%(&]") then |
| 424 |
return true |
| 425 |
end -- default value is a constructor call (most likely for a const reference) |
| 426 |
|
| 427 |
--if string.find(par, "&") then |
| 428 |
|
| 429 |
-- if string.find(def, ":") or string.find(def, "^%s*new%s+") then |
| 430 |
|
| 431 |
-- -- it's a reference with default to something like Class::member, or 'new Class' |
| 432 |
-- return true |
| 433 |
-- end |
| 434 |
--end |
| 435 |
|
| 436 |
return false -- ? |
| 437 |
end |
| 438 |
|
| 439 |
function strip_last_arg(all_args, last_arg) -- strips the default value from the last argument |
| 440 |
|
| 441 |
local _,_,s_arg = string.find(last_arg, "^([^=]+)") |
| 442 |
last_arg = string.gsub(last_arg, "([%%%(%)])", "%%%1"); |
| 443 |
all_args = string.gsub(all_args, "%s*,%s*"..last_arg.."%s*%)%s*$", ")") |
| 444 |
return all_args, s_arg |
| 445 |
end |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
-- Internal constructor |
| 450 |
function _Function (t) |
| 451 |
setmetatable(t,classFunction) |
| 452 |
|
| 453 |
if t.const ~= 'const' and t.const ~= '' then |
| 454 |
error("#invalid 'const' specification") |
| 455 |
end |
| 456 |
|
| 457 |
append(t) |
| 458 |
if t:inclass() then |
| 459 |
--print ('t.name is '..t.name..', parent.name is '..t.parent.name) |
| 460 |
if string.gsub(t.name, "%b<>", "") == string.gsub(t.parent.original_name or t.parent.name, "%b<>", "") then |
| 461 |
t.name = 'new' |
| 462 |
t.lname = 'new' |
| 463 |
t.parent._new = true |
| 464 |
t.type = t.parent.name |
| 465 |
t.ptr = '*' |
| 466 |
elseif string.gsub(t.name, "%b<>", "") == '~'..string.gsub(t.parent.original_name or t.parent.name, "%b<>", "") then |
| 467 |
t.name = 'delete' |
| 468 |
t.lname = 'delete' |
| 469 |
t.parent._delete = true |
| 470 |
end |
| 471 |
end |
| 472 |
t.cname = t:cfuncname("tolua")..t:overload(t) |
| 473 |
return t |
| 474 |
end |
| 475 |
|
| 476 |
-- Constructor |
| 477 |
-- Expects three strings: one representing the function declaration, |
| 478 |
-- another representing the argument list, and the third representing |
| 479 |
-- the "const" or empty string. |
| 480 |
function Function (d,a,c) |
| 481 |
--local t = split(strsub(a,2,-2),',') -- eliminate braces |
| 482 |
--local t = split_params(strsub(a,2,-2)) |
| 483 |
|
| 484 |
if not flags['W'] and string.find(a, "%.%.%.%s*%)") then |
| 485 |
|
| 486 |
warning("Functions with variable arguments (`...') are not supported. Ignoring "..d..a..c) |
| 487 |
return nil |
| 488 |
end |
| 489 |
|
| 490 |
|
| 491 |
local i=1 |
| 492 |
local l = {n=0} |
| 493 |
|
| 494 |
a = string.gsub(a, "%s*([%(%)])%s*", "%1") |
| 495 |
local t,strip,last = strip_pars(strsub(a,2,-2)); |
| 496 |
if strip then |
| 497 |
--local ns = string.sub(strsub(a,1,-2), 1, -(string.len(last)+1)) |
| 498 |
local ns = join(t, ",", 1, last-1) |
| 499 |
|
| 500 |
ns = "("..string.gsub(ns, "%s*,%s*$", "")..')' |
| 501 |
--ns = strip_defaults(ns) |
| 502 |
|
| 503 |
local f = Function(d, ns, c) |
| 504 |
for i=1,last do |
| 505 |
t[i] = string.gsub(t[i], "=.*$", "") |
| 506 |
end |
| 507 |
end |
| 508 |
|
| 509 |
while t[i] do |
| 510 |
l.n = l.n+1 |
| 511 |
l[l.n] = Declaration(t[i],'var',true) |
| 512 |
i = i+1 |
| 513 |
end |
| 514 |
local f = Declaration(d,'func') |
| 515 |
f.args = l |
| 516 |
f.const = c |
| 517 |
return _Function(f) |
| 518 |
end |
| 519 |
|
| 520 |
function join(t, sep, first, last) |
| 521 |
|
| 522 |
first = first or 1 |
| 523 |
last = last or #t |
| 524 |
local lsep = "" |
| 525 |
local ret = "" |
| 526 |
local loop = false |
| 527 |
for i = first,last do |
| 528 |
|
| 529 |
ret = ret..lsep..t[i] |
| 530 |
lsep = sep |
| 531 |
loop = true |
| 532 |
end |
| 533 |
if not loop then |
| 534 |
return "" |
| 535 |
end |
| 536 |
|
| 537 |
return ret |
| 538 |
end |
| 539 |
|
| 540 |
function strip_pars(s) |
| 541 |
|
| 542 |
local t = split_c_tokens(s, ',') |
| 543 |
local strip = false |
| 544 |
local last |
| 545 |
|
| 546 |
for i=t.n,1,-1 do |
| 547 |
|
| 548 |
if not strip and param_object(t[i]) then |
| 549 |
last = i |
| 550 |
strip = true |
| 551 |
end |
| 552 |
--if strip then |
| 553 |
-- t[i] = string.gsub(t[i], "=.*$", "") |
| 554 |
--end |
| 555 |
end |
| 556 |
|
| 557 |
return t,strip,last |
| 558 |
|
| 559 |
end |
| 560 |
|
| 561 |
function strip_defaults(s) |
| 562 |
|
| 563 |
s = string.gsub(s, "^%(", "") |
| 564 |
s = string.gsub(s, "%)$", "") |
| 565 |
|
| 566 |
local t = split_c_tokens(s, ",") |
| 567 |
local sep, ret = "","" |
| 568 |
for i=1,t.n do |
| 569 |
t[i] = string.gsub(t[i], "=.*$", "") |
| 570 |
ret = ret..sep..t[i] |
| 571 |
sep = "," |
| 572 |
end |
| 573 |
|
| 574 |
return "("..ret..")" |
| 575 |
end |
| 576 |
|
| 577 |
|