示例#1
0
        public string getPostData(string ExpectNo, string EncodingMsg, string ruleid, string guid)
        {
            VsaEngine          Engine = VsaEngine.CreateEngine();
            string             txt    = GlobalClass.LoadTextFile("getPostData.js", string.Format("jscript\\{0}\\", gc.ForWeb));
            CSharpCodeProvider cc     = new CSharpCodeProvider();
            // icc = cc.CreateCompiler();
            string allscript = string.Format("{0}getPostData('{1}','{2}','{3}','{4}');", txt, ExpectNo, EncodingMsg, ruleid, guid);

            try
            {
                object               value = Eval.JScriptEvaluate(allscript, Engine);
                StringBuilder        sb    = new StringBuilder();
                JavaScriptSerializer json  = new JavaScriptSerializer();
                json.Serialize(value, sb);
                return(sb.ToString());
                //return DetailStringClass.ToJson(value);
            }
            catch (Exception ce)
            {
                return(null);
            }
            finally
            {
                Engine = null;
            }
        }
示例#2
0
    // Test the "parseInt" function inside an evaluation.
    public void TestGlobalEvalParseInt()
    {
        VsaEngine    engine;
        IVsaCodeItem item;

        // Create a new engine instance.
        engine = VsaEngine.CreateEngine();

        // Compile an empty script.
        item = (IVsaCodeItem)(engine.Items.CreateItem
                                  ("script1", VsaItemType.Code, VsaItemFlag.None));
        item.SourceText = "";
        Assert("Compile", engine.Compile());
        engine.Run();

        // Evaluate a "parseInt" call within the engine context.
        AssertEquals("EvalParseInt (1)", 123.0,
                     (double)(Eval.JScriptEvaluate
                                  ("parseInt(\"123\", 0)", engine)),
                     0.0001);
        AssertEquals("EvalParseInt (2)", 123.0,
                     (double)(Eval.JScriptEvaluate
                                  ("parseInt(\"123\")", engine)),
                     0.0001);

        // Close the engine.
        engine.Close();
    }
示例#3
0
        public bool EvaluateConditionWithCharacter(Context context)
        {
            StringBuilder builder          = new StringBuilder(data);
            List <string> valuesToReplace  = new List <string>();
            int           startingPosition = 0;
            int           positionOfVariable;

            while ((positionOfVariable = data.IndexOf(StaticData.VariableSeparator, startingPosition)) != -1)
            {
                startingPosition = ParseUtilites.GetEndOfVariableAtPosition(data, positionOfVariable + 1);
                valuesToReplace.Add(data.Substring(positionOfVariable + 1, startingPosition - positionOfVariable));
            }
            foreach (var val in valuesToReplace.Where(context.Contains))
            {
                string replaceTo;
                object value = context.GetValue(val);
                double res;
                if (double.TryParse(value.ToString(), out res))
                {
                    replaceTo = res.ToString();
                }
                else
                {
                    replaceTo = '"' + value.ToString() + '"';
                }
                builder.Replace(StaticData.VariableSeparator + val, replaceTo);
            }
            string condition = builder.ToString();
            var    engine    = VsaEngine.CreateEngine();
            var    result    = Eval.JScriptEvaluate(condition, engine);

            return(bool.Parse(result.ToString()));
        }
        static void Main(string[] args)
        {
            object value = Eval.JScriptEvaluate("1+2-(3*5)", VsaEngine.CreateEngine());

            Console.Write(value);
            Console.ReadLine();
        }
 public static object InvokeMethodInfo(MethodInfo m, object[] arguments, bool construct, object thisob, VsaEngine engine)
 {
     if (engine == null)
     {
         engine = VsaEngine.CreateEngine();
     }
     return(LateBinding.CallOneOfTheMembers(new MemberInfo[] { m }, arguments, construct, thisob, JSBinder.ob, null, null, engine));
 }
