Package org.jruby.runtime

Examples of org.jruby.runtime.DynamicScope


     * @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(new EvalStaticScope(currentScope.getStaticScope()), currentScope);

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


     *
     */
    @JRubyMethod
    public IRubyObject to_proc(ThreadContext context, Block unusedBlock) {
        Ruby runtime = context.getRuntime();
        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

            String[] names = new String[paramNames.size()];
            paramNames.toArray(names);
            context.preBsfApply(names);

            // Populate values for the parameter names
            DynamicScope scope = context.getCurrentScope();
            for (int i = 0, size = args.size(); i < size; i++) {
                scope.setValue(i, JavaEmbedUtils.javaToRuby(runtime, args.get(i)), 0);
            }

            return JavaEmbedUtils.rubyToJava(evaler.parse(runtime, funcBody.toString(), file, line).run());
        } catch (StackOverflowError soe) {
            throw context.getRuntime().newSystemStackError("stack level too deep", soe);
View Full Code Here

    private static void preLoadCommon(ThreadContext context, StaticScope staticScope) {
        RubyClass objectClass = context.getRuntime().getObject();
        IRubyObject topLevel = context.getRuntime().getTopSelf();
        staticScope.setModule(objectClass);
        DynamicScope scope = DynamicScope.newDynamicScope(staticScope);

        // Each root node has a top-level scope that we need to push
        context.preScopedBody(scope);
        context.preNodeEval(objectClass, topLevel);
    }
View Full Code Here

        }
        return scopeOffsets;
    }

    public static IRubyObject match2AndUpdateScope(IRubyObject receiver, ThreadContext context, IRubyObject value, String scopeOffsets) {
        DynamicScope scope = context.getCurrentScope();
        IRubyObject match = ((RubyRegexp)receiver).op_match(context, value);
        updateScopeWithCaptures(context, scope, decodeCaptureOffsets(scopeOffsets), value);
        return match;
    }
View Full Code Here

    private IRubyObject subBangNoIter19(Ruby runtime, ThreadContext context, Regex pattern, Regex prepared, RubyString repl, RubyRegexp regexp) {
        int begin = value.getBegin();
        int range = begin + value.getRealSize();
        final Matcher matcher = prepared.matcher(value.getUnsafeBytes(), begin, range);

        DynamicScope scope = context.getCurrentScope();
        if (matcher.search(begin, range, Option.NONE) >= 0) {
            repl = RubyRegexp.regsub19(repl, this, matcher, pattern);
            RubyMatchData match = RubyRegexp.updateBackRef19(context, this, scope, matcher, pattern);
            match.regexp = regexp;
            return subBangCommon19(context, pattern, matcher, repl, repl.flags);
        } else {
            return scope.setBackRef(runtime.getNil());
        }
    }
View Full Code Here

        return gsubCommon(context, bang, arg0, block, repl, repl.flags);
    }

    private IRubyObject gsubCommon(ThreadContext context, final boolean bang, IRubyObject arg, Block block, RubyString repl, int tuFlags) {
        Ruby runtime = context.getRuntime();
        DynamicScope scope = context.getCurrentScope();
        Regex pattern = getQuotedPattern(arg);

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

        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 */
        } else if (repl == null && bang && isFrozen()) {
            throw getRuntime().newRuntimeError("can't modify frozen string");
        }

        int blen = slen + 30; /* len + margin */
        ByteList dest = new ByteList(blen);
        dest.setRealSize(blen);
        int offset = 0, buf = 0, bp = 0, cp = begin;

        Encoding enc = getEncodingForKCodeDefault(runtime, pattern, arg);

        RubyMatchData match = null;
        while (beg >= 0) {
            final RubyString val;
            final int begz = matcher.getBegin();
            final int endz = matcher.getEnd();

            if (repl == null) { // block given
                match = RubyRegexp.updateBackRef(context, this, scope, matcher, pattern);
                val = objAsString(context, block.yield(context, substr(runtime, begz, endz - begz)));
                modifyCheck(bytes, slen);
                if (bang) frozenCheck();
            } else {
                val = RubyRegexp.regsub(repl, this, matcher, enc);
            }

            tuFlags |= val.flags;

            ByteList vbuf = val.value;
            int len = (bp - buf) + (beg - offset) + vbuf.getRealSize() + 3;
            if (blen < len) {
                while (blen < len) blen <<= 1;
                len = bp - buf;
                dest.realloc(blen);
                dest.setRealSize(blen);
                bp = buf + len;
            }
            len = beg - offset; /* copy pre-match substr */
            System.arraycopy(bytes, cp, dest.getUnsafeBytes(), bp, len);
            bp += len;
            System.arraycopy(vbuf.getUnsafeBytes(), vbuf.getBegin(), dest.getUnsafeBytes(), bp, vbuf.getRealSize());
            bp += vbuf.getRealSize();
            offset = endz;

            if (begz == endz) {
                if (slen <= endz) break;
                len = enc.length(bytes, begin + endz, range);
                System.arraycopy(bytes, begin + endz, dest.getUnsafeBytes(), bp, len);
                bp += len;
                offset = endz + len;
            }
            cp = begin + offset;
            if (offset > slen) break;
            beg = matcher.search(cp, range, Option.NONE);
        }

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

        if (slen > offset) {
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();

        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.getRuntime();
        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.