Example #1
0
        /// <summary>
        /// Tries to convert a CLR object to a MoonSharp value, using "trivial" logic.
        /// Skips on custom conversions, etc.
        /// Does NOT throw on failure.
        /// </summary>
        internal static DynValue TryObjectToTrivialDynValue(Script script, object obj)
        {
            if (obj == null)
            {
                return(DynValue.Nil);
            }

            if (obj is DynValue)
            {
                return((DynValue)obj);
            }

            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 (NumericConversions.NumericTypes.Contains(t))
            {
                return(DynValue.NewNumber(NumericConversions.TypeToDouble(t, obj)));
            }

            if (obj is Table)
            {
                return(DynValue.NewTable((Table)obj));
            }

            return(null);
        }
Example #2
0
        /// <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.TypeToDouble(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);
        }
Example #3
0
        /// <summary>
        /// Tries to convert a CLR object to a MoonSharp value, using more in-depth analysis
        /// </summary>
        internal static DynValue ObjectToDynValue(Script script, object obj)
        {
            DynValue v = TryObjectToSimpleDynValue(script, obj);

            if (v != null)
            {
                return(v);
            }

            v = UserData.Create(obj);
            if (v != null)
            {
                return(v);
            }

            if (obj is Type)
            {
                v = UserData.CreateStatic(obj as Type);
            }

            // unregistered enums go as integers
            if (obj is Enum)
            {
                return(DynValue.NewNumber(NumericConversions.TypeToDouble(Enum.GetUnderlyingType(obj.GetType()), obj)));
            }

            if (v != null)
            {
                return(v);
            }

            if (obj is Delegate)
            {
                return(DynValue.NewCallback(CallbackFunction.FromDelegate(script, (Delegate)obj)));
            }

            if (obj is MethodInfo)
            {
                MethodInfo mi = (MethodInfo)obj;

                if (mi.IsStatic)
                {
                    return(DynValue.NewCallback(CallbackFunction.FromMethodInfo(script, mi)));
                }
            }

            if (obj is System.Collections.IList)
            {
                Table t = TableConversions.ConvertIListToTable(script, (System.Collections.IList)obj);
                return(DynValue.NewTable(t));
            }

            if (obj is System.Collections.IDictionary)
            {
                Table t = TableConversions.ConvertIDictionaryToTable(script, (System.Collections.IDictionary)obj);
                return(DynValue.NewTable(t));
            }

            var enumerator = EnumerationToDynValue(script, obj);

            if (enumerator != null)
            {
                return(enumerator);
            }


            throw ScriptRuntimeException.ConvertObjectFailed(obj);
        }
Example #4
0
        /// <summary>
        /// Converts a DynValue to a CLR object of a specific type
        /// </summary>
        internal static object DynValueToObjectOfType(DynValue value, Type desiredType, object defaultValue, bool isOptional)
        {
            if (desiredType.IsByRef)
            {
                desiredType = desiredType.GetElementType();
            }

            var converter = Script.GlobalOptions.CustomConverters.GetScriptToClrCustomConversion(value.Type, desiredType);

            if (converter != null)
            {
                var v = converter(value);
                if (v != null)
                {
                    return(v);
                }
            }

            if (desiredType == typeof(DynValue))
            {
                return(value);
            }

            if (desiredType == typeof(object))
            {
                return(DynValueToObject(value));
            }

            StringConversions.StringSubtype stringSubType = StringConversions.GetStringSubtype(desiredType);
            string str = null;

            Type nt           = Nullable.GetUnderlyingType(desiredType);
            Type nullableType = null;

            if (nt != null)
            {
                nullableType = desiredType;
                desiredType  = nt;
            }

            switch (value.Type)
            {
            case DataType.Void:
                if (isOptional)
                {
                    return(defaultValue);
                }
                else if ((!Framework.Do.IsValueType(desiredType)) || (nullableType != null))
                {
                    return(null);
                }
                break;

            case DataType.Nil:
                if (Framework.Do.IsValueType(desiredType))
                {
                    if (nullableType != null)
                    {
                        return(null);
                    }

                    if (isOptional)
                    {
                        return(defaultValue);
                    }
                }
                else
                {
                    return(null);
                }
                break;

            case DataType.Boolean:
                if (desiredType == typeof(bool))
                {
                    return(value.Boolean);
                }
                if (stringSubType != StringConversions.StringSubtype.None)
                {
                    str = value.Boolean.ToString();
                }
                break;

            case DataType.Number:
                if (Framework.Do.IsEnum(desiredType))
                {                               // number to enum conv
                    Type underType = Enum.GetUnderlyingType(desiredType);
                    return(NumericConversions.DoubleToType(underType, value.Number));
                }
                if (NumericConversions.NumericTypes.Contains(desiredType))
                {
                    return(NumericConversions.DoubleToType(desiredType, value.Number));
                }
                if (stringSubType != StringConversions.StringSubtype.None)
                {
                    str = value.Number.ToString();
                }
                break;

            case DataType.String:
                if (stringSubType != StringConversions.StringSubtype.None)
                {
                    str = value.String;
                }
                break;

            case DataType.Function:
                if (desiredType == typeof(Closure))
                {
                    return(value.Function);
                }
                else if (desiredType == typeof(ScriptFunctionDelegate))
                {
                    return(value.Function.GetDelegate());
                }
                break;

            case DataType.ClrFunction:
                if (desiredType == typeof(CallbackFunction))
                {
                    return(value.Callback);
                }
                else if (desiredType == typeof(Func <ScriptExecutionContext, CallbackArguments, DynValue>))
                {
                    return(value.Callback.ClrCallback);
                }
                break;

            case DataType.UserData:
                if (value.UserData.Object != null)
                {
                    var udObj  = value.UserData.Object;
                    var udDesc = value.UserData.Descriptor;

                    if (udDesc.IsTypeCompatible(desiredType, udObj))
                    {
                        return(udObj);
                    }

                    if (stringSubType != StringConversions.StringSubtype.None)
                    {
                        str = udDesc.AsString(udObj);
                    }
                }
                break;

            case DataType.Table:
                if (desiredType == typeof(Table) || Framework.Do.IsAssignableFrom(desiredType, typeof(Table)))
                {
                    return(value.Table);
                }
                else
                {
                    object o = TableConversions.ConvertTableToType(value.Table, desiredType);
                    if (o != null)
                    {
                        return(o);
                    }
                }
                break;

            case DataType.Tuple:
                break;
            }

            if (stringSubType != StringConversions.StringSubtype.None && str != null)
            {
                return(StringConversions.ConvertString(stringSubType, str, desiredType, value.Type));
            }

            throw ScriptRuntimeException.ConvertObjectFailed(value.Type, desiredType);
        }