Ejemplo n.º 1
0
        public void PreloadingTest()
        {
            string[] lines = new[] {
                "CHAT c1",
                "SET ab = hello",
                "SAY $ab $de",

                "CHAT c2 {preload=true}",
                "SET $de = preload",
            };
            ChatRuntime rt = new ChatRuntime();

            rt.ParseText(String.Join("\n", lines), true);
            rt.Preload(globals);

            var s = rt.InvokeImmediate(globals);

            Assert.That(s, Is.EqualTo("hello preload"));
        }
Ejemplo n.º 2
0
        public void PreloadingBindingFunc()
        {
            string[] lines = new[] {
                "CHAT c1",
                "SAY $d $e",

                "CHAT c2 {preload=true}",
                "SET $d = hello",
                "SET $e = $emotion.Cap()",
            };
            ChatRuntime rt = new ChatRuntime();

            rt.ParseText(String.Join("\n", lines), true);
            rt.Preload(globals);

            globals.Add("emotion", "darkness");

            var s = rt.InvokeImmediate(globals);

            Assert.That(s, Is.EqualTo("hello Darkness"));
        }
Ejemplo n.º 3
0
        //public static string Validate(IDictionary<string, string> kvs)
        //{
        //    return ParseScript(kvs).ToJSON();
        //}

        public static string Execute(IDictionary <string, string> kvs)
        {
            var parsed = ParseScript(kvs);

            if (parsed.Status != Result.OK)
            {
                return(parsed.ToJSON());
            }

            var result = string.Empty;

            try
            {
                runtime.Preload(globals);
                result = runtime.InvokeImmediate(globals);
                // result = WebUtility.HtmlEncode(result);
                //result = result.Replace("\"", "\\\""); // yuck
                //result = result.Replace("\n", "\\n"); // yuck
            }
            catch (Exception e)
            {
                Console.WriteLine("GOT ERROR");
                result = WebUtility.HtmlEncode(result);
                //result = result.Replace("\"", "\\\""); // yuck
                //result = result.Replace("\n", "\\n"); // yuck
                return(Result.Error(e.Message).ToJSON());
            }

            if (result.IsNullOrEmpty())
            {
                result = " ";
            }

            //result = result.Replace("\"", "\\\""); // yuck
            //result = result.Replace("\n", "\\n"); // yuck


            return(Result.Success(result).ToJSON());
        }
Ejemplo n.º 4
0
        public static string SendEditorResponse(HttpListenerRequest request)
        {
            var html = PageContent.Replace("%%URL%%", SERVER_URL);
            var wmsg = "Enter your script here";

            IDictionary <string, string> kvs = ParsePostData(request);
            var path = kvs.ContainsKey("path") ? kvs["path"] : null;
            var code = kvs.ContainsKey("code") ? kvs["code"] : null;
            var mode = kvs.ContainsKey("mode") ? kvs["mode"] : "validate";

            // fetch code from file
            if (!string.IsNullOrEmpty(path))
            {
                code = new WebClient().DownloadString(path);
            }

            // default info message
            if (string.IsNullOrEmpty(code))
            {
                return(html.Replace("%%CODE%%", wmsg));
            }

            html = html.Replace("%%CODE%%", WebUtility.HtmlEncode(code));
            html = html.Replace("%%CCLASS%%", "shown");

            // only process the selection if there is one
            if (kvs.ContainsKey("selectionStart"))
            {
                code = kvs["selection"];
                html = html.Replace("%%STARTINDEX%%", kvs["selectionStart"]);
                html = html.Replace("%%ENDINDEX%%", kvs["selectionEnd"]);
            }

            try
            {
                string content = string.Empty;
                var    globals = new Dictionary <string, object>();
                runtime            = new ChatRuntime(Client.AppConfig.TAC);
                runtime.strictMode = false;                              // allow unbound symbols/functions
                runtime.ParseText(code, !kvs.ContainsKey("useValidators") ||
                                  !kvs["useValidators"].Equals("true")); // true to disable validators
                runtime.Chats().ForEach(c => { content += c.ToTree() + "\n\n"; });

                var result = string.Empty;
                if (mode == "execute")
                {
                    // first run any chats marked with 'preload=true'
                    runtime.Preload(globals);

                    // run the first chat with all timing disabled
                    result = WebUtility.HtmlEncode(runtime.InvokeImmediate(globals));

                    if (result.IsNullOrEmpty())
                    {
                        result = "[empty-string]";
                    }
                }

                html = html.Replace("%%RESULT%%", WebUtility.HtmlEncode(content));
                html = html.Replace("%%EXECUTE%%", result);
                html = html.Replace("%%RCLASS%%", "success");
            }
            catch (ParseException ex)
            {
                OnError(ref html, ex, ex.lineNumber);
            }
            catch (Exception e)
            {
                OnError(ref html, e, -1);
            }

            return(html);
        }