/// <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 value)
            {
                return(value);
            }

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

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

            return(null);
        }
        /// <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);
        }
        /// <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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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.IsValid)
                {
                    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(DynValue.Invalid);
        }
Ejemplo n.º 6
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);
        }