Esempio n. 1
0
        public static object ConstructSpecializedMap(IConstructor ctor, string pref, Node node)
        {
            Hash result = null;

            try {
                result = (Hash)Type.GetType(pref).GetConstructor(Type.EmptyTypes).Invoke(null);
            } catch (Exception e) {
                throw new ConstructorException("Can't construct a mapping from class: " + pref, e);
            }
            foreach (KeyValuePair <object, object> e in ctor.ConstructMapping(node))
            {
                result.Add(e.Key, e.Value);
            }
            return(result);
        }
Esempio n. 2
0
        public static object ConstructCliObject(IConstructor ctor, string pref, Node node)
        {
            // TODO: should this use serialization or some more standard CLR mechanism?
            //       (it is very ad-hoc)
            // TODO: use DLR APIs instead of reflection
            try {
                Type   type   = Type.GetType(pref);
                object result = type.GetConstructor(Type.EmptyTypes).Invoke(null);

                foreach (KeyValuePair <object, object> e in ctor.ConstructMapping(node))
                {
                    string name = e.Key.ToString();
                    name = "" + char.ToUpper(name[0]) + name.Substring(1);
                    PropertyInfo prop = type.GetProperty(name);

                    prop.SetValue(result, Convert.ChangeType(e.Value, prop.PropertyType), null);
                }
                return(result);
            } catch (Exception e) {
                throw new ConstructorException("Can't construct a CLI object from class: " + pref, e);
            }
        }
Esempio n. 3
0
        public static object ConstructRubyStruct(IConstructor ctor, string className, Node node)
        {
            MappingNode mapping = node as MappingNode;

            if (mapping == null)
            {
                throw new ConstructorException("can only construct struct from mapping node");
            }

            RubyScope  scope = ctor.Scope;
            RubyModule module;
            RubyClass  cls;

            if (scope.RubyContext.TryGetModule(scope.GlobalScope, className, out module))
            {
                cls = module as RubyClass;
                if (cls == null)
                {
                    throw new ConstructorException("Struct type name must be Ruby class");
                }
            }
            else
            {
                RubyModule structModule = scope.RubyContext.GetModule(typeof(RubyStruct));
                cls = RubyUtils.GetConstant(scope, structModule, className, false) as RubyClass;
                if (cls == null)
                {
                    throw new ConstructorException(String.Format("Cannot find struct class \"{0}\"", className));
                }
            }

            RubyStruct newStruct = RubyStruct.Create(cls);

            foreach (var pair in ctor.ConstructMapping(mapping))
            {
                RubyStructOps.SetValue(newStruct, SymbolTable.StringToId(pair.Key.ToString()), pair.Value);
            }
            return(newStruct);
        }
Esempio n. 4
0
        public static object ConstructPrivateObject(IConstructor ctor, string className, Node node)
        {
            MappingNode mapping = node as MappingNode;

            if (mapping == null)
            {
                throw new ConstructorException("can only construct private type from mapping node");
            }
            RubyModule module;
            RubyScope  scope = ctor.Scope;

            if (scope.RubyContext.TryGetModule(scope.GlobalScope, className, out module))
            {
                if (!module.IsClass)
                {
                    throw new ConstructorException("Cannot construct module");
                }
                Hash           values = ctor.ConstructMapping(mapping);
                RubyMethodInfo method = (module.GetMethod("yaml_initialize") as RubyMethodInfo);
                if (method != null)
                {
                    object result = RubyUtils.CreateObject((RubyClass)module);
                    _YamlInitialize.Target(_YamlInitialize, scope.RubyContext, result, className, values);
                    return(result);
                }
                else
                {
                    return(RubyUtils.CreateObject((RubyClass)module, values, true));
                }
            }
            else
            {
                //TODO: YAML::Object
                throw new NotImplementedError("YAML::Object is not implemented yet");
            }
        }
