sovr610 / luainterface

Automatically exported from code.google.com/p/luainterface
1 stars 0 forks source link

Enhance support for calling methods with params arguments. #22

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In the current build, it is extremely tedious to call .net methods which 
contain a params argument.  Users must first manually create an array object 
that matches the correct type of the array tagged with the params command, then 
populate each element of that array and finally pass the array in as an 
argument.  The method calling logic should be capable of determining whether or 
not a particular argument is an array, and build the proper object internally.  
On the user side, the call should simply require passing in a table built on 
the fly.

For example, given the method signature public static int Add(params int[] 
values), it should be possible to call the method in lua script with the 
following notation:  Add({1,3,5,2,9});

Internally, LuaInterface should take note of the params attribute, grab the 
table passed in for that argument, and parse it into an int array.

Additionally, if a single value is passed, such as Add(1), then lua interface 
should be able to generate an int array and push the single value into it.

Original issue reported on code.google.com by eonstorm@gmail.com on 3 Jul 2010 at 2:59

GoogleCodeExporter commented 9 years ago
I've added support for this functionality as of revision 17.

I've tested it pretty thoroughly, but it would be awesome if people could grab 
the r17 version and check it out as well.

Original comment by eonstorm@gmail.com on 3 Jul 2010 at 3:29

GoogleCodeExporter commented 9 years ago
In my testing i could never get it to call the new code in r17 for a params 
argument

i was able to hack in my own support to make it work like i wanted in 
Metatables.c line 820, basicly if last ParameterInfo for a method and it is a 
params you know any remaining arguments should be added to the args array, 
currently they are not and it throws out invalid arguents, i hacked in support 
that loops remaining args and adds them to the args array, using this code it 
allows you to use function({arg, arg2}) or function (arg, arg2), all i did was 
add an else if check to see if any remaining arguments after current and if so 
loop them and add to params array, now not sure how good this code is but fixes 
the problem for me and allows me to call functions with out the {}

Code:

                    else if (currentLuaParam < nLuaParams)
                    {
                        int idx = 1;
                        paramArray = Array.CreateInstance(paramArrayType, nLuaParams - currentLuaParam + 1);
                        paramArray.SetValue(luaParamValue, 0);
                        currentLuaParam++;
                        while (currentLuaParam <= nLuaParams && _IsParamsArray(luaState, currentLuaParam, currentNetParam, out extractValue))
                        {
                            luaParamValue = extractValue(luaState, currentLuaParam);
                            paramArrayType = currentNetParam.ParameterType.GetElementType();
                            paramArray.SetValue(luaParamValue, idx);
                            currentLuaParam++;
                            idx++;
                        }
                        currentLuaParam--;
                    }

Original comment by icewolfz...@gmail.com on 16 Jul 2010 at 3:27

GoogleCodeExporter commented 9 years ago
another error i found but am still trying to fix is function() for params as in 
.net if no params it is still a valid function call but in lua it reports as 
invalid arguments

Original comment by icewolfz...@gmail.com on 16 Jul 2010 at 3:41

GoogleCodeExporter commented 9 years ago
another error i found but am still trying to fix is function() for params as in 
.net if no params it is still a valid function call but in lua it reports as 
invalid arguments

Original comment by icewolfz...@gmail.com on 16 Jul 2010 at 3:41

GoogleCodeExporter commented 9 years ago
i was able to fix params if none and it is of type param array

line 775 of metatables.c if you add a elseif to check if a params array and add 
a empty array of type as value it fixes it.

also had to add a 2nd IsParamsArray,  just returns true or false and nothing 
nothing else

code :

else if (_IsParamsArray(luaState, currentLuaParam, currentNetParam))
{    
paramList.Add(Array.CreateInstance(currentNetParam.ParameterType.GetElementType(
), 0));
}

        private bool _IsParamsArray(IntPtr luaState, int currentLuaParam, ParameterInfo currentNetParam)
        {
            if (currentNetParam.GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0)
                return true;
            Debug.WriteLine("Type wasn't Params object.");
            return false;
        }

Original comment by icewolfz...@gmail.com on 16 Jul 2010 at 3:59

GoogleCodeExporter commented 9 years ago
a patch of my changes to metatables.c

Original comment by icewolfz...@gmail.com on 16 Jul 2010 at 4:03

Attachments: