Пример #1
0
        public static LuaValue ToLuaValue(this object obj, ScriptContext context)
        {
            if (obj is LuaValue)
            {
                return((LuaValue)obj);
            }

            if (obj == null)
            {
                return(LuaNil.Instance);
            }

            if (obj is double)
            {
                return((LuaValue)(double)obj);
            }

            if (obj is int)
            {
                return((LuaValue)(int)obj);
            }

            if (obj is bool)
            {
                return((LuaValue)(bool)obj);
            }

            if (obj is string)
            {
                return((LuaValue)(string)obj);
            }

            if (obj is IScriptBindable)
            {
                // Object needs additional notification / context
                var notify = obj as IScriptNotifyBind;
                if (notify != null)
                {
                    notify.OnScriptBind(context);
                }

                return(new LuaCustomClrObject(obj));
            }

            if (obj is Array)
            {
                var array = obj as Array;
                var i     = 1;
                var table = context.CreateTable();
                foreach (var x in array)
                {
                    table.Add(i++, x.ToLuaValue(context));
                }

                return(table);
            }

            throw new InvalidOperationException("Cannot convert type '{0}' to Lua. Class must implement IScriptBindable.".F(obj.GetType()));
        }
Пример #2
0
        public static LuaTable ToLuaTable(this IEnumerable collection, ScriptContext context)
        {
            var i     = 1;
            var table = context.CreateTable();

            foreach (var x in collection)
            {
                table.Add(i++, x.ToLuaValue(context));
            }
            return(table);
        }