Package org.cx4a.rsense.ruby

Examples of org.cx4a.rsense.ruby.RubyModule


    public static Vertex classVarDeclaration(Graph graph, ClassVarDeclNode node) {
        return classVarDeclaration(graph, node, null);
    }
   
    public static Vertex classVarDeclaration(Graph graph, ClassVarDeclNode node, Vertex src) {
        RubyModule klass = graph.getRuntime().getContext().getFrameModule();
        if (src == null) {
            src = graph.createVertex(node.getValueNode());
        }
        VertexHolder holder = (VertexHolder) klass.getClassVar(node.getName());
        if (holder == null) {
            holder = graph.createFreeVertexHolder();
            klass.setClassVar(node.getName(), holder);
        }
        // Clear older types (performance issue)
        holder.getVertex().getTypeSet().clear();
        graph.addEdgeAndPropagate(src, holder.getVertex());
        return src;
View Full Code Here


    public static Vertex classVarAssign(Graph graph, ClassVarAsgnNode node) {
        return classVarAssign(graph, node, null);
    }

    public static Vertex classVarAssign(Graph graph, ClassVarAsgnNode node, Vertex src) {
        RubyModule klass = graph.getRuntime().getContext().getFrameModule();
        if (src == null) {
            src = graph.createVertex(node.getValueNode());
        }
        VertexHolder holder = (VertexHolder) klass.getClassVar(node.getName());
        if (holder == null) {
            holder = graph.createFreeVertexHolder();
            klass.setClassVar(node.getName(), holder);
        }
        // Clear older types (performance issue)
        holder.getVertex().getTypeSet().clear();
        graph.addEdgeAndPropagate(src, holder.getVertex());
        return src;
View Full Code Here

        graph.addEdgeAndPropagate(src, holder.getVertex());
        return src;
    }

    public static Vertex classVariable(Graph graph, ClassVarNode node) {
        RubyModule klass = graph.getRuntime().getContext().getFrameModule();
        VertexHolder holder = (VertexHolder) klass.getClassVar(node.getName());
        if (holder == null) {
            holder = graph.createFreeVertexHolder();
            klass.setClassVar(node.getName(), holder);
        }
        return holder.getVertex();
    }
View Full Code Here

    public static Vertex constDeclaration(Graph graph, ConstDeclNode node) {
        return constDeclaration(graph, node, null);
    }
   
    public static Vertex constDeclaration(Graph graph, ConstDeclNode node, Vertex src) {
        RubyModule module = null;
        String name = null;
        INameNode constNode = (INameNode) node.getConstNode();
        if (constNode == null) {
            name = node.getName();
            module = graph.getRuntime().getContext().getFrameModule();
        } else if (constNode instanceof Colon2Node) {
            Node leftNode = ((Colon2Node) constNode).getLeftNode();

            Vertex v = graph.createVertex(leftNode);
            for (IRubyObject mod : v.getTypeSet()) {
                if (mod instanceof RubyModule) {
                    module = (RubyModule) mod;
                    break;
                }
            }
            name = constNode.getName();
        } else { // colon3
            module = graph.getRuntime().getObject();
            name = constNode.getName();
        }

        if (src == null) {
            src = graph.createVertex(node.getValueNode());
        }
        if (module != null && name != null) {
            module.setConstant(name, graph.createVertexHolder(src));
        }

        return src;
    }
View Full Code Here

    private static Vertex applyTemplateAttribute(Graph graph, CallVertex vertex, String name, TemplateAttribute attr, boolean callSuper) {
        IRubyObject receiver = attr.getReceiver();
        IRubyObject[] args = attr.getArgs();
        RubyClass receiverType = null;
        if (callSuper) {
            RubyModule module = graph.getRuntime().getContext().getFrameModule();
            if (module instanceof RubyClass) {
                RubyClass klass = (RubyClass) module;
                receiverType = klass.getSuperClass();
            } else {
                Logger.error("Cannot call super in module");
View Full Code Here

            // FIXME module check
            graph.getRuntime().getContext().getCurrentFrame().setVisibility(visibility);
        } else {
            for (IRubyObject receiver : receivers) {
                if (receiver.isKindOf(graph.getRuntime().getModule())) {
                    RubyModule module = (RubyModule) receiver;
                    for (Vertex arg : args) {
                        String name = Vertex.getStringOrSymbol(arg);
                        if (name != null) {
                            DynamicMethod method = module.getMethod(name);
                            if (method != null) {
                                method.setVisibility(visibility);
                                if (visibility == Visibility.MODULE_FUNCTION) {
                                    module.getSingletonClass().addMethod(name, method);
                                }
                            }
                        }                       
                    }
                }
View Full Code Here

            List<TypeExpression> types = app.getTypes();
            IRubyObject ret = resolveIdentity(template, app.getIdentity());
            if (!(ret instanceof RubyModule) || !arg.isKindOf((RubyModule) ret)) {
                return false;
            } else {
                RubyModule klass = (RubyModule) ret;
                ClassType klassType = RuntimeHelper.getClassAnnotation(klass);
                TypeVarMap typeVarMap = RuntimeHelper.getTypeVarMap(arg);
                if (klassType != null && typeVarMap != null) {
                    List<TypeVariable> vars = klassType.getTypes();
                    for (int i = 0; i < vars.size(); i++) {
View Full Code Here

                                if (!(receiver instanceof RubyModule)) {
                                    // FIXME toplevel
                                    receiver = receiver.getMetaClass();
                                }
                                if (receiver instanceof RubyModule) {
                                    RubyModule module = (RubyModule) receiver;
                                    for (IRubyObject target : arg.getTypeSet()) {
                                        if (target instanceof RubyModule) {
                                            module.includeModule((RubyModule) target);
                                            included = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (!included) {
                        result.setCallNextMethod(true);
                    }
                }
            });

        addSpecialMethod("[]", new SpecialMethod() {
                public void call(Ruby runtime, TypeSet receivers, Vertex[] args, Block block, Result result) {
                    Collection<IRubyObject> arg = null;
                    TypeSet ts = new TypeSet();
                    for (IRubyObject receiver : receivers) {
                        if (receiver instanceof Hash) {
                            if (args != null && args.length > 0) {
                                Hash hash = (Hash) receiver;
                                Object key = Hash.getRealKey(args[0].getNode());
                                if (!hash.isModified() && key != null) {
                                    Vertex v = hash.get(key);
                                    if (v != null) {
                                        ts.addAll(v.getTypeSet());
                                    }
                                }
                            }
                        } else if (receiver instanceof Array) {
                            if (args != null && args.length > 0) {
                                Array array = (Array) receiver;
                                Integer n = Vertex.getFixnum(args[0]);
                                if (!array.isModified() && n != null) {
                                    Vertex v = array.getElement(n);
                                    if (v != null) {
                                        ts.addAll(v.getTypeSet());
                                    }
                                }
                            }
                        } else if (receiver instanceof Proc) {
                            if (arg == null) {
                                if (args == null) {
                                    arg = Arrays.asList((IRubyObject) RuntimeHelper.createArray(Graph.this, new Vertex[0]));
                                } else if (args.length == 1) {
                                    arg = args[0].getTypeSet();
                                } else { arg = Arrays.asList((IRubyObject) RuntimeHelper.createArray(Graph.this, args));
                                }
                            }
                            Vertex returnVertex = createFreeVertex();
                            RuntimeHelper.yield(Graph.this, (Proc) receiver, arg, true, returnVertex);
                            ts.addAll(returnVertex.getTypeSet());
                        }
                    }

                    if (ts.isEmpty()) {
                        result.setCallNextMethod(true);
                    } else {
                        result.setResultTypeSet(ts);
                    }
                }
            });

        addSpecialMethod("private", new SpecialMethod() {
                public void call(Ruby runtime, TypeSet receivers, Vertex[] args, Block block, Result result) {
                    RuntimeHelper.setMethodsVisibility(Graph.this, receivers, args, Visibility.PRIVATE);
                }
            });

        addSpecialMethod("protected", new SpecialMethod() {
                public void call(Ruby runtime, TypeSet receivers, Vertex[] args, Block block, Result result) {
                    RuntimeHelper.setMethodsVisibility(Graph.this, receivers, args, Visibility.PROTECTED);
                }
            });

        addSpecialMethod("public", new SpecialMethod() {
                public void call(Ruby runtime, TypeSet receivers, Vertex[] args, Block block, Result result) {
                    RuntimeHelper.setMethodsVisibility(Graph.this, receivers, args, Visibility.PUBLIC);
                }
            });

        addSpecialMethod("module_function", new SpecialMethod() {
                public void call(Ruby runtime, TypeSet receivers, Vertex[] args, Block block, Result result) {
                    RuntimeHelper.setMethodsVisibility(Graph.this, receivers, args, Visibility.MODULE_FUNCTION);
                }
            });

        addSpecialMethod("attr", new SpecialMethod() {
                public void call(Ruby runtime, TypeSet receivers, Vertex[] args, Block block, Result result) {
                    if (args != null && args.length > 0) {
                        RuntimeHelper.defineAttrs(Graph.this, receivers, new Vertex[] { args[0] }, true, args.length > 1);
                    }
                }
            });

        addSpecialMethod("attr_reader", new SpecialMethod() {
                public void call(Ruby runtime, TypeSet receivers, Vertex[] args, Block block, Result result) {
                    RuntimeHelper.defineAttrs(Graph.this, receivers, args, true, false);
                }
            });

        addSpecialMethod("attr_writer", new SpecialMethod() {
                public void call(Ruby runtime, TypeSet receivers, Vertex[] args, Block block, Result result) {
                    RuntimeHelper.defineAttrs(Graph.this, receivers, args, false, true);
                }
            });

        addSpecialMethod("attr_accessor", new SpecialMethod() {
                public void call(Ruby runtime, TypeSet receivers, Vertex[] args, Block block, Result result) {
                    RuntimeHelper.defineAttrs(Graph.this, receivers, args, true, true);
                }
            });

        addSpecialMethod("alias_method", new SpecialMethod() {
                public void call(Ruby runtime, TypeSet receivers, Vertex[] args, Block block, Result result) {
                    boolean callNextMethod = true;
                    if (args != null && args.length == 2) {
                        for (IRubyObject receiver : receivers) {
                            if (receiver instanceof RubyModule) {
                                callNextMethod = false;
                                String newName = Vertex.getStringOrSymbol(args[0]);
                                String oldName = Vertex.getStringOrSymbol(args[1]);
                                if (newName != null && oldName != null) {
                                    RubyModule module = (RubyModule) receiver;
                                    DynamicMethod method = module.getMethod(oldName);
                                    if (method instanceof Method)
                                        module.addMethod(newName, new AliasMethod(newName, (Method) method));
                                }
                            }
                        }
                    }
                    result.setCallNextMethod(callNextMethod);
View Full Code Here

    public IRubyObject newInstanceOf(RubyClass klass) {
        return runtime.newInstance(klass);
    }

    public Object visitAliasNode(AliasNode node) {
        RubyModule module = context.getFrameModule();
        DynamicMethod method = module.getMethod(node.getOldName());
        if (method instanceof Method)
            module.addMethod(node.getNewName(), new AliasMethod(node.getNewName(), (Method) method));
        return Vertex.EMPTY;
    }
View Full Code Here

    }
   
    public Object visitClassNode(ClassNode node) {
        Colon3Node cpath = node.getCPath();
        String name = cpath.getName();
        RubyModule module = RuntimeHelper.getNamespace(this, cpath);
        if (module == null) {
            Logger.error(SourceLocation.of(node), "namespace unresolved: %s", cpath);
            return Vertex.EMPTY;
        }

        RubyClass superClass = null;
        if (node.getSuperNode() != null) {
            Vertex v = createVertex(node.getSuperNode());
            if (v != null) {
                IRubyObject superObj = v.singleType();
                if (superObj instanceof RubyClass) {
                    superClass = (RubyClass) superObj;
                }
            }
            if (superClass == null) {
                Logger.error("superclass not found: %s", cpath.getName());
            }
        }

        RubyModule klass = module.defineOrGetClassUnder(name, superClass, SourceLocation.of(node));

        if (klass != null) {
            context.pushFrame(klass, name, klass, null, Visibility.PUBLIC);
            context.pushScope(new LocalScope(klass));
View Full Code Here

TOP

Related Classes of org.cx4a.rsense.ruby.RubyModule

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.