Package org.jruby

Examples of org.jruby.RubyModule$ModuleKernelMethods


            case 'm' :
                rubyObj = RubyModule.unmarshalFrom(this);
                break;
            case 'e':
                RubySymbol moduleName = (RubySymbol) unmarshalObject();
                RubyModule tp = null;
                try {
                    tp = runtime.getClassFromPath(moduleName.asJavaString());
                } catch (RaiseException e) {
                    if (runtime.fastGetModule("NameError").isInstance(e.getException())) {
                        throw runtime.newArgumentError("undefined class/module " + moduleName.asJavaString());
                    }
                    throw e;
                }

                rubyObj = unmarshalObject();
               
                tp.extend_object(rubyObj);
                tp.callMethod(runtime.getCurrentContext(),"extended", rubyObj);
                break;
            case 'l' :
                rubyObj = RubyBignum.unmarshalFrom(this);
                break;
            case 'S' :
View Full Code Here


    }

    private IRubyObject userUnmarshal() throws IOException {
        String className = unmarshalObject().asJavaString();
        ByteList marshaled = unmarshalString();
        RubyModule classInstance = findClass(className);
        if (!classInstance.respondsTo("_load")) {
            throw runtime.newTypeError("class " + classInstance.getName() + " needs to have method `_load'");
        }
        RubyString data = RubyString.newString(getRuntime(), marshaled);
        if (ivarsWaiting) {
            defaultVariablesUnmarshal(data);
            ivarsWaiting = false;
        }
        IRubyObject result = classInstance.callMethod(getRuntime().getCurrentContext(),
            "_load", data);
        registerLinkTarget(result);
        return result;
    }
