public bool MatchAndInvoke(string Data, jsObject Args, bool KeyIsWild) { return(ForEach <jsObject>((Name, Handlers) => { var Key = KeyIsWild ? Data : Name; var Wild = KeyIsWild ? Name : Data; var Matches = jsObject.Null; if ((Matches = Key.Match(Wild)) != null) { if (Matches.Count == 0) { Matches = Args; } else { Args.CopyTo(Matches); } Handlers.ForEach <Handler>((Id, Func) => { Func(Matches); }); return true; } return false; })); }
private object LoopNodes <K, V>(jsObject Context, K Key, V Value) { var Var = new jsObject(); Variable.Assign(Context, Var); Var.Set("Key", Key); Var.Set("Value", Value); foreach (var Node in this.Values) { if (Node is Return) { return(Node); } var Ret = GetValue(Node, Context); if (Ret == Break || Ret == Continue) { return(Ret); } } return(null); }
static Library() { Defined = new jsObject <Library>(); TypeLibsByName = new jsObject <Library>(); StaticLibraries = new jsObject <Library>(); TypeLibs = new Dictionary <Type, Library>(); Constructors = new Library(); var LType = typeof(Library); var Assemblies = new Assembly[] { Assembly.GetExecutingAssembly(), Assembly.GetCallingAssembly(), Assembly.GetEntryAssembly() }; foreach (var Asm in Assemblies) { foreach (var Type in Asm.GetTypes()) { if (LType.IsAssignableFrom(Type)) { Activator.CreateInstance(Type); } } } Assemblies = null; }
public static bool EvaluateNode(object Node, jsObject Context) { var Val = GetValue(Node, Context); if (Val == null) { return(false); } if (Val is bool) { return((bool)Val); } else if (Val is string) { return(!string.IsNullOrEmpty((string)Val)); } else if (Val is int) { return((int)Val != 0); } else if (Val is double) { return(!double.IsNaN((double)Val)); } else if (Val is jsObject) { return(!(Val as jsObject).IsEmpty); } return(false); }
public virtual object Evaluate(jsObject Context) { foreach (var Obj in this.Values) { var Result = GetValue(Obj, Context); if (Obj is Return) { return(Result); } if (Obj is Operator) { continue; } if (Obj is Expression) { if (!(Obj is Call)) { if (Result != null) { return(Result); } } } if (Result == Expression.Break || Result == Expression.Continue) { return(Result); } } return(null); }
public void SetCookie(string name, string value, long expire = 0, string path = "", string domain = "", bool secure = false) { var Options = new jsObject(); Options[name] = value; if (expire > 0) { Options["expire"] = DateTime.UtcNow.AddSeconds(expire).HttpTimeString(); } if (!string.IsNullOrEmpty(path)) { Options["path"] = path; } if (!string.IsNullOrEmpty(domain)) { Options["domain"] = domain; } if (secure) { Options["secure"] = "true"; } Result.Cookies[name] = Options; }
public Host(jsObject Base) { if (Base != null) { Base.CopyTo(this); } }
public static object Get(string Key, jsObject Context) { if (Context != null) { return(Context[Key]); } return(null); }
public static object Invoke(Handler Func, jsObject Args) { if (Func == null) { return(null); } return(Func(Args)); }
public IEnumerable <object> Execute(string EventName, jsObject Args) { var List = getObject(EventName); for (int i = 0; i < List.Count; i++) { yield return((List.ElementAt(i).Value as Handler)(Args)); } }
public static void Set(string Key, jsObject Context, object Data) { if (Context == null || string.IsNullOrEmpty(Key)) { return; } Context[Key] = Data; }
public override object Evaluate(jsObject Context) { if (Bool.EvaluateNode(Boolean, Context)) { return base.Evaluate(Context); } else if (Else != null) { return Else.Evaluate(Context); } return null; }
public void Host(string Name, jsObject Info) { var Host = new Host(Info) { Name = Name }; this.Host(Name, Host); }
public void Invoke(string EventName, jsObject Args) { var List = getObject(EventName); if (List != null) { List.ForEach((K, V) => { (V as Handler)(Args); }); } }
public object Evaluate(string Script, params object[] Args) { var Context = new jsObject(Args); if (Parse(Script)) { return(Evaluate(Context)); } return(null); }
public static object Eval(Engine Engine, string Key, jsObject Context) { var Var = Parse(Engine, Key, 0); if (Var != null) { return(Var.Evaluate(Context)); } return(null); }
public void Print(string FileName, jsObject Data) { if (File.Exists(FileName)) { Print( Data.Template( File.ReadAllText(FileName) ) ); } }
public override object Evaluate(jsObject Context) { var Types = GetArgTypes(Context); var Func = SystemFunctions.Get(Type, Name, Types); if (Func != null) { return(Func.Evaluate(Context)); } return(null); }
public static Event.Handler GetFunctionHandler(Function Func, jsObject Context) { if (Func == null) { return(null); } return((Args) => { Function.GetHandlerArguments(Func, Context, Args); return Func.Evaluate(Args); }); }
public void Configure(string FileName) { var Args = new jsObject( "Server", this ); Script.Engine Eng = new Script.Engine(); Eng.Parse( File.ReadAllText(FileName) ); Eng.Evaluate(Args); }
public static Type[] GetArgTypes(jsObject Args) { List <Type> TypeList = new List <System.Type>(); foreach (var Pair in Args) { if (Pair.Key != "this") { TypeList.Add(Pair.Value.GetType()); } } return(TypeList.ToArray()); }
public object Psx(Request Request, string FileName) { var Eng = ScriptCache.Get(FileName); if (Eng == null) { return(null); } var Args = new jsObject( "Request", Request ); return(Eng.Evaluate(Args)); }
public static object Invoke(Handler Func, jsObject Args, params object[] ArgPairs) { if (Func == null) { return(null); } for (int i = 0; i < ArgPairs.Length / 2; i++) { Args[ArgPairs[i].ToString()] = ArgPairs[i + 1]; i++; } return(Func(Args)); }
public static object Eval(string Script, jsObject Context = null) { var Eng = new Engine(); if (Eng.Parse(Script)) { if (Context == null) { Context = new jsObject(); } return(Eng.Evaluate(Context)); } return(null); }
public object Call(jsObject Context, jsObject ArgList, object This = null, Engine Engine = null) { var Args = GetFunctionArguments(this, Context, ArgList); if (!Args.ContainsKey("this")) { Variable.Set( "this", Args, This == null && Object != null ? Object.Evaluate(Context) : This ); } return(this.Evaluate(Args)); }
static void Main(string[] args) { App.Init(Log.Levels.Info, args); var Engine = new Script.Engine(); var Context = new jsObject(); if (File.Exists("Config.psx")) { if (Engine.Parse(File.ReadAllText("Config.psx"))) { Engine.Evaluate(Context); } } App.WaitforExit(); }
public static object[] GetArguments(jsObject Context) { var Args = new object[Context.Count]; var Index = 0; Context.ForEach((Key, Value) => { Node Node = Value as Node; if (Node != null) { Value = Node.GetSystemHandler(); } Args[Index++] = Value; }); return(Args); }
public CustomTypeInstance CreateInstance(jsObject Args) { Function Func; CustomTypeInstance Instance = new CustomTypeInstance(this); Properties.ForEach((K, V) => { V.Evaluate(Instance); }); if (Functions.TryGetValue(ShortName, out Func)) { Args.Set("this", Instance); Func.Evaluate(Args); } return(Instance); }
public virtual void OnClientRequest(Request Request) { var Args = new jsObject( "Server", this, "Request", Request, "FileName", Request.Packet.Target ); if (Request.Host.Handlers.MatchAndInvoke(Request.Packet.Target, Args, true)) { } else if (Handlers.MatchAndInvoke(Request.Packet.Target, Args, true)) { } else { var WWW = Request.Host.GetWWW(Request); Args["FileName"] = WWW; var Ext = Request.Host.GetExtension(WWW); var MIME = GetMime( Ext ); if (!Handlers.MatchAndInvoke(MIME, Args)) { if (Request.Packet.Headers.Get <string>("If-Modified-Since") == File.GetLastWriteTimeUtc(WWW).HttpTimeString()) { Request.Result = Result.NotModified; } else if (File.Exists(WWW)) { DefaultFileHandler(WWW, Request); } else { Request.Result = Result.NotFound; } } } Request.Finish(); }
public static jsObject GetFunctionArguments(Function This, jsObject Context, jsObject ArgList) { var Args = new jsObject(); var Index = 0; ArgList.ForEach((Key, Value) => { var Name = ""; if (This != null) { if (Index >= This.Arguments.Length) { Name = Key; } else { Name = This.Arguments[Index]; } } else { Name = Key; } var VFunc = Value as Function; if (VFunc != null) { if (VFunc.Arguments.Length > 0) { Value = GetFunctionHandler(VFunc, Context); } else { Value = VFunc.GetSystemHandler(); } } Variable.Set(Name, Args, GetValue(Value, Context)); Index++; }); return(Args); }
private dynamic ParseObject() { dynamic obj = new jsObject(); Accept(TokenKind.LBrace); if (currentToken.Kind != TokenKind.RBrace) { var prop = ParseProperty(); obj.__defineProperty__(prop.Key, prop.Value); //obj.__fields__[prop.Key] = prop.Value; } while (currentToken.Kind != TokenKind.RBrace) { Accept(TokenKind.Comma); var prop = ParseProperty(); //obj.__fields__[prop.Key] = prop.Value; obj.__defineProperty__(prop.Key, prop.Value); } Accept(TokenKind.RBrace); return obj; }