示例#6
0
        /// <summary>
        /// 计算字符串算式,并返回结果
        /// </summary>
        /// <param name="算式">如:(356+258)*3/0.3%2</param>
        /// <returns>计算结果</returns>
        public static double 计算字符串算式(string 算式)
        {
            var ve = VsaEngine.CreateEngine();
            //获取计算结果 ret
            object ret = Microsoft.JScript.Eval.JScriptEvaluate(算式, ve);

            //返回结果
            return(Convert.ToDouble(ret));
        }
        public static object CallMethod(string name, object thisob, object[] arguments, VsaEngine engine)
        {
            if (engine == null)
            {
                engine = VsaEngine.CreateEngine();
            }
            LateBinding binding = new LateBinding(name, thisob, true);

            return(binding.Call(arguments, false, false, engine));
        }
        public static object CallConstructor(string typename, object[] arguments, VsaEngine engine)
        {
            if (engine == null)
            {
                engine = VsaEngine.CreateEngine();
            }
            object type = GetType(typename);

            return(LateBinding.CallValue(null, type, arguments, true, false, engine));
        }
        public static object CallStaticMethod(string name, string typename, object[] arguments, VsaEngine engine)
        {
            if (engine == null)
            {
                engine = VsaEngine.CreateEngine();
            }
            object      type    = GetType(typename);
            LateBinding binding = new LateBinding(name, type, true);

            return(binding.Call(arguments, false, false, engine));
        }