View Full Code Here

        result.callMethod(getRuntime().getCurrentContext(),"marshal_load", marshaled);
        return result;
    }

    private RubyClass findClass(String className) {
        RubyModule classInstance;
        try {
            classInstance = runtime.getClassFromPath(className);
        } catch (RaiseException e) {
            if (runtime.getModule("NameError").isInstance(e.getException())) {
                throw runtime.newArgumentError("undefined class/module " + className);
View Full Code Here

     * Just enough configuration settings (most don't make sense in Java) to run the rubytests
     * unit tests. The tests use <code>bindir</code>, <code>RUBY_INSTALL_NAME</code> and
     * <code>EXEEXT</code>.
     */
    public void load(Ruby runtime, boolean wrap) {
        RubyModule configModule = runtime.defineModule("Config");
       
        configModule.defineAnnotatedMethods(RbConfigLibrary.class);
       
        RubyHash configHash = RubyHash.newHash(runtime);
        configModule.defineConstant("CONFIG", configHash);
        runtime.getObject().defineConstant("RbConfig", configModule);

        String[] versionParts = Constants.RUBY_VERSION.split("\\.");
        setConfig(configHash, "MAJOR", versionParts[0]);
        setConfig(configHash, "MINOR", versionParts[1]);
View Full Code Here

    }


    public static void callTraceFunction(Ruby runtime, ThreadContext context, RubyEvent event) {
        String name = context.getFrameName();
        RubyModule type = context.getFrameKlazz();
        runtime.callEventHooks(context, event, context.getFile(), context.getLine(), name, type);
    }
View Full Code Here

    /* Something like cvar_cbase() from eval.c, factored out for the benefit
     * of all the classvar-related node evaluations */
    public static RubyModule getClassVariableBase(ThreadContext context, Ruby runtime) {
        StaticScope scope = context.getCurrentScope().getStaticScope();
        RubyModule rubyClass = scope.getModule();
        if (rubyClass.isSingleton() || rubyClass == runtime.getDummy()) {
            scope = scope.getPreviousCRefScope();
            rubyClass = scope.getModule();
            if (scope.getPreviousCRefScope() == null) {
                runtime.getWarnings().warn(ID.CVAR_FROM_TOPLEVEL_SINGLETON_METHOD, "class variable access from toplevel singleton method");
            }           
View Full Code Here

            }
            case RubiniusInstructions.ADD_METHOD: {
                int val = getInt(bytecodes, ip);
                ip += 4;
                String name = literals[val].toString();
                RubyModule clzz = (RubyModule)stack[stackTop--];
                RubyArray method = (RubyArray)stack[stackTop--];
               
                Visibility visibility = context.getCurrentVisibility();
                if (name == "initialize" || visibility == Visibility.MODULE_FUNCTION) {
                    visibility = Visibility.PRIVATE;
                }
               
                RubiniusCMethod cmethod = new RubiniusCMethod(method);
               
                StaticScope staticScope = new LocalStaticScope(context.getCurrentScope().getStaticScope());
                staticScope.setVariables(new String[cmethod.locals]);
                staticScope.determineModule();

                RubiniusMethod newMethod = new RubiniusMethod(clzz, cmethod, staticScope, visibility);

                clzz.addMethod(name, newMethod);
   
                if (context.getCurrentVisibility() == Visibility.MODULE_FUNCTION) {
                    clzz.getSingletonClass().addMethod(
                            name,
                            new WrapperMethod(clzz.getSingletonClass(), newMethod,
                                    Visibility.PUBLIC));
                    clzz.callMethod(context, "singleton_method_added", literals[val]);
                }
   
                if (clzz.isSingleton()) {
                    ((MetaClass) clzz).getAttached().callMethod(
                            context, "singleton_method_added", literals[val]);
                } else {
                    clzz.callMethod(context, "method_added", literals[val]);
                }
                stack[++stackTop] = method;
                break;
            }
            case RubiniusInstructions.META_PUSH_NEG_1: {
View Full Code Here

        return EMPTY_LIST;
    }
   
    @Override
    public IRubyObject interpret(Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) {
        RubyModule module = context.getRubyClass();
  
        if (module == null) {
            throw runtime.newTypeError("No class to undef method '" + name + "'.");
        }
       
        module.undef(context, name);
  
        return runtime.getNil();       
    }
View Full Code Here

                break;
            case YARVInstructions.SETINSTANCEVARIABLE:
                self.getInstanceVariables().fastSetInstanceVariable(bytecodes[ip].s_op0, pop());
                break;
            case YARVInstructions.GETCLASSVARIABLE: {
                RubyModule rubyClass = context.getRubyClass();
                String name = bytecodes[ip].s_op0;
   
                if (rubyClass == null) {
                    push(self.getMetaClass().fastGetClassVar(name));
                } else if (!rubyClass.isSingleton()) {
                    push(rubyClass.fastGetClassVar(name));
                } else {
                    RubyModule module = (RubyModule)(((MetaClass)rubyClass).getAttached());

                    if (module != null) {
                        push(module.fastGetClassVar(name));
                    } else {
                        push(runtime.getNil());
                    }
                }
                break;
            }
            case YARVInstructions.SETCLASSVARIABLE: {
                RubyModule rubyClass = context.getCurrentScope().getStaticScope().getModule();
   
                if (rubyClass == null) {
                    rubyClass = self.getMetaClass();
                } else if (rubyClass.isSingleton()) {
                    rubyClass = (RubyModule)(((MetaClass)rubyClass).getAttached());
                }
   
                rubyClass.fastSetClassVar(bytecodes[ip].s_op0, pop());
                break;
            }
            case YARVInstructions.GETCONSTANT:
                push(context.getConstant(bytecodes[ip].s_op0));
                break;
            case YARVInstructions.SETCONSTANT:
                context.setConstantInCurrent(bytecodes[ip].s_op0, pop());
                runtime.incGlobalState();
                break;
            case YARVInstructions.PUTNIL:
                push(context.getRuntime().getNil());
                break;
            case YARVInstructions.PUTSELF:
                push(self);
                break;
            case YARVInstructions.PUTOBJECT:
                //System.out.println("PUTOBJECT: " + bytecodes[ip].o_op0);
                push(bytecodes[ip].o_op0);
                break;
            case YARVInstructions.PUTSTRING:
                push(context.getRuntime().newString(bytecodes[ip].s_op0));
                break;
            case YARVInstructions.CONCATSTRINGS: {
                StringBuilder concatter = new StringBuilder();
               
                for (int i = 0; i < bytecodes[ip].l_op0; i++) {
                    concatter.append(pop().toString());
                }
               
                push(runtime.newString(concatter.toString()));
                break;
            }
            case YARVInstructions.TOSTRING:
                IRubyObject top = peek();
                if (!(top instanceof RubyString)) {
                    set(top.callMethod(context, "to_s"));
                }
                break;
            case YARVInstructions.NEWARRAY:
                push(runtime.newArrayNoCopy(popArray(new IRubyObject[(int) bytecodes[ip].l_op0])));
                break;
            case YARVInstructions.DUPARRAY:
                push(bytecodes[ip].o_op0.dup());
                break;
            case YARVInstructions.NEWHASH:
                int hsize = (int)bytecodes[ip].l_op0;
                RubyHash h = RubyHash.newHash(runtime);
                IRubyObject v,k;
                for(int i = hsize; i>0; i -= 2) {
                    v = pop();
                    k = pop();
                    h.op_aset(context, k, v);
                }
                push(h);
                break;
            case YARVInstructions.PUTNOT:
                push(peek().isTrue() ? runtime.getFalse() : runtime.getTrue());
                break;
            case YARVInstructions.POP:
                pop();
                break;
            case YARVInstructions.DUP:
                push(peek());
                break;
            case YARVInstructions.DUPN:
                dupn((int) bytecodes[ip].l_op0);
                break;
            case YARVInstructions.SWAP:
                swap();
                break;
            case YARVInstructions.TOPN:
                topn((int) bytecodes[ip].l_op0);
                break;
            case YARVInstructions.SETN:
                setn((int) bytecodes[ip].l_op0, peek());
                break;
            case YARVInstructions.EMPTSTACK:
                stackTop = stackStart;
                break;
            case YARVInstructions.DEFINEMETHOD:
                RubyModule containingClass = context.getRubyClass();
   
                if (containingClass == null) {
                    throw runtime.newTypeError("No class to add method.");
                }

                String mname = bytecodes[ip].iseq_op.name;
                if (containingClass == runtime.getObject() && mname == "initialize") {
                    runtime.getWarnings().warn(ID.REDEFINING_DANGEROUS, "redefining Object#initialize may cause infinite loop", "Object#initialize");
                }
   
                Visibility visibility = context.getCurrentVisibility();
                if (mname == "initialize" || visibility == Visibility.MODULE_FUNCTION) {
                    visibility = Visibility.PRIVATE;
                }
               
                if (containingClass.isSingleton()) {
                    IRubyObject attachedObject = ((MetaClass) containingClass).getAttached();
                   
                    if (attachedObject instanceof RubyFixnum || attachedObject instanceof RubySymbol) {
                        throw runtime.newTypeError("can't define singleton method \"" +
                                mname + "\" for " + attachedObject.getType());
                    }
                }

                StaticScope sco = new LocalStaticScope(null);
                sco.setVariables(bytecodes[ip].iseq_op.locals);
                YARVMethod newMethod = new YARVMethod(containingClass, bytecodes[ip].iseq_op, sco, visibility);

                containingClass.addMethod(mname, newMethod);
   
                if (context.getCurrentVisibility() == Visibility.MODULE_FUNCTION) {
                    RubyModule singleton = containingClass.getSingletonClass();
                    singleton.addMethod(mname, new WrapperMethod(singleton, newMethod, Visibility.PUBLIC));
                    containingClass.callMethod(context, "singleton_method_added", runtime.fastNewSymbol(mname));
                }
   
                // 'class << state.self' and 'class << obj' uses defn as opposed to defs
                if (containingClass.isSingleton()) {
View Full Code Here

    }
   
    @Override
    public String definition(Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) {
        try {
            RubyModule left = runtime.getObject();

            if (hasConstant(left)) {
                return "constant";
            } else if (hasMethod(left)) {
                return "method";
View Full Code Here

TOP

Related Classes of org.jruby.RubyModule$ModuleKernelMethods

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.