Package org.jruby.runtime

Examples of org.jruby.runtime.DynamicScope


    public KeywordRestArgNode getKeyRest() {
        return keyRest;
    }

    public void prepare(ThreadContext context, Ruby runtime, IRubyObject self, IRubyObject[] args, Block block) {
        DynamicScope scope = context.getCurrentScope();

        // Bind 'normal' parameter values to the local scope for this method.
        if (!hasMasgnArgs) {
            // no arg grouping, just use bulk assignment methods
            if (preCount > 0) scope.setArgValues(args, Math.min(args.length, preCount));
            if (postCount > 0 && args.length > preCount) scope.setEndArgValues(args, postIndex, Math.min(args.length - preCount, postCount));
        } else {
            masgnAwareArgAssign(context, runtime, self, args, block, scope);
        }

        // optArgs and restArgs require more work, so isolate them and ArrayList creation here
View Full Code Here


            }
        }
    }

    public void prepare(ThreadContext context, Ruby runtime, IRubyObject self, Block block) {
        DynamicScope scope = context.getCurrentScope();

        if (isSimple) {
            scope.setArgValues();
        } else {
            prepare(context, runtime, self, IRubyObject.NULL_ARRAY, block);
        }
        if (getBlock() != null) processBlockArg(scope, runtime, block);
    }
View Full Code Here

            prepare(context, runtime, self, IRubyObject.NULL_ARRAY, block);
        }
        if (getBlock() != null) processBlockArg(scope, runtime, block);
    }
    public void prepare(ThreadContext context, Ruby runtime, IRubyObject self, IRubyObject arg0, Block block) {
        DynamicScope scope = context.getCurrentScope();

        if (isSimple) {
            scope.setArgValues(arg0);
        } else {
            prepare(context, runtime, self, new IRubyObject[] {arg0}, block);
        }
        if (getBlock() != null) processBlockArg(scope, runtime, block);
    }
