Advertisement
Guest User

Jruby behavior nodes

a guest
Nov 28th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.53 KB | None | 0 0
  1. package org.jruby.truffle.core.behavior;
  2.  
  3.  
  4. import com.oracle.truffle.api.CompilerDirectives;
  5. import com.oracle.truffle.api.dsl.Specialization;
  6. import com.oracle.truffle.api.frame.VirtualFrame;
  7. import com.oracle.truffle.api.nodes.Node;
  8. import com.oracle.truffle.api.source.SourceSection;
  9. import org.jruby.truffle.builtins.CoreClass;
  10. import org.jruby.truffle.builtins.CoreMethod;
  11. import org.jruby.truffle.builtins.CoreMethodNode;
  12. import org.jruby.truffle.builtins.UnaryCoreMethodNode;
  13. import org.jruby.truffle.core.*;
  14. import org.jruby.truffle.core.behavior.init.InitBehaviorExpression;
  15. import org.jruby.truffle.core.behavior.init.InitFold;
  16. import org.jruby.truffle.core.behavior.utility.BehaviorOption;
  17. import org.jruby.truffle.core.behavior.utility.DependencyStaticScope;
  18. import org.jruby.truffle.language.NotProvided;
  19. import org.jruby.truffle.language.dispatch.CallDispatchHeadNode;
  20. import org.jruby.truffle.language.dispatch.DispatchHeadNodeFactory;
  21. import org.jruby.truffle.language.objects.WriteObjectFieldNode;
  22. import org.jruby.truffle.RubyContext;
  23. import org.jruby.RubyProc;
  24. import org.jruby.truffle.BehaviorObject;
  25. import org.jruby.truffle.language.objects.WriteObjectFieldNodeGen;
  26.  
  27. @CoreClass("BehaviorCore")
  28. public class BehaviorModule {
  29.  
  30.     @CoreMethod(names = "fold", isModuleFunction = true, required = 1, needsBlock = true  )
  31.     public abstract static class FoldExprNode extends CoreMethodNode {
  32.  
  33.         @Node.Child
  34.         DependencyStaticScope extractDeps;
  35.         @Node.Child
  36.         InitFold initFold;
  37.  
  38.         public FoldExprNode(RubyContext context, SourceSection sourceSection) {
  39.             super(context, sourceSection);
  40.             extractDeps = new DependencyStaticScope();
  41.             initFold = new InitFold(context);
  42.         }
  43.  
  44.         @Override
  45.         public Object execute(VirtualFrame frame) {
  46.             return NotProvided.INSTANCE;
  47.         }
  48.  
  49.         @Specialization
  50.         public BehaviorObject fold(VirtualFrame frame, int value, RubyProc proc){
  51.             BehaviorObject[] deps = extractDeps.execute(proc);
  52.             return initFold.execute(frame,deps,value,proc);
  53.         }
  54.         @Specialization
  55.         public BehaviorObject fold(VirtualFrame frame, double value, RubyProc proc){
  56.             BehaviorObject[] deps = extractDeps.execute(proc);
  57.             return initFold.execute(frame,deps,value,proc);        }
  58.         @Specialization
  59.         public BehaviorObject fold(VirtualFrame frame, Object value, RubyProc proc){
  60.             BehaviorObject[] deps = extractDeps.execute(proc);
  61.             return initFold.execute(frame,deps,value,proc);
  62.         }
  63.     }
  64.  
  65.     @CoreMethod(names = {"behavior","signal","map"}, isModuleFunction = true, needsBlock = true)
  66.     public abstract static class BehaviorExprNode extends UnaryCoreMethodNode {
  67.         @Child
  68.         private WriteObjectFieldNode writeSignalExpr;
  69.         @Child
  70.         InitBehaviorExpression execSignalExpr;
  71.         @Child
  72.         DependencyStaticScope extractDeps;
  73.  
  74.         public BehaviorExprNode(RubyContext context, SourceSection sourceSection) {
  75.             super(context, sourceSection);
  76.             writeSignalExpr = WriteObjectFieldNodeGen.create(BehaviorOption.SIGNAL_EXPR);
  77.             execSignalExpr = new InitBehaviorExpression(context, sourceSection);
  78.             extractDeps = new DependencyStaticScope();
  79.         }
  80.  
  81.         @Specialization
  82.         BehaviorObject map(VirtualFrame frame, RubyProc block) {
  83.             BehaviorObject self = newSignal();
  84.             BehaviorObject[] dependsOn = extractDeps.execute(block);
  85.             self.setupPropagationDep(dependsOn);
  86.             writeSignalExpr.execute(self, block);
  87.             execSignalExpr.execute(frame, self, dependsOn);
  88.             return self;
  89.         }
  90.  
  91.         @CompilerDirectives.TruffleBoundary
  92.         private BehaviorObject newSignal() {
  93.             return new BehaviorObject(getContext());
  94.             /*return (BehaviorObject) //FIXME correct?
  95.                     (new BehaviorObject.SignalRuntimeAllocator()).allocate(
  96.                             getContext(),
  97.                             getContext().getCoreLibrary().getBehaviorClass(),
  98.                             null);*/
  99.         }
  100.     }
  101.  
  102.  
  103.     @CoreMethod(names = "source", isModuleFunction = true, required = 1)
  104.     public abstract static class SourceNode extends UnaryCoreMethodNode {
  105.         @Child
  106.         CallDispatchHeadNode callInit;
  107.  
  108.  
  109.         public SourceNode(RubyContext context, SourceSection sourceSection) {
  110.             super(context, sourceSection);
  111.             callInit = DispatchHeadNodeFactory.createMethodCall(context, true);
  112.         }
  113.  
  114.         @Specialization
  115.         BehaviorObject source(VirtualFrame frame, int value) {
  116.             final BehaviorObject self = newSignal();
  117.             return (BehaviorObject) callInit.call(frame, newSignal(), "initialize", null, value);
  118.         }
  119.  
  120.         @Specialization
  121.         BehaviorObject source(VirtualFrame frame, Object value) {
  122.             final BehaviorObject self = newSignal();
  123.             return (BehaviorObject) callInit.call(frame, newSignal(), "initialize", null, value);
  124.         }
  125.  
  126.         @CompilerDirectives.TruffleBoundary
  127.         private BehaviorObject newSignal() {
  128.             return new BehaviorObject(getContext());
  129.             /* FIXME correct?
  130.             return (BehaviorObject) (new BehaviorObject.SignalRuntimeAllocator()).allocate(
  131.                     getContext(),
  132.                     getContext().getCoreLibrary().getBehaviorSourceClass(),
  133.                     null);*/
  134.         }
  135.  
  136.     }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement