Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   ***** BEGIN LICENSE BLOCK *****
   * Version: CPL 1.0/GPL 2.0/LGPL 2.1
   *
   * The contents of this file are subject to the Common 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/cpl-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 CPL, 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 CPL, the GPL or the LGPL.
  ***** END LICENSE BLOCK *****/
 
 package org.jruby.compiler.impl;
 
 import  org.objectweb.asm.Label;
 
 import static org.jruby.util.CodegenUtils.*;

Author(s):
headius
 
     public InvokeDynamicInvocationCompiler(BaseBodyCompiler methodCompilerSkinnyMethodAdapter method) {
         super(methodCompilermethod);
     }
 
     @Override
     public void invokeAttrAssign(String nameCompilerCallback receiverCallbackArgumentsCallback argsCallbackboolean selfCallboolean expr) {
         .loadThreadContext(); // [adapter, tc]
         
         // for visibility checking without requiring frame self
         // TODO: don't bother passing when fcall or vcall, and adjust callsite appropriately
         .loadSelf();
 
         if (receiverCallback != null) {
             receiverCallback.call();
         } else {
             .loadSelf();
         }
 
         String signature;
 
         argsCallback.call();
         // with args, no block
         switch (argsCallback.getArity()) {
         case 1:
             signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classIRubyObject.class));
             break;
         case 2:
             signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classIRubyObject.classIRubyObject.class));
             break;
         case 3:
             signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classIRubyObject.classIRubyObject.classIRubyObject.class));
             break;
         default:
             signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classIRubyObject[].class));
         }
         
         // adapter, tc, recv, args{0,1}, block{0,1}]
         .invokedynamic(
                 "attrAssign" + (selfCall ? "Self" : "") + (expr ? "Expr" : "") + ":" + JavaNameMangler.mangleMethodName(name),
                 signature,
                 InvokeDynamicSupport.getInvocationHandle(),
                 .getScriptCompiler().getSourcename(),
                 .getLastLine() + 1);
         
         // TODO: void invokedynamic to avoid pop
         if (!expr.pop();
     }
 
     @Override
    public void invokeDynamic(String nameCompilerCallback receiverCallbackArgumentsCallback argsCallbackCallType callTypeCompilerCallback closureArgboolean iterator) {
        if (callType == .) {
            super.invokeDynamic(namereceiverCallbackargsCallbackcallTypeclosureArgiterator);
            return;
        }
        
        .loadThreadContext(); // [adapter, tc]
        
        // for visibility checking without requiring frame self
        // TODO: don't bother passing when fcall or vcall, and adjust callsite appropriately
        .loadSelf();
        if (receiverCallback != null) {
            receiverCallback.call();
        } else {
            .loadSelf();
        }
        String invokeName;
        if (iterator) {
            switch (callType) {
                case :        invokeName = "callIter"break;
                case :    invokeName = "fcallIter"break;
                case :      invokeName = "vcallIter"break;
                default:            throw new NotCompilableException("unknown call type " + callType);
            }
        } else {
            switch (callType) {
                case :        invokeName = "call"break;
                case :    invokeName = "fcall"break;
                case :      invokeName = "vcall"break;
                default:            throw new NotCompilableException("unknown call type " + callType);
            }
        }
        invokeName += ":" + JavaNameMangler.mangleMethodName(name);
        String signature;
        // args
        if (argsCallback == null) {
            // block
            if (closureArg == null) {
                // no args, no block
                signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.class));
            } else {
                // no args, with block
                closureArg.call();
                signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classBlock.class));
            }
        } else {
            argsCallback.call();
            // block
            if (closureArg == null) {
                // with args, no block
                switch (argsCallback.getArity()) {
                case 1:
                    signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classIRubyObject.class));
                    break;
                case 2:
                    signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classIRubyObject.classIRubyObject.class));
                    break;
                case 3:
                    signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classIRubyObject.classIRubyObject.classIRubyObject.class));
                    break;
                default:
                    signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classIRubyObject[].class));
                }
            } else {
                // with args, with block
                closureArg.call();
                
                switch (argsCallback.getArity()) {
                case 1:
                    signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classIRubyObject.classBlock.class));
                    break;
                case 2:
                    signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classIRubyObject.classIRubyObject.classBlock.class));
                    break;
                case 3:
                    signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classIRubyObject.classIRubyObject.classIRubyObject.classBlock.class));
                    break;
                default:
                    signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classIRubyObject[].classBlock.class));
                }
            }
        }
        
        // adapter, tc, recv, args{0,1}, block{0,1}]
        .invokedynamic(invokeName,
                signature,
                InvokeDynamicSupport.getInvocationHandle(),
                .getScriptCompiler().getSourcename(),
                .getLastLine() + 1);
    }
    public void invokeDynamicVarargs(String nameCompilerCallback receiverCallbackArgumentsCallback argsCallbackCallType callTypeCompilerCallback closureArgboolean iterator) {
        if (callType == .) {
            super.invokeDynamic(namereceiverCallbackargsCallbackcallTypeclosureArgiterator);
            return;
        }
        assert argsCallback.getArity() == -1;
        .loadThreadContext(); // [adapter, tc]
        // for visibility checking without requiring frame self
        // TODO: don't bother passing when fcall or vcall, and adjust callsite appropriately
        .loadSelf();
        if (receiverCallback != null) {
            receiverCallback.call();
        } else {
            .loadSelf();
        }
        String invokeName;
        if (iterator) {
            switch (callType) {
                case :        invokeName = "callIter"break;
                case :    invokeName = "fcallIter"break;
                default:            throw new NotCompilableException("unknown call type " + callType);
            }
        } else {
            switch (callType) {
                case :        invokeName = "call"break;
                case :    invokeName = "fcall"break;
                default:            throw new NotCompilableException("unknown call type " + callType);
            }
        }
        invokeName += ":" + JavaNameMangler.mangleMethodName(name);
        String signature;
        argsCallback.call();
        // block
        if (closureArg == null) {
            signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classIRubyObject[].class));
        } else {
            closureArg.call();
            signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.classIRubyObject[].classBlock.class));
        }
        // adapter, tc, recv, args{0,1}, block{0,1}]
        .invokedynamic(invokeName,
                signature,
                InvokeDynamicSupport.getInvocationHandle(),
                .getScriptCompiler().getSourcename(),
                .getLastLine() + 1);
    }
    @Override
    public void invokeEqq(ArgumentsCallback receiversfinal CompilerCallback argument) {
        if (argument == null) {
            super.invokeEqq(receiversargument);
        } else {
            if (receivers.getArity() == 1) {
                invokeDynamic("==="receiversnew ArgumentsCallback() {
                    public int getArity() {
                        return 1;
                    }
                    public void call(BodyCompiler context) {
                        argument.call(context);
                    }
                }, .nullfalse);
                .isTrue();
            } else {
                super.invokeEqq(receiversargument);
            }
        }
    }
    @Override
    public void yieldSpecific(ArgumentsCallback argsCallback) {
        .loadBlock();
        String signature;
        if (argsCallback == null) {
            signature = sig(IRubyObject.classBlock.classThreadContext.class);
        } else {
            argsCallback.call();
            switch (argsCallback.getArity()) {
            case 1:
                signature = sig(IRubyObject.classBlock.classThreadContext.classIRubyObject.class);
                break;
            case 2:
                signature = sig(IRubyObject.classBlock.classThreadContext.classIRubyObject.classIRubyObject.class);
                break;
            case 3:
                signature = sig(IRubyObject.classBlock.classThreadContext.classIRubyObject.classIRubyObject.classIRubyObject.class);
                break;
            default:
                throw new NotCompilableException("Can't do specific-arity call for > 3 args yet");
            }
        }
        .invokedynamic(
                "yieldSpecific",
                signature,
                InvokeDynamicSupport.getInvocationHandle(),
                .getScriptCompiler().getSourcename(),
                .getLastLine() + 1);
    }
    @Override
    public void invokeBinaryFixnumRHS(String nameCompilerCallback receiverCallbacklong fixnum) {
            super.invokeBinaryFixnumRHS(namereceiverCallbackfixnum);
            return;
        }
        
        .loadThreadContext(); // [adapter, tc]
        // for visibility checking without requiring frame self
        // TODO: don't bother passing when fcall or vcall, and adjust callsite appropriately
        .loadSelf();
        if (receiverCallback != null) {
            receiverCallback.call();
        } else {
            .loadSelf();
        }
        String signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.class));
        .invokedynamic(
                "fixnumOperator:" + JavaNameMangler.mangleMethodName(name),
                signature,
                InvokeDynamicSupport.getFixnumOperatorHandle(),
                fixnum,
                .getScriptCompiler().getSourcename(),
                .getLastLine() + 1);
    }
    @Override
    public void invokeBinaryBooleanFixnumRHS(String nameCompilerCallback receiverCallbacklong fixnum) {
            super.invokeBinaryFixnumRHS(namereceiverCallbackfixnum);
            return;
        }
        
        .loadThreadContext(); // [adapter, tc]
        // for visibility checking without requiring frame self
        // TODO: don't bother passing when fcall or vcall, and adjust callsite appropriately
        .loadSelf();
        if (receiverCallback != null) {
            receiverCallback.call();
        } else {
            .loadSelf();
        }
        String signature = sig(boolean.classparams(ThreadContext.classIRubyObject.classIRubyObject.class));
        .invokedynamic(
                "fixnumBoolean:" + JavaNameMangler.mangleMethodName(name),
                signature,
                InvokeDynamicSupport.getFixnumBooleanHandle(),
                fixnum,
                .getScriptCompiler().getSourcename(),
                .getLastLine() + 1);
    }
    
    public void invokeBinaryFloatRHS(String nameCompilerCallback receiverCallbackdouble flote) {
            super.invokeBinaryFloatRHS(namereceiverCallbackflote);
            return;
        }
        
        .loadThreadContext(); // [adapter, tc]
        // for visibility checking without requiring frame self
        // TODO: don't bother passing when fcall or vcall, and adjust callsite appropriately
        .loadSelf();
        if (receiverCallback != null) {
            receiverCallback.call();
        } else {
            .loadSelf();
        }
        String signature = sig(IRubyObject.classparams(ThreadContext.classIRubyObject.classIRubyObject.class));
        .invokedynamic(
                "floatOperator:" + JavaNameMangler.mangleMethodName(name),
                signature,
                InvokeDynamicSupport.getFloatOperatorHandle(),
                flote,
                .getScriptCompiler().getSourcename(),
                .getLastLine() + 1);
    }
    @Override
    public void invokeOpAsgnWithMethod(final String operatorNamefinal String attrNamefinal String attrAsgnNamefinal CompilerCallback receiverCallbackfinal ArgumentsCallback argsCallback) {
        final int temp = ..grabTempLocal();
        receiverCallback.call();
        ..astore(temp);
        final CompilerCallback receiver = new CompilerCallback() {
            public void call(BodyCompiler context) {
                ..aload(temp);
            }
        };
        ArgumentsCallback result = new ArgumentsCallback() {
            public void call(BodyCompiler context) {
                CompilerCallback value = new CompilerCallback() {
                    public void call(BodyCompiler context) {
                        invokeDynamic(attrNamereceivernull.nullfalse);
                    }
                };
                invokeDynamic(operatorNamevalueargsCallback.nullfalse);
            }
            public int getArity() {
                return 1;
            }
        };
        invokeAttrAssign(attrAsgnNamereceiverresultfalsetrue);
    }
    public void invokeOpAsgnWithOr(String attrNameString attrAsgnNameCompilerCallback receiverCallbackArgumentsCallback argsCallback) {
        final int temp = ..grabTempLocal();
        receiverCallback.call();
        ..astore(temp);
        final CompilerCallback receiver = new CompilerCallback() {
            public void call(BodyCompiler context) {
                ..aload(temp);
            }
        };
        invokeDynamic(attrNamereceivernull.nullfalse);
        Label done = new Label();
        Label isTrue = new Label();
        .dup();
        .invokeIRubyObject("isTrue"sig(boolean.class));
        .iftrue(done);
        .pop(); // pop extra attr value
        invokeAttrAssign(attrAsgnNamereceiverargsCallbackfalsetrue);
        .label(done);
    }
    public void invokeOpAsgnWithAnd(String attrNameString attrAsgnNameCompilerCallback receiverCallbackArgumentsCallback argsCallback) {
        final int temp = ..grabTempLocal();
        receiverCallback.call();
        ..astore(temp);
        final CompilerCallback receiver = new CompilerCallback() {
            public void call(BodyCompiler context) {
                ..aload(temp);
            }
        };
        invokeDynamic(attrNamereceivernull.nullfalse);
        Label done = new Label();
        .dup();
        .invokeIRubyObject("isTrue"sig(boolean.class));
        .iffalse(done);
        .pop(); // pop extra attr value
        invokeAttrAssign(attrAsgnNamereceiverargsCallbackfalsetrue);
        .label(done);
    }
New to GrepCode? Check out our FAQ X