Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   ***** BEGIN LICENSE BLOCK *****
   * Version: EPL 1.0/GPL 2.0/LGPL 2.1
   *
   * The contents of this file are subject to the Eclipse Public
   * License Version 1.0 (the "License"); you may not use this file
   * except in compliance with the License. You may obtain a copy of
   * the License at http://www.eclipse.org/legal/epl-v10.html
   *
  * Software distributed under the License is distributed on an "AS
  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  * implied. See the License for the specific language governing
  * rights and limitations under the License.
  * 
  * Alternatively, the contents of this file may be used under the terms of
  * either of the GNU General Public License Version 2 or later (the "GPL"),
  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the EPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the EPL, the GPL or the LGPL.
  ***** END LICENSE BLOCK *****/
 package org.jruby.compiler.impl;
 
 import static org.jruby.util.CodegenUtils.*;
A CacheCompiler that uses invokedynamic as a lazy thunk for literals and other invokedynamic features like SwitchPoint to produce fast (nearly free) invalidatable caches for things like constant lookup.
 
     public InvokeDynamicCacheCompiler(StandardASMCompiler scriptCompiler) {
         super(scriptCompiler);
     }

    
Cache a constant reference using invokedynamic. This cache uses a java.lang.invoke.SwitchPoint as the invalidation mechanism in order to avoid the cost of constantly pinging a constant generation in org.jruby.Ruby. This allows a nearly free constant cache.

Parameters:
method the method compiler with which bytecode is emitted
constantName the name of the constant to look up
 
     @Override
     public void cacheConstant(BaseBodyCompiler methodString constantName) {
         if (!.) {
             super.cacheConstant(methodconstantName);
             return;
         }
 
         method.loadThis();
         method.loadThreadContext();
         method.method.invokedynamic(
                 constantName,
                 sig(IRubyObject.classAbstractScript.classThreadContext.class),
                 InvokeDynamicSupport.getConstantHandle(),
                 method.getScopeIndex());
     }

    
Cache a constant boolean using invokedynamic. This cache uses a java.lang.invoke.SwitchPoint as the invalidation mechanism in order to avoid the cost of constantly pinging a constant generation in org.jruby.Ruby. This allows a nearly free constant cache.

Parameters:
method the method compiler with which bytecode is emitted
constantName the name of the constant to look up
    @Override
    public void cacheConstantBoolean(BaseBodyCompiler methodString constantName) {
            super.cacheConstantBoolean(methodconstantName);
            return;
        }
        method.loadThis();
        method.loadThreadContext();
        method.method.invokedynamic(
                constantName,
                sig(boolean.classAbstractScript.classThreadContext.class),
                InvokeDynamicSupport.getConstantBooleanHandle(),
                method.getScopeIndex());
    }

    
This doesn't get used, since it's only used from cacheRegexp in superclass, and that is completely bound via invokedynamic now. Implemented here and in InvokeDynamicSupport for consistency.

Parameters:
method the method compiler with which bytecode is emitted
contents the contents of the bytelist to cache
    @Override
    public void cacheByteList(BaseBodyCompiler methodByteList contents) {
            super.cacheByteList(methodcontents);
            return;
        }
        
        String asString = Helpers.rawBytesToString(contents.bytes());
        String encodingName = new String(contents.getEncoding().getName());
        
        method.method.invokedynamic(
                "getByteList",
                sig(ByteList.class),
                InvokeDynamicSupport.getByteListHandle(),
                asString,
                encodingName);
    }

    
Cache the __ENCODING__ keyword using invokedynamic.

Parameters:
method the method compiler with which bytecode is emitted
encoding the encoding for this script
    @Override
    public void cacheRubyEncoding(BaseBodyCompiler methodEncoding encoding) {
            super.cacheRubyEncoding(methodencoding);
            return;
        }
        
        String encodingName = new String(encoding.getName());
        
        method.loadThreadContext();
        
        method.method.invokedynamic(
                "getEncoding",
                sig(RubyEncoding.classThreadContext.class),
                InvokeDynamicSupport.getEncodingHandle(),
                encodingName);
    }

    
