From 2e9fe777e425e65d733110247e73c1d9fb25ab1c Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 15 Mar 2014 06:45:26 -0700 Subject: Patched tolua to emit range checks for enums --- lib/tolua++/src/bin/lua/enumerate.lua | 78 ++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 23 deletions(-) (limited to 'lib/tolua++/src/bin/lua/enumerate.lua') diff --git a/lib/tolua++/src/bin/lua/enumerate.lua b/lib/tolua++/src/bin/lua/enumerate.lua index 99fe74629..5f0dedfe1 100644 --- a/lib/tolua++/src/bin/lua/enumerate.lua +++ b/lib/tolua++/src/bin/lua/enumerate.lua @@ -48,6 +48,21 @@ function classEnumerate:print (ident,close) print(ident.."}"..close) end +_global_output_enums = {} + +-- write support code +function classEnumerate:supcode () + if _global_output_enums[self.name] ~= nil then + _global_output_enums[self.name] = 1 + output("int tolua_is" .. self.name .. " (lua_State* L, int lo, int def, tolua_Error* err)") + output("{") + output("if (!tolua_isnumber(L,lo,def,err)) return 0;") + output("int val = tolua_tonumber(L,lo,def);") + output("return val >= " .. self.min .. " && val <= " ..self.max .. ";") + output("}") + end +end + -- Internal constructor function _Enumerate (t,varname) setmetatable(t,classEnumerate) @@ -67,40 +82,57 @@ function _Enumerate (t,varname) t.access = parent.curr_member_access t.global_access = t:check_public_access() end -return t + return t end -- Constructor -- Expects a string representing the enumerate body function Enumerate (n,b,varname) b = string.gsub(b, ",[%s\n]*}", "\n}") -- eliminate last ',' - local t = split(strsub(b,2,-2),',') -- eliminate braces - local i = 1 - local e = {n=0} - while t[i] do - local tt = split(t[i],'=') -- discard initial value - e.n = e.n + 1 - e[e.n] = tt[1] - i = i+1 - end - -- set lua names - i = 1 - e.lnames = {} - local ns = getcurrnamespace() - while e[i] do - local t = split(e[i],'@') - e[i] = t[1] + local t = split(strsub(b,2,-2),',') -- eliminate braces + local i = 1 + local e = {n=0} + local value = 0 + local min = 0 + local max = 0 + while t[i] do + local tt = split(t[i],'=') -- discard initial value + e.n = e.n + 1 + e[e.n] = tt[1] + tt[2] = tonumber(tt[2]) + if tt[2] == nil then + tt[2] = value + end + value = tt[2] + 1 -- advance the selected value + if tt[2] > max then + max = tt[2] + end + if tt[2] < min then + min = tt[2] + end + i = i+1 + end + -- set lua names + i = 1 + e.lnames = {} + local ns = getcurrnamespace() + while e[i] do + local t = split(e[i],'@') + e[i] = t[1] if not t[2] then - t[2] = applyrenaming(t[1]) + t[2] = applyrenaming(t[1]) end - e.lnames[i] = t[2] or t[1] - _global_enums[ ns..e[i] ] = (ns..e[i]) - i = i+1 - end + e.lnames[i] = t[2] or t[1] + _global_enums[ ns..e[i] ] = (ns..e[i]) + i = i+1 + end e.name = n + e.min = min + e.max = max if n ~= "" then Typedef("int "..n) + _is_functions[n] = "tolua_is" .. n end - return _Enumerate(e, varname) + return _Enumerate(e, varname) end -- cgit v1.2.3 From 0a505576e5220113dfbcc140b70659f822a17f44 Mon Sep 17 00:00:00 2001 From: Tycho Date: Mon, 17 Mar 2014 10:28:04 -0700 Subject: Fixed =~ bug --- lib/tolua++/src/bin/lua/enumerate.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/tolua++/src/bin/lua/enumerate.lua') diff --git a/lib/tolua++/src/bin/lua/enumerate.lua b/lib/tolua++/src/bin/lua/enumerate.lua index 5f0dedfe1..9c534a020 100644 --- a/lib/tolua++/src/bin/lua/enumerate.lua +++ b/lib/tolua++/src/bin/lua/enumerate.lua @@ -52,7 +52,7 @@ _global_output_enums = {} -- write support code function classEnumerate:supcode () - if _global_output_enums[self.name] ~= nil then + if _global_output_enums[self.name] == nil then _global_output_enums[self.name] = 1 output("int tolua_is" .. self.name .. " (lua_State* L, int lo, int def, tolua_Error* err)") output("{") -- cgit v1.2.3 From e8f7c18ba75be4e62b9b0b7f55c5fe8eae1af1ec Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 19 Mar 2014 11:34:33 -0700 Subject: Fixed type error in lua bindings --- lib/tolua++/src/bin/lua/enumerate.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/tolua++/src/bin/lua/enumerate.lua') diff --git a/lib/tolua++/src/bin/lua/enumerate.lua b/lib/tolua++/src/bin/lua/enumerate.lua index 9c534a020..a00b434aa 100644 --- a/lib/tolua++/src/bin/lua/enumerate.lua +++ b/lib/tolua++/src/bin/lua/enumerate.lua @@ -54,11 +54,11 @@ _global_output_enums = {} function classEnumerate:supcode () if _global_output_enums[self.name] == nil then _global_output_enums[self.name] = 1 - output("int tolua_is" .. self.name .. " (lua_State* L, int lo, int def, tolua_Error* err)") + output("lua_Number tolua_is" .. self.name .. " (lua_State* L, int lo, int def, tolua_Error* err)") output("{") output("if (!tolua_isnumber(L,lo,def,err)) return 0;") - output("int val = tolua_tonumber(L,lo,def);") - output("return val >= " .. self.min .. " && val <= " ..self.max .. ";") + output("lua_Number val = tolua_tonumber(L,lo,def);") + output("return val >= " .. self.min .. ".0 && val <= " ..self.max .. ".0;") output("}") end end -- cgit v1.2.3 From 04adca34106a83e7ccd8d3e6107e16d79595ba6d Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 19 Mar 2014 12:02:26 -0700 Subject: Fixed tolua emitting isnumber insteand of is --- lib/tolua++/src/bin/lua/enumerate.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/tolua++/src/bin/lua/enumerate.lua') diff --git a/lib/tolua++/src/bin/lua/enumerate.lua b/lib/tolua++/src/bin/lua/enumerate.lua index a00b434aa..d5d4426cf 100644 --- a/lib/tolua++/src/bin/lua/enumerate.lua +++ b/lib/tolua++/src/bin/lua/enumerate.lua @@ -130,8 +130,8 @@ function Enumerate (n,b,varname) e.min = min e.max = max if n ~= "" then + _enum_is_functions[n] = ("tolua_is" .. n) Typedef("int "..n) - _is_functions[n] = "tolua_is" .. n end return _Enumerate(e, varname) end -- cgit v1.2.3 From 6a3fe7adcc2a3855a574dbfc2bb79c86e7539f26 Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 19 Mar 2014 12:38:00 -0700 Subject: Fixed bugs in patched tolua output --- lib/tolua++/src/bin/lua/enumerate.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/tolua++/src/bin/lua/enumerate.lua') diff --git a/lib/tolua++/src/bin/lua/enumerate.lua b/lib/tolua++/src/bin/lua/enumerate.lua index d5d4426cf..ef3a9574c 100644 --- a/lib/tolua++/src/bin/lua/enumerate.lua +++ b/lib/tolua++/src/bin/lua/enumerate.lua @@ -48,13 +48,17 @@ function classEnumerate:print (ident,close) print(ident.."}"..close) end +function emitenumprototype(type) + output("int tolua_is" .. string.gsub(type,"::","_") .. " (lua_State* L, int lo, const char * type, int def, tolua_Error* err);") +end + _global_output_enums = {} -- write support code function classEnumerate:supcode () if _global_output_enums[self.name] == nil then _global_output_enums[self.name] = 1 - output("lua_Number tolua_is" .. self.name .. " (lua_State* L, int lo, int def, tolua_Error* err)") + output("int tolua_is" .. string.gsub(self.name,"::","_") .. " (lua_State* L, int lo, const char * type, int def, tolua_Error* err)") output("{") output("if (!tolua_isnumber(L,lo,def,err)) return 0;") output("lua_Number val = tolua_tonumber(L,lo,def);") @@ -130,7 +134,7 @@ function Enumerate (n,b,varname) e.min = min e.max = max if n ~= "" then - _enum_is_functions[n] = ("tolua_is" .. n) + _enums[n] = 1 Typedef("int "..n) end return _Enumerate(e, varname) -- cgit v1.2.3 From a69a1ef0325c5e472f1dd736f3a14a260d747ad9 Mon Sep 17 00:00:00 2001 From: Tycho Date: Thu, 20 Mar 2014 13:23:15 -0700 Subject: Fixed enum checking functions not being called in generated code --- lib/tolua++/src/bin/lua/enumerate.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/tolua++/src/bin/lua/enumerate.lua') diff --git a/lib/tolua++/src/bin/lua/enumerate.lua b/lib/tolua++/src/bin/lua/enumerate.lua index ef3a9574c..09b22a094 100644 --- a/lib/tolua++/src/bin/lua/enumerate.lua +++ b/lib/tolua++/src/bin/lua/enumerate.lua @@ -49,7 +49,7 @@ function classEnumerate:print (ident,close) end function emitenumprototype(type) - output("int tolua_is" .. string.gsub(type,"::","_") .. " (lua_State* L, int lo, const char * type, int def, tolua_Error* err);") + output("int tolua_is" .. string.gsub(type,"::","_") .. " (lua_State* L, int lo, int def, tolua_Error* err);") end _global_output_enums = {} @@ -58,7 +58,7 @@ _global_output_enums = {} function classEnumerate:supcode () if _global_output_enums[self.name] == nil then _global_output_enums[self.name] = 1 - output("int tolua_is" .. string.gsub(self.name,"::","_") .. " (lua_State* L, int lo, const char * type, int def, tolua_Error* err)") + output("int tolua_is" .. string.gsub(self.name,"::","_") .. " (lua_State* L, int lo, int def, tolua_Error* err)") output("{") output("if (!tolua_isnumber(L,lo,def,err)) return 0;") output("lua_Number val = tolua_tonumber(L,lo,def);") @@ -134,7 +134,7 @@ function Enumerate (n,b,varname) e.min = min e.max = max if n ~= "" then - _enums[n] = 1 + _enums[n] = true Typedef("int "..n) end return _Enumerate(e, varname) -- cgit v1.2.3