/// <summary> /// Tries to convert a CLR object to a MoonSharp value, using "simple" logic. /// Does NOT throw on failure. /// </summary> internal static DynValue TryObjectToSimpleDynValue(Script script, object obj) { if (obj == null) { return(DynValue.Nil); } if (obj is DynValue) { return((DynValue)obj); } var converter = Script.GlobalOptions.CustomConverters.GetClrToScriptCustomConversion(obj.GetType()); if (converter != null) { var v = converter(script, obj); if (v != null) { return(v); } } Type t = obj.GetType(); if (obj is bool) { return(DynValue.NewBoolean((bool)obj)); } if (obj is string || obj is StringBuilder || obj is char) { return(DynValue.NewString(obj.ToString())); } if (obj is Closure) { return(DynValue.NewClosure((Closure)obj)); } if (NumericConversions.NumericTypes.Contains(t)) { return(DynValue.NewNumber(NumericConversions.TypeToFix(t, obj))); } if (obj is Table) { return(DynValue.NewTable((Table)obj)); } if (obj is CallbackFunction) { return(DynValue.NewCallback((CallbackFunction)obj)); } if (obj is Delegate) { Delegate d = (Delegate)obj; #if NETFX_CORE MethodInfo mi = d.GetMethodInfo(); #else MethodInfo mi = d.Method; #endif if (CallbackFunction.CheckCallbackSignature(mi, false)) { return(DynValue.NewCallback((Func <ScriptExecutionContext, CallbackArguments, DynValue>)d)); } } return(null); }
/// <summary> /// Tries to convert a CLR object to a MoonSharp value, using "simple" logic. /// Does NOT throw on failure. /// </summary> internal static R_VAL TryObjectToSimpleValue(RubyState state, object obj) { if (obj == null) { return(R_VAL.NIL); } if (obj is R_VAL) { return(( R_VAL )obj); } Type t = obj.GetType(); if (obj is bool) { return(R_VAL.NewBoolean(( bool )obj)); } if (obj is string || obj is StringBuilder || obj is char) { return(R_VAL.NewString(obj.ToString())); } if (obj is Closure) { return(R_VAL.NewClosure(( Closure )obj)); } if (NumericConversions.NumericTypes.Contains(t)) { return(R_VAL.NewNumber(NumericConversions.TypeToDouble(t, obj))); } if (obj is Table) { return(R_VAL.NewTable(( Table )obj)); } if (obj is CallbackFunction) { return(R_VAL.NewCallback(( CallbackFunction )obj)); } if (obj is Delegate) { Delegate d = ( Delegate )obj; #if NETFX_CORE MethodInfo mi = d.GetMethodInfo(); #else MethodInfo mi = d.Method; #endif if (CallbackFunction.CheckCallbackSignature(mi, false)) { return(R_VAL.NewCallback((Func <ScriptExecutionContext, CallbackArguments, R_VAL>)d)); } } return(R_VAL.NIL); }
/// <summary> /// Tries to convert a CLR object to a MoonSharp value, using "simple" logic. /// Does NOT throw on failure. /// </summary> internal static DynValue TryObjectToSimpleDynValue(Script script, object obj) { if (obj == null) { return(DynValue.Nil); } if (obj is DynValue value) { return(value); } var converter = Script.GlobalOptions.CustomConverters.GetClrToScriptCustomConversion(obj.GetType()); if (converter != null) { var v = converter(script, obj); if (v != null) { return(v); } } var t = obj.GetType(); if (obj is bool b) { return(DynValue.NewBoolean(b)); } if (obj is string || obj is StringBuilder || obj is char) { return(DynValue.NewString(obj.ToString())); } if (obj is Closure closure) { return(DynValue.NewClosure(closure)); } if (NumericConversions.NumericTypes.Contains(t)) { return(DynValue.NewNumber(NumericConversions.TypeToDouble(t, obj))); } if (obj is Table table) { return(DynValue.NewTable(table)); } if (obj is CallbackFunction function) { return(DynValue.NewCallback(function)); } if (obj is Delegate d) { var mi = d.Method; if (CallbackFunction.CheckCallbackSignature(mi, false)) { return(DynValue.NewCallback((Func <ScriptExecutionContext, CallbackArguments, DynValue>)d)); } } return(null); }