예제 #1
0
        internal static object ConvertString(StringSubtype stringSubType, string str, Type desiredType, DataType dataType)
        {
            switch (stringSubType)
            {
            case StringSubtype.String:
                return(str);

            case StringSubtype.StringBuilder:
                return(new StringBuilder(str));

            case StringSubtype.Char:
                if (!string.IsNullOrEmpty(str))
                {
                    return(str[0]);
                }

                break;

            case StringSubtype.None:
            default:
                break;
            }

            throw ScriptRuntimeException.ConvertObjectFailed(dataType, desiredType);
        }
예제 #2
0
        /// <summary>
        /// Converts a DynValue to a CLR object [simple conversion]
        /// </summary>
        internal static object DynValueToObject(DynValue value)
        {
            var converter = Script.GlobalOptions.CustomConverters.GetScriptToClrCustomConversion(value.Type, typeof(System.Object));

            if (converter != null)
            {
                var v = converter(value, typeof(System.Object));
                if (v != null)
                {
                    return(v);
                }
            }

            switch (value.Type)
            {
            case DataType.Void:
            case DataType.Nil:
                return(null);

            case DataType.Boolean:
                return(value.Boolean);

            case DataType.Number:
                return(value.Number);

            case DataType.String:
                return(value.String);

            case DataType.Function:
                return(value.Function);

            case DataType.Table:
                return(value.Table);

            case DataType.Tuple:
                return(value.Tuple);

            case DataType.UserData:
                object content;
                if (value.UserData.TryGet(out content))
                {
                    return(content);
                }
                else if (value.UserData.Descriptor != null)
                {
                    return(value.UserData.Descriptor.Type);
                }
                else
                {
                    return(null);
                }

            case DataType.ClrFunction:
                return(value.Callback);

            default:
                throw ScriptRuntimeException.ConvertObjectFailed(value.Type);
            }
        }
예제 #3
0
        /// <summary>
        /// Tries to convert a CLR object to a MoonSharp value, using more in-depth analysis
        /// </summary>
        internal static DynValue StructToDynValue <T>(Script script, T obj)
        {
            var v = UserData.Create(obj);

            if (v.IsValid)
            {
                return(v);
            }
            v = ObjectToDynValue(script, obj);
            if (v.IsValid)
            {
                throw ScriptRuntimeException.ConvertObjectFailed(obj);
            }
            return(v);
        }
예제 #4
0
        internal static object MoonSharpValueToClrObject(DynValue value)
        {
            switch (value.Type)
            {
            case DataType.Void:
            case DataType.Nil:
                return(null);

            case DataType.Boolean:
                return(value.Boolean);

            case DataType.Number:
                return(value.Number);

            case DataType.String:
                return(value.String);

            case DataType.Function:
                return(value.Function);

            case DataType.Table:
                return(value.Table);

            case DataType.Tuple:
                return(value.Tuple);

            case DataType.UserData:
                if (value.UserData.Object != null)
                {
                    return(value.UserData.Object);
                }
                else if (value.UserData.Descriptor != null)
                {
                    return(value.UserData.Descriptor.Type);
                }
                else
                {
                    return(null);
                }

            case DataType.ClrFunction:
                return(value.Callback);

            default:
                throw ScriptRuntimeException.ConvertObjectFailed(value.Type);
            }
        }
예제 #5
0
        /// <summary>
        /// Tries to convert a CLR object to a MoonSharp value, using more in-depth analysis
        /// </summary>
        internal static R_VAL ObjectToValue(RubyState state, object obj)
        {
            R_VAL v = TryObjectToSimpleValue(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(R_VAL.NewNumber(
                           NumericConversions.TypeToDouble(Enum.GetUnderlyingType(obj.GetType()), obj)));
            }

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

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

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

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

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

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

            var enumerator = EnumerationToValue(script, obj);

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


            throw ScriptRuntimeException.ConvertObjectFailed(obj);
        }
