示例#1
0
        public bool TryImportScript(string scriptText, UPath scriptPath, IDynamicObject scriptObject, ScriptFlags flags, out object result, LunetTemplateContext context = null)
        {
            if (scriptText == null)
            {
                throw new ArgumentNullException(nameof(scriptText));
            }
            if (scriptPath == null)
            {
                throw new ArgumentNullException(nameof(scriptPath));
            }
            if (scriptObject == null)
            {
                throw new ArgumentNullException(nameof(scriptObject));
            }

            result = null;
            context ??= new LunetTemplateContext(Builtins);

            var scriptResult = ParseScript(scriptText, scriptPath.FullName, ScriptMode.ScriptOnly);

            if (!scriptResult.HasErrors)
            {
                if ((flags & ScriptFlags.AllowSiteFunctions) != 0)
                {
                    context.PushGlobal(SiteFunctions);
                }

                context.PushGlobal((ScriptObject)scriptObject);
                context.PushSourceFile(scriptPath.FullName);
                context.EnableOutput   = false;
                context.TemplateLoader = (flags & ScriptFlags.AllowSiteFunctions) != 0 ? unauthorizedTemplateLoader : new TemplateLoaderFromIncludes(Site);

                try
                {
                    result = scriptResult.Template.Evaluate(context);
                }
                catch (ScriptRuntimeException exception)
                {
                    LogException(exception);
                    return(false);
                }
                finally
                {
                    context.PopSourceFile();
                    context.PopGlobal();
                    if ((flags & ScriptFlags.AllowSiteFunctions) != 0)
                    {
                        context.PopGlobal();
                    }
                }
                return(true);
            }
            return(false);
        }
示例#2
0
        public LunetTemplateContext CreatePageContext()
        {
            var context = new LunetTemplateContext(Builtins);

            context.PushGlobal(Site.Builtins);
            return(context);
        }