public NativeFunction(NativeFunctionDelegate fun, int argc) : base(argc)
 {
     if (fun == null)
     {
         throw new ArgumentNullException(nameof(fun));
     }
     this.fun = fun;
 }
Beispiel #2
0
        public NativeFunctionInstance(string name, MelonObject self, MelonEngine engine, NativeFunctionDelegate del) : base(name, engine)
        {
            Self = self;

            ReturnTypeAttribute returnTypeAttribute = del.Method.GetCustomAttribute <ReturnTypeAttribute>();

            if (returnTypeAttribute != null)
            {
                ReturnType = new TypeReference(engine, engine.GetType(returnTypeAttribute.Type));
            }

            ParameterAttribute[] parameterAttributes = del.Method.GetCustomAttributes <ParameterAttribute>().ToArray();

            if (parameterAttributes?.Length > 0)
            {
                ParameterTypes = new FunctionParameter[parameterAttributes.Length];

                bool hasVarargs = false;

                for (int i = 0; i < parameterAttributes.Length; i++)
                {
                    if (parameterAttributes[i].IsGeneric)
                    {
                        ParameterTypes[i] = new FunctionParameter(parameterAttributes[i].Name, parameterAttributes[i].GenericIndex);
                    }
                    else
                    {
                        ParameterTypes[i] = new FunctionParameter(parameterAttributes[i].Name, new TypeReference(engine, engine.GetType(parameterAttributes[i].Type)));
                    }

                    if (parameterAttributes[i].IsVarargs)
                    {
                        if (hasVarargs || i < parameterAttributes.Length - 1)
                        {
                            throw new MelonException("Varargs parameter can only appear once and has to be the last parameter");
                        }

                        ParameterTypes[i].IsVarargs = true;

                        hasVarargs = true;
                    }
                }
            }
            else
            {
                ParameterTypes = new[] { new FunctionParameter("", new TypeReference(engine, engine.anyType))
                                         {
                                             IsVarargs = true
                                         } };
            }

            Delegate = del;
        }
        private NativeFunctionDelegate FindNativeFunction(string name)
        {
            NativeFunctionDelegate nativeFunction = null;

            if (NativeFunctions.TryGetValue(name, out nativeFunction))
            {
                return(nativeFunction);
            }

            if (NativeFunctions.TryGetValue(StringUtility.PascalCase(name), out nativeFunction))
            {
                return(nativeFunction);
            }

            if (NativeFunctions.TryGetValue(StringUtility.CamelCase(name), out nativeFunction))
            {
                return(nativeFunction);
            }

            return(null);
        }
Beispiel #4
0
 public void RegisterFunction(string name, int argc, NativeFunctionDelegate fun) => RegisterFunction(name, new NativeFunction(fun, argc));
 public NativeFunction(NativeFunctionDelegate fun) : this(fun, 0)
 {
 }