예제 #6
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.FixToType(underType, value.Number));
                }
                if (NumericConversions.NumericTypes.Contains(desiredType))
                {
                    return(NumericConversions.FixToType(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);
        }
예제 #7
0
        //TODO use compiled lamda expresssion, which would GREATLY optimize this tree
        //the process of converting
        /// <summary>
        /// Converts a DynValue to a CLR object of a specific type
        /// </summary>
        internal static T DynValueToTypedValue <T>(DynValue value, T defaultValue, bool isOptional)
        {
            var desiredType = typeof(T);

            if (desiredType.IsByRef)
            {
                desiredType = desiredType.GetElementType();
            }

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

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

            if (desiredType == typeof(DynValue))
            {
                return(ValueConverter <DynValue, T> .Instance.Convert(value));
            }

            if (desiredType == typeof(object))
            {
                return((T)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 ((!desiredType.IsValueType) || (nullableType != null))
                {
                    return(default(T));
                }
                break;

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

                    if (isOptional)
                    {
                        return(defaultValue);
                    }
                }
                else
                {
                    return(default(T));
                }
                break;

            case DataType.Boolean:
                if (desiredType == typeof(bool))
                {
                    return(ValueConverter <bool, T> .Instance.Convert(value.Boolean));
                }
                if (stringSubType != StringConversions.StringSubtype.None)
                {
                    str = value.Boolean.ToString();
                }
                break;

            case DataType.Number:
                if (desiredType.IsEnum || NumericConversions.NumericTypes.Contains(desiredType))
                {
                    return(ValueConverter <double, T> .Instance.Convert(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(ValueConverter <Closure, T> .Instance.Convert(value.Function));
                }
                else if (desiredType == typeof(ScriptFunctionDelegate <T>))
                {
                    return(ValueConverter <ScriptFunctionDelegate <T>, T> .Instance.Convert(value.Function.GetDelegate <T>()));
                }
                break;

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

            case DataType.UserData:
                if (value.UserData.HasValue())
                {
                    T t;
                    if (value.UserData.TryGet(out t))
                    {
                        return(t);
                    }
                    if (stringSubType != StringConversions.StringSubtype.None)
                    {
                        str = value.UserData.AsString();
                    }
                }
                break;

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

            case DataType.Tuple:
                break;
            }

            if (stringSubType != StringConversions.StringSubtype.None && str != null)
            {
                return(ValueConverter <object, T> .Instance.Convert(StringConversions.ConvertString(stringSubType, str, desiredType, value.Type)));
            }

            throw ScriptRuntimeException.ConvertObjectFailed(value.Type, desiredType);
        }
        /// <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)
        {
            var v = TryObjectToSimpleDynValue(script, obj);

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

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

            if (obj is Type type)
            {
                v = UserData.CreateStatic(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 d)
            {
                return(DynValue.NewCallback(CallbackFunction.FromDelegate(script, d)));
            }

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

            if (obj is IList list)
            {
                var t = TableConversions.ConvertIListToTable(script, list);
                return(DynValue.NewTable(t));
            }

            if (obj is IDictionary dict)
            {
                var t = TableConversions.ConvertIDictionaryToTable(script, dict);
                return(DynValue.NewTable(t));
            }

            var enumerator = EnumerationToDynValue(script, obj);

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


            throw ScriptRuntimeException.ConvertObjectFailed(obj);
        }
예제 #9
0
        internal static DynValue ClrObjectToComplexMoonSharpValue(Script script, object obj)
        {
            DynValue v = TryClrObjectToSimpleMoonSharpValue(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);
            }

            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 = ConvertIListToTable(script, (System.Collections.IList)obj);
                return(DynValue.NewTable(t));
            }

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

            if (obj is System.Collections.IEnumerable)
            {
                var enumer = (System.Collections.IEnumerable)obj;
                return(EnumerableWrapper.ConvertIterator(script, enumer.GetEnumerator()));
            }

            if (obj is System.Collections.IEnumerator)
            {
                var enumer = (System.Collections.IEnumerator)obj;
                return(EnumerableWrapper.ConvertIterator(script, enumer));
            }

            throw ScriptRuntimeException.ConvertObjectFailed(obj);
        }
예제 #10
0
        internal static object MoonSharpValueToObjectOfType(DynValue value, Type desiredType, object defaultValue)
        {
            if (desiredType == typeof(DynValue))
            {
                return(value);
            }

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

            bool isString        = false;
            bool isStringBuilder = false;
            bool isChar          = false;

            if (desiredType == typeof(string))
            {
                isString = true;
            }
            else if (desiredType == typeof(StringBuilder))
            {
                isStringBuilder = true;
            }
            else if (desiredType == typeof(char) && value.String.Length > 0)
            {
                isChar = true;
            }

            bool   isAnyString = isString || isStringBuilder || isChar;
            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 (desiredType.IsValueType)
                {
                    if (defaultValue != null)
                    {
                        return(defaultValue);
                    }

                    if (nullableType != null)
                    {
                        return(null);
                    }
                }
                else
                {
                    return(defaultValue);
                }
                break;

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

                    if (defaultValue != null)
                    {
                        return(defaultValue);
                    }
                }
                else
                {
                    return(null);
                }
                break;

            case DataType.Boolean:
                if (desiredType == typeof(bool))
                {
                    return(value.Boolean);
                }
                if (isAnyString)
                {
                    str = value.Boolean.ToString();
                }
                break;

            case DataType.Number:
                if (NumericTypes.Contains(desiredType))
                {
                    return(DoubleToType(desiredType, value.Number));
                }
                if (isAnyString)
                {
                    str = value.Number.ToString();
                }
                break;

            case DataType.String:
                if (isAnyString)
                {
                    str = value.String;
                }
                break;

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

            case DataType.ClrFunction:
                if (desiredType == typeof(CallbackFunction))
                {
                    return(value.Callback);
                }
                break;

            case DataType.UserData:
                if (value.UserData.Object != null)
                {
                    if (desiredType.IsInstanceOfType(value.UserData.Object))
                    {
                        return(value.UserData.Object);
                    }
                    if (isAnyString)
                    {
                        str = value.UserData.Object.ToString();
                    }
                }
                break;

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

            case DataType.Tuple:
                break;
            }

            if (str != null)
            {
                if (isString)
                {
                    return(str);
                }
                if (isStringBuilder)
                {
                    return(new StringBuilder(str));
                }
                if (isChar && str.Length > 0)
                {
                    return(str[0]);
                }
            }

            throw ScriptRuntimeException.ConvertObjectFailed(value.Type, desiredType);
        }
