Exemplo n.º 1
0
        public Kernel()
        {
            SilverNativeFunction nfunc;

            GetType().GetMethods(BindingFlags.Static | BindingFlags.NonPublic).ToList()
                .ForEach(method => {
                    nfunc = new SilverNativeFunction(GetType(), method);
                    Silver.Globals.SetVariable(nfunc.Name, nfunc);
                });
            // Register wrapper for functions which need it, like eval
            Stream stream = typeof (Kernel).Assembly.GetManifestResourceStream("IronSilver.Scripts.core.sv");
            var buffer = new byte[stream.Length];
            stream.Read(buffer, 0, (int) stream.Length);
            string source = Encoding.UTF8.GetString(buffer);

            Silver.Execute(source);
        }
Exemplo n.º 2
0
 private bool CheckForMatch(SilverNativeFunction function, List<FunctionArgument> args)
 {
     if (function.Arguments.Count == args.Count) {
         var _args = new List<object>();
         args.ForEach(arg => {
             dynamic val = CompilerServices.CreateLambdaForExpression(arg.Value)();
             if (val is SilverString) {
                 val = (string) val;
             }
             if (val is SilverNumber)
             {
                 val = SilverNumber.Convert((SilverNumber)val);
             }
             _args.Add(val);
         });
         bool match = true;
         int i = 0;
         foreach (ParameterInfo param in function.Method.GetParameters()) {
             if (_args[i++].GetType() != param.ParameterType) {
                 match = false;
                 break;
             }
         }
         return match;
     }
     if (args.Count > function.Arguments.Count) {
         if (function.Arguments.Any() && function.Arguments.Last().IsVarArg) {
             return true;
         }
         return false;
     }
     int myCount = args.Count;
     int theirCount = function.Arguments.Count;
     function.Arguments.ForEach(arg => {
         if (arg.HasDefault) {
             theirCount--;
         }
     });
     int vo = 0;
     if (function.Arguments.Any() && function.Arguments.Last().IsVarArg) {
         vo = 1;
     }
     if (myCount == theirCount) {
         return true;
     }
     if (myCount + vo == theirCount) {
         return true;
     }
     return false;
 }