[1650] | 1 | -- tolua: array class |
---|
| 2 | -- Written by Waldemar Celes |
---|
| 3 | -- TeCGraf/PUC-Rio |
---|
| 4 | -- Jul 1999 |
---|
| 5 | -- $Id: array.lua,v 1.1 2000/11/06 22:03:57 celes Exp $ |
---|
| 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 | -- Array class |
---|
| 14 | -- Represents a extern array variable or a public member of a class. |
---|
| 15 | -- Stores all fields present in a declaration. |
---|
| 16 | classArray = { |
---|
| 17 | } |
---|
| 18 | classArray.__index = classArray |
---|
| 19 | setmetatable(classArray,classDeclaration) |
---|
| 20 | |
---|
| 21 | -- Print method |
---|
| 22 | function classArray:print (ident,close) |
---|
[2710] | 23 | print(ident.."Array{") |
---|
| 24 | print(ident.." mod = '"..self.mod.."',") |
---|
| 25 | print(ident.." type = '"..self.type.."',") |
---|
| 26 | print(ident.." ptr = '"..self.ptr.."',") |
---|
| 27 | print(ident.." name = '"..self.name.."',") |
---|
| 28 | print(ident.." def = '"..self.def.."',") |
---|
| 29 | print(ident.." dim = '"..self.dim.."',") |
---|
| 30 | print(ident.." ret = '"..self.ret.."',") |
---|
| 31 | print(ident.."}"..close) |
---|
[1650] | 32 | end |
---|
| 33 | |
---|
| 34 | -- check if it is a variable |
---|
| 35 | function classArray:isvariable () |
---|
[2710] | 36 | return true |
---|
[1650] | 37 | end |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | -- get variable value |
---|
| 41 | function classArray:getvalue (class,static) |
---|
[2710] | 42 | if class and static then |
---|
| 43 | return class..'::'..self.name..'[tolua_index]' |
---|
| 44 | elseif class then |
---|
| 45 | return 'self->'..self.name..'[tolua_index]' |
---|
| 46 | else |
---|
| 47 | return self.name..'[tolua_index]' |
---|
| 48 | end |
---|
[1650] | 49 | end |
---|
| 50 | |
---|
| 51 | -- Write binding functions |
---|
| 52 | function classArray:supcode () |
---|
[2710] | 53 | local class = self:inclass() |
---|
[1650] | 54 | |
---|
[2710] | 55 | -- get function ------------------------------------------------ |
---|
| 56 | if class then |
---|
| 57 | output("/* get function:",self.name," of class ",class," */") |
---|
| 58 | else |
---|
| 59 | output("/* get function:",self.name," */") |
---|
| 60 | end |
---|
| 61 | self.cgetname = self:cfuncname("tolua_get") |
---|
| 62 | output("#ifndef TOLUA_DISABLE_"..self.cgetname) |
---|
| 63 | output("\nstatic int",self.cgetname,"(lua_State* tolua_S)") |
---|
| 64 | output("{") |
---|
| 65 | output(" int tolua_index;") |
---|
[1650] | 66 | |
---|
[2710] | 67 | -- declare self, if the case |
---|
| 68 | local _,_,static = strfind(self.mod,'^%s*(static)') |
---|
| 69 | if class and static==nil then |
---|
| 70 | output(' ',self.parent.type,'*','self;') |
---|
| 71 | output(' lua_pushstring(tolua_S,".self");') |
---|
| 72 | output(' lua_rawget(tolua_S,1);') |
---|
| 73 | output(' self = ') |
---|
| 74 | output('(',self.parent.type,'*) ') |
---|
| 75 | output('lua_touserdata(tolua_S,-1);') |
---|
| 76 | elseif static then |
---|
| 77 | _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)') |
---|
| 78 | end |
---|
[1650] | 79 | |
---|
[2710] | 80 | -- check index |
---|
| 81 | output('#ifndef TOLUA_RELEASE\n') |
---|
| 82 | output(' {') |
---|
| 83 | output(' tolua_Error tolua_err;') |
---|
| 84 | output(' if (!tolua_isnumber(tolua_S,2,0,&tolua_err))') |
---|
| 85 | output(' tolua_error(tolua_S,"#vinvalid type in array indexing.",&tolua_err);') |
---|
| 86 | output(' }') |
---|
| 87 | output('#endif\n') |
---|
| 88 | if flags['1'] then -- for compatibility with tolua5 ? |
---|
| 89 | output(' tolua_index = (int)tolua_tonumber(tolua_S,2,0)-1;') |
---|
| 90 | else |
---|
| 91 | output(' tolua_index = (int)tolua_tonumber(tolua_S,2,0);') |
---|
| 92 | end |
---|
| 93 | output('#ifndef TOLUA_RELEASE\n') |
---|
| 94 | if self.dim and self.dim ~= '' then |
---|
| 95 | output(' if (tolua_index<0 || tolua_index>='..self.dim..')') |
---|
| 96 | else |
---|
| 97 | output(' if (tolua_index<0)') |
---|
| 98 | end |
---|
| 99 | output(' tolua_error(tolua_S,"array indexing out of range.",NULL);') |
---|
| 100 | output('#endif\n') |
---|
[1650] | 101 | |
---|
[2710] | 102 | -- return value |
---|
| 103 | local t,ct = isbasic(self.type) |
---|
[3127] | 104 | local push_func = get_push_function(t) |
---|
[2710] | 105 | if t then |
---|
| 106 | output(' tolua_push'..t..'(tolua_S,(',ct,')'..self:getvalue(class,static)..');') |
---|
| 107 | else |
---|
| 108 | t = self.type |
---|
| 109 | if self.ptr == '&' or self.ptr == '' then |
---|
[3127] | 110 | output(' ',push_func,'(tolua_S,(void*)&'..self:getvalue(class,static)..',"',t,'");') |
---|
[2710] | 111 | else |
---|
[3127] | 112 | output(' ',push_func,'(tolua_S,(void*)'..self:getvalue(class,static)..',"',t,'");') |
---|
[2710] | 113 | end |
---|
| 114 | end |
---|
| 115 | output(' return 1;') |
---|
| 116 | output('}') |
---|
| 117 | output('#endif //#ifndef TOLUA_DISABLE\n') |
---|
| 118 | output('\n') |
---|
[1650] | 119 | |
---|
[2710] | 120 | -- set function ------------------------------------------------ |
---|
| 121 | if not strfind(self.type,'const') then |
---|
| 122 | if class then |
---|
| 123 | output("/* set function:",self.name," of class ",class," */") |
---|
| 124 | else |
---|
| 125 | output("/* set function:",self.name," */") |
---|
| 126 | end |
---|
| 127 | self.csetname = self:cfuncname("tolua_set") |
---|
| 128 | output("#ifndef TOLUA_DISABLE_"..self.csetname) |
---|
| 129 | output("\nstatic int",self.csetname,"(lua_State* tolua_S)") |
---|
| 130 | output("{") |
---|
[1650] | 131 | |
---|
[2710] | 132 | -- declare index |
---|
| 133 | output(' int tolua_index;') |
---|
[1650] | 134 | |
---|
[2710] | 135 | -- declare self, if the case |
---|
| 136 | local _,_,static = strfind(self.mod,'^%s*(static)') |
---|
| 137 | if class and static==nil then |
---|
| 138 | output(' ',self.parent.type,'*','self;') |
---|
| 139 | output(' lua_pushstring(tolua_S,".self");') |
---|
| 140 | output(' lua_rawget(tolua_S,1);') |
---|
| 141 | output(' self = ') |
---|
| 142 | output('(',self.parent.type,'*) ') |
---|
| 143 | output('lua_touserdata(tolua_S,-1);') |
---|
| 144 | elseif static then |
---|
| 145 | _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)') |
---|
| 146 | end |
---|
[1650] | 147 | |
---|
[2710] | 148 | -- check index |
---|
| 149 | output('#ifndef TOLUA_RELEASE\n') |
---|
| 150 | output(' {') |
---|
| 151 | output(' tolua_Error tolua_err;') |
---|
| 152 | output(' if (!tolua_isnumber(tolua_S,2,0,&tolua_err))') |
---|
| 153 | output(' tolua_error(tolua_S,"#vinvalid type in array indexing.",&tolua_err);') |
---|
| 154 | output(' }') |
---|
| 155 | output('#endif\n') |
---|
[1650] | 156 | |
---|
[2710] | 157 | if flags['1'] then -- for compatibility with tolua5 ? |
---|
| 158 | output(' tolua_index = (int)tolua_tonumber(tolua_S,2,0)-1;') |
---|
| 159 | else |
---|
| 160 | output(' tolua_index = (int)tolua_tonumber(tolua_S,2,0);') |
---|
| 161 | end |
---|
[1650] | 162 | |
---|
[2710] | 163 | output('#ifndef TOLUA_RELEASE\n') |
---|
| 164 | if self.dim and self.dim ~= '' then |
---|
| 165 | output(' if (tolua_index<0 || tolua_index>='..self.dim..')') |
---|
| 166 | else |
---|
| 167 | output(' if (tolua_index<0)') |
---|
| 168 | end |
---|
| 169 | output(' tolua_error(tolua_S,"array indexing out of range.",NULL);') |
---|
| 170 | output('#endif\n') |
---|
[1650] | 171 | |
---|
[2710] | 172 | -- assign value |
---|
| 173 | local ptr = '' |
---|
| 174 | if self.ptr~='' then ptr = '*' end |
---|
| 175 | output(' ') |
---|
| 176 | if class and static then |
---|
| 177 | output(class..'::'..self.name..'[tolua_index]') |
---|
| 178 | elseif class then |
---|
| 179 | output('self->'..self.name..'[tolua_index]') |
---|
| 180 | else |
---|
| 181 | output(self.name..'[tolua_index]') |
---|
| 182 | end |
---|
| 183 | local t = isbasic(self.type) |
---|
| 184 | output(' = ') |
---|
| 185 | if not t and ptr=='' then output('*') end |
---|
| 186 | output('((',self.mod,self.type) |
---|
| 187 | if not t then |
---|
| 188 | output('*') |
---|
| 189 | end |
---|
| 190 | output(') ') |
---|
| 191 | local def = 0 |
---|
| 192 | if self.def ~= '' then def = self.def end |
---|
| 193 | if t then |
---|
| 194 | output('tolua_to'..t,'(tolua_S,3,',def,'));') |
---|
| 195 | else |
---|
[3127] | 196 | local to_func = get_to_function(self.type) |
---|
| 197 | output(to_func,'(tolua_S,3,',def,'));') |
---|
[2710] | 198 | end |
---|
| 199 | output(' return 0;') |
---|
| 200 | output('}') |
---|
| 201 | output('#endif //#ifndef TOLUA_DISABLE\n') |
---|
| 202 | output('\n') |
---|
| 203 | end |
---|
[1650] | 204 | end |
---|
| 205 | |
---|
| 206 | function classArray:register (pre) |
---|
[3127] | 207 | if not self:check_public_access() then |
---|
| 208 | return |
---|
| 209 | end |
---|
| 210 | |
---|
[2710] | 211 | pre = pre or '' |
---|
| 212 | if self.csetname then |
---|
| 213 | output(pre..'tolua_array(tolua_S,"'..self.lname..'",'..self.cgetname..','..self.csetname..');') |
---|
| 214 | else |
---|
| 215 | output(pre..'tolua_array(tolua_S,"'..self.lname..'",'..self.cgetname..',NULL);') |
---|
| 216 | end |
---|
[1650] | 217 | end |
---|
| 218 | |
---|
| 219 | -- Internal constructor |
---|
| 220 | function _Array (t) |
---|
[2710] | 221 | setmetatable(t,classArray) |
---|
| 222 | append(t) |
---|
| 223 | return t |
---|
[1650] | 224 | end |
---|
| 225 | |
---|
| 226 | -- Constructor |
---|
| 227 | -- Expects a string representing the variable declaration. |
---|
| 228 | function Array (s) |
---|
[2710] | 229 | return _Array (Declaration(s,'var')) |
---|
[1650] | 230 | end |
---|
| 231 | |
---|
| 232 | |
---|