示例#10
0
        public static object EvalJScript(string JScript)
        {
            object Result = null;

            try
            {
                Result = Microsoft.JScript.Eval.JScriptEvaluate(JScript, VsaEngine.CreateEngine());
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
            return(Result);
        }
示例#11
0
        private static decimal EvaluateNumericExpression(string istrExpresion)
        {
            VsaEngine engine = VsaEngine.CreateEngine();

            try
            {
                object resultado = Eval.JScriptEvaluate(istrExpresion, engine);
                return(System.Convert.ToDecimal(resultado));
            }
            catch
            {
                return(0);
            }
            engine.Close();
        }
示例#12
0
 public string getUrl(string guid)
 {
     try
     {
         string    txt       = GlobalClass.LoadTextFile("getUrl.js", string.Format("jscript\\{0}\\", gc.ForWeb));
         string    allscript = string.Format("{0};getUrl('{1}');", txt, guid);
         VsaEngine Engine    = VsaEngine.CreateEngine();
         object    value     = Eval.JScriptEvaluate(allscript, Engine);
         return(value?.ToString());
     }
     catch (Exception ce)
     {
         return(null);
     }
 }
示例#13
0
        public static ArrayObject EvalJScript(string JScript)
        {
            VsaEngine   Engine = VsaEngine.CreateEngine();
            ArrayObject Result = null;

            try
            {
                Result = Microsoft.JScript.Eval.JScriptEvaluate(JScript, Engine) as ArrayObject;
            }
            catch (Exception ex)
            {
                return(null);
            }
            return(Result);
        }
示例#14
0
        private string EvalExpression(string expression)
        {
            VsaEngine engine = VsaEngine.CreateEngine();

            try
            {
                object o = Eval.JScriptEvaluate(expression, engine);
                return(System.Convert.ToDouble(o).ToString());
            }
            catch
            {
                return("No se puede evaluar la expresión");
            }
            engine.Close();
        }
        public static object GetDefaultIndexedPropertyValue(object thisob, object[] arguments, VsaEngine engine, string[] namedParameters)
        {
            if (engine == null)
            {
                engine = VsaEngine.CreateEngine();
            }
            object[] target = null;
            int      num    = (arguments == null) ? 0 : arguments.Length;

            if (((namedParameters != null) && (namedParameters.Length > 0)) && ((namedParameters[0] == "this") && (num > 0)))
            {
                target = new object[num - 1];
                ArrayObject.Copy(arguments, 1, target, 0, num - 1);
            }
            else
            {
                target = arguments;
            }
            LateBinding binding = new LateBinding(null, thisob, true);

            return(binding.Call(target, false, false, engine));
        }
示例#16
0
文件: Immediate.cs 项目: mo5h/omeo
        private object Execute()
        {
            string code = _htmled.Text;
            string sOutput;
            object result = null;

            try
            {
                VsaEngine engine1 = VsaEngine.CreateEngine();
                result = Eval.JScriptEvaluate(code, true, engine1).ToString();

                sOutput = "<html><body><pre>" + result.ToString() + "</pre></body></html>";
            }
            catch (Exception ex)
            {
                sOutput = "<html><body><pre style=\"background-color: silver; color: red;\">" + ex.ToString() + "</pre></body></html>";
            }

            _htmlvw.Html = sOutput;

            return(result);
        }
示例#17
0
    // Test engine creation.
    public void TestEngineCreate()
    {
        VsaEngine engine;
        VsaEngine engine2;

        // Create a new engine instance.
        engine = VsaEngine.CreateEngine();
        AssertNotNull("Create (1)", engine);
        AssertEquals("Create (2)", engine, Globals.contextEngine);

        // Try to create another (returns same instance).
        engine2 = VsaEngine.CreateEngine();
        AssertEquals("Create (3)", engine, engine2);

        // Close the engine.
        engine.Close();
        AssertNull("Create (4)", Globals.contextEngine);

        // Create a new engine, which should be different this time.
        engine = VsaEngine.CreateEngine();
        Assert("Create (5)", engine2 != engine);
        AssertNotNull("Create (6)", engine);
        engine.Close();
    }
示例#18
0
        public static string RunJavascript(string scriptText)
        {
            VsaEngine engine = VsaEngine.CreateEngine();

            return(Eval.JScriptEvaluate(scriptText, engine).ToString());
        }
示例#19
0
文件: jsrun.cs 项目: ForNeVeR/pnet
    // Main entry point for the application.
    public static int Main(String[] args)
    {
#if CONFIG_EXTENDED_NUMERICS && CONFIG_REFLECTION
        StreamReader     reader = null;
        int              len;
        VsaEngine        engine;
        IVsaCodeItem     item;
        StringCollection scripts = new StringCollection();

        // Create an engine instance and add the script to it.
        engine = VsaEngine.CreateEngine();
        engine.SetOption("print", true);

        // get command-line arguments
        int    i   = 0;
        String arg = args.Length == 0 ? null : args[0];
        while (arg != null)
        {
            String next = null;
            if (arg.StartsWith("-") && !arg.StartsWith("--") &&
                arg.Length > 2)
            {
                next = "-" + arg.Substring(2, arg.Length - 2);
                arg  = arg.Substring(0, 2);
            }
            switch (arg)
            {
            case "-h":
            case "--help":
                Usage();
                return(0);

            case "-v":
            case "--version":
                Version();
                return(0);

            default:
                // matches both short and long options. (-/--)
                if (arg.StartsWith("-"))
                {
#if !CONFIG_SMALL_CONSOLE
                    Console.Error.WriteLine
#else
                    Console.WriteLine
#endif
                        ("jsrun: unkown option `{0}'", arg);

                    return(1);
                }
                // not an option - assumes script path
                else
                {
                    // To prevent a relative and a absolute pathname to same file!
                    FileInfo fi = new FileInfo(arg);
                    if (!fi.Exists)
                    {
#if !CONFIG_SMALL_CONSOLE
                        Console.Error.WriteLine
#else
                        Console.WriteLine
#endif
                            ("jsrun: {0}: No such file or directory", arg);

                        return(1);
                    }
                    // Cannot load same script source twice!
                    if (scripts.Contains(fi.FullName))
                    {
#if !CONFIG_SMALL_CONSOLE
                        Console.Error.WriteLine
#else
                        Console.WriteLine
#endif
                            ("jsrun: {0}: use of duplicate sources illegal.", fi.FullName);
                    }
                    else
                    {
                        scripts.Add(fi.FullName);
                    }
                    // Load script file
                    try
                    {
                        reader = new StreamReader(arg);
                    }
                    catch (Exception e)
                    {
#if !CONFIG_SMALL_CONSOLE
                        Console.Error.WriteLine
#else
                        Console.WriteLine
#endif
                            ("jsrun: ({0}): {1}", e.GetType(), e.Message);
                    }

                    // Load the script into memory as a string.
                    StringBuilder scriptBuilder = new StringBuilder();
                    char[]        scriptBuffer  = new char [512];
                    while ((len = reader.Read(scriptBuffer, 0, 512)) > 0)
                    {
                        scriptBuilder.Append(scriptBuffer, 0, len);
                    }
                    reader.Close();

                    item = (IVsaCodeItem)(engine.Items.CreateItem
                                              (String.Concat("script", engine.Items.Count + 1),
                                              VsaItemType.Code, VsaItemFlag.None));
                    item.SourceText = scriptBuilder.ToString();
                    item.SetOption("codebase", arg);
                }
                break;
            }                             // switch(arg)

            if (next != null)
            {
                arg = next;
            }
            else if (i + 1 >= args.Length)
            {
                arg = null;
            }
            else
            {
                i   = i + 1;
                arg = args[i];
            }
        }                         // for each in args

        // We need at least one item.
        if (engine.Items.Count == 0)
        {
            Usage();
            return(1);
        }

        // Compile and run the script.
        if (!engine.Compile())
        {
#if !CONFIG_SMALL_CONSOLE
            Console.Error.WriteLine
#else
            Console.WriteLine
#endif
                ("jsrun: Could not compile script");

            return(1);
        }
        engine.Run();

        // Shut down the engine and exit.
        engine.Close();
        return(0);
#else
        // Use error output and exit status in case any
        // script/program is depending on output.
#if !CONFIG_SMALL_CONSOLE
        Console.Error.WriteLine
#else
        Console.WriteLine
#endif
            ("JScript is not available in this configuration " +
            "because the library does\n" +
            "not have sufficient features to support JScript.");

        return(1);
#endif
    }
示例#20
0
        private decimal TinhBieuThucDonGian(string bieuthuc)
        {
            VsaEngine vsaEngine = VsaEngine.CreateEngine();

            return(decimal.Parse(Eval.JScriptEvaluate(bieuthuc, vsaEngine).ToString()));
        }
示例#21
0
    // Test the global object properties.
    public void TestEngineGlobals()
    {
        // Create a new engine instance.
        VsaEngine engine = VsaEngine.CreateEngine();

        // Get the global object.
        LenientGlobalObject global = engine.LenientGlobalObject;

        AssertNotNull("Globals (1)", global);

        // Check that the "Object" and "Function" objects
        // appear to be of the right form.
        Object objectConstructor = global.Object;

        AssertNotNull("Globals (2)", objectConstructor);
        Assert("Globals (3)", objectConstructor is ObjectConstructor);
        Object functionConstructor = global.Function;

        AssertNotNull("Globals (4)", functionConstructor);
        Assert("Globals (5)",
               functionConstructor is FunctionConstructor);

        // Check the type information.
        AssertSame("Type (1)", typeof(Boolean), global.boolean);
        AssertSame("Type (2)", typeof(Byte), global.@byte);
        AssertSame("Type (3)", typeof(SByte), global.@sbyte);
        AssertSame("Type (4)", typeof(Char), global.@char);
        AssertSame("Type (5)", typeof(Int16), global.@short);
        AssertSame("Type (6)", typeof(UInt16), global.@ushort);
        AssertSame("Type (7)", typeof(Int32), global.@int);
        AssertSame("Type (8)", typeof(UInt32), global.@uint);
        AssertSame("Type (9)", typeof(Int64), global.@long);
        AssertSame("Type (10)", typeof(UInt64), global.@ulong);
        AssertSame("Type (11)", typeof(Single), global.@float);
        AssertSame("Type (12)", typeof(Double), global.@double);
        AssertSame("Type (13)", typeof(Decimal), global.@decimal);
        AssertSame("Type (14)", typeof(Void), global.@void);

        // Check the global functions.
        CheckScriptFunction("CollectGarbage",
                            global.CollectGarbage, 0);
        CheckScriptFunction("decodeURI",
                            global.decodeURI, 1);
        CheckScriptFunction("decodeURIComponent",
                            global.decodeURIComponent, 1);
        CheckScriptFunction("encodeURI",
                            global.encodeURI, 1);
        CheckScriptFunction("encodeURIComponent",
                            global.encodeURIComponent, 1);
        CheckScriptFunction("escape",
                            global.escape, 1);
        CheckScriptFunction("eval",
                            global.eval, 1);
        CheckScriptFunction("isFinite",
                            global.isFinite, 1);
        CheckScriptFunction("isNaN",
                            global.isNaN, 1);
        CheckScriptFunction("parseFloat",
                            global.parseFloat, 1);
        CheckScriptFunction("parseInt",
                            global.parseInt, 2);
        CheckScriptFunction("ScriptEngine",
                            global.ScriptEngine, 0);
        CheckScriptFunction("ScriptEngineBuildVersion",
                            global.ScriptEngineBuildVersion, 0);
        CheckScriptFunction("ScriptEngineMajorVersion",
                            global.ScriptEngineMajorVersion, 0);
        CheckScriptFunction("ScriptEngineMinorVersion",
                            global.ScriptEngineMinorVersion, 0);
        CheckScriptFunction("unescape",
                            global.unescape, 1);

        // Check the global values.
        Assert("Value (1)",
               Double.IsPositiveInfinity((double)(global.Infinity)));
        Assert("Value (2)", Double.IsNaN((double)(global.NaN)));
        AssertNull("Value (3)", global.undefined);

        // Close the engine and exit.
        engine.Close();
    }
示例#22
0
        public ActionResult AddDebt(string summ, int group_id, string count, string comment)
        {
            var     t   = JsonConvert.DeserializeObject <result>(count);
            var     div = t.array.Count();
            Decimal Sum = 0;

            try
            {
                Sum = System.Convert.ToDecimal(Eval.JScriptEvaluate(summ, VsaEngine.CreateEngine()));
            }
            catch (Exception)
            {
                return(RedirectToAction("Index", "Error", new { error = "Введено неверное выражение" }));
            }
            if (Sum > 0)
            {
                Decimal debt = Sum / div;
                foreach (var id in t.array)
                {
                    var          user  = db.users.Where((x) => x.id == id.id).First();
                    VDolgah.debt first = null;
                    if ((first = user.debts1.Where((x) => x.column == (Session["user"] as VDolgah.user).id).FirstOrDefault()) != null)
                    {
                        first.value += debt;
                        addLog(debt, group_id, comment, user.id);
                    }
                    else if ((first = user.debts.Where((x) => x.row == (Session["user"] as VDolgah.user).id).FirstOrDefault()) != null)
                    {
                        first.value -= debt;
                        if (first.value == 0)
                        {
                            db.debts.Remove(first);
                        }
                        if (first.value < 0)
                        {
                            debt tmp = new VDolgah.debt();
                            tmp.row    = first.column;
                            tmp.column = first.row;
                            tmp.value  = -first.value;
                            db.debts.Add(tmp);
                            addLog(debt, group_id, comment, tmp.row);
                            db.debts.Remove(first);
                        }
                        else
                        {
                            addLog(debt, group_id, comment, user.id);
                        }
                    }
                    else if (id.id != (Session["user"] as VDolgah.user).id)
                    {
                        var d = new debt();
                        d.row    = id.id;
                        d.column = (Session["user"] as VDolgah.user).id;
                        d.value  = debt;
                        db.debts.Add(d);
                        addLog(debt, group_id, comment, user.id);
                    }
                }
                db.SaveChanges();
                return(RedirectToAction("Index", new { group_id = group_id }));
            }
            else
            {
                return(RedirectToAction("Index", "Error", new { error = "Введен долг, который равен 0 или отрицательный" }));
            }
        }