Cache a closure body (BlockBody) using invokedynamic.
    @Override
    public int cacheClosure(BaseBodyCompiler methodString closureMethodint arityStaticScope scopeString fileint lineboolean hasMultipleArgsHeadNodeType argsNodeIdASTInspector inspector) {
        String descriptor = Helpers.buildBlockDescriptor(
                closureMethod,
                arity,
                file,
                line,
                hasMultipleArgsHead,
                argsNodeId,
                inspector);
        method.loadThis();
        method.loadThreadContext();
        int scopeIndex = cacheStaticScope(methodscope);
        
        method.method.invokedynamic(
                "getBlockBody",
                sig(BlockBody.classObject.classThreadContext.classStaticScope.class),
                InvokeDynamicSupport.getBlockBodyHandle(),
                descriptor);
        return scopeIndex;
    }

    
Cache a closure body (BlockBody) for 1.9 mode using invokedynamic.
    @Override
    public int cacheClosure19(BaseBodyCompiler methodString closureMethodint arityStaticScope scopeString fileint lineboolean hasMultipleArgsHeadNodeType argsNodeIdString parameterListASTInspector inspector) {
        String descriptor = Helpers.buildBlockDescriptor19(
                closureMethod,
                arity,
                file,
                line,
                hasMultipleArgsHead,
                argsNodeId,
                parameterList,
                inspector);
        method.loadThis();
        method.loadThreadContext();
        int scopeIndex = cacheStaticScope(methodscope);
        
        method.method.invokedynamic(
                "getBlockBody19",
                sig(BlockBody.classObject.classThreadContext.classStaticScope.class),
                InvokeDynamicSupport.getBlockBody19Handle(),
                descriptor);
        return scopeIndex;
    }

    
Cache a Regexp literal using invokedynamic.

Parameters:
method the method compiler with which bytecode is emitted
pattern the contents of the bytelist for the regexp pattern
options the regexp options
    @Override
    public void cacheRegexp(BaseBodyCompiler methodByteList patternint options) {
            super.cacheRegexp(methodpatternoptions);
            return;
        }
        
        String asString = Helpers.rawBytesToString(pattern.bytes());
        String encodingName = new String(pattern.getEncoding().getName());
        
        method.loadThreadContext();
        
        method.method.invokedynamic(
                "getRegexp",
                sig(RubyRegexp.classThreadContext.class),
                InvokeDynamicSupport.getRegexpHandle(),
                asString,
                encodingName,
                options);
    }

    
Cache a Fixnum literal using invokedynamic.

Parameters:
method the method compiler with which bytecode is emitted
value the value of the Fixnum
    @Override
    public void cacheFixnum(BaseBodyCompiler methodlong value) {
            super.cacheFixnum(methodvalue);
            return;
        }
        
        method.loadThreadContext();
        
        method.method.invokedynamic(
                "getFixnum",
                sig(RubyFixnum.classThreadContext.class),
                InvokeDynamicSupport.getFixnumHandle(),
                value);
    }

    
Cache a Float literal using invokedynamic.

Parameters:
method the method compiler with which bytecode is emitted
value the value of the Float
    @Override
    public void cacheFloat(BaseBodyCompiler methoddouble value) {
            super.cacheFloat(methodvalue);
            return;
        }
        
        method.loadThreadContext();
        
        method.method.invokedynamic(
                "getFloat",
                sig(RubyFloat.classThreadContext.class),
                InvokeDynamicSupport.getFloatHandle(),
                value);
    }

    
Cache a StaticScope using invokedynamic.

Parameters:
method the method compiler with which bytecode is emitted
scope the original scope to base the new one on
    @Override
    public int cacheStaticScope(BaseBodyCompiler methodStaticScope scope) {
        String scopeString = Helpers.encodeScope(scope);
        
        int index = ;
        ++;
        
        method.loadThis();
        method.loadThreadContext();
        method.loadStaticScope();
        
        method.method.invokedynamic(
                "getStaticScope",
                sig(StaticScope.classAbstractScript.classThreadContext.classStaticScope.class),
                InvokeDynamicSupport.getStaticScopeHandle(),
                scopeString,
                index);
        
        return index;
    }
    
    public void loadStaticScope(BaseBodyCompiler methodint index) {
        method.loadThis();
        
        method.method.invokedynamic(
                "getStaticScope",
                sig(StaticScope.classAbstractScript.class),
                InvokeDynamicSupport.getLoadStaticScopeHandle(),
                index);
    }
    
    
Cache a CallSite object using invokedynamic.

