OpenModuleNode.java

  1. /*
  2.  * Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved. This
  3.  * code is released under a tri EPL/GPL/LGPL license. You can use it,
  4.  * redistribute it and/or modify it under the terms of the:
  5.  *
  6.  * Eclipse Public License version 1.0
  7.  * GNU General Public License version 2
  8.  * GNU Lesser General Public License version 2.1
  9.  */
  10. package org.jruby.truffle.nodes.objects;

  11. import com.oracle.truffle.api.Truffle;
  12. import com.oracle.truffle.api.frame.VirtualFrame;
  13. import com.oracle.truffle.api.nodes.IndirectCallNode;
  14. import com.oracle.truffle.api.source.SourceSection;
  15. import org.jruby.truffle.nodes.RubyNode;
  16. import org.jruby.truffle.nodes.methods.MethodDefinitionNode;
  17. import org.jruby.truffle.runtime.LexicalScope;
  18. import org.jruby.truffle.runtime.RubyArguments;
  19. import org.jruby.truffle.runtime.RubyContext;
  20. import org.jruby.truffle.runtime.core.RubyModule;
  21. import org.jruby.truffle.runtime.methods.RubyMethod;

  22. /**
  23.  * Open a module and execute a method in it - probably to define new methods.
  24.  */
  25. public class OpenModuleNode extends RubyNode {

  26.     @Child protected RubyNode definingModule;
  27.     @Child protected MethodDefinitionNode definitionMethod;
  28.     @Child protected IndirectCallNode callModuleDefinitionNode;

  29.     public OpenModuleNode(RubyContext context, SourceSection sourceSection, RubyNode definingModule, MethodDefinitionNode definitionMethod) {
  30.         super(context, sourceSection);
  31.         this.definingModule = definingModule;
  32.         this.definitionMethod = definitionMethod;
  33.         callModuleDefinitionNode = Truffle.getRuntime().createIndirectCallNode();
  34.     }

  35.     @Override
  36.     public Object execute(VirtualFrame frame) {
  37.         notDesignedForCompilation();

  38.         // TODO(CS): cast
  39.         final RubyModule module = (RubyModule) definingModule.execute(frame);

  40.         LexicalScope lexicalScope = definitionMethod.getSharedMethodInfo().getLexicalScope();
  41.         lexicalScope.setLiveModule(module);
  42.         lexicalScope.getParent().getLiveModule().addLexicalDependent(module);

  43.         final RubyMethod definition = definitionMethod.executeMethod(frame).withDeclaringModule(module);
  44.         return callModuleDefinitionNode.call(frame, definition.getCallTarget(), RubyArguments.pack(definition, definition.getDeclarationFrame(), module, null, new Object[]{}));
  45.     }

  46. }