Example #1
0
        public async Task <Object> Evaluate(S4JExecutor Executor, S4JToken token, IDictionary <String, object> variables)
        {
            S4JTokenFunction function = token as S4JTokenFunction;
            StringBuilder    code     = new StringBuilder();

            CSharpEvaluatorGlobals       globals        = new CSharpEvaluatorGlobals();
            IDictionary <string, object> globaVariables = globals.Globals as IDictionary <string, object>;

            // var globalObject = new Dictionary<string, object>();

            foreach (KeyValuePair <string, object> keyAndVal in variables)
            {
                globaVariables[keyAndVal.Key] = keyAndVal.Value;
                code.Append("var ").Append(keyAndVal.Key).Append(" = ").Append("Globals.").Append(keyAndVal.Key).Append(";");
            }

            dynamic dbProxy = new ExpandoObject();

            foreach (var source in Executor.Sources)
            {
                (dbProxy as IDictionary <string, object>)[source.Key] = new DbApi(source.Value);
            }
            globaVariables["db"] = dbProxy;
            code.Append("var ").Append("db").Append(" = ").Append("Globals.").Append("db").Append(";");

            code.Append(function.ToJsonWithoutGate());

            var refs = new List <MetadataReference> {
                MetadataReference.CreateFromFile(typeof(System.Linq.Enumerable).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Microsoft.CSharp.RuntimeBinder.RuntimeBinderException).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(System.Runtime.CompilerServices.DynamicAttribute).GetTypeInfo().Assembly.Location)
            };

            var imports = ScriptOptions.Default.
                          WithImports(
                "System",
                "System.Text",
                "System.Linq",
                "System.Collections",
                "System.Collections.Generic").
                          WithReferences(refs);

            object result = await CSharpScript.EvaluateAsync(
                code.ToString(),
                imports,
                globals);

            return(result);
        }
Example #2
0
        public async Task <Object> Evaluate(S4JExecutor Executor, S4JToken token, IDictionary <String, object> variables)
        {
            S4JTokenFunction function = token as S4JTokenFunction;
            StringBuilder    code     = new StringBuilder();

            DynLanEvaluatorGlobals       globals        = new DynLanEvaluatorGlobals();
            IDictionary <string, object> globaVariables = globals.Globals as IDictionary <string, object>;

            // var globalObject = new Dictionary<string, object>();

            foreach (KeyValuePair <string, object> keyAndVal in variables)
            {
                globaVariables[keyAndVal.Key /*.ToUpper()*/] = keyAndVal.Value;

                /*if (keyAndVal.Value == null)
                 * {
                 *  code.Append($"object {keyAndVal.Key} = {keyAndVal.Value.SerializeJson()};\n");
                 * }
                 * else
                 * {
                 *  code.Append($"var {keyAndVal.Key} = {keyAndVal.Value.SerializeJson()};\n");
                 * }*/
            }

            Dictionary <String, Object> dbProxy = new Dictionary <String, Object>();

            foreach (var source in Executor.Sources)
            {
                dbProxy[source.Key] = new DbApi(source.Value);
            }
            globaVariables["db"] = dbProxy;

            code.Append(function.ToJsonWithoutGate());

            // string finalCode = MyStringHelper.AddReturnStatement(code.ToString());

            Object result = new Compiler().
                            Compile(code.ToString()).
                            Eval(globaVariables);

            return(result);
        }
Example #3
0
        public async Task <Object> Evaluate(S4JExecutor Executor, S4JToken token, IDictionary <String, object> variables)
        {
            S4JTokenFunction functionToken = token as S4JTokenFunction;
            S4JStateFunction functionState = token.State as S4JStateFunction;

            MyQuery query = new MyQuery();

            foreach (KeyValuePair <string, object> keyAndVal in variables)
            {
                BuildScriptForVariable(query, keyAndVal.Key, keyAndVal.Value);
            }

            query.Append(functionToken.ToJsonWithoutGate());

            String connectionString = Executor.Sources.Get(functionState.FunctionName);

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                var result = con.SelectItems(query.ToString());
                return(result);
            }
        }
Example #4
0
        async private Task EvaluateFunction(S4JToken token)
        {
            if (token == null)
            {
                return;
            }

            IDictionary <String, object> variables = GetExecutiongVariables(token);
            S4JTokenFunction             function  = token as S4JTokenFunction;

            object result = null;

            bool canBeEvaluated = true;

            if (token.Tags.Count > 0 && TagExecutor != null)
            {
                using (ExecutorContext context = new ExecutorContext(token, variables))
                    canBeEvaluated = TagExecutor(context);
            }

            token.IsVisible = canBeEvaluated;
            if (canBeEvaluated)
            {
                result = await function.Evaluator?.Evaluate(this, token, variables);
            }

            function.IsEvaluated = true;
            function.Result      = result;

            if (function.Parent is S4JTokenObject objectParent &&
                token.IsObjectSingleKey)
            {
                EvaluateFunctionInsideObject(
                    objectParent,
                    function,
                    result);
            }