Parameters:
method the method compiler with which bytecode is emitted
name the method name the call site invokes, or null for "super"
callType the type of call
    @Override
    public void cacheCallSite(BaseBodyCompiler methodString nameCallType callType) {
        char callTypeChar = 0;
        
        switch (callType) {
            case :
                callTypeChar = 'N';
                break;
            case :
                callTypeChar = 'F';
                break;
            case :
                callTypeChar = 'V';
                break;
            case :
                callTypeChar = 'S';
                break;
        }
        
        if (name == nullname = "super";
        
        method.method.invokedynamic(
                "getCallSite",
                sig(CallSite.class),
                InvokeDynamicSupport.getCallSiteHandle(),
                name,
                callTypeChar);
    }

    
Cache a String literal using invokedynamic.

Parameters:
method the method compiler with which bytecode is emitted
contents the contents of the bytelist for the String
codeRange the code range for the String
    @Override
    public void cacheString(BaseBodyCompiler methodByteList contentsint codeRange) {
            super.cacheString(methodcontentscodeRange);
            return;
        }
        
        String asString = Helpers.rawBytesToString(contents.bytes());
        String encodingName = new String(contents.getEncoding().getName());
        
        method.loadThreadContext();
        
        method.method.invokedynamic(
                "getString",
                sig(RubyString.classThreadContext.class),
                InvokeDynamicSupport.getStringHandle(),
                asString,
                encodingName,
                codeRange);
    }

    
Cache a BigInteger using invokedynamic. Used for Bignum construction

Parameters:
method the method compiler with which bytecode is emitted
bigint the BigInteger to cache
    @Override
    public void cacheBigInteger(BaseBodyCompiler methodBigInteger bigint) {
            super.cacheBigInteger(methodbigint);
            return;
        }
        
        String asString = bigint.toString(16);
        
        method.method.invokedynamic(
                "getBigInteger",
                sig(BigInteger.class),
                InvokeDynamicSupport.getBigIntegerHandle(),
                asString);
    }
    
    
Cache the symbol for the given string using invokedynamic.

Parameters:
method the method compiler with which bytecode is emitted
symbol the string of the Symbol to cache
    public void cacheSymbol(BaseBodyCompiler methodString symbol) {
            super.cacheSymbol(methodsymbol);
            return;
        }
        
        method.loadThreadContext();
        
        method.method.invokedynamic(
                "getSymbol",
                sig(RubySymbol.classThreadContext.class),
                InvokeDynamicSupport.getSymbolHandle(),
                symbol);
    }
    public void cachedGetVariable(BaseBodyCompiler methodString name) {
        if (!.) {
            super.cachedGetVariable(methodname);
            return;
        }
        
        method.loadSelf();
        
        method.method.invokedynamic(
                "get:" + name,
                sig(IRubyObject.classIRubyObject.class),
                InvokeDynamicSupport.getVariableHandle(),
                method.getScriptCompiler().getSourcename(),
                method.getLastLine() + 1
                );
    }
    public void cachedSetVariable(BaseBodyCompiler methodString nameCompilerCallback valueCallback) {
        if (!.) {
            super.cachedSetVariable(methodnamevalueCallback);
            return;
        }
        
        method.loadSelf();
        valueCallback.call(method);
        
        method.method.invokedynamic(
                "set:" + name,
                sig(IRubyObject.classIRubyObject.classIRubyObject.class),
                InvokeDynamicSupport.getVariableHandle(),
                method.getScriptCompiler().getSourcename(),
                method.getLastLine() + 1
        );
    }
    
    public void cacheGlobal(BaseBodyCompiler methodString globalName) {
        method.loadThreadContext();
        method.method.invokedynamic(
                "get:" + JavaNameMangler.mangleMethodName(globalName),
                sig(IRubyObject.classThreadContext.class),
                InvokeDynamicSupport.getGlobalHandle(),
                method.getScriptCompiler().getSourcename(), 
                method.getLastLine() + 1);
    }
    
    public void cacheGlobalBoolean(BaseBodyCompiler methodString globalName) {
        method.loadThreadContext();
        method.method.invokedynamic(
                "getBoolean:" + JavaNameMangler.mangleMethodName(globalName),
                sig(boolean.classThreadContext.class),
                InvokeDynamicSupport.getGlobalBooleanHandle(),
                method.getScriptCompiler().getSourcename(), 
                method.getLastLine() + 1);
    }
    
    public void cacheBoolean(BaseBodyCompiler methodboolean tru) {
        method.loadThreadContext();
        method.method.invokedynamic(
                "loadBoolean:" + tru,
                sig(RubyBoolean.classThreadContext.class),
                InvokeDynamicSupport.getLoadBooleanHandle());
    }
New to GrepCode? Check out our FAQ X