View Full Code Here

    public static IRubyObject convert(ThreadContext context, IRubyObject recv, IRubyObject a1, IRubyObject a2) {
        return convertCommon(context, recv, a1, a2);
    }
   
    private static IRubyObject convertCommon(ThreadContext context, IRubyObject recv, IRubyObject a1, IRubyObject a2) {
        DynamicScope scope = context.getCurrentScope();
        IRubyObject backref = scope.getBackRef(context.runtime);
        if (backref instanceof RubyMatchData) ((RubyMatchData)backref).use();

        if (a1 instanceof RubyString) a1 = str_to_c_strict(context, a1);
        if (a2 instanceof RubyString) a2 = str_to_c_strict(context, a2);

        scope.setBackRef(backref);

        if (a1 instanceof RubyComplex) {
            RubyComplex a1Complex = (RubyComplex)a1;
            if (k_exact_p(a1Complex.image) && f_zero_p(context, a1Complex.image)) {
                a1 = a1Complex.real;
View Full Code Here

     *
     */
    @JRubyMethod
    public IRubyObject to_proc(ThreadContext context, Block unusedBlock) {
        Ruby runtime = context.runtime;
        DynamicScope currentScope = context.getCurrentScope();
        MethodBlock mb = new MethodBlock(this, currentScope.getStaticScope()) {
            @Override
            public IRubyObject callback(IRubyObject value, IRubyObject method, IRubyObject self, Block block) {
                return bmcall(value, method, self, block);
            }
        };
View Full Code Here

     * @param script The scriptlet to run
     * @returns The result of the eval
     */
    public IRubyObject evalScriptlet(String script) {
        ThreadContext context = getCurrentContext();
        DynamicScope currentScope = context.getCurrentScope();
        ManyVarsDynamicScope newScope = new ManyVarsDynamicScope(getStaticScopeFactory().newEvalScope(currentScope.getStaticScope()), currentScope);

        return evalScriptlet(script, newScope);
    }
View Full Code Here

        int n   = instrs.length;
        int ipc = 0;
        Instr instr = null;
        Object exception = null;
        Ruby runtime = context.runtime;
        DynamicScope currDynScope = context.getCurrentScope();

        // Set up thread-poll counter for this scope
        Counter tpCount = null;
        if (profile) {
            tpCount = scopeThreadPollCounts.get(scope);
            if (tpCount == null) {
                tpCount = new Counter();
                scopeThreadPollCounts.put(scope, tpCount);
            }
        }

        // Enter the looooop!
        while (ipc < n) {
            instr = instrs[ipc];
            Operation operation = instr.getOperation();

            if (debug) {
                LOG.info("I: {}", instr);
               interpInstrsCount++;
            } else if (profile) {
                if (operation.modifiesCode()) codeModificationsCount++;
               interpInstrsCount++;
            }

            try {
                Variable resultVar = null;
                Object result = null;
                switch(operation) {

                // ----------- Control-transfer instructions -------------
                case JUMP: {
                    ipc = ((JumpInstr)instr).getJumpTarget().getTargetPC();
                    break;
                }
                case MODULE_GUARD:
                case JUMP_INDIRECT:
                case B_TRUE:
                case B_FALSE:
                case B_NIL:
                case B_UNDEF:
                case BEQ:
                case BNE: {
                    ipc = instr.interpretAndGetNewIPC(context, currDynScope, self, temp, ipc);
                    break;
                }

                // ------------- Arg-receive instructions ------------
                case RECV_PRE_REQD_ARG: {
                    ReceivePreReqdArgInstr ra = (ReceivePreReqdArgInstr)instr;
                    int argIndex = ra.getArgIndex();
                    result = (argIndex < args.length) ? args[argIndex] : context.nil; // SSS FIXME: This check is only required for closures, not methods
                    resultVar = ra.getResult();
                    ipc++;
                    break;
                }
                case RECV_POST_REQD_ARG: {
                    ReceivePostReqdArgInstr ra = (ReceivePostReqdArgInstr)instr;
                    result = ra.receivePostReqdArg(args);
                    if (result == null) result = context.nil; // For blocks
                    resultVar = ra.getResult();
                    ipc++;
                    break;
                }
                case RECV_OPT_ARG: {
                    ReceiveOptArgBase ra = (ReceiveOptArgBase)instr;
                    result = ra.receiveOptArg(args);
                    resultVar = ra.getResult();
                    ipc++;
                    break;
                }
                case RECV_REST_ARG: {
                    ReceiveRestArgBase ra = (ReceiveRestArgBase)instr;
                    result = ra.receiveRestArg(runtime, args);
                    resultVar = ra.getResult();
                    ipc++;
                    break;
                }
                case RECV_CLOSURE: {
                    result = block == Block.NULL_BLOCK ? context.nil : runtime.newProc(Block.Type.PROC, block);
                    resultVar = ((ResultInstr)instr).getResult();
                    ipc++;
                    break;
                }
                case RECV_EXCEPTION: {
                    ReceiveExceptionInstr rei = (ReceiveExceptionInstr)instr;
                    result = (exception instanceof RaiseException && rei.checkType) ? ((RaiseException)exception).getException() : exception;
                    resultVar = rei.getResult();
                    ipc++;
                    break;
                }

                // --------- Return flavored instructions --------
                case BREAK: {
                    BreakInstr bi = (BreakInstr)instr;
                    IRubyObject rv = (IRubyObject)bi.getReturnValue().retrieve(context, self, currDynScope, temp);
                    // This also handles breaks in lambdas -- by converting them to a return
                    return IRRuntimeHelpers.initiateBreak(context, scope, bi.getScopeToReturnTo(), rv, blockType);
                }
                case RETURN: {
                    return (IRubyObject)((ReturnBase)instr).getReturnValue().retrieve(context, self, currDynScope, temp);
                }
                case NONLOCAL_RETURN: {
                    NonlocalReturnInstr ri = (NonlocalReturnInstr)instr;
                    IRubyObject rv = (IRubyObject)ri.getReturnValue().retrieve(context, self, currDynScope, temp);
                    ipc = n;
                    // If not in a lambda, check if this was a non-local return
                    if (!IRRuntimeHelpers.inLambda(blockType)) {
                        IRRuntimeHelpers.initiateNonLocalReturn(context, scope, ri.methodToReturnFrom, rv);
                    }
                    return rv;
                }

                // --------- Bookkeeping instructions --------
                case CHECK_ARITY: {
                    ((CheckArityInstr)instr).checkArity(runtime, args.length);
                    ipc++;
                    break;
                }
                case PUSH_FRAME: {
                    context.preMethodFrameAndClass(implClass, scope.getName(), self, block, scope.getStaticScope());
                    context.setCurrentVisibility(visibility);
                    ipc++;
                    break;
                }
                case PUSH_BINDING: {
                    // SSS NOTE: Method scopes only!
                    //
                    // Blocks are a headache -- so, these instrs. are only added to IRMethods.
                    // Blocks have more complicated logic for pushing a dynamic scope (see InterpretedIRBlockBody)
                    currDynScope = DynamicScope.newDynamicScope(scope.getStaticScope());
                    context.pushScope(currDynScope);
                    ipc++;
                    break;
                }
                case POP_FRAME: {
                    context.popFrame();
                    context.popRubyClass();
                    ipc++;
                    break;
                }
                case POP_BINDING: {
                    context.popScope();
                    ipc++;
                    break;
                }
                case THREAD_POLL: {
                    if (profile) {
                        tpCount.count++;
                        globalThreadPollCount++;
                        // SSS: Uncomment this to analyze profile
                        // Every 10K profile counts, spit out profile stats
                        // if (globalThreadPollCount % 10000 == 0) analyzeProfile(); //outputProfileStats();
                    }
                    context.callThreadPoll();
                    ipc++;
                    break;
                }
                case LINE_NUM: {
                    context.setLine(((LineNumberInstr)instr).lineNumber);
                    ipc++;
                    break;
                }
                case RUNTIME_HELPER: {
                    ipc++;
                    resultVar = ((ResultInstr)instr).getResult();
                    result = ((RuntimeHelperCall)instr).callHelper(context, currDynScope, self, temp, scope, blockType);
                    break;
                }

                // ---------- Common instruction ---------
                case COPY: {
                    CopyInstr c = (CopyInstr)instr;
                    result = c.getSource().retrieve(context, self, currDynScope, temp);
                    resultVar = ((ResultInstr)instr).getResult();
                    ipc++;
                    break;
                }

                // ---------- All the rest ---------
                default:
                    ipc++;
                    if (instr instanceof ResultInstr) resultVar = ((ResultInstr)instr).getResult();
                    result = instr.interpret(context, currDynScope, self, temp, block);
                    break;
                }

                if (resultVar != null) {
                    if (resultVar instanceof TemporaryVariable) {
                        temp[((TemporaryVariable)resultVar).offset] = result;
                    }
                    else {
                        LocalVariable lv = (LocalVariable)resultVar;
                        currDynScope.setValue((IRubyObject) result, lv.getLocation(), lv.getScopeDepth());
                    }
                }
            } catch (Throwable t) {
                // Unrescuable:
                //    IRReturnJump, ThreadKill, RubyContinuation, MainExitException, etc.
View Full Code Here

        int slen = value.getRealSize();
        final int range = begin + slen;
        byte[]bytes = value.getUnsafeBytes();
        final Matcher matcher = prepared.matcher(bytes, begin, range);

        final DynamicScope scope = context.getCurrentScope();
        int beg = matcher.search(begin, range, Option.NONE);
        if (beg < 0) {
            scope.setBackRef(runtime.getNil());
            return bang ? runtime.getNil() : strDup(runtime); /* bang: true, no match, no substitution */
        }

        RubyString dest = new RubyString(runtime, getMetaClass(), new ByteList(slen + 30));
        int offset = 0, cp = begin;
        Encoding enc = value.getEncoding();
        dest.setEncoding(enc);
        dest.setCodeRange(enc.isAsciiCompatible() ? CR_7BIT : CR_VALID);

        RubyMatchData match = null;
        do {
            final RubyString val;
            int begz = matcher.getBegin();
            int endz = matcher.getEnd();

            if (repl != null) {     // string given
                val = RubyRegexp.regsub19(repl, this, matcher, pattern);
            } else {
                final RubyString substr = makeShared19(runtime, begz, endz - begz)
                if (hash != null) { // hash given
                    val = objAsString(context, hash.op_aref(context, substr));
                } else {            // block given
                    match = RubyRegexp.updateBackRef19(context, this, scope, matcher, pattern);
                    match.regexp = regexp;
                    val = objAsString(context, block.yield(context, substr));
                }
                modifyCheck(bytes, slen, enc);
                if (bang) frozenCheck();
            }

            tuFlags |= val.flags;

            int len = beg - offset;
            if (len != 0) dest.cat(bytes, cp, len, enc);
            dest.cat19(val);
            offset = endz;
            if (begz == endz) {
                if (slen <= endz) break;
                len = StringSupport.length(enc, bytes, begin + endz, range);
                dest.cat(bytes, begin + endz, len, enc);
                offset = endz + len;
            }
            cp = begin + offset;
            if (offset > slen) break;
            beg = matcher.search(cp, range, Option.NONE);
        } while (beg >= 0);

        if (slen > offset) dest.cat(bytes, cp, slen - offset, enc);

        if (match != null) { // block given
            scope.setBackRef(match);
        } else {
            match = RubyRegexp.updateBackRef19(context, this, scope, matcher, pattern);
            match.regexp = regexp;
        }

View Full Code Here

    private IRubyObject scanIter(ThreadContext context, Regex pattern, Matcher matcher, Encoding enc, Block block, int begin, int range, int tuFlags) {
        Ruby runtime = context.runtime;
        byte[]bytes = value.getUnsafeBytes();
        int size = value.getRealSize();
        RubyMatchData match = null;
        DynamicScope scope = context.getCurrentScope();

        int end = 0;
        if (pattern.numberOfCaptures() == 0) {
            while (matcher.search(begin + end, range, Option.NONE) >= 0) {
                end = positionEnd(matcher, enc, begin, range);
                match = RubyRegexp.updateBackRef(context, this, scope, matcher, pattern);
                RubyString substr = makeShared(runtime, matcher.getBegin(), matcher.getEnd() - matcher.getBegin());
                substr.infectBy(tuFlags);
                match.infectBy(tuFlags);
                block.yield(context, substr);
                modifyCheck(bytes, size);
            }
        } else {
            while (matcher.search(begin + end, range, Option.NONE) >= 0) {
                end = positionEnd(matcher, enc, begin, range);
                match = RubyRegexp.updateBackRef(context, this, scope, matcher, pattern);
                match.infectBy(tuFlags);
                block.yield(context, populateCapturesForScan(runtime, matcher, range, tuFlags, false));
                modifyCheck(bytes, size);
            }
        }
        scope.setBackRef(match == null ? runtime.getNil() : match);
        return this;
    }
View Full Code Here

                end = positionEnd(matcher, enc, begin, range);
                ary.append(populateCapturesForScan(runtime, matcher, range, tuFlags, false));
            }
        }

        DynamicScope scope = context.getCurrentScope();
        if (ary.size() > 0) {
            RubyMatchData match = RubyRegexp.updateBackRef(context, this, scope, matcher, pattern);
            match.infectBy(tuFlags);
        } else {
            scope.setBackRef(runtime.getNil());
        }
        return ary;
    }
View Full Code Here

TOP

Related Classes of org.jruby.runtime.DynamicScope

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.