Beispiel #1
0
    public object[] CallFunction(LuaObject func, params object[] arg)
    {
        int n = arg.Length;

        if (n + 1 > max_args)
        {
            throw new ArgumentException("Too many args");
        }
        args[0].type = var_type.LUAOBJ;
        args[0].d    = func.id;

        int sn = 0;

        for (int i = 0; i < n; i++)
        {
            int r = pushvalue(ref args[i + 1], arg[i]);
            switch (r)
            {
            case 0:
                throw new ArgumentException(String.Format("Unsupport type : {1} at {0}", i, arg[i].GetType()));

            case 1:
                break;

            case 2:
                // string
                args[i + 1].d = sn;
                strs[sn]      = (string)arg[i];
                ++sn;
                break;
            }
        }
        int retn = c_callfunction(L, n + 1, args, sn, strs);

        if (retn < 0)
        {
            throw new ArgumentException(Marshal.PtrToStringAnsi(args[0].ptr));
        }
        if (retn == 0)
        {
            return(null);
        }
        object[] ret = new object[retn];
        for (int i = 0; i < retn; i++)
        {
            switch (args[i].type)
            {
            case var_type.NIL:
                ret[i] = null;
                break;

            case var_type.INTEGER:
                ret[i] = args[i].d;
                break;

            case var_type.INT64:
                ret[i] = args[i].d64;
                break;

            case var_type.REAL:
                ret[i] = args[i].f;
                break;

            case var_type.BOOLEAN:
                ret[i] = (args[i].d != 0) ? true : false;
                break;

            case var_type.STRING:
                // todo: encoding
                ret[i] = Marshal.PtrToStringAnsi(args[i].ptr);
                break;

            case var_type.POINTER:
                ret[i] = args[i].ptr;
                break;

            case var_type.LUAOBJ:
                ret[i] = new LuaObject {
                    id = args[i].d
                };
                break;

            case var_type.SHAPEOBJ:
                ret[i] = objects.Get(args[i].d);
                if (ret[i] == null)
                {
                    throw new ArgumentException("Invalid shape object");
                }
                break;
            }
        }

        return(ret);
    }