Esempio n. 5
0
        public static object ConstructCliObject(IConstructor ctor, string pref, Node node) {
            // TODO: should this use serialization or some more standard CLR mechanism?
            //       (it is very ad-hoc)
            // TODO: use DLR APIs instead of reflection
            try {
                Type type = Type.GetType(pref);
                object result = type.GetConstructor(Type.EmptyTypes).Invoke(null);

                foreach (KeyValuePair<object, object> e in ctor.ConstructMapping(node)) {
                    string name = e.Key.ToString();
                    name = "" + char.ToUpper(name[0]) + name.Substring(1);
                    PropertyInfo prop = type.GetProperty(name);

                    prop.SetValue(result, Convert.ChangeType(e.Value, prop.PropertyType), null);
                }
                return result;

            } catch (Exception e) {
                throw new ConstructorException("Can't construct a CLI object from class: " + pref, e);
            }
        }
Esempio n. 6
0
 public static object ConstructSpecializedMap(IConstructor ctor, string pref, Node node) {
     Hash result = null;
     try {
         result = (Hash)Type.GetType(pref).GetConstructor(Type.EmptyTypes).Invoke(null);
     } catch (Exception e) {
         throw new ConstructorException("Can't construct a mapping from class: " + pref, e);
     }
     foreach (KeyValuePair<object, object> e in ctor.ConstructMapping(node)) {
         result.Add(e.Key, e.Value);
     }
     return result;
 }
Esempio n. 7
0
 public static Hash ConstructYamlMap(IConstructor ctor, Node node) {
     return ctor.ConstructMapping(node);
 }        
Esempio n. 8
0
 public static ICollection ConstructYamlSet(IConstructor ctor, Node node) {
     return ctor.ConstructMapping(node).Keys;
 }
Esempio n. 9
0
        public static object ConstructRubyStruct(IConstructor ctor, string className, Node node) {
            MappingNode mapping = node as MappingNode;
            if (mapping == null) {
                throw new ConstructorException("can only construct struct from mapping node");
            }

            RubyContext context = ctor.GlobalScope.Context;
            RubyModule module;
            RubyClass cls;
            if (context.TryGetModule(ctor.GlobalScope, className, out module)) {
                cls = module as RubyClass;
                if (cls == null) {
                    throw new ConstructorException("Struct type name must be Ruby class");
                }
            } else {
                RubyModule structModule = context.GetModule(typeof(RubyStruct));
                cls = RubyUtils.GetConstant(ctor.GlobalScope, structModule, className, false) as RubyClass;
                if (cls == null) {
                    throw new ConstructorException(String.Format("Cannot find struct class \"{0}\"", className));
                }
            }

            RubyStruct newStruct = RubyStruct.Create(cls);
            foreach (var pair in ctor.ConstructMapping(mapping)) {
                RubyStructOps.SetValue(newStruct, SymbolTable.StringToId(pair.Key.ToString()), pair.Value);        
            }
            return newStruct;
        }
Esempio n. 10
0
 public static object ConstructPrivateObject(IConstructor ctor, string className, Node node) {
     MappingNode mapping = node as MappingNode;
     if (mapping == null) {
         throw new ConstructorException("can only construct private type from mapping node");
     }
     RubyModule module;
     RubyGlobalScope globalScope = ctor.GlobalScope;
     if (globalScope.Context.TryGetModule(globalScope, className, out module)) {
         if (!module.IsClass) {
             throw new ConstructorException("Cannot construct module");
         }
         Hash values = ctor.ConstructMapping(mapping);
         RubyMethodInfo method = module.GetMethod("yaml_initialize") as RubyMethodInfo;
         if (method != null) {
             object result = RubyUtils.CreateObject((RubyClass)module);
             _YamlInitialize.Target(_YamlInitialize, globalScope.Context, result, className, values);
             return result;
         } else {
             return RubyUtils.CreateObject((RubyClass)module, values, true);
         }
     } else {
         //TODO: YAML::Object
         throw new NotImplementedError("YAML::Object is not implemented yet");
     }
 }
Esempio n. 11
0
 public static Hash ConstructYamlMap(IConstructor ctor, Node node)
 {
     return(ctor.ConstructMapping(node));
 }
Esempio n. 12
0
 public static ICollection ConstructYamlSet(IConstructor ctor, Node node)
 {
     return(ctor.ConstructMapping(node).Keys);
 }