예제 #11
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)
        {
            if (obj == null)
            {
                return(DynValue.Nil);
            }
            if (obj is DynValue _dyn)
            {
                return(_dyn);
            }
            if (obj is Task task)
            {
                return(ObjectToDynValue(script, new TaskWrapper(task)));
            }

            DynValue v = TryObjectToSimpleDynValue(script, obj);

            if (v.IsNotNil())
            {
                return(v);
            }

            v = UserData.Create(obj);
            if (v.IsNotNil())
            {
                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.IsNotNil())
            {
                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.IsNotNil())
            {
                return(enumerator);
            }


            throw ScriptRuntimeException.ConvertObjectFailed(obj);
        }
예제 #12
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);
            }

            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);
        }
예제 #13
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));
            }

#if HASDYNAMIC
            var objType = obj.GetType();

#if PCL
            var isTuple = objType.IsGenericType && objType.GetInterfaces().Where(f => f.Name == "ITuple").Count() > 0;
#else
            var isTuple = objType.IsGenericType && objType.GetInterface("ITuple") != null;
#endif

            if (isTuple)
            {
                var args = objType.GetGenericArguments().Length;
                var vals = new DynValue[args];

                for (int i = 0; i < args; i++)
                {
                    var prop = objType.GetProperty("Item" + (i + 1));
                    var val  = prop.GetValue(obj, null);
                    vals[i] = DynValue.FromObject(script, val);
                }

                return(DynValue.NewTupleNested(vals));
            }
#endif

            var enumerator = EnumerationToDynValue(script, obj);
            if (enumerator != null)
            {
                return(enumerator);
            }


            throw ScriptRuntimeException.ConvertObjectFailed(obj);
        }