public static bool TryGetPage(this ScriptScopeContext scope, string virtualPath, out SharpPage page, out SharpCodePage codePage) { if (scope.PageResult.Partials.TryGetValue(virtualPath, out page)) { codePage = null; return(true); } if (!scope.Context.TryGetPage(scope.PageResult.VirtualPath, virtualPath, out page, out codePage)) { return(false); } codePage?.Init(); if (codePage is IRequiresRequest requiresRequest) { if (scope.GetValue(ScriptConstants.Request) is IRequest request) { requiresRequest.Request = request; } } return(true); }
public async Task WriteAsync(ScriptScopeContext scope) { var renderParams = renderMethod.GetParameters(); var args = new object[renderParams.Length]; Dictionary <string, string> requestParams = null; for (var i = 0; i < renderParams.Length; i++) { var renderParam = renderParams[i]; var arg = scope.GetValue(renderParam.Name); if (arg == null) { if (requestParams == null) { requestParams = (scope.GetValue("Request") as Web.IRequest)?.GetRequestParams(); } if (requestParams != null && requestParams.TryGetValue(renderParam.Name, out var reqParam)) { arg = reqParam; } } args[i] = arg; } try { var result = renderInvoker(this, args); if (result != null) { var str = result.ToString(); await scope.OutputStream.WriteAsync(str); } } catch (Exception ex) { throw new TargetInvocationException($"Failed to invoke render method on {GetType().Name}", ex); } }
public override object Evaluate(ScriptScopeContext scope) { var rhs = Right.Evaluate(scope); if (Left is JsIdentifier id) { if (scope.ScopedParams.ContainsKey(id.Name)) { scope.ScopedParams[id.Name] = rhs; } else { scope.PageResult.Args[id.Name] = rhs; } return(rhs); } else if (Left is JsMemberExpression memberExpr) { if (memberExpr.Object is JsIdentifier targetId) { var target = scope.GetValue(targetId.Name); if (target == null) { throw new ArgumentNullException(targetId.Name); } if (assignFn == null) { if (memberExpr.Property is JsIdentifier propName) { assignFn = scope.Context.GetAssignExpression( target.GetType(), (targetId.Name + "." + propName.Name).AsMemory()); } else { var strExpr = memberExpr.ToRawString(); assignFn = scope.Context.GetAssignExpression(target.GetType(), strExpr.AsMemory()); } } if (assignFn != null) { assignFn(scope, target, rhs); return(rhs); } } } throw new NotSupportedException("Assignment Expression not supported: " + Left.ToRawString()); }
public static bool TryGetMethod(this ScriptScopeContext scope, string name, int fnArgValuesCount, out Delegate fn, out ScriptMethods scriptMethod, out bool requiresScope) { scriptMethod = null; requiresScope = false; var result = scope.PageResult; fn = scope.GetValue(name) as Delegate; if (fn == null) { fn = result.GetFilterInvoker(name, fnArgValuesCount, out scriptMethod); } if (fn == null) { fn = result.GetContextFilterInvoker(name, fnArgValuesCount + 1, out scriptMethod); if (fn == null) { var contextFilter = result.GetContextBlockInvoker(name, fnArgValuesCount + 1, out scriptMethod); if (contextFilter != null) { // Other languages require captured output of Context Blocks var filter = scriptMethod; fn = (StaticMethodInvoker)(args => { var ctxScope = (ScriptScopeContext)args[0]; using (var ms = MemoryStreamFactory.GetStream()) { args[0] = ctxScope.ScopeWithStream(ms); var task = (Task)contextFilter(filter, args); task.Wait(); var discard = task.GetResult(); var ret = MemoryProvider.Instance.FromUtf8(ms.GetBufferAsMemory().Span); return(ret.ToString()); } }); } } if (fn != null) { requiresScope = true; } } return(fn != null); }
public bool startsWithPathInfo(ScriptScopeContext scope, string pathInfo) => pathInfo == "/" ? matchesPathInfo(scope, pathInfo) : scope.GetValue("PathInfo")?.ToString().TrimEnd('/').StartsWith(pathInfo?.TrimEnd('/') ?? "") == true;
public bool matchesPathInfo(ScriptScopeContext scope, string pathInfo) => scope.GetValue("PathInfo")?.ToString().TrimEnd('/') == pathInfo?.TrimEnd('/');
internal IHttpRequest req(ScriptScopeContext scope) => scope.GetValue("Request") as IHttpRequest;
public string httpPathInfo(ScriptScopeContext scope) => scope.GetValue("PathInfo")?.ToString();
internal IRequest req(ScriptScopeContext scope) => scope.GetValue(ScriptConstants.Request) as IRequest;