Ejemplo n.º 1
0
        private static object ConstructRubyStruct(RubyConstructor /*!*/ ctor, string /*!*/ structName, Node /*!*/ node)
        {
            MappingNode mapping = node as MappingNode;

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

            if (structName.Length == 0)
            {
                // TODO:
                throw new NotSupportedException("anonymous structs not supported");
            }

            RubyContext context = ctor.GlobalScope.Context;
            RubyModule  module;

            // TODO: MRI calls "members" on an arbitrary object

            // MRI checks Struct first, then falls back to Object
            if (!context.TryGetModule(ctor.GlobalScope, "Struct::" + structName, out module) &&
                !context.TryGetModule(ctor.GlobalScope, structName, out module))
            {
                throw RubyExceptions.CreateTypeError("Undefined struct `{0}'", structName);
            }

            RubyClass cls = module as RubyClass;

            if (cls == null)
            {
                throw RubyExceptions.CreateTypeError("`{0}' is not a class", structName);
            }

            RubyStruct newStruct = RubyStruct.Create(cls);

            foreach (var pair in ctor.ConstructMapping(mapping))
            {
                var attributeName = pair.Key as MutableString;
                int index;

                // TODO: encoding
                if (attributeName != null && newStruct.TryGetIndex(attributeName.ToString(), out index))
                {
                    newStruct[index] = pair.Value;
                }
            }
            return(newStruct);
        }
Ejemplo n.º 2
0
        private static object ConstructPrivateObject(RubyConstructor /*!*/ 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);
                // TODO: call allocate here:

                object         result = null;
                RubyMethodInfo method = module.GetMethod("yaml_initialize") as RubyMethodInfo;
                if (method != null)
                {
                    result = RubyMarshal.Utils.CreateObjectAndSetIvars((RubyClass)module);
                    ctor._yamlInitializeSite.Target(ctor._yamlInitializeSite, result, className, values);
                }
                else
                {
                    var dict = new Dictionary <string, object>(values.Count);
                    foreach (var kvp in EnumerateAttributes(globalScope.Context, values))
                    {
                        dict.Add(kvp.Key, kvp.Value);
                    }
                    result = RubyMarshal.Utils.CreateObjectAndSetIvars((RubyClass)module, dict);
                }
                return(result);
            }
            else
            {
                //TODO: YAML::Object
                throw new NotImplementedError("YAML::Object is not implemented yet");
            }
        }
Ejemplo n.º 3
0
        private static object ConstructRubyStruct(RubyConstructor/*!*/ 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;
        }
Ejemplo n.º 4
0
 private static object ConstructPrivateObject(RubyConstructor/*!*/ 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);
             ctor._yamlInitializeSite.Target(ctor._yamlInitializeSite, 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");
     }
 }
Ejemplo n.º 5
0
        private static object ConstructRubyStruct(RubyConstructor/*!*/ ctor, string/*!*/ structName, Node/*!*/ node) {
            MappingNode mapping = node as MappingNode;
            if (mapping == null) {
                throw new ConstructorException("can only construct struct from mapping node");
            }

            if (structName.Length == 0) {
                // TODO:
                throw new NotSupportedException("anonymous structs not supported");
            }

            RubyContext context = ctor.GlobalScope.Context;
            RubyModule module;

            // TODO: MRI calls "members" on an arbitrary object
            
            // MRI checks Struct first, then falls back to Object
            if (!context.TryGetModule(ctor.GlobalScope, "Struct::" + structName, out module) && 
                !context.TryGetModule(ctor.GlobalScope, structName, out module)) {
                throw RubyExceptions.CreateTypeError("Undefined struct `{0}'", structName);
            }

            RubyClass cls = module as RubyClass;
            if (cls == null) {
                throw RubyExceptions.CreateTypeError("`{0}' is not a class", structName);
            }

            RubyStruct newStruct = RubyStruct.Create(cls);
            foreach (var pair in ctor.ConstructMapping(mapping)) {
                var attributeName = pair.Key as MutableString;
                int index;

                // TODO: encoding
                if (attributeName != null && newStruct.TryGetIndex(attributeName.ToString(), out index)) {
                    newStruct[index] = pair.Value;
                }
            }
            return newStruct;
        }
Ejemplo n.º 6
0
        private static object ConstructPrivateObject(RubyConstructor/*!*/ 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);
                // TODO: call allocate here:

                object result = null;
                RubyMethodInfo method = module.GetMethod("yaml_initialize") as RubyMethodInfo;
                if (method != null) {
                    result = RubyMarshal.Utils.CreateObjectAndSetIvars((RubyClass)module);
                    ctor._yamlInitializeSite.Target(ctor._yamlInitializeSite, result, className, values);
                } else {
                    var dict = new Dictionary<string, object>(values.Count);
                    foreach (var kvp in EnumerateAttributes(globalScope.Context, values)) {
                        dict.Add(kvp.Key, kvp.Value);
                    }
                    result = RubyMarshal.Utils.CreateObjectAndSetIvars((RubyClass)module, dict);
                }
                return result;
            } else {
                //TODO: YAML::Object
                throw new NotImplementedError("YAML::Object is not implemented yet");
            }
        }