Esempio n. 1
0
 public static LuaValue Wrap(LuaValue[] args)
 {
     LuaFunction f = args[0] as LuaFunction;
     LuaCoroutine c = new LuaCoroutine(f);
     LuaFunction f2 = new LuaFunction(new LuaFunc(delegate(LuaValue[] args2) { return LuaBoolean.From(c.Resume(args2)); }));
     return f2;
 }
Esempio n. 2
0
        public static LuaValue Lines(LuaValue[] values)
        {
            LuaUserdata data = values[0] as LuaUserdata;
            TextReader reader = data.Value as TextReader;

            LuaFunction func = new LuaFunction((LuaValue[] args) =>
                {
                    LuaUserdata _data = values[0] as LuaUserdata;
                    TextReader _reader = data.Value as TextReader;

                    string line = _reader.ReadLine();

                    if (line != null)
                    {
                        return new LuaString(line);
                    }
                    else
                    {
                        return LuaNil.Nil;
                    }
                }
            );

            return new LuaMultiValue(new LuaValue[] { func, data, LuaNil.Nil });
        }
Esempio n. 3
0
        public LuaClass(string name, bool final, bool _static)
        {
            this.Name = Rename(name);
            this.Final = final;
            this.Static = _static;

            this.IndexFunction = new LuaFunction(new LuaFunc((LuaValue[] args) =>
                                                             {return null; }));
            this.CallFunction = new LuaFunction(new LuaFunc((LuaValue[] args) =>
                                                            {return null; }));
            this.NewIndexFunction = new LuaFunction(new LuaFunc((LuaValue[] args) =>
                                                                {return null; }));
            this.ToStringFunction = new LuaFunction(new LuaFunc((LuaValue[] args) =>
                                                                {return new LuaString("Lua Class: " + GetHashCode() + ", Name: " + Name); }));
            this.Constructor = new LuaFunction(new LuaFunc((LuaValue[] args) =>
                                                           {
                                                               // do nothing
                                                               return LuaNil.Nil;
                                                           }));
            this.Destructor = new LuaFunction(new LuaFunc((LuaValue[] args) =>
                                                          {
                                                              // do nothing
                                                              return LuaNil.Nil;
                                                          }));
            this.Self = new LuaTable();
            Self.Register("new", New);
            Self.Register("Set", Set);
            Self.Register("HasMember", HasMember);
            Self.Register("Inherits", Inherits);
            Self.Register("IsParentOf", IsParentOf);
            Self.Register("GetParentClasses", GetParentClasses);
            Self.Register("GetChildClasses", GetChildClasses);
            Self.Register("CallParentMethod", CallParentMethod);
            Self.Register("CreateSubclass", CreateSubClass);
            Self.Register("CallMethod", CallMethod);
            Self.Register("GetTable", GetTable);
            GenerateMetaTable();
        }
Esempio n. 4
0
        public static LuaValue IPairs(LuaValue[] values)
        {
            LuaTable table = values[0] as LuaTable;
            LuaFunction func = new LuaFunction(
                (LuaValue[] args) =>
                {
                    LuaTable tbl = args[0] as LuaTable;
                    int index = (int)(args[1] as LuaNumber).Number;
                    int nextIndex = index + 1;

                    if (nextIndex <= tbl.Count)
                    {
                        return new LuaMultiValue(new LuaValue[] { new LuaNumber(nextIndex), tbl.GetValue(nextIndex) });
                    }
                    else
                    {
                        return LuaNil.Nil;
                    }
                }
               );

            return new LuaMultiValue(new LuaValue[] { func, table, new LuaNumber(0) });
        }
Esempio n. 5
0
        public void GenerateMetaTable()
        {
            Self.MetaTable = new LuaTable();
            Self.MetaTable.Register("__index",new LuaFunc(delegate(LuaValue[] args)
                                                          { //(table, key)
                                                              LuaValue key = args[0];
                                                              IndexFunction.Invoke(new LuaValue[] {Self, key}); // user defined __index function
                                                              // attempt to get from parents also
                                                              return GetObject(key, this); //CallMethod(new LuaValue[] {key});
                                                          }));

            Self.MetaTable.Register("__call", new LuaFunc(delegate(LuaValue[] args)
                                                          { //(func, ...)
                                                              if (args.Length == 0)
                                                                  return LuaNil.Nil;
                                                              List<LuaValue> args2 = new List<LuaValue>();
                                                              foreach (LuaValue a in args)
                                                                  args2.Add(a);
                                                              args2.RemoveAt(0);
                                                              CallFunction.Invoke(new LuaValue[] {args[0], new LuaMultiValue(args2.ToArray())}); // user defined __call function
                                                              return CallMethod(new LuaValue[] {args[0], new LuaUserdata(args2.ToArray())}); // call function
                                                          }));

            Self.MetaTable.Register("__newindex",new LuaFunc(delegate(LuaValue[] args)
                                                             {
                                                                 LuaValue key = args[1];
                                                                 LuaValue value = args[2];
                                                                 //(table, key, value)
                                                                 NewIndexFunction.Invoke(new LuaValue[] {Self, key, value}); // user defined __newindex function
                                                                 // check for user defined "metamethods"
                                                                 string obj = key.Value.ToString();
                                                                 if (obj == "__index")
                                                                 {
                                                                     // assign this to the __indexfunction variable
                                                                     IndexFunction = value as LuaFunction;
                                                                 }
                                                                 else if (obj == "__newindex")
                                                                     NewIndexFunction = value as LuaFunction;
                                                                 else if (obj == "__call")
                                                                     CallFunction = value as LuaFunction;
                                                                 else if (obj == "__tostring")
                                                                     ToStringFunction = value as LuaFunction;
                                                                 else if (obj == "final")
                                                                     Final = true;
                                                                 else if (obj == "static")
                                                                     Static = true;
                                                                 else if (obj == "name")
                                                                     Name = Rename(value.Value.ToString());
                                                                 else if (obj == "constructor")
                                                                     Constructor = (LuaFunction) value;
                                                                 else if (obj == "destructor")
                                                                     Destructor = (LuaFunction) value;
                                                                 else // its a normal key/value pair
                                                                     Self.SetKeyValue(key, value);
                                                                 return LuaNil.Nil;
                                                             }));
            Self.MetaTable.Register("__tostring", new LuaFunc(delegate(LuaValue[] args)
                                                              {
                                                                  return ToStringFunction.Invoke(new LuaValue[] {});
                                                              }));
            this.MetaTable = Self.MetaTable;
        }
