예제 #1
0
        public void ConfigTest()
        {
            string file = "config { foo { a = \"test\"; b = \"bar\"; } }";

            ScriptEngine engine = new ScriptEngine();

            ConfigBuilder.Structure s = new ConfigBuilder.Structure();

            s.AddElement(new ConfigBuilder.KeyValueTagBuilder("foo", false));

            engine.RegisterBuilder(new ConfigBuilder("config", s, NewConfigBuilt));

            engine.Build(file);

            if (_results == null)
            {
                Assert.Fail("Was not returned a configuration.");
            }

            Dictionary<string, List<Token>> foo = (Dictionary<string, List<Token>>)_results["foo"][0];

            Chunk c = new Chunk(foo["b"], new Script(engine) {UseEngineGlobals = false});

            object result = c.Evaluate();

            Assert.AreEqual("bar", (string) result);
        }
예제 #2
0
        public object Invoke(Script script, Scope scope, string functionName, List<List<Token>> arguments)
        {
            // First, check script functions.
            if (_scriptFunctions.ContainsKey(functionName))
            {
                Function f = _scriptFunctions[functionName];
                if (f.Parameters.Count == arguments.Count)
                {
                    // We have a match.
                    var args = new object[arguments.Count];
                    for (int i = 0; i < arguments.Count; i++)
                    {
                        args[i] = new Chunk(arguments[i], script).Evaluate(scope);
                    }
                    return f.Invoke(script, scope, args);
                }
            }

            if (!_functions.ContainsKey(functionName))
            {
                return null;
            }

            var results = new object[arguments.Count];
            var types = new Type[arguments.Count];
            for (int i = 0; i < arguments.Count; i++)
            {
                results[i] = new Chunk(arguments[i], script).Evaluate();
                types[i] = results[i].GetType();
            }

            MethodInfo calling = null;

            List<KeyValuePair<object, MethodInfo>> methods = _functions[functionName];
            for (int i = 0; i < methods.Count; i++)
            {
                if (methods[i].Value.GetParameters().Length == results.Length)
                {
                    bool canInvoke = true;
                    ParameterInfo[] args = methods[i].Value.GetParameters();
                    var needsChange = new bool[args.Length];
                    for (int j = 0; j < args.Length; j++)
                    {
                        if (args[j].ParameterType == types[j] || types[j].IsSubclassOf(args[j].ParameterType))
                        {
                            needsChange[j] = false;
                            continue;
                        }
                        needsChange[j] = true;
                        if (!args[j].ParameterType.IsAssignableFrom(types[j]))
                        {
                            if (types[j] == typeof (Number))
                            {
                                try
                                {
                                    Convert.ChangeType(results[j], args[j].ParameterType);
                                }
                                catch (Exception e)
                                {
                                    canInvoke = false;
                                }
                            }
                            else
                            {
                                canInvoke = false;
                            }
                        }
                    }
                    if (canInvoke)
                    {
                        for (int j = 0; j < args.Length; j++)
                        {
                            if (needsChange[j])
                            {
                                results[j] = Convert.ChangeType(results[j], args[j].ParameterType);
                            }
                        }
                        return methods[i].Value.Invoke(methods[i].Key, results);
                    }
                }
            }

            return null;
        }