public static MondFunction CreateStaticMethodShim(Type type, string name, MondBindingOptions options) => delegate(MondState state, MondValue[] args) { var target = BindingUtils.GetTarget <MethodInfo>( type, name, args, BindingFlags.Public | BindingFlags.Static, options); var values = TypeConverter.MarshalToClr(args, target.Types, state, options); var result = target.Method.Invoke(null, values); if (target.Method.ReturnType == typeof(void)) { return(MondValue.Undefined); } if (result == null) { return(MondValue.Null); } var mondType = TypeConverter.ToMondType(result.GetType()); return(TypeConverter.MarshalToMond(result, mondType, state, options)); };
public static MondFunction CreateConstructorShim(Type type, MondValue prototype, MondBindingOptions options) => delegate(MondState state, MondValue[] args) { var target = BindingUtils.GetTarget <ConstructorInfo>( type, null, args, BindingFlags.Public | BindingFlags.Instance, options); var values = TypeConverter.MarshalToClr(args, target.Types, state, options); return(new MondValue(state) { UserData = target.Method.Invoke(values), Prototype = prototype }); };
public static MondInstanceFunction CreateInstanceMethodShim( Type type, string name, MondBindingOptions options, bool isMetamethod = false) => delegate(MondState state, MondValue instance, MondValue[] args) { // Remove the object instance from the argument list. // This is primarily to prevent argument mismatch exceptions // when the Mond runtime tries to dispatch a metamethod. if ((args != null) && (args.Length >= 1) && (args[0] == instance) && (name.StartsWith("__") || isMetamethod)) { args = args.Skip(1).ToArray(); } var target = BindingUtils.GetTarget <MethodInfo>( type, name, args, BindingFlags.Public | BindingFlags.Instance, options); var values = TypeConverter.MarshalToClr(args, target.Types, state, options); var result = target.Method.Invoke(instance.UserData, values); if (target.Method.ReturnType == typeof(void)) { return(MondValue.Undefined); } if (result == null) { return(MondValue.Null); } var mondType = TypeConverter.ToMondType(result.GetType()); return(TypeConverter.MarshalToMond(result, mondType, state, options)); };