Esempio n. 6
0
 public static LuaValue Pairs(LuaValue[] values)
 {
     LuaTable table = values[0] as LuaTable;
     LuaFunction func = new LuaFunction(Next);
     return new LuaMultiValue(new LuaValue[] { func, table, LuaNil.Nil });
 }
Esempio n. 7
0
 public LuaCoroutine(LuaFunction f)
 {
     this.func = f;
     _status = "normal";
 }
Esempio n. 8
0
 public static LuaValue IterateParentClasses(LuaValue[] args)
 {
     LuaClass _class = args[0] as LuaClass;
     LuaFunction f = new LuaFunction(NextClassIterator);
     return new LuaMultiValue(new LuaValue[] { f, _class.GetParentClasses(new LuaValue[] {}), LuaNil.Nil });
 }
Esempio n. 9
0
 public static LuaValue Lines(LuaValue[] args)
 {
     LuaFunction f = new LuaFunction(NextLine);
     return new LuaMultiValue(new LuaValue[] { f, GetLines(args), LuaNil.Nil });
 }
Esempio n. 10
0
        public static LuaValue GetControlCreator(LuaValue[] values)
        {
            LuaString typeString = values[0] as LuaString;
            string typeName = "System.Windows.Forms." + typeString.Text;
            Type type = Assembly.GetAssembly(typeof(Application)).GetType(typeName, true, true);

            LuaFunction func = new LuaFunction((LuaValue[] args) =>
                                               {
                                                   object control = Activator.CreateInstance(type);
                                                   LuaTable table = args[0] as LuaTable;
                                                   string name = null;
                                                   
                                                   if (table.Count > 0)
                                                   {
                                                       AddChildControls(control, table);
                                                   }

                                                   if (table.Count > 0)
                                                   {
                                                       foreach (var pair in table.KeyValuePairs)
                                                       {
                                                           string member = (pair.Key as LuaString).Text;

                                                           if (member == "Name")
                                                           {
                                                               name = (string)pair.Value.Value;
                                                               continue;
                                                           }

                                                           SetMemberValue(control, type, member, pair.Value.Value);
                                                       }
                                                   }

                                                   LuaUserdata data = new LuaUserdata(control);
                                                   data.MetaTable = GetControlMetaTable();

                                                   if (name != null)
                                                   {
                                                       LuaTable enviroment = currentModule.GetValue("_G") as LuaTable;
                                                       enviroment.SetNameValue(name, data);
                                                   }

                                                   return data;
                                               }
                                              );

            currentModule.SetNameValue(typeString.Text, func);
            return func;
        }
Esempio n. 11
0
 public static LuaValue GMatch(LuaValue[] args)
 {
     LuaFunction f = new LuaFunction(NextGMatch);
     return new LuaMultiValue(new LuaValue[] { f, GetGMatches(args), LuaNil.Nil });
 }
Esempio n. 12
0
 public LuaFunction Register(string name, LuaFunc function)
 {
     LuaFunction luaFunc = new LuaFunction(function);
     this.SetNameValue(name, luaFunc);
     return luaFunc;
 }
Esempio n. 13
0
 public void Sort(LuaFunction compare)
 {/*
     this.list.Sort((a, b) =>
                    {
                        LuaValue result = compare.Invoke(new LuaValue[] { a, b });
                        LuaBoolean boolValue = result as LuaBoolean;
                        if (boolValue != null && boolValue.BoolValue == true)
                        {
                            return 1;
                        }
                        else
                        {
                            return -1;
                        }
                    });*/
 }
Esempio n. 14
0
 public LuaCoroutine(LuaFunction f)
 {
     this.func = f;
     _status   = "normal";
 }