public void Handle(HttpContext context) { JSApplication application = null; if (context.Application != null) { application = (JSApplication)context.Application["JSApplication"]; } if (application == null) { application = new JSApplication(AppDomain.CurrentDomain.BaseDirectory); } var mainTemplate = application.Settings.Root + application.Settings.TemplateFolder + application.Settings.Entry; if (mainTemplate.EndsWith(".js")) { ProcessRequest(context); } else if (mainTemplate.EndsWith(".xdoc")) { application.XDocService.ProcessRequest(context); } if (context.Application != null) { context.Application["JSApplication"] = application; } }
public void ProcessRequest(HttpContext context) { JSApplication application = null; JSSession session = null; if (context.Application != null) { application = (JSApplication)context.Application["JSApplication"]; } if (application == null) { application = new JSApplication(AppDomain.CurrentDomain.BaseDirectory); } if (context.Session != null) { session = (JSSession)context.Session["JSSession"]; } if (session == null) { session = new JSSession(); } ProcessRequest(context, application, session); if (context.Application != null) { context.Application["JSApplication"] = application; } if (context.Session != null) { context.Session["JSSession"] = session; } }
public string RunTemplate(string template, string data, ref XHTMLMerge.SVCache svCache) { var application = new JSApplication(); var session = new JSSession(); return(RunTemplate(template, data, ref application, ref session, ref svCache)); }
public void ProcessRequest(HttpContext context, JSApplication application, JSSession session) { XHTMLMerge.SVCache svCache = null; var path = GetPath(context.Request); var arguments = Core.Tool.Construct("Object", application.Engine.Scope); arguments.Set("request", CreateRequest(context, path, application.Engine.Scope)); var response = Core.Tool.Construct("Object", application.Engine.Scope); response.Set("contentType", new Core.Javascript.String("text/html")); response.Set("statusCode", new Core.Javascript.Number(200)); arguments.Set("response", response); var responseString = RunTemplate(application.Settings.Entry, arguments, ref application, ref session, ref svCache); try { context.Response.ContentType = response.Get <Core.Javascript.String>("contentType").Value; context.Response.StatusCode = (int)response.Get <Core.Javascript.Number>("statusCode").Value; }catch (Exception) { // TODO: log error } context.Response.AppendHeader("Access-Control-Allow-Origin", "*"); context.Response.AppendHeader("Access-Control-Allow-Headers", "X-Requested-With"); var buffer = Encoding.UTF8.GetBytes(responseString); var output = context.Response.OutputStream; output.Write(buffer, 0, buffer.Length); output.Close(); }
public void Set() { Request = _request; Application = _application; Session = _session; Buffer = _buffer; }
// Every template is executed via this function public string RunTemplate(string template, Core.Javascript.Object arguments, ref JSApplication application, ref JSSession session, ref XHTMLMerge.SVCache svCache) { try { if (arguments == null) { arguments = Core.Tool.Construct("Object", application.Engine.Scope); } var scope = new Scope(application.Engine.Scope, null, ScopeType.Session); scope.SetVariable("__application__", new Foreign(application)); scope.SetVariable("__session__", new Foreign(session)); scope.SetVariable("__svCache__", new Foreign(svCache)); // TODO: better way to forward session var result = External.Functions.include( Static.Undefined, new Constant[] { new Core.Javascript.String(template), arguments }, scope ); if (result is Core.Javascript.String s) { return(s.Value); } } catch (Error e) { return(e.Message + "\n" + string.Join("\n", e.StackTrace.Select(loc => Debug.GetFileName(loc.FileId) + " (" + loc.LineNr + ")"))); } catch (Exception e) { return("System error - " + e.ToString()); } return(""); }
public Block GetFile(string name, JSApplication application) { var path = name; if (!System.IO.Path.IsPathRooted(path)) { path = application.Settings.Root + application.Settings.TemplateFolder + path; } var key = Core.Tool.NormalizePath(path); if (Files.ContainsKey(key)) { var modified = System.IO.File.GetLastWriteTime(path); if (modified > Files[key].LastModified) { Files[key] = LoadFile(path); } return(Files[key].Node); } else { var sourceFile = LoadFile(path); Files[key] = sourceFile; return(sourceFile.Node); } }
public State(Request request, JSApplication application, JSSession session, StringBuilder buffer) { _request = request; _application = application; _session = session; _buffer = buffer; }
public string RunTemplate(string template, string data, ref JSApplication application, ref JSSession session, ref XHTMLMerge.SVCache svCache) { try { dynamic arguments = new NetJSObject(); return(RunTemplate(template, arguments, ref application, ref session, ref svCache)); } catch { } return("invalid arguments (must be valid json)"); }
public FunctionRequest(dynamic function, JSApplication application, Action <object> resultCallback, JSSession session = null, object argument0 = null, object argument1 = null, object argument2 = null, object argument3 = null) { _function = function; _argument0 = argument0; _argument1 = argument1; _argument2 = argument2; _argument3 = argument3; State = new State(this, application, session ?? new JSSession(), new StringBuilder()); ResultCallback = resultCallback; }
public string RunTemplate(string template, string data, ref JSApplication application, ref JSSession session, ref XHTMLMerge.SVCache svCache) { var arguments = JSON.parse(null, new[] { new Core.Javascript.String(data) }, application.Engine.Scope); if (arguments is Core.Javascript.Object a) { return(RunTemplate(template, a, ref application, ref session, ref svCache)); } return("invalid arguments (must be valid json)"); }
public void RunCode(string code, JSApplication application, JSSession session, Action <object> resultCallback) { var request = new ScriptRequest( application.Compile(code), application, resultCallback, session ); application.AddRequest(request); }
public void RunScriptSync(string template, JSApplication application, JSSession session, Action <object> resultCallback) { var request = new ScriptRequest( application.Cache.GetScript(template, application), application, resultCallback, session ); request.Call(); }
public string RunTemplate(string template, Core.Javascript.Object arguments, ref JSApplication application) { if (application == null) { application = new JSApplication(); } var session = new JSSession(); var svCache = new XHTMLMerge.SVCache(); return(RunTemplate(template, arguments, ref application, ref session, ref svCache)); }
public V8Script GetScript(string name, JSApplication application) { var path = GetPath(name, application, true); try { var source = ReadFile(path); return(application.Compile(path, source)); } catch (System.IO.IOException) { application.Error(new IOError($"Could not load file '{path}'"), ErrorStage.Compilation); } catch (Exception e) { application.Error(e, ErrorStage.Compilation); } // Return an empty script to not block rest of execution return(application.Compile(path, "")); }
public dynamic GetResource(string name, bool returnVar, JSApplication application) { var path = GetPath(name, application, true); try { var source = ReadFile(path); var code = Transpiler.TranspileTemplate(source, returnVar); var script = application.Compile(path, code); var function = application.Evaluate(script); return(function); } catch (System.IO.IOException) { application.Error(new IOError($"Could not load file '{path}'"), ErrorStage.Compilation); } catch (Exception e) { application.Error(e, ErrorStage.Compilation); } // Return an empty function to not block rest of execution var c = Transpiler.TranspileTemplate("", returnVar); var s = application.Compile(name, c); var f = application.Evaluate(s); return(f); }
public string GetPath(string name, JSApplication application, bool inSource) { var path = name; if (path.StartsWith("/")) { path = application.Settings.Root + (inSource ? application.Settings.TemplateFolder : "") + path; } else if (!System.IO.Path.IsPathRooted(path)) { var currentLocation = GetCurrentLocation(); if (currentLocation.Length == 0) { path = application.Settings.Root + (inSource ? application.Settings.TemplateFolder : "") + path; } else { var nameParts = currentLocation.Split(new[] { '/', '\\' }); path = string.Join("/", nameParts.Take(nameParts.Length - 1)) + "/" + name; } } return(path); }
public string RunTemplate(string template, string data, ref JSApplication application, ref JSSession session) { var svCache = new XHTMLMerge.SVCache(); return(RunTemplate(template, data, ref application, ref session, ref svCache)); }
// Every template is executed via this function public string RunTemplate(string template, Dictionary <string, object> arguments, ref JSApplication application, ref JSSession session, ref XHTMLMerge.SVCache svCache) { try { if (arguments == null) { arguments = new Dictionary <string, object>(); } NetJS.API.XDoc.SetXDocInfo(new API.XDoc.XDocInfo() { AppCache = null, SVCache = svCache }); // TODO: better way to forward session dynamic args = new NetJSObject(); foreach (var pair in arguments) { args[pair.Key] = pair.Value; } var result = API.Functions.include(template, args); return(result.ToString()); } catch (ScriptEngineException se) { return(se.ErrorDetails); } catch (Error e) { return(e.ToString()); } catch (Exception e) { return("System error - " + e.ToString()); } }
public ScriptRequest(Microsoft.ClearScript.V8.V8Script script, JSApplication application, Action <object> resultCallback, JSSession session = null) { Script = script; State = new State(this, application, session ?? new JSSession(), new StringBuilder()); ResultCallback = resultCallback; }