Esempio n. 1
0
        public static object ExeConfig(Models.Code code, WebSite site)
        {
            RenderContext context = new RenderContext();

            context.WebSite = site;
            var engine   = new Jint.Engine(JintSetting.SetOption);
            var kcontext = new k(context);

            engine.SetValue("k", kcontext);

            var k = engine.GetValue("k");

            kcontext.ReturnValues.Clear();

            try
            {
                engine.ExecuteWithErrorHandle(code.Config, new Jint.Parser.ParserOptions()
                {
                    Tolerant = true
                });
            }
            catch (System.Exception ex)
            {
                Kooboo.Data.Log.Instance.Exception.WriteException(ex);
            }

            if (kcontext.ReturnValues.Count() > 0)
            {
                return(kcontext.ReturnValues.Last());
            }
            else
            {
                var obj = EngineConfigReturnObject(engine);
                return(obj);
            }
        }
Esempio n. 2
0
        public static string ExecuteInnerScript(RenderContext context, string InnerJsCode)
        {
            if (string.IsNullOrEmpty(InnerJsCode))
            {
                return(null);
            }

            Jint.Engine engine = null;

            var debugsession = Kooboo.Sites.ScriptDebugger.SessionManager.GetDebugSession(context, InnerJsCode);

            if (debugsession == null)
            {
                engine = Kooboo.Sites.Scripting.Manager.GetJsEngine(context);
            }
            else
            {
                engine = Kooboo.Sites.Scripting.Manager.GetDebugJsEngine(context);

                foreach (var item in debugsession.BreakLines)
                {
                    engine.BreakPoints.Add(new Jint.Runtime.Debugger.BreakPoint(item, 0));
                }

                debugsession.JsEngine = engine;

                engine.Break += (s, e) => Engine_Break(s, e, debugsession);

                engine.Step += (s, e) => Engine_Step(s, e, debugsession);
            }
            try
            {
                engine.ExecuteWithErrorHandle(InnerJsCode, new Jint.Parser.ParserOptions()
                {
                    Tolerant = true
                });
            }
            catch (Exception ex)
            {
                Kooboo.Data.Log.Instance.Exception.WriteException(ex);
                return(ex.Message);
            }

            if (debugsession != null)
            {
                var info = new ScriptDebugger.DebugInfo
                {
                    Start = new Jint.Parser.Position()
                    {
                        Line = -1
                    },
                    End = new Jint.Parser.Position()
                    {
                        Line = -1
                    },
                    EndOfExe = true,
                    HasValue = true
                };
                debugsession.DebugInfo = info;
            }

            return(ReturnValue(context, engine));
        }
Esempio n. 3
0
        public static object ExecuteDataSource(RenderContext context, Guid CodeId, Dictionary <string, object> parameters)
        {
            var sitedb = context.WebSite.SiteDb();

            var code = sitedb.Code.Get(CodeId);

            if (code == null)
            {
                return(null);
            }

            object result = null;

            Jint.Engine engine = null;

            var debugsession = Kooboo.Sites.ScriptDebugger.SessionManager.GetDebugSession(context, code.Id);

            if (debugsession == null)
            {
                engine = Kooboo.Sites.Scripting.Manager.GetJsEngine(context);
            }
            else
            {
                engine = Kooboo.Sites.Scripting.Manager.GetDebugJsEngine(context);

                foreach (var item in debugsession.BreakLines)
                {
                    engine.BreakPoints.Add(new Jint.Runtime.Debugger.BreakPoint(item, 0));
                }

                debugsession.JsEngine = engine;

                engine.Break += (s, e) => Engine_Break(s, e, debugsession);

                engine.Step += (s, e) => Engine_Step(s, e, debugsession);
            }

            try
            {
                var kcontext = context.GetItem <k>();

                Dictionary <string, string> config = new Dictionary <string, string>();
                if (parameters != null)
                {
                    foreach (var item in parameters)
                    {
                        if (item.Value != null)
                        {
                            config.Add(item.Key, item.Value.ToString());
                        }
                        else
                        {
                            config.Add(item.Key, null);
                        }
                    }
                }

                kcontext.config = new KDictionary(config);
                kcontext.ReturnValues.Clear();

                engine.ExecuteWithErrorHandle(code.Body, new Jint.Parser.ParserOptions()
                {
                    Tolerant = true
                });

                kcontext.config = null;

                if (kcontext.ReturnValues.Count > 0)
                {
                    result = kcontext.ReturnValues.Last();
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }

            if (debugsession != null)
            {
                var info = new ScriptDebugger.DebugInfo
                {
                    Start = new Jint.Parser.Position()
                    {
                        Line = -1
                    },
                    End = new Jint.Parser.Position()
                    {
                        Line = -1
                    },
                    EndOfExe = true,
                    HasValue = true
                };
                debugsession.DebugInfo = info;
            }

            return(result);
        }
Esempio n. 4
0
        public static string ExecuteCode(RenderContext context, string JsCode, Guid CodeId = default(Guid))
        {
            if (string.IsNullOrEmpty(JsCode))
            {
                return(null);
            }

            Jint.Engine engine = null;

            var debugsession = Kooboo.Sites.ScriptDebugger.SessionManager.GetDebugSession(context, CodeId);

            if (debugsession == null)
            {
                engine = Kooboo.Sites.Scripting.Manager.GetJsEngine(context);
            }
            else
            {
                engine = Kooboo.Sites.Scripting.Manager.GetDebugJsEngine(context);

                foreach (var item in debugsession.BreakLines)
                {
                    engine.BreakPoints.Add(new Jint.Runtime.Debugger.BreakPoint(item, 0));
                }

                debugsession.JsEngine = engine;

                engine.Break += (s, e) => Engine_Break(s, e, debugsession);

                engine.Step += (s, e) => Engine_Step(s, e, debugsession);
            }
            try
            {
                engine.ExecuteWithErrorHandle(JsCode, new Jint.Parser.ParserOptions()
                {
                    Tolerant = true
                });
            }
            catch (Exception ex)
            {
                Kooboo.Data.Log.Instance.Exception.WriteException(ex);

                if (debugsession != null)
                {
                    var info = new ScriptDebugger.DebugInfo
                    {
                        Start = new Jint.Parser.Position()
                        {
                            Line = -1
                        },
                        End = new Jint.Parser.Position()
                        {
                            Line = -1
                        },
                        EndOfExe    = true,
                        HasValue    = true,
                        IsException = true
                    };

                    if (ex is JavaScriptException)
                    {
                        var jsex = ex as JavaScriptException;
                        info.Start.Line = jsex.LineNumber;
                        info.End.Line   = jsex.LineNumber;

                        info.Message = "Line " + jsex.LineNumber.ToString() + " " + jsex.Error.ToString() + " " + jsex.Message + " " + jsex.Source;
                    }
                    else
                    {
                        info.Message = ex.Message + ex.StackTrace;
                    }

                    debugsession.DebugInfo = info;
                }

                return(ex.Message);
            }

            if (debugsession != null)
            {
                if (debugsession.DebugInfo != null)
                {
                    debugsession.DebugInfo.EndOfExe = true;
                }
                else
                {
                    var info = new ScriptDebugger.DebugInfo
                    {
                        Start = new Jint.Parser.Position()
                        {
                            Line = -1
                        },
                        End = new Jint.Parser.Position()
                        {
                            Line = -1
                        },
                        EndOfExe = true,
                        HasValue = true
                    };
                    debugsession.DebugInfo = info;
                }
            }

            return(ReturnValue(context, engine));
        }