Browse Source

Add rudimentary Lua function overload checker to catch invalid order.
Fix two of the pkg files being detected to have wrong order declared.

Yao Wei Tjong 姚伟忠 10 years ago
parent
commit
2fafa4c366

+ 1 - 1
Source/Urho3D/LuaScript/pkgs/Math/StringHash.pkg

@@ -4,8 +4,8 @@ class StringHash
 {
     StringHash();
     StringHash(const StringHash& rhs);
+    StringHash(const char* str);            // This works for Lua string and Urho3D:String
     explicit StringHash(unsigned value);
-    StringHash(const String str);
     ~StringHash();
 
     StringHash operator + (const StringHash& rhs) const;

+ 3 - 7
Source/Urho3D/LuaScript/pkgs/Resource/JSONValue.pkg

@@ -14,12 +14,8 @@ class JSONValue
 {
     JSONValue();
     JSONValue(bool value);
-    JSONValue(int value);
-    JSONValue(unsigned value);
-    JSONValue(float value);
-    JSONValue(double value);
-    JSONValue(const String value);
-    JSONValue(const char* value);
+    JSONValue(const char* value);           // This works for Lua string and Urho3D:String
+    JSONValue(double value);                // This works for all number types, must be declared after string overload
     JSONValue(const JSONArray& value);
     JSONValue(const JSONObject& value);
     JSONValue(const JSONValue& value);
@@ -118,4 +114,4 @@ static void JSONValueSetObject(JSONValue* jsonValue, const JSONObject& value)
     (*jsonValue) = value;
 }
 
-$}
+$}

+ 20 - 1
Source/Urho3D/LuaScript/pkgs/ToCppHook.lua

@@ -137,7 +137,8 @@ end
 
 function is_arithmetic(t)
     for _, type in pairs({ "char", "short", "int", "unsigned", "long", "float", "double", "bool" }) do
-        if t:find(type) then return true end
+        _, pos = t:find(type)
+        if pos ~= nil and t:sub(pos + 1, pos + 1) ~= "*" then return true end
     end
     return false
 end
@@ -206,3 +207,21 @@ function get_property_methods_hook(ptype, name)
         return Name, "Set"..Name
     end
 end
+
+-- Rudimentary checker to prevent function overloads being declared in a wrong order
+-- The checker assumes function overloads are declared in group one after another within a same pkg file
+-- The checker only checks for single argument function at the moment, but it can be extended to support more when it is necessary
+local overload_checker = {name="", has_number=false}
+function pre_call_hook(self)
+    if table.getn(self.args) ~= 1 then return end
+    if overload_checker.name ~= self.name then
+        overload_checker.name = self.name
+        overload_checker.has_number = false
+    end
+    local t = self.args[1].type
+    if overload_checker.has_number then
+        if t:find("String") or t:find("char*") then warning(self:inclass() .. ":" .. self.name .. " has potential binding problem: number overload becomes dead code if it is declared before string overload") end
+    else
+        overload_checker.has_number = t ~= "bool" and is_arithmetic(t)
+    end
+end